diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 12374595a..ec3bce8ba 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1245,6 +1245,11 @@ void CheckClass::privateFunctions() if (!_settings->_checkCodingStyle) return; + // don't check code that contains templates. Templates that are + // "unused" are removed from the code. #2067 + if (_tokenizer->codeWithTemplates()) + return; + // dont check borland classes with properties.. if (Token::findmatch(_tokenizer->tokens(), "; __property ;")) return; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 39564d9aa..79ff25ff3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -50,6 +50,7 @@ Tokenizer::Tokenizer() { _tokens = 0; _tokensBack = 0; + _codeWithTemplates = false; } Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger) @@ -57,6 +58,7 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger) { _tokens = 0; _tokensBack = 0; + _codeWithTemplates = false; } Tokenizer::~Tokenizer() @@ -2308,6 +2310,8 @@ void Tokenizer::simplifyTemplates() { if (Token::simpleMatch(tok, "template <")) { + _codeWithTemplates = true; + for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { // Just a declaration => ignore this diff --git a/lib/tokenize.h b/lib/tokenize.h index 3ccebf7c1..bfdb98103 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -516,6 +516,12 @@ public: void unsupportedTypedef(const Token *tok) const; + /** Was there templates in the code? */ + bool codeWithTemplates() const + { + return _codeWithTemplates; + } + private: /** Disable copy constructor, no implementation */ Tokenizer(const Tokenizer &); @@ -532,6 +538,12 @@ private: /** E.g. "A" for code where "#ifdef A" is true. This is used to print additional information in error situations. */ std::string _configuration; + + /** + * was there any templates? templates that are "unused" are + * removed from the token list + */ + bool _codeWithTemplates; }; /// @} diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index 0b099189c..f039bd541 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -54,6 +54,9 @@ private: TEST_CASE(derivedClass); // skip warning for derived classes. It might be a virtual function. TEST_CASE(borland); // skip FP when using __property + + // No false positives when there are "unused" templates that are removed in the simplified token list + TEST_CASE(template1); } @@ -391,6 +394,25 @@ private: "};"); ASSERT_EQUALS("", errout.str()); } + + void template1() + { + // ticket #2067 - Template methods do not "use" private ones + check("class A {\n" + "public:\n" + " template \n" + " T getVal() const;\n" + "\n" + "private:\n" + " int internalGetVal() const { return 8000; }\n" + "};\n" + "\n" + "template \n" + "T A::getVal() const {\n" + " return internalGetVal();\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUnusedPrivateFunction)