Refactoring: Tokenizer object given as a parameter to most of the classes

This commit is contained in:
Reijo Tomperi 2008-11-12 21:34:47 +00:00
parent 58f0f03449
commit 134985e410
20 changed files with 96 additions and 21 deletions

View File

@ -37,6 +37,15 @@ extern bool ShowAll;
// CallStack used when parsing into subfunctions. // CallStack used when parsing into subfunctions.
static std::list<const TOKEN *> CallStack; static std::list<const TOKEN *> CallStack;
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer )
{
_tokenizer = tokenizer;
}
CheckBufferOverrunClass::~CheckBufferOverrunClass()
{
}
// Modified version of 'ReportError' that also reports the callstack // Modified version of 'ReportError' that also reports the callstack
void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[]) void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])

View File

@ -27,6 +27,9 @@
class CheckBufferOverrunClass class CheckBufferOverrunClass
{ {
public: public:
CheckBufferOverrunClass( Tokenizer *tokenizer );
~CheckBufferOverrunClass();
// Buffer overrun.. // Buffer overrun..
void CheckBufferOverrun(); void CheckBufferOverrun();
@ -39,6 +42,7 @@ private:
void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size ); void CheckBufferOverrun_CheckScope( const TOKEN *tok, const char *varname[], const int size, const int total_size );
void ReportError(const TOKEN *tok, const char errmsg[]); void ReportError(const TOKEN *tok, const char errmsg[]);
Tokenizer *_tokenizer;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -101,6 +101,16 @@ struct VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1)
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckClass::CheckClass( Tokenizer *tokenizer )
{
_tokenizer = tokenizer;
}
CheckClass::~CheckClass()
{
}
const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ) const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel )
{ {
const char *_classname[2] = {0,0}; const char *_classname[2] = {0,0};

View File

@ -34,6 +34,9 @@ struct VAR
class CheckClass class CheckClass
{ {
public: public:
CheckClass( Tokenizer *tokenizer );
~CheckClass();
void CheckConstructors(); void CheckConstructors();
void CheckUnusedPrivateFunctions(); void CheckUnusedPrivateFunctions();
@ -47,6 +50,8 @@ private:
void InitVar(struct VAR *varlist, const char varname[]); void InitVar(struct VAR *varlist, const char varname[]);
const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ); const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel );
struct VAR *ClassChecking_GetVarList(const TOKEN *tok1); struct VAR *ClassChecking_GetVarList(const TOKEN *tok1);
Tokenizer *_tokenizer;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -35,6 +35,16 @@
// HEADERS - No implementation in a header // HEADERS - No implementation in a header
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckHeaders::CheckHeaders( Tokenizer *tokenizer )
{
_tokenizer = tokenizer;
}
CheckHeaders::~CheckHeaders()
{
}
void CheckHeaders::WarningHeaderWithImplementation() void CheckHeaders::WarningHeaderWithImplementation()
{ {
for (TOKEN *tok = tokens; tok; tok = tok->next) for (TOKEN *tok = tokens; tok; tok = tok->next)

View File

@ -22,12 +22,18 @@
#define CheckHeadersH #define CheckHeadersH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h"
class CheckHeaders class CheckHeaders
{ {
public: public:
CheckHeaders( Tokenizer *tokenizer );
~CheckHeaders();
void WarningHeaderWithImplementation(); void WarningHeaderWithImplementation();
void WarningIncludeHeader(); void WarningIncludeHeader();
private:
Tokenizer *_tokenizer;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -41,6 +41,16 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer )
{
_tokenizer = tokenizer;
}
CheckMemoryLeakClass::~CheckMemoryLeakClass()
{
}
bool CheckMemoryLeakClass::isclass( const std::string &typestr ) bool CheckMemoryLeakClass::isclass( const std::string &typestr )
{ {
if ( typestr == "char" || if ( typestr == "char" ||

View File

@ -33,6 +33,8 @@ enum AllocType { No, Malloc, gMalloc, New, NewA };
class CheckMemoryLeakClass class CheckMemoryLeakClass
{ {
public: public:
CheckMemoryLeakClass( Tokenizer *tokenizer );
~CheckMemoryLeakClass();
void CheckMemoryLeak(); void CheckMemoryLeak();
private: private:
@ -53,6 +55,8 @@ private:
AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] ); AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] );
AllocType GetAllocationType( const TOKEN *tok2 ); AllocType GetAllocationType( const TOKEN *tok2 );
bool isclass( const std::string &typestr ); bool isclass( const std::string &typestr );
Tokenizer *_tokenizer;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -35,6 +35,16 @@
// Warning on C-Style casts.. p = (kalle *)foo; // Warning on C-Style casts.. p = (kalle *)foo;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckOther::CheckOther( Tokenizer *tokenizer )
{
_tokenizer = tokenizer;
}
CheckOther::~CheckOther()
{
}
void CheckOther::WarningOldStylePointerCast() void CheckOther::WarningOldStylePointerCast()
{ {
for (const TOKEN *tok = tokens; tok; tok = tok->next) for (const TOKEN *tok = tokens; tok; tok = tok->next)

View File

@ -28,6 +28,9 @@
class CheckOther class CheckOther
{ {
public: public:
CheckOther( Tokenizer *tokenizer );
~CheckOther();
// Casting // Casting
void WarningOldStylePointerCast(); void WarningOldStylePointerCast();
@ -71,6 +74,8 @@ public:
void CheckIncompleteStatement(); void CheckIncompleteStatement();
private: private:
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] ); void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
Tokenizer *_tokenizer;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -177,13 +177,13 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in
// Check that the memsets are valid. // Check that the memsets are valid.
// The 'memset' function can do dangerous things if used wrong. // The 'memset' function can do dangerous things if used wrong.
// Important: The checking doesn't work on simplified tokens list. // Important: The checking doesn't work on simplified tokens list.
CheckClass checkClass; CheckClass checkClass( &tokenizer );
checkClass.CheckMemset(); checkClass.CheckMemset();
// Check for unsigned divisions where one operand is signed // Check for unsigned divisions where one operand is signed
// Very important to run it before 'SimplifyTokenList' // Very important to run it before 'SimplifyTokenList'
CheckOther checkOther; CheckOther checkOther( &tokenizer );
checkOther.CheckUnsignedDivision(); checkOther.CheckUnsignedDivision();
// Give warning when using char variable as array index // Give warning when using char variable as array index
@ -195,7 +195,7 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in
// Including header which is not needed (too many false positives) // Including header which is not needed (too many false positives)
// if ( CheckCodingStyle ) // if ( CheckCodingStyle )
// { // {
// CheckHeaders checkHeaders; // CheckHeaders checkHeaders( &tokenizer );
// checkHeaders.WarningIncludeHeader(); // checkHeaders.WarningIncludeHeader();
// } // }
@ -203,11 +203,11 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();
// Memory leak // Memory leak
CheckMemoryLeakClass checkMemoryLeak; CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
// Buffer overruns.. // Buffer overruns..
CheckBufferOverrunClass checkBufferOverrun; CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
checkBufferOverrun.CheckBufferOverrun(); checkBufferOverrun.CheckBufferOverrun();
// Check that all class constructors are ok. // Check that all class constructors are ok.

View File

@ -52,7 +52,7 @@ private:
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true; ShowAll = true;
CheckBufferOverrunClass checkBufferOverrun; CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
checkBufferOverrun.CheckBufferOverrun(); checkBufferOverrun.CheckBufferOverrun();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -57,7 +57,7 @@ private:
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true; ShowAll = true;
CheckOther checkOther; CheckOther checkOther( &tokenizer );
checkOther.CheckCharVariable(); checkOther.CheckCharVariable();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -46,7 +46,7 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
CheckClass checkClass; CheckClass checkClass( &tokenizer );
checkClass.CheckConstructors(); checkClass.CheckConstructors();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -51,7 +51,7 @@ private:
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true; ShowAll = true;
CheckOther checkOther; CheckOther checkOther( &tokenizer );
checkOther.CheckUnsignedDivision(); checkOther.CheckUnsignedDivision();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -48,7 +48,7 @@ private:
errout.str(""); errout.str("");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther; CheckOther checkOther( &tokenizer );
checkOther.CheckIncompleteStatement(); checkOther.CheckIncompleteStatement();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -51,7 +51,7 @@ private:
// Check for memory leaks.. // Check for memory leaks..
ShowAll = false; ShowAll = false;
FillFunctionList(0); FillFunctionList(0);
CheckMemoryLeakClass checkMemoryLeak; CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -54,7 +54,7 @@ private:
errout.str(""); errout.str("");
// Check for unused private functions.. // Check for unused private functions..
CheckClass checkClass; CheckClass checkClass( &tokenizer );
checkClass.CheckUnusedPrivateFunctions(); checkClass.CheckUnusedPrivateFunctions();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -48,7 +48,7 @@ private:
errout.str(""); errout.str("");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther; CheckOther checkOther( &tokenizer );
checkOther.CheckStructMemberUsage(); checkOther.CheckStructMemberUsage();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -85,7 +85,9 @@ public:
// Return size. // Return size.
static int SizeOfType(const char type[]); static int SizeOfType(const char type[]);
std::vector<std::string> _files;
TOKEN *_tokens;
TOKEN *_tokens_back;
private: private: