Tokenizer: detect unknown macro 'if (x) MACRO }' (#5209)

This commit is contained in:
Daniel Marjamäki 2023-07-01 13:17:41 +02:00 committed by GitHub
parent 59a8944e30
commit 96955ccfe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -8054,6 +8054,17 @@ void Tokenizer::reportUnknownMacros() const
}
}
// Report unknown macros before } "{ .. if (x) MACRO }"
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, ")|; %name% }")) {
const Token* prev = tok->linkAt(2);
while (Token::simpleMatch(prev, "{"))
prev = prev->previous();
if (Token::Match(prev, ";|)"))
unknownMacroError(tok->next());
}
}
// Report unknown macros that contain several statements "MACRO(a;b;c)"
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (!Token::Match(tok, "%name% ("))

View File

@ -6843,6 +6843,9 @@ private:
ASSERT_NO_THROW(tokenizeAndStringify(code11));
ASSERT_NO_THROW(tokenizeAndStringify("alignas(8) alignas(16) int x;")); // alignas is not unknown macro
ASSERT_THROW(tokenizeAndStringify("void foo() { if(x) SYSTEM_ERROR }"), InternalError);
ASSERT_THROW(tokenizeAndStringify("void foo() { dostuff(); SYSTEM_ERROR }"), InternalError);
}
void findGarbageCode() { // Test Tokenizer::findGarbageCode()