Class (unused private functions): Reactivated and improved this check

This commit is contained in:
Daniel Marjamäki 2007-05-11 17:13:04 +00:00
parent 0fd16be44b
commit dad497f314
1 changed files with 40 additions and 6 deletions

View File

@ -72,7 +72,7 @@ static void CppCheck(const char FileName[])
//std::ofstream f("tokens.txt"); //std::ofstream f("tokens.txt");
//for (TOKEN *tok = tokens; tok; tok = tok->next) //for (TOKEN *tok = tokens; tok; tok = tok->next)
// f << tok->linenr << ":" << tok->str << '\n'; // f << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]:" << tok->str << '\n';
//f.close(); //f.close();
// Check that all class constructors are ok. // Check that all class constructors are ok.
@ -81,7 +81,7 @@ static void CppCheck(const char FileName[])
// Check that all private functions are called. // Check that all private functions are called.
// Temporarily inactivated to avoid any false positives // Temporarily inactivated to avoid any false positives
//CheckUnusedPrivateFunctions(); CheckUnusedPrivateFunctions();
// Check that the memsets are valid. // Check that the memsets are valid.
// This function can do dangerous things if used wrong. // This function can do dangerous things if used wrong.
@ -735,6 +735,13 @@ void CheckUnusedPrivateFunctions()
{ {
const char *classname = tok1->next->str; const char *classname = tok1->next->str;
// The class implementation must be available..
const char *pattern_classconstructor[] = {"","::","",NULL};
pattern_classconstructor[0] = classname;
pattern_classconstructor[2] = classname;
if (!findtoken(tokens,pattern_classconstructor))
continue;
// Get private functions.. // Get private functions..
std::list<std::string> FuncList; std::list<std::string> FuncList;
FuncList.clear(); FuncList.clear();
@ -742,6 +749,13 @@ void CheckUnusedPrivateFunctions()
unsigned int indent_level = 0; unsigned int indent_level = 0;
for (TOKEN *tok = tok1; tok; tok = tok->next) for (TOKEN *tok = tok1; tok; tok = tok->next)
{ {
if (match(tok,"friend class"))
{
// Todo: Handle friend classes
FuncList.clear();
break;
}
if (tok->str[0] == '{') if (tok->str[0] == '{')
indent_level++; indent_level++;
if (tok->str[0] == '}') if (tok->str[0] == '}')
@ -760,10 +774,14 @@ void CheckUnusedPrivateFunctions()
priv = false; priv = false;
else if (priv && indent_level == 1) else if (priv && indent_level == 1)
{ {
if (isalpha(tok->str[0]) && tok->next->str[0]=='(') if (isalpha(tok->str[0]) &&
tok->next->str[0]=='(' &&
strcmp(tok->str,classname) != 0)
{
FuncList.push_back(tok->str); FuncList.push_back(tok->str);
} }
} }
}
// Check that all private functions are used.. // Check that all private functions are used..
const char *pattern_function[] = {"","::",NULL}; const char *pattern_function[] = {"","::",NULL};
@ -810,10 +828,26 @@ void CheckUnusedPrivateFunctions()
} }
while (HasFuncImpl && !FuncList.empty()) while (HasFuncImpl && !FuncList.empty())
{
bool fp = false;
// Final check; check if the function pointer is used somewhere..
const char *_pattern[] = {"=","",NULL};
_pattern[1] = FuncList.front().c_str();
fp |= (findtoken(tokens, _pattern) != NULL);
_pattern[0] = "(";
fp |= (findtoken(tokens, _pattern) != NULL);
_pattern[0] = ")";
fp |= (findtoken(tokens, _pattern) != NULL);
_pattern[0] = ",";
fp |= (findtoken(tokens, _pattern) != NULL);
if (!fp)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'"; ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'";
ReportErr(ostr.str()); ReportErr(ostr.str());
}
FuncList.pop_front(); FuncList.pop_front();
} }
} }