From 1245f1d6211188833581e0600bf4c2029094ce45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 7 Nov 2016 21:49:58 +0100 Subject: [PATCH] Unused Functions: Fix checking when --cppcheck-build-dir is used. --- lib/checkunusedfunctions.cpp | 17 ++++++++++++----- lib/checkunusedfunctions.h | 4 ++-- lib/cppcheck.cpp | 14 ++++++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 3baafb900..fa18415ef 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -39,13 +39,14 @@ static const struct CWE CWE561(561U); // Dead Code // FUNCTION USAGE - Check for unused functions etc //--------------------------------------------------------------------------- -void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings) +void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings, bool clear) { const bool doMarkup = settings->library.markupFile(FileName); const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase(); // Function declarations.. - _functionDecl.clear(); + if (clear) + _functionDecl.clear(); for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) { const Scope* scope = symbolDatabase->functionScopes[i]; const Function* func = scope->function; @@ -79,7 +80,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } // Function usage.. - _functionCalls.clear(); + if (clear) + _functionCalls.clear(); for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { // parsing of library code to find called functions @@ -293,12 +295,12 @@ CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f) std::string CheckUnusedFunctions::analyzerInfo() const { std::ostringstream ret; - for (std::list::const_iterator it = instance._functionDecl.begin(); it != instance._functionDecl.end(); ++it) { + for (std::list::const_iterator it = _functionDecl.begin(); it != _functionDecl.end(); ++it) { ret << " functionName) << '\"' << " lineNumber=\"" << it->lineNumber << "\"/>\n"; } - for (std::set::const_iterator it = instance._functionCalls.begin(); it != instance._functionCalls.end(); ++it) { + for (std::set::const_iterator it = _functionCalls.begin(); it != _functionCalls.end(); ++it) { ret << " \n"; } return ret.str(); @@ -351,6 +353,11 @@ void CheckUnusedFunctions::analyseWholeProgram(ErrorLogger * const errorLogger, for (std::map::const_iterator decl = decls.begin(); decl != decls.end(); ++decl) { const std::string &functionName = decl->first; + + if (functionName == "main" || functionName == "WinMain" || functionName == "_tmain" || + functionName == "if" || functionName.compare(0, 8, "operator") == 0) + continue; + if (calls.find(functionName) == calls.end()) { const Location &loc = decl->second; unusedFunctionError(errorLogger, loc.fileName, loc.lineNumber, functionName); diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index d59cc6efe..55c567878 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -47,7 +47,7 @@ public: // Parse current tokens and determine.. // * Check what functions are used // * What functions are declared - void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings); + void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings, bool clear=true); void check(ErrorLogger * const errorLogger, const Settings& settings); @@ -62,7 +62,7 @@ public: std::string analyzerInfo() const; /** @brief Combine and analyze all analyzerInfos for all TUs */ - void analyseWholeProgram(ErrorLogger * const errorLogger, const std::string &buildDir, const std::map &files); + static void analyseWholeProgram(ErrorLogger * const errorLogger, const std::string &buildDir, const std::map &files); private: diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 75a9cf9a8..fc7735a43 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -122,6 +122,8 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin } } + CheckUnusedFunctions checkUnusedFunctions; + bool internalErrorFound(false); try { Preprocessor preprocessor(_settings, this); @@ -316,6 +318,10 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin // Check normal tokens checkNormalTokens(_tokenizer); + // Analyze info.. + if (!_settings.buildDir.empty()) + checkUnusedFunctions.parseTokens(_tokenizer, filename.c_str(), &_settings, false); + // simplify more if required, skip rest of iteration if failed if (_simplify) { // if further simplification fails then skip rest of iteration @@ -368,7 +374,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin exitcode=1; // e.g. reflect a syntax error } - analyzerInformation.setFileInfo("CheckUnusedFunctions", CheckUnusedFunctions::instance.analyzerInfo()); + analyzerInformation.setFileInfo("CheckUnusedFunctions", checkUnusedFunctions.analyzerInfo()); analyzerInformation.close(); // In jointSuppressionReport mode, unmatched suppressions are @@ -733,11 +739,11 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::map