diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ff4a7a83faf..0680fcd3f47 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8537,13 +8537,15 @@ void Tokenizer::findGarbageCode() const if (!Token::simpleMatch(tok, "for (")) // find for loops continue; // count number of semicolons - int semicolons = 0; + int semicolons = 0, colons = 0; const Token* const startTok = tok; tok = tok->next()->link()->previous(); // find ")" of the for-loop // walk backwards until we find the beginning (startTok) of the for() again for (; tok != startTok; tok = tok->previous()) { if (tok->str() == ";") { // do the counting semicolons++; + } else if (tok->str() == ":") { + colons++; } else if (tok->str() == ")") { // skip pairs of ( ) tok = tok->link(); } @@ -8553,6 +8555,8 @@ void Tokenizer::findGarbageCode() const syntaxError(tok); if (semicolons == 1 && !(isCPP() && mSettings.standards.cpp >= Standards::CPP20)) syntaxError(tok); + if (semicolons == 0 && colons == 0) + syntaxError(tok); } // Operators without operands.. diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index b84e9607275..5a6187cb3ca 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -1174,8 +1174,7 @@ class TestGarbage : public TestFixture { } void garbageCode142() { // #7050 - checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }"); - (void)errout_str(); // we are not interested in the output + ASSERT_THROW(checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }"), InternalError); } void garbageCode143() { // #6922 @@ -1312,8 +1311,7 @@ class TestGarbage : public TestFixture { ASSERT_THROW(checkCode(code), InternalError); code = "void f1() { for (int n = 0 n < 10 n++); }"; - checkCode(code); - (void)errout_str(); // we are not interested in the output + ASSERT_THROW(checkCode(code), InternalError); } void garbageSymbolDatabase() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e9d386610b2..f6a0a67f4eb 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7048,6 +7048,8 @@ class TestTokenizer : public TestFixture { ASSERT_THROW_EQUALS(tokenizeAndStringify("int (*f) MACRO((void *));\n"), // #12010 InternalError, "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + + ASSERT_THROW(tokenizeAndStringify("{ for (()()) }"), InternalError); // #11643 }