From b7988a3dab0ce390d7f50c7857d7e7022f1def8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 3 Dec 2011 12:19:26 +0100 Subject: [PATCH] Fixed #3336 (False positive: Member function is not used (used by template function)) --- lib/checkunusedfunctions.cpp | 8 ++++++++ lib/checkunusedfunctions.h | 6 ++++-- test/testunusedfunctions.cpp | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 663ad3790..07cd3c89a 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -33,6 +33,11 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer) { + // if there are templates there might be false positives + templates |= tokenizer.codeWithTemplates(); + if (templates) + return; + // Function declarations.. for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { if (tok->fileIndex() != 0) @@ -144,6 +149,9 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer) void CheckUnusedFunctions::check(ErrorLogger * const errorLogger) { + if (templates) + return; + for (std::map::const_iterator it = _functions.begin(); it != _functions.end(); ++it) { const FunctionUsage &func = it->second; if (func.usedOtherFile || func.filename.empty()) diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index d78c25a47..7b0afc44c 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -32,12 +32,12 @@ class CheckUnusedFunctions: public Check { public: /** @brief This constructor is used when registering the CheckUnusedFunctions */ - CheckUnusedFunctions() : Check(myName()) + CheckUnusedFunctions() : Check(myName()), templates(false) { } /** @brief This constructor is used when running checks. */ CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) - : Check(myName(), tokenizer, settings, errorLogger) + : Check(myName(), tokenizer, settings, errorLogger), templates(false) { } // Parse current tokens and determine.. @@ -88,6 +88,8 @@ private: }; std::map _functions; + + bool templates; }; /// @} //--------------------------------------------------------------------------- diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 41a6f118d..2cd4f6131 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -40,6 +40,7 @@ private: TEST_CASE(else1); TEST_CASE(functionpointer); TEST_CASE(template1); + TEST_CASE(template2); TEST_CASE(throwIsNotAFunction); TEST_CASE(unusedError); TEST_CASE(unusedMain); @@ -146,6 +147,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void template2() { + check("void f() { }\n" + "\n" + "template void g()\n" + "{\n" + " f();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void throwIsNotAFunction() { check("struct A {void f() const throw () {}}; int main() {A a; a.f();}\n"); ASSERT_EQUALS("", errout.str());