summaryrefslogtreecommitdiffstats
path: root/src/tools/moc/preprocessor.cpp
diff options
context:
space:
mode:
authorAurélien Brooke <aurelien@bahiasoft.fr>2025-07-18 12:00:31 +0200
committerAurélien Brooke <aurelien@bahiasoft.fr>2025-07-21 23:15:17 +0200
commit3babea74b2acfae64c9db2a42f33ce2c4cfad445 (patch)
tree11076a4d4a32a816113c85ec036e7b7647cd0927 /src/tools/moc/preprocessor.cpp
parent499edb6a98d1369cbacba8f4a14c1cb921673956 (diff)
moc: add lexem() functions that return QByteArrayViews
In many cases, the result of the lexem() functions is only used as a view and is not stored. To avoid constructing thousands of temporary QByteArray instances, introduce lexemView() and unquotedLexemView() that return a QByteArrayView instead. We need to carefuly remove .constData() calls on previously-QByteArrays, because QByteArrayViews may not be NUL-terminated, so we must transfer the size information to e.g. QString::fromLocal8bit(). This saves only about 3 % of temporary allocs but enables further optimizations. Change-Id: I55e389f7602ac42d3e2f83b38161286d5c17bb48 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/tools/moc/preprocessor.cpp')
-rw-r--r--src/tools/moc/preprocessor.cpp15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/tools/moc/preprocessor.cpp b/src/tools/moc/preprocessor.cpp
index fd2d196a7a5..77c2c66ecdd 100644
--- a/src/tools/moc/preprocessor.cpp
+++ b/src/tools/moc/preprocessor.cpp
@@ -190,7 +190,7 @@ Symbols Preprocessor::tokenize(const QByteArray& input, int lineNum, Preprocesso
const QByteArray newString
= '\"'
- + symbols.constLast().unquotedLexem()
+ + symbols.constLast().unquotedLexemView()
+ input.mid(lexem - begin + 1, data - lexem - 2)
+ '\"';
symbols.last() = Symbol(symbols.constLast().lineNum,
@@ -650,7 +650,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym
const Symbols &arg = arguments.at(index);
QByteArray stringified;
for (const Symbol &sym : arg)
- stringified += sym.lexem();
+ stringified += sym.lexemView();
stringified.replace('"', "\\\"");
stringified.prepend('"');
@@ -967,9 +967,8 @@ int PP_Expression::primary_expression()
test(PP_RPAREN);
} else {
next();
- const QByteArray &lex = lexem();
- auto lexView = QByteArrayView(lex);
- if (lex.endsWith('L'))
+ auto lexView = lexemView();
+ if (lexView.endsWith('L'))
lexView.chop(1);
value = lexView.toInt(nullptr, 0);
}
@@ -1120,7 +1119,7 @@ void Preprocessor::preprocess(const QByteArray &filename, Symbols &preprocessed)
QByteArray include;
bool local = false;
if (test(PP_STRING_LITERAL)) {
- local = lexem().startsWith('\"');
+ local = lexemView().startsWith('\"');
include = unquotedLexem();
} else
continue;
@@ -1322,7 +1321,7 @@ void Preprocessor::parseDefineArguments(Macro *m)
if (t == PP_RPAREN)
break;
if (t != PP_IDENTIFIER) {
- QByteArray l = lexem();
+ QByteArrayView l = lexemView();
if (l == "...") {
m->isVariadic = true;
arguments += Symbol(symbol().lineNum, PP_IDENTIFIER, "__VA_ARGS__");
@@ -1346,7 +1345,7 @@ void Preprocessor::parseDefineArguments(Macro *m)
break;
if (t == PP_COMMA)
continue;
- if (lexem() == "...") {
+ if (lexemView() == "...") {
//GCC extension: #define FOO(x, y...) x(y)
// The last argument was already parsed. Just mark the macro as variadic.
m->isVariadic = true;