2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2008-12-19 22:15:06 +01:00
|
|
|
* cppcheck - c/c++ syntax checking
|
2009-01-02 20:02:35 +01:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam
|
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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "checkfunctionusage.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include <sstream>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// FUNCTION USAGE - Check for unused functions etc
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
CheckFunctionUsage::CheckFunctionUsage(ErrorLogger *errorLogger)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckFunctionUsage::~CheckFunctionUsage()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void CheckFunctionUsage::setErrorLogger(ErrorLogger *errorLogger)
|
2008-12-19 20:31:12 +01:00
|
|
|
{
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void CheckFunctionUsage::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;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok2, ")"))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! Token::Match(tok2, ") {") && ! Token::Match(tok2, ") const {"))
|
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-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]") ||
|
|
|
|
Token::Match(tok, ":: %var% (") ||
|
|
|
|
Token::Match(tok, "|= %var% (") ||
|
|
|
|
Token::Match(tok, "&= %var% (") ||
|
|
|
|
Token::Match(tok, "&& %var% (") ||
|
|
|
|
Token::Match(tok, "|| %var% (") ||
|
|
|
|
Token::Match(tok, "else %var% (") ||
|
|
|
|
Token::Match(tok, "return %var% ("))
|
2008-12-18 22:28:57 +01:00
|
|
|
funcname = tok->next();
|
|
|
|
|
|
|
|
// 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-01-03 21:29:20 +01:00
|
|
|
if (parlevel == 0 && (Token::Match(tok2, ") {") || 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckFunctionUsage::check()
|
|
|
|
{
|
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-01-05 16:49:57 +01:00
|
|
|
if (! func.usedSameFile)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (func.filename != "+")
|
2008-12-18 22:28:57 +01:00
|
|
|
errmsg << "[" << func.filename << "] ";
|
|
|
|
errmsg << "The function '" << it->first << "' is never used.";
|
2009-01-05 16:49:57 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
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
|
|
|
{
|
|
|
|
/* TODO - add error message "function is only used in <file> it can be static"
|
|
|
|
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() );
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|