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.. // Array index..
if ( TOKEN::Match(tok, "%var1% [ %num% ]", varname) ) 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) if (strtol(num, NULL, 10) >= size)
{ {
ReportError(tok->next, "Array index out of bounds"); ReportError(tok->next, "Array index out of bounds");
@ -100,12 +100,12 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Array index.. // Array index..
if ( !TOKEN::Match(tok, "%var%") && !TOKEN::Match(tok,"[.&]") && TOKEN::Match(tok->next, "%var1% [ %num% ]", varname) ) 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) if (strtol(num, NULL, 10) >= size)
{ {
ReportError(tok->next, "Array index out of bounds"); ReportError(tok->next, "Array index out of bounds");
} }
tok = tok->at(4); tok = tok->tokAt(4);
continue; continue;
} }
@ -121,7 +121,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
if ( TOKEN::Match( tok->next, "( %var1% , %num% , %num% )", varname ) || if ( TOKEN::Match( tok->next, "( %var1% , %num% , %num% )", varname ) ||
TOKEN::Match( tok->next, "( %var% , %var1% , %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 ) if ( atoi(num) > total_size )
{ {
ReportError(tok, "Buffer overrun"); ReportError(tok, "Buffer overrun");
@ -134,15 +134,15 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Loop.. // Loop..
if ( TOKEN::Match(tok, "for (") ) if ( TOKEN::Match(tok, "for (") )
{ {
const TOKEN *tok2 = tok->at(2); const TOKEN *tok2 = tok->tokAt(2);
// for - setup.. // for - setup..
if ( TOKEN::Match(tok2, "%var% = 0 ;") ) if ( TOKEN::Match(tok2, "%var% = 0 ;") )
tok2 = tok2->at(4); tok2 = tok2->tokAt(4);
else if ( TOKEN::Match(tok2, "%type% %var% = 0 ;") ) 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 ;") ) else if ( TOKEN::Match(tok2, "%type% %type% %var% = 0 ;") )
tok2 = tok2->at(6); tok2 = tok2->tokAt(6);
else else
continue; continue;
@ -152,14 +152,14 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Get index variable and stopsize. // Get index variable and stopsize.
const char *strindex = tok2->str; 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 ) if ( value <= size )
continue; continue;
// Goto the end of the for loop.. // Goto the end of the for loop..
while (tok2 && !TOKEN::Match(tok2,")")) while (tok2 && !TOKEN::Match(tok2,")"))
tok2 = tok2->next; tok2 = tok2->next;
if (!(tok2->at(5))) if (!(tok2->tokAt(5)))
break; break;
std::ostringstream pattern; std::ostringstream pattern;
@ -197,7 +197,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
if ( TOKEN::Match(tok, "strcpy ( %var1% , %str% )", varname) ) if ( TOKEN::Match(tok, "strcpy ( %var1% , %str% )", varname) )
{ {
int len = 0; int len = 0;
const char *str = TOKEN::getstr(tok, varc + 4 ); const char *str = tok->strAt(varc + 4 );
while ( *str ) while ( *str )
{ {
if (*str=='\\') if (*str=='\\')
@ -261,7 +261,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
continue; continue;
// Parse head of function.. // Parse head of function..
ftok = ftok->at(2); ftok = ftok->tokAt(2);
parlevel = 1; parlevel = 1;
while ( ftok && parlevel == 1 && par >= 1 ) while ( ftok && parlevel == 1 && par >= 1 )
{ {
@ -325,15 +325,15 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable()
if (TOKEN::Match(tok, "%type% %var% [ %num% ] ;")) if (TOKEN::Match(tok, "%type% %var% [ %num% ] ;"))
{ {
varname[0] = TOKEN::getstr(tok,1); varname[0] = tok->strAt(1);
size = strtoul(TOKEN::getstr(tok,3), NULL, 10); size = strtoul(tok->strAt(3), NULL, 10);
type = tok->str; type = tok->str;
} }
else if (indentlevel > 0 && TOKEN::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) else if (indentlevel > 0 && TOKEN::Match(tok, "[*;{}] %var% = new %type% [ %num% ]"))
{ {
varname[0] = TOKEN::getstr(tok,1); varname[0] = tok->strAt(1);
size = strtoul(TOKEN::getstr(tok,6), NULL, 10); size = strtoul(tok->strAt(6), NULL, 10);
type = TOKEN::getstr(tok, 4); type = tok->strAt(4);
} }
else else
{ {
@ -346,7 +346,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable()
// The callstack is empty // The callstack is empty
CallStack.clear(); 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; continue;
const char *varname[3] = {0,0,0}; const char *varname[3] = {0,0,0};
varname[1] = TOKEN::getstr(tok2, ivar); varname[1] = tok2->strAt(ivar);
int arrsize = atoi(TOKEN::getstr(tok2, ivar+2)); int arrsize = atoi(tok2->strAt(ivar+2));
int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str); int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str);
if (total_size == 0) if (total_size == 0)
continue; continue;
@ -413,7 +413,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
if ( TOKEN::Match(tok4, ") {") ) if ( TOKEN::Match(tok4, ") {") )
{ {
const char *names[2] = {varname[1], 0}; 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; break;
} }
} }
@ -428,11 +428,11 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
// Declare variable: Fred fred1; // Declare variable: Fred fred1;
if ( TOKEN::Match( tok3->next, "%var% ;" ) ) if ( TOKEN::Match( tok3->next, "%var% ;" ) )
varname[0] = TOKEN::getstr(tok3, 1); varname[0] = tok3->strAt(1);
// Declare pointer: Fred *fred1 // Declare pointer: Fred *fred1
else if ( TOKEN::Match(tok3->next, "* %var% [,);=]") ) else if ( TOKEN::Match(tok3->next, "* %var% [,);=]") )
varname[0] = TOKEN::getstr(tok3, 2); varname[0] = tok3->strAt(2);
else else
continue; continue;
@ -456,7 +456,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
// Function implementation.. // Function implementation..
if ( TOKEN::Match(tok3, ") {") ) if ( TOKEN::Match(tok3, ") {") )
{ {
CheckTok = tok3->at(2); CheckTok = tok3->tokAt(2);
break; break;
} }
@ -508,7 +508,7 @@ void CheckBufferOverrunClass::WarningDangerousFunctions()
_errorLogger->reportErr(ostr.str()); _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; std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead"; 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? // Pointer?
else if ( TOKEN::Match(next, "%type% * %var% ;") ) else if ( TOKEN::Match(next, "%type% * %var% ;") )
{ {
varname = TOKEN::getstr(next, 2); varname = next->strAt(2);
} }
if (varname) if (varname)
@ -126,9 +126,9 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn
TOKEN::Match(tok, "class %var1% : %type% {", _classname) ) ) TOKEN::Match(tok, "class %var1% : %type% {", _classname) ) )
{ {
if ( TOKEN::Match(tok, "class %var% {") ) if ( TOKEN::Match(tok, "class %var% {") )
tok = tok->at(3); tok = tok->tokAt(3);
else else
tok = tok->at(5); tok = tok->tokAt(5);
indentlevel = 1; indentlevel = 1;
} }
@ -261,7 +261,7 @@ void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN
// Skip "this->" // Skip "this->"
if ( TOKEN::Match(ftok, "this .") ) if ( TOKEN::Match(ftok, "this .") )
ftok = ftok->at(2); ftok = ftok->tokAt(2);
// Clearing all variables.. // Clearing all variables..
if (TOKEN::Match(ftok,"memset ( this ,")) if (TOKEN::Match(ftok,"memset ( this ,"))
@ -456,7 +456,7 @@ void CheckClass::CheckUnusedPrivateFunctions()
else if (priv && indent_level == 1) else if (priv && indent_level == 1)
{ {
if ( TOKEN::Match(tok, "typedef %type% (") ) if ( TOKEN::Match(tok, "typedef %type% (") )
tok = tok->at(2); tok = tok->tokAt(2);
if (TOKEN::Match(tok, "%var% (") && if (TOKEN::Match(tok, "%var% (") &&
!TOKEN::Match(tok,classname)) !TOKEN::Match(tok,classname))
@ -555,15 +555,15 @@ void CheckClass::CheckMemset()
// Todo: Handle memcpy and memmove // Todo: Handle memcpy and memmove
const char *type = NULL; const char *type = NULL;
if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )")) 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% ) )")) 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% ) )")) 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% ) )")) 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% ) )")) 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 // No type defined => The tokens didn't match
if (!(type && type[0])) if (!(type && type[0]))
@ -591,7 +591,7 @@ void CheckClass::CheckMemset()
if (TOKEN::Match(tstruct, "std :: %type% %var% ;")) if (TOKEN::Match(tstruct, "std :: %type% %var% ;"))
{ {
std::ostringstream ostr; 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()); _errorLogger->reportErr(ostr.str());
break; break;
} }

