2008-11-22 19:51:01 +01:00
|
|
|
/*
|
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* 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"
|
2008-11-23 07:21:44 +01:00
|
|
|
#include "tokenize.h"
|
2008-11-22 19:51:01 +01:00
|
|
|
#include <sstream>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// FUNCTION USAGE - Check for unused functions etc
|
|
|
|
//---------------------------------------------------------------------------
|
2008-11-23 07:21:44 +01:00
|
|
|
|
|
|
|
CheckFunctionUsage::CheckFunctionUsage( ErrorLogger *errorLogger )
|
|
|
|
{
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckFunctionUsage::~CheckFunctionUsage()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer )
|
|
|
|
{
|
|
|
|
// Function declarations..
|
|
|
|
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
if ( tok->FileIndex != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const TOKEN *funcname = 0;
|
|
|
|
|
|
|
|
if ( TOKEN::Match( tok, "%type% %var% (" ) )
|
2008-11-23 11:09:16 +01:00
|
|
|
funcname = tok->tokAt(1);
|
2008-11-23 07:21:44 +01:00
|
|
|
else if ( TOKEN::Match(tok, "%type% * %var% (") )
|
2008-11-23 11:09:16 +01:00
|
|
|
funcname = tok->tokAt(2);
|
2008-11-23 12:40:41 +01:00
|
|
|
else if ( TOKEN::Match(tok, "%type% :: %var% (") && !TOKEN::Match(tok, tok->strAt(2)) )
|
|
|
|
funcname = tok->tokAt(2);
|
2008-11-23 07:21:44 +01:00
|
|
|
|
|
|
|
// Check that ") {" is found..
|
|
|
|
for (const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next)
|
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok2, ")") )
|
|
|
|
{
|
2008-11-23 08:33:14 +01:00
|
|
|
if ( ! TOKEN::Match(tok2, ") {") && ! TOKEN::Match(tok2, ") const {") )
|
2008-11-23 07:21:44 +01:00
|
|
|
funcname = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-23 08:33:14 +01:00
|
|
|
if ( funcname )
|
2008-11-23 07:21:44 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
FunctionUsage &func = _functions[ funcname->str ];
|
2008-11-23 07:21:44 +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..
|
|
|
|
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
const TOKEN *funcname = 0;
|
|
|
|
|
2008-11-23 18:28:26 +01:00
|
|
|
if ( TOKEN::Match( tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]" ) ||
|
2008-11-23 12:40:41 +01:00
|
|
|
TOKEN::Match(tok, ":: %var% (") ||
|
|
|
|
TOKEN::Match(tok, "|= %var% (") ||
|
|
|
|
TOKEN::Match(tok, "&= %var% (") ||
|
2008-11-23 15:17:03 +01:00
|
|
|
TOKEN::Match(tok, "&& %var% (") ||
|
|
|
|
TOKEN::Match(tok, "|| %var% (") ||
|
2008-11-23 15:26:47 +01:00
|
|
|
TOKEN::Match(tok, "else %var% (") ||
|
2008-11-23 15:17:03 +01:00
|
|
|
TOKEN::Match(tok, "return %var% (") )
|
2008-11-23 07:21:44 +01:00
|
|
|
funcname = tok->next;
|
2008-11-23 12:40:41 +01:00
|
|
|
|
|
|
|
// funcname ( => Assert that the end paranthesis isn't followed by {
|
|
|
|
if ( TOKEN::Match(funcname, "%var% (") )
|
|
|
|
{
|
|
|
|
int parlevel = 0;
|
|
|
|
for ( const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if (TOKEN::Match(tok2, "("))
|
|
|
|
++parlevel;
|
|
|
|
|
|
|
|
else if (TOKEN::Match(tok2, ")"))
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel == 0 && (TOKEN::Match(tok2, ") {") || TOKEN::Match(tok2, ") const")))
|
|
|
|
funcname = NULL;
|
|
|
|
if ( parlevel <= 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-23 07:21:44 +01:00
|
|
|
|
|
|
|
if ( funcname )
|
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
FunctionUsage &func = _functions[ funcname->str ];
|
2008-11-23 07:21:44 +01:00
|
|
|
|
|
|
|
if ( func.filename.empty() || func.filename == "+" )
|
|
|
|
func.usedOtherFile = true;
|
|
|
|
|
|
|
|
else
|
|
|
|
func.usedSameFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckFunctionUsage::check()
|
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
for ( std::map<std::string, FunctionUsage>::const_iterator it = _functions.begin(); it != _functions.end(); ++it )
|
2008-11-23 07:21:44 +01:00
|
|
|
{
|
|
|
|
const FunctionUsage &func = it->second;
|
|
|
|
if ( func.usedOtherFile || func.filename.empty() )
|
|
|
|
continue;
|
|
|
|
if ( ! func.usedSameFile )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << "The function '" << it->first << "' is never used.";
|
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
else if ( ! func.usedOtherFile )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
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() );
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|