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)
|
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
|
BIN = ${DESTDIR}/usr/bin
|
||||||
|
|
||||||
all: ${OBJS} main.o
|
all: ${OBJS} main.o
|
||||||
g++ -Wall -g -o cppcheck $^
|
g++ -Wall -g -o cppcheck $^
|
||||||
test: ${OBJS} testrunner.o testsuite.o ${TESTS}
|
test: ${OBJS} testrunner.o testsuite.o ${TESTS}
|
||||||
g++ -Wall -g -o testrunner $^
|
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
|
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
|
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
|
||||||
main.o: main.cpp cppcheck.h settings.h errorlogger.h
|
main.o: main.cpp cppcheck.h settings.h errorlogger.h
|
||||||
|
|
50
cppcheck.cpp
50
cppcheck.cpp
|
@ -29,7 +29,6 @@
|
||||||
#include "FileLister.h"
|
#include "FileLister.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -37,9 +36,9 @@
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CppCheck::CppCheck() : _checkFunctionUsage( this )
|
CppCheck::CppCheck( ErrorLogger &errorLogger ) : _checkFunctionUsage( this )
|
||||||
{
|
{
|
||||||
|
_errorLogger = &errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CppCheck::~CppCheck()
|
CppCheck::~CppCheck()
|
||||||
|
@ -148,8 +147,8 @@ void CppCheck::check()
|
||||||
std::string fname = _filenames[c];
|
std::string fname = _filenames[c];
|
||||||
|
|
||||||
// If only errors are printed, print filename after the check
|
// If only errors are printed, print filename after the check
|
||||||
if (!_settings._errorsOnly)
|
if ( _settings._errorsOnly == false )
|
||||||
std::cout << "Checking " << fname << "...\n";
|
_errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) );
|
||||||
|
|
||||||
std::ifstream fin( fname.c_str() );
|
std::ifstream fin( fname.c_str() );
|
||||||
std::map<std::string, std::string> code;
|
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 )
|
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
||||||
checkFile(it->second, _filenames[c].c_str());
|
checkFile(it->second, _filenames[c].c_str());
|
||||||
|
|
||||||
if (_settings._errorsOnly)
|
if ( _settings._errorsOnly == false && _errout.str().empty() )
|
||||||
{
|
_errorLogger->reportOut( "No errors found" );
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This generates false positives - especially for libraries
|
// This generates false positives - especially for libraries
|
||||||
if ( _settings._checkFunctionUsage )
|
if ( _settings._checkFunctionUsage )
|
||||||
{
|
{
|
||||||
_errout.str("");
|
_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();
|
_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;
|
return;
|
||||||
_errorList.push_back( errmsg );
|
_errorList.push_back( errmsg );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_errorLogger->reportErr( errmsg );
|
||||||
|
|
||||||
_errout << errmsg << std::endl;
|
_errout << errmsg << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppCheck::reportErr( const TOKEN *token, const std::string &errmsg)
|
void CppCheck::reportOut( const std::string &outmsg)
|
||||||
{
|
{
|
||||||
/*
|
// This is currently never called. It is here just to comply with
|
||||||
std::string message = _tokenizer.fileLine( token ) + errmsg;
|
// the interface.
|
||||||
reportErr( message );
|
|
||||||
*/
|
|
||||||
reportErr( errmsg );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ class CppCheck : public ErrorLogger
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
CppCheck();
|
CppCheck( ErrorLogger &errorLogger );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor.
|
* Destructor.
|
||||||
|
@ -83,14 +83,16 @@ class CppCheck : public ErrorLogger
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void checkFile(const std::string &code, const char FileName[]);
|
void checkFile(const std::string &code, const char FileName[]);
|
||||||
void reportErr( const std::string &errmsg);
|
virtual void reportErr( const std::string &errmsg);
|
||||||
void reportErr( const TOKEN *token, 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::list<std::string> _errorList;
|
||||||
std::ostringstream _errout;
|
std::ostringstream _errout;
|
||||||
Settings _settings;
|
Settings _settings;
|
||||||
std::vector<std::string> _filenames;
|
std::vector<std::string> _filenames;
|
||||||
CheckFunctionUsage _checkFunctionUsage;
|
CheckFunctionUsage _checkFunctionUsage;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CPPCHECK_H
|
#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
|
#ifndef ERRORLOGGER_H
|
||||||
#define ERRORLOGGER_H
|
#define ERRORLOGGER_H
|
||||||
|
|
||||||
class TOKEN;
|
#include <string>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an interface, which the class responsible of error logging
|
* This is an interface, which the class responsible of error logging
|
||||||
|
@ -29,7 +29,7 @@ class ErrorLogger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void reportErr( const std::string &errmsg) = 0;
|
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
|
#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/
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "cppcheckexecutor.h"
|
||||||
|
|
||||||
#include "cppcheck.h"
|
/**
|
||||||
#include <iostream>
|
* Main function of cppcheck
|
||||||
|
*
|
||||||
//---------------------------------------------------------------------------
|
* @param argc Passed to CppCheck::parseFromArgs()
|
||||||
// Main function of cppcheck
|
* @param argv Passed to CppCheck::parseFromArgs()
|
||||||
//---------------------------------------------------------------------------
|
* @return 0
|
||||||
|
*/
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
CppCheck cppCheck;
|
CppCheckExecutor exec;
|
||||||
std::string result = cppCheck.parseFromArgs( argc, argv );
|
exec.check( argc, argv );
|
||||||
if( result.length() == 0 )
|
|
||||||
cppCheck.check();
|
|
||||||
else
|
|
||||||
std::cout << result;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -124,8 +124,7 @@ void TestFixture::reportErr( const std::string &errmsg)
|
||||||
errout << errmsg << std::endl;
|
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);
|
void assertFail(const char *filename, int linenr);
|
||||||
|
|
||||||
public:
|
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);
|
TestFixture(const std::string &_name);
|
||||||
virtual ~TestFixture() { }
|
virtual ~TestFixture() { }
|
||||||
|
|
Loading…
Reference in New Issue