From 3776604f068eb7a527211706e1032174b92682fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Oct 2019 15:55:38 +0200 Subject: [PATCH] SymbolDatabase: Add variable in if condition 'if (auto x = bar())' --- lib/symboldatabase.cpp | 7 +++++-- test/testsymboldatabase.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 2d9736fe5..497220463 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3543,7 +3543,10 @@ void Scope::getVariableList(const Settings* settings) { const Token *start; - if (bodyStart) + if (type == eIf) + start = classDef; + + else if (bodyStart) start = bodyStart->next(); // global scope @@ -3642,7 +3645,7 @@ void Scope::getVariableList(const Settings* settings) } // Search for start of statement.. - else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:")) + else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|(|public:|protected:|private:")) continue; else if (tok->str() == ";") continue; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 7c1cf972e..a1e3acd7d 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -141,6 +141,7 @@ private: TEST_CASE(isVariableDeclarationPointerConst); TEST_CASE(isVariableDeclarationRValueRef); TEST_CASE(isVariableDeclarationDoesNotIdentifyCase); + TEST_CASE(isVariableDeclarationIf); TEST_CASE(isVariableStlType); TEST_CASE(isVariablePointerToConstPointer); TEST_CASE(isVariablePointerToVolatilePointer); @@ -831,6 +832,18 @@ private: ASSERT_EQUALS("a", b->typeStartToken()->str()); } + void isVariableDeclarationIf() { + GET_SYMBOL_DB("void foo() {\n" + " for (auto& elem : items) {\n" + " if (auto x = bar()) {}\n" + " }\n" + "}"); + const Token *x = Token::findsimplematch(tokenizer.tokens(), "x"); + ASSERT(x); + ASSERT(x->varId()); + ASSERT(x->variable()); + } + void VariableValueType1() { GET_SYMBOL_DB("typedef uint8_t u8;\n" "static u8 x;\n");