Refactoring: Some global functions moved to Tokenizer class

This commit is contained in:
Reijo Tomperi 2008-11-20 20:54:52 +00:00
parent dd5abf2c05
commit 180e3b99e6
9 changed files with 153 additions and 147 deletions

View File

@ -51,8 +51,8 @@ void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])
std::ostringstream ostr; 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++ ) for ( it = CallStack.begin(); it != CallStack.end(); it++ )
ostr << FileLine(*it, _tokenizer ) << " -> "; ostr << _tokenizer->fileLine(*it ) << " -> ";
ostr << FileLine(tok, _tokenizer) << ": " << errmsg; ostr << _tokenizer->fileLine(tok) << ": " << errmsg;
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -401,7 +401,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
if ( Match(tok, "class") ) if ( Match(tok, "class") )
{ {
std::string func_pattern(structname + std::string(" :: %var% (")); std::string func_pattern(structname + std::string(" :: %var% ("));
const TOKEN *tok3 = findmatch(_tokenizer->tokens(), func_pattern.c_str()); const TOKEN *tok3 = Tokenizer::findmatch(_tokenizer->tokens(), func_pattern.c_str());
while ( tok3 ) while ( tok3 )
{ {
for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next ) for ( const TOKEN *tok4 = tok3; tok4; tok4 = tok4->next )
@ -416,7 +416,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable()
break; break;
} }
} }
tok3 = findmatch(tok3->next, func_pattern.c_str()); tok3 = Tokenizer::findmatch(tok3->next, func_pattern.c_str());
} }
} }
@ -503,14 +503,14 @@ void CheckBufferOverrunClass::WarningDangerousFunctions()
if (Match(tok, "gets (")) if (Match(tok, "gets ("))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Found 'gets'. You should use 'fgets' instead"; ostr << _tokenizer->fileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0) else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Found 'scanf'. You should use 'fgets' instead"; ostr << _tokenizer->fileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }

View File

@ -337,7 +337,7 @@ void CheckClass::CheckConstructors()
if ( varlist ) if ( varlist )
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok1, _tokenizer); ostr << _tokenizer->fileLine(tok1);
ostr << " The class '" << classname << "' has no constructor"; ostr << " The class '" << classname << "' has no constructor";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
@ -376,7 +376,7 @@ void CheckClass::CheckConstructors()
if (!var->init) if (!var->init)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(constructor_token, _tokenizer); ostr << _tokenizer->fileLine(constructor_token);
ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'"; ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
@ -575,7 +575,7 @@ void CheckClass::CheckMemset()
if (Tokenizer::findtoken(_tokenizer->tokens(),pattern1)) if (Tokenizer::findtoken(_tokenizer->tokens(),pattern1))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Using '" << tok->str << "' on class."; ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on class.";
ReportErr(ostr.str()); ReportErr(ostr.str());
continue; continue;
} }
@ -591,7 +591,7 @@ void CheckClass::CheckMemset()
if (Match(tstruct, "std :: %type% %var% ;")) if (Match(tstruct, "std :: %type% %var% ;"))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Using '" << tok->str << "' on struct that contains a 'std::" << Tokenizer::getstr(tstruct,2) << "'"; ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on struct that contains a 'std::" << Tokenizer::getstr(tstruct,2) << "'";
ReportErr(ostr.str()); ReportErr(ostr.str());
break; break;
} }
@ -612,7 +612,7 @@ void CheckClass::CheckOperatorEq1()
if (const TOKEN *tok = Tokenizer::findtoken(_tokenizer->tokens(),pattern)) if (const TOKEN *tok = Tokenizer::findtoken(_tokenizer->tokens(),pattern))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": 'operator=' should return something"; ostr << _tokenizer->fileLine(tok) << ": 'operator=' should return something";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }

View File

