Skip to content

Commit c60ecbe

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

File tree

12 files changed

+4487
-6
lines changed

12 files changed

+4487
-6
lines changed

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ include($$GENERATORPATH/parser/rxx.pri)
1818

1919
include($$GENERATORPATH/parser/rpp/rpp.pri)
2020

21+
include($$GENERATORPATH/simplecpp/simplecpp.pri)
22+
2123
CONFIG += strict_c++
2224
win32-msvc*{
2325
#Disable warning C4996 (deprecated declarations)

generator/main.cpp

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "fileout.h"
5050
#include "control.h"
5151
#include "pp.h"
52+
#include "simplecpp.h"
5253

5354
#include <QDir>
5455
#include <QFileInfo>
@@ -125,7 +126,95 @@ namespace
125126
}
126127

127128
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())
129218
{
130219
rpp::pp_environment env;
131220
rpp::pp preprocess(env);

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/qtscript_masterinclude.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@
3838
** $QT_END_LICENSE$
3939
**
4040
****************************************************************************/
41+
42+
43+
// Qt
44+
#define QOBJECTDEFS_H
45+
#define QTMETAMACROS_H
46+
47+
// not yet supported
48+
#define Q_SLOTS slots
49+
#define Q_SIGNALS signals
50+
#define Q_FLAGS(a)
51+
#define Q_FLAG(a)
52+
#define Q_PRIVATE_SLOT(a, b)
53+
#define Q_DECLARE_INTERFACE(a,b)
54+
#define Q_INTERFACES(a)
55+
#define Q_GADGET
56+
#define Q_OVERRIDE(a)
57+
#define Q_OS_OS2
58+
#define Q_NO_USING_KEYWORD
59+
#define Q_DECL_OVERRIDE override
60+
61+
// There are symbols in Qt that exist in Debug but
62+
// not in release
63+
#define QT_NO_DEBUG
64+
65+
#define QT_JAMBI_RUN
66+
67+
// Qt6
68+
#define Q_NAMESPACE_EXPORT(...)
69+
#define Q_ENUM_NS(x)
70+
#define Q_FLAG_NS(x)
71+
#define Q_MOC_INCLUDE(...)
72+
73+
// ignore static_assert
74+
#define static_assert(...)
75+
76+
77+
78+
4179
// We need to force the endianess in Qt5
4280
#define Q_BYTE_ORDER Q_LITTLE_ENDIAN
4381

@@ -122,9 +160,11 @@
122160
#define QDOC_PROPERTY(text) Q_PROPERTY(text)
123161

124162
// don't need this:
125-
#define Q_REVISION(v)
163+
#define Q_REVISION(...)
126164
#define Q_DECLARE_OPERATORS_FOR_FLAGS(x)
127165
#define Q_GADGET_EXPORT(x)
166+
#define Q_DECLARE_TYPEINFO(...)
167+
#define Q_DECLARE_MIXED_ENUM_OPERATORS_SYMMETRIC(...)
128168

129169
#include <QtCore/QMetaType>
130170

generator/shellgenerator.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ namespace {
135135
}
136136
}
137137

138+
namespace {
139+
140+
QString trimSpaces(const QString& expr)
141+
{
142+
QString result = expr;
143+
if (!result.contains("\"") && !result.contains("'")) {
144+
// assume all spaces are expendable (some new spaces were introduced by the new simplecpp preprocessor)
145+
result.replace(" ", "");
146+
}
147+
return result;
148+
}
149+
150+
};
151+
138152

139153
void ShellGenerator::writeFunctionArguments(QTextStream &s,
140154
const AbstractMetaFunction *meta_function,
@@ -167,9 +181,9 @@ void ShellGenerator::writeFunctionArguments(QTextStream &s,
167181
}
168182
}
169183
if ((option & IncludeDefaultExpression) && !arg->defaultValueExpression().isEmpty()) {
170-
s << " = ";
184+
s << " = ";
171185

172-
QString expr = arg->defaultValueExpression();
186+
QString expr = trimSpaces(arg->defaultValueExpression());
173187
if (expr == "NULL")
174188
{
175189
expr = "nullptr";

generator/simplecpp/LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BSD Zero Clause License
2+
3+
Copyright (c) 2023 simplecpp team
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
PERFORMANCE OF THIS SOFTWARE.

generator/simplecpp/README.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This code is taken from https://github.com/danmar/simplecpp, version 1.2.0
2+
3+
The code was released under the 0BSD license (see LICENSE file).
4+
5+
The code is lightly patched (see do_not_stop_on_error.patch).

0 commit comments

Comments
 (0)