diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9fe7804aa..4f698ded9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8493,6 +8493,12 @@ void Tokenizer::findGarbageCode() const if (Token::Match(tok, "> %cop%")) continue; } + if (Token::Match(tok, "%or%|%oror%|==|!=|+|-|/|!|>=|<=|~|++|--|::|sizeof|throw|decltype|typeof {|if|else|try|catch|while|do|for|return|switch|break|namespace")) + syntaxError(tok); + if (Token::Match(tok, "( %any% )") && tok->next()->isKeyword() && !Token::simpleMatch(tok->next(), "void")) + syntaxError(tok); + if (Token::Match(tok, "%num%|%bool%|%char%|%str% %num%|%bool%|%char%|%str%") && !Token::Match(tok, "%str% %str%")) + syntaxError(tok); if (Token::Match(tok, "%assign% typename|class %assign%")) syntaxError(tok); if (Token::Match(tok, "%cop%|=|,|[ %or%|%oror%|/|%")) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index d79eb8ae6..52f344aac 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -230,6 +230,7 @@ private: TEST_CASE(garbageCode197); // #8385 TEST_CASE(garbageCode198); // #8383 TEST_CASE(garbageCode199); // #8752 + TEST_CASE(garbageCode200); // #8757 TEST_CASE(garbageCodeFuzzerClientMode1); // test cases created with the fuzzer client, mode 1 @@ -975,7 +976,7 @@ private: } void garbageCode131() { - checkCode("( void ) { ( ) } ( ) / { ( ) }"); + ASSERT_THROW(checkCode("( void ) { ( ) } ( ) / { ( ) }"), InternalError); // actually the invalid code should trigger an syntax error... } @@ -1545,6 +1546,11 @@ private: checkCode("d f(){e n00e0[]n00e0&""0+f=0}"); } + // #8757 + void garbageCode200() { + ASSERT_THROW(checkCode("(){e break,{(case)!{e:[]}}}"), InternalError); + } + void syntaxErrorFirstToken() { ASSERT_THROW(checkCode("&operator(){[]};"), InternalError); // #7818 ASSERT_THROW(checkCode("*(*const<> (size_t); foo) { } *(*const (size_t)() ; foo) { }"), InternalError); // #6858 @@ -1599,6 +1605,10 @@ private: // case must be inside switch block ASSERT_THROW(checkCode("void f() { switch (a) {}; case 1: }"), InternalError); // #8184 ASSERT_THROW(checkCode("struct V : { public case {} ; struct U : U void { V *f (int x) (x) } }"), InternalError); // #5120 + ASSERT_THROW(checkCode("void f() { 0 0; }"), InternalError); + ASSERT_THROW(checkCode("void f() { true 0; }"), InternalError); + ASSERT_THROW(checkCode("void f() { 'a' 0; }"), InternalError); + ASSERT_THROW(checkCode("void f() { 1 \"\"; }"), InternalError); } void enumTrailingComma() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 836acfb20..6f3cc2b5a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8641,6 +8641,8 @@ private: ASSERT_NO_THROW(tokenizeAndStringify("void f() { do switch (a) {} while (1); }")) ASSERT_NO_THROW(tokenizeAndStringify("void f() { label: switch (a) {} }")); ASSERT_NO_THROW(tokenizeAndStringify("void f() { UNKNOWN_MACRO if (a) {} }")) + ASSERT_NO_THROW(tokenizeAndStringify("void f() { []() -> int * {}; }")); + ASSERT_NO_THROW(tokenizeAndStringify("void f() { const char* var = \"1\" \"2\"; }")); // TODO ASSERT_NO_THROW(tokenizeAndStringify("void f() { MACRO(switch); }")); // TODO ASSERT_NO_THROW(tokenizeAndStringify("void f() { MACRO(x,switch); }"));