CPlusPlus: Fix pragma parsing

Trailing comments were not handled properly.
Amends ccae4fc93c.

Fixes: QTCREATORBUG-31611
Change-Id: I9ec57e4b90faa6a5097f1bfdcaff20531b3dc589
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2024-09-23 14:12:59 +02:00
parent f470846866
commit 80fdc9eb93
2 changed files with 10 additions and 4 deletions

View File

@@ -1876,8 +1876,11 @@ void Preprocessor::handlePragmaDirective(PPToken *tk)
lex(tk); // consume "pragma" token lex(tk); // consume "pragma" token
while (isContinuationToken(*tk)) { while (isContinuationToken(*tk)) {
if (!consumeComments(tk)) if (tk->isComment()) {
return; if (!consumeComments(tk))
return;
continue;
}
pragma.tokens << tk->asByteArrayRef().toByteArray(); pragma.tokens << tk->asByteArrayRef().toByteArray();
lex(tk); lex(tk);
} }

View File

@@ -2105,10 +2105,12 @@ void tst_Preprocessor::pragmas()
QByteArray output; QByteArray output;
MockClient client(&env, &output); MockClient client(&env, &output);
Preprocessor preprocess(&client, &env); Preprocessor preprocess(&client, &env);
preprocess.setKeepComments(true);
QByteArray input = QByteArray input =
"#pragma once\n" "#pragma once\n"
"#include <iostream>\n" "#include <iostream>\n"
"#pragma pack(/*distraction*/push)\n" "#pragma pack(/*distraction*/push) // comment\n"
"#define THE_MACRO\n"
"struct S { bool b1; int i; short s; bool b2; };\n" "struct S { bool b1; int i; short s; bool b2; };\n"
"#pragma pack(pop)\n"; "#pragma pack(pop)\n";
preprocess.run(QLatin1String("<stdin>"), input); preprocess.run(QLatin1String("<stdin>"), input);
@@ -2117,8 +2119,9 @@ void tst_Preprocessor::pragmas()
QCOMPARE(client.pragmas().at(0).tokens, QByteArrayList{"once"}); QCOMPARE(client.pragmas().at(0).tokens, QByteArrayList{"once"});
QCOMPARE(client.pragmas().at(1).line, 3); QCOMPARE(client.pragmas().at(1).line, 3);
QCOMPARE(client.pragmas().at(1).tokens, (QByteArrayList{"pack", "(", "push", ")"})); QCOMPARE(client.pragmas().at(1).tokens, (QByteArrayList{"pack", "(", "push", ")"}));
QCOMPARE(client.pragmas().at(2).line, 5); QCOMPARE(client.pragmas().at(2).line, 6);
QCOMPARE(client.pragmas().at(2).tokens, (QByteArrayList{"pack", "(", "pop", ")"})); QCOMPARE(client.pragmas().at(2).tokens, (QByteArrayList{"pack", "(", "pop", ")"}));
QCOMPARE(client.definedMacrosLine(), QList<int>{4});
} }
void tst_Preprocessor::excessive_nesting() void tst_Preprocessor::excessive_nesting()