Few static variables are now private members. ShowAll etc. global variables are now members of Settings class and given as a

parameter to the classes that need them.
This commit is contained in:
Reijo Tomperi 2008-11-16 15:18:50 +00:00
parent 2ecb805283
commit ce08224547
16 changed files with 52 additions and 69 deletions

View File

@ -30,12 +30,10 @@
#include <stdlib.h> // <- strtoul #include <stdlib.h> // <- strtoul
//---------------------------------------------------------------------------
extern bool ShowAll;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// CallStack used when parsing into subfunctions. // CallStack used when parsing into subfunctions.
static std::list<const TOKEN *> CallStack;
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer ) CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer )
{ {

View File

@ -42,7 +42,8 @@ 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; Tokenizer *_tokenizer;
std::list<const TOKEN *> CallStack;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -34,8 +34,16 @@
#endif #endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
extern bool CheckCodingStyle; CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings )
{
_tokenizer = tokenizer;
_settings = settings;
}
CheckClass::~CheckClass()
{
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -101,16 +109,6 @@ 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};
@ -332,7 +330,7 @@ void CheckClass::CheckConstructors()
if ( ! constructor_token ) if ( ! constructor_token )
{ {
// There's no class constructor // There's no class constructor
if ( CheckCodingStyle ) if ( _settings._checkCodingStyle )
{ {
// Check that all member variables are initialized.. // Check that all member variables are initialized..
struct VAR *varlist = ClassChecking_GetVarList(tok1); struct VAR *varlist = ClassChecking_GetVarList(tok1);

View File

@ -22,6 +22,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "settings.h"
#include <list> #include <list>
struct VAR struct VAR
@ -34,7 +35,7 @@ struct VAR
class CheckClass class CheckClass
{ {
public: public:
CheckClass( Tokenizer *tokenizer ); CheckClass( Tokenizer *tokenizer, const Settings &settings );
~CheckClass(); ~CheckClass();
void CheckConstructors(); void CheckConstructors();
@ -52,6 +53,7 @@ private:
struct VAR *ClassChecking_GetVarList(const TOKEN *tok1); struct VAR *ClassChecking_GetVarList(const TOKEN *tok1);
Tokenizer *_tokenizer; Tokenizer *_tokenizer;
Settings _settings;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -41,9 +41,10 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer ) CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_settings = settings;
} }
CheckMemoryLeakClass::~CheckMemoryLeakClass() CheckMemoryLeakClass::~CheckMemoryLeakClass()
@ -281,9 +282,6 @@ bool CheckMemoryLeakClass::notvar(const TOKEN *tok, const char *varnames[])
Match(tok, "%var1% == 0", varnames) ); Match(tok, "%var1% == 0", varnames) );
} }
extern bool ShowAll;
/** /**
* Extract a new tokens list that is easier to parse than the "tokens" * Extract a new tokens list that is easier to parse than the "tokens"
* tok - start parse token * tok - start parse token
@ -344,7 +342,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
AllocType alloc = GetAllocationType(Tokenizer::gettok(tok,3)); AllocType alloc = GetAllocationType(Tokenizer::gettok(tok,3));
// If "--all" hasn't been given, don't check classes.. // If "--all" hasn't been given, don't check classes..
if ( alloc == New && ! ShowAll ) if ( alloc == New && ! _settings._showAll )
{ {
if ( Match(Tokenizer::gettok(tok,3), "new %type% [(;]") ) if ( Match(Tokenizer::gettok(tok,3), "new %type% [(;]") )
{ {
@ -941,7 +939,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN
{ {
if ( IsName(tok->str) || strchr(";}", tok->str[0]) ) if ( IsName(tok->str) || strchr(";}", tok->str[0]) )
{ {
if (ShowAll || !isclass(Tokenizer::getstr(tok,1))) if (_settings._showAll || !isclass(Tokenizer::getstr(tok,1)))
CheckMemoryLeak_ClassMembers_Variable( classname, Tokenizer::getstr(tok, 3) ); CheckMemoryLeak_ClassMembers_Variable( classname, Tokenizer::getstr(tok, 3) );
} }
} }

View File

@ -26,6 +26,7 @@
/** \brief Check for memory leaks */ /** \brief Check for memory leaks */
#include "tokenize.h" #include "tokenize.h"
#include "settings.h"
#include <list> #include <list>
#include <vector> #include <vector>
@ -34,7 +35,7 @@ enum AllocType { No, Malloc, gMalloc, New, NewA };
class CheckMemoryLeakClass class CheckMemoryLeakClass
{ {
public: public:
CheckMemoryLeakClass( Tokenizer *tokenizer ); CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings );
~CheckMemoryLeakClass(); ~CheckMemoryLeakClass();
void CheckMemoryLeak(); void CheckMemoryLeak();
@ -58,6 +59,7 @@ private:
bool isclass( const std::string &typestr ); bool isclass( const std::string &typestr );
Tokenizer *_tokenizer; Tokenizer *_tokenizer;
Settings _settings;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -34,12 +34,6 @@
#include <fstream> #include <fstream>
#include <map> #include <map>
//---------------------------------------------------------------------------
bool Debug = false;
bool ShowAll = false;
bool CheckCodingStyle = false;
bool ErrorsOnly = false;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CppCheck::CppCheck() CppCheck::CppCheck()
@ -83,10 +77,6 @@ void CppCheck::check(int argc, char* argv[])
pathnames.push_back( argv[i] ); pathnames.push_back( argv[i] );
} }
Debug = _settings._debug;
ShowAll = _settings._showAll;
CheckCodingStyle = _settings._checkCodingStyle;
ErrorsOnly = _settings._errorsOnly;
_tokenizer.settings( _settings ); _tokenizer.settings( _settings );
std::vector<std::string> filenames; std::vector<std::string> filenames;
@ -206,7 +196,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[], unsigne
// 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( &_tokenizer ); CheckClass checkClass( &_tokenizer, _settings );
checkClass.CheckMemset(); checkClass.CheckMemset();
@ -222,7 +212,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[], unsigne
// Including header which is not needed (too many false positives) // Including header which is not needed (too many false positives)
// if ( CheckCodingStyle ) // if ( _settings._checkCodingStyle )
// { // {
// CheckHeaders checkHeaders( &tokenizer ); // CheckHeaders checkHeaders( &tokenizer );
// checkHeaders.WarningIncludeHeader(); // checkHeaders.WarningIncludeHeader();
@ -232,7 +222,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[], unsigne
_tokenizer.SimplifyTokenList(); _tokenizer.SimplifyTokenList();
// Memory leak // Memory leak
CheckMemoryLeakClass checkMemoryLeak( &_tokenizer ); CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings );
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
// Buffer overruns.. // Buffer overruns..

