Refactoring: Rename class "TOKEN" to "Token"

This commit is contained in:
Reijo Tomperi 2009-01-03 20:29:20 +00:00
parent 2f9c73ce82
commit 5d1d267624
21 changed files with 707 additions and 708 deletions

View File

@ -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 TOKEN *>::const_iterator it;
std::list<const Token *>::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";

View File

@ -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<const TOKEN *> _callStack;
std::list<const Token *> _callStack;
};
//---------------------------------------------------------------------------

View File

@ -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<std::string> &callstack)
void CheckClass::ClassChecking_VarList_Initialize(const Token *tok1, const Token *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &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<std::string> 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;

View File

@ -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<std::string> &callstack);
void ClassChecking_VarList_Initialize(const Token *tok1, const Token *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &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;

View File

@ -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;

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -57,16 +57,16 @@ private:
};
void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] );
void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname );
void CheckMemoryLeak_ClassMembers_ParseClass( const Token *tok1, std::vector<const char *> &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<const TOKEN *> 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<const TOKEN *> &callstack, const char varname[] );
const char * call_func( const TOKEN *tok, std::list<const TOKEN *> 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<const Token *> 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<const Token *> &callstack, const char varname[] );
const char * call_func( const Token *tok, std::list<const Token *> 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);
};
//---------------------------------------------------------------------------

View File

@ -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<std::string, char> 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| {" );
}
}

View File

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

View File

@ -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

View File

@ -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"

View File

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

View File

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

View File

@ -22,7 +22,7 @@
#include <sstream>
#include "errorlogger.h"
class TOKEN;
class Token;
class TestFixture : public ErrorLogger
{

View File

@ -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" );

View File

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

View File

@ -26,7 +26,7 @@
#include <ctype.h> // 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() )

34
token.h
View File

@ -16,16 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#ifndef TOKEN_H
#define TOKEN_H
#ifndef TokenH
#define TokenH
#include <string>
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

View File

@ -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<TOKEN*>(tok->tokAt(4));
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
tok = const_cast<Token*>(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<TOKEN*>(tok->tokAt(5));
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
tok = const_cast<Token*>(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() << "]";

View File

@ -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<std::string> *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<std::string, unsigned int> _typeSize;
std::vector<const TOKEN *> _functionList;
std::vector<const Token *> _functionList;
std::vector<std::string> _files;
struct DefineSymbol * _dsymlist;
TOKEN *_tokens;
Token *_tokens;
};
//---------------------------------------------------------------------------