CheckUnusedFunction: Refactorings to use same infrastructure for whole program analysis as CheckUninitVar and CheckBufferOverrun

This commit is contained in:
Daniel Marjamäki 2014-11-15 18:44:23 +01:00
parent 6c5ba733b6
commit 828417c934
11 changed files with 84 additions and 71 deletions

View File

@ -785,7 +785,6 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
} }
} }
cppcheck.checkFunctionUsage();
cppcheck.analyseWholeProgram(); cppcheck.analyseWholeProgram();
} else if (!ThreadExecutor::isEnabled()) { } else if (!ThreadExecutor::isEnabled()) {
std::cout << "No thread support yet implemented for this platform." << std::endl; std::cout << "No thread support yet implemented for this platform." << std::endl;

View File

@ -94,8 +94,9 @@ public:
virtual ~FileInfo() {} virtual ~FileInfo() {}
}; };
virtual FileInfo * getFileInfo(const Tokenizer *tokenizer) const { virtual FileInfo * getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const {
(void)tokenizer; (void)tokenizer;
(void)settings;
return nullptr; return nullptr;
} }

View File

@ -1805,8 +1805,10 @@ void CheckBufferOverrun::writeOutsideBufferSizeError(const Token *tok, const std
" Please check the second and the third parameter of the function '"+strFunctionName+"'."); " 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; MyFileInfo *fileInfo = new MyFileInfo;
// Array usage.. // Array usage..

View File

@ -220,7 +220,7 @@ public:
}; };
/** @brief Parse current TU and extract file info */ /** @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 */ /** @brief Analyse all file infos for all TU */
virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger); virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);

View File

@ -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; MyFileInfo * mfi = new MyFileInfo;
analyseFunctions(tokenizer, mfi->uvarFunctions); analyseFunctions(tokenizer, mfi->uvarFunctions);
// TODO: add suspicious function calls // TODO: add suspicious function calls

View File

@ -80,7 +80,7 @@ public:
}; };
/** @brief Parse current TU and extract file info */ /** @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 */ /** @brief Analyse all file infos for all TU */
virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger); virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);

View File

@ -25,19 +25,25 @@
#include <cctype> #include <cctype>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Register this check class // Register this check class
CheckUnusedFunctions CheckUnusedFunctions::instance; namespace {
CheckUnusedFunctions instance;
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// 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) Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
{ {
const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase(); if (!settings->isEnabled("unusedFunction"))
return nullptr;
const SymbolDatabase* symbolDatabase = tokenizer->getSymbolDatabase();
MyFileInfo *fileInfo = new MyFileInfo;
const std::string FileName = tokenizer->list.getFiles().front();
// Function declarations.. // Function declarations..
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) { for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) {
@ -54,24 +60,24 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
if (func->retDef->str() == "template") if (func->retDef->str() == "template")
continue; continue;
FunctionUsage &usage = _functions[func->name()]; FunctionUsage &usage = fileInfo->_functions[func->name()];
if (!usage.lineNumber) if (!usage.lineNumber)
usage.lineNumber = func->token->linenr(); usage.lineNumber = func->token->linenr();
// No filename set yet.. // No filename set yet..
if (usage.filename.empty()) { if (usage.filename.empty()) {
usage.filename = tokenizer.list.getSourceFilePath(); usage.filename = tokenizer->list.getSourceFilePath();
} }
// Multiple files => filename = "+" // Multiple files => filename = "+"
else if (usage.filename != tokenizer.list.getSourceFilePath()) { else if (usage.filename != tokenizer->list.getSourceFilePath()) {
//func.filename = "+"; //func.filename = "+";
usage.usedOtherFile |= usage.usedSameFile; usage.usedOtherFile |= usage.usedSameFile;
} }
} }
// Function usage.. // Function usage..
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
if (settings->library.isexecutableblock(FileName, tok->str())) { if (settings->library.isexecutableblock(FileName, tok->str())) {
@ -88,11 +94,11 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
} else if (markupVarToken->str() == settings->library.blockend(FileName)) } else if (markupVarToken->str() == settings->library.blockend(FileName))
scope--; scope--;
else if (!settings->library.iskeyword(FileName, markupVarToken->str())) { else if (!settings->library.iskeyword(FileName, markupVarToken->str())) {
if (_functions.find(markupVarToken->str()) != _functions.end()) if (fileInfo->_functions.find(markupVarToken->str()) != fileInfo->_functions.end())
_functions[markupVarToken->str()].usedOtherFile = true; fileInfo->_functions[markupVarToken->str()].usedOtherFile = true;
else if (markupVarToken->next()->str() == "(") { else if (markupVarToken->next()->str() == "(") {
FunctionUsage &func = _functions[markupVarToken->str()]; FunctionUsage &func = fileInfo->_functions[markupVarToken->str()];
func.filename = tokenizer.list.getSourceFilePath(); func.filename = tokenizer->list.getSourceFilePath();
if (func.filename.empty() || func.filename == "+") if (func.filename.empty() || func.filename == "+")
func.usedOtherFile = true; func.usedOtherFile = true;
else else
@ -110,15 +116,15 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
if (settings->library.isexportedprefix(tok->str(), propToken->str())) { if (settings->library.isexportedprefix(tok->str(), propToken->str())) {
const Token* nextPropToken = propToken->next(); const Token* nextPropToken = propToken->next();
const std::string& value = nextPropToken->str(); const std::string& value = nextPropToken->str();
if (_functions.find(value) != _functions.end()) { if (fileInfo->_functions.find(value) != fileInfo->_functions.end()) {
_functions[value].usedOtherFile = true; fileInfo->_functions[value].usedOtherFile = true;
} }
} }
if (settings->library.isexportedsuffix(tok->str(), propToken->str())) { if (settings->library.isexportedsuffix(tok->str(), propToken->str())) {
const Token* prevPropToken = propToken->previous(); const Token* prevPropToken = propToken->previous();
const std::string& value = prevPropToken->str(); const std::string& value = prevPropToken->str();
if (value != ")" && _functions.find(value) != _functions.end()) { if (value != ")" && fileInfo->_functions.find(value) != fileInfo->_functions.end()) {
_functions[value].usedOtherFile = true; fileInfo->_functions[value].usedOtherFile = true;
} }
} }
propToken = propToken->next(); propToken = propToken->next();
@ -133,7 +139,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
while (propToken && propToken->str() != ")") { while (propToken && propToken->str() != ")") {
const std::string& value = propToken->str(); const std::string& value = propToken->str();
if (!value.empty()) { if (!value.empty()) {
_functions[value].usedOtherFile = true; fileInfo->_functions[value].usedOtherFile = true;
break; break;
} }
propToken = propToken->next(); propToken = propToken->next();
@ -158,7 +164,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
} }
if (index == argIndex) { if (index == argIndex) {
value = value.substr(1, value.length() - 2); value = value.substr(1, value.length() - 2);
_functions[value].usedOtherFile = true; fileInfo->_functions[value].usedOtherFile = true;
} }
} }
} }
@ -202,7 +208,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
} }
if (funcname) { if (funcname) {
FunctionUsage &func = _functions[ funcname->str()]; FunctionUsage &func = fileInfo->_functions[ funcname->str()];
if (func.filename.empty() || func.filename == "+") if (func.filename.empty() || func.filename == "+")
func.usedOtherFile = true; func.usedOtherFile = true;
@ -210,13 +216,33 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
func.usedSameFile = true; func.usedSameFile = true;
} }
} }
return fileInfo;
} }
void CheckUnusedFunctions::analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger)
void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
{ {
std::map<std::string, FunctionUsage> _functions;
for (std::list<Check::FileInfo*>::const_iterator it = fileInfo.begin(); it != fileInfo.end(); ++it) {
const MyFileInfo *f = dynamic_cast<MyFileInfo*>(*it);
if (f) {
for (std::map<std::string, FunctionUsage>::const_iterator it2 = f->_functions.begin(); it2 != f->_functions.end(); ++it2) {
std::map<std::string, FunctionUsage>::iterator it3 = _functions.find(it2->first);
if (it3 == _functions.end())
_functions[it2->first] = it2->second;
else {
if (it3->second.filename.empty()) {
it3->second.filename = it2->second.filename;
it3->second.lineNumber = it2->second.lineNumber;
}
it3->second.usedOtherFile |= it2->second.usedOtherFile;
}
}
}
}
for (std::map<std::string, FunctionUsage>::const_iterator it = _functions.begin(); it != _functions.end(); ++it) { for (std::map<std::string, FunctionUsage>::const_iterator it = _functions.begin(); it != _functions.end(); ++it) {
const FunctionUsage &func = it->second; const FunctionUsage &func = it->second;
if (func.usedOtherFile || func.filename.empty()) if (func.usedOtherFile || func.filename.empty())
@ -233,7 +259,7 @@ void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
filename = ""; filename = "";
else else
filename = func.filename; filename = func.filename;
unusedFunctionError(errorLogger, filename, func.lineNumber, it->first); CheckUnusedFunctions::unusedFunctionError(&errorLogger, filename, func.lineNumber, it->first);
} else if (! func.usedOtherFile) { } else if (! func.usedOtherFile) {
/** @todo add error message "function is only used in <file> it can be static" */ /** @todo add error message "function is only used in <file> it can be static" */
/* /*

View File

@ -43,11 +43,12 @@ 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 check(ErrorLogger * const errorLogger); /** @brief Parse current TU and extract file info */
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
static CheckUnusedFunctions instance; /** @brief Analyse all file infos for all TU */
virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
private: private:
@ -66,9 +67,7 @@ private:
/** /**
* Dummy implementation, just to provide error for --errorlist * Dummy implementation, just to provide error for --errorlist
*/ */
void runSimplifiedChecks(const Tokenizer *, const Settings *, ErrorLogger *) { void runSimplifiedChecks(const Tokenizer *, const Settings *, ErrorLogger *) {}
}
static std::string myName() { static std::string myName() {
return "Unused functions"; return "Unused functions";
@ -78,7 +77,7 @@ private:
return "Check for functions that are never called\n"; return "Check for functions that are never called\n";
} }
class CPPCHECKLIB FunctionUsage { class FunctionUsage {
public: public:
FunctionUsage() : lineNumber(0), usedSameFile(false), usedOtherFile(false) { FunctionUsage() : lineNumber(0), usedSameFile(false), usedOtherFile(false) {
} }
@ -89,7 +88,10 @@ private:
bool usedOtherFile; bool usedOtherFile;
}; };
class MyFileInfo : public Check::FileInfo {
public:
std::map<std::string, FunctionUsage> _functions; std::map<std::string, FunctionUsage> _functions;
};
}; };
/// @} /// @}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -19,7 +19,6 @@
#include "preprocessor.h" // Preprocessor #include "preprocessor.h" // Preprocessor
#include "tokenize.h" // Tokenizer #include "tokenize.h" // Tokenizer
#include "checkunusedfunctions.h"
#include "check.h" #include "check.h"
#include "path.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) void CppCheck::analyseFile(std::istream &fin, const std::string &filename)
{ {
// Preprocess file.. // Preprocess file..
@ -379,9 +361,6 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se
(*it)->runChecks(&_tokenizer, &_settings, this); (*it)->runChecks(&_tokenizer, &_settings, this);
} }
if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1)
CheckUnusedFunctions::instance.parseTokens(_tokenizer, FileName, &_settings);
executeRules("normal", _tokenizer); executeRules("normal", _tokenizer);
if (!_simplify) if (!_simplify)
@ -404,7 +383,7 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se
// Analyse the tokens.. // Analyse the tokens..
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer); Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer, &_settings);
if (fi != nullptr) if (fi != nullptr)
fileInfo.push_back(fi); fileInfo.push_back(fi);
} }

View File

@ -82,12 +82,6 @@ public:
*/ */
unsigned int check(const std::string &path, const std::string &content); 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. * @brief Get reference to current settings.
* @return a reference to current settings * @return a reference to current settings

View File

@ -65,7 +65,7 @@ private:
errout.str(""); errout.str("");
Settings settings; Settings settings;
settings.addEnabled("style"); settings.addEnabled("unusedFunction");
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
@ -74,8 +74,10 @@ private:
// Check for unused functions.. // Check for unused functions..
CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this); CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this);
checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings); std::list<Check::FileInfo*> fileInfo;
checkUnusedFunctions.check(this); fileInfo.push_back(checkUnusedFunctions.getFileInfo(&tokenizer, &settings));
checkUnusedFunctions.analyseWholeProgram(fileInfo,*this);
delete fileInfo.back();
} }
void incondition() { void incondition() {
@ -326,6 +328,8 @@ private:
const char code[] = "static void f() { }"; const char code[] = "static void f() { }";
std::list<Check::FileInfo*> fileInfo;
for (int i = 1; i <= 2; ++i) { for (int i = 1; i <= 2; ++i) {
std::ostringstream fname; std::ostringstream fname;
fname << "test" << i << ".cpp"; fname << "test" << i << ".cpp";
@ -334,16 +338,21 @@ private:
errout.str(""); errout.str("");
Settings settings; Settings settings;
settings.addEnabled("unusedFunction");
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, fname.str().c_str()); tokenizer.tokenize(istr, fname.str().c_str());
c.parseTokens(tokenizer, "someFile.c", &settings); fileInfo.push_back(c.getFileInfo(&tokenizer, &settings));
} }
// Check for unused functions.. // Check for unused functions..
c.check(this); c.analyseWholeProgram(fileInfo,*this);
while (!fileInfo.empty()) {
delete fileInfo.back();
fileInfo.pop_back();
}
ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str()); ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str());
} }