View File

@ -26,7 +26,6 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll;
class TestBufferOverrun : public TestFixture class TestBufferOverrun : public TestFixture
{ {
@ -54,7 +53,6 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true;
CheckBufferOverrunClass checkBufferOverrun( &tokenizer ); CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
checkBufferOverrun.CheckBufferOverrun(); checkBufferOverrun.CheckBufferOverrun();

View File

@ -25,7 +25,6 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll;
class TestCharVar : public TestFixture class TestCharVar : public TestFixture
{ {
@ -59,7 +58,6 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true;
CheckOther checkOther( &tokenizer ); CheckOther checkOther( &tokenizer );
checkOther.CheckCharVariable(); checkOther.CheckCharVariable();

View File

@ -24,7 +24,7 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestConstructors : public TestFixture class TestConstructors : public TestFixture
{ {
@ -45,8 +45,10 @@ private:
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
CheckClass checkClass( &tokenizer ); Settings settings;
settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings );
checkClass.CheckConstructors(); checkClass.CheckConstructors();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -28,7 +28,6 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll;
class TestDivision : public TestFixture class TestDivision : public TestFixture
{ {
@ -50,7 +49,6 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
ShowAll = true;
CheckOther checkOther( &tokenizer ); CheckOther checkOther( &tokenizer );
checkOther.CheckUnsignedDivision(); checkOther.CheckUnsignedDivision();

View File

@ -27,7 +27,6 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll;
class TestMemleak : public TestFixture class TestMemleak : public TestFixture
{ {
@ -49,12 +48,12 @@ private:
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
ShowAll = false;
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = false;
tokenizer.settings( settings ); tokenizer.settings( settings );
tokenizer.FillFunctionList(0); tokenizer.FillFunctionList(0);
CheckMemoryLeakClass checkMemoryLeak( &tokenizer ); CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings );
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -20,14 +20,9 @@
#include "testsuite.h" #include "testsuite.h"
#include <string> #include <string>
#include <vector> #include <vector>
extern bool ShowAll;
extern bool CheckCodingStyle;
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
ShowAll = false;
CheckCodingStyle = true;
TestFixture::runTests( (argc==2) ? argv[1] : NULL ); TestFixture::runTests( (argc==2) ? argv[1] : NULL );
return 0; return 0;
} }

View File

@ -24,7 +24,6 @@
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll;
class TestUnusedPrivateFunction : public TestFixture class TestUnusedPrivateFunction : public TestFixture
{ {
@ -53,8 +52,10 @@ private:
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for unused private functions.. // Check for unused private functions..
CheckClass checkClass( &tokenizer ); Settings settings;
settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings );
checkClass.CheckUnusedPrivateFunctions(); checkClass.CheckUnusedPrivateFunctions();
tokenizer.DeallocateTokens(); tokenizer.DeallocateTokens();

View File

@ -69,6 +69,7 @@ Tokenizer::Tokenizer()
{ {
tokens = 0; tokens = 0;
tokens_back = 0; tokens_back = 0;
dsymlist = 0;
} }
Tokenizer::~Tokenizer() Tokenizer::~Tokenizer()
@ -81,13 +82,7 @@ Tokenizer::~Tokenizer()
// "#define abc 123" will create a defined symbol "abc" with the value 123 // "#define abc 123" will create a defined symbol "abc" with the value 123
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
struct DefineSymbol
{
char *name;
char *value;
struct DefineSymbol *next;
};
static struct DefineSymbol * dsymlist;
std::vector<std::string> *Tokenizer::getFiles() std::vector<std::string> *Tokenizer::getFiles()
{ {

View File

@ -94,11 +94,16 @@ public:
void FillFunctionList(const unsigned int file_id); void FillFunctionList(const unsigned int file_id);
const TOKEN *GetFunctionTokenByName( const char funcname[] ) const; const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;
void CheckGlobalFunctionUsage(const std::vector<std::string> &filenames); void CheckGlobalFunctionUsage(const std::vector<std::string> &filenames);
void settings( const Settings &settings ); void settings( const Settings &settings );
private: private:
struct DefineSymbol
{
char *name;
char *value;
struct DefineSymbol *next;
};
class GlobalFunction class GlobalFunction
{ {
@ -136,6 +141,9 @@ private:
std::list< GlobalFunction > UsedGlobalFunctions; std::list< GlobalFunction > UsedGlobalFunctions;
std::vector<std::string> Files; std::vector<std::string> Files;
Settings _settings; Settings _settings;
struct DefineSymbol * dsymlist;
}; };