Refactoring: Global variable Files is no more. Use tokenizer->getFiles() to get a pointer to it.

This commit is contained in:
Reijo Tomperi 2008-11-13 22:39:47 +00:00
parent 1c9bf8cabd
commit 1efb4c95a6
20 changed files with 66 additions and 58 deletions

View File

@ -53,8 +53,8 @@ void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])
std::ostringstream ostr;
std::list<const TOKEN *>::const_iterator it;
for ( it = CallStack.begin(); it != CallStack.end(); it++ )
ostr << FileLine(*it) << " -> ";
ostr << FileLine(tok) << ": " << errmsg;
ostr << FileLine(*it, _tokenizer ) << " -> ";
ostr << FileLine(tok, _tokenizer) << ": " << errmsg;
ReportErr(ostr.str());
}
//---------------------------------------------------------------------------
@ -479,14 +479,14 @@ void CheckBufferOverrunClass::WarningDangerousFunctions()
if (Match(tok, "gets ("))
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
ostr << FileLine(tok, _tokenizer) << ": Found 'gets'. You should use 'fgets' instead";
ReportErr(ostr.str());
}
else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0)
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
ostr << FileLine(tok, _tokenizer) << ": Found 'scanf'. You should use 'fgets' instead";
ReportErr(ostr.str());
}
}

View File

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

View File

@ -56,7 +56,7 @@ void CheckHeaders::WarningHeaderWithImplementation()
if (Match(tok, ") {"))
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found implementation in header";
ostr << FileLine(tok, _tokenizer) << ": Found implementation in header";
ReportErr(ostr.str());
// Goto next file..
@ -90,13 +90,13 @@ void CheckHeaders::WarningIncludeHeader()
// Get fileindex of included file..
unsigned int hfile = 0;
const char *includefile = includetok->next->str;
while (hfile < Files.size())
while (hfile < _tokenizer->getFiles()->size())
{
if ( SameFileName( Files[hfile].c_str(), includefile ) )
if ( SameFileName( _tokenizer->getFiles()->at(hfile).c_str(), includefile ) )
break;
hfile++;
}
if (hfile == Files.size())
if (hfile == _tokenizer->getFiles()->size())
continue;
// This header is needed if:
@ -242,7 +242,7 @@ void CheckHeaders::WarningIncludeHeader()
if (!Needed)
{
std::ostringstream ostr;
ostr << FileLine(includetok) << ": The included header '" << includefile << "' is not needed";
ostr << FileLine(includetok, _tokenizer) << ": The included header '" << includefile << "' is not needed";
if (NeedDeclaration)
ostr << " (but a forward declaration is needed)";
ReportErr(ostr.str());

View File

@ -249,7 +249,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, const char *varn
void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const char varname[] )
{
std::ostringstream errmsg;
errmsg << FileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
errmsg << FileLine(Tok1, _tokenizer) << ": Mismatching allocation and deallocation: " << varname;
ReportErr( errmsg.str() );
}
//---------------------------------------------------------------------------
@ -257,7 +257,7 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const char varname[
void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] )
{
std::ostringstream errmsg;
errmsg << FileLine(tok) << ": Memory leak: " << varname;
errmsg << FileLine(tok, _tokenizer) << ": Memory leak: " << varname;
ReportErr( errmsg.str() );
}
//---------------------------------------------------------------------------

View File

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

View File

@ -59,10 +59,10 @@ static std::list< GlobalFunction > UsedGlobalFunctions;
//---------------------------------------------------------------------------
std::string FileLine(const TOKEN *tok)
std::string FileLine( const TOKEN *tok, Tokenizer *_tokenizer )
{
std::ostringstream ostr;
ostr << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]";
ostr << "[" << _tokenizer->getFiles()->at(tok->FileIndex) << ":" << tok->linenr << "]";
return ostr.str();
}
//---------------------------------------------------------------------------

View File

@ -27,10 +27,11 @@
#include <string>
#include <sstream>
#include <vector>
#include "tokenize.h"
class TOKEN;
std::string FileLine(const TOKEN *tok);
std::string FileLine(const TOKEN *tok, Tokenizer *_tokenizer);
// Are two filenames the same? Case insensitive on windows
bool SameFileName( const char fname1[], const char fname2[] );

View File

@ -184,7 +184,6 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in
OnlyReportUniqueErrors = true;
// Tokenize the file
Files.clear();
{
std::istringstream istr(code);
tokenizer.Tokenize(istr, FileName);

View File

@ -39,6 +39,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();

View File

@ -45,6 +45,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );

View File

@ -37,6 +37,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();

View File

@ -41,6 +41,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
//SimplifyTokenList(); <- this can't be used as it removes 'unsigned'

View File

@ -39,6 +39,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();

View File

@ -40,6 +40,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();

View File

@ -24,12 +24,9 @@
bool ShowAll = false;
bool CheckCodingStyle = true;
extern std::vector<std::string> Files;
int main(int argc, const char *argv[])
{
Files.push_back( "test.cpp" );
TestFixture::runTests( (argc==2) ? argv[1] : NULL );
return 0;
}

View File

@ -60,6 +60,7 @@ private:
// tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0);
@ -85,6 +86,7 @@ private:
// tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0);
@ -108,6 +110,7 @@ private:
// tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0);

View File

@ -46,6 +46,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );

View File

@ -39,6 +39,7 @@ private:
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();

View File

@ -59,7 +59,7 @@ TOKEN *Tokenizer::_gettok(TOKEN *tok, int index)
//---------------------------------------------------------------------------
std::vector<std::string> Files;
TOKEN *tokens;
//---------------------------------------------------------------------------
@ -88,6 +88,11 @@ struct DefineSymbol
};
static struct DefineSymbol * dsymlist;
std::vector<std::string> *Tokenizer::getFiles()
{
return &Files;
}
void Tokenizer::Define(const char Name[], const char Value[])
{
if (!(Name && Name[0]))

View File

@ -28,9 +28,6 @@
#include <cstdlib>
#include <cstring>
extern std::vector<std::string> Files;
class TOKEN
{
private:
@ -90,14 +87,12 @@ public:
void initTokens();
std::vector<std::string> _files;
TOKEN *_tokens;
std::vector<std::string> *getFiles();
std::vector<std::string> Files;
private:
void Define(const char Name[], const char Value[]);
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);