tokenizer: Added DeallocateTokens to the destructor so it's not necessary to cleanup manually

This commit is contained in:
Daniel Marjamäki 2008-11-22 09:44:02 +00:00
parent 9ce8918895
commit 2db69e6072
10 changed files with 220 additions and 242 deletions

View File

@ -57,8 +57,6 @@ private:
// Check for memory leaks..
CheckBufferOverrunClass checkBufferOverrun( &tokenizer, this );
checkBufferOverrun.CheckBufferOverrun();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -60,8 +60,6 @@ private:
// Check for memory leaks..
CheckOther checkOther( &tokenizer, this );
checkOther.CheckCharVariable();
tokenizer.DeallocateTokens();
}
void array_index()

View File

@ -51,8 +51,6 @@ private:
settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings, this );
checkClass.CheckConstructors();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -51,8 +51,6 @@ private:
// Check for memory leaks..
CheckOther checkOther( &tokenizer, this );
checkOther.CheckUnsignedDivision();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -50,8 +50,6 @@ private:
// Check for unused variables..
CheckOther checkOther( &tokenizer, this );
checkOther.CheckIncompleteStatement();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -54,8 +54,6 @@ private:
tokenizer.FillFunctionList(0);
CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this );
checkMemoryLeak.CheckMemoryLeak();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -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();
}

View File

@ -57,8 +57,6 @@ private:
settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings, this );
checkClass.CheckUnusedPrivateFunctions();
tokenizer.DeallocateTokens();
}

View File

@ -49,8 +49,6 @@ private:
// Check for unused variables..
CheckOther checkOther( &tokenizer, this );
checkOther.CheckStructMemberUsage();
tokenizer.DeallocateTokens();
}
void run()

View File

@ -28,8 +28,8 @@
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
#include <stdlib.h> // <- 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 &&"));
}
//---------------------------------------------------------------------------