From dfaf75db54b84a69871eeb9276cbb0c1a38cce58 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 19 Feb 2019 00:08:36 -0600 Subject: [PATCH] Fix issue 8984: FP constStatement regression with qualified name (#1676) --- lib/checkother.cpp | 27 +++++++++++++++++++++++++-- test/testincompletestatement.cpp | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 85e35c93c..ac4e34b37 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1339,6 +1339,30 @@ void CheckOther::charBitOpError(const Token *tok) // Incomplete statement.. //--------------------------------------------------------------------------- +static bool isType(const Token * tok, bool unknown) +{ + if (Token::Match(tok, "%type%")) + return true; + if (Token::simpleMatch(tok, "::")) + return isType(tok->astOperand2(), unknown); + if (Token::simpleMatch(tok, "<") && tok->link()) + return true; + if (unknown && Token::Match(tok, "%name% !!(")) + return true; + return false; +} + +static bool isVarDeclOp(const Token* tok) +{ + if (!tok) + return false; + const Token * vartok = tok->astOperand2(); + if (vartok && vartok->variable() && vartok->variable()->nameToken() == vartok) + return true; + const Token * typetok = tok->astOperand1(); + return isType(typetok, Token::Match(vartok, "%var%")); +} + static bool isConstStatement(const Token *tok) { if (!tok) @@ -1350,8 +1374,7 @@ static bool isConstStatement(const Token *tok) if (Token::Match(tok, "%var%")) return true; if (Token::Match(tok, "*|&|&&") && - (Token::Match(tok->previous(), "::|.") || Token::Match(tok->astOperand1(), "%type%") || - Token::Match(tok->astOperand2(), "%type%"))) + (Token::Match(tok->previous(), "::|.") || isVarDeclOp(tok))) return false; if (Token::Match(tok, "<<|>>") && !astIsIntegral(tok, false)) return false; diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 77f302962..91845cc78 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -84,6 +84,7 @@ private: TEST_CASE(mapindex); TEST_CASE(commaoperator); TEST_CASE(redundantstmts); + TEST_CASE(vardecl); } void test1() { @@ -335,6 +336,27 @@ private: ASSERT_EQUALS("[test.cpp:1]: (warning) Unused variable value 'x'\n", errout.str()); } + + void vardecl() { + // #8984 + check("void f() { a::b *c = d(); }", true); + ASSERT_EQUALS("", errout.str()); + + check("void f() { std::vector *c; }", true); + ASSERT_EQUALS("", errout.str()); + + check("void f() { a::b &c = d(); }", true); + ASSERT_EQUALS("", errout.str()); + + check("void f() { std::vector &c; }", true); + ASSERT_EQUALS("", errout.str()); + + check("void f() { a::b &&c = d(); }", true); + ASSERT_EQUALS("", errout.str()); + + check("void f() { std::vector &&c; }", true); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestIncompleteStatement)