Refactoring: at() changed to tokAt() and getstr() changed to strAt()

This commit is contained in:
Reijo Tomperi 2008-11-23 10:09:16 +00:00
parent 79854620c7
commit 0b2e7a0ef3
9 changed files with 178 additions and 177 deletions

View File

@ -74,7 +74,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Array index..
if ( TOKEN::Match(tok, "%var1% [ %num% ]", varname) )
{
const char *num = TOKEN::getstr(tok, 2 + varc);
const char *num = tok->strAt(2 + varc);
if (strtol(num, NULL, 10) >= size)
{
ReportError(tok->next, "Array index out of bounds");
@ -100,12 +100,12 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Array index..
if ( !TOKEN::Match(tok, "%var%") && !TOKEN::Match(tok,"[.&]") && TOKEN::Match(tok->next, "%var1% [ %num% ]", varname) )
{
const char *num = TOKEN::getstr(tok->next, 2 + varc);
const char *num = tok->next->strAt(2 + varc);
if (strtol(num, NULL, 10) >= size)
{
ReportError(tok->next, "Array index out of bounds");
}
tok = tok->at(4);
tok = tok->tokAt(4);
continue;
}
@ -121,7 +121,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
if ( TOKEN::Match( tok->next, "( %var1% , %num% , %num% )", varname ) ||
TOKEN::Match( tok->next, "( %var% , %var1% , %num% )", varname ) )
{
const char *num = TOKEN::getstr(tok, varc + 6);
const char *num = tok->strAt(varc + 6);
if ( atoi(num) > total_size )
{
ReportError(tok, "Buffer overrun");
@ -134,15 +134,15 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Loop..
if ( TOKEN::Match(tok, "for (") )
{
const TOKEN *tok2 = tok->at(2);
const TOKEN *tok2 = tok->tokAt(2);
// for - setup..
if ( TOKEN::Match(tok2, "%var% = 0 ;") )
tok2 = tok2->at(4);
tok2 = tok2->tokAt(4);
else if ( TOKEN::Match(tok2, "%type% %var% = 0 ;") )
tok2 = tok2->at(5);
tok2 = tok2->tokAt(5);
else if ( TOKEN::Match(tok2, "%type% %type% %var% = 0 ;") )
tok2 = tok2->at(6);
tok2 = tok2->tokAt(6);
else
continue;
@ -152,14 +152,14 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Get index variable and stopsize.
const char *strindex = tok2->str;
int value = (tok2->next->str[1] ? 1 : 0) + atoi(TOKEN::getstr(tok2, 2));
int value = (tok2->next->str[1] ? 1 : 0) + atoi(tok2->strAt(2));
if ( value <= size )
continue;
// Goto the end of the for loop..
while (tok2 && !TOKEN::Match(tok2,")"))
tok2 = tok2->next;
if (!(tok2->at(5)))
if (!(tok2->tokAt(5)))
break;
std::ostringstream pattern;
@ -197,7 +197,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
if ( TOKEN::Match(tok, "strcpy ( %var1% , %str% )", varname) )
{
int len = 0;
const char *str = TOKEN::getstr(tok, varc + 4 );
const char *str = tok->strAt(varc + 4 );
while ( *str )
{
if (*str=='\\')
@ -261,7 +261,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
continue;
// Parse head of function..
ftok = ftok->at(2);
ftok = ftok->tokAt(2);
parlevel = 1;
while ( ftok && parlevel == 1 && par >= 1 )
{
@ -325,15 +325,15 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable()
if (TOKEN::Match(tok, "%type% %var% [ %num% ] ;"))
{
varname[0] = TOKEN::getstr(tok,1);
size = strtoul(TOKEN::getstr(tok,3), NULL, 10);
varname[0] = tok->strAt(1);
size = strtoul(tok->strAt(3), NULL, 10);
type = tok->str;
}
else if (indentlevel > 0 && TOKEN::Match(tok, "[*;{}] %var% = new %type% [ %num% ]"))
{
varname[0] = TOKEN::getstr(tok,1);
size = strtoul(TOKEN::getstr(tok,6), NULL, 10);
type = TOKEN::getstr(tok, 4);
varname[0] = tok->strAt(1);
size = strtoul(tok->strAt(6), NULL, 10);
type = tok->strAt(4);
}
else
{
@ -346,7 +346,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable()
// The callstack is empty
CallStack.clear();
CheckBufferOverrun_CheckScope( tok->at(5), varname, size, total_size );
CheckBufferOverrun_CheckScope( tok->tokAt(5), varname, size, total_size );
}
}
}
@ -391,8 +391,8 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
continue;
const char *varname[3] = {0,0,0};
varname[1] = TOKEN::getstr(tok2, ivar);
int arrsize = atoi(TOKEN::getstr(tok2, ivar+2));
varname[1] = tok2->strAt(ivar);
int arrsize = atoi(tok2->strAt(ivar+2));
int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str);
if (total_size == 0)
continue;
@ -413,7 +413,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
if ( TOKEN::Match(tok4, ") {") )
{
const char *names[2] = {varname[1], 0};
CheckBufferOverrun_CheckScope( tok4->at(2), names, arrsize, total_size );
CheckBufferOverrun_CheckScope( tok4->tokAt(2), names, arrsize, total_size );
break;
}
}
@ -428,11 +428,11 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
// Declare variable: Fred fred1;
if ( TOKEN::Match( tok3->next, "%var% ;" ) )
varname[0] = TOKEN::getstr(tok3, 1);
varname[0] = tok3->strAt(1);
// Declare pointer: Fred *fred1
else if ( TOKEN::Match(tok3->next, "* %var% [,);=]") )
varname[0] = TOKEN::getstr(tok3, 2);
varname[0] = tok3->strAt(2);
else
continue;
@ -456,7 +456,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
// Function implementation..
if ( TOKEN::Match(tok3, ") {") )
{
CheckTok = tok3->at(2);
CheckTok = tok3->tokAt(2);
break;
}
@ -508,7 +508,7 @@ void CheckBufferOverrunClass::WarningDangerousFunctions()
_errorLogger->reportErr(ostr.str());
}
else if (TOKEN::Match(tok, "scanf (") && strcmp(TOKEN::getstr(tok,2),"\"%s\"") == 0)
else if (TOKEN::Match(tok, "scanf (") && strcmp(tok->strAt(2),"\"%s\"") == 0)
{
std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";

View File

@ -90,7 +90,7 @@ struct VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1)
// Pointer?
else if ( TOKEN::Match(next, "%type% * %var% ;") )
{
varname = TOKEN::getstr(next, 2);
varname = next->strAt(2);
}
if (varname)
@ -126,9 +126,9 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn
TOKEN::Match(tok, "class %var1% : %type% {", _classname) ) )
{
if ( TOKEN::Match(tok, "class %var% {") )
tok = tok->at(3);
tok = tok->tokAt(3);
else
tok = tok->at(5);
tok = tok->tokAt(5);
indentlevel = 1;
}
@ -261,7 +261,7 @@ void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN
// Skip "this->"
if ( TOKEN::Match(ftok, "this .") )
ftok = ftok->at(2);
ftok = ftok->tokAt(2);
// Clearing all variables..
if (TOKEN::Match(ftok,"memset ( this ,"))
@ -456,7 +456,7 @@ void CheckClass::CheckUnusedPrivateFunctions()
else if (priv && indent_level == 1)
{
if ( TOKEN::Match(tok, "typedef %type% (") )
tok = tok->at(2);
tok = tok->tokAt(2);
if (TOKEN::Match(tok, "%var% (") &&
!TOKEN::Match(tok,classname))
@ -555,15 +555,15 @@ void CheckClass::CheckMemset()
// Todo: Handle memcpy and memmove
const char *type = NULL;
if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )"))
type = TOKEN::getstr(tok, 8);
type = tok->strAt(8);
else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( %type% ) )"))
type = TOKEN::getstr(tok, 9);
type = tok->strAt(9);
else if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( struct %type% ) )"))
type = TOKEN::getstr(tok, 9);
type = tok->strAt(9);
else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( struct %type% ) )"))
type = TOKEN::getstr(tok, 10);
type = tok->strAt(10);
else if (TOKEN::Match(tok, "%type% ( %var% , %var% , sizeof ( %type% ) )"))
type = TOKEN::getstr(tok, 8);
type = tok->strAt(8);
// No type defined => The tokens didn't match
if (!(type && type[0]))
@ -591,7 +591,7 @@ void CheckClass::CheckMemset()
if (TOKEN::Match(tstruct, "std :: %type% %var% ;"))
{
std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on struct that contains a 'std::" << TOKEN::getstr(tstruct,2) << "'";
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on struct that contains a 'std::" << tstruct->strAt(2) << "'";
_errorLogger->reportErr(ostr.str());
break;
}

