Skip to content

Commit

Permalink
Fix #11904 (Scope::findFunction: better handling when non-virtual met…
Browse files Browse the repository at this point in the history
…hod with same name and arguments exists both in base class and derived class)
  • Loading branch information
danmar committed Aug 31, 2023
1 parent 98401a3 commit 361ccea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

//---------------------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int> v);\n"
Expand Down

0 comments on commit 361ccea

Please sign in to comment.