cppcheck/lib/checkunusedfunctions.cpp

266 lines
10 KiB
C++
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2014-02-15 07:45:39 +01:00
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
2008-12-18 22:28:57 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
//---------------------------------------------------------------------------
#include "checkunusedfunctions.h"
2008-12-18 22:28:57 +01:00
#include "tokenize.h"
#include "token.h"
#include "symboldatabase.h"
2011-10-30 09:53:01 +01:00
#include <cctype>
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
// Register this check class
CheckUnusedFunctions CheckUnusedFunctions::instance;
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings)
2008-12-18 22:28:57 +01:00
{
const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase();
// Function declarations..
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); i++) {
const Scope* scope = symbolDatabase->functionScopes[i];
const Function* func = scope->function;
if (!func || !func->token || scope->classStart->fileIndex() != 0)
continue;
// Don't warn about functions that are marked by __attribute__((constructor)) or __attribute__((destructor))
if (func->isAttributeConstructor() || func->isAttributeDestructor() || func->type != Function::eFunction)
continue;
// Don't care about templates
if (func->retDef->str() == "template")
continue;
2008-12-18 22:28:57 +01:00
FunctionUsage &usage = _functions[func->name()];
2008-12-18 22:28:57 +01:00
if (!usage.lineNumber)
usage.lineNumber = func->token->linenr();
// No filename set yet..
if (usage.filename.empty()) {
usage.filename = tokenizer.list.getSourceFilePath();
}
// Multiple files => filename = "+"
else if (usage.filename != tokenizer.list.getSourceFilePath()) {
//func.filename = "+";
usage.usedOtherFile |= usage.usedSameFile;
2008-12-18 22:28:57 +01:00
}
}
// Function usage..
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
2013-10-20 14:09:10 +02:00
// parsing of library code to find called functions
if (settings->library.isexecutableblock(FileName, tok->str())) {
2014-01-02 18:18:24 +01:00
const Token * markupVarToken = tok->tokAt(settings->library.blockstartoffset(FileName));
int scope = 0;
bool start = true;
2013-10-20 14:09:10 +02:00
// find all function calls in library code (starts with '(', not if or while etc)
while (scope || start) {
2014-01-02 18:18:24 +01:00
if (markupVarToken->str() == settings->library.blockstart(FileName)) {
2013-10-20 14:09:10 +02:00
scope++;
if (start) {
start = false;
}
2014-01-02 18:18:24 +01:00
} else if (markupVarToken->str() == settings->library.blockend(FileName))
2013-10-20 14:09:10 +02:00
scope--;
2014-01-02 18:18:24 +01:00
else if (!settings->library.iskeyword(FileName, markupVarToken->str())) {
if (_functions.find(markupVarToken->str()) != _functions.end())
_functions[markupVarToken->str()].usedOtherFile = true;
2014-01-02 21:47:35 +01:00
else if (markupVarToken->next()->str() == "(") {
FunctionUsage &func = _functions[markupVarToken->str()];
func.filename = tokenizer.list.getSourceFilePath();
2014-01-02 18:18:24 +01:00
if (func.filename.empty() || func.filename == "+")
func.usedOtherFile = true;
else
func.usedSameFile = true;
}
2013-10-20 14:09:10 +02:00
}
2014-01-02 18:18:24 +01:00
markupVarToken = markupVarToken->next();
2013-10-20 14:09:10 +02:00
}
}
if (!settings->library.markupFile(FileName) // only check source files
2013-10-20 14:09:10 +02:00
&& settings->library.isexporter(tok->str()) && tok->next() != 0) {
const Token * propToken = tok->next();
while (propToken && propToken->str() != ")") {
if (settings->library.isexportedprefix(tok->str(), propToken->str())) {
const Token* nextPropToken = propToken->next();
const std::string& value = nextPropToken->str();
if (_functions.find(value) != _functions.end()) {
_functions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
}
}
if (settings->library.isexportedsuffix(tok->str(), propToken->str())) {
const Token* prevPropToken = propToken->previous();
const std::string& value = prevPropToken->str();
if (value != ")" && _functions.find(value) != _functions.end()) {
_functions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
}
}
propToken = propToken->next();
2013-10-20 14:09:10 +02:00
}
}
if (settings->library.markupFile(FileName)
2013-10-20 14:09:10 +02:00
&& settings->library.isimporter(FileName, tok->str()) && tok->next()) {
const Token * propToken = tok->next();
if (propToken->next()) {
propToken = propToken->next();
while (propToken && propToken->str() != ")") {
const std::string& value = propToken->str();
2013-10-20 14:09:10 +02:00
if (!value.empty()) {
_functions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
break;
}
propToken = propToken->next();
2013-10-20 14:09:10 +02:00
}
}
}
if (settings->library.isreflection(tok->str())) {
const int argIndex = settings->library.reflectionArgument(tok->str());
if (argIndex >= 0) {
const Token * funcToken = tok->next();
int index = 0;
std::string value;
2014-03-14 15:51:15 +01:00
while (funcToken) {
if (funcToken->str()==",") {
if (++index == argIndex)
break;
value = "";
2014-03-13 17:47:00 +01:00
} else
value += funcToken->str();
funcToken = funcToken->next();
}
if (index == argIndex) {
2013-10-20 14:09:10 +02:00
value = value.substr(1, value.length() - 2);
_functions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
}
}
}
const Token *funcname = nullptr;
if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% (")) {
funcname = tok->next();
}
else if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% <") && Token::simpleMatch(tok->linkAt(2), "> (")) {
funcname = tok->next();
}
2014-08-31 20:33:27 +02:00
else if (Token::Match(tok, "[;{}.,()[=+-/|!?:] &| %var% [(),;]:}]")) {
2008-12-18 22:28:57 +01:00
funcname = tok->next();
2014-08-31 20:33:27 +02:00
if (funcname->str() == "&")
funcname = funcname->next();
}
2008-12-18 22:28:57 +01:00
else if (Token::Match(tok, "[;{}.,()[=+-/|!?:] &| %var% :: %var%")) {
funcname = tok->next();
if (funcname->str() == "&")
funcname = funcname->next();
while (Token::Match(funcname,"%var% :: %var%"))
funcname = funcname->tokAt(2);
if (!Token::Match(funcname, "%var% [(),;:}]"))
continue;
}
2009-06-05 15:02:26 +02:00
else
continue;
// funcname ( => Assert that the end parentheses isn't followed by {
if (Token::Match(funcname, "%var% (|<")) {
const Token *ftok = funcname->next();
if (ftok->str() == "<")
ftok = ftok->link();
if (Token::Match(ftok->linkAt(1), ") const|throw|{"))
2014-02-16 10:32:10 +01:00
funcname = nullptr;
2008-12-18 22:28:57 +01:00
}
2011-10-13 20:53:06 +02:00
if (funcname) {
FunctionUsage &func = _functions[ funcname->str()];
2008-12-18 22:28:57 +01:00
if (func.filename.empty() || func.filename == "+")
2008-12-18 22:28:57 +01:00
func.usedOtherFile = true;
else
func.usedSameFile = true;
}
}
}
2008-12-18 22:28:57 +01:00
void CheckUnusedFunctions::check(ErrorLogger * const errorLogger)
{
2011-10-13 20:53:06 +02:00
for (std::map<std::string, FunctionUsage>::const_iterator it = _functions.begin(); it != _functions.end(); ++it) {
2008-12-18 22:28:57 +01:00
const FunctionUsage &func = it->second;
if (func.usedOtherFile || func.filename.empty())
2008-12-18 22:28:57 +01:00
continue;
if (it->first == "main" ||
it->first == "WinMain" ||
it->first == "_tmain" ||
it->first == "if" ||
(it->first.compare(0, 8, "operator") == 0 && it->first.size() > 8 && !std::isalnum(it->first[8])))
continue;
2011-10-13 20:53:06 +02:00
if (! func.usedSameFile) {
2009-01-11 07:44:32 +01:00
std::string filename;
if (func.filename == "+")
2009-01-11 07:44:32 +01:00
filename = "";
else
filename = func.filename;
unusedFunctionError(errorLogger, filename, func.lineNumber, it->first);
2011-10-13 20:53:06 +02:00
} else if (! func.usedOtherFile) {
/** @todo add error message "function is only used in <file> it can be static" */
/*
2008-12-18 22:28:57 +01:00
std::ostringstream errmsg;
errmsg << "The function '" << it->first << "' is only used in the file it was declared in so it should have local linkage.";
_errorLogger->reportErr( errmsg.str() );
*/
}
}
}
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
2011-09-18 18:58:28 +02:00
const std::string &filename, unsigned int lineNumber,
const std::string &funcname)
{
2010-07-08 13:01:53 +02:00
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2011-10-13 20:53:06 +02:00
if (!filename.empty()) {
2010-07-08 13:01:53 +02:00
ErrorLogger::ErrorMessage::FileLocation fileLoc;
fileLoc.setfile(filename);
fileLoc.line = lineNumber;
2010-07-08 13:01:53 +02:00
locationList.push_back(fileLoc);
}
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", false);
2010-07-08 13:01:53 +02:00
if (errorLogger)
errorLogger->reportErr(errmsg);
else
reportError(errmsg);
}