From b86b07827a0677a8bc790fc8af243e077d266b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 12 Jul 2009 14:23:01 +0200 Subject: [PATCH] unused private functions: don't check classes declared in header files unless it is known that their whole implementation is seen --- src/checkclass.cpp | 6 ++++++ test/testunusedprivfunc.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/checkclass.cpp b/src/checkclass.cpp index 6249e3898..12bee7f63 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -439,6 +439,12 @@ void CheckClass::privateFunctions() // Locate some class 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(); // Get private functions.. diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index 386cf09e9..ea27c98a8 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -46,6 +46,7 @@ private: TEST_CASE(classInClass); TEST_CASE(sameFunctionNames); + TEST_CASE(incompleteImplementation); } @@ -102,7 +103,7 @@ private: "unsigned int Fred::f()\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" "class Fred\n" @@ -118,7 +119,7 @@ private: "{\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 check("#file \"p.h\"\n" @@ -272,6 +273,25 @@ private: "};"); 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)