From c66a1291978db8fbfeced12140f763213e798b39 Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 17 Apr 2023 16:59:31 +0200 Subject: [PATCH 1/2] Fix #8592 SymbolDatabase: better handling of 'using namespace' --- lib/symboldatabase.cpp | 14 +++++++++++++- test/testsymboldatabase.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 230b50bed31..53ed86cb05d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5547,7 +5547,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const //--------------------------------------------------------------------------- -const Function* SymbolDatabase::findFunction(const Token *tok) const +const Function* SymbolDatabase::findFunction(const Token* const tok) const { // find the scope this function is in const Scope *currScope = tok->scope(); @@ -5663,6 +5663,18 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const return func; currScope = currScope->nestedIn; } + // check using namespace + currScope = tok->scope(); + while (currScope) { + for (const auto& ul : currScope->usingList) { + if (ul.scope) { + const Function* func = ul.scope->findFunction(tok); + if (func) + return func; + } + } + currScope = currScope->nestedIn; + } } // Check for constructor if (Token::Match(tok, "%name% (|{")) { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 9b144fcb6d5..956c5a27100 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -443,6 +443,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(findFunction44); // #11182 TEST_CASE(findFunction45); TEST_CASE(findFunction46); + TEST_CASE(findFunction47); TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionGlobalScope); // ::foo @@ -7149,6 +7150,37 @@ class TestSymbolDatabase : public TestFixture { ASSERT_EQUALS(3, functok->function()->tokenDef->linenr()); } + void findFunction47() { // #8592 + { + GET_SYMBOL_DB("namespace N {\n" + " void toupper(std::string& str);\n" + "}\n" + "void f(std::string s) {\n" + " using namespace N;\n" + " toupper(s);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "toupper ( s"); + ASSERT(functok && functok->function()); + ASSERT(functok->function()->name() == "toupper"); + ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); + } + { + GET_SYMBOL_DB("namespace N {\n" + " void toupper(std::string& str);\n" + "}\n" + "using namespace N;\n" + "void f(std::string s) {\n" + " toupper(s);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "toupper ( s"); + ASSERT(functok && functok->function()); + ASSERT(functok->function()->name() == "toupper"); + ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); + } + } + void findFunctionContainer() { { GET_SYMBOL_DB("void dostuff(std::vector v);\n" From 480b400154453cd779086119d3b316e8f3432bac Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 21 Apr 2023 14:29:38 +0200 Subject: [PATCH 2/2] Fix merge --- test/testsymboldatabase.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 21fd9fbabff..42c5379c1ee 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -7185,6 +7185,7 @@ class TestSymbolDatabase : public TestFixture { ASSERT(functok->function()->name() == "toupper"); ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); } + } void findFunction48() { GET_SYMBOL_DB("struct S {\n"