Skip to content

Commit bc2755d

Browse files
Fix #13975 SymbolDatabase: wrongly set function pointer when called constructor is implicitly defined (danmar#7637)
1 parent 7c1981b commit bc2755d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lib/symboldatabase.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5586,7 +5586,7 @@ bool Scope::hasInlineOrLambdaFunction(const Token** tokStart, bool onlyInline) c
55865586
});
55875587
}
55885588

5589-
void Scope::findFunctionInBase(const std::string & name, nonneg int args, std::vector<const Function *> & matches) const
5589+
void Scope::findFunctionInBase(const Token* tok, nonneg int args, std::vector<const Function *> & matches) const
55905590
{
55915591
if (isClassOrStruct() && definedType && !definedType->derivedFrom.empty()) {
55925592
const std::vector<Type::BaseInfo> &derivedFrom = definedType->derivedFrom;
@@ -5596,16 +5596,18 @@ void Scope::findFunctionInBase(const std::string & name, nonneg int args, std::v
55965596
if (base->classScope == this) // Ticket #5120, #5125: Recursive class; tok should have been found already
55975597
continue;
55985598

5599-
auto range = utils::as_const(base->classScope->functionMap).equal_range(name);
5599+
auto range = utils::as_const(base->classScope->functionMap).equal_range(tok->str());
56005600
for (auto it = range.first; it != range.second; ++it) {
56015601
const Function *func = it->second;
5602+
if (func->isDestructor() && !Token::simpleMatch(tok->tokAt(-1), "~"))
5603+
continue;
56025604
if ((func->isVariadic() && args >= (func->argCount() - 1)) ||
56035605
(args == func->argCount() || (args < func->argCount() && args >= func->minArgCount()))) {
56045606
matches.push_back(func);
56055607
}
56065608
}
56075609

5608-
base->classScope->findFunctionInBase(name, args, matches);
5610+
base->classScope->findFunctionInBase(tok, args, matches);
56095611
}
56105612
}
56115613
}
@@ -5791,7 +5793,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
57915793
const std::size_t numberOfMatchesNonBase = matches.size();
57925794

57935795
// check in base classes
5794-
findFunctionInBase(tok->str(), args, matches);
5796+
findFunctionInBase(tok, args, matches);
57955797

57965798
// Non-call => Do not match parameters
57975799
if (!isCall) {

lib/symboldatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ class CPPCHECKLIB Scope {
12051205
*/
12061206
bool isVariableDeclaration(const Token* tok, const Token*& vartok, const Token*& typetok) const;
12071207

1208-
void findFunctionInBase(const std::string & name, nonneg int args, std::vector<const Function *> & matches) const;
1208+
void findFunctionInBase(const Token* tok, nonneg int args, std::vector<const Function *> & matches) const;
12091209

12101210
/** @brief initialize varlist */
12111211
void getVariableList(const Token *start, const Token *end);

test/testsymboldatabase.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ class TestSymbolDatabase : public TestFixture {
532532
TEST_CASE(findFunction58); // #13310
533533
TEST_CASE(findFunction59);
534534
TEST_CASE(findFunction60);
535+
TEST_CASE(findFunction61);
535536
TEST_CASE(findFunctionRef1);
536537
TEST_CASE(findFunctionRef2); // #13328
537538
TEST_CASE(findFunctionContainer);
@@ -8650,6 +8651,19 @@ class TestSymbolDatabase : public TestFixture {
86508651
ASSERT(fun && !fun->function());
86518652
}
86528653

8654+
void findFunction61() {
8655+
GET_SYMBOL_DB("namespace N {\n" // #13975
8656+
" struct B {\n"
8657+
" virtual ~B() = default;\n"
8658+
" };\n"
8659+
" struct D : B {\n"
8660+
" D() : B() {}\n"
8661+
" };\n"
8662+
"}\n");
8663+
const Token* fun = Token::findsimplematch(tokenizer.tokens(), "B ( ) {");
8664+
ASSERT(fun && !fun->function());
8665+
}
8666+
86538667
void findFunctionRef1() {
86548668
GET_SYMBOL_DB("struct X {\n"
86558669
" const std::vector<int> getInts() const & { return mInts; }\n"

0 commit comments

Comments
 (0)