Fixed #1921 (false positive: the function '...' can be declared as const (inline friend))

This commit is contained in:
Daniel Marjamäki 2010-08-07 16:08:44 +02:00
parent ed4f1164a1
commit 262885f3e0
3 changed files with 20 additions and 1 deletions

View File

@ -184,6 +184,13 @@ void CheckClass::createSymbolDatabase()
break; break;
} }
// friend function
else if (tok1->previous()->str() == "friend")
{
function.isFriend = true;
break;
}
tok1 = tok1->previous(); tok1 = tok1->previous();
} }
@ -1867,7 +1874,7 @@ void CheckClass::checkConst()
const Func & func = *it1; const Func & func = *it1;
// does the function have a body? // does the function have a body?
if (func.type == Func::Function && func.hasBody && !func.isStatic && !func.isConst && !func.isVirtual) if (func.type == Func::Function && func.hasBody && !func.isFriend && !func.isStatic && !func.isConst && !func.isVirtual)
{ {
// get function name // get function name
const std::string functionName((func.tokenDef->isName() ? "" : "operator") + func.tokenDef->str()); const std::string functionName((func.tokenDef->isName() ? "" : "operator") + func.tokenDef->str());

View File

@ -181,6 +181,7 @@ private:
isConst(false), isConst(false),
isVirtual(false), isVirtual(false),
isStatic(false), isStatic(false),
isFriend(false),
isOperator(false), isOperator(false),
type(Function) type(Function)
{ {
@ -194,6 +195,7 @@ private:
bool isConst; // is const bool isConst; // is const
bool isVirtual; // is virtual bool isVirtual; // is virtual
bool isStatic; // is static bool isStatic; // is static
bool isFriend; // is friend
bool isOperator; // is operator bool isOperator; // is operator
Type type; // constructor, destructor, ... Type type; // constructor, destructor, ...
}; };

View File

@ -141,6 +141,7 @@ private:
TEST_CASE(constFunc); // a function that calls const functions can be const TEST_CASE(constFunc); // a function that calls const functions can be const
TEST_CASE(constVirtualFunc); TEST_CASE(constVirtualFunc);
TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if
TEST_CASE(constFriend); // ticket #1921 - fp for friend function
} }
// Check the operator Equal // Check the operator Equal
@ -3870,6 +3871,15 @@ private:
checkConst(code, &settings); checkConst(code, &settings);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void constFriend() // ticket #1921
{
const char code[] = "class foo {\n"
" friend void f() { }\n"
"};";
checkConst(code);
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestClass) REGISTER_TEST(TestClass)