diff --git a/Makefile b/Makefile index 20344acee..76ba9a364 100644 --- a/Makefile +++ b/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 diff --git a/cppcheck.cpp b/cppcheck.cpp index 9b2654b49..5f1ad917e 100644 --- a/cppcheck.cpp +++ b/cppcheck.cpp @@ -29,7 +29,6 @@ #include "FileLister.h" #include -#include #include #include #include @@ -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 code; @@ -158,34 +157,18 @@ void CppCheck::check() for ( std::map::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. } - - - diff --git a/cppcheck.h b/cppcheck.h index b46b1118d..c79d0164b 100644 --- a/cppcheck.h +++ b/cppcheck.h @@ -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 _errorList; std::ostringstream _errout; Settings _settings; std::vector _filenames; CheckFunctionUsage _checkFunctionUsage; + ErrorLogger *_errorLogger; }; #endif // CPPCHECK_H diff --git a/cppcheckexecutor.cpp b/cppcheckexecutor.cpp new file mode 100644 index 000000000..7d9a5b847 --- /dev/null +++ b/cppcheckexecutor.cpp @@ -0,0 +1,33 @@ +#include "cppcheckexecutor.h" +#include "cppcheck.h" +#include + +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; +} diff --git a/cppcheckexecutor.h b/cppcheckexecutor.h new file mode 100644 index 000000000..9da0e278c --- /dev/null +++ b/cppcheckexecutor.h @@ -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 diff --git a/errorlogger.h b/errorlogger.h index 7eef369b5..9fde1c802 100644 --- a/errorlogger.h +++ b/errorlogger.h @@ -19,7 +19,7 @@ #ifndef ERRORLOGGER_H #define ERRORLOGGER_H -class TOKEN; +#include /** * 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 diff --git a/main.cpp b/main.cpp index 010cf52b5..4cf13d56d 100644 --- a/main.cpp +++ b/main.cpp @@ -15,25 +15,19 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see - -//--------------------------------------------------------------------------- -// 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; } - - diff --git a/testsuite.cpp b/testsuite.cpp index e31b84d5d..da499d401 100644 --- a/testsuite.cpp +++ b/testsuite.cpp @@ -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) -{ - reportErr( errmsg ); -} - +void TestFixture::reportOut( const std::string &outmsg) +{ + // These can probably be ignored +} diff --git a/testsuite.h b/testsuite.h index 0a10c8084..b2962ba2a 100644 --- a/testsuite.h +++ b/testsuite.h @@ -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() { }