diff --git a/src/checkclass.cpp b/src/checkclass.cpp index bbc0390f3..7d0fc2e37 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -476,7 +476,7 @@ void CheckClass::privateFunctions() continue; // Get private functions.. - std::list FuncList; + std::list FuncList; FuncList.clear(); bool priv = false; unsigned int indent_level = 0; @@ -511,7 +511,7 @@ void CheckClass::privateFunctions() if (Token::Match(tok, "%var% (") && !Token::Match(tok, classname.c_str())) { - FuncList.push_back(tok->str()); + FuncList.push_back(tok); } } } @@ -552,7 +552,17 @@ void CheckClass::privateFunctions() --indent_level; } if (Token::Match(ftok->next(), "(")) - FuncList.remove(ftok->str()); + { + // Remove function from FuncList + for (std::list::iterator it = FuncList.begin(); it != FuncList.end(); ++it) + { + if (ftok->str() == (*it)->str()) + { + FuncList.remove(*it); + break; + } + } + } ftok = ftok->next(); } } @@ -564,12 +574,10 @@ void CheckClass::privateFunctions() while (HasFuncImpl && !FuncList.empty()) { // Final check; check if the function pointer is used somewhere.. - const std::string _pattern("return|(|)|,|= " + FuncList.front()); + const std::string _pattern("return|(|)|,|= " + FuncList.front()->str()); if (!Token::findmatch(_tokenizer->tokens(), _pattern.c_str())) { - std::ostringstream ostr; - ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'"; - _errorLogger->reportErr(ostr.str()); + _errorLogger->reportErr(ErrorMessage::unusedPrivateFunction(_tokenizer, FuncList.front(), classname, FuncList.front()->str())); } FuncList.pop_front(); } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 502b2c6b2..de0badd15 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -305,11 +305,14 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (ErrorMessage::dangerousUsageStrtol(_settings)) checkOther.InvalidFunctionUsage(); - if (_settings._checkCodingStyle) - { - // Check that all private functions are called. + // Check that all private functions are called. + if (ErrorMessage::unusedPrivateFunction(_settings)) checkClass.privateFunctions(); + + + if (_settings._checkCodingStyle) + { checkClass.operatorEq(); // if (condition); diff --git a/src/errormessage.h b/src/errormessage.h index 7b5af16c9..a4cc5c0bc 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -48,6 +48,15 @@ public: return true; } + static std::string unusedPrivateFunction(const Tokenizer *tokenizer, const Token *Location, const std::string &classname, const std::string &funcname) + { + return msg1(tokenizer, Location) + "Unused private function '" + classname + "::" + funcname + "'"; + } + static bool unusedPrivateFunction(const Settings &s) + { + return s._checkCodingStyle; + } + static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index b80d4113c..ff389c63b 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -77,7 +77,7 @@ private: "unsigned int Fred::f()\n" "{ }\n"); - ASSERT_EQUALS(std::string("Class 'Fred', unused private function: 'f'\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:4]: Unused private function 'Fred::f'\n"), errout.str()); } @@ -107,9 +107,7 @@ private: "Fred::Fred()\n" "{}\n"); - std::string str(errout.str()); - - ASSERT_EQUALS(std::string("Class 'Fred', unused private function: 'get'\n"), str); + ASSERT_EQUALS(std::string("[test.cpp:6]: Unused private function 'Fred::get'\n"), errout.str()); } diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 3208b1952..e3ab7b127 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -56,6 +56,7 @@ int main() // checkclass.cpp.. err.push_back(Message("noConstructor", Message::STYLE, "The class '%1' has no constructor", "classname")); err.push_back(Message("uninitVar", 0, "Uninitialized member variable '%1::%2'", "classname", "varname")); + err.push_back(Message("unusedPrivateFunction", Message::STYLE, "Unused private function '%1::%2'", "classname", "funcname")); // checkmemoryleak.cpp.. err.push_back(Message("memleak", 0, "Memory leak: %1", "varname"));