2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2009-06-07 22:12:20 +02:00
|
|
|
#include "checkunusedfunctions.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include "tokenize.h"
|
2009-07-13 19:11:31 +02:00
|
|
|
#include "token.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// FUNCTION USAGE - Check for unused functions etc
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-06-07 22:12:20 +02:00
|
|
|
CheckUnusedFunctions::CheckUnusedFunctions(ErrorLogger *errorLogger)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:12:20 +02:00
|
|
|
CheckUnusedFunctions::~CheckUnusedFunctions()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:12:20 +02:00
|
|
|
void CheckUnusedFunctions::setErrorLogger(ErrorLogger *errorLogger)
|
2008-12-19 20:31:12 +01:00
|
|
|
{
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-07 22:12:20 +02:00
|
|
|
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Function declarations..
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->fileIndex() != 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2010-02-03 18:37:48 +01:00
|
|
|
// token contains a ':' => skip to next ; or {
|
2009-02-27 17:36:37 +01:00
|
|
|
if (tok->str().find(":") != std::string::npos)
|
2010-02-03 18:37:48 +01:00
|
|
|
{
|
|
|
|
while (tok && tok->str().find_first_of(";{"))
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-27 17:36:37 +01:00
|
|
|
|
2009-06-14 07:58:36 +02:00
|
|
|
// If this is a template function, skip it
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok->previous() && tok->previous()->str() == ">")
|
2009-06-14 07:58:36 +02:00
|
|
|
continue;
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *funcname = 0;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "%type% %var% ("))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = tok->tokAt(1);
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "%type% * %var% ("))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = tok->tokAt(2);
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2)))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = tok->tokAt(2);
|
|
|
|
|
2009-12-08 22:41:47 +01:00
|
|
|
// Don't assume throw as a function name: void foo() throw () {}
|
|
|
|
if (Token::Match(tok->previous(), ")|const"))
|
|
|
|
funcname = 0;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// Check that ") {" is found..
|
2009-01-03 21:29:20 +01:00
|
|
|
for (const Token *tok2 = funcname; tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok2->str() == ")")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-12-08 22:41:47 +01:00
|
|
|
if (! Token::simpleMatch(tok2, ") {") &&
|
|
|
|
! Token::simpleMatch(tok2, ") const {") &&
|
|
|
|
! Token::simpleMatch(tok2, ") const throw ( ) {") &&
|
|
|
|
! Token::simpleMatch(tok2, ") throw ( ) {"))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (funcname)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
FunctionUsage &func = _functions[ funcname->str()];
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
// No filename set yet..
|
|
|
|
if (func.filename.empty())
|
|
|
|
func.filename = tokenizer.getFiles()->at(0);
|
|
|
|
|
|
|
|
// Multiple files => filename = "+"
|
|
|
|
else if (func.filename != tokenizer.getFiles()->at(0))
|
|
|
|
{
|
|
|
|
func.filename = "+";
|
|
|
|
func.usedOtherFile |= func.usedSameFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function usage..
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *funcname = 0;
|
|
|
|
|
2009-02-27 17:36:37 +01:00
|
|
|
if (Token::Match(tok->next(), "%var% ("))
|
|
|
|
{
|
|
|
|
funcname = tok->next();
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:02:26 +02:00
|
|
|
else if (Token::Match(tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]"))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = tok->next();
|
|
|
|
|
2009-10-30 15:00:13 +01:00
|
|
|
else if (Token::Match(tok, "[=(,] & %var% :: %var% [,);]"))
|
2009-05-31 08:01:16 +02:00
|
|
|
funcname = tok->tokAt(4);
|
|
|
|
|
2009-06-05 15:02:26 +02:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// funcname ( => Assert that the end paranthesis isn't followed by {
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(funcname, "%var% ("))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
int parlevel = 0;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (const Token *tok2 = funcname; tok2; tok2 = tok2->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
--parlevel;
|
2009-05-22 17:03:42 +02:00
|
|
|
if (parlevel == 0 && (Token::Match(tok2, ") const|{")))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = NULL;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (parlevel <= 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (funcname)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
FunctionUsage &func = _functions[ funcname->str()];
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (func.filename.empty() || func.filename == "+")
|
2008-12-18 22:28:57 +01:00
|
|
|
func.usedOtherFile = true;
|
|
|
|
|
|
|
|
else
|
|
|
|
func.usedSameFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-07 22:12:20 +02:00
|
|
|
void CheckUnusedFunctions::check()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01: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;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (func.usedOtherFile || func.filename.empty())
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
2009-10-24 14:55:56 +02:00
|
|
|
if (it->first == "main" || it->first == "WinMain" || it->first == "if")
|
2009-01-16 17:20:35 +01:00
|
|
|
continue;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! func.usedSameFile)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-11 07:44:32 +01:00
|
|
|
std::string filename;
|
2009-01-11 09:10:51 +01:00
|
|
|
if (func.filename == "+")
|
2009-01-11 07:44:32 +01:00
|
|
|
filename = "";
|
|
|
|
else
|
|
|
|
filename = func.filename;
|
2009-02-08 22:20:35 +01:00
|
|
|
_errorLogger->unusedFunction(filename, it->first);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (! func.usedOtherFile)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @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() );
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|