From 8845e8bc89434b10fb1b30f562a8724de8064b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 Nov 2008 18:34:51 +0000 Subject: [PATCH] Refactoring: Tokenizer - renaming functions, moved the 'tokenizeCode' to the private section --- cppcheck.cpp | 4 ++-- testbufferoverrun.cpp | 7 +++---- testcharvar.cpp | 5 ++--- testconstructors.cpp | 7 +++---- testdivision.cpp | 6 ++---- testfunctionusage.cpp | 5 ++--- testincompletestatement.cpp | 5 ++--- testmemleak.cpp | 5 ++--- testsimplifytokens.cpp | 4 ++-- testtokenize.cpp | 12 ++++-------- testunusedprivfunc.cpp | 3 +-- testunusedvar.cpp | 5 ++--- tokenize.cpp | 10 +++++----- tokenize.h | 20 ++++++++++++++------ 14 files changed, 46 insertions(+), 52 deletions(-) diff --git a/cppcheck.cpp b/cppcheck.cpp index 5f1ad917e..9c2193e60 100644 --- a/cppcheck.cpp +++ b/cppcheck.cpp @@ -186,7 +186,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) // Tokenize the file { std::istringstream istr(code); - _tokenizer.Tokenize(istr, FileName); + _tokenizer.tokenize(istr, FileName); } _tokenizer.fillFunctionList(); @@ -218,7 +218,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) // } - _tokenizer.SimplifyTokenList(); + _tokenizer.simplifyTokenList(); if ( _settings._checkFunctionUsage ) diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index 2850cd392..68dbd67a4 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -40,10 +40,9 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); // Fill function list Settings settings; @@ -54,7 +53,7 @@ private: // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. + // Check for buffer overruns.. CheckBufferOverrunClass checkBufferOverrun( &tokenizer, this ); checkBufferOverrun.CheckBufferOverrun(); } diff --git a/testcharvar.cpp b/testcharvar.cpp index ddf0a2d87..08c0226ab 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -45,9 +45,8 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); + tokenizer.tokenize( istr, "test.cpp" ); // Fill function list Settings settings; @@ -58,7 +57,7 @@ private: // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. + // Check char variable usage.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckCharVariable(); } diff --git a/testconstructors.cpp b/testconstructors.cpp index 351fd733d..bfa1d9ec8 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -39,15 +39,14 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. + // Check class constructors.. Settings settings; settings._checkCodingStyle = true; CheckClass checkClass( &tokenizer, settings, this ); diff --git a/testdivision.cpp b/testdivision.cpp index 5fd81ec46..c35b5d637 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -41,15 +41,13 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - //SimplifyTokenList(); <- this can't be used as it removes 'unsigned' + tokenizer.tokenize( istr, "test.cpp" ); // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. + // Check for unsigned divisions.. CheckOther checkOther( &tokenizer, this ); checkOther.CheckUnsignedDivision(); } diff --git a/testfunctionusage.cpp b/testfunctionusage.cpp index dd399f1d6..4e961705f 100644 --- a/testfunctionusage.cpp +++ b/testfunctionusage.cpp @@ -46,14 +46,13 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); + tokenizer.tokenize( istr, "test.cpp" ); // Clear the error buffer.. errout.str(""); - // Check for memory leaks.. + // Check for unused functions.. CheckFunctionUsage checkFunctionUsage(this); checkFunctionUsage.parseTokens( tokenizer ); checkFunctionUsage.check(); diff --git a/testincompletestatement.cpp b/testincompletestatement.cpp index 59303e898..203d4a052 100644 --- a/testincompletestatement.cpp +++ b/testincompletestatement.cpp @@ -40,10 +40,9 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); diff --git a/testmemleak.cpp b/testmemleak.cpp index 92022bec4..0d1761478 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -39,10 +39,9 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); diff --git a/testsimplifytokens.cpp b/testsimplifytokens.cpp index 8007bd026..55ab1635f 100644 --- a/testsimplifytokens.cpp +++ b/testsimplifytokens.cpp @@ -43,8 +43,8 @@ private: { std::istringstream istr(code); Tokenizer tokenizer; - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); std::string ret; for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next ) diff --git a/testtokenize.cpp b/testtokenize.cpp index 0cea45f6e..4b454f404 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -65,9 +65,8 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(filedata); - tokenizer.TokenizeCode(istr, 0); + tokenizer.tokenize(istr, "test.cpp"); // Expected result.. const char *expected[] = @@ -89,9 +88,8 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(filedata); - tokenizer.TokenizeCode(istr, 0); + tokenizer.tokenize(istr, "test.cpp"); // Expected result.. ASSERT_EQUALS( std::string(10000,'a'), std::string(tokenizer.tokens()->aaaa()) ); @@ -111,9 +109,8 @@ private: // tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(filedata); - tokenizer.TokenizeCode(istr, 0); + tokenizer.tokenize(istr, "test.cpp"); // Expected result.. const char *expected[] = @@ -142,9 +139,8 @@ private: "{ }\n"; // tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode(istr, 0); + tokenizer.tokenize(istr, "test.cpp"); tokenizer.fillFunctionList(); diff --git a/testunusedprivfunc.cpp b/testunusedprivfunc.cpp index 472df62c2..28b037399 100644 --- a/testunusedprivfunc.cpp +++ b/testunusedprivfunc.cpp @@ -46,9 +46,8 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); + tokenizer.tokenize( istr, "test.cpp" ); // Clear the error buffer.. errout.str(""); diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 4fc604452..b8809986b 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -39,10 +39,9 @@ private: { // Tokenize.. Tokenizer tokenizer; - tokenizer._files.push_back( "test.cpp" ); std::istringstream istr(code); - tokenizer.TokenizeCode( istr ); - tokenizer.SimplifyTokenList(); + tokenizer.tokenize( istr, "test.cpp" ); + tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); diff --git a/tokenize.cpp b/tokenize.cpp index 2abe20c1d..af5252e96 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -231,7 +231,7 @@ void Tokenizer::InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n) // Tokenize - tokenizes a given file. //--------------------------------------------------------------------------- -void Tokenizer::Tokenize(std::istream &code, const char FileName[]) +void Tokenizer::tokenize(std::istream &code, const char FileName[]) { // Has this file been tokenized already? for (unsigned int i = 0; i < _files.size(); i++) @@ -244,7 +244,7 @@ void Tokenizer::Tokenize(std::istream &code, const char FileName[]) _files.push_back(FileName); // Tokenize the file.. - TokenizeCode( code, (unsigned int)(_files.size() - 1) ); + tokenizeCode( code, (unsigned int)(_files.size() - 1) ); } //--------------------------------------------------------------------------- @@ -252,7 +252,7 @@ void Tokenizer::Tokenize(std::istream &code, const char FileName[]) // Tokenize - tokenizes input stream //--------------------------------------------------------------------------- -void Tokenizer::TokenizeCode(std::istream &code, const unsigned int FileIndex) +void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) { // Tokenize the file. unsigned int lineno = 1; @@ -301,7 +301,7 @@ void Tokenizer::TokenizeCode(std::istream &code, const unsigned int FileIndex) addtoken(line.c_str(), lineno, FileIndex); std::ifstream fin( line.c_str() ); - Tokenize(fin, line.c_str()); + tokenize(fin, line.c_str()); } else if (strncmp(line.c_str(), "#define", 7) == 0) @@ -605,7 +605,7 @@ void Tokenizer::TokenizeCode(std::istream &code, const unsigned int FileIndex) // Simplify token list //--------------------------------------------------------------------------- -void Tokenizer::SimplifyTokenList() +void Tokenizer::simplifyTokenList() { // Remove the keyword 'unsigned' diff --git a/tokenize.h b/tokenize.h index 6c4def3c5..725f2a5bd 100644 --- a/tokenize.h +++ b/tokenize.h @@ -35,17 +35,25 @@ private: // Deallocate lists.. void DeallocateTokens(); + /** + * Helper function for "tokenize". This recursively parses into included header files. + */ + void tokenizeCode(std::istream &code, const unsigned int FileIndex=0); + public: Tokenizer(); ~Tokenizer(); + + /** + * Tokenize code + * @param code input stream for code + * @param FileName The filename + */ + void tokenize(std::istream &code, const char FileName[]); - void Tokenize(std::istream &code, const char FileName[]); + /** Simplify tokenlist */ + void simplifyTokenList(); - // Simplify tokenlist - // ----------------------------- - void SimplifyTokenList(); - - void TokenizeCode(std::istream &code, const unsigned int FileIndex=0); // Helper functions for handling the tokens list..