CPlusPlus: Fix lexer crash

At some point in the preprocessing stage, some tokens get "squeezed", so
their associated string no longer refers to the document content, but
only to their own symbol. However, there is at least one context that
operated under the assumption that the token's offset still pointed into
the "global" string. As the token's offset is zero after parsing, this
lead to the same piece of code being preprocessed in an infinite loop.

Fixes: QTCREATORBUG-19525
Change-Id: I231ba51811cfa0b5c6dfe7f75fe0384472252c6f
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2020-06-17 16:40:04 +02:00
parent 1b8c68f617
commit 8eac3fba80
4 changed files with 28 additions and 1 deletions

View File

@@ -405,6 +405,7 @@ private slots:
void multi_byte_code_point_in_expansion();
void trigraph();
void nested_arguments_expansion();
void preprocessorSymbolsAsMacroArguments();
};
// Remove all #... lines, and 'simplify' string, to allow easily comparing the result
@@ -2099,6 +2100,20 @@ void tst_Preprocessor::nested_arguments_expansion()
QVERIFY(prep.contains(output));
}
void tst_Preprocessor::preprocessorSymbolsAsMacroArguments()
{
Environment env;
Preprocessor preprocess(nullptr, &env);
const QByteArray input =
"#define IFGEN(if, endif) if (1 == 0) endif\n"
"int main()\n"
"{\n"
"IFGEN(#if, #endif)\n"
"return 0;\n"
"}\n";
QVERIFY(preprocess.run(QLatin1String("<stdin>"), input).startsWith("# 1 \"<stdin>\"\n"));
}
void tst_Preprocessor::excessive_nesting()
{
Environment env;