|
49 | 49 | #include "fileout.h" |
50 | 50 | #include "control.h" |
51 | 51 | #include "pp.h" |
| 52 | +#include "simplecpp.h" |
52 | 53 |
|
53 | 54 | #include <QDir> |
54 | 55 | #include <QFileInfo> |
@@ -125,7 +126,95 @@ namespace |
125 | 126 | } |
126 | 127 |
|
127 | 128 | bool |
128 | | - preprocess(const QString &sourceFile, const QString &targetFile, const QString &commandLineIncludes = QString()) |
| 129 | + preprocess(const QString& sourceFile, const QString& targetFile, const QString& commandLineIncludes = QString()) |
| 130 | + { |
| 131 | + simplecpp::DUI dui; // settings |
| 132 | + |
| 133 | + foreach(QString include, getIncludeDirectories(commandLineIncludes)) { |
| 134 | + dui.includePaths.push_back(QDir::toNativeSeparators(include).toStdString()); |
| 135 | + } |
| 136 | + dui.defines.push_back("__cplusplus=1"); |
| 137 | + dui.defines.push_back("__STDC__"); |
| 138 | + dui.std = "c++17"; |
| 139 | + dui.removeComments = true; |
| 140 | + |
| 141 | + QFile file(sourceFile); |
| 142 | + if (!file.open(QFile::ReadOnly)) |
| 143 | + { |
| 144 | + std::cerr << "Main file not found:" << sourceFile.toUtf8().constData() << std::endl; |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + QByteArray ba = file.readAll(); |
| 149 | + file.close(); |
| 150 | + |
| 151 | + // Perform preprocessing |
| 152 | + simplecpp::OutputList outputList; |
| 153 | + std::vector<std::string> files; |
| 154 | + simplecpp::TokenList* rawtokens = new simplecpp::TokenList(ba.constData(), ba.size(), files, {}, &outputList); |
| 155 | + rawtokens->removeComments(); |
| 156 | + simplecpp::TokenList outputTokens(files); |
| 157 | + std::map<std::string, simplecpp::TokenList*> filedata; |
| 158 | + simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList); |
| 159 | + simplecpp::cleanup(filedata); |
| 160 | + delete rawtokens; |
| 161 | + rawtokens = nullptr; |
| 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; |
| 202 | + } |
| 203 | + |
| 204 | + QFile f(targetFile); |
| 205 | + if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 206 | + { |
| 207 | + fprintf(stderr, "Failed to write preprocessed file: %s\n", qPrintable(targetFile)); |
| 208 | + } |
| 209 | + std::string result = outputTokens.stringify(); |
| 210 | + f.write(result.c_str(), result.length()); |
| 211 | + |
| 212 | + return true; |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + bool |
| 217 | + preprocess2(const QString &sourceFile, const QString &targetFile, const QString &commandLineIncludes = QString()) |
129 | 218 | { |
130 | 219 | rpp::pp_environment env; |
131 | 220 | rpp::pp preprocess(env); |
|
0 commit comments