From 5afb755a3c1f8f97bffce9257e71423a7681b4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 Sep 2018 06:39:02 +0200 Subject: [PATCH] SymbolDatabase: Fix variable matching --- lib/astutils.cpp | 3 --- lib/symboldatabase.cpp | 7 +++++++ lib/tokenize.cpp | 2 +- test/testsymboldatabase.cpp | 13 +++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e6019bce4..03bd48a9a 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -206,9 +206,6 @@ static const Token * followVariableExpression(const Token * tok, bool cpp) if (const Variable * var2 = tok2->variable()) { if(!var2->scope()) return tok; - // FIXME This is a quick fix. Fix SymbolDatabase so typeStartToken points at the variable declaration and not a case statement - if(Token::simpleMatch(var2->typeStartToken(), "case")) - return tok; const Token * endToken2 = var2->scope() != tok->scope() ? var2->scope()->bodyEnd : endToken; if (!var2->isLocal() && !var2->isConst() && !var2->isArgument()) return tok; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 122668dc2..372d712a2 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3435,6 +3435,13 @@ void Scope::getVariableList(const Settings* settings) continue; } + // skip case/default + if (Token::Match(tok, "case|default")) { + while (tok->next() && !Token::Match(tok->next(), "[:;{}]")) + tok = tok->next(); + continue; + } + // Search for start of statement.. else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:")) continue; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8907f436b..5e2aa1f90 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2572,7 +2572,7 @@ void Tokenizer::setVarId() // Variable declarations can't start with "return" etc. -#define NOTSTART_C "goto", "NOT", "return", "sizeof", "typedef" +#define NOTSTART_C "case", "default", "goto", "NOT", "return", "sizeof", "typedef" static const std::set notstart_c = { NOTSTART_C }; static const std::set notstart_cpp = { NOTSTART_C, "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 28473e8e9..6cb58a628 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -139,6 +139,7 @@ private: TEST_CASE(isVariableDeclarationDoesNotIdentifyCppCast); TEST_CASE(isVariableDeclarationPointerConst); TEST_CASE(isVariableDeclarationRValueRef); + TEST_CASE(isVariableDeclarationDoesNotIdentifyCase); TEST_CASE(isVariableStlType); TEST_CASE(VariableValueType1); @@ -788,6 +789,18 @@ private: ASSERT(var.tokens()->tokAt(2)->scope() != 0); } + void isVariableDeclarationDoesNotIdentifyCase() { + GET_SYMBOL_DB_C("a b;\n" + "void f() {\n" + " switch (c) {\n" + " case b:;\n" + " }" + "}"); + const Variable* b = db->getVariableFromVarId(1); + ASSERT_EQUALS("b", b->name()); + ASSERT_EQUALS("a", b->typeStartToken()->str()); + } + void VariableValueType1() { GET_SYMBOL_DB("typedef uint8_t u8;\n" "static u8 x;\n");