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
//---------------------------------------------------------------------------
CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
{
_tokenizer = tokenizer;
_settings = settings;
_errorLogger = errorLogger;
}
CheckClass::~CheckClass()
{
}
CheckClass::CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
{
_tokenizer = tokenizer;
_settings = settings;
_errorLogger = errorLogger;
}
CheckClass::~CheckClass()
{
}
//---------------------------------------------------------------------------

View File

@ -20,24 +20,24 @@
#ifndef 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
{
public:
CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
~CheckClass();
#include "tokenize.h"
#include "settings.h"
#include "errorlogger.h"
#include <list>
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();
@ -45,17 +45,17 @@ public:
void CheckMemset();
void CheckOperatorEq1(); // Warning upon "void operator=(.."
private:
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[]);
const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel );
struct VAR *ClassChecking_GetVarList(const TOKEN *tok1);
Tokenizer *_tokenizer;
Settings _settings;
ErrorLogger *_errorLogger;
void CheckOperatorEq1(); // Warning upon "void operator=(.."
private:
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[]);
const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel );
struct VAR *ClassChecking_GetVarList(const TOKEN *tok1);
const Tokenizer *_tokenizer;
Settings _settings;
ErrorLogger *_errorLogger;
};
//---------------------------------------------------------------------------
#endif

View File

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

View File

@ -21,20 +21,20 @@
#ifndef CheckHeadersH
#define CheckHeadersH
//---------------------------------------------------------------------------
#include "tokenize.h"
#include "errorlogger.h"
class CheckHeaders
{
public:
CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger );
#include "tokenize.h"
#include "errorlogger.h"
class CheckHeaders
{
public:
CheckHeaders( const Tokenizer *tokenizer, ErrorLogger *errorLogger );
~CheckHeaders();
void WarningHeaderWithImplementation();
void WarningIncludeHeader();
private:
Tokenizer *_tokenizer;
void WarningIncludeHeader();
private:
const Tokenizer *_tokenizer;
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;
_settings = settings;
_tokenizer = tokenizer;
_settings = settings;
_errorLogger = errorLogger;
}
@ -95,7 +95,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
{
if ( strcmp(mallocfunc[i], tok2->str) == 0 )
return Malloc;
}
}
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
const char *gmallocfunc[] = {"g_new",
@ -124,12 +124,12 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
if ( Tokenizer::Match( tok2, "new %type% [" ) )
return NewA;
if ( Tokenizer::Match( tok2, "fopen (" ) )
return FOPEN;
if ( Tokenizer::Match( tok2, "popen (" ) )
return POPEN;
if ( Tokenizer::Match( tok2, "fopen (" ) )
return FOPEN;
if ( Tokenizer::Match( tok2, "popen (" ) )
return POPEN;
// Userdefined allocation function..
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) )
return gMalloc;
if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) )
return FOPEN;
if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) )
return POPEN;
if ( Tokenizer::Match(tok, "fclose ( %var1% )", varnames) )
return FOPEN;
if ( Tokenizer::Match(tok, "pclose ( %var1% )", varnames) )
return POPEN;
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)
return 0;
if ( callstack.size() > 2 )
return "dealloc";
const char *funcname = tok->str;
for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it )
{
if ( std::string(funcname) == (*it)->str )
return "dealloc";
if ( callstack.size() > 2 )
return "dealloc";
const char *funcname = tok->str;
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);
@ -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[] )
{
std::ostringstream errmsg;
for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok )
std::ostringstream errmsg;
for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok )
errmsg << _tokenizer->fileLine(*tok) << " -> ";
errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
_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[] )
{
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << ": Memory leak: " << varname;
_errorLogger->reportErr( errmsg.str() );
@ -370,9 +370,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
dealloctype = dealloc;
}
// if else switch
// if else switch
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) )
{
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) )
{
addtoken("if(!var)");
}
else if ( Tokenizer::Match(tok, "if ( true )") )
{
addtoken("if(true)");
}
else if ( Tokenizer::Match(tok, "if ( false )") )
{
addtoken("if(false)");
}
else if ( Tokenizer::Match(tok, "if") )
{
// Check if the condition depends on var somehow..
bool dep = false;
int parlevel = 0;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if ( Tokenizer::Match(tok2,"(") )
++parlevel;
if ( Tokenizer::Match(tok2,")") )
{
--parlevel;
if ( parlevel <= 0 )
break;
}
if ( !Tokenizer::Match(tok2,".") &&
Tokenizer::Match(tok2->next, "%var1%", varnames) &&
!Tokenizer::Match(tok2->next, "%var1% .", varnames) )
{
dep = true;
break;
}
}
addtoken( (dep ? "ifv" : "if") );
}
}
else if ( Tokenizer::Match(tok, "if ( true )") )
{
addtoken("if(true)");
}
else if ( Tokenizer::Match(tok, "if ( false )") )
{
addtoken("if(false)");
}
else if ( Tokenizer::Match(tok, "if") )
{
// Check if the condition depends on var somehow..
bool dep = false;
int parlevel = 0;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if ( Tokenizer::Match(tok2,"(") )
++parlevel;
if ( Tokenizer::Match(tok2,")") )
{
--parlevel;
if ( parlevel <= 0 )
break;
}
if ( !Tokenizer::Match(tok2,".") &&
Tokenizer::Match(tok2->next, "%var1%", varnames) &&
!Tokenizer::Match(tok2->next, "%var1% .", varnames) )
{
dep = true;
break;
}
}
addtoken( (dep ? "ifv" : "if") );
}
else if ( Tokenizer::Match(tok, "else") || Tokenizer::Match(tok, "switch") )
{
addtoken(tok->str);
@ -605,10 +605,10 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Delete empty if that is not followed by an else
if ( Tokenizer::Match(tok2,"[;{}] if ;") ||
Tokenizer::Match(tok2,"[;{}] if(var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(!var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(true) ;") ||
Tokenizer::Match(tok2,"[;{}] if(false) ;") ||
Tokenizer::Match(tok2,"[;{}] if(var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(!var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(true) ;") ||
Tokenizer::Match(tok2,"[;{}] if(false) ;") ||
Tokenizer::Match(tok2,"[;{}] ifv ;") )
{
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..
// This may cause false positives
if (_settings._showAll &&
// This may cause false positives
if (_settings._showAll &&
(Tokenizer::Match(tok2, "[;{}] if dealloc ;") || Tokenizer::Match(tok2, "[;{}] if use ;")) &&
!Tokenizer::Match(Tokenizer::gettok(tok2,4), "else"))
{
@ -788,12 +788,12 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
{
{
std::list<const TOKEN *> callstack;
AllocType alloctype = No;
AllocType dealloctype = No;
AllocType alloctype = No;
AllocType dealloctype = No;
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
// 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);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv continue ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv continue ;"), 3), varname);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv break ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv break ;"), 3), varname);
}
else if ( _settings._showAll && Tokenizer::findmatch(tok, "alloc ; ifv return ;") )
{
MemoryLeak(Tokenizer::gettok(Tokenizer::findmatch(tok, "alloc ; ifv return ;"), 3), varname);
}
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 ) );
if ( alloc != No )
{
{
std::list<const TOKEN *> callstack;
if ( Dealloc != No && Dealloc != alloc )
MismatchError( tok, callstack, FullVariableName.str().c_str() );
@ -1063,7 +1063,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vec
varnames[0] = varname;
AllocType dealloc = GetDeallocationType( tok, varnames );
if ( dealloc != No )
{
{
std::list<const TOKEN *> callstack;
if ( Dealloc != No && Dealloc != dealloc )
MismatchError( tok, callstack, FullVariableName.str().c_str() );

View File

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

View File

@ -34,9 +34,9 @@
// 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;
}

View File

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

View File

@ -17,10 +17,10 @@
*/
#define UNIT_TESTING
#include "tokenize.h"
#include "CheckBufferOverrun.h"
#include "testsuite.h"
#include "testsuite.h"
#include <sstream>
@ -32,22 +32,22 @@ public:
TestBufferOverrun() : TestFixture("TestBufferOverrun")
{ }
private:
private:
void check( const char code[] )
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
// Tokenize..
Tokenizer tokenizer;
tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();
// Fill function list
Settings settings;
settings._checkCodingStyle = true;
// Fill function list
Settings settings;
settings._checkCodingStyle = true;
tokenizer.settings( settings );
tokenizer.FillFunctionList(0);
@ -323,7 +323,7 @@ private:
"Fred::Fred()\n"
"{\n"
" str[10] = 0;\n"
"}\n" );
"}\n" );
std::string err( errout.str() );
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 "CheckOther.h"
#include "testsuite.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
@ -30,8 +31,8 @@ public:
TestCharVar() : TestFixture("TestCharVar")
{ }
private:
private:
void run()
{
@ -43,14 +44,14 @@ private:
void check( const char code[] )
{
// Tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
Tokenizer tokenizer;
tokenizer.Files.push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode( istr );
// Fill function list
Settings settings;
settings._checkCodingStyle = true;
// Fill function list
Settings settings;
settings._checkCodingStyle = true;
tokenizer.settings( settings );
tokenizer.FillFunctionList(0);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -98,7 +98,7 @@ public:
void initTokens();
std::vector<std::string> *getFiles();
const std::vector<std::string> *getFiles() const;
void FillFunctionList(const unsigned int file_id);
const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;