increased constness of member variable _tokenizer in the Check* classes

This commit is contained in:
Daniel Marjamäki 2008-11-22 20:00:36 +00:00
parent a60dad3562
commit 0d3a99adb0
19 changed files with 358 additions and 359 deletions

View File

@ -33,17 +33,17 @@
#endif #endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) CheckClass::CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_settings = settings; _settings = settings;
_errorLogger = errorLogger; _errorLogger = errorLogger;
} }
CheckClass::~CheckClass() CheckClass::~CheckClass()
{ {
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -20,24 +20,24 @@
#ifndef CheckClassH #ifndef CheckClassH
#define CheckClassH #define CheckClassH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h"
#include "settings.h"
#include "errorlogger.h"
#include <list>
struct VAR
{
const char *name;
bool init;
struct VAR *next;
};
class CheckClass #include "tokenize.h"
{ #include "settings.h"
public: #include "errorlogger.h"
CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); #include <list>
~CheckClass();
struct VAR
{
const char *name;
bool init;
struct VAR *next;
};
class CheckClass
{
public:
CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
~CheckClass();
void CheckConstructors(); void CheckConstructors();
@ -45,17 +45,17 @@ public:
void CheckMemset(); void CheckMemset();
void CheckOperatorEq1(); // Warning upon "void operator=(.." void CheckOperatorEq1(); // Warning upon "void operator=(.."
private: private:
void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &callstack); void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &callstack);
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; const Tokenizer *_tokenizer;
Settings _settings; Settings _settings;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -34,17 +34,17 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// HEADERS - No implementation in a header // HEADERS - No implementation in a header
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckHeaders::CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger ) CheckHeaders::CheckHeaders( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_errorLogger = errorLogger; _errorLogger = errorLogger;
} }
CheckHeaders::~CheckHeaders() CheckHeaders::~CheckHeaders()
{ {
} }
void CheckHeaders::WarningHeaderWithImplementation() void CheckHeaders::WarningHeaderWithImplementation()
{ {

View File

@ -21,20 +21,20 @@
#ifndef CheckHeadersH #ifndef CheckHeadersH
#define CheckHeadersH #define CheckHeadersH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "errorlogger.h" #include "errorlogger.h"
class CheckHeaders class CheckHeaders
{ {
public: public:
CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger ); CheckHeaders( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
~CheckHeaders(); ~CheckHeaders();
void WarningHeaderWithImplementation(); void WarningHeaderWithImplementation();
void WarningIncludeHeader(); void WarningIncludeHeader();
private: private:
Tokenizer *_tokenizer; const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
}; };

View File

@ -37,10 +37,10 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ) CheckMemoryLeakClass::CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_settings = settings; _settings = settings;
_errorLogger = errorLogger; _errorLogger = errorLogger;
} }
@ -95,7 +95,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
{ {
if ( strcmp(mallocfunc[i], tok2->str) == 0 ) if ( strcmp(mallocfunc[i], tok2->str) == 0 )
return Malloc; return Malloc;
} }
// Does tok2 point on "malloc", "strdup" or "kmalloc".. // Does tok2 point on "malloc", "strdup" or "kmalloc"..
const char *gmallocfunc[] = {"g_new", const char *gmallocfunc[] = {"g_new",
@ -124,12 +124,12 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
if ( Tokenizer::Match( tok2, "new %type% [" ) ) if ( Tokenizer::Match( tok2, "new %type% [" ) )
return NewA; return NewA;
if ( Tokenizer::Match( tok2, "fopen (" ) ) if ( Tokenizer::Match( tok2, "fopen (" ) )
return FOPEN; return FOPEN;
if ( Tokenizer::Match( tok2, "popen (" ) ) if ( Tokenizer::Match( tok2, "popen (" ) )
return POPEN; return POPEN;
// Userdefined allocation function.. // Userdefined allocation function..
std::list<AllocFunc>::const_iterator it = listallocfunc.begin(); std::list<AllocFunc>::const_iterator it = listallocfunc.begin();
@ -167,12 +167,12 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const
if ( Tokenizer::Match(tok, "g_free ( %var1% ) ;", varnames) ) if ( Tokenizer::Match(tok, "g_free ( %var1% ) ;", varnames) )
return gMalloc; return gMalloc;
if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) ) if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) )
return FOPEN; return FOPEN;
if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) ) if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) )
return POPEN; return POPEN;
return No; return No;
} }
@ -185,15 +185,15 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
if (GetAllocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No) if (GetAllocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No)
return 0; return 0;
if ( callstack.size() > 2 )
return "dealloc";
const char *funcname = tok->str; if ( callstack.size() > 2 )
for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) return "dealloc";
{
if ( std::string(funcname) == (*it)->str ) const char *funcname = tok->str;
return "dealloc"; for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it )
{
if ( std::string(funcname) == (*it)->str )
return "dealloc";
} }
callstack.push_back(tok); callstack.push_back(tok);
@ -244,8 +244,8 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] ) void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok ) for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok )
errmsg << _tokenizer->fileLine(*tok) << " -> "; errmsg << _tokenizer->fileLine(*tok) << " -> ";
errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname; errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
_errorLogger->reportErr( errmsg.str() ); _errorLogger->reportErr( errmsg.str() );
@ -253,7 +253,7 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<con
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] ) void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << ": Memory leak: " << varname; errmsg << _tokenizer->fileLine(tok) << ": Memory leak: " << varname;
_errorLogger->reportErr( errmsg.str() ); _errorLogger->reportErr( errmsg.str() );
@ -370,9 +370,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
dealloctype = dealloc; dealloctype = dealloc;
} }
// if else switch // if else switch
if ( Tokenizer::Match(tok, "if ( %var1% )", varnames) || if ( Tokenizer::Match(tok, "if ( %var1% )", varnames) ||
Tokenizer::Match(tok, "if ( %var1% != 0 )", varnames) || Tokenizer::Match(tok, "if ( %var1% != 0 )", varnames) ||
Tokenizer::Match(tok, "if ( 0 != %var1% )", varnames) ) Tokenizer::Match(tok, "if ( 0 != %var1% )", varnames) )
{ {
addtoken("if(var)"); addtoken("if(var)");
@ -381,40 +381,40 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
else if ( Tokenizer::Match(tok, "if (") && notvar(Tokenizer::gettok(tok,2), varnames) ) else if ( Tokenizer::Match(tok, "if (") && notvar(Tokenizer::gettok(tok,2), varnames) )
{ {
addtoken("if(!var)"); addtoken("if(!var)");
} }
else if ( Tokenizer::Match(tok, "if ( true )") ) else if ( Tokenizer::Match(tok, "if ( true )") )
{ {
addtoken("if(true)"); addtoken("if(true)");
} }
else if ( Tokenizer::Match(tok, "if ( false )") ) else if ( Tokenizer::Match(tok, "if ( false )") )
{ {
addtoken("if(false)"); addtoken("if(false)");
} }
else if ( Tokenizer::Match(tok, "if") ) else if ( Tokenizer::Match(tok, "if") )
{ {
// Check if the condition depends on var somehow.. // Check if the condition depends on var somehow..
bool dep = false; bool dep = false;
int parlevel = 0; int parlevel = 0;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next ) for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{ {
if ( Tokenizer::Match(tok2,"(") ) if ( Tokenizer::Match(tok2,"(") )
++parlevel; ++parlevel;
if ( Tokenizer::Match(tok2,")") ) if ( Tokenizer::Match(tok2,")") )
{ {
--parlevel; --parlevel;
if ( parlevel <= 0 ) if ( parlevel <= 0 )
break; break;
} }
if ( !Tokenizer::Match(tok2,".") && if ( !Tokenizer::Match(tok2,".") &&
Tokenizer::Match(tok2->next, "%var1%", varnames) && Tokenizer::Match(tok2->next, "%var1%", varnames) &&
!Tokenizer::Match(tok2->next, "%var1% .", varnames) ) !Tokenizer::Match(tok2->next, "%var1% .", varnames) )
{ {
dep = true; dep = true;
break; break;
} }
} }
addtoken( (dep ? "ifv" : "if") ); addtoken( (dep ? "ifv" : "if") );
} }
else if ( Tokenizer::Match(tok, "else") || Tokenizer::Match(tok, "switch") ) else if ( Tokenizer::Match(tok, "else") || Tokenizer::Match(tok, "switch") )
{ {
addtoken(tok->str); addtoken(tok->str);
@ -605,10 +605,10 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Delete empty if that is not followed by an else // Delete empty if that is not followed by an else
if ( Tokenizer::Match(tok2,"[;{}] if ;") || if ( Tokenizer::Match(tok2,"[;{}] if ;") ||
Tokenizer::Match(tok2,"[;{}] if(var) ;") || Tokenizer::Match(tok2,"[;{}] if(var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(!var) ;") || Tokenizer::Match(tok2,"[;{}] if(!var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(true) ;") || Tokenizer::Match(tok2,"[;{}] if(true) ;") ||
Tokenizer::Match(tok2,"[;{}] if(false) ;") || Tokenizer::Match(tok2,"[;{}] if(false) ;") ||
Tokenizer::Match(tok2,"[;{}] ifv ;") ) Tokenizer::Match(tok2,"[;{}] ifv ;") )
{ {
if ( ! Tokenizer::Match(Tokenizer::gettok(tok2,3), "else") ) if ( ! Tokenizer::Match(Tokenizer::gettok(tok2,3), "else") )
@ -620,8 +620,8 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
} }
// Delete "if dealloc ;" and "if use ;" that is not followed by an else.. // Delete "if dealloc ;" and "if use ;" that is not followed by an else..
// This may cause false positives // This may cause false positives
if (_settings._showAll && if (_settings._showAll &&
(Tokenizer::Match(tok2, "[;{}] if dealloc ;") || Tokenizer::Match(tok2, "[;{}] if use ;")) && (Tokenizer::Match(tok2, "[;{}] if dealloc ;") || Tokenizer::Match(tok2, "[;{}] if use ;")) &&
!Tokenizer::Match(Tokenizer::gettok(tok2,4), "else")) !Tokenizer::Match(Tokenizer::gettok(tok2,4), "else"))
{ {
@ -788,12 +788,12 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" // Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
{ {
std::list<const TOKEN *> callstack; std::list<const TOKEN *> callstack;
AllocType alloctype = No; AllocType alloctype = No;
AllocType dealloctype = No; AllocType dealloctype = No;
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
// If the variable is not allocated at all => no memory leak // If the variable is not allocated at all => no memory leak
@ -831,21 +831,21 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
{ {
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if return ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; if return ;"), 3), varname);
} }
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") )
{ {
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname);
} }
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") )
{ {
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname);
} }
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") ) else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") )
{ {
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname); MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname);
} }
else if ( Tokenizer::findmatch(tok, "alloc ; return ;") ) else if ( Tokenizer::findmatch(tok, "alloc ; return ;") )
{ {
@ -1048,7 +1048,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec
{ {
AllocType alloc = GetAllocationType( Tokenizer::gettok( tok, 2 ) ); AllocType alloc = GetAllocationType( Tokenizer::gettok( tok, 2 ) );
if ( alloc != No ) if ( alloc != No )
{ {
std::list<const TOKEN *> callstack; std::list<const TOKEN *> callstack;
if ( Dealloc != No && Dealloc != alloc ) if ( Dealloc != No && Dealloc != alloc )
MismatchError( tok, callstack, FullVariableName.str().c_str() ); MismatchError( tok, callstack, FullVariableName.str().c_str() );
@ -1063,7 +1063,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec
varnames[0] = varname; varnames[0] = varname;
AllocType dealloc = GetDeallocationType( tok, varnames ); AllocType dealloc = GetDeallocationType( tok, varnames );
if ( dealloc != No ) if ( dealloc != No )
{ {
std::list<const TOKEN *> callstack; std::list<const TOKEN *> callstack;
if ( Dealloc != No && Dealloc != dealloc ) if ( Dealloc != No && Dealloc != dealloc )
MismatchError( tok, callstack, FullVariableName.str().c_str() ); MismatchError( tok, callstack, FullVariableName.str().c_str() );

View File

@ -24,60 +24,60 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
/** \brief Check for memory leaks */ /** \brief Check for memory leaks */
#include "tokenize.h" #include "tokenize.h"
#include "settings.h" #include "settings.h"
#include "errorlogger.h" #include "errorlogger.h"
#include <list> #include <list>
#include <vector> #include <vector>
class CheckMemoryLeakClass class CheckMemoryLeakClass
{ {
public: public:
CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger ); CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
~CheckMemoryLeakClass(); ~CheckMemoryLeakClass();
void CheckMemoryLeak(); void CheckMemoryLeak();
private: private:
enum AllocType { No, Malloc, gMalloc, New, NewA, FOPEN, POPEN }; enum AllocType { No, Malloc, gMalloc, New, NewA, FOPEN, POPEN };
// Extra allocation.. // Extra allocation..
class AllocFunc class AllocFunc
{ {
public: public:
const char *funcname; const char *funcname;
AllocType alloctype; AllocType alloctype;
AllocFunc(const char f[], AllocType a) AllocFunc(const char f[], AllocType a)
{ {
funcname = f; funcname = f;
alloctype = a; alloctype = a;
} }
}; };
void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] ); void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] );
void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname ); void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname );
void CheckMemoryLeak_ClassMembers(); void CheckMemoryLeak_ClassMembers();
void CheckMemoryLeak_InFunction(); void CheckMemoryLeak_InFunction();
void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ); void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] );
void simplifycode(TOKEN *tok); void simplifycode(TOKEN *tok);
void erase(TOKEN *begin, const TOKEN *end); void erase(TOKEN *begin, const TOKEN *end);
TOKEN *getcode(const TOKEN *tok, std::list<const TOKEN *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype); TOKEN *getcode(const TOKEN *tok, std::list<const TOKEN *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype);
bool notvar(const TOKEN *tok, const char *varnames[]); bool notvar(const TOKEN *tok, const char *varnames[]);
void instoken(TOKEN *tok, const char str[]); void instoken(TOKEN *tok, const char str[]);
void MemoryLeak( const TOKEN *tok, const char varname[] ); void MemoryLeak( const TOKEN *tok, const char varname[] );
void MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] ); void MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] );
const char * call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ); const char * call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype );
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; const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
Settings _settings; Settings _settings;
std::list<AllocFunc> listallocfunc; std::list<AllocFunc> listallocfunc;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -34,9 +34,9 @@
// Warning on C-Style casts.. p = (kalle *)foo; // Warning on C-Style casts.. p = (kalle *)foo;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckOther::CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger ) CheckOther::CheckOther( const Tokenizer *tokenizer, ErrorLogger *errorLogger )
{ {
_tokenizer = tokenizer; _tokenizer = tokenizer;
_errorLogger = errorLogger; _errorLogger = errorLogger;
} }

