From 8f4cb36e1e98d8dca94f8dd80d7123f0c2b83eab Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 3 Jul 2019 01:28:24 -0500 Subject: [PATCH] Check for more garbage code (#1949) * Check for garbage commas * Find garbage dot operator --- lib/tokenize.cpp | 15 +++++++++++++++ test/testgarbage.cpp | 8 ++++---- test/testother.cpp | 6 +++--- test/testsymboldatabase.cpp | 7 ------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a9c9c083a..4c2c40188 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9227,6 +9227,21 @@ void Tokenizer::findGarbageCode() const syntaxError(tok); if (Token::Match(tok, "[+-] [;,)]}]") && !(isCPP() && Token::Match(tok->previous(), "operator [+-] ;"))) syntaxError(tok); + if (Token::simpleMatch(tok, ",")) { + if (Token::Match(tok->previous(), "(|[|{|<|%assign%|%or%|%oror%|==|!=|+|-|/|!|>=|<=|~|^|::|sizeof|throw|decltype|typeof")) + syntaxError(tok); + if (Token::Match(tok->next(), ")|]|>|%assign%|%or%|%oror%|==|!=|/|>=|<=|&&")) + syntaxError(tok); + } + if (Token::simpleMatch(tok, ".") && + !Token::simpleMatch(tok->previous(), ".") && + !Token::simpleMatch(tok->next(), ".") && + !Token::Match(tok->previous(), "{|, . %name% =")) { + if (!Token::Match(tok->previous(), ")|]|>|}|%name%")) + syntaxError(tok); + if (!Token::Match(tok->next(), "template|operator|*|~|%name%")) + syntaxError(tok); + } } // ternary operator without : diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 714da834e..86821973e 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -841,9 +841,9 @@ private: void garbageCode101() { // #6835 // Reported case - checkCode("template < class , =( , int) X = 1 > struct A { } ( ) { = } [ { } ] ( ) { A < void > 0 }"); + ASSERT_THROW(checkCode("template < class , =( , int) X = 1 > struct A { } ( ) { = } [ { } ] ( ) { A < void > 0 }"), InternalError); // Reduced case - checkCode("template < class =( , ) X = 1> struct A {}; A a;"); + ASSERT_THROW(checkCode("template < class =( , ) X = 1> struct A {}; A a;"), InternalError); } void garbageCode102() { // #6846 @@ -1416,7 +1416,7 @@ private: void garbageCode170() { // 7255 - checkCode("d i(){{f*s=typeid(()0,)}}", false); + ASSERT_THROW(checkCode("d i(){{f*s=typeid(()0,)}}", false), InternalError); } void garbageCode171() { @@ -1595,7 +1595,7 @@ private: } void garbageCode204() { - checkCode("template ()> c; template a as() {} as>();"); + ASSERT_THROW(checkCode("template ()> c; template a as() {} as>();"), InternalError); } void syntaxErrorFirstToken() { diff --git a/test/testother.cpp b/test/testother.cpp index 8f4dce195..8607f6b37 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3781,13 +3781,13 @@ private: check("void f() {\n" " enum { Four = 4 };\n" - " static_assert(Four == 4, "");\n" + " static_assert(Four == 4, \"\");\n" "}"); ASSERT_EQUALS("", errout.str()); check("void f() {\n" " enum { Four = 4 };\n" - " static_assert(4 == Four, "");\n" + " static_assert(4 == Four, \"\");\n" "}"); ASSERT_EQUALS("", errout.str()); @@ -3801,7 +3801,7 @@ private: check("void f() {\n" " enum { FourInEnumOne = 4 };\n" " enum { FourInEnumTwo = 4 };\n" - " static_assert(FourInEnumOne == FourInEnumTwo, "");\n" + " static_assert(FourInEnumOne == FourInEnumTwo, \"\");\n" "}"); ASSERT_EQUALS("", errout.str()); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 7a237d842..827259f2e 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -204,7 +204,6 @@ private: TEST_CASE(functionArgs1); TEST_CASE(functionArgs2); - TEST_CASE(functionArgs3); TEST_CASE(functionArgs4); TEST_CASE(functionArgs5); // #7650 TEST_CASE(functionArgs6); // #7651 @@ -1917,12 +1916,6 @@ private: ASSERT_EQUALS(true, a->dimensions()[1].known); } - void functionArgs3() { - GET_SYMBOL_DB("void f(int i,) { }"); // Don't crash - const Variable *a = db->getVariableFromVarId(1); - ASSERT_EQUALS("i", a->nameToken()->str()); - } - void functionArgs4() { GET_SYMBOL_DB("void f1(char [10], struct foo [10]);"); ASSERT_EQUALS(true, db->scopeList.front().functionList.size() == 1UL);