diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0d96de87b..ef5e8c208 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8508,6 +8508,22 @@ void Tokenizer::findGarbageCode() const } } + // keyword keyword + const std::set nonConsecutiveKeywords{"break", + "continue", + "for", + "goto", + "if", + "return", + "switch", + "throw", + "typedef", + "while"}; + for (const Token *tok = tokens(); tok; tok = tok->next()) { + if (Token::Match(tok, "%name% %name%") && nonConsecutiveKeywords.count(tok->str()) == 1 && nonConsecutiveKeywords.count(tok->next()->str()) == 1) + syntaxError(tok); + } + // case keyword must be inside switch for (const Token *tok = tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "switch (")) { diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 208732dfd..ae2f2743d 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -244,6 +244,7 @@ private: TEST_CASE(syntaxErrorFirstToken); // Make sure syntax errors are detected and reported TEST_CASE(syntaxErrorLastToken); // Make sure syntax errors are detected and reported TEST_CASE(syntaxErrorCase); + TEST_CASE(syntaxErrorFuzzerCliType1); TEST_CASE(enumTrailingComma); TEST_CASE(nonGarbageCode1); // #8346 @@ -339,8 +340,8 @@ private: tokenizer.tokenize(istr, "test.cpp"); assertThrowFail(__FILE__, __LINE__); } catch (InternalError& e) { - ASSERT_EQUALS("Analysis failed. If the code is valid then please report this failure.", e.errorMessage); - ASSERT_EQUALS("cppcheckError", e.id); + ASSERT_EQUALS("syntax error", e.errorMessage); + ASSERT_EQUALS("syntaxError", e.id); ASSERT_EQUALS(4, e.token->linenr()); } } @@ -433,7 +434,7 @@ private: } void garbageCode7() { - checkCode("1 (int j) { return return (c) * sizeof } y[1];"); + ASSERT_THROW(checkCode("1 (int j) { return return (c) * sizeof } y[1];"), InternalError); ASSERT_THROW(checkCode("foo(Args&&...) fn void = { } auto template