diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index f3b1f7028..c4f777f29 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4194,19 +4194,19 @@ void Scope::getVariableList(const Settings* settings) { if (!bodyStartList.empty()) { for (const Token *bs: bodyStartList) - getVariableList(settings, bs->next()); + getVariableList(settings, bs->next(), bs->link()); } // global scope else if (type == Scope::eGlobal) - getVariableList(settings, check->mTokenizer->tokens()); + getVariableList(settings, check->mTokenizer->tokens(), nullptr); // forward declaration else return; } -void Scope::getVariableList(const Settings* settings, const Token* start) +void Scope::getVariableList(const Settings* settings, const Token* start, const Token* end) { // Variable declared in condition: if (auto x = bar()) if (Token::Match(classDef, "if|while ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) { @@ -4214,7 +4214,7 @@ void Scope::getVariableList(const Settings* settings, const Token* start) } AccessControl varaccess = defaultAccess(); - for (const Token *tok = start; tok && tok != bodyEnd; tok = tok->next()) { + for (const Token *tok = start; tok && tok != end; tok = tok->next()) { // syntax error? if (tok->next() == nullptr) break; diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 993f8166d..73b95ef24 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1204,7 +1204,7 @@ private: void findFunctionInBase(const std::string & name, nonneg int args, std::vector & matches) const; /** @brief initialize varlist */ - void getVariableList(const Settings* settings, const Token *start); + void getVariableList(const Settings* settings, const Token *start, const Token *end); }; enum class Reference { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 5dde9d7e1..cf0b4f225 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -355,6 +355,7 @@ private: TEST_CASE(createSymbolDatabaseFindAllScopes1); TEST_CASE(createSymbolDatabaseFindAllScopes2); + TEST_CASE(createSymbolDatabaseFindAllScopes3); TEST_CASE(enum1); TEST_CASE(enum2); @@ -4811,6 +4812,33 @@ private: ASSERT(var2->variable()); } + void createSymbolDatabaseFindAllScopes3() { + GET_SYMBOL_DB("namespace ns {\n" + "\n" + "namespace ns_details {\n" + "template struct has_A : std::false_type {};\n" + "template struct has_A::type> : std::true_type {};\n" + "template struct is_A_impl : public std::is_trivially_copyable {};\n" + "template struct is_A_impl : public std::is_same {};\n" + "}\n" + "\n" + "template struct is_A : ns_details::is_A_impl::value> {};\n" + "template struct is_A> : std::integral_constant::value && is_A::value> {};\n" + "}\n" + "\n" + "extern \"C\" {\n" + "static const int foo = 8;\n" + "}\n"); + ASSERT(db); + ASSERT_EQUALS(6, db->scopeList.size()); + ASSERT_EQUALS(1, db->scopeList.front().varlist.size()); + auto list = db->scopeList; + list.pop_front(); + for (const auto &scope : list) { + ASSERT_EQUALS(0, scope.varlist.size()); + } + } + void enum1() { GET_SYMBOL_DB("enum BOOL { FALSE, TRUE }; enum BOOL b;");