Unused Functions: Fix checking when --cppcheck-build-dir is used.

This commit is contained in:
Daniel Marjamäki 2016-11-07 21:49:58 +01:00
parent d361ca7b61
commit 1245f1d621
3 changed files with 24 additions and 11 deletions

View File

@ -39,13 +39,14 @@ static const struct CWE CWE561(561U); // Dead Code
// FUNCTION USAGE - Check for unused functions etc // 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 bool doMarkup = settings->library.markupFile(FileName);
const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase(); const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase();
// Function declarations.. // Function declarations..
_functionDecl.clear(); if (clear)
_functionDecl.clear();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) { for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) {
const Scope* scope = symbolDatabase->functionScopes[i]; const Scope* scope = symbolDatabase->functionScopes[i];
const Function* func = scope->function; const Function* func = scope->function;
@ -79,7 +80,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
} }
// Function usage.. // Function usage..
_functionCalls.clear(); if (clear)
_functionCalls.clear();
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
// parsing of library code to find called functions // parsing of library code to find called functions
@ -293,12 +295,12 @@ CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f)
std::string CheckUnusedFunctions::analyzerInfo() const std::string CheckUnusedFunctions::analyzerInfo() const
{ {
std::ostringstream ret; std::ostringstream ret;
for (std::list<FunctionDecl>::const_iterator it = instance._functionDecl.begin(); it != instance._functionDecl.end(); ++it) { for (std::list<FunctionDecl>::const_iterator it = _functionDecl.begin(); it != _functionDecl.end(); ++it) {
ret << " <functiondecl" ret << " <functiondecl"
<< " functionName=\"" << ErrorLogger::toxml(it->functionName) << '\"' << " functionName=\"" << ErrorLogger::toxml(it->functionName) << '\"'
<< " lineNumber=\"" << it->lineNumber << "\"/>\n"; << " lineNumber=\"" << it->lineNumber << "\"/>\n";
} }
for (std::set<std::string>::const_iterator it = instance._functionCalls.begin(); it != instance._functionCalls.end(); ++it) { for (std::set<std::string>::const_iterator it = _functionCalls.begin(); it != _functionCalls.end(); ++it) {
ret << " <functioncall functionName=\"" << ErrorLogger::toxml(*it) << "\"/>\n"; ret << " <functioncall functionName=\"" << ErrorLogger::toxml(*it) << "\"/>\n";
} }
return ret.str(); return ret.str();
@ -351,6 +353,11 @@ void CheckUnusedFunctions::analyseWholeProgram(ErrorLogger * const errorLogger,
for (std::map<std::string, Location>::const_iterator decl = decls.begin(); decl != decls.end(); ++decl) { for (std::map<std::string, Location>::const_iterator decl = decls.begin(); decl != decls.end(); ++decl) {
const std::string &functionName = decl->first; 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()) { if (calls.find(functionName) == calls.end()) {
const Location &loc = decl->second; const Location &loc = decl->second;
unusedFunctionError(errorLogger, loc.fileName, loc.lineNumber, functionName); unusedFunctionError(errorLogger, loc.fileName, loc.lineNumber, functionName);

View File

@ -47,7 +47,7 @@ public:
// Parse current tokens and determine.. // Parse current tokens and determine..
// * Check what functions are used // * Check what functions are used
// * What functions are declared // * 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); void check(ErrorLogger * const errorLogger, const Settings& settings);
@ -62,7 +62,7 @@ public:
std::string analyzerInfo() const; std::string analyzerInfo() const;
/** @brief Combine and analyze all analyzerInfos for all TUs */ /** @brief Combine and analyze all analyzerInfos for all TUs */
void analyseWholeProgram(ErrorLogger * const errorLogger, const std::string &buildDir, const std::map<std::string, std::size_t> &files); static void analyseWholeProgram(ErrorLogger * const errorLogger, const std::string &buildDir, const std::map<std::string, std::size_t> &files);
private: private:

View File

@ -122,6 +122,8 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
} }
} }
CheckUnusedFunctions checkUnusedFunctions;
bool internalErrorFound(false); bool internalErrorFound(false);
try { try {
Preprocessor preprocessor(_settings, this); Preprocessor preprocessor(_settings, this);
@ -316,6 +318,10 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
// Check normal tokens // Check normal tokens
checkNormalTokens(_tokenizer); 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 // simplify more if required, skip rest of iteration if failed
if (_simplify) { if (_simplify) {
// if further simplification fails then skip rest of iteration // 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 exitcode=1; // e.g. reflect a syntax error
} }
analyzerInformation.setFileInfo("CheckUnusedFunctions", CheckUnusedFunctions::instance.analyzerInfo()); analyzerInformation.setFileInfo("CheckUnusedFunctions", checkUnusedFunctions.analyzerInfo());
analyzerInformation.close(); analyzerInformation.close();
// In jointSuppressionReport mode, unmatched suppressions are // In jointSuppressionReport mode, unmatched suppressions are
@ -733,11 +739,11 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::map<s
{ {
if (buildDir.empty()) if (buildDir.empty())
return; return;
// if (_settings.isEnabled("unusedFunction")) if (_settings.isEnabled("unusedFunction"))
// CheckUnusedFunctions::instance.analyseWholeProgram(this, buildDir, files); CheckUnusedFunctions::analyseWholeProgram(this, buildDir, files);
} }
bool CppCheck::isUnusedFunctionCheckEnabled() const bool CppCheck::isUnusedFunctionCheckEnabled() const
{ {
return ((_settings.jobs == 1 || !_settings.buildDir.empty()) && _settings.isEnabled("unusedFunction")); return (_settings.jobs == 1 && _settings.isEnabled("unusedFunction"));
} }