From 0b1ee103539356442e5c7be244a668380b89b739 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 12 Nov 2008 22:50:40 +0000 Subject: [PATCH] Refactoring: tokens_back and TypeSize are no longer global variables --- CheckBufferOverrun.cpp | 4 ++-- main.cpp | 3 +-- testbufferoverrun.cpp | 5 ++--- testcharvar.cpp | 3 +-- testconstructors.cpp | 3 +-- testdivision.cpp | 3 +-- testincompletestatement.cpp | 3 +-- testmemleak.cpp | 3 +-- testtokenize.cpp | 9 +++------ testunusedprivfunc.cpp | 3 +-- testunusedvar.cpp | 3 +-- tokenize.cpp | 18 +++++++++++++----- tokenize.h | 26 +++++++++++++++++--------- 13 files changed, 45 insertions(+), 41 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index e644e4401..74ca9f785 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -341,7 +341,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_LocalVariable() continue; } - int total_size = size * Tokenizer::SizeOfType(type); + int total_size = size * _tokenizer->SizeOfType(type); if (total_size == 0) continue; @@ -397,7 +397,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_StructVariable() const char *varname[3] = {0,0,0}; varname[1] = Tokenizer::getstr(tok2, ivar); int arrsize = atoi(Tokenizer::getstr(tok2, ivar+2)); - int total_size = arrsize * Tokenizer::SizeOfType(tok2->next->str); + int total_size = arrsize * _tokenizer->SizeOfType(tok2->next->str); if (total_size == 0) continue; diff --git a/main.cpp b/main.cpp index bb0d05780..037859385 100644 --- a/main.cpp +++ b/main.cpp @@ -164,8 +164,7 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in OnlyReportUniqueErrors = true; - // Tokenize the file - tokens = tokens_back = NULL; + // Tokenize the file Files.clear(); { std::istringstream istr(code); diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index 172594035..f0817d730 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -37,10 +37,9 @@ public: private: void check( const char code[] ) { - // Tokenize.. - tokens = tokens_back = NULL; - std::istringstream istr(code); + // Tokenize.. Tokenizer tokenizer; + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testcharvar.cpp b/testcharvar.cpp index 62eb501f9..ef7eca6be 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -44,9 +44,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); // Fill function list diff --git a/testconstructors.cpp b/testconstructors.cpp index d22f48e2a..2d9512440 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -36,9 +36,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testdivision.cpp b/testdivision.cpp index d1931f044..ed6e6cf4d 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -40,9 +40,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); //SimplifyTokenList(); <- this can't be used as it removes 'unsigned' diff --git a/testincompletestatement.cpp b/testincompletestatement.cpp index 8dd05af0b..acefaa88b 100644 --- a/testincompletestatement.cpp +++ b/testincompletestatement.cpp @@ -38,9 +38,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testmemleak.cpp b/testmemleak.cpp index 44b3f95fa..0b3d048aa 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -39,9 +39,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/testtokenize.cpp b/testtokenize.cpp index 0160fba07..c0110628c 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -59,9 +59,8 @@ private: " \"def\"\n"; // tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(filedata); - Tokenizer tokenizer; tokenizer.TokenizeCode(istr, 0); // Expected result.. @@ -85,9 +84,8 @@ private: std::string filedata(10000,'a'); // tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(filedata); - Tokenizer tokenizer; tokenizer.TokenizeCode(istr, 0); // Expected result.. @@ -109,9 +107,8 @@ private: "}\n"; // tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(filedata); - Tokenizer tokenizer; tokenizer.TokenizeCode(istr, 0); // Expected result.. diff --git a/testunusedprivfunc.cpp b/testunusedprivfunc.cpp index d01e11603..72ed72ad3 100644 --- a/testunusedprivfunc.cpp +++ b/testunusedprivfunc.cpp @@ -45,9 +45,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; + Tokenizer tokenizer; std::istringstream istr(code); - Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); // Clear the error buffer.. diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 38839ebcc..6f8e0e55d 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -38,9 +38,8 @@ private: void check( const char code[] ) { // Tokenize.. - tokens = tokens_back = NULL; - std::istringstream istr(code); Tokenizer tokenizer; + std::istringstream istr(code); tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); diff --git a/tokenize.cpp b/tokenize.cpp index ca343c4d1..e1e44d657 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -25,7 +25,7 @@ #include #include -#include + #include #include @@ -60,12 +60,20 @@ TOKEN *Tokenizer::_gettok(TOKEN *tok, int index) //--------------------------------------------------------------------------- std::vector Files; -TOKEN *tokens, *tokens_back; +TOKEN *tokens; //--------------------------------------------------------------------------- - - +Tokenizer::Tokenizer() +{ + tokens = 0; + tokens_back = 0; +} + +Tokenizer::~Tokenizer() +{ + +} //--------------------------------------------------------------------------- // Defined symbols. @@ -211,7 +219,7 @@ void Tokenizer::combine_2tokens(TOKEN *tok, const char str1[], const char str2[] // SizeOfType - gives the size of a type //--------------------------------------------------------------------------- -std::map TypeSize; + int Tokenizer::SizeOfType(const char type[]) { diff --git a/tokenize.h b/tokenize.h index bc90daf03..d41a4c80d 100644 --- a/tokenize.h +++ b/tokenize.h @@ -22,7 +22,8 @@ #define tokenizeH //--------------------------------------------------------------------------- -#include +#include +#include #include #include #include @@ -65,6 +66,8 @@ extern TOKEN *tokens, *tokens_back; class Tokenizer { public: + Tokenizer(); + ~Tokenizer(); void Tokenize(std::istream &code, const char FileName[]); @@ -83,27 +86,32 @@ public: static const char *getstr(const TOKEN *tok, int index); // Return size. - static int SizeOfType(const char type[]); + int SizeOfType(const char type[]); + + void initTokens(); std::vector _files; TOKEN *_tokens; - TOKEN *_tokens_back; + private: - static void Define(const char Name[], const char Value[]); + void Define(const char Name[], const char Value[]); - static void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); + void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); - static void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]); + void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]); - static void DeleteNextToken(TOKEN *tok); + void DeleteNextToken(TOKEN *tok); - static TOKEN *_gettok(TOKEN *tok, int index); + TOKEN *_gettok(TOKEN *tok, int index); - static void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n); + void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n); + + TOKEN *tokens_back; + std::map TypeSize; };