Unused Functions: Fix checking when --cppcheck-build-dir is used.
This commit is contained in:
parent
d361ca7b61
commit
1245f1d621
|
@ -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<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"
|
||||
<< " functionName=\"" << ErrorLogger::toxml(it->functionName) << '\"'
|
||||
<< " 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";
|
||||
}
|
||||
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) {
|
||||
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);
|
||||
|
|
|
@ -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<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:
|
||||
|
||||
|
|
|
@ -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<s
|
|||
{
|
||||
if (buildDir.empty())
|
||||
return;
|
||||
// if (_settings.isEnabled("unusedFunction"))
|
||||
// CheckUnusedFunctions::instance.analyseWholeProgram(this, buildDir, files);
|
||||
if (_settings.isEnabled("unusedFunction"))
|
||||
CheckUnusedFunctions::analyseWholeProgram(this, buildDir, files);
|
||||
}
|
||||
|
||||
bool CppCheck::isUnusedFunctionCheckEnabled() const
|
||||
{
|
||||
return ((_settings.jobs == 1 || !_settings.buildDir.empty()) && _settings.isEnabled("unusedFunction"));
|
||||
return (_settings.jobs == 1 && _settings.isEnabled("unusedFunction"));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue