unused private functions: don't check classes declared in header files unless it is known that their whole implementation is seen

This commit is contained in:
Daniel Marjamäki 2009-07-12 14:23:01 +02:00
parent dfdbf85730
commit b86b07827a
2 changed files with 28 additions and 2 deletions

View File

@ -439,6 +439,12 @@ void CheckClass::privateFunctions()
// Locate some class // Locate some class
for (const Token *tok1 = Token::findmatch(_tokenizer->tokens(), "class %var% {"); tok1; tok1 = Token::findmatch(tok1->next(), "class %var% {")) for (const Token *tok1 = Token::findmatch(_tokenizer->tokens(), "class %var% {"); tok1; tok1 = Token::findmatch(tok1->next(), "class %var% {"))
{ {
// If the class implementation is incomplete there may be false positives about unused private functions.
// Therefore I only check classes that are declared in the source file.
// Todo: check classes that are declared in header file too. make sure the whole implementation is seen.
if (tok1->fileIndex() != 0)
continue;
const std::string &classname = tok1->next()->str(); const std::string &classname = tok1->next()->str();
// Get private functions.. // Get private functions..

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(classInClass); TEST_CASE(classInClass);
TEST_CASE(sameFunctionNames); TEST_CASE(sameFunctionNames);
TEST_CASE(incompleteImplementation);
} }
@ -102,7 +103,7 @@ private:
"unsigned int Fred::f()\n" "unsigned int Fred::f()\n"
"{ }\n"); "{ }\n");
ASSERT_EQUALS("[p.h:4]: (style) Unused private function 'Fred::f'\n", errout.str()); TODO_ASSERT_EQUALS("[p.h:4]: (style) Unused private function 'Fred::f'\n", errout.str());
check("#file \"p.h\"\n" check("#file \"p.h\"\n"
"class Fred\n" "class Fred\n"
@ -118,7 +119,7 @@ private:
"{\n" "{\n"
"}\n" "}\n"
"\n"); "\n");
ASSERT_EQUALS("[p.h:4]: (style) Unused private function 'Fred::f'\n", errout.str()); TODO_ASSERT_EQUALS("[p.h:4]: (style) Unused private function 'Fred::f'\n", errout.str());
// Don't warn about include files which implementation we don't see // Don't warn about include files which implementation we don't see
check("#file \"p.h\"\n" check("#file \"p.h\"\n"
@ -272,6 +273,25 @@ private:
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void incompleteImplementation()
{
// The implementation for "A::a" is missing - so don't check if
// "A::b" is used or not
check("#file \"test.h\"\n"
"class A\n"
"{\n"
"public:\n"
" A()\n"
" void a();\n"
"private:\n"
" void b();\n"
"};\n"
"#endfile\n"
"A::A() { }\n"
"void A::b() { }\n");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestUnusedPrivateFunction) REGISTER_TEST(TestUnusedPrivateFunction)