View File

@ -23,13 +23,13 @@
#define CheckOtherH #define CheckOtherH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "errorlogger.h" #include "errorlogger.h"
class CheckOther class CheckOther
{ {
public: public:
CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger ); CheckOther( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
~CheckOther(); ~CheckOther();
// Casting // Casting
@ -76,7 +76,7 @@ public:
private: private:
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] ); void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
Tokenizer *_tokenizer; const Tokenizer *_tokenizer;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
}; };

View File

@ -17,10 +17,10 @@
*/ */
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckBufferOverrun.h" #include "CheckBufferOverrun.h"
#include "testsuite.h" #include "testsuite.h"
#include <sstream> #include <sstream>
@ -32,22 +32,22 @@ public:
TestBufferOverrun() : TestFixture("TestBufferOverrun") TestBufferOverrun() : TestFixture("TestBufferOverrun")
{ } { }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();
// Fill function list // Fill function list
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
tokenizer.settings( settings ); tokenizer.settings( settings );
tokenizer.FillFunctionList(0); tokenizer.FillFunctionList(0);
@ -323,7 +323,7 @@ private:
"Fred::Fred()\n" "Fred::Fred()\n"
"{\n" "{\n"
" str[10] = 0;\n" " str[10] = 0;\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:10]: Array index out of bounds\n"), err ); ASSERT_EQUALS( std::string("[test.cpp:10]: Array index out of bounds\n"), err );
} }

