cppcheck/lib/checkunusedfunctions.cpp

463 lines
18 KiB
C++
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2023-01-28 10:16:34 +01:00
* Copyright (C) 2007-2023 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"
2017-05-27 04:33:47 +02:00
#include "astutils.h"
2017-05-27 04:33:47 +02:00
#include "errorlogger.h"
#include "errortypes.h"
2017-05-27 04:33:47 +02:00
#include "library.h"
#include "settings.h"
#include "symboldatabase.h"
2017-05-27 04:33:47 +02:00
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include <algorithm>
#include <cctype>
2017-05-27 04:33:47 +02:00
#include <cstdlib>
#include <cstring>
#include <fstream> // IWYU pragma: keep
#include <map>
#include <sstream> // IWYU pragma: keep
#include <tuple>
2017-05-27 04:33:47 +02:00
#include <utility>
#include <vector>
#include <tinyxml2.h>
namespace CTU {
class FileInfo;
}
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
// Register this check class
CheckUnusedFunctions CheckUnusedFunctions::instance;
2016-07-16 21:21:24 +02:00
static const struct CWE CWE561(561U); // Dead Code
static std::string stripTemplateParameters(const std::string& funcName) {
std::string name = funcName;
const auto pos = name.find('<');
if (pos > 0 && pos != std::string::npos)
name.erase(pos - 1);
return name;
}
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------
2018-02-10 15:34:49 +01:00
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings)
2008-12-18 22:28:57 +01:00
{
const bool doMarkup = settings->library.markupFile(FileName);
const SymbolDatabase* symbolDatabase = tokenizer.getSymbolDatabase();
// Function declarations..
2019-04-27 17:04:14 +02:00
for (const Scope* scope : symbolDatabase->functionScopes) {
const Function* func = scope->function;
if (!func || !func->token || scope->bodyStart->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 || func->isOperator())
continue;
if (func->isExtern())
continue;
2018-06-17 18:58:27 +02:00
mFunctionDecl.emplace_back(func);
FunctionUsage &usage = mFunctions[stripTemplateParameters(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..
const Token *lambdaEndToken = nullptr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
2013-10-20 14:09:10 +02:00
if (tok == lambdaEndToken)
lambdaEndToken = nullptr;
else if (!lambdaEndToken && tok->str() == "[")
lambdaEndToken = findLambdaEndToken(tok);
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));
// not found
if (!markupVarToken)
continue;
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) && markupVarToken) {
2014-01-02 18:18:24 +01:00
if (markupVarToken->str() == settings->library.blockstart(FileName)) {
2013-10-20 14:09:10 +02:00
scope++;
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())) {
2018-06-17 18:58:27 +02:00
mFunctionCalls.insert(markupVarToken->str());
if (mFunctions.find(markupVarToken->str()) != mFunctions.end())
mFunctions[markupVarToken->str()].usedOtherFile = true;
2014-01-02 21:47:35 +01:00
else if (markupVarToken->next()->str() == "(") {
2018-06-17 18:58:27 +02:00
FunctionUsage &func = mFunctions[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 (!doMarkup // only check source files
&& settings->library.isexporter(tok->str()) && tok->next() != nullptr) {
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();
2018-06-17 18:58:27 +02:00
if (mFunctions.find(value) != mFunctions.end()) {
mFunctions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
}
2018-06-17 18:58:27 +02:00
mFunctionCalls.insert(value);
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();
2018-06-17 18:58:27 +02:00
if (value != ")" && mFunctions.find(value) != mFunctions.end()) {
mFunctions[value].usedOtherFile = true;
2013-10-20 14:09:10 +02:00
}
2018-06-17 18:58:27 +02:00
mFunctionCalls.insert(value);
2013-10-20 14:09:10 +02:00
}
propToken = propToken->next();
2013-10-20 14:09:10 +02:00
}
}
if (doMarkup && 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()) {
2018-06-17 18:58:27 +02:00
mFunctions[value].usedOtherFile = true;
mFunctionCalls.insert(value);
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.clear();
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);
2018-06-17 18:58:27 +02:00
mFunctions[value].usedOtherFile = true;
mFunctionCalls.insert(std::move(value));
2013-10-20 14:09:10 +02:00
}
}
}
const Token *funcname = nullptr;
if ((lambdaEndToken || tok->scope()->isExecutable()) && Token::Match(tok, "%name% (")) {
funcname = tok;
} else if ((lambdaEndToken || tok->scope()->isExecutable()) && Token::Match(tok, "%name% <") && Token::simpleMatch(tok->linkAt(1), "> (")) {
funcname = tok;
} else if (Token::Match(tok, "< %name%") && tok->link()) {
funcname = tok->next();
while (Token::Match(funcname, "%name% :: %name%"))
funcname = funcname->tokAt(2);
} else if (tok->scope()->type != Scope::ScopeType::eEnum && Token::Match(tok, "[;{}.,()[=+-/|!?:]")) {
funcname = tok->next();
if (funcname && funcname->str() == "&")
funcname = funcname->next();
if (funcname && funcname->str() == "::")
funcname = funcname->next();
while (Token::Match(funcname, "%name% :: %name%"))
funcname = funcname->tokAt(2);
if (!Token::Match(funcname, "%name% [(),;]:}>]") || funcname->varId())
continue;
}
if (!funcname || funcname->isKeyword() || funcname->isStandardType())
2009-06-05 15:02:26 +02:00
continue;
// funcname ( => Assert that the end parentheses isn't followed by {
if (Token::Match(funcname, "%name% (|<")) {
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) {
const auto baseName = stripTemplateParameters(funcname->str());
FunctionUsage &func = mFunctions[baseName];
2015-01-28 22:29:07 +01:00
const std::string& called_from_file = tokenizer.list.getSourceFilePath();
2008-12-18 22:28:57 +01:00
if (func.filename.empty() || func.filename == "+" || func.filename != called_from_file)
2008-12-18 22:28:57 +01:00
func.usedOtherFile = true;
else
func.usedSameFile = true;
mFunctionCalls.insert(baseName);
2008-12-18 22:28:57 +01:00
}
}
}
2008-12-18 22:28:57 +01:00
2018-03-12 10:19:55 +01:00
static bool isOperatorFunction(const std::string & funcName)
{
/* Operator functions are invalid function names for C, so no need to check
* this in here. As result the returned error function might be incorrect.
*
* List of valid operators can be found at:
* http://en.cppreference.com/w/cpp/language/operators
*
* Conversion functions must be a member function (at least for gcc), so no
* need to cover them for unused functions.
*
2018-05-14 13:11:45 +02:00
* To speed up the comparison, not the whole list of operators is used.
* Instead only the character after the operator prefix is checked to be a
* none alpa numeric value, but the '_', to cover function names like
* "operator_unused". In addition the following valid operators are checked:
* - new
* - new[]
* - delete
* - delete[]
*/
const std::string operatorPrefix = "operator";
if (funcName.compare(0, operatorPrefix.length(), operatorPrefix) != 0) {
return false;
}
// Taking care of funcName == "operator", which is no valid operator
if (funcName.length() == operatorPrefix.length()) {
return false;
}
const char firstOperatorChar = funcName[operatorPrefix.length()];
if (firstOperatorChar == '_') {
return false;
}
if (!std::isalnum(firstOperatorChar)) {
return true;
}
const std::vector<std::string> additionalOperators = {
"new", "new[]", "delete", "delete[]"
};
return std::find(additionalOperators.cbegin(), additionalOperators.cend(), funcName.substr(operatorPrefix.length())) != additionalOperators.cend();
}
bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings& settings) const
{
using ErrorParams = std::tuple<std::string, unsigned int, std::string>;
std::vector<ErrorParams> errors; // ensure well-defined order
for (std::unordered_map<std::string, FunctionUsage>::const_iterator it = mFunctions.cbegin(); it != mFunctions.cend(); ++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 (settings.library.isentrypoint(it->first))
continue;
if (!func.usedSameFile) {
if (isOperatorFunction(it->first))
continue;
2009-01-11 07:44:32 +01:00
std::string filename;
if (func.filename != "+")
2018-03-12 10:19:55 +01:00
filename = func.filename;
errors.emplace_back(filename, func.lineNumber, it->first);
2021-08-07 20:51:18 +02:00
} else if (!func.usedOtherFile) {
/** @todo add error message "function is only used in <file> it can be static" */
/*
2021-08-07 20:51:18 +02: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.";
mErrorLogger->reportErr( errmsg.str() );
errors = true;
*/
2008-12-18 22:28:57 +01:00
}
}
std::sort(errors.begin(), errors.end());
for (const auto& e : errors)
unusedFunctionError(errorLogger, std::get<0>(e), std::get<1>(e), std::get<2>(e));
return !errors.empty();
2008-12-18 22:28:57 +01:00
}
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
2021-08-07 20:51:18 +02:00
const std::string &filename, unsigned int lineNumber,
const std::string &funcname)
{
2020-05-23 07:16:49 +02:00
std::list<ErrorMessage::FileLocation> locationList;
2011-10-13 20:53:06 +02:00
if (!filename.empty()) {
locationList.emplace_back(filename, lineNumber);
2010-07-08 13:01:53 +02:00
}
const ErrorMessage errmsg(locationList, emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal);
2010-07-08 13:01:53 +02:00
if (errorLogger)
errorLogger->reportErr(errmsg);
else
reportError(errmsg);
}
Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
{
if (!settings->checks.isEnabled(Checks::unusedFunction))
return nullptr;
if (settings->useSingleJob() && settings->buildDir.empty())
instance.parseTokens(*tokenizer, tokenizer->list.getFiles().front().c_str(), settings);
return nullptr;
}
2018-12-25 21:11:23 +01:00
bool CheckUnusedFunctions::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger)
{
2018-12-25 21:11:23 +01:00
(void)ctu;
(void)fileInfo;
return check(&errorLogger, settings);
}
2016-10-29 12:18:11 +02:00
CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f)
: functionName(f->name()), lineNumber(f->token->linenr())
2021-08-07 20:51:18 +02:00
{}
std::string CheckUnusedFunctions::analyzerInfo() const
2016-10-29 12:18:11 +02:00
{
std::ostringstream ret;
2019-04-27 17:04:14 +02:00
for (const FunctionDecl &functionDecl : mFunctionDecl) {
ret << " <functiondecl"
2019-04-27 17:04:14 +02:00
<< " functionName=\"" << ErrorLogger::toxml(functionDecl.functionName) << '\"'
<< " lineNumber=\"" << functionDecl.lineNumber << "\"/>\n";
}
2019-04-27 17:04:14 +02:00
for (const std::string &fc : mFunctionCalls) {
ret << " <functioncall functionName=\"" << ErrorLogger::toxml(fc) << "\"/>\n";
2016-10-29 12:18:11 +02:00
}
return ret.str();
}
namespace {
struct Location {
Location() : lineNumber(0) {}
Location(std::string f, const int l) : fileName(std::move(f)), lineNumber(l) {}
std::string fileName;
int lineNumber;
};
}
void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir)
{
std::map<std::string, Location> decls;
std::set<std::string> calls;
const std::string filesTxt(buildDir + "/files.txt");
std::ifstream fin(filesTxt.c_str());
std::string filesTxtLine;
while (std::getline(fin, filesTxtLine)) {
const std::string::size_type firstColon = filesTxtLine.find(':');
if (firstColon == std::string::npos)
continue;
const std::string::size_type secondColon = filesTxtLine.find(':', firstColon+1);
if (secondColon == std::string::npos)
continue;
const std::string xmlfile = buildDir + '/' + filesTxtLine.substr(0,firstColon);
const std::string sourcefile = filesTxtLine.substr(secondColon+1);
tinyxml2::XMLDocument doc;
2018-04-04 21:51:31 +02:00
const tinyxml2::XMLError error = doc.LoadFile(xmlfile.c_str());
if (error != tinyxml2::XML_SUCCESS)
continue;
const tinyxml2::XMLElement * const rootNode = doc.FirstChildElement();
if (rootNode == nullptr)
continue;
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
2017-08-24 17:23:44 +02:00
if (std::strcmp(e->Name(), "FileInfo") != 0)
continue;
const char *checkattr = e->Attribute("check");
if (checkattr == nullptr || std::strcmp(checkattr,"CheckUnusedFunctions") != 0)
continue;
for (const tinyxml2::XMLElement *e2 = e->FirstChildElement(); e2; e2 = e2->NextSiblingElement()) {
const char* functionName = e2->Attribute("functionName");
if (functionName == nullptr)
continue;
if (std::strcmp(e2->Name(),"functioncall") == 0) {
calls.insert(functionName);
continue;
} else if (std::strcmp(e2->Name(),"functiondecl") == 0) {
const char* lineNumber = e2->Attribute("lineNumber");
if (lineNumber)
decls[functionName] = Location(sourcefile, strToInt<int>(lineNumber));
}
}
}
}
for (std::map<std::string, Location>::const_iterator decl = decls.cbegin(); decl != decls.cend(); ++decl) {
const std::string &functionName = decl->first;
if (settings.library.isentrypoint(functionName))
continue;
if (calls.find(functionName) == calls.end() && !isOperatorFunction(functionName)) {
const Location &loc = decl->second;
unusedFunctionError(errorLogger, loc.fileName, loc.lineNumber, functionName);
}
}
}