|
48 | 48 | #include "generatorset.h" |
49 | 49 | #include "fileout.h" |
50 | 50 | #include "control.h" |
51 | | -#include "pp.h" |
| 51 | +#include "simplecpp.h" |
52 | 52 |
|
53 | 53 | #include <QDir> |
54 | 54 | #include <QFileInfo> |
55 | 55 | #include <QFile> |
56 | 56 | #include <QTextStream> |
57 | 57 | #include <QRegularExpression> |
58 | 58 |
|
| 59 | +#include <memory> |
| 60 | + |
59 | 61 | void displayHelp(GeneratorSet *generatorSet); |
60 | 62 |
|
61 | 63 | namespace |
@@ -125,53 +127,86 @@ namespace |
125 | 127 | } |
126 | 128 |
|
127 | 129 | bool |
128 | | - preprocess(const QString &sourceFile, const QString &targetFile, const QString &commandLineIncludes = QString()) |
| 130 | + preprocess(const QString& sourceFile, const QString& targetFile, const QString& commandLineIncludes = QString()) |
129 | 131 | { |
130 | | - rpp::pp_environment env; |
131 | | - rpp::pp preprocess(env); |
132 | | - |
133 | | - rpp::pp_null_output_iterator null_out; |
| 132 | + simplecpp::DUI dui; // settings |
134 | 133 |
|
135 | | - const char *ppconfig = ":/trolltech/generator/parser/rpp/pp-qt-configuration"; |
| 134 | + for(QString include : getIncludeDirectories(commandLineIncludes)) { |
| 135 | + dui.includePaths.push_back(QDir::toNativeSeparators(include).toStdString()); |
| 136 | + } |
| 137 | + dui.defines.push_back("__cplusplus=1"); |
| 138 | + dui.defines.push_back("__STDC__"); |
| 139 | + dui.std = "c++20"; |
| 140 | + dui.removeComments = true; |
136 | 141 |
|
137 | | - QFile file(ppconfig); |
| 142 | + QFile file(sourceFile); |
138 | 143 | if (!file.open(QFile::ReadOnly)) |
139 | 144 | { |
140 | | - fprintf(stderr, "Preprocessor configuration file not found '%s'\n", ppconfig); |
| 145 | + std::cerr << "Main file not found:" << sourceFile.toUtf8().constData() << std::endl; |
141 | 146 | return false; |
142 | 147 | } |
143 | 148 |
|
144 | 149 | QByteArray ba = file.readAll(); |
145 | 150 | file.close(); |
146 | | - preprocess.operator()(ba.constData(), ba.constData() + ba.size(), null_out); |
147 | 151 |
|
148 | | - foreach(QString |
149 | | - include, getIncludeDirectories(commandLineIncludes)) { |
150 | | - preprocess.push_include_path(QDir::toNativeSeparators(include).toStdString()); |
| 152 | + // Perform preprocessing (code originally taken from simplecpp/main.cpp and modified) |
| 153 | + simplecpp::OutputList outputList; |
| 154 | + std::vector<std::string> files; |
| 155 | + std::unique_ptr<simplecpp::TokenList> rawtokens(new simplecpp::TokenList(ba.constData(), ba.size(), files, {}, &outputList)); |
| 156 | + rawtokens->removeComments(); |
| 157 | + simplecpp::TokenList outputTokens(files); |
| 158 | + std::map<std::string, simplecpp::TokenList*> filedata; |
| 159 | + simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList); |
| 160 | + simplecpp::cleanup(filedata); |
| 161 | + rawtokens.reset(); |
| 162 | + |
| 163 | + for (const simplecpp::Output& output : outputList) { |
| 164 | + if (output.type == simplecpp::Output::MISSING_HEADER) { |
| 165 | + // do not print these messages for now, we are not interested |
| 166 | + continue; |
| 167 | + } |
| 168 | + std::cerr << output.location.file() << ':' << output.location.line << ": "; |
| 169 | + switch (output.type) { |
| 170 | + case simplecpp::Output::ERROR: |
| 171 | + std::cerr << "#error: "; |
| 172 | + break; |
| 173 | + case simplecpp::Output::WARNING: |
| 174 | + std::cerr << "#warning: "; |
| 175 | + break; |
| 176 | + case simplecpp::Output::MISSING_HEADER: |
| 177 | + std::cerr << "missing header: "; |
| 178 | + break; |
| 179 | + case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY: |
| 180 | + std::cerr << "include nested too deeply: "; |
| 181 | + break; |
| 182 | + case simplecpp::Output::SYNTAX_ERROR: |
| 183 | + std::cerr << "syntax error: "; |
| 184 | + break; |
| 185 | + case simplecpp::Output::PORTABILITY_BACKSLASH: |
| 186 | + std::cerr << "portability: "; |
| 187 | + break; |
| 188 | + case simplecpp::Output::UNHANDLED_CHAR_ERROR: |
| 189 | + std::cerr << "unhandled char error: "; |
| 190 | + break; |
| 191 | + case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND: |
| 192 | + std::cerr << "explicit include not found: "; |
| 193 | + break; |
| 194 | + case simplecpp::Output::FILE_NOT_FOUND: |
| 195 | + std::cerr << "file not found: "; |
| 196 | + break; |
| 197 | + case simplecpp::Output::DUI_ERROR: |
| 198 | + std::cerr << "dui error: "; |
| 199 | + break; |
| 200 | + } |
| 201 | + std::cerr << output.msg << std::endl; |
151 | 202 | } |
152 | 203 |
|
153 | | - QString currentDir = QDir::current().absolutePath(); |
154 | | - QFileInfo sourceInfo(sourceFile); |
155 | | - QDir::setCurrent(sourceInfo.absolutePath()); |
156 | | - |
157 | | - std::string result; |
158 | | - result.reserve(20 * 1024); // 20K |
159 | | - |
160 | | - result += "# 1 \"builtins\"\n"; |
161 | | - result += "# 1 \""; |
162 | | - result += sourceFile.toStdString(); |
163 | | - result += "\"\n"; |
164 | | - |
165 | | - preprocess.file(sourceInfo.fileName().toStdString(), |
166 | | - rpp::pp_output_iterator<std::string>(result)); |
167 | | - |
168 | | - QDir::setCurrent(currentDir); |
169 | | - |
170 | 204 | QFile f(targetFile); |
171 | 205 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) |
172 | 206 | { |
173 | 207 | fprintf(stderr, "Failed to write preprocessed file: %s\n", qPrintable(targetFile)); |
174 | 208 | } |
| 209 | + std::string result = outputTokens.stringify(); |
175 | 210 | f.write(result.c_str(), result.length()); |
176 | 211 |
|
177 | 212 | return true; |
|
0 commit comments