Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11904 (Scope::findFunction: better handling when non-virtual method with same name and arguments exists both in base class and derived class) #5379

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading