Skip to content

Commit ea215c3

Browse files
committed
Fixed false positives in CheckClass::checkConst() due to unmatched function overloads
Fixed function matching if constness mismatches
1 parent 4d6cb8d commit ea215c3

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

lib/checkclass.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,23 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const
18321832

18331833
bool CheckClass::isMemberFunc(const Scope *scope, const Token *tok) const
18341834
{
1835-
if (tok->function() && tok->function()->nestedIn == scope)
1835+
if (!tok->function()) {
1836+
for (std::list<Function>::const_iterator i = scope->functionList.cbegin(); i != scope->functionList.cend(); ++i) {
1837+
if (i->name() == tok->str()) {
1838+
const Token* tok2 = tok->tokAt(2);
1839+
size_t argsPassed = tok2->str() == ")" ? 0 : 1;
1840+
for (;;) {
1841+
tok2 = tok2->nextArgument();
1842+
if (tok2)
1843+
argsPassed++;
1844+
else
1845+
break;
1846+
}
1847+
if (argsPassed == i->argCount() || (argsPassed < i->argCount() && argsPassed >= i->minArgCount()))
1848+
return true;
1849+
}
1850+
}
1851+
} else if (tok->function()->nestedIn == scope)
18361852
return !tok->function()->isStatic();
18371853

18381854
// not found in this class
@@ -1855,7 +1871,9 @@ bool CheckClass::isMemberFunc(const Scope *scope, const Token *tok) const
18551871

18561872
bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
18571873
{
1858-
if (tok->function() && tok->function()->nestedIn == scope)
1874+
if (!tok->function())
1875+
return false;
1876+
else if (tok->function()->nestedIn == scope)
18591877
return tok->function()->isConst();
18601878

18611879
// not found in this class

lib/symboldatabase.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,6 +3846,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
38463846
// check each function against the arguments in the function call for a match
38473847
for (std::size_t i = 0; i < matches.size();) {
38483848
bool erased = false;
3849+
bool constFallback = false;
38493850
const Function * func = matches[i];
38503851
size_t same = 0;
38513852

@@ -3857,9 +3858,12 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
38573858
if (scope && scope->functionOf && scope->functionOf->isClassOrStruct()) {
38583859
// check if isConst mismatches
38593860
if (!(scope->function && scope->function->isConst() == func->isConst())) {
3860-
if (!erased)
3861-
++i;
3862-
continue;
3861+
if (scope->function->isConst()) {
3862+
if (!erased)
3863+
++i;
3864+
continue;
3865+
}
3866+
constFallback = true;
38633867
}
38643868
}
38653869
}
@@ -4072,10 +4076,14 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
40724076
size_t hasToBe = func->isVariadic() ? (func->argCount() - 1) : args;
40734077

40744078
// check if all arguments matched
4075-
if (same == hasToBe)
4076-
return func;
4079+
if (same == hasToBe) {
4080+
if (constFallback)
4081+
fallback1Func = func;
4082+
else
4083+
return func;
4084+
}
40774085

4078-
if (!fallback1Func) {
4086+
else if (!fallback1Func) {
40794087
if (same + fallback1 == hasToBe)
40804088
fallback1Func = func;
40814089
else if (!fallback2Func && same + fallback2 + fallback1 == hasToBe)

test/testsymboldatabase.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class TestSymbolDatabase: public TestFixture {
287287
TEST_CASE(findFunction15);
288288
TEST_CASE(findFunction16);
289289
TEST_CASE(findFunction17);
290+
TEST_CASE(findFunction18);
290291

291292
TEST_CASE(noexceptFunction1);
292293
TEST_CASE(noexceptFunction2);
@@ -3785,6 +3786,22 @@ class TestSymbolDatabase: public TestFixture {
37853786
ASSERT_EQUALS(true, f && f->function() && f->function()->tokenDef->linenr() == 2);
37863787
}
37873788

3789+
void findFunction18() {
3790+
GET_SYMBOL_DB("class Fred {\n"
3791+
" void f(int i) { }\n"
3792+
" void f(float f) const { }\n"
3793+
" void a() { f(1); }\n"
3794+
" void b() { f(1.f); }\n"
3795+
"};");
3796+
3797+
ASSERT_EQUALS("", errout.str());
3798+
3799+
const Token *f = Token::findsimplematch(tokenizer.tokens(), "f ( 1 ) ;");
3800+
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 2);
3801+
3802+
f = Token::findsimplematch(tokenizer.tokens(), "f ( 1.f ) ;");
3803+
ASSERT_EQUALS(true, f && f->function() && f->function()->tokenDef->linenr() == 3);
3804+
}
37883805

37893806
#define FUNC(x) const Function *x = findFunctionByName(#x, &db->scopeList.front()); \
37903807
ASSERT_EQUALS(true, x != nullptr); \

0 commit comments

Comments
 (0)