fix #3040 (False positive - Technically the member function 'PSIPTable::SetSection' can be const.)

This commit is contained in:
Robert Reif 2011-08-22 20:34:00 -04:00
parent 88b11d889c
commit c7cb38b0b5
2 changed files with 42 additions and 2 deletions

View File

@ -1493,15 +1493,25 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
unsigned int args = countParameters(tok);
std::list<Function>::const_iterator func;
unsigned int matches = 0;
unsigned int consts = 0;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func)
{
/** @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;
if (func->tokenDef->str() == tok->str() && func->argCount() == args)
{
matches++;
if (func->isConst)
consts++;
}
}
// if there are multiple matches that are all const, return const
if (matches > 0 && matches == consts)
return true;
// not found in this class
if (!scope->derivedFrom.empty())
{

View File

@ -178,6 +178,7 @@ private:
TEST_CASE(const48); // ticket #2672
TEST_CASE(const49); // ticket #2795
TEST_CASE(const50); // ticket #2943
TEST_CASE(const51); // ticket #3040
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const
@ -5630,6 +5631,35 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const51() // ticket 3040
{
checkConst("class PSIPTable {\n"
"public:\n"
" PSIPTable() : _pesdata(0) { }\n"
" const unsigned char* pesdata() const { return _pesdata; }\n"
" unsigned char* pesdata() { return _pesdata; }\n"
" void SetSection(uint num) { pesdata()[6] = num; }\n"
"private:\n"
" unsigned char *_pesdata;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class PESPacket {\n"
"public:\n"
" PESPacket() : _pesdata(0) { }\n"
" const unsigned char* pesdata() const { return _pesdata; }\n"
" unsigned char* pesdata() { return _pesdata; }\n"
"private:\n"
" unsigned char *_pesdata;\n"
"};\n"
"class PSIPTable : public PESPacket\n"
"{\n"
"public:\n"
" void SetSection(uint num) { pesdata()[6] = num; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation()
{
checkConst("struct s\n"