From 0d3a99adb04739371e452e47bb60d1c91a21c198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Nov 2008 20:00:36 +0000 Subject: [PATCH] increased constness of member variable _tokenizer in the Check* classes --- CheckClass.cpp | 22 ++--- CheckClass.h | 56 +++++------ CheckHeaders.cpp | 22 ++--- CheckHeaders.h | 24 ++--- CheckMemoryLeak.cpp | 180 +++++++++++++++++------------------ CheckMemoryLeak.h | 108 ++++++++++----------- CheckOther.cpp | 4 +- CheckOther.h | 6 +- testbufferoverrun.cpp | 26 +++--- testcharvar.cpp | 19 ++-- testconstructors.cpp | 15 +-- testdivision.cpp | 7 +- testincompletestatement.cpp | 9 +- testmemleak.cpp | 181 ++++++++++++++++++------------------ testtokenize.cpp | 22 ++--- testunusedprivfunc.cpp | 9 +- testunusedvar.cpp | 3 +- tokenize.cpp | 2 +- tokenize.h | 2 +- 19 files changed, 358 insertions(+), 359 deletions(-) diff --git a/CheckClass.cpp b/CheckClass.cpp index c73956689..b4a57e25c 100644 --- a/CheckClass.cpp +++ b/CheckClass.cpp @@ -33,17 +33,17 @@ #endif //--------------------------------------------------------------------------- -CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) -{ - _tokenizer = tokenizer; - _settings = settings; - _errorLogger = errorLogger; -} - -CheckClass::~CheckClass() -{ - -} +CheckClass::CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) +{ + _tokenizer = tokenizer; + _settings = settings; + _errorLogger = errorLogger; +} + +CheckClass::~CheckClass() +{ + +} //--------------------------------------------------------------------------- diff --git a/CheckClass.h b/CheckClass.h index 5f355cfda..280b06d5e 100644 --- a/CheckClass.h +++ b/CheckClass.h @@ -20,24 +20,24 @@ #ifndef CheckClassH #define CheckClassH //--------------------------------------------------------------------------- - -#include "tokenize.h" -#include "settings.h" -#include "errorlogger.h" -#include - -struct VAR -{ - const char *name; - bool init; - struct VAR *next; -}; -class CheckClass -{ -public: - CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); - ~CheckClass(); +#include "tokenize.h" +#include "settings.h" +#include "errorlogger.h" +#include + +struct VAR +{ + const char *name; + bool init; + struct VAR *next; +}; + +class CheckClass +{ +public: + CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); + ~CheckClass(); void CheckConstructors(); @@ -45,17 +45,17 @@ public: void CheckMemset(); - void CheckOperatorEq1(); // Warning upon "void operator=(.." - -private: - void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list &callstack); - void InitVar(struct VAR *varlist, const char varname[]); - const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ); - struct VAR *ClassChecking_GetVarList(const TOKEN *tok1); - - Tokenizer *_tokenizer; - Settings _settings; - ErrorLogger *_errorLogger; + void CheckOperatorEq1(); // Warning upon "void operator=(.." + +private: + void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list &callstack); + void InitVar(struct VAR *varlist, const char varname[]); + const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ); + struct VAR *ClassChecking_GetVarList(const TOKEN *tok1); + + const Tokenizer *_tokenizer; + Settings _settings; + ErrorLogger *_errorLogger; }; //--------------------------------------------------------------------------- #endif diff --git a/CheckHeaders.cpp b/CheckHeaders.cpp index b990b3cfa..3735779c8 100644 --- a/CheckHeaders.cpp +++ b/CheckHeaders.cpp @@ -34,17 +34,17 @@ //--------------------------------------------------------------------------- // HEADERS - No implementation in a header //--------------------------------------------------------------------------- - -CheckHeaders::CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger ) -{ - _tokenizer = tokenizer; - _errorLogger = errorLogger; -} - -CheckHeaders::~CheckHeaders() -{ - -} + +CheckHeaders::CheckHeaders( const Tokenizer *tokenizer, ErrorLogger *errorLogger ) +{ + _tokenizer = tokenizer; + _errorLogger = errorLogger; +} + +CheckHeaders::~CheckHeaders() +{ + +} void CheckHeaders::WarningHeaderWithImplementation() { diff --git a/CheckHeaders.h b/CheckHeaders.h index 852bdffe7..efd68e78c 100644 --- a/CheckHeaders.h +++ b/CheckHeaders.h @@ -21,20 +21,20 @@ #ifndef CheckHeadersH #define CheckHeadersH //--------------------------------------------------------------------------- - -#include "tokenize.h" -#include "errorlogger.h" - -class CheckHeaders -{ -public: - CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger ); + +#include "tokenize.h" +#include "errorlogger.h" + +class CheckHeaders +{ +public: + CheckHeaders( const Tokenizer *tokenizer, ErrorLogger *errorLogger ); ~CheckHeaders(); void WarningHeaderWithImplementation(); - void WarningIncludeHeader(); - -private: - Tokenizer *_tokenizer; + void WarningIncludeHeader(); + +private: + const Tokenizer *_tokenizer; ErrorLogger *_errorLogger; }; diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 06e01c024..77d3ac2c7 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -37,10 +37,10 @@ //--------------------------------------------------------------------------- -CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) +CheckMemoryLeakClass::CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) { - _tokenizer = tokenizer; - _settings = settings; + _tokenizer = tokenizer; + _settings = settings; _errorLogger = errorLogger; } @@ -95,7 +95,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T { if ( strcmp(mallocfunc[i], tok2->str) == 0 ) return Malloc; - } + } // Does tok2 point on "malloc", "strdup" or "kmalloc".. const char *gmallocfunc[] = {"g_new", @@ -124,12 +124,12 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T if ( Tokenizer::Match( tok2, "new %type% [" ) ) return NewA; - - if ( Tokenizer::Match( tok2, "fopen (" ) ) - return FOPEN; - - if ( Tokenizer::Match( tok2, "popen (" ) ) - return POPEN; + + if ( Tokenizer::Match( tok2, "fopen (" ) ) + return FOPEN; + + if ( Tokenizer::Match( tok2, "popen (" ) ) + return POPEN; // Userdefined allocation function.. std::list::const_iterator it = listallocfunc.begin(); @@ -167,12 +167,12 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const if ( Tokenizer::Match(tok, "g_free ( %var1% ) ;", varnames) ) return gMalloc; - - if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) ) - return FOPEN; - - if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) ) - return POPEN; + + if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) ) + return FOPEN; + + if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) ) + return POPEN; return No; } @@ -185,15 +185,15 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list 2 ) - return "dealloc"; - const char *funcname = tok->str; - for ( std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) - { - if ( std::string(funcname) == (*it)->str ) - return "dealloc"; + if ( callstack.size() > 2 ) + return "dealloc"; + + const char *funcname = tok->str; + for ( std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) + { + if ( std::string(funcname) == (*it)->str ) + return "dealloc"; } callstack.push_back(tok); @@ -244,8 +244,8 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list &callstack, const char varname[] ) { - std::ostringstream errmsg; - for ( std::list::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok ) + std::ostringstream errmsg; + for ( std::list::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok ) errmsg << _tokenizer->fileLine(*tok) << " -> "; errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname; _errorLogger->reportErr( errmsg.str() ); @@ -253,7 +253,7 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::listfileLine(tok) << ": Memory leak: " << varname; _errorLogger->reportErr( errmsg.str() ); @@ -370,9 +370,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list dealloctype = dealloc; } - // if else switch + // if else switch if ( Tokenizer::Match(tok, "if ( %var1% )", varnames) || - Tokenizer::Match(tok, "if ( %var1% != 0 )", varnames) || + Tokenizer::Match(tok, "if ( %var1% != 0 )", varnames) || Tokenizer::Match(tok, "if ( 0 != %var1% )", varnames) ) { addtoken("if(var)"); @@ -381,40 +381,40 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list else if ( Tokenizer::Match(tok, "if (") && notvar(Tokenizer::gettok(tok,2), varnames) ) { addtoken("if(!var)"); - } - else if ( Tokenizer::Match(tok, "if ( true )") ) - { - addtoken("if(true)"); - } - else if ( Tokenizer::Match(tok, "if ( false )") ) - { - addtoken("if(false)"); - } - else if ( Tokenizer::Match(tok, "if") ) - { - // Check if the condition depends on var somehow.. - bool dep = false; - int parlevel = 0; - for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next ) - { - if ( Tokenizer::Match(tok2,"(") ) - ++parlevel; - if ( Tokenizer::Match(tok2,")") ) - { - --parlevel; - if ( parlevel <= 0 ) - break; - } - if ( !Tokenizer::Match(tok2,".") && - Tokenizer::Match(tok2->next, "%var1%", varnames) && - !Tokenizer::Match(tok2->next, "%var1% .", varnames) ) - { - dep = true; - break; - } - } - addtoken( (dep ? "ifv" : "if") ); - } + } + else if ( Tokenizer::Match(tok, "if ( true )") ) + { + addtoken("if(true)"); + } + else if ( Tokenizer::Match(tok, "if ( false )") ) + { + addtoken("if(false)"); + } + else if ( Tokenizer::Match(tok, "if") ) + { + // Check if the condition depends on var somehow.. + bool dep = false; + int parlevel = 0; + for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next ) + { + if ( Tokenizer::Match(tok2,"(") ) + ++parlevel; + if ( Tokenizer::Match(tok2,")") ) + { + --parlevel; + if ( parlevel <= 0 ) + break; + } + if ( !Tokenizer::Match(tok2,".") && + Tokenizer::Match(tok2->next, "%var1%", varnames) && + !Tokenizer::Match(tok2->next, "%var1% .", varnames) ) + { + dep = true; + break; + } + } + addtoken( (dep ? "ifv" : "if") ); + } else if ( Tokenizer::Match(tok, "else") || Tokenizer::Match(tok, "switch") ) { addtoken(tok->str); @@ -605,10 +605,10 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Delete empty if that is not followed by an else if ( Tokenizer::Match(tok2,"[;{}] if ;") || - Tokenizer::Match(tok2,"[;{}] if(var) ;") || - Tokenizer::Match(tok2,"[;{}] if(!var) ;") || - Tokenizer::Match(tok2,"[;{}] if(true) ;") || - Tokenizer::Match(tok2,"[;{}] if(false) ;") || + Tokenizer::Match(tok2,"[;{}] if(var) ;") || + Tokenizer::Match(tok2,"[;{}] if(!var) ;") || + Tokenizer::Match(tok2,"[;{}] if(true) ;") || + Tokenizer::Match(tok2,"[;{}] if(false) ;") || Tokenizer::Match(tok2,"[;{}] ifv ;") ) { if ( ! Tokenizer::Match(Tokenizer::gettok(tok2,3), "else") ) @@ -620,8 +620,8 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. - // This may cause false positives - if (_settings._showAll && + // This may cause false positives + if (_settings._showAll && (Tokenizer::Match(tok2, "[;{}] if dealloc ;") || Tokenizer::Match(tok2, "[;{}] if use ;")) && !Tokenizer::Match(Tokenizer::gettok(tok2,4), "else")) { @@ -788,12 +788,12 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) -{ +{ std::list callstack; - AllocType alloctype = No; - AllocType dealloctype = No; - + AllocType alloctype = No; + AllocType dealloctype = No; + TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); // If the variable is not allocated at all => no memory leak @@ -831,21 +831,21 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const { MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if return ;"), 3), varname); } - - else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") ) - { - MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname); - } - - else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") ) - { - MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname); - } - - else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") ) - { - MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname); - } + + else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") ) + { + MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname); + } + + else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") ) + { + MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname); + } + + else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") ) + { + MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname); + } else if ( Tokenizer::findmatch(tok, "alloc ; return ;") ) { @@ -1048,7 +1048,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec { AllocType alloc = GetAllocationType( Tokenizer::gettok( tok, 2 ) ); if ( alloc != No ) - { + { std::list callstack; if ( Dealloc != No && Dealloc != alloc ) MismatchError( tok, callstack, FullVariableName.str().c_str() ); @@ -1063,7 +1063,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec varnames[0] = varname; AllocType dealloc = GetDeallocationType( tok, varnames ); if ( dealloc != No ) - { + { std::list callstack; if ( Dealloc != No && Dealloc != dealloc ) MismatchError( tok, callstack, FullVariableName.str().c_str() ); diff --git a/CheckMemoryLeak.h b/CheckMemoryLeak.h index bdf7baeb6..26ac52cc8 100644 --- a/CheckMemoryLeak.h +++ b/CheckMemoryLeak.h @@ -24,60 +24,60 @@ //--------------------------------------------------------------------------- /** \brief Check for memory leaks */ - -#include "tokenize.h" -#include "settings.h" -#include "errorlogger.h" -#include -#include - -class CheckMemoryLeakClass -{ -public: - CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); - ~CheckMemoryLeakClass(); - void CheckMemoryLeak(); - -private: - - enum AllocType { No, Malloc, gMalloc, New, NewA, FOPEN, POPEN }; - - // Extra allocation.. - class AllocFunc - { - public: - const char *funcname; - AllocType alloctype; - - AllocFunc(const char f[], AllocType a) - { - funcname = f; - alloctype = a; - } - }; - - void CheckMemoryLeak_ClassMembers_Variable( const std::vector &classname, const char varname[] ); - void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector &classname ); - void CheckMemoryLeak_ClassMembers(); - void CheckMemoryLeak_InFunction(); - void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ); - void simplifycode(TOKEN *tok); - void erase(TOKEN *begin, const TOKEN *end); - - TOKEN *getcode(const TOKEN *tok, std::list callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype); - bool notvar(const TOKEN *tok, const char *varnames[]); - void instoken(TOKEN *tok, const char str[]); - void MemoryLeak( const TOKEN *tok, const char varname[] ); - void MismatchError( const TOKEN *Tok1, const std::list &callstack, const char varname[] ); - const char * call_func( const TOKEN *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ); - AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[]); - AllocType GetAllocationType( const TOKEN *tok2 ); - bool isclass( const std::string &typestr ); - - Tokenizer *_tokenizer; - ErrorLogger *_errorLogger; - Settings _settings; - std::list listallocfunc; + +#include "tokenize.h" +#include "settings.h" +#include "errorlogger.h" +#include +#include + +class CheckMemoryLeakClass +{ +public: + CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); + ~CheckMemoryLeakClass(); + void CheckMemoryLeak(); + +private: + + enum AllocType { No, Malloc, gMalloc, New, NewA, FOPEN, POPEN }; + + // Extra allocation.. + class AllocFunc + { + public: + const char *funcname; + AllocType alloctype; + + AllocFunc(const char f[], AllocType a) + { + funcname = f; + alloctype = a; + } + }; + + void CheckMemoryLeak_ClassMembers_Variable( const std::vector &classname, const char varname[] ); + void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector &classname ); + void CheckMemoryLeak_ClassMembers(); + void CheckMemoryLeak_InFunction(); + void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ); + void simplifycode(TOKEN *tok); + void erase(TOKEN *begin, const TOKEN *end); + + TOKEN *getcode(const TOKEN *tok, std::list callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype); + bool notvar(const TOKEN *tok, const char *varnames[]); + void instoken(TOKEN *tok, const char str[]); + void MemoryLeak( const TOKEN *tok, const char varname[] ); + void MismatchError( const TOKEN *Tok1, const std::list &callstack, const char varname[] ); + const char * call_func( const TOKEN *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ); + AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[]); + AllocType GetAllocationType( const TOKEN *tok2 ); + bool isclass( const std::string &typestr ); + + const Tokenizer *_tokenizer; + ErrorLogger *_errorLogger; + Settings _settings; + std::list listallocfunc; }; //--------------------------------------------------------------------------- diff --git a/CheckOther.cpp b/CheckOther.cpp index 8cfb8e8e4..04a113013 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -34,9 +34,9 @@ // Warning on C-Style casts.. p = (kalle *)foo; //--------------------------------------------------------------------------- -CheckOther::CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger ) +CheckOther::CheckOther( const Tokenizer *tokenizer, ErrorLogger *errorLogger ) { - _tokenizer = tokenizer; + _tokenizer = tokenizer; _errorLogger = errorLogger; } diff --git a/CheckOther.h b/CheckOther.h index 2ce44afc8..ccfa61e17 100644 --- a/CheckOther.h +++ b/CheckOther.h @@ -23,13 +23,13 @@ #define CheckOtherH //--------------------------------------------------------------------------- -#include "tokenize.h" +#include "tokenize.h" #include "errorlogger.h" class CheckOther { public: - CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger ); + CheckOther( const Tokenizer *tokenizer, ErrorLogger *errorLogger ); ~CheckOther(); // Casting @@ -76,7 +76,7 @@ public: private: void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] ); - Tokenizer *_tokenizer; + const Tokenizer *_tokenizer; ErrorLogger *_errorLogger; }; diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index 290945efc..9dc51f9f2 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -17,10 +17,10 @@ */ - +#define UNIT_TESTING #include "tokenize.h" #include "CheckBufferOverrun.h" -#include "testsuite.h" +#include "testsuite.h" #include @@ -32,22 +32,22 @@ public: TestBufferOverrun() : TestFixture("TestBufferOverrun") { } -private: - - +private: + + void check( const char code[] ) { - // Tokenize.. - Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(code); + // Tokenize.. + Tokenizer tokenizer; + tokenizer.Files.push_back( "test.cpp" ); + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); - // Fill function list - Settings settings; - settings._checkCodingStyle = true; + // Fill function list + Settings settings; + settings._checkCodingStyle = true; tokenizer.settings( settings ); tokenizer.FillFunctionList(0); @@ -323,7 +323,7 @@ private: "Fred::Fred()\n" "{\n" " str[10] = 0;\n" - "}\n" ); + "}\n" ); std::string err( errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:10]: Array index out of bounds\n"), err ); } diff --git a/testcharvar.cpp b/testcharvar.cpp index ecbce317a..c4402e8f9 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -17,9 +17,10 @@ */ +#define UNIT_TESTING #include "tokenize.h" #include "CheckOther.h" -#include "testsuite.h" +#include "testsuite.h" #include extern std::ostringstream errout; @@ -30,8 +31,8 @@ public: TestCharVar() : TestFixture("TestCharVar") { } -private: - +private: + void run() { @@ -43,14 +44,14 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(code); + Tokenizer tokenizer; + tokenizer.Files.push_back( "test.cpp" ); + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); - // Fill function list - Settings settings; - settings._checkCodingStyle = true; + // Fill function list + Settings settings; + settings._checkCodingStyle = true; tokenizer.settings( settings ); tokenizer.FillFunctionList(0); diff --git a/testconstructors.cpp b/testconstructors.cpp index 3f29f5a18..08bb597f8 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -18,12 +18,13 @@ +#define UNIT_TESTING #include "tokenize.h" #include "CheckClass.h" -#include "testsuite.h" +#include "testsuite.h" #include -extern std::ostringstream errout; +extern std::ostringstream errout; class TestConstructors : public TestFixture { @@ -31,14 +32,14 @@ public: TestConstructors() : TestFixture("TestConstructors") { } -private: - +private: + void check( const char code[] ) { // Tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); @@ -46,8 +47,8 @@ private: // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. - Settings settings; + // Check for memory leaks.. + Settings settings; settings._checkCodingStyle = true; CheckClass checkClass( &tokenizer, settings, this ); checkClass.CheckConstructors(); diff --git a/testdivision.cpp b/testdivision.cpp index ccbc06300..9e55675a9 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -21,6 +21,7 @@ // such as "svar / uvar". Treating "svar" as unsigned data is not good +#define UNIT_TESTING #include "tokenize.h" #include "CheckOther.h" #include "testsuite.h" @@ -39,9 +40,9 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(code); + Tokenizer tokenizer; + tokenizer.Files.push_back( "test.cpp" ); + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); //SimplifyTokenList(); <- this can't be used as it removes 'unsigned' diff --git a/testincompletestatement.cpp b/testincompletestatement.cpp index 85ae74162..e7ab6cdcd 100644 --- a/testincompletestatement.cpp +++ b/testincompletestatement.cpp @@ -20,6 +20,7 @@ // Check for unused variables.. +#define UNIT_TESTING #include "testsuite.h" #include "tokenize.h" #include "CheckOther.h" @@ -38,16 +39,16 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(code); + Tokenizer tokenizer; + tokenizer.Files.push_back( "test.cpp" ); + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); // Clear the error buffer.. errout.str(""); - // Check for unused variables.. + // Check for unused variables.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckIncompleteStatement(); } diff --git a/testmemleak.cpp b/testmemleak.cpp index bf77deb78..b892ed726 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -19,6 +19,7 @@ +#define UNIT_TESTING #include "tokenize.h" #include "CheckMemoryLeak.h" #include "testsuite.h" @@ -38,7 +39,7 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); @@ -48,7 +49,7 @@ private: // Check for memory leaks.. Settings settings; - settings._checkCodingStyle = true; + settings._checkCodingStyle = true; settings._showAll = false; tokenizer.settings( settings ); tokenizer.FillFunctionList(0); @@ -81,11 +82,11 @@ private: TEST_CASE( ifelse9 ); TEST_CASE( if1 ); - TEST_CASE( if2 ); - TEST_CASE( if3 ); - TEST_CASE( if4 ); - TEST_CASE( if5 ); - + TEST_CASE( if2 ); + TEST_CASE( if3 ); + TEST_CASE( if4 ); + TEST_CASE( if5 ); + TEST_CASE( alwaysTrue ); TEST_CASE( forwhile1 ); @@ -110,8 +111,8 @@ private: TEST_CASE( func2 ); TEST_CASE( func3 ); TEST_CASE( func4 ); - TEST_CASE( func5 ); - TEST_CASE( func6 ); + TEST_CASE( func5 ); + TEST_CASE( func6 ); TEST_CASE( class1 ); TEST_CASE( class2 ); @@ -222,8 +223,8 @@ private: { check( "void foo()\n" "{\n" - " char *str;\n" - " if (somecondition)\n" + " char *str;\n" + " if (somecondition)\n" " str = strdup(\"abc\");\n" " if (somecondition)\n" " DeleteString(str);\n" @@ -415,66 +416,66 @@ private: "}\n" ); ASSERT_EQUALS( std::string(""), errout.str() ); } - - void if3() - { - check( "void f()\n" - "{\n" - " char *s = new char[100];\n" - " if (0 != s)\n" - " foo(s);\n" - "}\n" ); - ASSERT_EQUALS( std::string(""), errout.str() ); - } - - void if4() - { - check( "void f()\n" - "{\n" - " char *s;\n" - " bool b = true;\n" - " if (b && (s = malloc(256)))\n" - " ;\n" - " if (b)\n" - " free(s);\n" - "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); - } - - void if5() - { - check( "void f()\n" - "{\n" - " char *p = malloc(256);\n" - " if (somecondition && !p)\n" - " return;\n" - " free(p);\n" - "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); - } - - - - - - void alwaysTrue() - { - check( "void f()\n" - "{\n" - " char *p = 0;\n" - " for (;;)\n" - " {\n" - " p = malloc(256);\n" - " if (1)\n" - " break;\n" - " }\n" - " free(p);\n" - "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); - } + + void if3() + { + check( "void f()\n" + "{\n" + " char *s = new char[100];\n" + " if (0 != s)\n" + " foo(s);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + void if4() + { + check( "void f()\n" + "{\n" + " char *s;\n" + " bool b = true;\n" + " if (b && (s = malloc(256)))\n" + " ;\n" + " if (b)\n" + " free(s);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } + + void if5() + { + check( "void f()\n" + "{\n" + " char *p = malloc(256);\n" + " if (somecondition && !p)\n" + " return;\n" + " free(p);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } + + + + + + void alwaysTrue() + { + check( "void f()\n" + "{\n" + " char *p = 0;\n" + " for (;;)\n" + " {\n" + " p = malloc(256);\n" + " if (1)\n" + " break;\n" + " }\n" + " free(p);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } @@ -760,28 +761,28 @@ private: "{\n" " char *p = new char[100];\n" " foo(p);\n" - "}\n" ); - std::string err( errout.str() ); + "}\n" ); + std::string err( errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err ); } - - void func6() - { - check( "static void foo(char *str)\n" - "{\n" - " goto abc;\n" - "}\n" - "\n" - "static void f()\n" - "{\n" - " char *p = new char[100];\n" - " foo(p);\n" - "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); - } - + + void func6() + { + check( "static void foo(char *str)\n" + "{\n" + " goto abc;\n" + "}\n" + "\n" + "static void f()\n" + "{\n" + " char *p = new char[100];\n" + " foo(p);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } + /* diff --git a/testtokenize.cpp b/testtokenize.cpp index 2dcfc68ed..e6592b7ee 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -24,7 +24,7 @@ #include "testsuite.h" #define UNIT_TESTING // Get access to "private" data in Tokenizer #include "tokenize.h" - + extern std::ostringstream errout; class TestTokenizer : public TestFixture { @@ -41,9 +41,7 @@ private: TEST_CASE( inlineasm ); - TEST_CASE( dupfuncname ); - - TEST_CASE( simplifyConditions ); + TEST_CASE( dupfuncname ); } @@ -66,7 +64,7 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(filedata); tokenizer.TokenizeCode(istr, 0); @@ -90,7 +88,7 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(filedata); tokenizer.TokenizeCode(istr, 0); @@ -112,7 +110,7 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(filedata); tokenizer.TokenizeCode(istr, 0); @@ -143,7 +141,7 @@ private: "{ }\n"; // tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode(istr, 0); @@ -151,13 +149,7 @@ private: ASSERT_EQUALS( 1, tokenizer.FunctionList.size() ); ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str ); - } - - void simplifyConditions() - { - // TODO Simplify various conditions.. - - } + } }; REGISTER_TEST( TestTokenizer ) diff --git a/testunusedprivfunc.cpp b/testunusedprivfunc.cpp index 5f5bad26c..f351a75c3 100644 --- a/testunusedprivfunc.cpp +++ b/testunusedprivfunc.cpp @@ -17,6 +17,7 @@ */ +#define UNIT_TESTING #include "tokenize.h" #include "CheckClass.h" #include "testsuite.h" @@ -44,17 +45,17 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + Tokenizer tokenizer; + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); // Clear the error buffer.. errout.str(""); - // Check for unused private functions.. + // Check for unused private functions.. Settings settings; - settings._checkCodingStyle = true; + settings._checkCodingStyle = true; CheckClass checkClass( &tokenizer, settings, this ); checkClass.CheckUnusedPrivateFunctions(); } diff --git a/testunusedvar.cpp b/testunusedvar.cpp index fc9b5d2c2..c5d26094f 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -20,6 +20,7 @@ // Check for unused variables.. +#define UNIT_TESTING #include "testsuite.h" #include "tokenize.h" #include "CheckOther.h" @@ -38,7 +39,7 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer.getFiles()->push_back( "test.cpp" ); + tokenizer.Files.push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/tokenize.cpp b/tokenize.cpp index 112565626..914cc5628 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -87,7 +87,7 @@ const TOKEN *Tokenizer::tokens() const -std::vector *Tokenizer::getFiles() +const std::vector *Tokenizer::getFiles() const { return &Files; } diff --git a/tokenize.h b/tokenize.h index 4987e75db..b5a716402 100644 --- a/tokenize.h +++ b/tokenize.h @@ -98,7 +98,7 @@ public: void initTokens(); - std::vector *getFiles(); + const std::vector *getFiles() const; void FillFunctionList(const unsigned int file_id); const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;