Skip to content

Commit 4ffe325

Browse files
authored
Fix #15307: Internal error for invalid code inside unknown macro argument (#9112)
1 parent 457e8cf commit 4ffe325

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8966,6 +8966,16 @@ void Tokenizer::findGarbageCode() const
89668966
"typedef",
89678967
"while" };
89688968

8969+
const auto hasTopLevelSemicolon = [](const Token *brace) {
8970+
for (const Token *tok = brace->next(); tok && tok != brace->link(); tok = tok->next()) {
8971+
if (Token::Match(tok, "(|[|{") && tok->link())
8972+
tok = tok->link();
8973+
else if (tok->str() == ";")
8974+
return true;
8975+
}
8976+
return false;
8977+
};
8978+
89698979
for (const Token *tok = tokens(); tok; tok = tok->next()) {
89708980
// initialization: = {
89718981
if (Token::simpleMatch(tok, "= {") && Token::simpleMatch(tok->linkAt(1), "} ("))
@@ -9017,6 +9027,17 @@ void Tokenizer::findGarbageCode() const
90179027
syntaxError(tok);
90189028
}
90199029

9030+
if (Token::Match(tok, "(|, {") && !Token::Match(tok->linkAt(1)->previous(), ";|}") && hasTopLevelSemicolon(tok->next())) {
9031+
const Token *par = tok;
9032+
while (par && !Token::Match(par, "(|;|{")) {
9033+
if (Token::Match(par, ")|]|}"))
9034+
par = par->link();
9035+
par = par->previous();
9036+
}
9037+
if (par && par->str() == "(" && Token::Match(par->previous(), "%name% (") && !par->previous()->isKeyword())
9038+
unknownMacroError(par->previous());
9039+
}
9040+
90209041
if (tok->isControlFlowKeyword()) {
90219042
if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... }
90229043
if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) {

‎test/testtokenize.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class TestTokenizer : public TestFixture {
490490
TEST_CASE(checkConfiguration);
491491

492492
TEST_CASE(unknownMacroBeforeReturn);
493+
TEST_CASE(unknownMacroBracedArgument);
493494

494495
TEST_CASE(cppKeywordInCSource);
495496

@@ -8853,6 +8854,12 @@ class TestTokenizer : public TestFixture {
88538854
ASSERT_THROW_INTERNAL(tokenizeAndStringify("int f() { X return 0; }\n"), UNKNOWN_MACRO);
88548855
}
88558856

8857+
void unknownMacroBracedArgument() {
8858+
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("F(a, { float y = p.y; return 0 });\n"),
8859+
UNKNOWN_MACRO,
8860+
"There is an unknown macro here somewhere. Configuration is required. If F is a macro then please configure it.");
8861+
}
8862+
88568863
void cppKeywordInCSource() {
88578864
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}\n", dinit(TokenizeOptions, $.cpp = false)));
88588865

0 commit comments

Comments
 (0)