Function Usage: Added a new class for checking function usage
This commit is contained in:
parent
d68e2ae966
commit
6687bc7b9a
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* 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"
|
||||
#include "tokenize.h"
|
||||
#include <sstream>
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// FUNCTION USAGE - Check for unused functions etc
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
CheckFunctionUsage::CheckFunctionUsage( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||
{
|
||||
_tokenizer = tokenizer;
|
||||
_errorLogger = errorLogger;
|
||||
functions.clear();
|
||||
}
|
||||
|
||||
CheckFunctionUsage::~CheckFunctionUsage()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void CheckFunctionUsage::parseTokens( const std::string &filename )
|
||||
{
|
||||
// Function declarations..
|
||||
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next )
|
||||
{
|
||||
if ( tok->FileIndex != 0 )
|
||||
continue;
|
||||
|
||||
const TOKEN *funcname = 0;
|
||||
|
||||
if ( Tokenizer::Match( tok, "%type% %var% (" ) )
|
||||
funcname = _tokenizer->gettok(tok, 1);
|
||||
else if ( Tokenizer::Match(tok, "%type% * %var% (") )
|
||||
funcname = _tokenizer->gettok(tok, 2);
|
||||
|
||||
if ( Tokenizer::Match(funcname, "%var% ( )") || Tokenizer::Match(funcname, "%var% ( %type%") )
|
||||
{
|
||||
FunctionUsage &func = functions[ funcname->str ];
|
||||
|
||||
// No filename set yet..
|
||||
if (func.filename.empty())
|
||||
func.filename = filename;
|
||||
|
||||
// Multiple files => filename = "+"
|
||||
else if (func.filename != filename)
|
||||
{
|
||||
func.filename = "+";
|
||||
func.usedOtherFile |= func.usedSameFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function usage..
|
||||
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next )
|
||||
{
|
||||
const TOKEN *funcname = 0;
|
||||
|
||||
if ( Tokenizer::Match( tok, "[;{}.)[=] %var% (" ) )
|
||||
funcname = tok;
|
||||
|
||||
else if ( Tokenizer::Match(tok, "= %var% ;") )
|
||||
funcname = tok->next;
|
||||
|
||||
if ( funcname )
|
||||
{
|
||||
FunctionUsage &func = functions[ funcname->str ];
|
||||
|
||||
if ( func.filename.empty() || func.filename == "+" )
|
||||
func.usedOtherFile = true;
|
||||
|
||||
else
|
||||
func.usedSameFile = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void CheckFunctionUsage::check()
|
||||
{
|
||||
for ( std::map<std::string, FunctionUsage>::const_iterator it = functions.begin(); it != functions.end(); ++it )
|
||||
{
|
||||
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() );
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* 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/
|
||||
*/
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#ifndef CheckFunctionUsageH
|
||||
#define CheckFunctionUsageH
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "tokenize.h"
|
||||
#include "errorlogger.h"
|
||||
|
||||
class CheckFunctionUsage
|
||||
{
|
||||
public:
|
||||
CheckFunctionUsage( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||
~CheckFunctionUsage();
|
||||
|
||||
// Parse current tokens and determine..
|
||||
// * Check what functions are used
|
||||
// * What functions are declared
|
||||
void parseTokens( const std::string &filename );
|
||||
|
||||
|
||||
void check();
|
||||
|
||||
private:
|
||||
const Tokenizer *_tokenizer;
|
||||
ErrorLogger *_errorLogger;
|
||||
|
||||
|
||||
class FunctionUsage
|
||||
{
|
||||
public:
|
||||
FunctionUsage()
|
||||
{
|
||||
filename = "";
|
||||
usedOtherFile = false;
|
||||
usedSameFile = false;
|
||||
}
|
||||
|
||||
std::string filename;
|
||||
bool usedSameFile;
|
||||
bool usedOtherFile;
|
||||
};
|
||||
|
||||
std::map<std::string, FunctionUsage> functions;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#endif
|
||||
|
4
Makefile
4
Makefile
|
@ -1,4 +1,4 @@
|
|||
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp
|
||||
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp
|
||||
OBJS=$(SRCS:%.cpp=%.o)
|
||||
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o settings.o cppcheck.o
|
||||
BIN = ${DESTDIR}/usr/bin
|
||||
|
@ -17,6 +17,8 @@ CheckBufferOverrun.o: CheckBufferOverrun.cpp CheckBufferOverrun.h tokenize.h
|
|||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
CheckClass.o: CheckClass.cpp CheckClass.h tokenize.h
|
||||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
CheckFunctionUsage.o: CheckFunctionUsage.cpp CheckFunctionUsage.h tokenize.h
|
||||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
CheckHeaders.o: CheckHeaders.cpp CheckHeaders.h tokenize.h
|
||||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
CheckMemoryLeak.o: CheckMemoryLeak.cpp CheckMemoryLeak.h tokenize.h
|
||||
|
|
Loading…
Reference in New Issue