Fixed #2089 (False negative: Function can be const (calling const function))

This commit is contained in:
Robert Reif 2010-10-13 07:26:41 +02:00 committed by Daniel Marjamäki
parent 2334192bff
commit a58094e827
3 changed files with 43 additions and 4 deletions

View File

@ -2139,6 +2139,37 @@ bool CheckClass::isMemberVar(const SpaceInfo *info, const Token *tok)
return false;
}
bool CheckClass::isConstMemberFunc(const SpaceInfo *info, const Token *tok)
{
std::list<Func>::const_iterator func;
for (func = info->functionList.begin(); func != info->functionList.end(); ++func)
{
if (func->tokenDef->str() == tok->str() && func->isConst)
return true;
}
// not found in this class
if (!info->derivedFrom.empty())
{
// check each base class
for (unsigned int i = 0; i < info->derivedFrom.size(); ++i)
{
// find the base class
const SpaceInfo *spaceInfo = info->derivedFrom[i].spaceInfo;
// find the function in the base class
if (spaceInfo)
{
if (isConstMemberFunc(spaceInfo, tok))
return true;
}
}
}
return false;
}
bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok)
{
// if the function doesn't have any assignment nor function call,
@ -2200,9 +2231,16 @@ bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok)
}
// function call..
else if ((Token::Match(tok1, "%var% (") &&
!(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType())) ||
Token::Match(tok1, "%var% < %any% > ("))
else if (Token::Match(tok1, "%var% (") &&
!(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType()))
{
if (!isConstMemberFunc(info, tok1))
{
isconst = false;
break;
}
}
else if (Token::Match(tok1, "%var% < %any% > ("))
{
isconst = false;
break;

View File

@ -305,6 +305,7 @@ private:
bool argsMatch(const Token *first, const Token *second, const std::string &path, unsigned int depth) const;
bool isMemberVar(const SpaceInfo *info, const Token *tok);
bool isConstMemberFunc(const SpaceInfo *info, const Token *tok);
bool checkConstFunc(const SpaceInfo *info, const Token *tok);
const Token *initBaseInfo(SpaceInfo *info, const Token *tok);

View File

@ -4130,7 +4130,7 @@ private:
" void f() const { };\n"
" void a() { f(); };\n"
"};\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::a' can be const\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::a' can be const\n", errout.str());
// ticket #1593
checkConst("#include <vector>\n"