Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9487,6 +9487,29 @@ void Tokenizer::findGarbageCode() const
if (Token::Match(tok, "[;{}] [ %name% %name% ] ;"))
syntaxError(tok->next());
}

bool statementStart = true;
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (tok->str() == "{") {
const Token *prev = tok->previous();
while (Token::Match(prev, "%name%|::") && prev->str() != "namespace")
prev = prev->previous();
if (Token::simpleMatch(prev, "namespace") || Token::Match(tok->tokAt(-2), "extern %str% {")) {
statementStart = true;
continue;
}
tok = tok->link();
statementStart = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

After a skipped { ... } block, statementStart is set to false. That is correct for struct S {..} s;, but not after a function body or a class X {} that ends without ;. As a result the check never runs on the next statement:

void f() {}
ABC { g() }

This still gives internalError: AST cyclic dependency. The same code after int x; gives unknownMacro, as intended. A macro block that follows a function is probably the most common form of this ticket.

Suggested fix: after a skipped block whose { is preceded by ) (or by const, override, noexcept and so on), set statementStart to true. Please also add a test for this case.

continue;
}
if (statementStart && Token::Match(tok, "%name% { !!}") && !tok->isKeyword() &&
!Token::findsimplematch(tok->next(), ";", tok->linkAt(1))) {
if (tok->isUpperCaseName())
unknownMacroError(tok);
syntaxError(tok);
}
statementStart = Token::Match(tok, ";|}");
}
}


Expand Down
9 changes: 9 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ class TestTokenizer : public TestFixture {
// Make sure the Tokenizer::findGarbageCode() does not have false positives
// The TestGarbage ensures that there are true positives
TEST_CASE(findGarbageCode);
TEST_CASE(nameBraceAtNamespaceScope);
TEST_CASE(checkEnableIf);
TEST_CASE(checkTemplates);
TEST_CASE(checkNamespaces);
Expand Down Expand Up @@ -8068,6 +8069,14 @@ class TestTokenizer : public TestFixture {
ignore_errout();
}

void nameBraceAtNamespaceScope() {
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("x{a}\n"), SYNTAX, "syntax error");
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("ABC { void f() {} }\n"),
UNKNOWN_MACRO,
"There is an unknown macro here somewhere. Configuration is required. If ABC is a macro then please configure it.");
ASSERT_NO_THROW(tokenizeAndStringify("DECLARE_FUNCTION { return 0; }\n"));
}

void findGarbageCode() { // Test Tokenizer::findGarbageCode()
// C++ try/catch in global scope
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("try { }\n"), SYNTAX, "syntax error: keyword 'try' is not allowed in global scope");
Expand Down
Loading