@ -56,7 +56,7 @@ void CheckHeaders::WarningHeaderWithImplementation()
if (Match(tok, ") {")) if (Match(tok, ") {"))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Found implementation in header"; ostr << _tokenizer->fileLine(tok) << ": Found implementation in header";
ReportErr(ostr.str()); ReportErr(ostr.str());
// Goto next file.. // Goto next file..
@ -242,7 +242,7 @@ void CheckHeaders::WarningIncludeHeader()
if (!Needed) if (!Needed)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(includetok, _tokenizer) << ": The included header '" << includefile << "' is not needed"; ostr << _tokenizer->fileLine(includetok) << ": The included header '" << includefile << "' is not needed";
if (NeedDeclaration) if (NeedDeclaration)
ostr << " (but a forward declaration is needed)"; ostr << " (but a forward declaration is needed)";
ReportErr(ostr.str()); ReportErr(ostr.str());

View File

@ -64,7 +64,7 @@ bool CheckMemoryLeakClass::isclass( const std::string &typestr )
std::ostringstream pattern; std::ostringstream pattern;
pattern << "struct " << typestr; pattern << "struct " << typestr;
if ( findmatch( _tokenizer->tokens(), pattern.str().c_str() ) ) if ( Tokenizer::findmatch( _tokenizer->tokens(), pattern.str().c_str() ) )
return false; return false;
return true; return true;
@ -208,7 +208,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
if ( Match(tok, "[,()] %var1% [,()]", varnames) ) if ( Match(tok, "[,()] %var1% [,()]", varnames) )
{ {
const TOKEN *ftok = _tokenizer->GetFunctionTokenByName(funcname); const TOKEN *ftok = _tokenizer->GetFunctionTokenByName(funcname);
const char *parname = GetParameterName( ftok, par ); const char *parname = Tokenizer::getParameterName( ftok, par );
if ( ! parname ) if ( ! parname )
return "use"; return "use";
// Check if the function deallocates the variable.. // Check if the function deallocates the variable..
@ -217,13 +217,13 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
TOKEN *func = getcode( Tokenizer::gettok(ftok,1), callstack, parname, alloctype, dealloctype ); TOKEN *func = getcode( Tokenizer::gettok(ftok,1), callstack, parname, alloctype, dealloctype );
simplifycode( func ); simplifycode( func );
const char *ret = 0; const char *ret = 0;
if (findmatch(func, "goto")) if (Tokenizer::findmatch(func, "goto"))
ret = 0; // TODO : "goto" isn't handled well ret = 0; // TODO : "goto" isn't handled well
else if (findmatch(func, "use")) else if (Tokenizer::findmatch(func, "use"))
ret = "use"; ret = "use";
else if (findmatch(func, "dealloc")) else if (Tokenizer::findmatch(func, "dealloc"))
ret = "dealloc"; ret = "dealloc";
deleteTokens(func); Tokenizer::deleteTokens(func);
return ret; return ret;
} }
} }
@ -237,8 +237,8 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<con
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok ) for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok )
errmsg << FileLine(*tok, _tokenizer) << " -> "; errmsg << _tokenizer->fileLine(*tok) << " -> ";
errmsg << FileLine(Tok1, _tokenizer) << ": Mismatching allocation and deallocation: " << varname; errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -246,7 +246,7 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<con
void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] ) void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok, _tokenizer) << ": Memory leak: " << varname; errmsg << _tokenizer->fileLine(tok) << ": Memory leak: " << varname;
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -778,69 +778,69 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
// If the variable is not allocated at all => no memory leak // If the variable is not allocated at all => no memory leak
if (findmatch(tok, "alloc") == 0) if (Tokenizer::findmatch(tok, "alloc") == 0)
{ {
deleteTokens(tok); Tokenizer::deleteTokens(tok);
return; return;
} }
// TODO : handle "goto" // TODO : handle "goto"
if (findmatch(tok, "goto")) if (Tokenizer::findmatch(tok, "goto"))
{ {
deleteTokens(tok); Tokenizer::deleteTokens(tok);
return; return;
} }
simplifycode( tok ); simplifycode( tok );
if ( findmatch(tok, "loop alloc ;") ) if ( Tokenizer::findmatch(tok, "loop alloc ;") )
{ {
MemoryLeak(findmatch(tok, "loop alloc ;"), varname); MemoryLeak(Tokenizer::findmatch(tok, "loop alloc ;"), varname);
} }
else if ( findmatch(tok, "alloc ; if continue ;") ) else if ( Tokenizer::findmatch(tok, "alloc ; if continue ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if continue ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if continue ;"), 3), varname);
} }
else if ( findmatch(tok, "alloc ; if break ;") ) else if ( Tokenizer::findmatch(tok, "alloc ; if break ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if break ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if break ;"), 3), varname);
} }
else if ( findmatch(tok, "alloc ; if return ;") ) else if ( Tokenizer::findmatch(tok, "alloc ; if return ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if return ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if return ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv continue ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv continue ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv break ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv break ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv return ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv return ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname);
} }
else if ( findmatch(tok, "alloc ; return ;") ) else if ( Tokenizer::findmatch(tok, "alloc ; return ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok,"alloc ; return ;"),2), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok,"alloc ; return ;"),2), varname);
} }
else if ( findmatch(tok, "alloc ; alloc") ) else if ( Tokenizer::findmatch(tok, "alloc ; alloc") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok,"alloc ; alloc"),2), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok,"alloc ; alloc"),2), varname);
} }
else if ( ! findmatch(tok,"dealloc") && else if ( ! Tokenizer::findmatch(tok,"dealloc") &&
! findmatch(tok,"use") && ! Tokenizer::findmatch(tok,"use") &&
! findmatch(tok,"return use ;") ) ! Tokenizer::findmatch(tok,"return use ;") )
{ {
const TOKEN *last = tok; const TOKEN *last = tok;
while (last->next) while (last->next)
@ -848,7 +848,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
MemoryLeak(last, varname); MemoryLeak(last, varname);
} }
deleteTokens(tok); Tokenizer::deleteTokens(tok);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -60,7 +60,7 @@ void CheckOther::WarningOldStylePointerCast()
continue; continue;
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": C-style pointer casting"; ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -84,7 +84,7 @@ void CheckOther::WarningIsDigit()
if (err) if (err)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": The condition can be simplified; use 'isdigit'"; ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isdigit'";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -113,7 +113,7 @@ void CheckOther::WarningIsAlpha()
if (err) if (err)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": The condition can be simplified; use 'isupper'"; ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isupper'";
ReportErr(ostr.str()); ReportErr(ostr.str());
continue; continue;
} }
@ -126,7 +126,7 @@ void CheckOther::WarningIsAlpha()
if (err) if (err)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": The condition can be simplified; use 'islower'"; ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'islower'";
ReportErr(ostr.str()); ReportErr(ostr.str());
continue; continue;
} }
@ -143,7 +143,7 @@ void CheckOther::WarningIsAlpha()
if (err) if (err)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": The condition can be simplified; use 'isalpha'"; ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isalpha'";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -197,7 +197,7 @@ void CheckOther::WarningRedundantCode()
if (err) if (err)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Redundant condition. It is safe to deallocate a NULL pointer"; ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -239,7 +239,7 @@ void CheckOther::WarningIf()
strcmp(Tokenizer::getstr(tok2,2), "else") != 0) strcmp(Tokenizer::getstr(tok2,2), "else") != 0)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Found \"if (condition);\""; ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\"";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
break; break;
@ -290,7 +290,7 @@ void CheckOther::WarningIf()
// we found the error. Report. // we found the error. Report.
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(Tokenizer::gettok(tok,4), _tokenizer) << ": The condition is always "; ostr << _tokenizer->fileLine(Tokenizer::gettok(tok,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)
@ -335,7 +335,7 @@ void CheckOther::InvalidFunctionUsage()
if (!(radix==0 || (radix>=2 && radix<=36))) if (!(radix==0 || (radix>=2 && radix<=36)))
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok2, _tokenizer) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36"; ostr << _tokenizer->fileLine(tok2) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -361,7 +361,7 @@ void CheckOther::CheckIfAssignment()
Match(tok, "if ( %var% = %var% )") ) Match(tok, "if ( %var% = %var% )") )
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok, _tokenizer) << ": Possible bug. Should it be '==' instead of '='?"; ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -400,7 +400,7 @@ void CheckOther::CheckUnsignedDivision()
{ {
// One of the operands are signed, the other is unsigned.. // One of the operands are signed, the other is unsigned..
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok->next, _tokenizer) << ": Warning: Division with signed and unsigned operators"; ostr << _tokenizer->fileLine(tok->next) << ": Warning: Division with signed and unsigned operators";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -412,7 +412,7 @@ void CheckOther::CheckUnsignedDivision()
if ( sign1 == 'u' ) if ( sign1 == 'u' )
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok->next, _tokenizer) << ": Unsigned division. The result will be wrong."; ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -424,7 +424,7 @@ void CheckOther::CheckUnsignedDivision()
if ( sign2 == 'u' ) if ( sign2 == 'u' )
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok->next, _tokenizer) << ": Unsigned division. The result will be wrong."; ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
} }
@ -584,7 +584,7 @@ void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char var
// Warning if "used" is true // Warning if "used" is true
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok1, _tokenizer) << " The scope of the variable '" << varname << "' can be limited"; errmsg << _tokenizer->fileLine(tok1) << " The scope of the variable '" << varname << "' can be limited";
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -601,7 +601,7 @@ void CheckOther::CheckConstantFunctionParameter()
if ( Match(tok,"[,(] const std :: %type% %var% [,)]") ) if ( Match(tok,"[,(] const std :: %type% %var% [,)]") )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok, _tokenizer) << " " << Tokenizer::getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead"; errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
@ -613,14 +613,14 @@ void CheckOther::CheckConstantFunctionParameter()
if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) ) if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok, _tokenizer) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead"; errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
pattern[0] = "struct"; pattern[0] = "struct";
if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) ) if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok, _tokenizer) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead"; errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
} }
@ -681,7 +681,7 @@ void CheckOther::CheckStructMemberUsage()
if ( ! used ) if ( ! used )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok, _tokenizer) << ": struct member '" << structname << "::" << varname << "' is never read"; errmsg << _tokenizer->fileLine(tok) << ": struct member '" << structname << "::" << varname << "' is never read";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
} }
} }
@ -723,7 +723,7 @@ void CheckOther::CheckCharVariable()
if (!Match(tok2,".") && Match(tok2->next, "%var% [ %var1% ]", varname)) if (!Match(tok2,".") && Match(tok2->next, "%var% [ %var1% ]", varname))
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok2->next, _tokenizer) << ": Warning - using char variable as array index"; errmsg << _tokenizer->fileLine(tok2->next) << ": Warning - using char variable as array index";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
break; break;
} }
@ -731,7 +731,7 @@ void CheckOther::CheckCharVariable()
if ( Match(tok2, "%var% [&|] %var1%", varname) || Match(tok2, "%var1% [&|]", varname) ) if ( Match(tok2, "%var% [&|] %var1%", varname) || Match(tok2, "%var1% [&|]", varname) )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok2, _tokenizer) << ": Warning - using char variable in bit operation"; errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
break; break;
} }
@ -767,14 +767,14 @@ void CheckOther::CheckIncompleteStatement()
if ( !Match(tok,"#") && Match(tok->next,"; %str%") && !Match(Tokenizer::gettok(tok,3), ",") ) if ( !Match(tok,"#") && Match(tok->next,"; %str%") && !Match(Tokenizer::gettok(tok,3), ",") )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok->next, _tokenizer) << ": 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";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
} }
if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(Tokenizer::gettok(tok,3), ",") ) if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(Tokenizer::gettok(tok,3), ",") )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok->next, _tokenizer) << ": 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";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
} }
} }

View File

@ -36,14 +36,7 @@ bool OnlyReportUniqueErrors;
std::ostringstream errout; std::ostringstream errout;
//---------------------------------------------------------------------------
std::string FileLine( const TOKEN *tok, Tokenizer *_tokenizer )
{
std::ostringstream ostr;
ostr << "[" << _tokenizer->getFiles()->at(tok->FileIndex) << ":" << tok->linenr << "]";
return ostr.str();
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool SameFileName( const char fname1[], const char fname2[] ) bool SameFileName( const char fname1[], const char fname2[] )
@ -99,20 +92,7 @@ bool IsStandardType(const char str[])
Ret |= (strcmp(str,type[i])==0); Ret |= (strcmp(str,type[i])==0);
return Ret; return Ret;
} }
//---------------------------------------------------------------------------
const char *GetParameterName( const TOKEN *ftok, int par )
{
int _par = 1;
for ( ; ftok; ftok = ftok->next)
{
if ( Match(ftok, ",") )
++_par;
if ( par==_par && Match(ftok, "%var% [,)]") )
return ftok->str;
}
return NULL;
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
bool Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) bool Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
@ -207,28 +187,8 @@ bool Match(const TOKEN *tok, const char pattern[], const char *varname1[], const
// The end of the pattern has been reached and nothing wrong has been found // The end of the pattern has been reached and nothing wrong has been found
return true; return true;
} }
//---------------------------------------------------------------------------
const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
{
for ( ; tok; tok = tok->next)
{
if ( Match(tok, pattern, varname1, varname2) )
return tok;
}
return 0;
}
//---------------------------------------------------------------------------
void deleteTokens(TOKEN *tok)
{
while (tok)
{
TOKEN *next = tok->next;
delete tok;
tok = next;
}
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -30,9 +30,6 @@
#include "tokenize.h" #include "tokenize.h"
std::string FileLine(const TOKEN *tok, Tokenizer *_tokenizer);
// Are two filenames the same? Case insensitive on windows // Are two filenames the same? Case insensitive on windows
bool SameFileName( const char fname1[], const char fname2[] ); bool SameFileName( const char fname1[], const char fname2[] );
@ -48,13 +45,12 @@ bool IsNumber(const char str[]);
bool IsStandardType(const char str[]); bool IsStandardType(const char str[]);
const char *GetParameterName( const TOKEN *ftok, int par );
bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0); bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
void deleteTokens(TOKEN *tok);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -1093,28 +1093,7 @@ const char *Tokenizer::getstr(const TOKEN *tok, int index)
// Deallocate lists..
void Tokenizer::DeallocateTokens()
{
while (_tokens)
{
TOKEN *next = _tokens->next;
delete _tokens;
_tokens = next;
}
tokens_back = _tokens;
while (dsymlist)
{
struct DefineSymbol *next = dsymlist->next;
free(dsymlist->name);
free(dsymlist->value);
delete dsymlist;
dsymlist = next;
}
Files.clear();
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -1333,3 +1312,69 @@ void Tokenizer::settings( const Settings &settings )
{ {
_settings = settings; _settings = settings;
} }
// Deallocate lists..
void Tokenizer::DeallocateTokens()
{
deleteTokens( _tokens );
_tokens = 0;
tokens_back = 0;
while (dsymlist)
{
struct DefineSymbol *next = dsymlist->next;
free(dsymlist->name);
free(dsymlist->value);
delete dsymlist;
dsymlist = next;
}
Files.clear();
}
void Tokenizer::deleteTokens(TOKEN *tok)
{
while (tok)
{
TOKEN *next = tok->next;
delete tok;
tok = next;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
const char *Tokenizer::getParameterName( const TOKEN *ftok, int par )
{
int _par = 1;
for ( ; ftok; ftok = ftok->next)
{
if ( Match(ftok, ",") )
++_par;
if ( par==_par && Match(ftok, "%var% [,)]") )
return ftok->str;
}
return NULL;
}
//---------------------------------------------------------------------------
const TOKEN *Tokenizer::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
{
for ( ; tok; tok = tok->next)
{
if ( Match(tok, pattern, varname1, varname2) )
return tok;
}
return 0;
}
//---------------------------------------------------------------------------
std::string Tokenizer::fileLine( const TOKEN *tok )
{
std::ostringstream ostr;
ostr << "[" << Files.at(tok->FileIndex) << ":" << tok->linenr << "]";
return ostr.str();
}

View File

@ -80,6 +80,11 @@ public:
static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]); static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
static const TOKEN *gettok(const TOKEN *tok, int index); static const TOKEN *gettok(const TOKEN *tok, int index);
static const char *getstr(const TOKEN *tok, int index); static const char *getstr(const TOKEN *tok, int index);
static void deleteTokens(TOKEN *tok);
static const char *getParameterName( const TOKEN *ftok, int par );
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
std::string fileLine( const TOKEN *tok );
// Return size. // Return size.
int SizeOfType(const char type[]); int SizeOfType(const char type[]);