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

This commit is contained in:
Daniel Marjamäki 2023-08-31 14:44:44 +02:00 committed by GitHub
parent ad1caa8100
commit 1b061564d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -5467,7 +5467,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
addMatchingFunctions(this); addMatchingFunctions(this);
// check in anonumous namespaces // check in anonymous namespaces
for (const Scope *nestedScope : nestedList) { for (const Scope *nestedScope : nestedList) {
if (nestedScope->type == eNamespace && nestedScope->className.empty()) if (nestedScope->type == eNamespace && nestedScope->className.empty())
addMatchingFunctions(nestedScope); addMatchingFunctions(nestedScope);
@ -5687,7 +5687,22 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
if (matches.size() == 1) if (matches.size() == 1)
return matches[0]; 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;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -442,6 +442,7 @@ private:
TEST_CASE(findFunction47); TEST_CASE(findFunction47);
TEST_CASE(findFunction48); TEST_CASE(findFunction48);
TEST_CASE(findFunction49); // #11888 TEST_CASE(findFunction49); // #11888
TEST_CASE(findFunction50); // #11904 - method with same name and arguments in derived class
TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionContainer);
TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionExternC);
TEST_CASE(findFunctionGlobalScope); // ::foo TEST_CASE(findFunctionGlobalScope); // ::foo
@ -7304,6 +7305,15 @@ private:
ASSERT_EQUALS(4, ftok->function()->tokenDef->linenr()); 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() { void findFunctionContainer() {
{ {
GET_SYMBOL_DB("void dostuff(std::vector<int> v);\n" GET_SYMBOL_DB("void dostuff(std::vector<int> v);\n"