Refactoring: CppCheckExecutor class added
This commit is contained in:
parent
bc267bbca5
commit
ad5fbe5dc6
6
Makefile
6
Makefile
|
@ -1,12 +1,14 @@
|
|||
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp token.cpp
|
||||
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp token.cpp cppcheckexecutor.cpp
|
||||
OBJS=$(SRCS:%.cpp=%.o)
|
||||
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o settings.o cppcheck.o token.o
|
||||
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o
|
||||
BIN = ${DESTDIR}/usr/bin
|
||||
|
||||
all: ${OBJS} main.o
|
||||
g++ -Wall -g -o cppcheck $^
|
||||
test: ${OBJS} testrunner.o testsuite.o ${TESTS}
|
||||
g++ -Wall -g -o testrunner $^
|
||||
cppcheckexecutor.o: cppcheckexecutor.cpp cppcheckexecutor.h cppcheck.h errorlogger.h
|
||||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
cppcheck.o: cppcheck.cpp cppcheck.h settings.h errorlogger.h preprocessor.h tokenize.h token.h CheckMemoryLeak.h CheckBufferOverrun.h CheckClass.h CheckHeaders.h CheckOther.h CheckFunctionUsage.h FileLister.h
|
||||
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||
main.o: main.cpp cppcheck.h settings.h errorlogger.h
|
||||
|
|
50
cppcheck.cpp
50
cppcheck.cpp
|
@ -29,7 +29,6 @@
|
|||
#include "FileLister.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
@ -37,9 +36,9 @@
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
CppCheck::CppCheck() : _checkFunctionUsage( this )
|
||||
CppCheck::CppCheck( ErrorLogger &errorLogger ) : _checkFunctionUsage( this )
|
||||
{
|
||||
|
||||
_errorLogger = &errorLogger;
|
||||
}
|
||||
|
||||
CppCheck::~CppCheck()
|
||||
|
@ -148,8 +147,8 @@ void CppCheck::check()
|
|||
std::string fname = _filenames[c];
|
||||
|
||||
// If only errors are printed, print filename after the check
|
||||
if (!_settings._errorsOnly)
|
||||
std::cout << "Checking " << fname << "...\n";
|
||||
if ( _settings._errorsOnly == false )
|
||||
_errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) );
|
||||
|
||||
std::ifstream fin( fname.c_str() );
|
||||
std::map<std::string, std::string> code;
|
||||
|
@ -158,34 +157,18 @@ void CppCheck::check()
|
|||
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
||||
checkFile(it->second, _filenames[c].c_str());
|
||||
|
||||
if (_settings._errorsOnly)
|
||||
{
|
||||
if ( !_errout.str().empty() )
|
||||
{
|
||||
std::cout << "Errors found in " << fname << ":\n";
|
||||
std::cerr << _errout.str();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( _errout.str().empty() )
|
||||
std::cout << "No errors found\n";
|
||||
else
|
||||
std::cerr << _errout.str();
|
||||
}
|
||||
if ( _settings._errorsOnly == false && _errout.str().empty() )
|
||||
_errorLogger->reportOut( "No errors found" );
|
||||
}
|
||||
|
||||
// This generates false positives - especially for libraries
|
||||
if ( _settings._checkFunctionUsage )
|
||||
{
|
||||
_errout.str("");
|
||||
std::cout << "Checking usage of global functions (this may take several minutes)..\n";
|
||||
if( _settings._errorsOnly == false )
|
||||
_errorLogger->reportOut( "Checking usage of global functions (this may take several minutes).." );
|
||||
|
||||
_checkFunctionUsage.check();
|
||||
if ( ! _errout.str().empty() )
|
||||
{
|
||||
std::cerr << "\n";
|
||||
std::cerr << _errout.str();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -322,17 +305,14 @@ void CppCheck::reportErr( const std::string &errmsg)
|
|||
return;
|
||||
_errorList.push_back( errmsg );
|
||||
}
|
||||
|
||||
_errorLogger->reportErr( errmsg );
|
||||
|
||||
_errout << errmsg << std::endl;
|
||||
}
|
||||
|
||||
void CppCheck::reportErr( const TOKEN *token, const std::string &errmsg)
|
||||
void CppCheck::reportOut( const std::string &outmsg)
|
||||
{
|
||||
/*
|
||||
std::string message = _tokenizer.fileLine( token ) + errmsg;
|
||||
reportErr( message );
|
||||
*/
|
||||
reportErr( errmsg );
|
||||
// This is currently never called. It is here just to comply with
|
||||
// the interface.
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class CppCheck : public ErrorLogger
|
|||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
CppCheck();
|
||||
CppCheck( ErrorLogger &errorLogger );
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
|
@ -83,14 +83,16 @@ class CppCheck : public ErrorLogger
|
|||
|
||||
private:
|
||||
void checkFile(const std::string &code, const char FileName[]);
|
||||
void reportErr( const std::string &errmsg);
|
||||
void reportErr( const TOKEN *token, const std::string &errmsg);
|
||||
virtual void reportErr( const std::string &errmsg);
|
||||
//void reportErr( const TOKEN *token, const std::string &errmsg);
|
||||
virtual void reportOut( const std::string &outmsg);
|
||||
|
||||
std::list<std::string> _errorList;
|
||||
std::ostringstream _errout;
|
||||
Settings _settings;
|
||||
std::vector<std::string> _filenames;
|
||||
CheckFunctionUsage _checkFunctionUsage;
|
||||
ErrorLogger *_errorLogger;
|
||||
};
|
||||
|
||||
#endif // CPPCHECK_H
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
#include "cppcheckexecutor.h"
|
||||
#include "cppcheck.h"
|
||||
#include <iostream>
|
||||
|
||||
CppCheckExecutor::CppCheckExecutor()
|
||||
{
|
||||
//ctor
|
||||
}
|
||||
|
||||
CppCheckExecutor::~CppCheckExecutor()
|
||||
{
|
||||
//dtor
|
||||
}
|
||||
|
||||
void CppCheckExecutor::check( int argc, char* argv[] )
|
||||
{
|
||||
CppCheck cppCheck( *this );
|
||||
std::string result = cppCheck.parseFromArgs( argc, argv );
|
||||
if( result.length() == 0 )
|
||||
cppCheck.check();
|
||||
else
|
||||
std::cout << result;
|
||||
}
|
||||
|
||||
void CppCheckExecutor::reportErr( const std::string &errmsg)
|
||||
{
|
||||
std::cerr << errmsg << std::endl;
|
||||
}
|
||||
|
||||
void CppCheckExecutor::reportOut( const std::string &outmsg)
|
||||
{
|
||||
std::cout << outmsg << std::endl;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef CPPCHECKEXECUTOR_H
|
||||
#define CPPCHECKEXECUTOR_H
|
||||
|
||||
#include "errorlogger.h"
|
||||
|
||||
class CppCheckExecutor : public ErrorLogger
|
||||
{
|
||||
public:
|
||||
CppCheckExecutor();
|
||||
virtual ~CppCheckExecutor();
|
||||
void check( int argc, char* argv[] );
|
||||
void reportErr( const std::string &errmsg);
|
||||
void reportOut( const std::string &outmsg);
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
#endif // CPPCHECKEXECUTOR_H
|
|
@ -19,7 +19,7 @@
|
|||
#ifndef ERRORLOGGER_H
|
||||
#define ERRORLOGGER_H
|
||||
|
||||
class TOKEN;
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* This is an interface, which the class responsible of error logging
|
||||
|
@ -27,9 +27,9 @@ class TOKEN;
|
|||
*/
|
||||
class ErrorLogger
|
||||
{
|
||||
public:
|
||||
public:
|
||||
virtual void reportErr( const std::string &errmsg) = 0;
|
||||
virtual void reportErr( const TOKEN *token, const std::string &errmsg) = 0;
|
||||
virtual void reportOut( const std::string &outmsg) = 0;
|
||||
};
|
||||
|
||||
#endif // #ifndef ERRORLOGGER_H
|
||||
|
|
26
main.cpp
26
main.cpp
|
@ -16,24 +16,18 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/
|
||||
*/
|
||||
|
||||
#include "cppcheckexecutor.h"
|
||||
|
||||
#include "cppcheck.h"
|
||||
#include <iostream>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Main function of cppcheck
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Main function of cppcheck
|
||||
*
|
||||
* @param argc Passed to CppCheck::parseFromArgs()
|
||||
* @param argv Passed to CppCheck::parseFromArgs()
|
||||
* @return 0
|
||||
*/
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
CppCheck cppCheck;
|
||||
std::string result = cppCheck.parseFromArgs( argc, argv );
|
||||
if( result.length() == 0 )
|
||||
cppCheck.check();
|
||||
else
|
||||
std::cout << result;
|
||||
|
||||
CppCheckExecutor exec;
|
||||
exec.check( argc, argv );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -124,8 +124,7 @@ void TestFixture::reportErr( const std::string &errmsg)
|
|||
errout << errmsg << std::endl;
|
||||
}
|
||||
|
||||
void TestFixture::reportErr( const TOKEN *token, const std::string &errmsg)
|
||||
void TestFixture::reportOut( const std::string &outmsg)
|
||||
{
|
||||
reportErr( errmsg );
|
||||
// These can probably be ignored
|
||||
}
|
||||
|
||||
|
|
|
@ -40,9 +40,9 @@ protected:
|
|||
void assertFail(const char *filename, int linenr);
|
||||
|
||||
public:
|
||||
void reportErr( const std::string &errmsg);
|
||||
virtual void reportErr( const std::string &errmsg);
|
||||
|
||||
void reportErr( const TOKEN *token, const std::string &errmsg);
|
||||
virtual void reportOut( const std::string &outmsg);
|
||||
|
||||
TestFixture(const std::string &_name);
|
||||
virtual ~TestFixture() { }
|
||||
|
|
Loading…
Reference in New Issue