From 5d1d2676249d592017e98e5387a943507bd3be53 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 3 Jan 2009 20:29:20 +0000 Subject: [PATCH] Refactoring: Rename class "TOKEN" to "Token" --- checkbufferoverrun.cpp | 98 +++++------ checkbufferoverrun.h | 6 +- checkclass.cpp | 142 +++++++-------- checkclass.h | 8 +- checkfunctionusage.cpp | 42 ++--- checkheaders.cpp | 38 ++-- checkmemoryleak.cpp | 384 ++++++++++++++++++++--------------------- checkmemoryleak.h | 30 ++-- checkother.cpp | 196 ++++++++++----------- checkother.h | 2 +- tasks.txt | 1 - testmemleak.cpp | 12 +- testmemleakmp.cpp | 4 +- testsimplifytokens.cpp | 2 +- testsuite.h | 2 +- testtoken.cpp | 4 +- testtokenize.cpp | 68 ++++---- token.cpp | 66 +++---- token.h | 34 ++-- tokenize.cpp | 254 +++++++++++++-------------- tokenize.h | 22 +-- 21 files changed, 707 insertions(+), 708 deletions(-) diff --git a/checkbufferoverrun.cpp b/checkbufferoverrun.cpp index c51c3368b..df4877ea6 100644 --- a/checkbufferoverrun.cpp +++ b/checkbufferoverrun.cpp @@ -48,10 +48,10 @@ CheckBufferOverrunClass::~CheckBufferOverrunClass() } // Modified version of 'ReportError' that also reports the callstack -void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[]) +void CheckBufferOverrunClass::ReportError(const Token *tok, const char errmsg[]) { std::ostringstream ostr; - std::list::const_iterator it; + std::list::const_iterator it; for ( it = _callStack.begin(); it != _callStack.end(); it++ ) ostr << _tokenizer->fileLine(*it ) << " -> "; ostr << _tokenizer->fileLine(tok) << ": " << errmsg; @@ -64,7 +64,7 @@ void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[]) // Check array usage.. //--------------------------------------------------------------------------- -void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, const char *varname[], const int size, const int total_size, unsigned int varid) +void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, const char *varname[], const int size, const int total_size, unsigned int varid) { unsigned int varc = 1; while ( varname[varc] ) @@ -75,7 +75,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // Array index.. if ( varid > 0 ) { - if ( TOKEN::Match(tok, "%varid% [ %num% ]", 0, varid) ) + if ( Token::Match(tok, "%varid% [ %num% ]", 0, varid) ) { const char *num = tok->strAt(2); if (strtol(num, NULL, 10) >= size) @@ -84,7 +84,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co } } } - else if ( TOKEN::Match(tok, "%var1% [ %num% ]", varname) ) + else if ( Token::Match(tok, "%var1% [ %num% ]", varname) ) { const char *num = tok->strAt(2 + varc); if (strtol(num, NULL, 10) >= size) @@ -112,7 +112,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // Array index.. if ( varid > 0 ) { - if ( !tok->isName() && !TOKEN::Match(tok, "[.&]") && TOKEN::Match(tok->next(), "%varid% [ %num% ]", 0, varid) ) + if ( !tok->isName() && !Token::Match(tok, "[.&]") && Token::Match(tok->next(), "%varid% [ %num% ]", 0, varid) ) { const char *num = tok->strAt(3); if (strtol(num, NULL, 10) >= size) @@ -121,7 +121,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co } } } - else if ( !tok->isName() && !TOKEN::Match(tok, "[.&]") && TOKEN::Match(tok->next(), "%var1% [ %num% ]", varname) ) + else if ( !tok->isName() && !Token::Match(tok, "[.&]") && Token::Match(tok->next(), "%var1% [ %num% ]", varname) ) { const char *num = tok->next()->strAt(2 + varc); if (strtol(num, NULL, 10) >= size) @@ -136,10 +136,10 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // memset, memcmp, memcpy, strncpy, fgets.. if ( varid > 0 ) { - if ( TOKEN::Match(tok, "memset|memcpy|memmove|memcmp|strncpy|fgets") ) + if ( Token::Match(tok, "memset|memcpy|memmove|memcmp|strncpy|fgets") ) { - if ( TOKEN::Match(tok->next(), "( %varid% , %num% , %num% )", 0, varid) || - TOKEN::Match(tok->next(), "( %var% , %varid% , %num% )", 0, varid) ) + if ( Token::Match(tok->next(), "( %varid% , %num% , %num% )", 0, varid) || + Token::Match(tok->next(), "( %var% , %varid% , %num% )", 0, varid) ) { const char *num = tok->strAt(6); if ( atoi(num) > total_size ) @@ -150,10 +150,10 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co continue; } } - else if (TOKEN::Match(tok,"memset|memcpy|memmove|memcmp|strncpy|fgets") ) + else if (Token::Match(tok,"memset|memcpy|memmove|memcmp|strncpy|fgets") ) { - if ( TOKEN::Match(tok->next(), "( %var1% , %num% , %num% )", varname) || - TOKEN::Match(tok->next(), "( %var% , %var1% , %num% )", varname) ) + if ( Token::Match(tok->next(), "( %var1% , %num% , %num% )", varname) || + Token::Match(tok->next(), "( %var% , %var1% , %num% )", varname) ) { const char *num = tok->strAt(varc + 6); if ( atoi(num) > total_size ) @@ -166,22 +166,22 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // Loop.. - if ( TOKEN::simpleMatch(tok, "for (") ) + if ( Token::simpleMatch(tok, "for (") ) { - const TOKEN *tok2 = tok->tokAt(2); + const Token *tok2 = tok->tokAt(2); // for - setup.. - if ( TOKEN::Match(tok2, "%var% = 0 ;") ) + if ( Token::Match(tok2, "%var% = 0 ;") ) tok2 = tok2->tokAt(4); - else if ( TOKEN::Match(tok2, "%type% %var% = 0 ;") ) + else if ( Token::Match(tok2, "%type% %var% = 0 ;") ) tok2 = tok2->tokAt(5); - else if ( TOKEN::Match(tok2, "%type% %type% %var% = 0 ;") ) + else if ( Token::Match(tok2, "%type% %type% %var% = 0 ;") ) tok2 = tok2->tokAt(6); else continue; // for - condition.. - if ( !TOKEN::Match(tok2, "%var% < %num% ;") && !TOKEN::Match(tok2, "%var% <= %num% ;")) + if ( !Token::Match(tok2, "%var% < %num% ;") && !Token::Match(tok2, "%var% <= %num% ;")) continue; // Get index variable and stopsize. @@ -215,7 +215,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co break; } - if ( TOKEN::Match(tok2, pattern.str().c_str(), varname) ) + if ( Token::Match(tok2, pattern.str().c_str(), varname) ) { ReportError(tok2, "Buffer overrun"); break; @@ -227,7 +227,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // Writing data into array.. - if ( TOKEN::Match(tok, "strcpy ( %var1% , %str% )", varname) ) + if ( Token::Match(tok, "strcpy ( %var1% , %str% )", varname) ) { int len = 0; const char *str = tok->strAt(varc + 4 ); @@ -249,7 +249,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co // Function call.. // It's not interesting to check what happens when the whole struct is // sent as the parameter, that is checked separately anyway. - if ( TOKEN::Match(tok, "%var% (") ) + if ( Token::Match(tok, "%var% (") ) { // Don't make recursive checking.. if (std::find(_callStack.begin(), _callStack.end(), tok) != _callStack.end()) @@ -260,7 +260,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co continue; unsigned int parlevel = 0, par = 0; - for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == "(" ) { @@ -282,7 +282,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co ++par; } - if ( parlevel == 1 && TOKEN::Match(tok2, "[(,] %var1% [,)]", varname) ) + if ( parlevel == 1 && Token::Match(tok2, "[(,] %var1% [,)]", varname) ) { ++par; break; @@ -293,7 +293,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co continue; // Find function.. - const TOKEN *ftok = _tokenizer->GetFunctionTokenByName(tok->aaaa()); + const Token *ftok = _tokenizer->GetFunctionTokenByName(tok->aaaa()); if ( !ftok ) continue; @@ -311,7 +311,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co else if ( ftok->str() == "," ) --par; - else if ( par==1 && parlevel==1 && TOKEN::Match(ftok, "%var% [,)]") ) + else if ( par==1 && parlevel==1 && Token::Match(ftok, "%var% [,)]") ) { // Parameter name.. const char *parname[2]; @@ -346,7 +346,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const TOKEN *tok, co void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable() { int indentlevel = 0; - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (tok->str() == "{") ++indentlevel; @@ -362,7 +362,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable() unsigned int varid = 0; int nextTok = 0; - if (TOKEN::Match(tok, "%type% %var% [ %num% ] ;")) + if (Token::Match(tok, "%type% %var% [ %num% ] ;")) { varname[0] = tok->strAt(1); size = strtoul(tok->strAt(3), NULL, 10); @@ -370,7 +370,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable() varid = tok->tokAt(1)->varId(); nextTok = 6; } - else if (TOKEN::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) + else if (Token::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) { varname[0] = tok->strAt(1); size = strtoul(tok->strAt(6), NULL, 10); @@ -403,25 +403,25 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable() void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable() { const char declstruct[] = "struct|class %var% {"; - for ( const TOKEN *tok = TOKEN::findmatch(_tokenizer->tokens(), declstruct); - tok; tok = TOKEN::findmatch(tok->next(), declstruct) ) + for ( const Token *tok = Token::findmatch(_tokenizer->tokens(), declstruct); + tok; tok = Token::findmatch(tok->next(), declstruct) ) { const std::string &structname = tok->next()->str(); // Found a struct declaration. Search for arrays.. - for ( const TOKEN *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) { if ( tok2->str() == "}" ) break; int ivar = 0; - if ( TOKEN::Match(tok2->next(), "%type% %var% [ %num% ] ;") ) + if ( Token::Match(tok2->next(), "%type% %var% [ %num% ] ;") ) ivar = 2; - else if ( TOKEN::Match(tok2->next(), "%type% %type% %var% [ %num% ] ;") ) + else if ( Token::Match(tok2->next(), "%type% %type% %var% [ %num% ] ;") ) ivar = 3; - else if ( TOKEN::Match(tok2->next(), "%type% * %var% [ %num% ] ;") ) + else if ( Token::Match(tok2->next(), "%type% * %var% [ %num% ] ;") ) ivar = 3; - else if ( TOKEN::Match(tok2->next(), "%type% %type% * %var% [ %num% ] ;") ) + else if ( Token::Match(tok2->next(), "%type% %type% * %var% [ %num% ] ;") ) ivar = 4; else continue; @@ -438,36 +438,36 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable() if ( tok->str() == "class" ) { std::string func_pattern(structname + " :: %var% ("); - const TOKEN *tok3 = TOKEN::findmatch(_tokenizer->tokens(), func_pattern.c_str()); + const Token *tok3 = Token::findmatch(_tokenizer->tokens(), func_pattern.c_str()); while ( tok3 ) { - for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next() ) + for ( const Token *tok4 = tok3; tok4; tok4 = tok4->next() ) { - if ( TOKEN::Match(tok4, "[;{}]") ) + if ( Token::Match(tok4, "[;{}]") ) break; - if ( TOKEN::simpleMatch(tok4, ") {") ) + if ( Token::simpleMatch(tok4, ") {") ) { const char *names[2] = {varname[1], 0}; CheckBufferOverrun_CheckScope(tok4->tokAt(2), names, arrsize, total_size, 0); break; } } - tok3 = TOKEN::findmatch(tok3->next(), func_pattern.c_str()); + tok3 = Token::findmatch(tok3->next(), func_pattern.c_str()); } } - for ( const TOKEN *tok3 = _tokenizer->tokens(); tok3; tok3 = tok3->next() ) + for ( const Token *tok3 = _tokenizer->tokens(); tok3; tok3 = tok3->next() ) { if ( tok3->str() != structname ) continue; // Declare variable: Fred fred1; - if ( TOKEN::Match( tok3->next(), "%var% ;" ) ) + if ( Token::Match( tok3->next(), "%var% ;" ) ) varname[0] = tok3->strAt(1); // Declare pointer: Fred *fred1 - else if ( TOKEN::Match(tok3->next(), "* %var% [,);=]") ) + else if ( Token::Match(tok3->next(), "* %var% [,);=]") ) varname[0] = tok3->strAt(2); else @@ -475,7 +475,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable() // Goto end of statement. - const TOKEN *CheckTok = NULL; + const Token *CheckTok = NULL; while ( tok3 ) { // End of statement. @@ -486,11 +486,11 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable() } // End of function declaration.. - if ( TOKEN::simpleMatch(tok3, ") ;") ) + if ( Token::simpleMatch(tok3, ") ;") ) break; // Function implementation.. - if ( TOKEN::simpleMatch(tok3, ") {") ) + if ( Token::simpleMatch(tok3, ") {") ) { CheckTok = tok3->tokAt(2); break; @@ -535,9 +535,9 @@ void CheckBufferOverrunClass::bufferOverrun() void CheckBufferOverrunClass::dangerousFunctions() { - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (TOKEN::Match(tok, "gets|scanf (")) + if (Token::Match(tok, "gets|scanf (")) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Found '" << tok->str() << "'. You should use 'fgets' instead"; diff --git a/checkbufferoverrun.h b/checkbufferoverrun.h index 97878a6b3..8accada51 100644 --- a/checkbufferoverrun.h +++ b/checkbufferoverrun.h @@ -40,13 +40,13 @@ public: private: void CheckBufferOverrun_StructVariable(); void CheckBufferOverrun_LocalVariable(); - void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size, unsigned int varid ); - void ReportError(const TOKEN *tok, const char errmsg[]); + void CheckBufferOverrun_CheckScope( const Token *tok, const char *varname[], const int size, const int total_size, unsigned int varid ); + void ReportError(const Token *tok, const char errmsg[]); const Tokenizer *_tokenizer; const Settings _settings; ErrorLogger *_errorLogger; - std::list _callStack; + std::list _callStack; }; //--------------------------------------------------------------------------- diff --git a/checkclass.cpp b/checkclass.cpp index af17a9d74..f8ba0da58 100644 --- a/checkclass.cpp +++ b/checkclass.cpp @@ -47,12 +47,12 @@ CheckClass::~CheckClass() //--------------------------------------------------------------------------- -struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1) +struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1) { // Get variable list.. struct VAR *varlist = NULL; unsigned int indentlevel = 0; - for (const TOKEN *tok = tok1; tok; tok = tok->next()) + for (const Token *tok = tok1; tok; tok = tok->next()) { if (!tok->next()) break; @@ -73,22 +73,22 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1) bool b = bool((*tok->strAt(0) != ':') && strchr(tok->strAt(0), ':') != 0); // Search for start of statement.. - if ( ! TOKEN::Match(tok, "[;{}]") && ! b ) + if ( ! Token::Match(tok, "[;{}]") && ! b ) continue; // This is the start of a statement - const TOKEN *next = tok->next(); + const Token *next = tok->next(); const char *varname = 0; // Is it a variable declaration? - if ( TOKEN::Match(next,"%type% %var% ;") ) + if ( Token::Match(next,"%type% %var% ;") ) { if ( next->isStandardType() ) varname = next->strAt(1); } // Pointer? - else if ( TOKEN::Match(next, "%type% * %var% ;") ) + else if ( Token::Match(next, "%type% * %var% ;") ) { varname = next->strAt(2); } @@ -109,7 +109,7 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1) } //--------------------------------------------------------------------------- -const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ) +const Token * CheckClass::FindClassFunction( const Token *tok, const char classname[], const char funcname[], int &indentlevel ) { if ( indentlevel < 0 || tok == NULL ) return NULL; @@ -125,7 +125,7 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn for ( ;tok; tok = tok->next() ) { - if ( indentlevel == 0 && TOKEN::Match(tok, classPattern.str().c_str()) ) + if ( indentlevel == 0 && Token::Match(tok, classPattern.str().c_str()) ) { while ( tok && tok->str() != "{" ) tok = tok->next(); @@ -172,9 +172,9 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn if ( indentlevel == 1 ) { // Member function implemented in the class declaration? - if (tok->str()!="~" && TOKEN::Match(tok->next(), internalPattern.str().c_str())) + if (tok->str()!="~" && Token::Match(tok->next(), internalPattern.str().c_str())) { - const TOKEN *tok2 = tok; + const Token *tok2 = tok; while ( tok2 && tok2->str() != "{" && tok2->str() != ";" ) tok2 = tok2->next(); if ( tok2 && tok2->str() == "{" ) @@ -182,7 +182,7 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn } } - else if ( indentlevel == 0 && TOKEN::Match(tok, externalPattern.str().c_str()) ) + else if ( indentlevel == 0 && Token::Match(tok, externalPattern.str().c_str()) ) { return tok; } @@ -206,7 +206,7 @@ void CheckClass::InitVar(struct VAR *varlist, const char varname[]) } //--------------------------------------------------------------------------- -void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list &callstack) +void CheckClass::ClassChecking_VarList_Initialize(const Token *tok1, const Token *ftok, struct VAR *varlist, const char classname[], std::list &callstack) { bool Assign = false; unsigned int indentlevel = 0; @@ -220,7 +220,7 @@ void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN // clKalle::clKalle() : var(value) { } if (indentlevel==0) { - if (Assign && TOKEN::Match(ftok, "%var% (")) + if (Assign && Token::Match(ftok, "%var% (")) { InitVar( varlist, ftok->aaaa() ); } @@ -246,29 +246,29 @@ void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN continue; // Before a new statement there is "[{};)=]" or "else" - if ( ! TOKEN::Match(ftok, "[{};)=]") && ftok->str() != "else" ) + if ( ! Token::Match(ftok, "[{};)=]") && ftok->str() != "else" ) continue; // Using the operator= function to initialize all variables.. - if ( TOKEN::simpleMatch(ftok->next(), "* this = ") ) + if ( Token::simpleMatch(ftok->next(), "* this = ") ) { for (struct VAR *var = varlist; var; var = var->next) var->init = true; break; } - if (!TOKEN::Match(ftok->next(), "%var%") && !TOKEN::Match(ftok->next(), "this . %var%")) + if (!Token::Match(ftok->next(), "%var%") && !Token::Match(ftok->next(), "this . %var%")) continue; // Goto the first token in this statement.. ftok = ftok->next(); // Skip "this->" - if ( TOKEN::simpleMatch(ftok, "this .") ) + if ( Token::simpleMatch(ftok, "this .") ) ftok = ftok->tokAt(2); // Clearing all variables.. - if (TOKEN::simpleMatch(ftok,"memset ( this ,")) + if (Token::simpleMatch(ftok,"memset ( this ,")) { for (struct VAR *var = varlist; var; var = var->next) var->init = true; @@ -276,26 +276,26 @@ void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN } // Calling member function? - else if (TOKEN::Match(ftok, "%var% (")) + else if (Token::Match(ftok, "%var% (")) { // No recursive calls! if ( std::find(callstack.begin(),callstack.end(),ftok->str()) == callstack.end() ) { callstack.push_back( ftok->str() ); int i = 0; - const TOKEN *ftok2 = FindClassFunction( tok1, classname, ftok->aaaa(), i ); + const Token *ftok2 = FindClassFunction( tok1, classname, ftok->aaaa(), i ); ClassChecking_VarList_Initialize(tok1, ftok2, varlist, classname, callstack); } } // Assignment of member variable? - else if (TOKEN::Match(ftok, "%var% =")) + else if (Token::Match(ftok, "%var% =")) { InitVar( varlist, ftok->aaaa() ); } // The functions 'clear' and 'Clear' are supposed to initialize variable. - if (TOKEN::Match(ftok,"%var% . clear|Clear (")) + if (Token::Match(ftok,"%var% . clear|Clear (")) { InitVar( varlist, ftok->aaaa() ); } @@ -316,7 +316,7 @@ void CheckClass::constructors() const char pattern_class[] = "class %var% {"; // Locate class - const TOKEN *tok1 = TOKEN::findmatch( _tokenizer->tokens(), pattern_class ); + const Token *tok1 = Token::findmatch( _tokenizer->tokens(), pattern_class ); while (tok1) { const char *className[2]; @@ -328,7 +328,7 @@ void CheckClass::constructors() { int indentlevel = 0; bool isPrivate = true; - for ( const TOKEN *tok = tok1; tok; tok = tok->next() ) + for ( const Token *tok = tok1; tok; tok = tok->next() ) { // Indentation if ( tok->str() == "{" ) @@ -351,7 +351,7 @@ void CheckClass::constructors() isPrivate = false; // Is there a private constructor? - else if ( isPrivate && TOKEN::Match(tok, "%var1% (", className) ) + else if ( isPrivate && Token::Match(tok, "%var1% (", className) ) { hasPrivateConstructor = true; break; @@ -364,14 +364,14 @@ void CheckClass::constructors() { // TODO: Handle private constructors. // Right now to avoid false positives I just bail out - tok1 = TOKEN::findmatch( tok1->next(), pattern_class ); + tok1 = Token::findmatch( tok1->next(), pattern_class ); continue; } // Are there a class constructor? - const TOKEN *constructor_token = TOKEN::findmatch( tok1, "%any% %var1% (", className ); - while ( TOKEN::Match( constructor_token, "~" ) ) - constructor_token = TOKEN::findmatch( constructor_token->next(), "%any% %var1% (", className ); + const Token *constructor_token = Token::findmatch( tok1, "%any% %var1% (", className ); + while ( Token::Match( constructor_token, "~" ) ) + constructor_token = Token::findmatch( constructor_token->next(), "%any% %var1% (", className ); // There are no constructor. if ( ! constructor_token ) @@ -397,7 +397,7 @@ void CheckClass::constructors() } } - tok1 = TOKEN::findmatch( tok1->next(), pattern_class ); + tok1 = Token::findmatch( tok1->next(), pattern_class ); continue; } @@ -418,16 +418,16 @@ void CheckClass::constructors() varlist = nextvar; } - tok1 = TOKEN::findmatch( tok1->next(), pattern_class ); + tok1 = Token::findmatch( tok1->next(), pattern_class ); } } -void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const char funcname[]) +void CheckClass::CheckConstructors(const Token *tok1, struct VAR *varlist, const char funcname[]) { const char * const className = tok1->strAt(1); int indentlevel = 0; - const TOKEN *constructor_token = FindClassFunction( tok1, className, funcname, indentlevel ); + const Token *constructor_token = FindClassFunction( tok1, className, funcname, indentlevel ); std::list callstack; ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, className, callstack); while ( constructor_token ) @@ -441,7 +441,7 @@ void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const // Is it a static member variable? std::ostringstream pattern; pattern << className << "::" << var->name << "="; - if (TOKEN::findmatch(_tokenizer->tokens(), pattern.str().c_str())) + if (Token::findmatch(_tokenizer->tokens(), pattern.str().c_str())) continue; // It's non-static and it's not initialized => error @@ -468,13 +468,13 @@ void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const void CheckClass::privateFunctions() { // Locate some class - for (const TOKEN *tok1 = TOKEN::findmatch(_tokenizer->tokens(), "class %var% {"); tok1; tok1 = TOKEN::findmatch(tok1->next(), "class %var% {")) + for (const Token *tok1 = Token::findmatch(_tokenizer->tokens(), "class %var% {"); tok1; tok1 = Token::findmatch(tok1->next(), "class %var% {")) { const std::string &classname = tok1->next()->str(); // The class implementation must be available.. const std::string classconstructor(classname + " :: " + classname); - if (!TOKEN::findmatch(_tokenizer->tokens(), classconstructor.c_str())) + if (!Token::findmatch(_tokenizer->tokens(), classconstructor.c_str())) continue; // Get private functions.. @@ -482,9 +482,9 @@ void CheckClass::privateFunctions() FuncList.clear(); bool priv = false; unsigned int indent_level = 0; - for (const TOKEN *tok = tok1; tok; tok = tok->next()) + for (const Token *tok = tok1; tok; tok = tok->next()) { - if (TOKEN::Match(tok,"friend %var%")) + if (Token::Match(tok,"friend %var%")) { // Todo: Handle friend classes FuncList.clear(); @@ -507,11 +507,11 @@ void CheckClass::privateFunctions() priv = false; else if (priv && indent_level == 1) { - if ( TOKEN::Match(tok, "typedef %type% (") ) + if ( Token::Match(tok, "typedef %type% (") ) tok = tok->tokAt(2); - if (TOKEN::Match(tok, "%var% (") && - !TOKEN::Match(tok,classname.c_str())) + if (Token::Match(tok, "%var% (") && + !Token::Match(tok,classname.c_str())) { FuncList.push_back(tok->str()); } @@ -521,12 +521,12 @@ void CheckClass::privateFunctions() // Check that all private functions are used.. const std::string pattern_function(classname + " ::"); bool HasFuncImpl = false; - const TOKEN *ftok = _tokenizer->tokens(); + const Token *ftok = _tokenizer->tokens(); while (ftok) { - ftok = TOKEN::findmatch(ftok,pattern_function.c_str()); + ftok = Token::findmatch(ftok,pattern_function.c_str()); int numpar = 0; - while (ftok && !TOKEN::Match(ftok, "[;{]")) + while (ftok && !Token::Match(ftok, "[;{]")) { if (ftok->str() == "(") ++numpar; @@ -553,7 +553,7 @@ void CheckClass::privateFunctions() break; --indent_level; } - if (TOKEN::Match( ftok->next(), "(") ) + if (Token::Match( ftok->next(), "(") ) FuncList.remove(ftok->str()); ftok = ftok->next(); } @@ -567,7 +567,7 @@ void CheckClass::privateFunctions() { // Final check; check if the function pointer is used somewhere.. const std::string _pattern("return|(|)|,|= " + FuncList.front()); - if (!TOKEN::findmatch(_tokenizer->tokens(), _pattern.c_str())) + if (!Token::findmatch(_tokenizer->tokens(), _pattern.c_str())) { std::ostringstream ostr; ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'"; @@ -585,22 +585,22 @@ void CheckClass::privateFunctions() void CheckClass::noMemset() { // Locate all 'memset' tokens.. - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (!TOKEN::Match(tok,"memset|memcpy|memmove")) + if (!Token::Match(tok,"memset|memcpy|memmove")) continue; // Todo: Handle memcpy and memmove const char *type = NULL; - if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )")) + if (Token::Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )")) type = tok->strAt(8); - else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( %type% ) )")) + else if (Token::Match(tok, "memset ( & %var% , %num% , sizeof ( %type% ) )")) type = tok->strAt(9); - else if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( struct %type% ) )")) + else if (Token::Match(tok, "memset ( %var% , %num% , sizeof ( struct %type% ) )")) type = tok->strAt(9); - else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( struct %type% ) )")) + else if (Token::Match(tok, "memset ( & %var% , %num% , sizeof ( struct %type% ) )")) type = tok->strAt(10); - else if (TOKEN::Match(tok, "%type% ( %var% , %var% , sizeof ( %type% ) )")) + else if (Token::Match(tok, "%type% ( %var% , %var% , sizeof ( %type% ) )")) type = tok->strAt(8); // No type defined => The tokens didn't match @@ -609,7 +609,7 @@ void CheckClass::noMemset() // Warn if type is a class.. const std::string pattern1(std::string("class ") + type); - if (TOKEN::findmatch(_tokenizer->tokens(),pattern1.c_str())) + if (Token::findmatch(_tokenizer->tokens(),pattern1.c_str())) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str() << "' on class."; @@ -619,12 +619,12 @@ void CheckClass::noMemset() // Warn if type is a struct that contains any std::* const std::string pattern2(std::string("struct ") + type); - for (const TOKEN *tstruct = TOKEN::findmatch(_tokenizer->tokens(), pattern2.c_str()); tstruct; tstruct = tstruct->next()) + for (const Token *tstruct = Token::findmatch(_tokenizer->tokens(), pattern2.c_str()); tstruct; tstruct = tstruct->next()) { if (tstruct->str() == "}") break; - if (TOKEN::Match(tstruct, "std :: %type% %var% ;")) + if (Token::Match(tstruct, "std :: %type% %var% ;")) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str() << "' on struct that contains a 'std::" << tstruct->strAt(2) << "'"; @@ -645,7 +645,7 @@ void CheckClass::noMemset() void CheckClass::operatorEq() { - if (const TOKEN *tok = TOKEN::findmatch(_tokenizer->tokens(), "void operator = (")) + if (const Token *tok = Token::findmatch(_tokenizer->tokens(), "void operator = (")) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": 'operator=' should return something"; @@ -665,14 +665,14 @@ void CheckClass::virtualDestructor() { const char pattern_classdecl[] = "class %var% : %var%"; - const TOKEN *derived = _tokenizer->tokens(); - while ((derived = TOKEN::findmatch(derived, pattern_classdecl)) != NULL) + const Token *derived = _tokenizer->tokens(); + while ((derived = Token::findmatch(derived, pattern_classdecl)) != NULL) { // Check that the derived class has a non empty destructor.. { std::ostringstream destructorPattern; destructorPattern << "~ " << derived->strAt(1) << " ( ) {"; - const TOKEN *derived_destructor = TOKEN::findmatch( _tokenizer->tokens(), destructorPattern.str().c_str() ); + const Token *derived_destructor = Token::findmatch( _tokenizer->tokens(), destructorPattern.str().c_str() ); // No destructor.. if ( ! derived_destructor ) @@ -682,23 +682,23 @@ void CheckClass::virtualDestructor() } // Empty destructor.. - if ( TOKEN::Match(derived_destructor, "~ %var% ( ) { }") ) + if ( Token::Match(derived_destructor, "~ %var% ( ) { }") ) { derived = derived->next(); continue; } } - const TOKEN *derivedClass = derived->tokAt(1); + const Token *derivedClass = derived->tokAt(1); // Iterate through each base class... derived = derived->tokAt(3); - while ( TOKEN::Match(derived, "%var%") ) + while ( Token::Match(derived, "%var%") ) { - bool isPublic = TOKEN::Match(derived, "public"); + bool isPublic = Token::Match(derived, "public"); // What kind of inheritance is it.. public|protected|private - if ( TOKEN::Match( derived, "public|protected|private" ) ) + if ( Token::Match( derived, "public|protected|private" ) ) derived = derived->next(); // Name of base class.. @@ -708,7 +708,7 @@ void CheckClass::virtualDestructor() // Update derived so it's ready for the next loop. derived = derived->next(); - if ( TOKEN::Match(derived, ",") ) + if ( Token::Match(derived, ",") ) derived = derived->next(); // If not public inheritance, skip checking of this base class.. @@ -716,18 +716,18 @@ void CheckClass::virtualDestructor() continue; // Find the destructor declaration for the base class. - const TOKEN *base = TOKEN::findmatch(_tokenizer->tokens(), "%any% ~ %var1% (", baseName); - while (TOKEN::Match(base, "::")) - base = TOKEN::findmatch(base->next(), "%any% ~ %var1% (", baseName); + const Token *base = Token::findmatch(_tokenizer->tokens(), "%any% ~ %var1% (", baseName); + while (Token::Match(base, "::")) + base = Token::findmatch(base->next(), "%any% ~ %var1% (", baseName); - while (TOKEN::Match(base, "%var%") && !TOKEN::Match(base, "virtual")) + while (Token::Match(base, "%var%") && !Token::Match(base, "virtual")) base = base->previous(); // Check that there is a destructor.. if ( ! base ) { // Is the class declaration available? - base = TOKEN::findmatch(_tokenizer->tokens(), "class %var1% :|{", baseName); + base = Token::findmatch(_tokenizer->tokens(), "class %var1% :|{", baseName); if ( base ) { std::ostringstream errmsg; diff --git a/checkclass.h b/checkclass.h index 016223388..2c8378869 100644 --- a/checkclass.h +++ b/checkclass.h @@ -50,13 +50,13 @@ private: struct VAR *next; }; - void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list &callstack); + 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 Token *FindClassFunction( const Token *tok, const char classname[], const char funcname[], int &indentlevel ); + struct VAR *ClassChecking_GetVarList(const Token *tok1); // Check constructors for a specified class - void CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const char funcname[]); + void CheckConstructors(const Token *tok1, struct VAR *varlist, const char funcname[]); const Tokenizer *_tokenizer; Settings _settings; diff --git a/checkfunctionusage.cpp b/checkfunctionusage.cpp index e4499a3cb..da28bc3f7 100644 --- a/checkfunctionusage.cpp +++ b/checkfunctionusage.cpp @@ -48,26 +48,26 @@ void CheckFunctionUsage::setErrorLogger( ErrorLogger *errorLogger ) void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer ) { // Function declarations.. - for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokenizer.tokens(); tok; tok = tok->next() ) { if ( tok->fileIndex() != 0 ) continue; - const TOKEN *funcname = 0; + const Token *funcname = 0; - if ( TOKEN::Match( tok, "%type% %var% (" ) ) + if ( Token::Match( tok, "%type% %var% (" ) ) funcname = tok->tokAt(1); - else if ( TOKEN::Match(tok, "%type% * %var% (") ) + else if ( Token::Match(tok, "%type% * %var% (") ) funcname = tok->tokAt(2); - else if ( TOKEN::Match(tok, "%type% :: %var% (") && !TOKEN::Match(tok, tok->strAt(2)) ) + else if ( Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2)) ) funcname = tok->tokAt(2); // Check that ") {" is found.. - for (const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next()) + for (const Token *tok2 = funcname; tok2; tok2 = tok2->next()) { - if ( TOKEN::Match(tok2, ")") ) + if ( Token::Match(tok2, ")") ) { - if ( ! TOKEN::Match(tok2, ") {") && ! TOKEN::Match(tok2, ") const {") ) + if ( ! Token::Match(tok2, ") {") && ! Token::Match(tok2, ") const {") ) funcname = NULL; break; } @@ -91,25 +91,25 @@ void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer ) } // Function usage.. - for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokenizer.tokens(); tok; tok = tok->next() ) { - const TOKEN *funcname = 0; + const Token *funcname = 0; - if ( TOKEN::Match( tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]" ) || - TOKEN::Match(tok, ":: %var% (") || - TOKEN::Match(tok, "|= %var% (") || - TOKEN::Match(tok, "&= %var% (") || - TOKEN::Match(tok, "&& %var% (") || - TOKEN::Match(tok, "|| %var% (") || - TOKEN::Match(tok, "else %var% (") || - TOKEN::Match(tok, "return %var% (") ) + if ( Token::Match( tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]" ) || + Token::Match(tok, ":: %var% (") || + Token::Match(tok, "|= %var% (") || + Token::Match(tok, "&= %var% (") || + Token::Match(tok, "&& %var% (") || + Token::Match(tok, "|| %var% (") || + Token::Match(tok, "else %var% (") || + Token::Match(tok, "return %var% (") ) funcname = tok->next(); // funcname ( => Assert that the end paranthesis isn't followed by { - if ( TOKEN::Match(funcname, "%var% (") ) + if ( Token::Match(funcname, "%var% (") ) { int parlevel = 0; - for ( const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = funcname; tok2; tok2 = tok2->next() ) { if (tok2->str() == "(") ++parlevel; @@ -117,7 +117,7 @@ void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer ) else if (tok2->str() == ")") { --parlevel; - if (parlevel == 0 && (TOKEN::Match(tok2, ") {") || TOKEN::Match(tok2, ") const"))) + if (parlevel == 0 && (Token::Match(tok2, ") {") || Token::Match(tok2, ") const"))) funcname = NULL; if ( parlevel <= 0 ) break; diff --git a/checkheaders.cpp b/checkheaders.cpp index 81de1d7d9..82da6f629 100644 --- a/checkheaders.cpp +++ b/checkheaders.cpp @@ -48,13 +48,13 @@ CheckHeaders::~CheckHeaders() void CheckHeaders::WarningHeaderWithImplementation() { - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Only interested in included file if (tok->fileIndex() == 0) continue; - if (TOKEN::Match(tok, ") {")) + if (Token::Match(tok, ") {")) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Found implementation in header"; @@ -83,7 +83,7 @@ void CheckHeaders::WarningHeaderWithImplementation() void CheckHeaders::WarningIncludeHeader() { // Including.. - for ( const TOKEN *includetok = _tokenizer->tokens(); includetok; includetok = includetok->next()) + for ( const Token *includetok = _tokenizer->tokens(); includetok; includetok = includetok->next()) { if (includetok->str() != "#include") continue; @@ -112,7 +112,7 @@ void CheckHeaders::WarningIncludeHeader() // Extract classes and names in the header.. int indentlevel = 0; - for ( const TOKEN *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next() ) + for ( const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next() ) { if ( tok1->fileIndex() != hfile ) continue; @@ -129,21 +129,21 @@ void CheckHeaders::WarningIncludeHeader() // Class or namespace declaration.. // -------------------------------------- - if (TOKEN::Match(tok1,"class %var% {") || TOKEN::Match(tok1,"class %var% :") || TOKEN::Match(tok1,"namespace %var% {")) + if (Token::Match(tok1,"class %var% {") || Token::Match(tok1,"class %var% :") || Token::Match(tok1,"namespace %var% {")) classlist.push_back(tok1->strAt(1)); // Variable declaration.. // -------------------------------------- - else if (TOKEN::Match(tok1, "%type% %var% ;") || TOKEN::Match(tok1, "%type% %var% [")) + else if (Token::Match(tok1, "%type% %var% ;") || Token::Match(tok1, "%type% %var% [")) namelist.push_back(tok1->strAt(1)); - else if (TOKEN::Match(tok1, "%type% * %var% ;") || TOKEN::Match(tok1, "%type% * %var% [")) + else if (Token::Match(tok1, "%type% * %var% ;") || Token::Match(tok1, "%type% * %var% [")) namelist.push_back(tok1->strAt(2)); - else if (TOKEN::Match(tok1, "const %type% %var% =") || TOKEN::Match(tok1, "const %type% %var% [")) + else if (Token::Match(tok1, "const %type% %var% =") || Token::Match(tok1, "const %type% %var% [")) namelist.push_back(tok1->strAt(2)); - else if (TOKEN::Match(tok1, "const %type% * %var% =") || TOKEN::Match(tok1, "const %type% * %var% [")) + else if (Token::Match(tok1, "const %type% * %var% =") || Token::Match(tok1, "const %type% * %var% [")) namelist.push_back(tok1->strAt(3)); // enum.. @@ -151,7 +151,7 @@ void CheckHeaders::WarningIncludeHeader() else if (tok1->str() == "enum") { tok1 = tok1->next(); - while ( ! TOKEN::Match( tok1, "; %any%" ) ) + while ( ! Token::Match( tok1, "; %any%" ) ) { if ( tok1->isName() ) namelist.push_back(tok1->str()); @@ -161,16 +161,16 @@ void CheckHeaders::WarningIncludeHeader() // function.. // -------------------------------------- - else if (TOKEN::Match(tok1,"%type% %var% (")) + else if (Token::Match(tok1,"%type% %var% (")) namelist.push_back(tok1->strAt(1)); - else if (TOKEN::Match(tok1,"%type% * %var% (")) + else if (Token::Match(tok1,"%type% * %var% (")) namelist.push_back(tok1->strAt(2)); - else if (TOKEN::Match(tok1,"const %type% %var% (")) + else if (Token::Match(tok1,"const %type% %var% (")) namelist.push_back(tok1->strAt(2)); - else if (TOKEN::Match(tok1,"const %type% * %var% (")) + else if (Token::Match(tok1,"const %type% * %var% (")) namelist.push_back(tok1->strAt(3)); // typedef.. @@ -182,10 +182,10 @@ void CheckHeaders::WarningIncludeHeader() int parlevel = 0; while (tok1->next()) { - if ( TOKEN::Match(tok1, "[({]") ) + if ( Token::Match(tok1, "[({]") ) ++parlevel; - else if ( TOKEN::Match(tok1, "[)}]") ) + else if ( Token::Match(tok1, "[)}]") ) --parlevel; else if (parlevel == 0) @@ -193,7 +193,7 @@ void CheckHeaders::WarningIncludeHeader() if ( tok1->str() == ";" ) break; - if ( TOKEN::Match(tok1, "%var% ;") ) + if ( Token::Match(tok1, "%var% ;") ) namelist.push_back(tok1->str()); } @@ -206,12 +206,12 @@ void CheckHeaders::WarningIncludeHeader() // Check if the extracted names are used... bool Needed = false; bool NeedDeclaration = false; - for ( const TOKEN *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) + for ( const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) { if (tok1->fileIndex() != includetok->fileIndex()) continue; - if ( TOKEN::Match(tok1, ": %var% {") || TOKEN::Match(tok1, ": %type% %var% {") ) + if ( Token::Match(tok1, ": %var% {") || Token::Match(tok1, ": %type% %var% {") ) { std::string classname = tok1->strAt((strcmp(tok1->strAt(2),"{")) ? 2 : 1); if (std::find(classlist.begin(),classlist.end(),classname)!=classlist.end()) diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index 58ba97cd2..0a1493e98 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -47,21 +47,21 @@ CheckMemoryLeakClass::~CheckMemoryLeakClass() } -bool CheckMemoryLeakClass::isclass( const TOKEN *tok ) +bool CheckMemoryLeakClass::isclass( const Token *tok ) { if ( tok->isStandardType() ) return false; std::ostringstream pattern; pattern << "struct " << tok->str(); - if ( TOKEN::findmatch( _tokenizer->tokens(), pattern.str().c_str() ) ) + if ( Token::findmatch( _tokenizer->tokens(), pattern.str().c_str() ) ) return false; return true; } //--------------------------------------------------------------------------- -CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const TOKEN *tok2 ) +CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const Token *tok2 ) { // What we may have... // * var = (char *)malloc(10); @@ -110,16 +110,16 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T return gMalloc; } - if ( TOKEN::Match( tok2, "new %type% [;(]" ) ) + if ( Token::Match( tok2, "new %type% [;(]" ) ) return New; - if ( TOKEN::Match( tok2, "new %type% [" ) ) + if ( Token::Match( tok2, "new %type% [" ) ) return NewA; - if ( TOKEN::Match( tok2, "fopen (" ) ) + if ( Token::Match( tok2, "fopen (" ) ) return FOPEN; - if ( TOKEN::Match( tok2, "popen (" ) ) + if ( Token::Match( tok2, "popen (" ) ) return POPEN; // Userdefined allocation function.. @@ -136,7 +136,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T -CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const TOKEN *tok2 ) +CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const Token *tok2 ) { // What we may have... // * var = (char *)realloc(..; @@ -149,64 +149,64 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const if ( ! tok2 ) return No; - if ( TOKEN::Match(tok2, "realloc") ) + if ( Token::Match(tok2, "realloc") ) return Malloc; // GTK memory reallocation.. - if ( TOKEN::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew") ) + if ( Token::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew") ) return gMalloc; return No; } -CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] ) +CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const Token *tok, const char *varnames[] ) { - if ( TOKEN::Match(tok, "delete %var1% ;", varnames) ) + if ( Token::Match(tok, "delete %var1% ;", varnames) ) return New; - if ( TOKEN::Match(tok, "delete [ ] %var1% ;", varnames) ) + if ( Token::Match(tok, "delete [ ] %var1% ;", varnames) ) return NewA; - if ( TOKEN::Match(tok, "free ( %var1% ) ;", varnames) || - TOKEN::Match(tok, "kfree ( %var1% ) ;", varnames) ) + if ( Token::Match(tok, "free ( %var1% ) ;", varnames) || + Token::Match(tok, "kfree ( %var1% ) ;", varnames) ) { return Malloc; } - if ( TOKEN::Match(tok, "g_free ( %var1% ) ;", varnames) ) + if ( Token::Match(tok, "g_free ( %var1% ) ;", varnames) ) return gMalloc; - if ( TOKEN::Match(tok, "fclose ( %var1% )", varnames) ) + if ( Token::Match(tok, "fclose ( %var1% )", varnames) ) return FOPEN; - if ( TOKEN::Match(tok, "pclose ( %var1% )", varnames) ) + if ( Token::Match(tok, "pclose ( %var1% )", varnames) ) return POPEN; return No; } //-------------------------------------------------------------------------- -const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ) +const char * CheckMemoryLeakClass::call_func( const Token *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ) { // Keywords that are not function calls.. - if (TOKEN::Match(tok,"if|for|while")) + if (Token::Match(tok,"if|for|while")) return 0; // String functions that are not allocating nor deallocating memory.. - if (TOKEN::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr")) + if (Token::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr")) return 0; // Memory functions that are not allocating nor deallocating memory.. - if (TOKEN::Match(tok, "memset|memcpy|memmove|memchr")) + if (Token::Match(tok, "memset|memcpy|memmove|memchr")) return 0; // I/O functions that are not allocating nor deallocating memory.. - if (TOKEN::Match(tok, "fgets|fgetc|fputs|fputc|printf")) + if (Token::Match(tok, "fgets|fgetc|fputs|fputc|printf")) return 0; // Convert functions that are not allocating nor deallocating memory.. - if (TOKEN::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod")) + if (Token::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod")) return 0; if (GetAllocationType(tok)!=No || GetReallocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No) @@ -216,7 +216,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::listaaaa(); - for ( std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) + for ( std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) { if ( (*it)->str() == funcname ) return "recursive"; @@ -240,28 +240,28 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::liststr() == "," ) ++par; - if ( TOKEN::Match(tok, "[,()] %var1% [,()]", varnames) ) + if ( Token::Match(tok, "[,()] %var1% [,()]", varnames) ) { - const TOKEN *ftok = _tokenizer->GetFunctionTokenByName(funcname); + const Token *ftok = _tokenizer->GetFunctionTokenByName(funcname); const char *parname = Tokenizer::getParameterName( ftok, par ); if ( ! parname ) return "recursive"; // Check if the function deallocates the variable.. while ( ftok && (ftok->str() != "{") ) ftok = ftok->next(); - TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype ); + Token *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype ); simplifycode( func ); - const TOKEN *func_ = func; + const Token *func_ = func; while ( func_ && func_->str() == ";" ) func_ = func_->next(); const char *ret = 0; // TODO : "goto" isn't handled well - if ( TOKEN::findmatch(func_, "dealloc") ) + if ( Token::findmatch(func_, "dealloc") ) ret = "dealloc"; - else if ( TOKEN::findmatch(func_, "use") ) + else if ( Token::findmatch(func_, "use") ) ret = "use"; - else if ( TOKEN::findmatch(func_, "&use") ) + else if ( Token::findmatch(func_, "&use") ) ret = "&use"; Tokenizer::deleteTokens(func); @@ -274,17 +274,17 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list &callstack, const char varname[] ) +void CheckMemoryLeakClass::MismatchError( const Token *Tok1, const std::list &callstack, const char varname[] ) { std::ostringstream errmsg; - for ( std::list::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok ) + 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() ); } //--------------------------------------------------------------------------- -void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[], AllocType alloctype ) +void CheckMemoryLeakClass::MemoryLeak( const Token *tok, const char varname[], AllocType alloctype ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok); @@ -300,29 +300,29 @@ void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[], A } //--------------------------------------------------------------------------- -void CheckMemoryLeakClass::instoken(TOKEN *tok, const char str[]) +void CheckMemoryLeakClass::instoken(Token *tok, const char str[]) { tok->insertToken( str ); } //--------------------------------------------------------------------------- -bool CheckMemoryLeakClass::notvar(const TOKEN *tok, const char *varnames[]) +bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[]) { - return bool( TOKEN::Match(tok, "! %var1% [;)&|]", varnames) || - TOKEN::Match(tok, "! ( %var1% )", varnames) || - TOKEN::Match(tok, "unlikely ( ! %var1% )", varnames) || - TOKEN::Match(tok, "unlikely ( %var1% == 0 )", varnames) || - TOKEN::Match(tok, "0 == %var1% [;)&|]", varnames) || - TOKEN::Match(tok, "%var1% == 0", varnames) ); + return bool( Token::Match(tok, "! %var1% [;)&|]", varnames) || + Token::Match(tok, "! ( %var1% )", varnames) || + Token::Match(tok, "unlikely ( ! %var1% )", varnames) || + Token::Match(tok, "unlikely ( %var1% == 0 )", varnames) || + Token::Match(tok, "0 == %var1% [;)&|]", varnames) || + Token::Match(tok, "%var1% == 0", varnames) ); } -TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype) +Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype) { const char *varnames[2]; varnames[0] = varname; varnames[1] = 0; - TOKEN *rethead = 0, *rettail = 0; + Token *rethead = 0, *rettail = 0; #define addtoken(_str) \ { \ if (rettail) \ @@ -332,7 +332,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } \ else \ { \ - rethead = new TOKEN; \ + rethead = new Token; \ rettail = rethead; \ rettail->str(_str); \ } \ @@ -372,7 +372,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( parlevel == 0 && tok->str()==";") addtoken(";"); - if (TOKEN::Match(tok, "[(;{}] %var1% =", varnames)) + if (Token::Match(tok, "[(;{}] %var1% =", varnames)) { AllocType alloc = GetAllocationType(tok->tokAt(3)); bool realloc = false; @@ -391,7 +391,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list // If "--all" hasn't been given, don't check classes.. if ( alloc == New && ! _settings._showAll ) { - if ( TOKEN::Match(tok->tokAt(3), "new %type% [(;]") ) + if ( Token::Match(tok->tokAt(3), "new %type% [(;]") ) { if ( isclass( tok->tokAt(4) ) ) alloc = No; @@ -415,12 +415,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list // is the pointer in rhs? bool rhs = false; std::string pattern("[=+] " + std::string(varname)); - for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == ";" ) break; - if ( TOKEN::Match(tok2, pattern.c_str()) ) + if ( Token::Match(tok2, pattern.c_str()) ) { rhs = true; break; @@ -431,7 +431,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } } - if ( TOKEN::Match(tok->previous(), "[;{})] %var%") ) + if ( Token::Match(tok->previous(), "[;{})] %var%") ) { AllocType dealloc = GetDeallocationType(tok, varnames); if ( dealloc != No ) @@ -447,9 +447,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } // if else switch - if ( TOKEN::Match(tok, "if ( %var1% )", varnames) || - TOKEN::Match(tok, "if ( %var1% != 0 )", varnames) || - TOKEN::Match(tok, "if ( 0 != %var1% )", varnames) ) + if ( Token::Match(tok, "if ( %var1% )", varnames) || + Token::Match(tok, "if ( %var1% != 0 )", varnames) || + Token::Match(tok, "if ( 0 != %var1% )", varnames) ) { addtoken("if(var)"); @@ -457,16 +457,16 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list while ( tok->str() != ")" ) tok = tok->next(); } - else if ( TOKEN::Match(tok, "if (") && notvar(tok->tokAt(2), varnames) ) + else if ( Token::Match(tok, "if (") && notvar(tok->tokAt(2), varnames) ) { addtoken("if(!var)"); } - else if ( TOKEN::Match(tok, "if") ) + else if ( Token::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() ) + for ( const Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == "(" ) ++parlevel; @@ -476,7 +476,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( parlevel <= 0 ) break; } - if ( TOKEN::Match(tok2, "fclose ( %var1% )", varnames) ) + if ( Token::Match(tok2, "fclose ( %var1% )", varnames) ) { addtoken( "dealloc" ); addtoken( ";" ); @@ -484,8 +484,8 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list break; } if ( (tok2->str() != ".") && - TOKEN::Match(tok2->next(), "%var1%", varnames) && - !TOKEN::Match(tok2->next(), "%var1% .", varnames) ) + Token::Match(tok2->next(), "%var1%", varnames) && + !Token::Match(tok2->next(), "%var1% .", varnames) ) { dep = true; break; @@ -539,12 +539,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( tok->str() == "return" ) { addtoken("return"); - if ( TOKEN::Match(tok, "return %var1%", varnames) || - TOKEN::Match(tok, "return & %var1%", varnames) ) + if ( Token::Match(tok, "return %var1%", varnames) || + Token::Match(tok, "return & %var1%", varnames) ) addtoken("use"); - if (TOKEN::simpleMatch(tok->next(), "(")) + if (Token::simpleMatch(tok->next(), "(")) { - for (const TOKEN *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) + for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) { if ( tok2->str() == "(" || tok2->str() == ")" ) break; @@ -559,23 +559,23 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } // throw.. - if ( TOKEN::Match(tok, "try|throw|catch") ) + if ( Token::Match(tok, "try|throw|catch") ) addtoken(tok->strAt(0)); // Assignment.. - if ( TOKEN::Match(tok,"[)=] %var1% [+;)]", varnames) || - TOKEN::Match(tok, "%var1% +=|-=", varnames) || - TOKEN::Match(tok, "+=|<< %var1% ;", varnames) ) + if ( Token::Match(tok,"[)=] %var1% [+;)]", varnames) || + Token::Match(tok, "%var1% +=|-=", varnames) || + Token::Match(tok, "+=|<< %var1% ;", varnames) ) { addtoken("use"); } - else if ( TOKEN::Match(tok, "[;{}=(,+-*/] %var1% [", varnames) ) + else if ( Token::Match(tok, "[;{}=(,+-*/] %var1% [", varnames) ) { addtoken("use_"); } // Investigate function calls.. - if ( TOKEN::Match(tok, "%var% (") ) + if ( Token::Match(tok, "%var% (") ) { const char *str = call_func(tok, callstack, varnames, alloctype, dealloctype); if ( str ) @@ -584,20 +584,20 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list // Callback.. bool matchFirst; - if ( (matchFirst = TOKEN::Match(tok, "( %var%")) || - TOKEN::Match(tok, "( * %var%") ) + if ( (matchFirst = Token::Match(tok, "( %var%")) || + Token::Match(tok, "( * %var%") ) { int tokIdx = matchFirst ? 2 : 3; - while ( TOKEN::simpleMatch(tok->tokAt(tokIdx), ".") && - TOKEN::Match(tok->tokAt(tokIdx + 1), "%var%") ) + while ( Token::simpleMatch(tok->tokAt(tokIdx), ".") && + Token::Match(tok->tokAt(tokIdx + 1), "%var%") ) tokIdx += 2; - if ( TOKEN::simpleMatch(tok->tokAt(tokIdx), ") (") ) + if ( Token::simpleMatch(tok->tokAt(tokIdx), ") (") ) { - for ( const TOKEN *tok2 = tok->tokAt(tokIdx + 2); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok->tokAt(tokIdx + 2); tok2; tok2 = tok2->next() ) { - if ( TOKEN::Match(tok2, "[;{]") ) + if ( Token::Match(tok2, "[;{]") ) break; else if ( tok2->str() == varname ) { @@ -609,11 +609,11 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } // Linux lists.. - if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) ) + if ( Token::Match( tok, "[=(,] & %var1% [.[]", varnames ) ) { addtoken("&use"); } - else if ( TOKEN::Match( tok, "[=(,] & %var1% [,)]", varnames ) ) + else if ( Token::Match( tok, "[=(,] & %var1% [,)]", varnames ) ) { addtoken("&use2"); } @@ -622,17 +622,17 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list return rethead; } -void CheckMemoryLeakClass::erase(TOKEN *begin, const TOKEN *end) +void CheckMemoryLeakClass::erase(Token *begin, const Token *end) { - TOKEN::eraseTokens( begin, end ); + Token::eraseTokens( begin, end ); } -void CheckMemoryLeakClass::simplifycode(TOKEN *tok) +void CheckMemoryLeakClass::simplifycode(Token *tok) { // Replace "throw" that is not in a try block with "return" int indentlevel = 0; int trylevel = -1; - for (TOKEN *tok2 = tok; tok2; tok2 = tok2->next()) + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { if ( tok2->str() == "{" ) ++indentlevel; @@ -654,17 +654,17 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) { done = true; - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL ) + for ( Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL ) { // Delete extra ";" - while (TOKEN::Match(tok2,"[;{}] ;")) + while (Token::Match(tok2,"[;{}] ;")) { erase(tok2, tok2->tokAt(2)); done = false; } // Replace "{ }" with ";" - if ( TOKEN::Match(tok2->next(), "{ }") ) + if ( Token::Match(tok2->next(), "{ }") ) { tok2->next()->str(";"); erase(tok2->next(), tok2->tokAt(3)); @@ -672,13 +672,13 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Delete braces around a single instruction.. - if ( TOKEN::Match(tok2->next(), "{ %var% ; }") ) + if ( Token::Match(tok2->next(), "{ %var% ; }") ) { erase( tok2, tok2->tokAt(2) ); erase( tok2->next()->next(), tok2->tokAt(4) ); done = false; } - if ( TOKEN::Match(tok2->next(), "{ %var% %var% ; }") ) + if ( Token::Match(tok2->next(), "{ %var% %var% ; }") ) { erase( tok2, tok2->tokAt(2) ); erase( tok2->next()->next()->next(), tok2->tokAt(5) ); @@ -686,32 +686,32 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } - if ( TOKEN::simpleMatch(tok2->next(), "if") ) + if ( Token::simpleMatch(tok2->next(), "if") ) { // Delete empty if that is not followed by an else - if (TOKEN::Match(tok2->next(), "if ; !!else") ) + if (Token::Match(tok2->next(), "if ; !!else") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Delete "if ; else ;" - else if ( TOKEN::Match(tok2->next(), "if ; else ;") ) + else if ( Token::Match(tok2->next(), "if ; else ;") ) { erase( tok2, tok2->tokAt(4) ); done = false; } // Two "if alloc ;" after one another.. perhaps only one of them can be executed each time - else if (!_settings._showAll && TOKEN::Match(tok2, "[;{}] if alloc ; if alloc ;")) + else if (!_settings._showAll && Token::Match(tok2, "[;{}] if alloc ; if alloc ;")) { erase(tok2, tok2->tokAt(4)); done = false; } // TODO Make this more generic. Delete "if ; else use ; use" - else if ( TOKEN::Match(tok2, "; if ; else assign|use ; assign|use") || - TOKEN::Match(tok2, "; if assign|use ; else ; assign|use") ) + else if ( Token::Match(tok2, "; if ; else assign|use ; assign|use") || + Token::Match(tok2, "; if assign|use ; else ; assign|use") ) { erase( tok2, tok2->tokAt(4) ); done = false; @@ -721,7 +721,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Reduce "if assign|dealloc|use ;" that is not followed by an else.. // If "--all" has been given these are deleted // Otherwise, only the "if" will be deleted - else if (TOKEN::Match(tok2, "[;{}] if assign|dealloc|use ; !!else") ) + else if (Token::Match(tok2, "[;{}] if assign|dealloc|use ; !!else") ) { if ( _settings._showAll ) erase(tok2, tok2->tokAt(3)); @@ -731,21 +731,21 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Reduce "if if" => "if" - else if ( TOKEN::Match(tok2, "if if") ) + else if ( Token::Match(tok2, "if if") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Reduce "if return ; alloc ;" => "alloc ;" - else if (TOKEN::Match(tok2, "[;{}] if return ; alloc ;")) + else if (Token::Match(tok2, "[;{}] if return ; alloc ;")) { erase(tok2, tok2->tokAt(4)); done = false; } // "[;{}] if alloc ; else return ;" => "[;{}] alloc ;" - else if (TOKEN::Match(tok2,"[;{}] if alloc ; else return ;")) + else if (Token::Match(tok2,"[;{}] if alloc ; else return ;")) { erase(tok2, tok2->tokAt(2)); // Remove "if" erase(tok2->next(), tok2->tokAt(5)); // Remove "; else return" @@ -753,28 +753,28 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Reduce "if ; else %var% ;" => "if %var% ;" - else if ( TOKEN::Match(tok2->next(), "if ; else %var% ;") ) + else if ( Token::Match(tok2->next(), "if ; else %var% ;") ) { erase( tok2->next(), tok2->tokAt(4) ); done = false; } // Reduce "if ; else return use ;" => "if return use ;" - else if ( TOKEN::Match(tok2->next(), "if ; else return use ;") ) + else if ( Token::Match(tok2->next(), "if ; else return use ;") ) { erase( tok2->next(), tok2->tokAt(4) ); done = false; } // Reduce "if return ; if return ;" => "if return ;" - else if ( TOKEN::Match(tok2->next(), "if return ; if return ;") ) + else if ( Token::Match(tok2->next(), "if return ; if return ;") ) { erase( tok2, tok2->tokAt(4) ); done = false; } // Delete first if in .. "if { dealloc|assign|use ; return ; } if return ;" - else if ( TOKEN::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; } if return ;") ) + else if ( Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; } if return ;") ) { erase(tok2, tok2->tokAt(8)); done = false; @@ -783,12 +783,12 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Reducing if.. else if ( _settings._showAll ) { - if (TOKEN::Match(tok2->next(), "if assign|dealloc|use ; else")) + if (Token::Match(tok2->next(), "if assign|dealloc|use ; else")) { erase(tok2->next(), tok2->tokAt(3)); done = false; } - if (TOKEN::Match(tok2,"[;{}] if { assign|dealloc|use ; return ; } !!else") ) + if (Token::Match(tok2,"[;{}] if { assign|dealloc|use ; return ; } !!else") ) { erase(tok2,tok2->tokAt(8)); done = false; @@ -799,14 +799,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else.. - if (TOKEN::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else") ) + if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;" - if ( TOKEN::Match(tok2, "; if(!var) alloc ; !!else") ) + if ( Token::Match(tok2, "; if(!var) alloc ; !!else") ) { // Remove the "if(!var)" erase( tok2, tok2->tokAt(2) ); @@ -819,28 +819,28 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Remove "catch ;" - if ( TOKEN::simpleMatch(tok2->next(), "catch ;") ) + if ( Token::simpleMatch(tok2->next(), "catch ;") ) { erase(tok2, tok2->tokAt(3)); done = false; } // Reduce "if* ;" that is not followed by an else.. - if (TOKEN::Match(tok2->next(), "if(var)|if(!var)|ifv ; !!else") ) + if (Token::Match(tok2->next(), "if(var)|if(!var)|ifv ; !!else") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Reduce "else ;" => ";" - if ( TOKEN::Match(tok2->next(), "else ;") ) + if ( Token::Match(tok2->next(), "else ;") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Delete if block: "alloc; if return use ;" - if (TOKEN::Match(tok2,"alloc ; if return use ; !!else") ) + if (Token::Match(tok2,"alloc ; if return use ; !!else") ) { erase(tok2, tok2->tokAt(5)); done = false; @@ -848,14 +848,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Replace "dealloc use ;" with "dealloc ;" - if ( TOKEN::Match(tok2, "dealloc use ;") ) + if ( Token::Match(tok2, "dealloc use ;") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Remove the "if break|continue ;" that follows "dealloc ; alloc ;" - if ( ! _settings._showAll && TOKEN::Match(tok2, "dealloc ; alloc ; if break|continue ;") ) + if ( ! _settings._showAll && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;") ) { tok2 = tok2->next()->next()->next(); erase(tok2, tok2->tokAt(3)); @@ -864,7 +864,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Reduce "do { alloc ; } " => "alloc ;" // TODO: If the loop can be executed twice reduce to "loop alloc ;" instead - if ( TOKEN::Match(tok2->next(), "do { alloc ; }") ) + if ( Token::Match(tok2->next(), "do { alloc ; }") ) { erase(tok2, tok2->tokAt(3)); erase(tok2->next()->next(), tok2->tokAt(4)); @@ -872,14 +872,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Reduce "loop if break ; => ";" - if ( TOKEN::Match( tok2->next(), "loop if break|continue ; !!else") ) + if ( Token::Match( tok2->next(), "loop if break|continue ; !!else") ) { erase( tok2, tok2->tokAt(4) ); done = false; } // Reduce "loop { assign|dealloc|use ; alloc ; if break ; }" to "assign|dealloc|use ; alloc ;" - if ( TOKEN::Match( tok2->next(), "loop { assign|dealloc|use ; alloc ; if break|continue ; }" ) ) + if ( Token::Match( tok2->next(), "loop { assign|dealloc|use ; alloc ; if break|continue ; }" ) ) { // erase "loop {" erase( tok2, tok2->tokAt(3) ); @@ -890,7 +890,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Replace "loop { X ; break ; }" with "X ;" - if ( TOKEN::Match(tok2->next(), "loop { %var% ; break ; }") ) + if ( Token::Match(tok2->next(), "loop { %var% ; break ; }") ) { erase(tok2, tok2->tokAt(3)); erase(tok2->next()->next(), tok2->tokAt(6)); @@ -898,77 +898,77 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Replace "loop ;" with ";" - if ( TOKEN::Match(tok2->next(), "loop ;") ) + if ( Token::Match(tok2->next(), "loop ;") ) { erase(tok2, tok2->tokAt(2)); done = false; } // Replace "loop !var ;" with ";" - if ( TOKEN::Match(tok2->next(), "loop !var ;") ) + if ( Token::Match(tok2->next(), "loop !var ;") ) { erase(tok2, tok2->tokAt(4)); done = false; } // Replace "loop !var alloc ;" with " alloc ;" - if ( TOKEN::Match(tok2->next(), "loop !var alloc ;") ) + if ( Token::Match(tok2->next(), "loop !var alloc ;") ) { erase(tok2, tok2->tokAt(3)); done = false; } // Delete if block in "alloc ; if(!var) return ;" - if ( TOKEN::Match(tok2, "alloc ; if(!var) return ;") ) + if ( Token::Match(tok2, "alloc ; if(!var) return ;") ) { erase(tok2, tok2->tokAt(4)); done = false; } // Delete if block: "alloc; if return use ;" - if (TOKEN::Match(tok2,"alloc ; if return use ; !!else") ) + if (Token::Match(tok2,"alloc ; if return use ; !!else") ) { erase(tok2, tok2->tokAt(5)); done = false; } // Reduce "[;{}] return ; %var%" => "[;{}] return ;" - if ( TOKEN::Match(tok2, "[;{}] return ; %var%") ) + if ( Token::Match(tok2, "[;{}] return ; %var%") ) { erase( tok2->next()->next(), tok2->tokAt(4) ); done = false; } // Reduce "[;{}] return use ; %var%" => "[;{}] return use ;" - if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") ) + if ( Token::Match(tok2, "[;{}] return use ; %var%") ) { erase( tok2->next()->next()->next(), tok2->tokAt(5) ); done = false; } // Reduce "if(var) return use ;" => "return use ;" - if ( TOKEN::Match(tok2->next(), "if(var) return use ; !!else") ) + if ( Token::Match(tok2->next(), "if(var) return use ; !!else") ) { erase( tok2, tok2->tokAt(2) ); done = false; } // Reduce "if(var) assign|dealloc|use ;" => "assign|dealloc|use ;" - if ( TOKEN::Match(tok2->next(), "if(var) assign|dealloc|use ; !!else") ) + if ( Token::Match(tok2->next(), "if(var) assign|dealloc|use ; !!else") ) { erase( tok2, tok2->tokAt(2) ); done = false; } // Reduce "[;{}] alloc ; dealloc ;" => "[;{}]" - if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") ) + if ( Token::Match(tok2, "[;{}] alloc ; dealloc ;") ) { erase( tok2, tok2->tokAt(5) ); done = false; } // Reduce "if* alloc ; dealloc ;" => ";" - if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") && + if ( Token::Match(tok2->tokAt(2), "alloc ; dealloc ;") && tok2->next()->str().find("if") == 0 ) { erase( tok2, tok2->tokAt(5) ); @@ -976,40 +976,40 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Delete second use in "use ; use ;" - while (TOKEN::Match(tok2, "[;{}] use ; use ;")) + while (Token::Match(tok2, "[;{}] use ; use ;")) { erase(tok2, tok2->tokAt(3)); done = false; } // Delete first part in "use ; dealloc ;" - if (TOKEN::Match(tok2, "[;{}] use ; dealloc ;")) + if (Token::Match(tok2, "[;{}] use ; dealloc ;")) { erase(tok2, tok2->tokAt(3)); done = false; } // Delete first part in "use ; return use ;" - if (TOKEN::Match(tok2, "[;{}] use ; return use ;")) + if (Token::Match(tok2, "[;{}] use ; return use ;")) { erase(tok2, tok2->tokAt(2)); done = false; } // Delete second case in "case ; case ;" - while (TOKEN::Match(tok2, "case ; case ;")) + while (Token::Match(tok2, "case ; case ;")) { erase(tok2, tok2->tokAt(3)); done = false; } // Replace switch with if (if not complicated) - if (TOKEN::Match(tok2, "switch {")) + if (Token::Match(tok2, "switch {")) { // Right now, I just handle if there are a few case and perhaps a default. bool valid = false; bool incase = false; - for ( const TOKEN * _tok = tok2->tokAt(2); _tok; _tok = _tok->next() ) + for ( const Token * _tok = tok2->tokAt(2); _tok; _tok = _tok->next() ) { if ( _tok->str() == "{" ) break; @@ -1029,11 +1029,11 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) else if (_tok->str() == "loop") break; - else if (incase && TOKEN::Match(_tok,"case")) + else if (incase && Token::Match(_tok,"case")) break; - incase |= TOKEN::Match(_tok,"case"); - incase &= !TOKEN::Match(_tok,"break"); + incase |= Token::Match(_tok,"case"); + incase &= !Token::Match(_tok,"break"); } if ( !incase && valid ) @@ -1043,9 +1043,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) erase( tok2, tok2->tokAt(2) ); tok2 = tok2->next(); bool first = true; - while (TOKEN::Match(tok2,"case") || TOKEN::Match(tok2,"default")) + while (Token::Match(tok2,"case") || Token::Match(tok2,"default")) { - bool def = TOKEN::Match(tok2, "default"); + bool def = Token::Match(tok2, "default"); tok2->str(first ? "if" : "}"); if ( first ) { @@ -1060,9 +1060,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) instoken( tok2, "if" ); instoken( tok2, "else" ); } - while ( tok2 && tok2->str() != "}" && ! TOKEN::Match(tok2,"break ;") ) + while ( tok2 && tok2->str() != "}" && ! Token::Match(tok2,"break ;") ) tok2 = tok2->next(); - if (TOKEN::Match(tok2,"break ;")) + if (Token::Match(tok2,"break ;")) { tok2->str(";"); tok2 = tok2->next()->next(); @@ -1079,25 +1079,25 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Check for memory leaks for a function variable. -void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) +void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const Token *Tok1, const char varname[] ) { - std::list callstack; + std::list callstack; AllocType alloctype = No; AllocType dealloctype = No; - const TOKEN *result; + const Token *result; - TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); + Token *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); //tok->printOut( "getcode result" ); // Simplify the code and check if freed memory is used.. - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { - while ( TOKEN::Match(tok2, "[;{}] ;") ) + while ( Token::Match(tok2, "[;{}] ;") ) erase(tok2, tok2->tokAt(2)); } - if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use|use_ ;")) != NULL ) + if ( (result = Token::findmatch(tok, "dealloc [;{}] use|use_ ;")) != NULL ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(result->tokAt(2)) << ": Using \"" << varname << "\" after it has been deallocated / released"; @@ -1105,7 +1105,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const } // Replace "&use" with "use". Replace "&use2" with ";" - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if (tok2->str() == "&use") tok2->str("use"); @@ -1125,44 +1125,44 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const //tok->printOut( "simplifycode result" ); // If the variable is not allocated at all => no memory leak - if (TOKEN::findmatch(tok, "alloc") == 0) + if (Token::findmatch(tok, "alloc") == 0) { Tokenizer::deleteTokens(tok); return; } // TODO : handle "goto" - if (TOKEN::findmatch(tok, "goto")) + if (Token::findmatch(tok, "goto")) { Tokenizer::deleteTokens(tok); return; } - if ( (result = TOKEN::findmatch(tok, "loop alloc ;")) != NULL ) + if ( (result = Token::findmatch(tok, "loop alloc ;")) != NULL ) { MemoryLeak(result, varname, alloctype); } - else if ( (result = TOKEN::findmatch(tok, "alloc ; if break|continue|return ;")) != NULL ) + else if ( (result = Token::findmatch(tok, "alloc ; if break|continue|return ;")) != NULL ) { MemoryLeak(result->tokAt(3), varname, alloctype); } - else if ( _settings._showAll && (result = TOKEN::findmatch(tok, "alloc ; ifv break|continue|return ;")) != NULL ) + else if ( _settings._showAll && (result = Token::findmatch(tok, "alloc ; ifv break|continue|return ;")) != NULL ) { MemoryLeak(result->tokAt(3), varname, alloctype); } - else if ( (result = TOKEN::findmatch(tok, "alloc ; alloc|assign|return ;")) != NULL ) + else if ( (result = Token::findmatch(tok, "alloc ; alloc|assign|return ;")) != NULL ) { MemoryLeak(result->tokAt(2), varname, alloctype); } - else if ( ! TOKEN::findmatch(tok,"dealloc") && - ! TOKEN::findmatch(tok,"use") && - ! TOKEN::findmatch(tok,"return use ;") ) + else if ( ! Token::findmatch(tok,"dealloc") && + ! Token::findmatch(tok,"use") && + ! Token::findmatch(tok,"return use ;") ) { - const TOKEN *last = tok; + const Token *last = tok; while (last->next()) last = last->next(); MemoryLeak(last, varname, alloctype); @@ -1171,27 +1171,27 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const // detect cases that "simplifycode" don't handle well.. else if ( _settings._debug ) { - TOKEN *first = tok; + Token *first = tok; while ( first && first->str() == ";" ) first = first->next(); bool noerr = false; - noerr |= TOKEN::Match( first, "alloc ; }" ); - noerr |= TOKEN::Match( first, "alloc ; dealloc ; }" ); - noerr |= TOKEN::Match( first, "alloc ; return use ; }" ); - noerr |= TOKEN::Match( first, "alloc ; use ; }" ); - noerr |= TOKEN::Match( first, "alloc ; use ; return ; }" ); - noerr |= TOKEN::Match( first, "if alloc ; dealloc ; }" ); - noerr |= TOKEN::Match( first, "if alloc ; return use ; }" ); - noerr |= TOKEN::Match( first, "if alloc ; use ; }" ); - noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ; }" ); - noerr |= TOKEN::Match( first, "alloc ; if return ; dealloc; }" ); + noerr |= Token::Match( first, "alloc ; }" ); + noerr |= Token::Match( first, "alloc ; dealloc ; }" ); + noerr |= Token::Match( first, "alloc ; return use ; }" ); + noerr |= Token::Match( first, "alloc ; use ; }" ); + noerr |= Token::Match( first, "alloc ; use ; return ; }" ); + noerr |= Token::Match( first, "if alloc ; dealloc ; }" ); + noerr |= Token::Match( first, "if alloc ; return use ; }" ); + noerr |= Token::Match( first, "if alloc ; use ; }" ); + noerr |= Token::Match( first, "alloc ; ifv return ; dealloc ; }" ); + noerr |= Token::Match( first, "alloc ; if return ; dealloc; }" ); // Unhandled case.. if ( ! noerr ) { std::cout << "Token listing..\n "; - for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok; tok2; tok2 = tok2->next() ) std::cout << " " << tok2->str(); std::cout << "\n"; } @@ -1212,7 +1212,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_InFunction() { bool infunc = false; int indentlevel = 0; - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (tok->str() == "{") ++indentlevel; @@ -1224,20 +1224,20 @@ void CheckMemoryLeakClass::CheckMemoryLeak_InFunction() // In function.. if ( indentlevel == 0 ) { - if ( TOKEN::Match(tok, ") {") ) + if ( Token::Match(tok, ") {") ) infunc = true; - else if ( TOKEN::Match(tok, "[;}]") ) + else if ( Token::Match(tok, "[;}]") ) infunc = false; } // Declare a local variable => Check if (indentlevel>0 && infunc) { - if ( TOKEN::Match(tok, "[{};] %type% * %var% [;=]") ) + if ( Token::Match(tok, "[{};] %type% * %var% [;=]") ) CheckMemoryLeak_CheckScope( tok->next(), tok->strAt(3) ); - else if ( TOKEN::Match(tok, "[{};] %type% %type% * %var% [;=]") ) + else if ( Token::Match(tok, "[{};] %type% %type% * %var% [;=]") ) CheckMemoryLeak_CheckScope( tok->next(), tok->strAt(4) ); } } @@ -1255,7 +1255,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_InFunction() void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers() { int indentlevel = 0; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -1263,7 +1263,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers() else if ( tok->str() == "}" ) --indentlevel; - else if ( indentlevel == 0 && TOKEN::Match(tok, "class %var% [{:]") ) + else if ( indentlevel == 0 && Token::Match(tok, "class %var% [{:]") ) { std::vector classname; classname.push_back( tok->strAt(1) ); @@ -1273,7 +1273,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers() } -void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector &classname ) +void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const Token *tok1, std::vector &classname ) { // Go into class. while ( tok1 && tok1->str() != "{" ) @@ -1282,7 +1282,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN tok1 = tok1->next(); int indentlevel = 0; - for ( const TOKEN *tok = tok1; tok; tok = tok->next() ) + for ( const Token *tok = tok1; tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -1299,7 +1299,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN continue; // Declaring subclass.. recursive checking - if ( TOKEN::Match(tok, "class %var% [{:]") ) + if ( Token::Match(tok, "class %var% [{:]") ) { classname.push_back( tok->strAt(1) ); CheckMemoryLeak_ClassMembers_ParseClass( tok, classname ); @@ -1307,9 +1307,9 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN } // Declaring member variable.. check allocations and deallocations - if ( TOKEN::Match(tok->next(), "%type% * %var% ;") ) + if ( Token::Match(tok->next(), "%type% * %var% ;") ) { - if ( tok->isName() || TOKEN::Match(tok, "[;}]")) + if ( tok->isName() || Token::Match(tok, "[;}]")) { if (_settings._showAll || !isclass(tok->tokAt(1))) CheckMemoryLeak_ClassMembers_Variable( classname, tok->strAt(3) ); @@ -1353,7 +1353,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec // Loop through all tokens. Inspect member functions bool memberfunction = false; int indentlevel = 0; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -1364,9 +1364,9 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec // Set the 'memberfunction' variable.. if ( indentlevel == 0 ) { - if ( TOKEN::Match(tok, "[;}]") ) + if ( Token::Match(tok, "[;}]") ) memberfunction = false; - else if ( TOKEN::Match( tok, fpattern.str().c_str() ) || TOKEN::Match( tok, destructor.str().c_str() ) ) + else if ( Token::Match( tok, fpattern.str().c_str() ) || Token::Match( tok, destructor.str().c_str() ) ) memberfunction = true; } @@ -1374,12 +1374,12 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec if ( indentlevel > 0 && memberfunction ) { // Allocate.. - if ( TOKEN::Match( tok, varname_eq.str().c_str() ) ) + if ( Token::Match( tok, varname_eq.str().c_str() ) ) { AllocType alloc = GetAllocationType( tok->tokAt(2) ); if ( alloc != No ) { - std::list callstack; + std::list callstack; if ( Dealloc != No && Dealloc != alloc ) MismatchError( tok, callstack, FullVariableName.str().c_str() ); if ( Alloc != No && Alloc != alloc ) @@ -1394,7 +1394,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec AllocType dealloc = GetDeallocationType( tok, varnames ); if ( dealloc != No ) { - std::list callstack; + std::list callstack; if ( Dealloc != No && Dealloc != dealloc ) MismatchError( tok, callstack, FullVariableName.str().c_str() ); if ( Alloc != No && Alloc != dealloc ) @@ -1436,7 +1436,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak() // Non-recursive function analysis //--------------------------------------------------------------------------- -TOKEN * CheckMemoryLeakClass::functionParameterCode(const TOKEN *ftok, int parameter) +Token * CheckMemoryLeakClass::functionParameterCode(const Token *ftok, int parameter) { int param = 1; // First parameter has index 1 @@ -1456,7 +1456,7 @@ TOKEN * CheckMemoryLeakClass::functionParameterCode(const TOKEN *ftok, int param if ( param != parameter ) continue; - if ( ! TOKEN::Match(ftok, "* %var% [,)]") ) + if ( ! Token::Match(ftok, "* %var% [,)]") ) continue; // Extract and return the code for this parameter.. @@ -1469,8 +1469,8 @@ TOKEN * CheckMemoryLeakClass::functionParameterCode(const TOKEN *ftok, int param // Return the code.. AllocType alloc=No, dealloc=No; - std::list callstack; - TOKEN *code = getcode( ftok, callstack, parname, alloc, dealloc ); + std::list callstack; + Token *code = getcode( ftok, callstack, parname, alloc, dealloc ); simplifycode( code ); return code; } diff --git a/checkmemoryleak.h b/checkmemoryleak.h index c8f0d9676..1194284ec 100644 --- a/checkmemoryleak.h +++ b/checkmemoryleak.h @@ -57,16 +57,16 @@ private: }; void CheckMemoryLeak_ClassMembers_Variable( const std::vector &classname, const char varname[] ); - void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector &classname ); + 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 CheckMemoryLeak_CheckScope( const Token *Tok1, const char varname[] ); /** * Simplify code e.g. by replacing empty "{ }" with ";" * @param tok first token. The tokens list can be modified. */ - void simplifycode(TOKEN *tok); + void simplifycode(Token *tok); /** * Delete tokens between begin and end. E.g. if begin = 1 @@ -75,7 +75,7 @@ private: * @param begin Tokens after this will be erased. * @param end Tokens before this will be erased. */ - void erase(TOKEN *begin, const TOKEN *end); + void erase(Token *begin, const Token *end); /** * Extract a new tokens list that is easier to parse than the "tokens" @@ -87,16 +87,16 @@ private: * @return Newly allocated token array. Caller needs to release reserved * memory by calling Tokenizer::deleteTokens(returnValue); */ - 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[], AllocType alloctype ); - 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 ); - AllocType GetReallocationType( const TOKEN *tok2 ); - bool isclass( const TOKEN *typestr ); + 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[], AllocType alloctype ); + 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 ); + AllocType GetReallocationType( const Token *tok2 ); + bool isclass( const Token *typestr ); const Tokenizer *_tokenizer; ErrorLogger *_errorLogger; @@ -107,7 +107,7 @@ private: #ifdef UNIT_TESTING public: #endif - TOKEN *functionParameterCode(const TOKEN *ftok, int parameter); + Token *functionParameterCode(const Token *ftok, int parameter); }; //--------------------------------------------------------------------------- diff --git a/checkother.cpp b/checkother.cpp index 174236263..c50c4052c 100644 --- a/checkother.cpp +++ b/checkother.cpp @@ -48,15 +48,15 @@ CheckOther::~CheckOther() void CheckOther::WarningOldStylePointerCast() { - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Old style pointer casting.. - if (!TOKEN::Match(tok, "( %type% * ) %var%")) + if (!Token::Match(tok, "( %type% * ) %var%")) continue; // Is "type" a class? const std::string pattern("class " + tok->next()->str()); - if (!TOKEN::findmatch(_tokenizer->tokens(), pattern.c_str())) + if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) continue; std::ostringstream ostr; @@ -76,20 +76,20 @@ void CheckOther::WarningRedundantCode() { // if (p) delete p - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if ( tok->str() != "if" ) continue; const char *varname1 = NULL; - const TOKEN *tok2 = NULL; + const Token *tok2 = NULL; - if (TOKEN::Match(tok,"if ( %var% )")) + if (Token::Match(tok,"if ( %var% )")) { varname1 = tok->strAt( 2); tok2 = tok->tokAt(4); } - else if (TOKEN::Match(tok,"if ( %var% != NULL )")) + else if (Token::Match(tok,"if ( %var% != NULL )")) { varname1 = tok->strAt( 2); tok2 = tok->tokAt(6); @@ -103,38 +103,38 @@ void CheckOther::WarningRedundantCode() { tok2 = tok2->next(); - if (TOKEN::Match(tok2,"delete %var% ; }")) + if (Token::Match(tok2,"delete %var% ; }")) { err = (strcmp(tok2->strAt(1),varname1)==0); } - else if (TOKEN::Match(tok2,"delete [ ] %var% ; }")) + else if (Token::Match(tok2,"delete [ ] %var% ; }")) { err = (strcmp(tok2->strAt(1),varname1)==0); } - else if (TOKEN::Match(tok2,"free ( %var% ) ; }")) + else if (Token::Match(tok2,"free ( %var% ) ; }")) { err = (strcmp(tok2->strAt(2),varname1)==0); } - else if (TOKEN::Match(tok2,"kfree ( %var% ) ; }")) + else if (Token::Match(tok2,"kfree ( %var% ) ; }")) { err = (strcmp(tok2->strAt(2),varname1)==0); } } else { - if (TOKEN::Match(tok2,"delete %var% ;")) + if (Token::Match(tok2,"delete %var% ;")) { err = (strcmp(tok2->strAt(1),varname1)==0); } - else if (TOKEN::Match(tok2,"delete [ ] %var% ;")) + else if (Token::Match(tok2,"delete [ ] %var% ;")) { err = (strcmp(tok2->strAt(1),varname1)==0); } - else if (TOKEN::Match(tok2,"free ( %var% ) ;")) + else if (Token::Match(tok2,"free ( %var% ) ;")) { err = (strcmp(tok2->strAt(2),varname1)==0); } - else if (TOKEN::Match(tok2,"kfree ( %var% ) ;")) + else if (Token::Match(tok2,"kfree ( %var% ) ;")) { err = (strcmp(tok2->strAt(2),varname1)==0); } @@ -163,17 +163,17 @@ void CheckOther::redundantCondition2() "{|{|" " %var% . remove ( %any% ) ; " "}|}|"; - const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), pattern ); + const Token *tok = Token::findmatch( _tokenizer->tokens(), pattern ); while ( tok ) { - bool b = TOKEN::Match( tok->tokAt(15), "{" ); + bool b = Token::Match( tok->tokAt(15), "{" ); // Get tokens for the fields %var% and %any% - const TOKEN *var1 = tok->tokAt(2); - const TOKEN *any1 = tok->tokAt(6); - const TOKEN *var2 = tok->tokAt(9); - const TOKEN *var3 = tok->tokAt(b ? 16 : 15); - const TOKEN *any2 = tok->tokAt(b ? 20 : 19); + const Token *var1 = tok->tokAt(2); + const Token *any1 = tok->tokAt(6); + const Token *var2 = tok->tokAt(9); + const Token *var3 = tok->tokAt(b ? 16 : 15); + const Token *any2 = tok->tokAt(b ? 20 : 19); // Check if all the "%var%" fields are the same and if all the "%any%" are the same.. if (var1->str() == var2->str() && @@ -186,7 +186,7 @@ void CheckOther::redundantCondition2() _errorLogger->reportErr(errmsg.str()); } - tok = TOKEN::findmatch( tok->next(), pattern ); + tok = Token::findmatch( tok->next(), pattern ); } } //--------------------------------------------------------------------------- @@ -201,14 +201,14 @@ void CheckOther::redundantCondition2() void CheckOther::WarningIf() { // Search for 'if (condition);' - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (!TOKEN::simpleMatch(tok, "if (")) + if (!Token::simpleMatch(tok, "if (")) continue; // Search for the end paranthesis for the condition.. int parlevel = 0; - for (const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next()) + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == "(") ++parlevel; @@ -217,7 +217,7 @@ void CheckOther::WarningIf() --parlevel; if ( parlevel <= 0 ) { - if ( TOKEN::Match(tok2, ") ; !!else") ) + if ( Token::Match(tok2, ") ; !!else") ) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\""; @@ -230,16 +230,16 @@ void CheckOther::WarningIf() } // Search for 'a=b; if (a==b)' - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Begin statement? - if ( ! TOKEN::Match(tok, "[;{}]") ) + if ( ! Token::Match(tok, "[;{}]") ) continue; tok = tok->next(); if ( ! tok ) break; - if (!TOKEN::Match(tok,"%var% = %var% ; if ( %var%")) + if (!Token::Match(tok,"%var% = %var% ; if ( %var%")) continue; if ( strcmp(tok->strAt( 9), ")") != 0 ) @@ -296,7 +296,7 @@ void CheckOther::WarningIf() void CheckOther::InvalidFunctionUsage() { - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { if ((tok->str() != "strtol") && (tok->str() != "strtoul")) continue; @@ -304,18 +304,18 @@ void CheckOther::InvalidFunctionUsage() // Locate the third parameter of the function call.. int parlevel = 0; int param = 1; - for ( const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok->next(); tok2; tok2 = tok2->next() ) { - if ( TOKEN::Match(tok2, "(") ) + if ( Token::Match(tok2, "(") ) ++parlevel; - else if (TOKEN::Match(tok2, ")")) + else if (Token::Match(tok2, ")")) --parlevel; - else if (parlevel == 1 && TOKEN::Match(tok2, ",")) + else if (parlevel == 1 && Token::Match(tok2, ",")) { ++param; if (param==3) { - if ( TOKEN::Match(tok2, ", %num% )") ) + if ( Token::Match(tok2, ", %num% )") ) { int radix = atoi(tok2->strAt( 1)); if (!(radix==0 || (radix>=2 && radix<=36))) @@ -340,11 +340,11 @@ void CheckOther::InvalidFunctionUsage() void CheckOther::CheckIfAssignment() { - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (TOKEN::Match(tok, "if ( %var% = %num% )") || - TOKEN::Match(tok, "if ( %var% = %str% )") || - TOKEN::Match(tok, "if ( %var% = %var% )") ) + if (Token::Match(tok, "if ( %var% = %num% )") || + Token::Match(tok, "if ( %var% = %str% )") || + Token::Match(tok, "if ( %var% = %var% )") ) { std::ostringstream ostr; ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?"; @@ -363,19 +363,19 @@ void CheckOther::CheckUnsignedDivision() { // Check for "ivar / uvar" and "uvar / ivar" std::map varsign; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { - if ( TOKEN::Match(tok, "[{};(,] %type% %var% [;=,)]") ) + if ( Token::Match(tok, "[{};(,] %type% %var% [;=,)]") ) { const char *type = tok->strAt( 1); if (strcmp(type,"char")==0 || strcmp(type,"short")==0 || strcmp(type,"int")==0) varsign[tok->strAt(2)] = 's'; } - else if ( TOKEN::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") ) + else if ( Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") ) varsign[tok->strAt(3)] = 'u'; - else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next(), "%var% / %var%")) + else if (!Token::Match(tok,"[).]") && Token::Match(tok->next(), "%var% / %var%")) { const char *varname1 = tok->strAt(1); const char *varname2 = tok->strAt(3); @@ -391,7 +391,7 @@ void CheckOther::CheckUnsignedDivision() } } - else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next(), "%var% / - %num%")) + else if (!Token::Match(tok,"[).]") && Token::Match(tok->next(), "%var% / - %num%")) { const char *varname1 = tok->strAt(1); char sign1 = varsign[varname1]; @@ -403,7 +403,7 @@ void CheckOther::CheckUnsignedDivision() } } - else if (TOKEN::Match(tok, "[([=*/+-] - %num% / %var%")) + else if (Token::Match(tok, "[([=*/+-] - %num% / %var%")) { const char *varname2 = tok->strAt(4); char sign2 = varsign[varname2]; @@ -430,12 +430,12 @@ void CheckOther::CheckVariableScope() // Walk through all tokens.. bool func = false; int indentlevel = 0; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { // Skip class and struct declarations.. if ( (tok->str() == "class") || (tok->str() == "struct") ) { - for (const TOKEN *tok2 = tok; tok2; tok2 = tok2->next()) + for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { if ( tok2->str() == "{" ) { @@ -458,7 +458,7 @@ void CheckOther::CheckVariableScope() } break; } - if (TOKEN::Match(tok2, "[,);]")) + if (Token::Match(tok2, "[,);]")) { break; } @@ -477,14 +477,14 @@ void CheckOther::CheckVariableScope() if ( indentlevel == 0 ) func = false; } - if ( indentlevel == 0 && TOKEN::Match(tok, ") {") ) + if ( indentlevel == 0 && Token::Match(tok, ") {") ) { func = true; } - if ( indentlevel > 0 && func && TOKEN::Match(tok, "[{};]") ) + if ( indentlevel > 0 && func && Token::Match(tok, "[{};]") ) { // First token of statement.. - const TOKEN *tok1 = tok->next(); + const Token *tok1 = tok->next(); if ( ! tok1 ) continue; @@ -495,8 +495,8 @@ void CheckOther::CheckVariableScope() continue; // Variable declaration? - if (TOKEN::Match(tok1, "%var% %var% ;") || - TOKEN::Match(tok1, "%var% %var% =") ) + if (Token::Match(tok1, "%var% %var% ;") || + Token::Match(tok1, "%var% %var% =") ) { CheckVariableScope_LookupVar( tok1, tok1->strAt( 1) ); } @@ -506,12 +506,12 @@ void CheckOther::CheckVariableScope() } //--------------------------------------------------------------------------- -void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] ) +void CheckOther::CheckVariableScope_LookupVar( const Token *tok1, const char varname[] ) { - const TOKEN *tok = tok1; + const Token *tok = tok1; // Skip the variable declaration.. - while (tok && !TOKEN::Match(tok,";")) + while (tok && !Token::Match(tok,";")) tok = tok->next(); // Check if the variable is used in this indentlevel.. @@ -581,20 +581,20 @@ void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char var void CheckOther::CheckConstantFunctionParameter() { - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if ( TOKEN::Match(tok,"[,(] const std :: %type% %var% [,)]") ) + if ( Token::Match(tok,"[,(] const std :: %type% %var% [,)]") ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(5) << " is passed by value, it could be passed by reference/pointer instead"; _errorLogger->reportErr( errmsg.str() ); } - else if ( TOKEN::Match(tok,"[,(] const %type% %var% [,)]") ) + else if ( Token::Match(tok,"[,(] const %type% %var% [,)]") ) { // Check if type is a struct or class. const std::string pattern(std::string("class|struct ") + tok->strAt(2)); - if ( TOKEN::findmatch(_tokenizer->tokens(), pattern.c_str()) ) + if ( Token::findmatch(_tokenizer->tokens(), pattern.c_str()) ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead"; @@ -614,25 +614,25 @@ void CheckOther::CheckStructMemberUsage() { const char *structname = 0; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { if ( tok->fileIndex() != 0 ) continue; if ( tok->str() == "}" ) structname = 0; - if ( TOKEN::Match(tok, "struct %type% {") ) + if ( Token::Match(tok, "struct %type% {") ) structname = tok->strAt( 1); - if (structname && TOKEN::Match(tok, "[{;]")) + if (structname && Token::Match(tok, "[{;]")) { const char *varname = 0; - if (TOKEN::Match(tok->next(), "%type% %var% [;[]")) + if (Token::Match(tok->next(), "%type% %var% [;[]")) varname = tok->strAt( 2 ); - else if (TOKEN::Match(tok->next(), "%type% %type% %var% [;[]")) + else if (Token::Match(tok->next(), "%type% %type% %var% [;[]")) varname = tok->strAt( 2 ); - else if (TOKEN::Match(tok->next(), "%type% * %var% [;[]")) + else if (Token::Match(tok->next(), "%type% * %var% [;[]")) varname = tok->strAt( 3 ); - else if (TOKEN::Match(tok->next(), "%type% %type% * %var% [;[]")) + else if (Token::Match(tok->next(), "%type% %type% * %var% [;[]")) varname = tok->strAt( 4 ); else continue; @@ -641,12 +641,12 @@ void CheckOther::CheckStructMemberUsage() varnames[0] = varname; varnames[1] = 0; bool used = false; - for ( const TOKEN *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next() ) { if ( tok->fileIndex() != 0 ) continue; - if (TOKEN::Match(tok2, ". %var%", varnames)) + if (Token::Match(tok2, ". %var%", varnames)) { if ( strcmp("=", tok2->strAt(2)) == 0 ) continue; @@ -675,17 +675,17 @@ void CheckOther::CheckStructMemberUsage() void CheckOther::CheckCharVariable() { - for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next()) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Declaring the variable.. - if ( TOKEN::Match(tok, "[{};(,] char %var% [;=,)]") ) + if ( Token::Match(tok, "[{};(,] char %var% [;=,)]") ) { const char *varname[2] = {0}; varname[0] = tok->strAt( 2); // Check usage of char variable.. int indentlevel = 0; - for ( const TOKEN *tok2 = tok->next(); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok->next(); tok2; tok2 = tok2->next() ) { if ( tok2->str() == "{" ) ++indentlevel; @@ -697,7 +697,7 @@ void CheckOther::CheckCharVariable() break; } - if ((tok2->str() != ".") && TOKEN::Match(tok2->next(), "%var% [ %var1% ]", varname)) + if ((tok2->str() != ".") && Token::Match(tok2->next(), "%var% [ %var1% ]", varname)) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok2->next()) << ": Warning - using char variable as array index"; @@ -705,7 +705,7 @@ void CheckOther::CheckCharVariable() break; } - if ( TOKEN::Match(tok2, "%var% [&|] %var1%", varname) || TOKEN::Match(tok2, "%var1% [&|]", varname) ) + if ( Token::Match(tok2, "%var% [&|] %var1%", varname) || Token::Match(tok2, "%var1% [&|]", varname) ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation"; @@ -731,7 +731,7 @@ void CheckOther::CheckIncompleteStatement() { int parlevel = 0; - for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next() ) + for ( const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() ) { if ( tok->str() == "(" ) ++parlevel; @@ -741,14 +741,14 @@ void CheckOther::CheckIncompleteStatement() if ( parlevel != 0 ) continue; - if ( (tok->str() != "#") && TOKEN::Match(tok->next(),"; %str%") && !TOKEN::Match(tok->tokAt(3), ",") ) + if ( (tok->str() != "#") && Token::Match(tok->next(),"; %str%") && !Token::Match(tok->tokAt(3), ",") ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with string constant"; _errorLogger->reportErr(errmsg.str()); } - if ( !TOKEN::Match(tok,"#") && TOKEN::Match(tok->next(),"; %num%") && !TOKEN::Match(tok->tokAt(3), ",") ) + if ( !Token::Match(tok,"#") && Token::Match(tok->next(),"; %num%") && !Token::Match(tok->tokAt(3), ",") ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with numeric constant"; @@ -769,20 +769,20 @@ void CheckOther::CheckIncompleteStatement() void CheckOther::unreachableCode() { - const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), "[;{}] return" ); + const Token *tok = Token::findmatch( _tokenizer->tokens(), "[;{}] return" ); while ( tok ) { // Goto the 'return' token tok = tok->next(); // Locate the end of the 'return' statement - while ( tok && ! TOKEN::Match(tok, ";") ) + while ( tok && ! Token::Match(tok, ";") ) tok = tok->next(); - while ( tok && TOKEN::Match(tok->next(), ";") ) + while ( tok && Token::Match(tok->next(), ";") ) tok = tok->next(); // If there is a statement below the return it is unreachable - if (!TOKEN::Match(tok, "; case|default|}|#") && !TOKEN::Match(tok, "; %var% :")) + if (!Token::Match(tok, "; case|default|}|#") && !Token::Match(tok, "; %var% :")) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok->next()) << ": Unreachable code below a 'return'"; @@ -790,7 +790,7 @@ void CheckOther::unreachableCode() } // Find the next 'return' statement - tok = TOKEN::findmatch( tok, "[;{}] return" ); + tok = Token::findmatch( tok, "[;{}] return" ); } } //--------------------------------------------------------------------------- @@ -804,7 +804,7 @@ void CheckOther::unreachableCode() // Usage of function variables //--------------------------------------------------------------------------- -static bool isOp(const TOKEN *tok) +static bool isOp(const Token *tok) { return bool(tok && (tok->str() == "&&" || @@ -816,13 +816,13 @@ static bool isOp(const TOKEN *tok) tok->str() == ">" || tok->str() == ">=" || tok->str() == "<<" || - TOKEN::Match(tok, "[+-*/&|,[]]"))); + Token::Match(tok, "[+-*/&|,[]]"))); } void CheckOther::functionVariableUsage() { // Parse all executing scopes.. - const TOKEN *tok1 = TOKEN::findmatch( _tokenizer->tokens(), ") const| {" ); + const Token *tok1 = Token::findmatch( _tokenizer->tokens(), ") const| {" ); while ( tok1 ) { // Varname, usage {1=declare, 2=read, 4=write} @@ -832,7 +832,7 @@ void CheckOther::functionVariableUsage() static const unsigned int USAGE_WRITE = 4; int indentlevel = 0; - for ( const TOKEN *tok = tok1; tok; tok = tok->next() ) + for ( const Token *tok = tok1; tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -843,34 +843,34 @@ void CheckOther::functionVariableUsage() break; } - if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|=") ) + if ( Token::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|=") ) varUsage[ tok->strAt(2) ] = USAGE_DECLARE; - if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|=") ) + if ( Token::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|=") ) varUsage[ tok->strAt(3) ] = USAGE_DECLARE; - if ( TOKEN::Match(tok, "delete|return %var%") ) + if ( Token::Match(tok, "delete|return %var%") ) varUsage[ tok->strAt(1) ] |= USAGE_READ; - if ( TOKEN::Match(tok, "%var% =") ) + if ( Token::Match(tok, "%var% =") ) varUsage[ tok->str() ] |= USAGE_WRITE; - if ( TOKEN::Match(tok, "else %var% =") ) + if ( Token::Match(tok, "else %var% =") ) varUsage[ tok->strAt(1) ] |= USAGE_WRITE; - if ( TOKEN::Match(tok, ">>|& %var%") ) + if ( Token::Match(tok, ">>|& %var%") ) varUsage[ tok->strAt(1) ] |= USAGE_WRITE; - if ((TOKEN::Match(tok,"[(=&!]") || isOp(tok)) && TOKEN::Match(tok->next(), "%var%")) + if ((Token::Match(tok,"[(=&!]") || isOp(tok)) && Token::Match(tok->next(), "%var%")) varUsage[ tok->strAt(1) ] |= USAGE_READ; - if (TOKEN::Match(tok, "-=|+=|*=|/= %var%")) + if (Token::Match(tok, "-=|+=|*=|/= %var%")) varUsage[ tok->strAt(1) ] |= USAGE_READ; - if (TOKEN::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next()))) + if (Token::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next()))) varUsage[ tok->str() ] |= USAGE_READ; - if ( TOKEN::Match(tok, "[(,] %var% [,)]") ) + if ( Token::Match(tok, "[(,] %var% [,)]") ) varUsage[ tok->strAt(1) ] |= USAGE_WRITE; } @@ -909,6 +909,6 @@ void CheckOther::functionVariableUsage() } // Goto next executing scope.. - tok1 = TOKEN::findmatch( tok1->next(), ") const| {" ); + tok1 = Token::findmatch( tok1->next(), ") const| {" ); } } diff --git a/checkother.h b/checkother.h index 5cde4a41e..47d8b061f 100644 --- a/checkother.h +++ b/checkother.h @@ -74,7 +74,7 @@ public: #ifndef UNIT_TESTING private: #endif - void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] ); + void CheckVariableScope_LookupVar( const Token *tok1, const char varname[] ); // Redundant condition // if (haystack.find(needle) != haystack.end()) diff --git a/tasks.txt b/tasks.txt index db92e8bed..0bd851562 100644 --- a/tasks.txt +++ b/tasks.txt @@ -6,7 +6,6 @@ Optimisations * Create a function similar to TOKEN::Match. That first "compiles" a pattern. Refactoring - * Rename class "TOKEN" to "Token" * Split large functions into smaller functions. When possible, convert unit tests for these smaller functions * Use %varid% instead of %var1% or %var2% in pattern matches diff --git a/testmemleak.cpp b/testmemleak.cpp index a491268aa..016dad270 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -1148,22 +1148,22 @@ private: void class3() { - check( "class TOKEN;\n" + check( "class Token;\n" "\n" "class Tokenizer\n" "{\n" "private:\n" - " TOKEN *_tokens;\n" + " Token *_tokens;\n" "\n" "public:\n" " Tokenizer();\n" " ~Tokenizer();\n" - " void deleteTokens(TOKEN *tok);\n" + " void deleteTokens(Token *tok);\n" "};\n" "\n" "Tokenizer::Tokenizer()\n" "{\n" - " _tokens = new TOKEN;\n" + " _tokens = new Token;\n" "}\n" "\n" "Tokenizer::~Tokenizer()\n" @@ -1171,11 +1171,11 @@ private: " deleteTokens(_tokens);\n" "}\n" "\n" - "void Tokenizer::deleteTokens(TOKEN *tok)\n" + "void Tokenizer::deleteTokens(Token *tok)\n" "{\n" " while (tok)\n" " {\n" - " TOKEN *next = tok->next();\n" + " Token *next = tok->next();\n" " delete tok;\n" " tok = next;\n" " }\n" diff --git a/testmemleakmp.cpp b/testmemleakmp.cpp index b1ed65e7a..e7801376a 100644 --- a/testmemleakmp.cpp +++ b/testmemleakmp.cpp @@ -58,11 +58,11 @@ private: // Check.. Settings settings; CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this ); - TOKEN *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1); + Token *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1); // Compare tokens.. std::string s; - for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok; tok2; tok2 = tok2->next() ) s += tok2->str() + " "; ASSERT_EQUALS( "; } ", s ); } diff --git a/testsimplifytokens.cpp b/testsimplifytokens.cpp index e1f420614..9d2347a7f 100644 --- a/testsimplifytokens.cpp +++ b/testsimplifytokens.cpp @@ -48,7 +48,7 @@ private: tokenizer.setVarId(); tokenizer.simplifyTokenList(); std::string ret; - for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokenizer.tokens(); tok; tok = tok->next() ) { ret += tok->str() + " "; } diff --git a/testsuite.h b/testsuite.h index 504ab454c..50aed9be2 100644 --- a/testsuite.h +++ b/testsuite.h @@ -22,7 +22,7 @@ #include #include "errorlogger.h" -class TOKEN; +class Token; class TestFixture : public ErrorLogger { diff --git a/testtoken.cpp b/testtoken.cpp index 01ddfcf96..9fe525be8 100644 --- a/testtoken.cpp +++ b/testtoken.cpp @@ -37,11 +37,11 @@ private: void nextprevious() { - TOKEN *token = new TOKEN; + Token *token = new Token; token->str( "1" ); token->insertToken( "2" ); token->next()->insertToken( "3" ); - TOKEN *last = token->next()->next(); + Token *last = token->next()->next(); ASSERT_EQUALS( token->str(), "1" ); ASSERT_EQUALS( token->next()->str(), "2" ); ASSERT_EQUALS( token->next()->next()->str(), "3" ); diff --git a/testtokenize.cpp b/testtokenize.cpp index 6232ddfaa..76c9d51b1 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -70,7 +70,7 @@ private: } - bool cmptok(const char *expected[], const TOKEN *actual) + bool cmptok(const char *expected[], const Token *actual) { unsigned int i = 0; for (; expected[i] && actual; ++i, actual = actual->next()) @@ -224,7 +224,7 @@ private: tokenizer.simplifyTokenList(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { { ; } }"), ostr.str() ); } @@ -245,7 +245,7 @@ private: ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() ); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { ; } else { ; } }"), ostr.str() ); } @@ -265,7 +265,7 @@ private: ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() ); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { if ( b ) { } } }"), ostr.str() ); } @@ -285,7 +285,7 @@ private: ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() ); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { for ( ; ; ) { } } }"), ostr.str() ); } @@ -310,7 +310,7 @@ private: ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() ); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" char * foo ( ) { char * str = malloc ( 10 ) ; if ( somecondition ) { for ( ; ; ) { } } return str ; }"), ostr.str() ); } @@ -332,7 +332,7 @@ private: tokenizer.simplifyKnownVariables(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; if ( 10 ) ; }"), ostr.str() ); } @@ -355,7 +355,7 @@ private: tokenizer.simplifyKnownVariables(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() ); } @@ -381,7 +381,7 @@ private: tokenizer.simplifyKnownVariables(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) ; }"), ostr.str() ); } @@ -403,7 +403,7 @@ private: tokenizer.simplifyKnownVariables(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; if ( g ( a ) ) ; }"), ostr.str() ); } @@ -425,7 +425,7 @@ private: tokenizer.simplifyKnownVariables(); std::ostringstream ostr; - for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str() ); } @@ -433,20 +433,20 @@ private: void multiCompare() { // Test for found - ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "one" ) ); - ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "two" ) ); - ASSERT_EQUALS( 1, TOKEN::multiCompare( "verybig|two|", "two" ) ); + ASSERT_EQUALS( 1, Token::multiCompare( "one|two", "one" ) ); + ASSERT_EQUALS( 1, Token::multiCompare( "one|two", "two" ) ); + ASSERT_EQUALS( 1, Token::multiCompare( "verybig|two|", "two" ) ); // Test for empty string found - ASSERT_EQUALS( 0, TOKEN::multiCompare( "|one|two", "notfound" ) ); - ASSERT_EQUALS( 0, TOKEN::multiCompare( "one||two", "notfound" ) ); - ASSERT_EQUALS( 0, TOKEN::multiCompare( "one|two|", "notfound" ) ); + ASSERT_EQUALS( 0, Token::multiCompare( "|one|two", "notfound" ) ); + ASSERT_EQUALS( 0, Token::multiCompare( "one||two", "notfound" ) ); + ASSERT_EQUALS( 0, Token::multiCompare( "one|two|", "notfound" ) ); // Test for not found - ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "notfound" ) ); - ASSERT_EQUALS( -1, TOKEN::multiCompare( "verybig|two", "s" ) ); - ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "ne" ) ); - ASSERT_EQUALS( -1, TOKEN::multiCompare( "abc|def", "a" ) ); + ASSERT_EQUALS( -1, Token::multiCompare( "one|two", "notfound" ) ); + ASSERT_EQUALS( -1, Token::multiCompare( "verybig|two", "s" ) ); + ASSERT_EQUALS( -1, Token::multiCompare( "one|two", "ne" ) ); + ASSERT_EQUALS( -1, Token::multiCompare( "abc|def", "a" ) ); } void match1() @@ -461,7 +461,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% | %var%") ); + ASSERT_EQUALS( true, Token::Match(tokenizer.tokens(), "%var% | %var%") ); } // Match "%var% || %var%" @@ -474,7 +474,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% || %var%") ); + ASSERT_EQUALS( true, Token::Match(tokenizer.tokens(), "%var% || %var%") ); } } @@ -489,7 +489,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "!!else") ); + ASSERT_EQUALS( true, Token::Match(tokenizer.tokens(), "!!else") ); } { @@ -501,7 +501,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "if ; !!else") ); + ASSERT_EQUALS( true, Token::Match(tokenizer.tokens(), "if ; !!else") ); } { @@ -513,7 +513,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "if ; !!else") ); + ASSERT_EQUALS( true, Token::Match(tokenizer.tokens(), "if ; !!else") ); } { @@ -525,7 +525,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( false, TOKEN::Match(tokenizer.tokens(), "!!else") ); + ASSERT_EQUALS( false, Token::Match(tokenizer.tokens(), "!!else") ); } { @@ -537,7 +537,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); // Match.. - ASSERT_EQUALS( false, TOKEN::Match(tokenizer.tokens(), "if ; !!else") ); + ASSERT_EQUALS( false, Token::Match(tokenizer.tokens(), "if ; !!else") ); } } @@ -558,17 +558,17 @@ private: tokenizer.tokenize(istr, "test.cpp"); tokenizer.setVarId(); - for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokenizer.tokens(); tok; tok = tok->next() ) { if ( tok->str() != "i" ) ASSERT_EQUALS( 0, tok->varId() ); - else if ( TOKEN::Match(tok, "i = 1") ) + else if ( Token::Match(tok, "i = 1") ) ASSERT_EQUALS( 1, tok->varId() ); - else if ( TOKEN::Match(tok, "i = 2") ) + else if ( Token::Match(tok, "i = 2") ) ASSERT_EQUALS( 2, tok->varId() ); - else if ( TOKEN::Match(tok, "i = 3") ) + else if ( Token::Match(tok, "i = 3") ) ASSERT_EQUALS( 3, tok->varId() ); - else if ( TOKEN::Match(tok, "i = 4") ) + else if ( Token::Match(tok, "i = 4") ) ASSERT_EQUALS( 2, tok->varId() ); } } @@ -588,7 +588,7 @@ private: tokenizer.tokenize(istr, "test.cpp"); tokenizer.setVarId(); - for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokenizer.tokens(); tok; tok = tok->next() ) { if ( tok->str() == "abc" ) ASSERT_EQUALS( 1, tok->varId() ); diff --git a/token.cpp b/token.cpp index e1d7be183..b73eb5510 100644 --- a/token.cpp +++ b/token.cpp @@ -26,7 +26,7 @@ #include // isalpha, isdigit #endif -TOKEN::TOKEN() : +Token::Token() : _str(""), _cstr(0), _isName(false), @@ -40,12 +40,12 @@ TOKEN::TOKEN() : { } -TOKEN::~TOKEN() +Token::~Token() { std::free(_cstr); } -void TOKEN::str( const char s[] ) +void Token::str( const char s[] ) { _str = s; std::free(_cstr); @@ -60,18 +60,18 @@ void TOKEN::str( const char s[] ) _varId = 0; } -void TOKEN::deleteNext() +void Token::deleteNext() { - TOKEN *n = _next; + Token *n = _next; _next = n->next(); delete n; if (_next) _next->previous(this); } -const TOKEN *TOKEN::tokAt(int index) const +const Token *Token::tokAt(int index) const { - const TOKEN *tok = this; + const Token *tok = this; while (index>0 && tok) { tok = tok->next(); @@ -80,13 +80,13 @@ const TOKEN *TOKEN::tokAt(int index) const return tok; } -const char *TOKEN::strAt(int index) const +const char *Token::strAt(int index) const { - const TOKEN *tok = this->tokAt(index); + const Token *tok = this->tokAt(index); return tok ? tok->_cstr : ""; } -int TOKEN::multiCompare( const char *needle, const char *haystack ) +int Token::multiCompare( const char *needle, const char *haystack ) { bool emptyStringFound = false; bool findNextOr = false; @@ -135,7 +135,7 @@ int TOKEN::multiCompare( const char *needle, const char *haystack ) return -1; } -bool TOKEN::simpleMatch(const TOKEN *tok, const char pattern[]) +bool Token::simpleMatch(const Token *tok, const char pattern[]) { const char *current, *next; @@ -164,7 +164,7 @@ bool TOKEN::simpleMatch(const TOKEN *tok, const char pattern[]) return true; } -bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], unsigned int varid) +bool Token::Match(const Token *tok, const char pattern[], const char *varname1[], unsigned int varid) { const char *p = pattern; while ( *p ) @@ -323,22 +323,22 @@ bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[] return true; } -bool TOKEN::isName() const +bool Token::isName() const { return _isName; } -bool TOKEN::isNumber() const +bool Token::isNumber() const { return _isNumber; } -bool TOKEN::isBoolean() const +bool Token::isBoolean() const { return _isBoolean; } -bool TOKEN::isStandardType() const +bool Token::isStandardType() const { bool ret = false; const char *type[] = {"bool","char","short","int","long","float","double",0}; @@ -349,49 +349,49 @@ bool TOKEN::isStandardType() const //--------------------------------------------------------------------------- -const TOKEN *TOKEN::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]) +const Token *Token::findmatch(const Token *tok, const char pattern[], const char *varname1[]) { for ( ; tok; tok = tok->next()) { - if ( TOKEN::Match(tok, pattern, varname1) ) + if ( Token::Match(tok, pattern, varname1) ) return tok; } return 0; } -unsigned int TOKEN::varId() const +unsigned int Token::varId() const { return _varId; } -void TOKEN::varId( unsigned int id ) +void Token::varId( unsigned int id ) { _varId = id; } -TOKEN *TOKEN::next() const +Token *Token::next() const { return _next; } -void TOKEN::next( TOKEN *next ) +void Token::next( Token *next ) { _next = next; } -TOKEN *TOKEN::previous() const +Token *Token::previous() const { return _previous; } -void TOKEN::previous( TOKEN *previous ) +void Token::previous( Token *previous ) { _previous = previous; } -void TOKEN::insertToken( const char str[] ) +void Token::insertToken( const char str[] ) { - TOKEN *newToken = new TOKEN; + Token *newToken = new Token; newToken->str( str ); newToken->_linenr = _linenr; newToken->_fileIndex = _fileIndex; @@ -405,7 +405,7 @@ void TOKEN::insertToken( const char str[] ) newToken->previous( this ); } -void TOKEN::eraseTokens( TOKEN *begin, const TOKEN *end ) +void Token::eraseTokens( Token *begin, const Token *end ) { if ( ! begin ) return; @@ -416,27 +416,27 @@ void TOKEN::eraseTokens( TOKEN *begin, const TOKEN *end ) } } -unsigned int TOKEN::fileIndex() const +unsigned int Token::fileIndex() const { return _fileIndex; } -void TOKEN::fileIndex( unsigned int fileIndex ) +void Token::fileIndex( unsigned int fileIndex ) { _fileIndex = fileIndex; } -unsigned int TOKEN::linenr() const +unsigned int Token::linenr() const { return _linenr; } -void TOKEN::linenr( unsigned int linenr ) +void Token::linenr( unsigned int linenr ) { _linenr = linenr; } -void TOKEN::printOut( const char *title ) const +void Token::printOut( const char *title ) const { std::cout << std::endl << "###"; if ( title ) @@ -445,7 +445,7 @@ void TOKEN::printOut( const char *title ) const std::cout << "########"; std::cout << "###" << std::endl; - for( const TOKEN *t = this; t; t = t->next() ) + for( const Token *t = this; t; t = t->next() ) { std::cout << t->linenr() << ": " << t->str(); if ( t->varId() ) diff --git a/token.h b/token.h index 8f731a4f9..bea0e666f 100644 --- a/token.h +++ b/token.h @@ -16,16 +16,16 @@ * along with this program. If not, see -class TOKEN +class Token { public: - TOKEN(); - ~TOKEN(); + Token(); + ~Token(); void str( const char s[] ); const std::string &str() const @@ -50,7 +50,7 @@ public: * For example index 1 would return next token, and 2 * would return next from that one. */ - const TOKEN *tokAt(int index) const; + const Token *tokAt(int index) const; const char *strAt(int index) const; @@ -73,7 +73,7 @@ public: * @return true if given token matches with given pattern * false if given token does not match with given pattern */ - static bool simpleMatch(const TOKEN *tok, const char pattern[]); + static bool simpleMatch(const Token *tok, const char pattern[]); /** * Match given token (or list of tokens) to a pattern list. @@ -105,13 +105,13 @@ public: * @return true if given token matches with given pattern * false if given token does not match with given pattern */ - static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, unsigned int varid=0); + static bool Match(const Token *tok, const char pattern[], const char *varname1[]=0, unsigned int varid=0); bool isName() const; bool isNumber() const; bool isBoolean() const; bool isStandardType() const; - static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0); + static const Token *findmatch(const Token *tok, const char pattern[], const char *varname1[]=0); /** * Needle is build from multiple alternatives. If one of @@ -135,7 +135,7 @@ public: unsigned int fileIndex() const; void fileIndex( unsigned int fileIndex ); - TOKEN *next() const; + Token *next() const; /** @@ -145,7 +145,7 @@ public: * @param begin Tokens after this will be erased. * @param end Tokens before this will be erased. */ - static void eraseTokens( TOKEN *begin, const TOKEN *end ); + static void eraseTokens( Token *begin, const Token *end ); /** * Insert new token after this token. This function will handle @@ -154,7 +154,7 @@ public: */ void insertToken( const char str[] ); - TOKEN *previous() const; + Token *previous() const; unsigned int varId() const; @@ -169,8 +169,8 @@ public: void printOut( const char *title = 0 ) const; private: - void next( TOKEN *next ); - void previous( TOKEN *previous ); + void next( Token *next ); + void previous( Token *previous ); std::string _str; char * _cstr; @@ -178,10 +178,10 @@ private: bool _isNumber; bool _isBoolean; unsigned int _varId; - TOKEN *_next; - TOKEN *_previous; + Token *_next; + Token *_previous; unsigned int _fileIndex; unsigned int _linenr; }; -#endif // TOKEN_H +#endif // TokenH diff --git a/tokenize.cpp b/tokenize.cpp index 151f4edee..7870e82fd 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -61,7 +61,7 @@ Tokenizer::~Tokenizer() // Helper functions.. -TOKEN *Tokenizer::_gettok(TOKEN *tok, int index) +Token *Tokenizer::_gettok(Token *tok, int index) { while (tok && index>0) { @@ -73,7 +73,7 @@ TOKEN *Tokenizer::_gettok(TOKEN *tok, int index) //--------------------------------------------------------------------------- -const TOKEN *Tokenizer::tokens() const +const Token *Tokenizer::tokens() const { return _tokens; } @@ -163,7 +163,7 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi } else { - _tokens = new TOKEN; + _tokens = new Token; _tokensBack = _tokens; _tokensBack->str( str2.str().c_str() ); } @@ -206,7 +206,7 @@ int Tokenizer::SizeOfType(const char type[]) const // InsertTokens - Copy and insert tokens //--------------------------------------------------------------------------- -void Tokenizer::InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n) +void Tokenizer::InsertTokens(Token *dest, Token *src, unsigned int n) { while (n > 0) { @@ -492,7 +492,7 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) addtoken( CurrentToken.c_str(), lineno, FileIndex ); // Combine tokens.. - for (TOKEN *tok = _tokens; tok && tok->next(); tok = tok->next()) + for (Token *tok = _tokens; tok && tok->next(); tok = tok->next()) { static const char* combineWithNext[][3] = { @@ -533,14 +533,14 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) } // typedef.. - for ( TOKEN *tok = _tokens; tok; ) + for ( Token *tok = _tokens; tok; ) { - if ( TOKEN::Match(tok, "typedef %type% %type% ;") ) + if ( Token::Match(tok, "typedef %type% %type% ;") ) { const char *type1 = tok->strAt(1); const char *type2 = tok->strAt(2); - tok = const_cast(tok->tokAt(4)); - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + tok = const_cast(tok->tokAt(4)); + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == type2 ) tok2->str(type1); @@ -548,13 +548,13 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) continue; } - else if ( TOKEN::Match(tok, "typedef %type% %type% %type% ;") ) + else if ( Token::Match(tok, "typedef %type% %type% %type% ;") ) { const char *type1 = tok->strAt(1); const char *type2 = tok->strAt(2); const char *type3 = tok->strAt(3); - tok = const_cast(tok->tokAt(5)); - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + tok = const_cast(tok->tokAt(5)); + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == type3 ) { @@ -570,13 +570,13 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) } // Remove __asm.. - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if ( TOKEN::simpleMatch(tok->next(), "__asm {") ) + if ( Token::simpleMatch(tok->next(), "__asm {") ) { while ( tok->next() ) { - bool last = TOKEN::simpleMatch( tok->next(), "}" ); + bool last = Token::simpleMatch( tok->next(), "}" ); // Unlink and delete tok->next() tok->deleteNext(); @@ -589,15 +589,15 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) } // Remove "volatile" - while ( TOKEN::simpleMatch(_tokens, "volatile") ) + while ( Token::simpleMatch(_tokens, "volatile") ) { - TOKEN *tok = _tokens; + Token *tok = _tokens; _tokens = _tokens->next(); delete tok; } - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - while ( TOKEN::simpleMatch(tok->next(), "volatile") ) + while ( Token::simpleMatch(tok->next(), "volatile") ) { tok->deleteNext(); } @@ -610,20 +610,20 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) void Tokenizer::setVarId() { // Clear all variable ids - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) tok->varId( 0 ); // Set variable ids.. unsigned int _varId = 0; - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if ( ! TOKEN::Match(tok, "[;{}(] %type% %var%") ) + if ( ! Token::Match(tok, "[;{}(] %type% %var%") ) continue; // Determine name of declared variable.. const char *varname = 0; - TOKEN *tok2 = tok->next(); - while ( tok2 && ! TOKEN::Match( tok2, "[;[=(]" ) ) + Token *tok2 = tok->next(); + while ( tok2 && ! Token::Match( tok2, "[;[=(]" ) ) { if ( tok2->isName() ) varname = tok2->strAt(0); @@ -633,7 +633,7 @@ void Tokenizer::setVarId() } // Variable declaration found => Set variable ids - if ( TOKEN::Match(tok2, "[;[=]") && varname ) + if ( Token::Match(tok2, "[;[=]") && varname ) { ++_varId; int indentlevel = 0; @@ -661,18 +661,18 @@ void Tokenizer::setVarId() } // Struct/Class members - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { if ( tok->varId() != 0 && - TOKEN::Match(tok->next(), ". %var%") && + Token::Match(tok->next(), ". %var%") && tok->tokAt(2)->varId() == 0 ) { ++_varId; const std::string pattern(std::string(". ") + tok->strAt(2)); - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { - if ( tok2->varId() == tok->varId() && TOKEN::simpleMatch( tok2->next(), pattern.c_str() ) ) + if ( tok2->varId() == tok->varId() && Token::simpleMatch( tok2->next(), pattern.c_str() ) ) tok2->next()->next()->varId( _varId ); } } @@ -688,7 +688,7 @@ void Tokenizer::simplifyTokenList() { // Remove the keyword 'unsigned' - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { if (tok->next() && (tok->next()->str() == "unsigned")) { @@ -697,14 +697,14 @@ void Tokenizer::simplifyTokenList() } // Replace constants.. - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { - if (TOKEN::Match(tok,"const %type% %var% = %num% ;")) + if (Token::Match(tok,"const %type% %var% = %num% ;")) { const char *sym = tok->strAt(2); const char *num = tok->strAt(4); - for (TOKEN *tok2 = _gettok(tok,6); tok2; tok2 = tok2->next()) + for (Token *tok2 = _gettok(tok,6); tok2; tok2 = tok2->next()) { if (tok2->str() == sym) { @@ -723,14 +723,14 @@ void Tokenizer::simplifyTokenList() _typeSize["long"] = sizeof(long); _typeSize["float"] = sizeof(float); _typeSize["double"] = sizeof(double); - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { - if (TOKEN::Match(tok,"class %var%")) + if (Token::Match(tok,"class %var%")) { _typeSize[tok->strAt(1)] = 11; } - else if (TOKEN::Match(tok, "struct %var%")) + else if (Token::Match(tok, "struct %var%")) { _typeSize[tok->strAt(1)] = 13; } @@ -738,12 +738,12 @@ void Tokenizer::simplifyTokenList() // Replace 'sizeof(type)'.. - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { if (tok->str() != "sizeof") continue; - if (TOKEN::Match(tok, "sizeof ( %type% * )")) + if (Token::Match(tok, "sizeof ( %type% * )")) { std::ostringstream str; // 'sizeof(type *)' has the same size as 'sizeof(char *)' @@ -756,7 +756,7 @@ void Tokenizer::simplifyTokenList() } } - else if (TOKEN::Match(tok, "sizeof ( %type% )")) + else if (Token::Match(tok, "sizeof ( %type% )")) { const char *type = tok->strAt( 2); int size = SizeOfType(type); @@ -772,7 +772,7 @@ void Tokenizer::simplifyTokenList() } } - else if (TOKEN::Match(tok, "sizeof ( * %var% )")) + else if (Token::Match(tok, "sizeof ( * %var% )")) { tok->str("100"); for ( int i = 0; i < 4; ++i ) @@ -781,10 +781,10 @@ void Tokenizer::simplifyTokenList() } // Replace 'sizeof(var)' - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { // type array [ num ] ; - if ( ! TOKEN::Match(tok, "%type% %var% [ %num% ] ;") ) + if ( ! Token::Match(tok, "%type% %var% [ %num% ] ;") ) continue; int size = SizeOfType(tok->aaaa()); @@ -796,7 +796,7 @@ void Tokenizer::simplifyTokenList() // Replace 'sizeof(var)' with number int indentlevel = 0; - for ( TOKEN *tok2 = _gettok(tok,5); tok2; tok2 = tok2->next() ) + for ( Token *tok2 = _gettok(tok,5); tok2; tok2 = tok2->next() ) { if (tok2->str() == "{") { @@ -810,8 +810,8 @@ void Tokenizer::simplifyTokenList() break; } - // Todo: TOKEN::Match varname directly - else if (TOKEN::Match(tok2, "sizeof ( %var% )")) + // Todo: Token::Match varname directly + else if (Token::Match(tok2, "sizeof ( %var% )")) { if (strcmp(tok2->strAt(2), varname) == 0) { @@ -834,9 +834,9 @@ void Tokenizer::simplifyTokenList() // Simple calculations.. for ( bool done = false; !done; done = true ) { - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { - if (TOKEN::simpleMatch(tok->next(), "* 1") || TOKEN::simpleMatch(tok->next(), "1 *")) + if (Token::simpleMatch(tok->next(), "* 1") || Token::simpleMatch(tok->next(), "1 *")) { for (int i = 0; i < 2; i++) tok->deleteNext(); @@ -844,7 +844,7 @@ void Tokenizer::simplifyTokenList() } // (1-2) - if (TOKEN::Match(tok, "[[,(=<>] %num% [+-*/] %num% [],);=<>]")) + if (Token::Match(tok, "[[,(=<>] %num% [+-*/] %num% [],);=<>]")) { int i1 = atoi(tok->strAt(1)); int i2 = atoi(tok->strAt(3)); @@ -876,16 +876,16 @@ void Tokenizer::simplifyTokenList() // Replace "*(str + num)" => "str[num]" - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { if ( ! strchr(";{}(=<>", tok->aaaa0()) ) continue; - TOKEN *next = tok->next(); + Token *next = tok->next(); if ( ! next ) break; - if (TOKEN::Match(next, "* ( %var% + %num% )")) + if (Token::Match(next, "* ( %var% + %num% )")) { const char *str[4] = {"var","[","num","]"}; str[0] = tok->strAt(3); @@ -905,21 +905,21 @@ void Tokenizer::simplifyTokenList() // Split up variable declarations if possible.. - for (TOKEN *tok = _tokens; tok; tok = tok->next()) + for (Token *tok = _tokens; tok; tok = tok->next()) { - if ( ! TOKEN::Match(tok, "[{};]") ) + if ( ! Token::Match(tok, "[{};]") ) continue; - TOKEN *type0 = tok->next(); - if (!TOKEN::Match(type0, "%type%")) + Token *type0 = tok->next(); + if (!Token::Match(type0, "%type%")) continue; - if (TOKEN::Match(type0, "else|return")) + if (Token::Match(type0, "else|return")) continue; - TOKEN *tok2 = NULL; + Token *tok2 = NULL; unsigned int typelen = 0; - if ( TOKEN::Match(type0, "%type% %var% ,|=") ) + if ( Token::Match(type0, "%type% %var% ,|=") ) { if ( type0->next()->str() != "operator" ) { @@ -928,7 +928,7 @@ void Tokenizer::simplifyTokenList() } } - else if ( TOKEN::Match(type0, "%type% * %var% ,|=") ) + else if ( Token::Match(type0, "%type% * %var% ,|=") ) { if ( type0->next()->next()->str() != "operator" ) { @@ -937,25 +937,25 @@ void Tokenizer::simplifyTokenList() } } - else if ( TOKEN::Match(type0, "%type% %var% [ %num% ] ,|=") ) + else if ( Token::Match(type0, "%type% %var% [ %num% ] ,|=") ) { tok2 = _gettok(type0, 5); // The ',' token typelen = 1; } - else if ( TOKEN::Match(type0, "%type% * %var% [ %num% ] ,|=") ) + else if ( Token::Match(type0, "%type% * %var% [ %num% ] ,|=") ) { tok2 = _gettok(type0, 6); // The ',' token typelen = 1; } - else if ( TOKEN::Match(type0, "struct %type% %var% ,|=") ) + else if ( Token::Match(type0, "struct %type% %var% ,|=") ) { tok2 = _gettok(type0, 3); typelen = 2; } - else if ( TOKEN::Match(type0, "struct %type% * %var% ,|=") ) + else if ( Token::Match(type0, "struct %type% * %var% ,|=") ) { tok2 = _gettok(type0, 4); typelen = 2; @@ -972,7 +972,7 @@ void Tokenizer::simplifyTokenList() else { - TOKEN *eq = tok2; + Token *eq = tok2; int parlevel = 0; while (tok2) @@ -992,7 +992,7 @@ void Tokenizer::simplifyTokenList() else if ( parlevel==0 && strchr(";,",tok2->aaaa0()) ) { // "type var =" => "type var; var =" - TOKEN *VarTok = _gettok(type0,typelen); + Token *VarTok = _gettok(type0,typelen); if (VarTok->aaaa0()=='*') VarTok = VarTok->next(); InsertTokens(eq, VarTok, 2); @@ -1014,18 +1014,18 @@ void Tokenizer::simplifyTokenList() } // Replace NULL with 0.. - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { if ( tok->str() == "NULL" ) tok->str("0"); } // Replace pointer casts of 0.. "(char *)0" => "0" - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if ( TOKEN::Match(tok->next(), "( %type% * ) 0") || TOKEN::Match(tok->next(),"( %type% %type% * ) 0") ) + if ( Token::Match(tok->next(), "( %type% * ) 0") || Token::Match(tok->next(),"( %type% %type% * ) 0") ) { - while (!TOKEN::simpleMatch(tok->next(),"0")) + while (!Token::simpleMatch(tok->next(),"0")) tok->deleteNext(); } } @@ -1045,14 +1045,14 @@ void Tokenizer::simplifyTokenList() } //--------------------------------------------------------------------------- -const TOKEN *Tokenizer::findClosing( const TOKEN *tok, const char *start, const char *end ) +const Token *Tokenizer::findClosing( const Token *tok, const char *start, const char *end ) { if( !tok ) return 0; // Find the closing "}" int indentLevel = 0; - for ( const TOKEN *closing = tok->next(); closing; closing = closing->next() ) + for ( const Token *closing = tok->next(); closing; closing = closing->next() ) { if( closing->str() == start ) { @@ -1077,16 +1077,16 @@ bool Tokenizer::removeReduntantConditions() { bool ret = false; - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if (!TOKEN::simpleMatch(tok, "if")) + if (!Token::simpleMatch(tok, "if")) continue; - if (!TOKEN::Match(tok->tokAt(1), "( %bool% ) {")) + if (!Token::Match(tok->tokAt(1), "( %bool% ) {")) continue; // Find matching else - const TOKEN *elseTag = 0; + const Token *elseTag = 0; // Find the closing "}" elseTag = Tokenizer::findClosing( tok->tokAt( 4 ), "{", "}" ); @@ -1100,19 +1100,19 @@ bool Tokenizer::removeReduntantConditions() // Handle if with else if( elseTag && elseTag->str()=="else" ) { - if( TOKEN::simpleMatch( elseTag->next(), "if" ) ) + if( Token::simpleMatch( elseTag->next(), "if" ) ) { // Handle "else if" if( boolValue == false ) { // Convert "if( false ) {aaa;} else if() {bbb;}" => "if() {bbb;}" - TOKEN::eraseTokens( tok, elseTag->tokAt( 2 ) ); + Token::eraseTokens( tok, elseTag->tokAt( 2 ) ); ret = true; } else { // Keep first if, remove every else if and else after it - const TOKEN *lastTagInIf = elseTag->tokAt( 2 ); + const Token *lastTagInIf = elseTag->tokAt( 2 ); while( lastTagInIf ) { if( lastTagInIf->str() == "(" ) @@ -1123,15 +1123,15 @@ bool Tokenizer::removeReduntantConditions() lastTagInIf = Tokenizer::findClosing( lastTagInIf, "{", "}" ); lastTagInIf = lastTagInIf->next(); - if( !TOKEN::simpleMatch( lastTagInIf, "else" ) ) + if( !Token::simpleMatch( lastTagInIf, "else" ) ) break; lastTagInIf = lastTagInIf->next(); - if( TOKEN::simpleMatch( lastTagInIf, "if" ) ) + if( Token::simpleMatch( lastTagInIf, "if" ) ) lastTagInIf = lastTagInIf->next(); } - TOKEN::eraseTokens( elseTag->previous(), lastTagInIf ); + Token::eraseTokens( elseTag->previous(), lastTagInIf ); ret = true; } } @@ -1146,14 +1146,14 @@ bool Tokenizer::removeReduntantConditions() else tok->str( ";" ); - TOKEN::eraseTokens( tok, elseTag->tokAt( 1 ) ); + Token::eraseTokens( tok, elseTag->tokAt( 1 ) ); } else { - if( TOKEN::simpleMatch( elseTag->tokAt( 1 ), "{" ) ) + if( Token::simpleMatch( elseTag->tokAt( 1 ), "{" ) ) { // Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}" - const TOKEN *end = Tokenizer::findClosing( elseTag->tokAt( 1 ), "{", "}" ); + const Token *end = Tokenizer::findClosing( elseTag->tokAt( 1 ), "{", "}" ); if( !end ) { // Possibly syntax error in code @@ -1161,7 +1161,7 @@ bool Tokenizer::removeReduntantConditions() } // Remove the "else { aaa; }" - TOKEN::eraseTokens( elseTag->previous(), end->tokAt( 1 ) ); + Token::eraseTokens( elseTag->previous(), end->tokAt( 1 ) ); } // Remove "if( true )" @@ -1170,7 +1170,7 @@ bool Tokenizer::removeReduntantConditions() else tok->str( ";" ); - TOKEN::eraseTokens( tok, tok->tokAt(5) ); + Token::eraseTokens( tok, tok->tokAt(5) ); } ret = true; @@ -1188,7 +1188,7 @@ bool Tokenizer::removeReduntantConditions() else tok->str( ";" ); - TOKEN::eraseTokens( tok, elseTag ); + Token::eraseTokens( tok, elseTag ); } else { @@ -1198,7 +1198,7 @@ bool Tokenizer::removeReduntantConditions() else tok->str( ";" ); - TOKEN::eraseTokens( tok, tok->tokAt( 5 ) ); + Token::eraseTokens( tok, tok->tokAt( 5 ) ); } ret = true; @@ -1212,9 +1212,9 @@ bool Tokenizer::simplifyIfAddBraces() { bool ret = false; - for ( TOKEN *tok = _tokens; tok; tok = tok ? tok->next() : NULL ) + for ( Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL ) { - if ( TOKEN::Match(tok, "if|for|while (") ) + if ( Token::Match(tok, "if|for|while (") ) { // Goto the ending ')' int parlevel = 1; @@ -1228,14 +1228,14 @@ bool Tokenizer::simplifyIfAddBraces() } // ')' should be followed by '{' - if (!tok || TOKEN::simpleMatch(tok, ") {")) + if (!tok || Token::simpleMatch(tok, ") {")) continue; } else if ( tok->str() == "else" ) { // An else followed by an if or brace don't need to be processed further - if ( TOKEN::Match( tok, "else if|{" ) ) + if ( Token::Match( tok, "else if|{" ) ) continue; } @@ -1291,16 +1291,16 @@ bool Tokenizer::simplifyConditions() { bool ret = false; - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if (TOKEN::simpleMatch(tok, "( true &&") || TOKEN::simpleMatch(tok, "&& true &&") || TOKEN::simpleMatch(tok->next(), "&& true )")) + if (Token::simpleMatch(tok, "( true &&") || Token::simpleMatch(tok, "&& true &&") || Token::simpleMatch(tok->next(), "&& true )")) { tok->deleteNext(); tok->deleteNext(); ret = true; } - else if (TOKEN::simpleMatch(tok, "( false ||") || TOKEN::simpleMatch(tok, "|| false ||") || TOKEN::simpleMatch(tok->next(), "|| false )")) + else if (Token::simpleMatch(tok, "( false ||") || Token::simpleMatch(tok, "|| false ||") || Token::simpleMatch(tok->next(), "|| false )")) { tok->deleteNext(); tok->deleteNext(); @@ -1308,9 +1308,9 @@ bool Tokenizer::simplifyConditions() } // Change numeric constant in condition to "true" or "false" - const TOKEN *tok2 = tok->tokAt(2); + const Token *tok2 = tok->tokAt(2); if ((tok->str()=="(" || tok->str()=="&&" || tok->str()=="||") && - TOKEN::Match(tok->next(), "%num%") && + Token::Match(tok->next(), "%num%") && tok2 && (tok2->str()==")" || tok2->str()=="&&" || tok2->str()=="||")) { @@ -1319,11 +1319,11 @@ bool Tokenizer::simplifyConditions() } // Reduce "(%num% == %num%)" => "(true)"/"(false)" - const TOKEN *tok4 = tok->tokAt(4); + const Token *tok4 = tok->tokAt(4); if ( ! tok4 ) break; if ( (tok->str()=="&&" || tok->str()=="||" || tok->str()=="(") && - TOKEN::Match(tok->tokAt(1), "%num% %any% %num%") && + Token::Match(tok->tokAt(1), "%num% %any% %num%") && (tok4->str()=="&&" || tok4->str()=="||" || tok4->str()==")") ) { double op1 = (strstr(tok->strAt(1), "0x")) ? strtol(tok->strAt(1),0,16) : atof( tok->strAt(1) ); @@ -1365,9 +1365,9 @@ bool Tokenizer::simplifyConditions() bool Tokenizer::simplifyCasts() { bool ret = false; - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { - if ( TOKEN::Match(tok->next(), "( %type% * )") ) + if ( Token::Match(tok->next(), "( %type% * )") ) { tok->deleteNext(); tok->deleteNext(); @@ -1376,20 +1376,20 @@ bool Tokenizer::simplifyCasts() ret = true; } - else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <" ) ) + else if ( Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <" ) ) { while ( tok->next() && tok->next()->str() != ">" ) tok->deleteNext(); tok->deleteNext(); tok->deleteNext(); - TOKEN *tok2 = tok; + Token *tok2 = tok; int parlevel = 0; while ( tok2->next() && parlevel >= 0 ) { tok2 = tok2->next(); - if ( TOKEN::simpleMatch(tok2->next(), "(") ) + if ( Token::simpleMatch(tok2->next(), "(") ) ++parlevel; - else if ( TOKEN::simpleMatch(tok2->next(), ")") ) + else if ( Token::simpleMatch(tok2->next(), ")") ) --parlevel; } if (tok2->next()) @@ -1408,7 +1408,7 @@ bool Tokenizer::simplifyFunctionReturn() { bool ret = false; int indentlevel = 0; - for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) + for ( const Token *tok = tokens(); tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -1416,13 +1416,13 @@ bool Tokenizer::simplifyFunctionReturn() else if ( tok->str() == "}" ) --indentlevel; - else if ( indentlevel == 0 && TOKEN::Match(tok, "%var% ( ) { return %num% ; }") ) + else if ( indentlevel == 0 && Token::Match(tok, "%var% ( ) { return %num% ; }") ) { std::ostringstream pattern; pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]"; - for ( TOKEN *tok2 = _tokens; tok2; tok2 = tok2->next() ) + for ( Token *tok2 = _tokens; tok2; tok2 = tok2->next() ) { - if ( TOKEN::Match(tok2, pattern.str().c_str()) ) + if ( Token::Match(tok2, pattern.str().c_str()) ) { tok2 = tok2->next(); tok2->str( tok->strAt(5) ); @@ -1440,15 +1440,15 @@ bool Tokenizer::simplifyFunctionReturn() bool Tokenizer::simplifyKnownVariables() { bool ret = false; - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( Token *tok = _tokens; tok; tok = tok->next() ) { // Search for a block of code - if ( ! TOKEN::Match(tok, ") const| {") ) + if ( ! Token::Match(tok, ") const| {") ) continue; // parse the block of code.. int indentlevel = 0; - for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + for ( Token *tok2 = tok; tok2; tok2 = tok2->next() ) { if ( tok2->str() == "{" ) @@ -1461,17 +1461,17 @@ bool Tokenizer::simplifyKnownVariables() break; } - else if ( TOKEN::Match(tok2, "%var% = %num% ;") || - TOKEN::Match(tok2, "%var% = %bool% ;")) + else if ( Token::Match(tok2, "%var% = %num% ;") || + Token::Match(tok2, "%var% = %bool% ;")) { unsigned int varid = tok2->varId(); if( varid == 0 ) continue; - for ( TOKEN *tok3 = tok2->next(); tok3; tok3 = tok3->next() ) + for ( Token *tok3 = tok2->next(); tok3; tok3 = tok3->next() ) { // Perhaps it's a loop => bail out - if ( TOKEN::Match(tok3, "[{}]") ) + if ( Token::Match(tok3, "[{}]") ) break; // Variable is used somehow in a non-defined pattern => bail out @@ -1479,7 +1479,7 @@ bool Tokenizer::simplifyKnownVariables() break; // Replace variable with numeric constant.. - if ( TOKEN::Match(tok3, "if ( %varid% )", 0, varid) ) + if ( Token::Match(tok3, "if ( %varid% )", 0, varid) ) { tok3 = tok3->next()->next(); tok3->str( tok2->strAt(2) ); @@ -1501,7 +1501,7 @@ bool Tokenizer::simplifyKnownVariables() //--------------------------------------------------------------------------- -const TOKEN *Tokenizer::GetFunctionTokenByName( const char funcname[] ) const +const Token *Tokenizer::GetFunctionTokenByName( const char funcname[] ) const { for ( unsigned int i = 0; i < _functionList.size(); ++i ) { @@ -1521,7 +1521,7 @@ void Tokenizer::fillFunctionList() bool classfunc = false; int indentlevel = 0; - for ( const TOKEN *tok = _tokens; tok; tok = tok->next() ) + for ( const Token *tok = _tokens; tok; tok = tok->next() ) { if ( tok->str() == "{" ) ++indentlevel; @@ -1540,10 +1540,10 @@ void Tokenizer::fillFunctionList() else if ( tok->str() == "::" ) classfunc = true; - else if (TOKEN::Match(tok, "%var% (")) + else if (Token::Match(tok, "%var% (")) { // Check if this is the first token of a function implementation.. - for ( const TOKEN *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) + for ( const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) { if ( tok2->str() == ";" ) { @@ -1558,7 +1558,7 @@ void Tokenizer::fillFunctionList() else if ( tok2->str() == ")" ) { - if ( TOKEN::Match(tok2, ") const| {") ) + if ( Token::Match(tok2, ") const| {") ) { _functionList.push_back( tok ); tok = tok2; @@ -1625,11 +1625,11 @@ void Tokenizer::DeallocateTokens() _files.clear(); } -void Tokenizer::deleteTokens(TOKEN *tok) +void Tokenizer::deleteTokens(Token *tok) { while (tok) { - TOKEN *next = tok->next(); + Token *next = tok->next(); delete tok; tok = next; } @@ -1637,14 +1637,14 @@ void Tokenizer::deleteTokens(TOKEN *tok) //--------------------------------------------------------------------------- -const char *Tokenizer::getParameterName( const TOKEN *ftok, int par ) +const char *Tokenizer::getParameterName( const Token *ftok, int par ) { int _par = 1; for ( ; ftok; ftok = ftok->next()) { if ( ftok->str()=="," ) ++_par; - if ( par==_par && TOKEN::Match(ftok, "%var% [,)]") ) + if ( par==_par && Token::Match(ftok, "%var% [,)]") ) return ftok->aaaa(); } return NULL; @@ -1652,7 +1652,7 @@ const char *Tokenizer::getParameterName( const TOKEN *ftok, int par ) //--------------------------------------------------------------------------- -std::string Tokenizer::fileLine( const TOKEN *tok ) const +std::string Tokenizer::fileLine( const Token *tok ) const { std::ostringstream ostr; ostr << "[" << _files.at(tok->fileIndex()) << ":" << tok->linenr() << "]"; diff --git a/tokenize.h b/tokenize.h index 1b897ac1e..cc8d15992 100644 --- a/tokenize.h +++ b/tokenize.h @@ -60,13 +60,13 @@ public: // Helper functions for handling the tokens list.. - static void deleteTokens(TOKEN *tok); - static const char *getParameterName( const TOKEN *ftok, int par ); + static void deleteTokens(Token *tok); + static const char *getParameterName( const Token *ftok, int par ); static bool SameFileName( const char fname1[], const char fname2[] ); - std::string fileLine( const TOKEN *tok ) const; + std::string fileLine( const Token *tok ) const; // Return size. int SizeOfType(const char type[]) const; @@ -76,8 +76,8 @@ public: const std::vector *getFiles() const; void fillFunctionList(); - const TOKEN *GetFunctionTokenByName( const char funcname[] ) const; - const TOKEN *tokens() const; + const Token *GetFunctionTokenByName( const char funcname[] ) const; + const Token *tokens() const; #ifndef UNIT_TESTING @@ -98,7 +98,7 @@ private: * @param end e.g. "}" * @return The end tag that matches given parameter or 0 if not found. */ - static const TOKEN *findClosing( const TOKEN *tok, const char *start, const char *end ); + static const Token *findClosing( const Token *tok, const char *start, const char *end ); void Define(const char Name[], const char Value[]); @@ -145,16 +145,16 @@ private: */ bool simplifyKnownVariables(); - TOKEN *_gettok(TOKEN *tok, int index); + Token *_gettok(Token *tok, int index); - void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n); + void InsertTokens(Token *dest, Token *src, unsigned int n); - TOKEN *_tokensBack; + Token *_tokensBack; std::map _typeSize; - std::vector _functionList; + std::vector _functionList; std::vector _files; struct DefineSymbol * _dsymlist; - TOKEN *_tokens; + Token *_tokens; }; //---------------------------------------------------------------------------