Skip to content

Commit ab34224

Browse files
committed
Replace preprocessor with code from simplecpp project
1 parent 55c815a commit ab34224

36 files changed

+4489
-4254
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ See https://code.qt.io/cgit/qt-labs/qtscriptgenerator.git for the original proje
2626
The PythonQt wrappers generated by the generator are distributed under the `LGPL`
2727
as well.
2828

29+
The integrated preprocessor has been replaced with code from the simplecpp project,
30+
see https://github.com/danmar/simplecpp. It is licensed under the `0BSDL` license,
31+
see the `LICENSE` file in the generator/simplecpp directory.
32+
Copyright (C) 2016-2023 simplecpp team
33+
2934
The generated wrappers are pre-generated and checked-in, so you only
3035
need to build and run the generator when you want to build additional wrappers
3136
or you want to upgrade/downgrade to another Qt version, but this requires

generator/abstractmetabuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ void AbstractMetaBuilder::traverseFunctions(ScopeModelItem scope_item, AbstractM
11691169
ReportHandler::warning(warn);
11701170
}
11711171

1172-
meta_class->addFunction(meta_function);
1172+
meta_class->addFunction(meta_function, /*check_duplicates=*/true);
11731173
} else if (meta_function->isDestructor()) {
11741174
meta_class->setDestructorException(meta_function->exception());
11751175
if (!meta_function->isPublic()) {

generator/generator.pri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RESOURCES += generator.qrc
1616

1717
include($$GENERATORPATH/parser/rxx.pri)
1818

19-
include($$GENERATORPATH/parser/rpp/rpp.pri)
19+
include($$GENERATORPATH/simplecpp/simplecpp.pri)
2020

2121
CONFIG += strict_c++
2222
win32-msvc*{

generator/generator.qrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@
1515
<file alias="typesystem_multimedia.txt">typesystem_multimedia.xml</file>
1616
<file alias="typesystem_qml.txt">typesystem_qml.xml</file>
1717
<file alias="typesystem_quick.txt">typesystem_quick.xml</file>
18-
<file>parser/rpp/pp-qt-configuration</file>
1918
</qresource>
2019
</RCC>

generator/main.cpp

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@
4848
#include "generatorset.h"
4949
#include "fileout.h"
5050
#include "control.h"
51-
#include "pp.h"
51+
#include "simplecpp.h"
5252

5353
#include <QDir>
5454
#include <QFileInfo>
5555
#include <QFile>
5656
#include <QTextStream>
5757
#include <QRegularExpression>
5858

59+
#include <memory>
60+
5961
void displayHelp(GeneratorSet *generatorSet);
6062

6163
namespace
@@ -125,53 +127,86 @@ namespace
125127
}
126128

127129
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())
129131
{
130-
rpp::pp_environment env;
131-
rpp::pp preprocess(env);
132-
133-
rpp::pp_null_output_iterator null_out;
132+
simplecpp::DUI dui; // settings
134133

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;
136141

137-
QFile file(ppconfig);
142+
QFile file(sourceFile);
138143
if (!file.open(QFile::ReadOnly))
139144
{
140-
fprintf(stderr, "Preprocessor configuration file not found '%s'\n", ppconfig);
145+
std::cerr << "Main file not found:" << sourceFile.toUtf8().constData() << std::endl;
141146
return false;
142147
}
143148

144149
QByteArray ba = file.readAll();
145150
file.close();
146-
preprocess.operator()(ba.constData(), ba.constData() + ba.size(), null_out);
147151

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;
151202
}
152203

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-
170204
QFile f(targetFile);
171205
if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
172206
{
173207
fprintf(stderr, "Failed to write preprocessed file: %s\n", qPrintable(targetFile));
174208
}
209+
std::string result = outputTokens.stringify();
175210
f.write(result.c_str(), result.length());
176211

177212
return true;

generator/parser/lexer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ void LocationManager::extract_line(int offset, int *line, QString *filename) con
6969
const unsigned char *cursor = begin_buffer + offset;
7070

7171
++cursor; // skip '#'
72+
if (cursor[0] == 'l' && cursor[1] == 'i' && cursor[2] == 'n' && cursor[3] == 'e') {
73+
cursor += 4;
74+
}
7275
if (std::isspace(*cursor) && std::isdigit(*(cursor + 1)))
7376
{
7477
++cursor;

generator/parser/rpp/builtin-macros.cpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

generator/parser/rpp/pp-cctype.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

generator/parser/rpp/pp-configuration

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)