diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index d00ce218f..6f9c3e6f3 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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::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 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); } diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index a8a39102e..54cfc5e5d 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -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: diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 38d52e088..192461508 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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(); diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 59602c92c..47f9f6266 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -162,17 +162,6 @@ public: return true; } - void unusedFunction(const std::string &filename, const std::string &funcname) - { - std::list 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; diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 8db48f17f..0c709d6c4 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -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()