From 96955ccfe98d4edd73bf22cc48654f37b265736c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 1 Jul 2023 13:17:41 +0200 Subject: [PATCH] Tokenizer: detect unknown macro 'if (x) MACRO }' (#5209) --- lib/tokenize.cpp | 11 +++++++++++ test/testtokenize.cpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4969542f8..815040df9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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% (")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2a5055a7a..266224c0e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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()