Skip to content

Commit

Permalink
Refs #12655: add unknownMacro warning (#6361)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Apr 29, 2024
1 parent 05d5710 commit d3e4284
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8674,8 +8674,6 @@ void Tokenizer::findGarbageCode() const
if (Token::Match(tok->next(), ")|]|>|%assign%|%or%|%oror%|==|!=|/|>=|<=|&&"))
syntaxError(tok);
}
if ((!isCPP() || !Token::simpleMatch(tok->previous(), "operator")) && Token::Match(tok, "[,;] ,"))
syntaxError(tok);
if (Token::simpleMatch(tok, ".") &&
!Token::simpleMatch(tok->previous(), ".") &&
!Token::simpleMatch(tok->next(), ".") &&
Expand Down Expand Up @@ -8703,22 +8701,28 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok);
if (Token::Match(tok, "! %comp%"))
syntaxError(tok);
if (Token::Match(tok, "] %name%") && (!isCPP() || !(tok->tokAt(-1) && Token::simpleMatch(tok->tokAt(-2), "delete ["))))
syntaxError(tok);
if (Token::Match(tok, "] %name%") && (!isCPP() || !(tok->tokAt(-1) && Token::simpleMatch(tok->tokAt(-2), "delete [")))) {
if (tok->next()->isUpperCaseName())
unknownMacroError(tok->next());
else
syntaxError(tok);
}

if (tok->link() && Token::Match(tok, "[([]") && (!tok->tokAt(-1) || !tok->tokAt(-1)->isControlFlowKeyword())) {
const Token* const end = tok->link();
for (const Token* inner = tok->next(); inner != end; inner = inner->next()) {
if (inner->str() == "{")
inner = inner->link();
else if (inner->str() == ";") {
else if (inner->str() == ";" || (Token::simpleMatch(inner, ", ,") && (!isCPP() || !Token::simpleMatch(inner->previous(), "operator")))) {
if (tok->tokAt(-1) && tok->tokAt(-1)->isUpperCaseName())
unknownMacroError(tok->tokAt(-1));
else
syntaxError(inner);
}
}
}
if ((!isCPP() || !Token::simpleMatch(tok->previous(), "operator")) && Token::Match(tok, "[,;] ,"))
syntaxError(tok);
}

// ternary operator without :
Expand Down
2 changes: 1 addition & 1 deletion test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ class TestGarbage : public TestFixture {
" typedef S0 b[][1][1] != 0\n"
"};\n"
"b[K][0] S0 b[][1][1] != 4{ 0 };\n"
"b[0][0]"), SYNTAX);
"b[0][0]"), UNKNOWN_MACRO);
}

void garbageCode149() { // #7085
Expand Down
8 changes: 8 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7133,6 +7133,14 @@ class TestTokenizer : public TestFixture {
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it.");

ASSERT_THROW_EQUALS(tokenizeAndStringify("struct S { int a[2] PACKED; };\n"),
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If PACKED is a macro then please configure it.");

ASSERT_THROW_EQUALS(tokenizeAndStringify("MACRO(a, b,,)\n"),
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it.");

ASSERT_THROW_INTERNAL(tokenizeAndStringify("{ for (()()) }"), SYNTAX); // #11643

ASSERT_NO_THROW(tokenizeAndStringify("S* g = ::new(ptr) S();")); // #12552
Expand Down

0 comments on commit d3e4284

Please sign in to comment.