From 2db69e60720d1774b1810b9e6c6dd739d0d6be49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Nov 2008 09:44:02 +0000 Subject: [PATCH] tokenizer: Added DeallocateTokens to the destructor so it's not necessary to cleanup manually --- testbufferoverrun.cpp | 2 - testcharvar.cpp | 2 - testconstructors.cpp | 2 - testdivision.cpp | 2 - testincompletestatement.cpp | 2 - testmemleak.cpp | 2 - testtokenize.cpp | 6 - testunusedprivfunc.cpp | 2 - testunusedvar.cpp | 2 - tokenize.cpp | 440 ++++++++++++++++++------------------ 10 files changed, 220 insertions(+), 242 deletions(-) diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index c14c052de..a4b8333d4 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -57,8 +57,6 @@ private: // Check for memory leaks.. CheckBufferOverrunClass checkBufferOverrun( &tokenizer, this ); checkBufferOverrun.CheckBufferOverrun(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/testcharvar.cpp b/testcharvar.cpp index b6250c5e4..f264e83be 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -60,8 +60,6 @@ private: // Check for memory leaks.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckCharVariable(); - - tokenizer.DeallocateTokens(); } void array_index() diff --git a/testconstructors.cpp b/testconstructors.cpp index 67268a59b..fdc7f4736 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -51,8 +51,6 @@ private: settings._checkCodingStyle = true; CheckClass checkClass( &tokenizer, settings, this ); checkClass.CheckConstructors(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/testdivision.cpp b/testdivision.cpp index 678c79929..9013e5ade 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -51,8 +51,6 @@ private: // Check for memory leaks.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckUnsignedDivision(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/testincompletestatement.cpp b/testincompletestatement.cpp index b0e246833..b224801a5 100644 --- a/testincompletestatement.cpp +++ b/testincompletestatement.cpp @@ -50,8 +50,6 @@ private: // Check for unused variables.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckIncompleteStatement(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/testmemleak.cpp b/testmemleak.cpp index 46c212e0e..960f37669 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -54,8 +54,6 @@ private: tokenizer.FillFunctionList(0); CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this ); checkMemoryLeak.CheckMemoryLeak(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/testtokenize.cpp b/testtokenize.cpp index 80cae1b5c..fcc66984a 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -82,8 +82,6 @@ private: // Compare.. ASSERT_EQUALS( true, cmptok(expected, tokenizer.tokens()) ); - - tokenizer.DeallocateTokens(); } @@ -99,8 +97,6 @@ private: // Expected result.. ASSERT_EQUALS( std::string(10000,'a'), std::string(tokenizer.tokens()->str) ); - - tokenizer.DeallocateTokens(); } @@ -135,8 +131,6 @@ private: // Compare.. ASSERT_EQUALS( true, cmptok(expected, tokenizer.tokens()) ); - - tokenizer.DeallocateTokens(); } diff --git a/testunusedprivfunc.cpp b/testunusedprivfunc.cpp index e62294b35..9ad065a24 100644 --- a/testunusedprivfunc.cpp +++ b/testunusedprivfunc.cpp @@ -57,8 +57,6 @@ private: settings._checkCodingStyle = true; CheckClass checkClass( &tokenizer, settings, this ); checkClass.CheckUnusedPrivateFunctions(); - - tokenizer.DeallocateTokens(); } diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 5a7528055..72a0623a5 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -49,8 +49,6 @@ private: // Check for unused variables.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckStructMemberUsage(); - - tokenizer.DeallocateTokens(); } void run() diff --git a/tokenize.cpp b/tokenize.cpp index 4bf88ef6b..a6bfb3c5d 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -28,8 +28,8 @@ #include #include -#include -#include +#include +#include #include #include #include // <- strtoul @@ -52,13 +52,13 @@ Tokenizer::Tokenizer(ErrorLogger *errorLogger) { _tokens = 0; tokens_back = 0; - dsymlist = 0; + dsymlist = 0; _errorLogger = errorLogger; } Tokenizer::~Tokenizer() { - + DeallocateTokens(); } //--------------------------------------------------------------------------- @@ -1315,219 +1315,219 @@ void Tokenizer::settings( const Settings &settings ) { _settings = settings; } - -// Deallocate lists.. -void Tokenizer::DeallocateTokens() -{ - deleteTokens( _tokens ); - _tokens = 0; - tokens_back = 0; - - while (dsymlist) - { - struct DefineSymbol *next = dsymlist->next; - free(dsymlist->name); - free(dsymlist->value); - delete dsymlist; - dsymlist = next; - } - - Files.clear(); -} - -void Tokenizer::deleteTokens(TOKEN *tok) -{ - while (tok) - { - TOKEN *next = tok->next; - delete tok; - tok = next; - } -} -//--------------------------------------------------------------------------- - -//--------------------------------------------------------------------------- - -const char *Tokenizer::getParameterName( const TOKEN *ftok, int par ) -{ - int _par = 1; - for ( ; ftok; ftok = ftok->next) - { - if ( Tokenizer::Match(ftok, ",") ) - ++_par; - if ( par==_par && Tokenizer::Match(ftok, "%var% [,)]") ) - return ftok->str; - } - return NULL; -} - -//--------------------------------------------------------------------------- - -const TOKEN *Tokenizer::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) -{ - for ( ; tok; tok = tok->next) - { - if ( Tokenizer::Match(tok, pattern, varname1, varname2) ) - return tok; - } - return 0; -} - -//--------------------------------------------------------------------------- - -std::string Tokenizer::fileLine( const TOKEN *tok ) -{ - std::ostringstream ostr; - ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]"; - return ostr.str(); -} - - -bool Tokenizer::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) -{ - if (!tok) - return false; - - const char *p = pattern; - while (*p) - { - // Skip spaces in pattern.. - while ( *p == ' ' ) - p++; - - // Extract token from pattern.. - char str[50]; - char *s = str; - while (*p && *p!=' ') - { - *s = *p; - s++; - p++; - } - *s = 0; - - // No token => Success! - if (str[0] == 0) - return true; - - // Any symbolname.. - if (strcmp(str,"%var%")==0 || strcmp(str,"%type%")==0) - { - if (!Tokenizer::IsName(tok->str)) - return false; - } - - // Variable name.. - else if (strcmp(str,"%var1%")==0 || strcmp(str,"%var2%")==0) - { - const char **varname = (strcmp(str,"%var1%")==0) ? varname1 : varname2; - - if ( ! varname ) - return false; - - if (strcmp(tok->str, varname[0]) != 0) - return false; - - for ( int i = 1; varname[i]; i++ ) - { - if ( ! Tokenizer::gettok(tok, 2) ) - return false; - - if ( strcmp(Tokenizer::getstr(tok, 1), ".") ) - return false; - - if ( strcmp(Tokenizer::getstr(tok, 2), varname[i]) ) - return false; - - tok = Tokenizer::gettok(tok, 2); - } - } - - else if (strcmp(str,"%num%")==0) - { - if ( ! Tokenizer::IsNumber(tok->str) ) - return false; - } - - - else if (strcmp(str,"%str%")==0) - { - if ( tok->str[0] != '\"' ) - return false; - } - - // [.. => search for a one-character token.. - else if (str[0]=='[' && strchr(str, ']') && tok->str[1] == 0) - { - *strrchr(str, ']') = 0; - if ( strchr( str + 1, tok->str[0] ) == 0 ) - return false; - } - - else if (strcmp(str, tok->str) != 0) - return false; - - tok = tok->next; - if (!tok && *p) - return false; - } - - // The end of the pattern has been reached and nothing wrong has been found - return true; -} - -//--------------------------------------------------------------------------- - -bool Tokenizer::SameFileName( const char fname1[], const char fname2[] ) -{ -#ifdef __linux__ - return bool( strcmp(fname1, fname2) == 0 ); -#endif -#ifdef __GNUC__ - return bool( strcasecmp(fname1, fname2) == 0 ); -#endif -#ifdef __BORLANDC__ - return bool( stricmp(fname1, fname2) == 0 ); -#endif -#ifdef _MSC_VER - return bool( _stricmp(fname1, fname2) == 0 ); -#endif -} - - -bool Tokenizer::IsName(const char str[]) -{ - return bool(str[0]=='_' || isalpha(str[0])); -} -//--------------------------------------------------------------------------- - -bool Tokenizer::IsNumber(const char str[]) -{ - return bool(isdigit(str[0]) != 0); -} -//--------------------------------------------------------------------------- - -bool Tokenizer::IsStandardType(const char str[]) -{ - if (!str) - return false; - bool Ret = false; - const char *type[] = {"bool","char","short","int","long","float","double",0}; - for (int i = 0; type[i]; i++) - Ret |= (strcmp(str,type[i])==0); - return Ret; -} -//--------------------------------------------------------------------------- - -bool Tokenizer::alwaysTrue( const TOKEN *tok ) -{ - return (Match(tok,"( 1 [|)]") | Match(tok,"( 1 ||") | - Match(tok,"( true [|)]") | Match(tok,"( true ||")); -} -//--------------------------------------------------------------------------- -bool Tokenizer::alwaysFalse( const TOKEN *tok ) -{ - return (Match(tok,"( 0 [&)]") | Match(tok,"( 0 &&") | - Match(tok,"( false [&)]") | Match(tok,"( false &&")); -} -//--------------------------------------------------------------------------- + +// Deallocate lists.. +void Tokenizer::DeallocateTokens() +{ + deleteTokens( _tokens ); + _tokens = 0; + tokens_back = 0; + + while (dsymlist) + { + struct DefineSymbol *next = dsymlist->next; + free(dsymlist->name); + free(dsymlist->value); + delete dsymlist; + dsymlist = next; + } + + Files.clear(); +} + +void Tokenizer::deleteTokens(TOKEN *tok) +{ + while (tok) + { + TOKEN *next = tok->next; + delete tok; + tok = next; + } +} +//--------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- + +const char *Tokenizer::getParameterName( const TOKEN *ftok, int par ) +{ + int _par = 1; + for ( ; ftok; ftok = ftok->next) + { + if ( Tokenizer::Match(ftok, ",") ) + ++_par; + if ( par==_par && Tokenizer::Match(ftok, "%var% [,)]") ) + return ftok->str; + } + return NULL; +} + +//--------------------------------------------------------------------------- + +const TOKEN *Tokenizer::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) +{ + for ( ; tok; tok = tok->next) + { + if ( Tokenizer::Match(tok, pattern, varname1, varname2) ) + return tok; + } + return 0; +} + +//--------------------------------------------------------------------------- + +std::string Tokenizer::fileLine( const TOKEN *tok ) +{ + std::ostringstream ostr; + ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]"; + return ostr.str(); +} + + +bool Tokenizer::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) +{ + if (!tok) + return false; + + const char *p = pattern; + while (*p) + { + // Skip spaces in pattern.. + while ( *p == ' ' ) + p++; + + // Extract token from pattern.. + char str[50]; + char *s = str; + while (*p && *p!=' ') + { + *s = *p; + s++; + p++; + } + *s = 0; + + // No token => Success! + if (str[0] == 0) + return true; + + // Any symbolname.. + if (strcmp(str,"%var%")==0 || strcmp(str,"%type%")==0) + { + if (!Tokenizer::IsName(tok->str)) + return false; + } + + // Variable name.. + else if (strcmp(str,"%var1%")==0 || strcmp(str,"%var2%")==0) + { + const char **varname = (strcmp(str,"%var1%")==0) ? varname1 : varname2; + + if ( ! varname ) + return false; + + if (strcmp(tok->str, varname[0]) != 0) + return false; + + for ( int i = 1; varname[i]; i++ ) + { + if ( ! Tokenizer::gettok(tok, 2) ) + return false; + + if ( strcmp(Tokenizer::getstr(tok, 1), ".") ) + return false; + + if ( strcmp(Tokenizer::getstr(tok, 2), varname[i]) ) + return false; + + tok = Tokenizer::gettok(tok, 2); + } + } + + else if (strcmp(str,"%num%")==0) + { + if ( ! Tokenizer::IsNumber(tok->str) ) + return false; + } + + + else if (strcmp(str,"%str%")==0) + { + if ( tok->str[0] != '\"' ) + return false; + } + + // [.. => search for a one-character token.. + else if (str[0]=='[' && strchr(str, ']') && tok->str[1] == 0) + { + *strrchr(str, ']') = 0; + if ( strchr( str + 1, tok->str[0] ) == 0 ) + return false; + } + + else if (strcmp(str, tok->str) != 0) + return false; + + tok = tok->next; + if (!tok && *p) + return false; + } + + // The end of the pattern has been reached and nothing wrong has been found + return true; +} + +//--------------------------------------------------------------------------- + +bool Tokenizer::SameFileName( const char fname1[], const char fname2[] ) +{ +#ifdef __linux__ + return bool( strcmp(fname1, fname2) == 0 ); +#endif +#ifdef __GNUC__ + return bool( strcasecmp(fname1, fname2) == 0 ); +#endif +#ifdef __BORLANDC__ + return bool( stricmp(fname1, fname2) == 0 ); +#endif +#ifdef _MSC_VER + return bool( _stricmp(fname1, fname2) == 0 ); +#endif +} + + +bool Tokenizer::IsName(const char str[]) +{ + return bool(str[0]=='_' || isalpha(str[0])); +} +//--------------------------------------------------------------------------- + +bool Tokenizer::IsNumber(const char str[]) +{ + return bool(isdigit(str[0]) != 0); +} +//--------------------------------------------------------------------------- + +bool Tokenizer::IsStandardType(const char str[]) +{ + if (!str) + return false; + bool Ret = false; + const char *type[] = {"bool","char","short","int","long","float","double",0}; + for (int i = 0; type[i]; i++) + Ret |= (strcmp(str,type[i])==0); + return Ret; +} +//--------------------------------------------------------------------------- + +bool Tokenizer::alwaysTrue( const TOKEN *tok ) +{ + return (Match(tok,"( 1 [|)]") | Match(tok,"( 1 ||") | + Match(tok,"( true [|)]") | Match(tok,"( true ||")); +} +//--------------------------------------------------------------------------- +bool Tokenizer::alwaysFalse( const TOKEN *tok ) +{ + return (Match(tok,"( 0 [&)]") | Match(tok,"( 0 &&") | + Match(tok,"( false [&)]") | Match(tok,"( false &&")); +} +//---------------------------------------------------------------------------