diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index a1d48b33b..b7917d445 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -790,8 +790,6 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha c++; } } - - cppcheck.checkFunctionUsage(); cppcheck.analyseWholeProgram(); } else if (!ThreadExecutor::isEnabled()) { std::cout << "No thread support yet implemented for this platform." << std::endl; diff --git a/lib/check.h b/lib/check.h index 97ddbe9d3..91786bd1e 100644 --- a/lib/check.h +++ b/lib/check.h @@ -91,8 +91,9 @@ public: virtual ~FileInfo() {} }; - virtual FileInfo * getFileInfo(const Tokenizer *tokenizer) const { + virtual FileInfo * getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const { (void)tokenizer; + (void)settings; return nullptr; } diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index e94a1c734..baf09503b 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1805,8 +1805,10 @@ void CheckBufferOverrun::writeOutsideBufferSizeError(const Token *tok, const std " Please check the second and the third parameter of the function '"+strFunctionName+"'."); } -Check::FileInfo* CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer) const +Check::FileInfo* CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const { + (void)settings; + MyFileInfo *fileInfo = new MyFileInfo; // Array usage.. diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index f9ba2c343..cdd79a322 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -220,10 +220,10 @@ public: }; /** @brief Parse current TU and extract file info */ - Check::FileInfo *getFileInfo(const Tokenizer *tokenizer) const; + Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const; /** @brief Analyse all file infos for all TU */ - virtual void analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger); + void analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger); private: diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 9a290da2e..7b8e29ceb 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1013,8 +1013,9 @@ public: /// @} -Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer) const +Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const { + (void)settings; MyFileInfo * mfi = new MyFileInfo; analyseFunctions(tokenizer, mfi->uvarFunctions); // TODO: add suspicious function calls diff --git a/lib/checkuninitvar.h b/lib/checkuninitvar.h index eb8eba297..ab7f735f1 100644 --- a/lib/checkuninitvar.h +++ b/lib/checkuninitvar.h @@ -80,10 +80,10 @@ public: }; /** @brief Parse current TU and extract file info */ - Check::FileInfo *getFileInfo(const Tokenizer *tokenizer) const; + Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const; /** @brief Analyse all file infos for all TU */ - virtual void analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger); + void analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger); void analyseFunctions(const Tokenizer *tokenizer, std::set &f) const; diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index c1dcddb17..54d301a8f 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -263,3 +263,17 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, else reportError(errmsg); } + +Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const +{ + if (settings->isEnabled("unusedFunction") && settings->_jobs == 1) + instance.parseTokens(*tokenizer, tokenizer->list.getFiles().front().c_str(), settings); + return nullptr; + +} + +void CheckUnusedFunctions::analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger) +{ + (void)fileInfo; + instance.check(&errorLogger); +} diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index 9f1c10e80..1752575c5 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -47,6 +47,12 @@ public: void check(ErrorLogger * const errorLogger); + /** @brief Parse current TU and extract file info */ + Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const; + + /** @brief Analyse all file infos for all TU */ + void analyseWholeProgram(const std::list &fileInfo, ErrorLogger &errorLogger); + static CheckUnusedFunctions instance; private: diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index ef36338a4..37eceecb8 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -19,7 +19,6 @@ #include "preprocessor.h" // Preprocessor #include "tokenize.h" // Tokenizer -#include "checkunusedfunctions.h" #include "check.h" #include "path.h" @@ -278,23 +277,6 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg } } - -void CppCheck::checkFunctionUsage() -{ - // This generates false positives - especially for libraries - if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) { - const bool verbose_orig = _settings._verbose; - _settings._verbose = false; - - if (_settings._errorsOnly == false) - _errorLogger.reportOut("Checking usage of global functions.."); - - CheckUnusedFunctions::instance.check(this); - - _settings._verbose = verbose_orig; - } -} - void CppCheck::analyseFile(std::istream &fin, const std::string &filename) { // Preprocess file.. @@ -379,8 +361,12 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se (*it)->runChecks(&_tokenizer, &_settings, this); } - if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) - CheckUnusedFunctions::instance.parseTokens(_tokenizer, FileName, &_settings); + // Analyse the tokens.. + for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { + Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer, &_settings); + if (fi != nullptr) + fileInfo.push_back(fi); + } executeRules("normal", _tokenizer); @@ -402,13 +388,6 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se (*it)->runSimplifiedChecks(&_tokenizer, &_settings, this); } - // Analyse the tokens.. - for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { - Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer); - if (fi != nullptr) - fileInfo.push_back(fi); - } - if (_settings.terminated()) return true; diff --git a/lib/cppcheck.h b/lib/cppcheck.h index ee3f95c42..bfecb2c12 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -82,12 +82,6 @@ public: */ unsigned int check(const std::string &path, const std::string &content); - /** - * @brief Check function usage. - * @note Call this after all files has been checked - */ - void checkFunctionUsage(); - /** * @brief Get reference to current settings. * @return a reference to current settings