View File

@ -53,11 +53,11 @@ void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer )
const TOKEN *funcname = 0;
if ( TOKEN::Match( tok, "%type% %var% (" ) )
funcname = tok->at(1);
funcname = tok->tokAt(1);
else if ( TOKEN::Match(tok, "%type% * %var% (") )
funcname = tok->at(2);
else if ( TOKEN::Match(tok, "%type% :: %var% (") && !TOKEN::Match(tok, TOKEN::getstr(tok,2)) )
funcname = tok->at(2);
funcname = tok->tokAt(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)

View File

@ -130,21 +130,21 @@ void CheckHeaders::WarningIncludeHeader()
// Class or namespace declaration..
// --------------------------------------
if (TOKEN::Match(tok1,"class %var% {") || TOKEN::Match(tok1,"class %var% :") || TOKEN::Match(tok1,"namespace %var% {"))
classlist.push_back(TOKEN::getstr(tok1, 1));
classlist.push_back(tok1->strAt(1));
// Variable declaration..
// --------------------------------------
else if (TOKEN::Match(tok1, "%type% %var% ;") || TOKEN::Match(tok1, "%type% %var% ["))
namelist.push_back(TOKEN::getstr(tok1, 1));
namelist.push_back(tok1->strAt(1));
else if (TOKEN::Match(tok1, "%type% * %var% ;") || TOKEN::Match(tok1, "%type% * %var% ["))
namelist.push_back(TOKEN::getstr(tok1, 2));
namelist.push_back(tok1->strAt(2));
else if (TOKEN::Match(tok1, "const %type% %var% =") || TOKEN::Match(tok1, "const %type% %var% ["))
namelist.push_back(TOKEN::getstr(tok1, 2));
namelist.push_back(tok1->strAt(2));
else if (TOKEN::Match(tok1, "const %type% * %var% =") || TOKEN::Match(tok1, "const %type% * %var% ["))
namelist.push_back(TOKEN::getstr(tok1, 3));
namelist.push_back(tok1->strAt(3));
// enum..
// --------------------------------------
@ -162,22 +162,22 @@ void CheckHeaders::WarningIncludeHeader()
// function..
// --------------------------------------
else if (TOKEN::Match(tok1,"%type% %var% ("))
namelist.push_back(TOKEN::getstr(tok1, 1));
namelist.push_back(tok1->strAt(1));
else if (TOKEN::Match(tok1,"%type% * %var% ("))
namelist.push_back(TOKEN::getstr(tok1, 2));
namelist.push_back(tok1->strAt(2));
else if (TOKEN::Match(tok1,"const %type% %var% ("))
namelist.push_back(TOKEN::getstr(tok1, 2));
namelist.push_back(tok1->strAt(2));
else if (TOKEN::Match(tok1,"const %type% * %var% ("))
namelist.push_back(TOKEN::getstr(tok1, 3));
namelist.push_back(tok1->strAt(3));
// typedef..
// --------------------------------------
else if (strcmp(tok1->str,"typedef")==0)
{
if (strcmp(TOKEN::getstr(tok1,1),"enum")==0)
if (strcmp(tok1->strAt(1),"enum")==0)
continue;
int parlevel = 0;
while (tok1->next)
@ -213,7 +213,7 @@ void CheckHeaders::WarningIncludeHeader()
if ( TOKEN::Match(tok1, ": %var% {") || TOKEN::Match(tok1, ": %type% %var% {") )
{
std::string classname = TOKEN::getstr(tok1, (strcmp(TOKEN::getstr(tok1,2),"{")) ? 2 : 1);
std::string classname = tok1->strAt((strcmp(tok1->strAt(2),"{")) ? 2 : 1);
if (std::find(classlist.begin(),classlist.end(),classname)!=classlist.end())
{
Needed = true;

View File

@ -148,7 +148,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const
// Redundant condition..
if ( TOKEN::Match(tok, "if ( %var1% )", varnames) )
{
tok = tok->at(4);
tok = tok->tokAt(4);
if ( TOKEN::Match(tok,"{") )
tok = tok->next;
}
@ -223,7 +223,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
// Check if the function deallocates the variable..
while ( ftok && ! TOKEN::Match(ftok,"{") )
ftok = ftok->next;
TOKEN *func = getcode( ftok->at(1), callstack, parname, alloctype, dealloctype );
TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype );
simplifycode( func );
const char *ret = 0;
if (TOKEN::findmatch(func, "goto"))
@ -336,14 +336,14 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if (TOKEN::Match(tok, "[(;{}] %var1% =", varnames))
{
AllocType alloc = GetAllocationType(tok->at(3));
AllocType alloc = GetAllocationType(tok->tokAt(3));
// If "--all" hasn't been given, don't check classes..
if ( alloc == New && ! _settings._showAll )
{
if ( TOKEN::Match(tok->at(3), "new %type% [(;]") )
if ( TOKEN::Match(tok->tokAt(3), "new %type% [(;]") )
{
if ( isclass( TOKEN::getstr(tok, 4) ) )
if ( isclass( tok->strAt(4) ) )
alloc = No;
}
}
@ -376,9 +376,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
TOKEN::Match(tok, "if ( 0 != %var1% )", varnames) )
{
addtoken("if(var)");
tok = tok->at(3); // Make sure the "use" will not be added
tok = tok->tokAt(3); // Make sure the "use" will not be added
}
else if ( TOKEN::Match(tok, "if (") && notvar(tok->at(2), varnames) )
else if ( TOKEN::Match(tok, "if (") && notvar(tok->tokAt(2), varnames) )
{
addtoken("if(!var)");
}
@ -524,7 +524,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
continue;
// Remove the next token "do"
erase( tok2, tok2->at(2) );
erase( tok2, tok2->tokAt(2) );
tok2 = tok2->next;
// Find the end of the "do" block..
@ -577,7 +577,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Delete extra ";"
while (TOKEN::Match(tok2,"[;{}] ;"))
{
erase(tok2, tok2->at(2));
erase(tok2, tok2->tokAt(2));
done = false;
}
@ -585,21 +585,21 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
if ( TOKEN::Match(tok2->next, "{ }") )
{
tok2->next->setstr(";");
erase(tok2->next, tok2->at(3));
erase(tok2->next, tok2->tokAt(3));
done = false;
}
// Delete braces around a single instruction..
if ( TOKEN::Match(tok2->next, "{ %var% ; }") )
{
erase( tok2, tok2->at(2) );
erase( tok2->next->next, tok2->at(4) );
erase( tok2, tok2->tokAt(2) );
erase( tok2->next->next, tok2->tokAt(4) );
done = false;
}
if ( TOKEN::Match(tok2->next, "{ return use ; }") )
{
erase( tok2, tok2->at(2) );
erase( tok2->next->next->next, tok2->at(5) );
erase( tok2, tok2->tokAt(2) );
erase( tok2->next->next->next, tok2->tokAt(5) );
done = false;
}
@ -611,9 +611,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
TOKEN::Match(tok2,"[;{}] if(false) ;") ||
TOKEN::Match(tok2,"[;{}] ifv ;") )
{
if ( ! TOKEN::Match(tok2->at(3), "else") )
if ( ! TOKEN::Match(tok2->tokAt(3), "else") )
{
erase(tok2, tok2->at(3));
erase(tok2, tok2->tokAt(3));
done = false;
continue;
}
@ -623,85 +623,85 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// This may cause false positives
if (_settings._showAll &&
(TOKEN::Match(tok2, "[;{}] if dealloc ;") || TOKEN::Match(tok2, "[;{}] if use ;")) &&
!TOKEN::Match(tok2->at(4), "else"))
!TOKEN::Match(tok2->tokAt(4), "else"))
{
erase(tok2->next, tok2->at(3));
erase(tok2->next, tok2->tokAt(3));
done = false;
}
// Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->at(6),"else"))
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{
erase(tok2, tok2->at(5));
erase(tok2, tok2->tokAt(5));
done = false;
}
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
if (TOKEN::Match(tok2,"[;{}] if alloc ; else return ;"))
{
erase(tok2, tok2->at(2)); // Remove "if"
erase(tok2->next, tok2->at(5)); // Remove "; else return"
erase(tok2, tok2->tokAt(2)); // Remove "if"
erase(tok2->next, tok2->tokAt(5)); // Remove "; else return"
done = false;
}
// Replace "dealloc use ;" with "dealloc ;"
if ( TOKEN::Match(tok2, "dealloc use ;") )
{
erase(tok2, tok2->at(2));
erase(tok2, tok2->tokAt(2));
done = false;
}
// Reducing if..
if (TOKEN::Match(tok2,"if dealloc ; else") || TOKEN::Match(tok2,"if use ; else"))
{
erase(tok2, tok2->at(2));
erase(tok2, tok2->tokAt(2));
done = false;
}
if (TOKEN::Match(tok2,"[;{}] if { dealloc ; return ; }") && !TOKEN::Match(tok2->at(8),"else"))
if (TOKEN::Match(tok2,"[;{}] if { dealloc ; return ; }") && !TOKEN::Match(tok2->tokAt(8),"else"))
{
erase(tok2,tok2->at(8));
erase(tok2,tok2->tokAt(8));
done = false;
}
// Replace "loop ;" with ";"
if ( TOKEN::Match(tok2->next, "loop ;") )
{
erase(tok2, tok2->at(2));
erase(tok2, tok2->tokAt(2));
done = false;
}
// Replace "loop !var ;" with ";"
if ( TOKEN::Match(tok2->next, "loop !var ;") )
{
erase(tok2, tok2->at(4));
erase(tok2, tok2->tokAt(4));
done = false;
}
// Replace "loop !var alloc ;" with " alloc ;"
if ( TOKEN::Match(tok2->next, "loop !var alloc ;") )
{
erase(tok2, tok2->at(3));
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete if block in "alloc ; if(!var) return ;"
if ( TOKEN::Match(tok2, "alloc ; if(!var) return ;") )
{
erase(tok2, tok2->at(4));
erase(tok2, tok2->tokAt(4));
done = false;
}
// Delete second use in "use ; use ;"
while (TOKEN::Match(tok2, "[;{}] use ; use ;"))
{
erase(tok2, tok2->at(3));
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete second case in "case ; case ;"
while (TOKEN::Match(tok2, "case ; case ;"))
{
erase(tok2, tok2->at(3));
erase(tok2, tok2->tokAt(3));
done = false;
}
@ -711,7 +711,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Right now, I just handle if there are a few case and perhaps a default.
bool valid = false;
bool incase = false;
for ( const TOKEN * _tok = tok2->at(2); _tok; _tok = _tok->next )
for ( const TOKEN * _tok = tok2->tokAt(2); _tok; _tok = _tok->next )
{
if ( _tok->str[0] == '{' )
break;
@ -742,7 +742,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
{
done = false;
tok2->setstr(";");
erase( tok2, tok2->at(2) );
erase( tok2, tok2->tokAt(2) );
tok2 = tok2->next;
bool first = true;
while (TOKEN::Match(tok2,"case") || TOKEN::Match(tok2,"default"))
@ -820,42 +820,42 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
else if ( TOKEN::findmatch(tok, "alloc ; if continue ;") )
{
// MemoryLeak(Tokenizer::gettok(TOKEN::findmatch(tok, "alloc ; if continue ;"), 3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if continue ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if continue ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; if break ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if break ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if break ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; if return ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if return ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if return ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv continue ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv continue ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv continue ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv break ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv break ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv break ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv return ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv return ;"))->at(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv return ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; return ;") )
{
MemoryLeak((TOKEN::findmatch(tok,"alloc ; return ;"))->at(2), varname);
MemoryLeak((TOKEN::findmatch(tok,"alloc ; return ;"))->tokAt(2), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; alloc") )
{
MemoryLeak((TOKEN::findmatch(tok,"alloc ; alloc"))->at(2), varname);
MemoryLeak((TOKEN::findmatch(tok,"alloc ; alloc"))->tokAt(2), varname);
}
else if ( ! TOKEN::findmatch(tok,"dealloc") &&
@ -906,10 +906,10 @@ void CheckMemoryLeakClass::CheckMemoryLeak_InFunction()
if (indentlevel>0 && infunc)
{
if ( TOKEN::Match(tok, "[{};] %type% * %var% [;=]") )
CheckMemoryLeak_CheckScope( tok->next, TOKEN::getstr(tok, 3) );
CheckMemoryLeak_CheckScope( tok->next, tok->strAt(3) );
else if ( TOKEN::Match(tok, "[{};] %type% %type% * %var% [;=]") )
CheckMemoryLeak_CheckScope( tok->next, TOKEN::getstr(tok, 4) );
CheckMemoryLeak_CheckScope( tok->next, tok->strAt(4) );
}
}
}
@ -937,7 +937,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers()
else if ( indentlevel == 0 && TOKEN::Match(tok, "class %var% [{:]") )
{
std::vector<const char *> classname;
classname.push_back( TOKEN::getstr(tok, 1) );
classname.push_back( tok->strAt(1) );
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
}
}
@ -972,7 +972,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN
// Declaring subclass.. recursive checking
if ( TOKEN::Match(tok, "class %var% [{:]") )
{
classname.push_back( TOKEN::getstr(tok, 1) );
classname.push_back( tok->strAt(1) );
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
classname.pop_back();
}
@ -982,8 +982,8 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN
{
if ( TOKEN::IsName(tok->str) || strchr(";}", tok->str[0]) )
{
if (_settings._showAll || !isclass(TOKEN::getstr(tok,1)))
CheckMemoryLeak_ClassMembers_Variable( classname, TOKEN::getstr(tok, 3) );
if (_settings._showAll || !isclass(tok->strAt(1)))
CheckMemoryLeak_ClassMembers_Variable( classname, tok->strAt(3) );
}
}
}
@ -1047,7 +1047,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec
// Allocate..
if ( TOKEN::Match( tok, varname_eq.str().c_str() ) )
{
AllocType alloc = GetAllocationType( tok->at(2) );
AllocType alloc = GetAllocationType( tok->tokAt(2) );
if ( alloc != No )
{
std::list<const TOKEN *> callstack;

View File

@ -55,7 +55,7 @@ void CheckOther::WarningOldStylePointerCast()
// Is "type" a class?
const char *pattern[] = {"class","",NULL};
pattern[1] = TOKEN::getstr(tok, 1);
pattern[1] = tok->strAt(1);
if (!TOKEN::findtoken(_tokenizer->tokens(), pattern))
continue;
@ -169,13 +169,13 @@ void CheckOther::WarningRedundantCode()
if (TOKEN::Match(tok,"if ( %var% )"))
{
varname1 = TOKEN::getstr(tok, 2);
tok2 = tok->at(4);
varname1 = tok->strAt( 2);
tok2 = tok->tokAt(4);
}
else if (TOKEN::Match(tok,"if ( %var% != NULL )"))
{
varname1 = TOKEN::getstr(tok, 2);
tok2 = tok->at(6);
varname1 = tok->strAt( 2);
tok2 = tok->tokAt(6);
}
if (varname1==NULL || tok2==NULL)
@ -186,13 +186,13 @@ void CheckOther::WarningRedundantCode()
bool err = false;
if (TOKEN::Match(tok2,"delete %var% ;"))
err = (strcmp(TOKEN::getstr(tok2,1),varname1)==0);
err = (strcmp(tok2->strAt(1),varname1)==0);
else if (TOKEN::Match(tok2,"delete [ ] %var% ;"))
err = (strcmp(TOKEN::getstr(tok2,1),varname1)==0);
err = (strcmp(tok2->strAt(1),varname1)==0);
else if (TOKEN::Match(tok2,"free ( %var% )"))
err = (strcmp(TOKEN::getstr(tok2,2),varname1)==0);
err = (strcmp(tok2->strAt(2),varname1)==0);
else if (TOKEN::Match(tok2,"kfree ( %var% )"))
err = (strcmp(TOKEN::getstr(tok2,2),varname1)==0);
err = (strcmp(tok2->strAt(2),varname1)==0);
if (err)
{
@ -235,8 +235,8 @@ void CheckOther::WarningIf()
parlevel--;
if (parlevel<=0)
{
if (strcmp(TOKEN::getstr(tok2,1), ";") == 0 &&
strcmp(TOKEN::getstr(tok2,2), "else") != 0)
if (strcmp(tok2->strAt(1), ";") == 0 &&
strcmp(tok2->strAt(2), "else") != 0)
{
std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\"";
@ -262,15 +262,15 @@ void CheckOther::WarningIf()
if (!TOKEN::Match(tok,"%var% = %var% ; if ( %var%"))
continue;
if ( strcmp(TOKEN::getstr(tok, 9), ")") != 0 )
if ( strcmp(tok->strAt( 9), ")") != 0 )
continue;
// var1 = var2 ; if ( var3 cond var4 )
const char *var1 = TOKEN::getstr(tok, 0);
const char *var2 = TOKEN::getstr(tok, 2);
const char *var3 = TOKEN::getstr(tok, 6);
const char *cond = TOKEN::getstr(tok, 7);
const char *var4 = TOKEN::getstr(tok, 8);
const char *var1 = tok->strAt( 0);
const char *var2 = tok->strAt( 2);
const char *var3 = tok->strAt( 6);
const char *cond = tok->strAt( 7);
const char *var4 = tok->strAt( 8);
// Check that var3 is equal with either var1 or var2
if (strcmp(var1,var3) && strcmp(var2,var3))
@ -290,7 +290,7 @@ void CheckOther::WarningIf()
// we found the error. Report.
std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok->at(4)) << ": The condition is always ";
ostr << _tokenizer->fileLine(tok->tokAt(4)) << ": The condition is always ";
for (int i = 0; i < 6; i++)
{
if (strcmp(cond, p[i]) == 0)
@ -331,7 +331,7 @@ void CheckOther::InvalidFunctionUsage()
{
if ( TOKEN::Match(tok2, ", %num% )") )
{
int radix = atoi(TOKEN::getstr(tok2, 1));
int radix = atoi(tok2->strAt( 1));
if (!(radix==0 || (radix>=2 && radix<=36)))
{
std::ostringstream ostr;
@ -381,18 +381,18 @@ void CheckOther::CheckUnsignedDivision()
{
if ( TOKEN::Match(tok, "[{};(,] %type% %var% [;=,)]") )
{
const char *type = TOKEN::getstr(tok, 1);
const char *type = tok->strAt( 1);
if (strcmp(type,"char")==0 || strcmp(type,"short")==0 || strcmp(type,"int")==0)
varsign[TOKEN::getstr(tok,2)] = 's';
varsign[tok->strAt(2)] = 's';
}
else if ( TOKEN::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") )
varsign[TOKEN::getstr(tok,3)] = 'u';
varsign[tok->strAt(3)] = 'u';
else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next, "%var% / %var%"))
{
const char *varname1 = TOKEN::getstr(tok,1);
const char *varname2 = TOKEN::getstr(tok,3);
const char *varname1 = tok->strAt(1);
const char *varname2 = tok->strAt(3);
char sign1 = varsign[varname1];
char sign2 = varsign[varname2];
@ -407,7 +407,7 @@ void CheckOther::CheckUnsignedDivision()
else if (!TOKEN::Match(tok,"[).]") && TOKEN::Match(tok->next, "%var% / - %num%"))
{
const char *varname1 = TOKEN::getstr(tok,1);
const char *varname1 = tok->strAt(1);
char sign1 = varsign[varname1];
if ( sign1 == 'u' )
{
@ -419,7 +419,7 @@ void CheckOther::CheckUnsignedDivision()
else if (TOKEN::Match(tok, "[([=*/+-] - %num% / %var%"))
{
const char *varname2 = TOKEN::getstr(tok,4);
const char *varname2 = tok->strAt(4);
char sign2 = varsign[varname2];
if ( sign2 == 'u' )
{
@ -513,7 +513,7 @@ void CheckOther::CheckVariableScope()
if (TOKEN::Match(tok1, "%var% %var% ;") ||
TOKEN::Match(tok1, "%var% %var% =") )
{
CheckVariableScope_LookupVar( tok1, TOKEN::getstr(tok1, 1) );
CheckVariableScope_LookupVar( tok1, tok1->strAt( 1) );
}
}
}
@ -564,7 +564,7 @@ void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char var
}
else if ( strcmp(TOKEN::getstr(tok, 0), varname) == 0 )
else if ( strcmp(tok->strAt( 0), varname) == 0 )
{
if ( indentlevel == 0 || used1 )
return;
@ -601,7 +601,7 @@ void CheckOther::CheckConstantFunctionParameter()
if ( TOKEN::Match(tok,"[,(] const std :: %type% %var% [,)]") )
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << " " << TOKEN::getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(5) << " is passed by value, it could be passed by reference/pointer instead";
_errorLogger->reportErr( errmsg.str() );
}
@ -609,18 +609,18 @@ void CheckOther::CheckConstantFunctionParameter()
{
// Check if type is a struct or class.
const char *pattern[3] = {"class","type",0};
pattern[1] = TOKEN::getstr(tok, 2);
pattern[1] = tok->strAt( 2);
if ( TOKEN::findtoken(_tokenizer->tokens(), pattern) )
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << " " << TOKEN::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead";
_errorLogger->reportErr( errmsg.str() );
}
pattern[0] = "struct";
if ( TOKEN::findtoken(_tokenizer->tokens(), pattern) )
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << " " << TOKEN::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead";
_errorLogger->reportErr( errmsg.str() );
}
}
@ -644,19 +644,19 @@ void CheckOther::CheckStructMemberUsage()
if ( TOKEN::Match(tok,"}") )
structname = 0;
if ( TOKEN::Match(tok, "struct %type% {") )
structname = TOKEN::getstr(tok, 1);
structname = tok->strAt( 1);
if (structname && TOKEN::Match(tok, "[{;]"))
{
const char *varname = 0;
if (TOKEN::Match(tok->next, "%type% %var% [;[]"))
varname = TOKEN::getstr( tok, 2 );
varname = tok->strAt( 2 );
else if (TOKEN::Match(tok->next, "%type% %type% %var% [;[]"))
varname = TOKEN::getstr( tok, 2 );
varname = tok->strAt( 2 );
else if (TOKEN::Match(tok->next, "%type% * %var% [;[]"))
varname = TOKEN::getstr( tok, 3 );
varname = tok->strAt( 3 );
else if (TOKEN::Match(tok->next, "%type% %type% * %var% [;[]"))
varname = TOKEN::getstr( tok, 4 );
varname = tok->strAt( 4 );
else
continue;
@ -671,7 +671,7 @@ void CheckOther::CheckStructMemberUsage()
if (TOKEN::Match(tok2, ". %var%", varnames))
{
if ( strcmp("=", TOKEN::getstr(tok2,2)) == 0 )
if ( strcmp("=", tok2->strAt(2)) == 0 )
continue;
used = true;
break;
@ -704,7 +704,7 @@ void CheckOther::CheckCharVariable()
if ( TOKEN::Match(tok, "[{};(,] char %var% [;=,)]") )
{
const char *varname[2] = {0};
varname[0] = TOKEN::getstr(tok, 2);
varname[0] = tok->strAt( 2);
// Check usage of char variable..
int indentlevel = 0;
@ -764,14 +764,14 @@ void CheckOther::CheckIncompleteStatement()
if ( parlevel != 0 )
continue;
if ( !TOKEN::Match(tok,"#") && TOKEN::Match(tok->next,"; %str%") && !TOKEN::Match(tok->at(3), ",") )
if ( !TOKEN::Match(tok,"#") && 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->at(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";

View File

@ -64,7 +64,7 @@ void TOKEN::deleteNext()
delete n;
}
const TOKEN *TOKEN::at(int index) const
const TOKEN *TOKEN::tokAt(int index) const
{
const TOKEN *tok = this;
while (index>0 && tok)
@ -75,6 +75,11 @@ const TOKEN *TOKEN::at(int index) const
return tok;
}
const char *TOKEN::strAt(int index) const
{
const TOKEN *tok = this->tokAt(index);
return tok ? tok->str : "";
}
bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
{
@ -123,16 +128,16 @@ bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[]
for ( int i = 1; varname[i]; i++ )
{
if ( !(tok->at(2)) )
if ( !(tok->tokAt(2)) )
return false;
if ( strcmp(TOKEN::getstr(tok, 1), ".") )
if ( strcmp(tok->strAt( 1), ".") )
return false;
if ( strcmp(TOKEN::getstr(tok, 2), varname[i]) )
if ( strcmp(tok->strAt( 2), varname[i]) )
return false;
tok = tok->at(2);
tok = tok->tokAt(2);
}
}
@ -179,11 +184,6 @@ bool TOKEN::IsNumber(const char str[])
return bool(isdigit(str[0]) != 0);
}
const char *TOKEN::getstr(const TOKEN *tok, int index)
{
tok = tok->at(index);
return tok ? tok->str : "";
}
bool TOKEN::IsStandardType(const char str[])
{

View File

@ -42,12 +42,13 @@ public:
* For example index 1 would return next token, and 2
* would return next from that one.
*/
const TOKEN *at(int index) const;
const TOKEN *tokAt(int index) const;
const char *strAt(int index) const;
static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
static bool IsName(const char str[]);
static bool IsNumber(const char str[]);
static const char *getstr(const TOKEN *tok, int index);
static bool IsStandardType(const char str[]);
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);

View File

@ -537,8 +537,8 @@ void Tokenizer::TokenizeCode(std::istream &code, const unsigned int FileIndex)
{
if (TOKEN::Match(tok, "typedef %type% %type% ;"))
{
const char *type1 = TOKEN::getstr(tok, 1);
const char *type2 = TOKEN::getstr(tok, 2);
const char *type1 = tok->strAt( 1);
const char *type2 = tok->strAt( 2);
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if (tok2->str!=type1 && tok2->str!=type2 && strcmp(tok2->str,type2)==0)
@ -550,9 +550,9 @@ void Tokenizer::TokenizeCode(std::istream &code, const unsigned int FileIndex)
else if (TOKEN::Match(tok, "typedef %type% %type% %type% ;"))
{
const char *type1 = TOKEN::getstr(tok, 1);
const char *type2 = TOKEN::getstr(tok, 2);
const char *type3 = TOKEN::getstr(tok, 3);
const char *type1 = tok->strAt( 1);
const char *type2 = tok->strAt( 2);
const char *type3 = tok->strAt( 3);
TOKEN *tok2 = tok;
while ( ! TOKEN::Match(tok2, ";") )
@ -622,8 +622,8 @@ void Tokenizer::SimplifyTokenList()
{
if (TOKEN::Match(tok,"const %type% %var% = %num% ;"))
{
const char *sym = TOKEN::getstr(tok,2);
const char *num = TOKEN::getstr(tok,4);
const char *sym = tok->strAt(2);
const char *num = tok->strAt(4);
for (TOKEN *tok2 = _gettok(tok,6); tok2; tok2 = tok2->next)
{
@ -648,12 +648,12 @@ void Tokenizer::SimplifyTokenList()
{
if (TOKEN::Match(tok,"class %var%"))
{
TypeSize[TOKEN::getstr(tok,1)] = 11;
TypeSize[tok->strAt(1)] = 11;
}
else if (TOKEN::Match(tok, "struct %var%"))
{
TypeSize[TOKEN::getstr(tok,1)] = 13;
TypeSize[tok->strAt(1)] = 13;
}
}
@ -679,7 +679,7 @@ void Tokenizer::SimplifyTokenList()
else if (TOKEN::Match(tok, "sizeof ( %type% )"))
{
const char *type = TOKEN::getstr(tok, 2);
const char *type = tok->strAt( 2);
int size = SizeOfType(type);
if (size > 0)
{
@ -712,8 +712,8 @@ void Tokenizer::SimplifyTokenList()
if (size <= 0)
continue;
const char *varname = TOKEN::getstr(tok, 1);
int total_size = size * atoi( TOKEN::getstr(tok, 3) );
const char *varname = tok->strAt( 1);
int total_size = size * atoi( tok->strAt( 3) );
// Replace 'sizeof(var)' with number
int indentlevel = 0;
@ -734,7 +734,7 @@ void Tokenizer::SimplifyTokenList()
// Todo: TOKEN::Match varname directly
else if (TOKEN::Match(tok2, "sizeof ( %var% )"))
{
if (strcmp(TOKEN::getstr(tok2,2), varname) == 0)
if (strcmp(tok2->strAt(2), varname) == 0)
{
std::ostringstream str;
str << total_size;
@ -766,19 +766,19 @@ void Tokenizer::SimplifyTokenList()
// (1-2)
if (strchr("[,(=<>",tok->str[0]) &&
TOKEN::IsNumber(TOKEN::getstr(tok,1)) &&
strchr("+-*/",*(TOKEN::getstr(tok,2))) &&
TOKEN::IsNumber(TOKEN::getstr(tok,3)) &&
strchr("],);=<>",*(TOKEN::getstr(tok,4))) )
TOKEN::IsNumber(tok->strAt(1)) &&
strchr("+-*/",*(tok->strAt(2))) &&
TOKEN::IsNumber(tok->strAt(3)) &&
strchr("],);=<>",*(tok->strAt(4))) )
{
int i1 = atoi(TOKEN::getstr(tok,1));
int i2 = atoi(TOKEN::getstr(tok,3));
if ( i2 == 0 && *(TOKEN::getstr(tok,2)) == '/' )
int i1 = atoi(tok->strAt(1));
int i2 = atoi(tok->strAt(3));
if ( i2 == 0 && *(tok->strAt(2)) == '/' )
{
continue;
}
switch (*(TOKEN::getstr(tok,2)))
switch (*(tok->strAt(2)))
{
case '+': i1 += i2; break;
case '-': i1 -= i2; break;
@ -813,8 +813,8 @@ void Tokenizer::SimplifyTokenList()
if (TOKEN::Match(next, "* ( %var% + %num% )"))
{
const char *str[4] = {"var","[","num","]"};
str[0] = TOKEN::getstr(tok,3);
str[2] = TOKEN::getstr(tok,5);
str[0] = tok->strAt(3);
str[2] = tok->strAt(5);
for (int i = 0; i < 4; i++)
{
@ -997,7 +997,7 @@ bool Tokenizer::simplifyConditions()
}
// Change numeric constant in condition to "true" or "false"
const TOKEN *tok2 = tok->at(2);
const TOKEN *tok2 = tok->tokAt(2);
if ((TOKEN::Match(tok, "(") || TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||")) &&
TOKEN::Match(tok->next, "%num%") &&
(TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) )