diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4dbd696f36a..67cd5d184eb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; + 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, ";|}"); + } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dcd1534a84f..2442e8d8bd1 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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); @@ -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");