diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 01d2a94f142..c9934166652 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5357,7 +5357,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const addMatchingFunctions(this); - // check in anonumous namespaces + // check in anonymous namespaces for (const Scope *nestedScope : nestedList) { if (nestedScope->type == eNamespace && nestedScope->className.empty()) addMatchingFunctions(nestedScope); @@ -5577,7 +5577,22 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const if (matches.size() == 1) return matches[0]; - return nullptr; + // Prioritize matches in derived scopes + const Function* ret = nullptr; + for (int i = 0; i < fallback1Func.size(); ++i) { + if (std::find(matches.cbegin(), matches.cend(), fallback1Func[i]) == matches.cend()) + continue; + if (this == fallback1Func[i]->nestedIn) { + if (!ret) + ret = fallback1Func[i]; + else { + ret = nullptr; + break; + } + } + } + + return ret; } //--------------------------------------------------------------------------- diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 13c7846061f..a78b6ffc5d2 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -442,6 +442,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(findFunction47); TEST_CASE(findFunction48); TEST_CASE(findFunction49); // #11888 + TEST_CASE(findFunction50); // #11904 - method with same name and arguments in derived class TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionGlobalScope); // ::foo @@ -7304,6 +7305,15 @@ class TestSymbolDatabase : public TestFixture { ASSERT_EQUALS(4, ftok->function()->tokenDef->linenr()); } + void findFunction50() { + GET_SYMBOL_DB("struct B { B(); void init(unsigned int value); };\n" + "struct D: B { D(); void init(unsigned int value); };\n" + "D::D() { init(0); }\n" + "void D::init(unsigned int value) {}\n"); + const Token* call = Token::findsimplematch(tokenizer.tokens(), "init ( 0 ) ;"); + ASSERT(call && call->function() && call->function()->functionScope); + } + void findFunctionContainer() { { GET_SYMBOL_DB("void dostuff(std::vector v);\n"