Unused functions: Refactoring

This commit is contained in:
Daniel Marjamäki 2010-07-08 13:01:53 +02:00
parent 1876cd482b
commit c34c3ee742
5 changed files with 32 additions and 49 deletions

View File

@ -30,21 +30,6 @@
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------
CheckUnusedFunctions::CheckUnusedFunctions(ErrorLogger *errorLogger)
{
_errorLogger = errorLogger;
}
CheckUnusedFunctions::~CheckUnusedFunctions()
{
}
void CheckUnusedFunctions::setErrorLogger(ErrorLogger *errorLogger)
{
_errorLogger = errorLogger;
}
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
{
// Function declarations..
@ -166,7 +151,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
void CheckUnusedFunctions::check()
void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
{
for (std::map<std::string, FunctionUsage>::const_iterator it = _functions.begin(); it != _functions.end(); ++it)
{
@ -182,7 +167,7 @@ void CheckUnusedFunctions::check()
filename = "";
else
filename = func.filename;
_errorLogger->unusedFunction(filename, it->first);
unusedFunctionError(errorLogger, filename, it->first);
}
else if (! func.usedOtherFile)
{
@ -196,7 +181,20 @@ void CheckUnusedFunctions::check()
}
}
void CheckUnusedFunctions::unusedFunctionError(const Token *tok)
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, const std::string &filename, const std::string &funcname)
{
reportError(tok, Severity::style, "unusedFunction", "The function 'funcName' is never used");
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
if (!filename.empty())
{
ErrorLogger::ErrorMessage::FileLocation fileLoc;
fileLoc.file = filename;
fileLoc.line = 1;
locationList.push_back(fileLoc);
}
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::stringify(Severity::style), "The function '" + funcname + "' is never used", "unusedFunction");
if (errorLogger)
errorLogger->reportErr(errmsg);
else
reportError(errmsg);
}

View File

@ -32,35 +32,33 @@
class CheckUnusedFunctions: public Check
{
public:
CheckUnusedFunctions(ErrorLogger *errorLogger = 0);
~CheckUnusedFunctions();
/** @brief This constructor is used when registering the CheckUnusedFunctions */
CheckUnusedFunctions() : Check()
{ }
/**
* Errors found by this class are forwarded to the given
* errorlogger.
* @param errorLogger The errorlogger to be used.
*/
void setErrorLogger(ErrorLogger *errorLogger);
/** @brief This constructor is used when running checks. */
CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger)
{ }
// Parse current tokens and determine..
// * Check what functions are used
// * What functions are declared
void parseTokens(const Tokenizer &tokenizer);
void check();
void check(ErrorLogger * const errorLogger);
private:
void getErrorMessages()
{
unusedFunctionError(0);
unusedFunctionError(0, "", "funcName");
}
/**
* Dummy implementation, just to provide error for --errorlist
*/
void unusedFunctionError(const Token *tok);
void unusedFunctionError(ErrorLogger * const errorLogger, const std::string &filename, const std::string &funcname);
/**
* Dummy implementation, just to provide error for --errorlist
@ -80,9 +78,6 @@ private:
return "Check for functions that are never called\n";
}
ErrorLogger *_errorLogger;
class FunctionUsage
{
public:

View File

@ -624,7 +624,6 @@ unsigned int CppCheck::check()
{
exitcode = 0;
_checkUnusedFunctions.setErrorLogger(this);
std::sort(_filenames.begin(), _filenames.end());
// TODO: Should this be moved out to its own function so all the files can be
@ -728,7 +727,7 @@ unsigned int CppCheck::check()
if (_settings._errorsOnly == false)
_errorLogger.reportOut("Checking usage of global functions..");
_checkUnusedFunctions.check();
_checkUnusedFunctions.check(this);
}
_errorList.clear();

View File

@ -162,17 +162,6 @@ public:
return true;
}
void unusedFunction(const std::string &filename, const std::string &funcname)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> loc;
ErrorLogger::ErrorMessage::FileLocation fileLoc;
fileLoc.file = filename;
fileLoc.line = 1;
loc.push_back(fileLoc);
reportErr(ErrorLogger::ErrorMessage(loc, "style", "The function '" + funcname + "' is never used", "unusedFunction"));
}
static bool mismatchAllocDealloc()
{
return true;

View File

@ -58,9 +58,11 @@ private:
errout.str("");
// Check for unused functions..
CheckUnusedFunctions checkUnusedFunctions(this);
Settings settings;
settings._checkCodingStyle = true;
CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this);
checkUnusedFunctions.parseTokens(tokenizer);
checkUnusedFunctions.check();
checkUnusedFunctions.check(this);
}
void incondition()