errmsg: Added "Unused private function ..."

This commit is contained in:
Daniel Marjamäki 2009-01-10 17:35:41 +00:00
parent dc6317878e
commit 7b10b0ff4d
5 changed files with 33 additions and 14 deletions

View File

@ -476,7 +476,7 @@ void CheckClass::privateFunctions()
continue;
// Get private functions..
std::list<std::string> FuncList;
std::list<const Token *> 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<const Token *>::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();
}

View File

@ -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);

View File

@ -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 + "";

View File

@ -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());
}

View File

@ -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"));