fix #2670 (False positive: function can be const, overloaded functions)

This commit is contained in:
Robert Reif 2011-03-22 19:23:36 -04:00
parent 5cdd635701
commit d36ed9aff1
2 changed files with 55 additions and 1 deletions

View File

@ -1446,13 +1446,45 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok)
return false;
}
static int countParameters(const Token *tok)
{
if (Token::Match(tok->tokAt(2), "void| )"))
return 0;
int numpar = 1;
int parlevel = 0;
for (; tok; tok = tok->next())
{
if (tok->str() == "(")
++parlevel;
else if (tok->str() == ")")
{
if (parlevel <= 1)
break;
--parlevel;
}
else if (parlevel == 1 && tok->str() == ",")
{
++numpar;
}
}
return numpar;
}
bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
{
unsigned int args = countParameters(tok);
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func)
{
if (func->tokenDef->str() == tok->str() && func->isConst)
/** @todo we need to look at the argument types when there are overloaded functions
* with the same number of arguments */
if (func->tokenDef->str() == tok->str() && func->argCount() == args && func->isConst)
return true;
}

View File

@ -167,6 +167,7 @@ private:
TEST_CASE(const44); // ticket #2595
TEST_CASE(const45); // ticket #2664
TEST_CASE(const46); // ticket #2636
TEST_CASE(const47); // ticket #2670
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const
@ -5228,6 +5229,27 @@ private:
"[test.cpp:7]: (information) Technically the member function 'Altren::fun2' can be const.\n", errout.str());
}
void const47() // ticket 2670
{
checkConst("class Altren {\n"
"public:\n"
" void foo() { delete this; }\n"
" void foo(int i) const { }\n"
" void bar() { foo(); }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Altren {\n"
"public:\n"
" void foo() { delete this; }\n"
" void foo(int i) const { }\n"
" void bar() { foo(1); }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (information) Technically the member function 'Altren::bar' can be const.\n", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation()
{
checkConst("struct s\n"