View File

@ -17,9 +17,10 @@
*/ */
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "testsuite.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
@ -30,8 +31,8 @@ public:
TestCharVar() : TestFixture("TestCharVar") TestCharVar() : TestFixture("TestCharVar")
{ } { }
private: private:
void run() void run()
{ {
@ -43,14 +44,14 @@ private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
// Fill function list // Fill function list
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
tokenizer.settings( settings ); tokenizer.settings( settings );
tokenizer.FillFunctionList(0); tokenizer.FillFunctionList(0);

View File

@ -18,12 +18,13 @@
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckClass.h" #include "CheckClass.h"
#include "testsuite.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestConstructors : public TestFixture class TestConstructors : public TestFixture
{ {
@ -31,14 +32,14 @@ public:
TestConstructors() : TestFixture("TestConstructors") TestConstructors() : TestFixture("TestConstructors")
{ } { }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();
@ -46,8 +47,8 @@ private:
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for memory leaks.. // Check for memory leaks..
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings, this ); CheckClass checkClass( &tokenizer, settings, this );
checkClass.CheckConstructors(); checkClass.CheckConstructors();

View File

@ -21,6 +21,7 @@
// such as "svar / uvar". Treating "svar" as unsigned data is not good // such as "svar / uvar". Treating "svar" as unsigned data is not good
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "testsuite.h" #include "testsuite.h"
@ -39,9 +40,9 @@ private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
//SimplifyTokenList(); <- this can't be used as it removes 'unsigned' //SimplifyTokenList(); <- this can't be used as it removes 'unsigned'

View File

@ -20,6 +20,7 @@
// Check for unused variables.. // Check for unused variables..
#define UNIT_TESTING
#include "testsuite.h" #include "testsuite.h"
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
@ -38,16 +39,16 @@ private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther( &tokenizer, this ); CheckOther checkOther( &tokenizer, this );
checkOther.CheckIncompleteStatement(); checkOther.CheckIncompleteStatement();
} }

View File

@ -19,6 +19,7 @@
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckMemoryLeak.h" #include "CheckMemoryLeak.h"
#include "testsuite.h" #include "testsuite.h"
@ -38,7 +39,7 @@ private:
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();
@ -48,7 +49,7 @@ private:
// Check for memory leaks.. // Check for memory leaks..
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
settings._showAll = false; settings._showAll = false;
tokenizer.settings( settings ); tokenizer.settings( settings );
tokenizer.FillFunctionList(0); tokenizer.FillFunctionList(0);
@ -81,11 +82,11 @@ private:
TEST_CASE( ifelse9 ); TEST_CASE( ifelse9 );
TEST_CASE( if1 ); TEST_CASE( if1 );
TEST_CASE( if2 ); TEST_CASE( if2 );
TEST_CASE( if3 ); TEST_CASE( if3 );
TEST_CASE( if4 ); TEST_CASE( if4 );
TEST_CASE( if5 ); TEST_CASE( if5 );
TEST_CASE( alwaysTrue ); TEST_CASE( alwaysTrue );
TEST_CASE( forwhile1 ); TEST_CASE( forwhile1 );
@ -110,8 +111,8 @@ private:
TEST_CASE( func2 ); TEST_CASE( func2 );
TEST_CASE( func3 ); TEST_CASE( func3 );
TEST_CASE( func4 ); TEST_CASE( func4 );
TEST_CASE( func5 ); TEST_CASE( func5 );
TEST_CASE( func6 ); TEST_CASE( func6 );
TEST_CASE( class1 ); TEST_CASE( class1 );
TEST_CASE( class2 ); TEST_CASE( class2 );
@ -222,8 +223,8 @@ private:
{ {
check( "void foo()\n" check( "void foo()\n"
"{\n" "{\n"
" char *str;\n" " char *str;\n"
" if (somecondition)\n" " if (somecondition)\n"
" str = strdup(\"abc\");\n" " str = strdup(\"abc\");\n"
" if (somecondition)\n" " if (somecondition)\n"
" DeleteString(str);\n" " DeleteString(str);\n"
@ -415,66 +416,66 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void if3() void if3()
{ {
check( "void f()\n" check( "void f()\n"
"{\n" "{\n"
" char *s = new char[100];\n" " char *s = new char[100];\n"
" if (0 != s)\n" " if (0 != s)\n"
" foo(s);\n" " foo(s);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void if4() void if4()
{ {
check( "void f()\n" check( "void f()\n"
"{\n" "{\n"
" char *s;\n" " char *s;\n"
" bool b = true;\n" " bool b = true;\n"
" if (b && (s = malloc(256)))\n" " if (b && (s = malloc(256)))\n"
" ;\n" " ;\n"
" if (b)\n" " if (b)\n"
" free(s);\n" " free(s);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
void if5() void if5()
{ {
check( "void f()\n" check( "void f()\n"
"{\n" "{\n"
" char *p = malloc(256);\n" " char *p = malloc(256);\n"
" if (somecondition && !p)\n" " if (somecondition && !p)\n"
" return;\n" " return;\n"
" free(p);\n" " free(p);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
void alwaysTrue() void alwaysTrue()
{ {
check( "void f()\n" check( "void f()\n"
"{\n" "{\n"
" char *p = 0;\n" " char *p = 0;\n"
" for (;;)\n" " for (;;)\n"
" {\n" " {\n"
" p = malloc(256);\n" " p = malloc(256);\n"
" if (1)\n" " if (1)\n"
" break;\n" " break;\n"
" }\n" " }\n"
" free(p);\n" " free(p);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
@ -760,28 +761,28 @@ private:
"{\n" "{\n"
" char *p = new char[100];\n" " char *p = new char[100];\n"
" foo(p);\n" " foo(p);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err ); ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err );
} }
void func6() void func6()
{ {
check( "static void foo(char *str)\n" check( "static void foo(char *str)\n"
"{\n" "{\n"
" goto abc;\n" " goto abc;\n"
"}\n" "}\n"
"\n" "\n"
"static void f()\n" "static void f()\n"
"{\n" "{\n"
" char *p = new char[100];\n" " char *p = new char[100];\n"
" foo(p);\n" " foo(p);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
/* /*

View File

@ -24,7 +24,7 @@
#include "testsuite.h" #include "testsuite.h"
#define UNIT_TESTING // Get access to "private" data in Tokenizer #define UNIT_TESTING // Get access to "private" data in Tokenizer
#include "tokenize.h" #include "tokenize.h"
extern std::ostringstream errout; extern std::ostringstream errout;
class TestTokenizer : public TestFixture class TestTokenizer : public TestFixture
{ {
@ -41,9 +41,7 @@ private:
TEST_CASE( inlineasm ); TEST_CASE( inlineasm );
TEST_CASE( dupfuncname ); TEST_CASE( dupfuncname );
TEST_CASE( simplifyConditions );
} }
@ -66,7 +64,7 @@ private:
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(filedata); std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0); tokenizer.TokenizeCode(istr, 0);
@ -90,7 +88,7 @@ private:
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(filedata); std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0); tokenizer.TokenizeCode(istr, 0);
@ -112,7 +110,7 @@ private:
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(filedata); std::istringstream istr(filedata);
tokenizer.TokenizeCode(istr, 0); tokenizer.TokenizeCode(istr, 0);
@ -143,7 +141,7 @@ private:
"{ }\n"; "{ }\n";
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode(istr, 0); tokenizer.TokenizeCode(istr, 0);
@ -151,13 +149,7 @@ private:
ASSERT_EQUALS( 1, tokenizer.FunctionList.size() ); ASSERT_EQUALS( 1, tokenizer.FunctionList.size() );
ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str ); ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str );
} }
void simplifyConditions()
{
// TODO Simplify various conditions..
}
}; };
REGISTER_TEST( TestTokenizer ) REGISTER_TEST( TestTokenizer )

View File

@ -17,6 +17,7 @@
*/ */
#define UNIT_TESTING
#include "tokenize.h" #include "tokenize.h"
#include "CheckClass.h" #include "CheckClass.h"
#include "testsuite.h" #include "testsuite.h"
@ -44,17 +45,17 @@ private:
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for unused private functions.. // Check for unused private functions..
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
CheckClass checkClass( &tokenizer, settings, this ); CheckClass checkClass( &tokenizer, settings, this );
checkClass.CheckUnusedPrivateFunctions(); checkClass.CheckUnusedPrivateFunctions();
} }

View File

@ -20,6 +20,7 @@
// Check for unused variables.. // Check for unused variables..
#define UNIT_TESTING
#include "testsuite.h" #include "testsuite.h"
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
@ -38,7 +39,7 @@ private:
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" ); tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.TokenizeCode( istr ); tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList(); tokenizer.SimplifyTokenList();

View File

@ -87,7 +87,7 @@ const TOKEN *Tokenizer::tokens() const
std::vector<std::string> *Tokenizer::getFiles() const std::vector<std::string> *Tokenizer::getFiles() const
{ {
return &Files; return &Files;
} }

View File

@ -98,7 +98,7 @@ public:
void initTokens(); void initTokens();
std::vector<std::string> *getFiles(); const std::vector<std::string> *getFiles() const;
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;