diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index 74ca9f785..5cd551366 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -53,8 +53,8 @@ void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[]) std::ostringstream ostr; std::list::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()); } } diff --git a/CheckClass.cpp b/CheckClass.cpp index 3c4c02a2b..5bb5de33f 100644 --- a/CheckClass.cpp +++ b/CheckClass.cpp @@ -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()); } } diff --git a/CheckHeaders.cpp b/CheckHeaders.cpp index ed45de8b6..5375c57b3 100644 --- a/CheckHeaders.cpp +++ b/CheckHeaders.cpp @@ -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()); diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 4fcb2bd73..f1955d14a 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -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() ); } //--------------------------------------------------------------------------- diff --git a/CheckOther.cpp b/CheckOther.cpp index 197443585..61c903d36 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -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()); } } diff --git a/CommonCheck.cpp b/CommonCheck.cpp index df2bd1687..bbdebd657 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -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(); } //--------------------------------------------------------------------------- diff --git a/CommonCheck.h b/CommonCheck.h index fceeaa94c..876a047aa 100644 --- a/CommonCheck.h +++ b/CommonCheck.h @@ -26,11 +26,12 @@ #include #include #include -#include +#include +#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[] ); diff --git a/main.cpp b/main.cpp index 6a32bcc41..3e8ab3a08 100644 --- a/main.cpp +++ b/main.cpp @@ -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); diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index f0817d730..5111951c8 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -38,7 +38,8 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; + Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testcharvar.cpp b/testcharvar.cpp index ef7eca6be..f4215c09c 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -45,6 +45,7 @@ private: { // Tokenize.. Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); diff --git a/testconstructors.cpp b/testconstructors.cpp index 9969b209e..665c66e6b 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -36,7 +36,8 @@ private: void check( const char code[] ) { // Tokenize.. - Tokenizer tokenizer; + Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testdivision.cpp b/testdivision.cpp index ed6e6cf4d..b807a2161 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -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' diff --git a/testincompletestatement.cpp b/testincompletestatement.cpp index acefaa88b..d112bdfe4 100644 --- a/testincompletestatement.cpp +++ b/testincompletestatement.cpp @@ -39,6 +39,7 @@ private: { // Tokenize.. Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testmemleak.cpp b/testmemleak.cpp index 0b3d048aa..cadc0af17 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -40,6 +40,7 @@ private: { // Tokenize.. Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testrunner.cpp b/testrunner.cpp index d8d1575ec..2d9b829b6 100644 --- a/testrunner.cpp +++ b/testrunner.cpp @@ -24,12 +24,9 @@ bool ShowAll = false; bool CheckCodingStyle = true; -extern std::vector Files; - int main(int argc, const char *argv[]) { - Files.push_back( "test.cpp" ); TestFixture::runTests( (argc==2) ? argv[1] : NULL ); return 0; } diff --git a/testtokenize.cpp b/testtokenize.cpp index c0110628c..70cf1ef96 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -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); diff --git a/testunusedprivfunc.cpp b/testunusedprivfunc.cpp index 72ed72ad3..8d7adf5dd 100644 --- a/testunusedprivfunc.cpp +++ b/testunusedprivfunc.cpp @@ -46,6 +46,7 @@ private: { // Tokenize.. Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 6f8e0e55d..011d2175a 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -39,6 +39,7 @@ private: { // Tokenize.. Tokenizer tokenizer; + tokenizer.getFiles()->push_back( "test.cpp" ); std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/tokenize.cpp b/tokenize.cpp index e1e44d657..415b5cadf 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -59,7 +59,7 @@ TOKEN *Tokenizer::_gettok(TOKEN *tok, int index) //--------------------------------------------------------------------------- -std::vector Files; + TOKEN *tokens; //--------------------------------------------------------------------------- @@ -87,6 +87,11 @@ struct DefineSymbol struct DefineSymbol *next; }; static struct DefineSymbol * dsymlist; + +std::vector *Tokenizer::getFiles() +{ + return &Files; +} void Tokenizer::Define(const char Name[], const char Value[]) { diff --git a/tokenize.h b/tokenize.h index d41a4c80d..d76f280a2 100644 --- a/tokenize.h +++ b/tokenize.h @@ -28,9 +28,6 @@ #include #include -extern std::vector Files; - - class TOKEN { private: @@ -90,14 +87,12 @@ public: void initTokens(); - std::vector _files; - TOKEN *_tokens; + std::vector *getFiles(); + + std::vector Files; private: - - - void Define(const char Name[], const char Value[]); void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);