View File

@ -53,11 +53,11 @@ void CheckFunctionUsage::parseTokens( const Tokenizer &tokenizer )
const TOKEN *funcname = 0; const TOKEN *funcname = 0;
if ( TOKEN::Match( tok, "%type% %var% (" ) ) if ( TOKEN::Match( tok, "%type% %var% (" ) )
funcname = tok->at(1); funcname = tok->tokAt(1);
else if ( TOKEN::Match(tok, "%type% * %var% (") ) else if ( TOKEN::Match(tok, "%type% * %var% (") )
funcname = tok->at(2); funcname = tok->tokAt(2);
else if ( TOKEN::Match(tok, "%type% :: %var% (") && !TOKEN::Match(tok, TOKEN::getstr(tok,2)) ) else if ( TOKEN::Match(tok, "%type% :: %var% (") && !TOKEN::Match(tok, tok->strAt(2)) )
funcname = tok->at(2); funcname = tok->tokAt(2);
// Check that ") {" is found.. // Check that ") {" is found..
for (const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next) for (const TOKEN *tok2 = funcname; tok2; tok2 = tok2->next)

View File

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

View File

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

View File

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

View File

@ -42,12 +42,13 @@ public:
* For example index 1 would return next token, and 2 * For example index 1 would return next token, and 2
* would return next from that one. * 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 Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
static bool IsName(const char str[]); static bool IsName(const char str[]);
static bool IsNumber(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 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 *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[]); 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% ;")) if (TOKEN::Match(tok, "typedef %type% %type% ;"))
{ {
const char *type1 = TOKEN::getstr(tok, 1); const char *type1 = tok->strAt( 1);
const char *type2 = TOKEN::getstr(tok, 2); const char *type2 = tok->strAt( 2);
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next ) for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{ {
if (tok2->str!=type1 && tok2->str!=type2 && strcmp(tok2->str,type2)==0) 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% ;")) else if (TOKEN::Match(tok, "typedef %type% %type% %type% ;"))
{ {
const char *type1 = TOKEN::getstr(tok, 1); const char *type1 = tok->strAt( 1);
const char *type2 = TOKEN::getstr(tok, 2); const char *type2 = tok->strAt( 2);
const char *type3 = TOKEN::getstr(tok, 3); const char *type3 = tok->strAt( 3);
TOKEN *tok2 = tok; TOKEN *tok2 = tok;
while ( ! TOKEN::Match(tok2, ";") ) while ( ! TOKEN::Match(tok2, ";") )
@ -622,8 +622,8 @@ void Tokenizer::SimplifyTokenList()
{ {
if (TOKEN::Match(tok,"const %type% %var% = %num% ;")) if (TOKEN::Match(tok,"const %type% %var% = %num% ;"))
{ {
const char *sym = TOKEN::getstr(tok,2); const char *sym = tok->strAt(2);
const char *num = TOKEN::getstr(tok,4); 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)
{ {
@ -648,12 +648,12 @@ void Tokenizer::SimplifyTokenList()
{ {
if (TOKEN::Match(tok,"class %var%")) if (TOKEN::Match(tok,"class %var%"))
{ {
TypeSize[TOKEN::getstr(tok,1)] = 11; TypeSize[tok->strAt(1)] = 11;
} }
else if (TOKEN::Match(tok, "struct %var%")) 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% )")) else if (TOKEN::Match(tok, "sizeof ( %type% )"))
{ {
const char *type = TOKEN::getstr(tok, 2); const char *type = tok->strAt( 2);
int size = SizeOfType(type); int size = SizeOfType(type);
if (size > 0) if (size > 0)
{ {
@ -712,8 +712,8 @@ void Tokenizer::SimplifyTokenList()
if (size <= 0) if (size <= 0)
continue; continue;
const char *varname = TOKEN::getstr(tok, 1); const char *varname = tok->strAt( 1);
int total_size = size * atoi( TOKEN::getstr(tok, 3) ); int total_size = size * atoi( tok->strAt( 3) );
// Replace 'sizeof(var)' with number // Replace 'sizeof(var)' with number
int indentlevel = 0; int indentlevel = 0;
@ -734,7 +734,7 @@ void Tokenizer::SimplifyTokenList()
// Todo: TOKEN::Match varname directly // Todo: TOKEN::Match varname directly
else if (TOKEN::Match(tok2, "sizeof ( %var% )")) 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; std::ostringstream str;
str << total_size; str << total_size;
@ -766,19 +766,19 @@ void Tokenizer::SimplifyTokenList()
// (1-2) // (1-2)
if (strchr("[,(=<>",tok->str[0]) && if (strchr("[,(=<>",tok->str[0]) &&
TOKEN::IsNumber(TOKEN::getstr(tok,1)) && TOKEN::IsNumber(tok->strAt(1)) &&
strchr("+-*/",*(TOKEN::getstr(tok,2))) && strchr("+-*/",*(tok->strAt(2))) &&
TOKEN::IsNumber(TOKEN::getstr(tok,3)) && TOKEN::IsNumber(tok->strAt(3)) &&
strchr("],);=<>",*(TOKEN::getstr(tok,4))) ) strchr("],);=<>",*(tok->strAt(4))) )
{ {
int i1 = atoi(TOKEN::getstr(tok,1)); int i1 = atoi(tok->strAt(1));
int i2 = atoi(TOKEN::getstr(tok,3)); int i2 = atoi(tok->strAt(3));
if ( i2 == 0 && *(TOKEN::getstr(tok,2)) == '/' ) if ( i2 == 0 && *(tok->strAt(2)) == '/' )
{ {
continue; continue;
} }
switch (*(TOKEN::getstr(tok,2))) switch (*(tok->strAt(2)))
{ {
case '+': i1 += i2; break; case '+': i1 += i2; break;
case '-': i1 -= i2; break; case '-': i1 -= i2; break;
@ -813,8 +813,8 @@ void Tokenizer::SimplifyTokenList()
if (TOKEN::Match(next, "* ( %var% + %num% )")) if (TOKEN::Match(next, "* ( %var% + %num% )"))
{ {
const char *str[4] = {"var","[","num","]"}; const char *str[4] = {"var","[","num","]"};
str[0] = TOKEN::getstr(tok,3); str[0] = tok->strAt(3);
str[2] = TOKEN::getstr(tok,5); str[2] = tok->strAt(5);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
@ -997,7 +997,7 @@ bool Tokenizer::simplifyConditions()
} }
// Change numeric constant in condition to "true" or "false" // 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, "||")) && if ((TOKEN::Match(tok, "(") || TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||")) &&
TOKEN::Match(tok->next, "%num%") && TOKEN::Match(tok->next, "%num%") &&
(TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) ) (TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) )