Refactoring: Errors are no longer logged via global functions to a global stream. Instead callback function is used via interface.
ErrorLogger interface was added and taken into use.
This commit is contained in:
parent
180e3b99e6
commit
51e9d98a5f
|
@ -26,7 +26,8 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h> // <- strtoul
|
#include <stdlib.h> // <- strtoul
|
||||||
|
|
||||||
|
@ -35,9 +36,10 @@
|
||||||
// CallStack used when parsing into subfunctions.
|
// CallStack used when parsing into subfunctions.
|
||||||
|
|
||||||
|
|
||||||
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer )
|
CheckBufferOverrunClass::CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||||
{
|
{
|
||||||
_tokenizer = tokenizer;
|
_tokenizer = tokenizer;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckBufferOverrunClass::~CheckBufferOverrunClass()
|
CheckBufferOverrunClass::~CheckBufferOverrunClass()
|
||||||
|
@ -53,7 +55,7 @@ void CheckBufferOverrunClass::ReportError(const TOKEN *tok, const char errmsg[])
|
||||||
for ( it = CallStack.begin(); it != CallStack.end(); it++ )
|
for ( it = CallStack.begin(); it != CallStack.end(); it++ )
|
||||||
ostr << _tokenizer->fileLine(*it ) << " -> ";
|
ostr << _tokenizer->fileLine(*it ) << " -> ";
|
||||||
ostr << _tokenizer->fileLine(tok) << ": " << errmsg;
|
ostr << _tokenizer->fileLine(tok) << ": " << errmsg;
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -504,14 +506,14 @@ void CheckBufferOverrunClass::WarningDangerousFunctions()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
|
ostr << _tokenizer->fileLine(tok) << ": Found 'gets'. You should use 'fgets' instead";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0)
|
else if (Match(tok, "scanf (") && strcmp(Tokenizer::getstr(tok,2),"\"%s\"") == 0)
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
|
ostr << _tokenizer->fileLine(tok) << ": Found 'scanf'. You should use 'fgets' instead";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,12 @@
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
class CheckBufferOverrunClass
|
class CheckBufferOverrunClass
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CheckBufferOverrunClass( Tokenizer *tokenizer );
|
CheckBufferOverrunClass( Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||||
~CheckBufferOverrunClass();
|
~CheckBufferOverrunClass();
|
||||||
|
|
||||||
// Buffer overrun..
|
// Buffer overrun..
|
||||||
|
@ -43,7 +44,8 @@ private:
|
||||||
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;
|
ErrorLogger *_errorLogger;
|
||||||
|
std::list<const TOKEN *> CallStack;
|
||||||
};
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -34,10 +34,11 @@
|
||||||
#endif
|
#endif
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings )
|
CheckClass::CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
|
||||||
{
|
{
|
||||||
_tokenizer = tokenizer;
|
_tokenizer = tokenizer;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckClass::~CheckClass()
|
CheckClass::~CheckClass()
|
||||||
|
@ -339,7 +340,7 @@ void CheckClass::CheckConstructors()
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok1);
|
ostr << _tokenizer->fileLine(tok1);
|
||||||
ostr << " The class '" << classname << "' has no constructor";
|
ostr << " The class '" << classname << "' has no constructor";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
// Delete the varlist..
|
// Delete the varlist..
|
||||||
while (varlist)
|
while (varlist)
|
||||||
|
@ -378,7 +379,7 @@ void CheckClass::CheckConstructors()
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(constructor_token);
|
ostr << _tokenizer->fileLine(constructor_token);
|
||||||
ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'";
|
ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -533,7 +534,7 @@ void CheckClass::CheckUnusedPrivateFunctions()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'";
|
ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
FuncList.pop_front();
|
FuncList.pop_front();
|
||||||
}
|
}
|
||||||
|
@ -576,7 +577,7 @@ void CheckClass::CheckMemset()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on class.";
|
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on class.";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -592,7 +593,7 @@ void CheckClass::CheckMemset()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on struct that contains a 'std::" << Tokenizer::getstr(tstruct,2) << "'";
|
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->str << "' on struct that contains a 'std::" << Tokenizer::getstr(tstruct,2) << "'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -613,7 +614,7 @@ void CheckClass::CheckOperatorEq1()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": 'operator=' should return something";
|
ostr << _tokenizer->fileLine(tok) << ": 'operator=' should return something";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
struct VAR
|
struct VAR
|
||||||
|
@ -35,7 +36,7 @@ struct VAR
|
||||||
class CheckClass
|
class CheckClass
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CheckClass( Tokenizer *tokenizer, const Settings &settings );
|
CheckClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
|
||||||
~CheckClass();
|
~CheckClass();
|
||||||
|
|
||||||
void CheckConstructors();
|
void CheckConstructors();
|
||||||
|
@ -54,6 +55,7 @@ private:
|
||||||
|
|
||||||
Tokenizer *_tokenizer;
|
Tokenizer *_tokenizer;
|
||||||
Settings _settings;
|
Settings _settings;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -20,7 +20,8 @@
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
#include "CheckHeaders.h"
|
#include "CheckHeaders.h"
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "CommonCheck.h"
|
#include "CommonCheck.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -35,9 +36,10 @@
|
||||||
// HEADERS - No implementation in a header
|
// HEADERS - No implementation in a header
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CheckHeaders::CheckHeaders( Tokenizer *tokenizer )
|
CheckHeaders::CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||||
{
|
{
|
||||||
_tokenizer = tokenizer;
|
_tokenizer = tokenizer;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckHeaders::~CheckHeaders()
|
CheckHeaders::~CheckHeaders()
|
||||||
|
@ -57,7 +59,7 @@ void CheckHeaders::WarningHeaderWithImplementation()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Found implementation in header";
|
ostr << _tokenizer->fileLine(tok) << ": Found implementation in header";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
|
|
||||||
// Goto next file..
|
// Goto next file..
|
||||||
unsigned int fileindex = tok->FileIndex;
|
unsigned int fileindex = tok->FileIndex;
|
||||||
|
@ -245,7 +247,7 @@ void CheckHeaders::WarningIncludeHeader()
|
||||||
ostr << _tokenizer->fileLine(includetok) << ": The included header '" << includefile << "' is not needed";
|
ostr << _tokenizer->fileLine(includetok) << ": The included header '" << includefile << "' is not needed";
|
||||||
if (NeedDeclaration)
|
if (NeedDeclaration)
|
||||||
ostr << " (but a forward declaration is needed)";
|
ostr << " (but a forward declaration is needed)";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,17 +23,19 @@
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
class CheckHeaders
|
class CheckHeaders
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CheckHeaders( Tokenizer *tokenizer );
|
CheckHeaders( Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||||
~CheckHeaders();
|
~CheckHeaders();
|
||||||
void WarningHeaderWithImplementation();
|
void WarningHeaderWithImplementation();
|
||||||
void WarningIncludeHeader();
|
void WarningIncludeHeader();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Tokenizer *_tokenizer;
|
Tokenizer *_tokenizer;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "CommonCheck.h"
|
#include "CommonCheck.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h> // free
|
#include <stdlib.h> // free
|
||||||
|
|
||||||
|
@ -41,10 +42,11 @@
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings )
|
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
|
||||||
{
|
{
|
||||||
_tokenizer = tokenizer;
|
_tokenizer = tokenizer;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckMemoryLeakClass::~CheckMemoryLeakClass()
|
CheckMemoryLeakClass::~CheckMemoryLeakClass()
|
||||||
|
@ -239,15 +241,15 @@ void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<con
|
||||||
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;
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
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;
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ enum AllocType { No, Malloc, gMalloc, New, NewA };
|
||||||
class CheckMemoryLeakClass
|
class CheckMemoryLeakClass
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings );
|
CheckMemoryLeakClass( Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
|
||||||
~CheckMemoryLeakClass();
|
~CheckMemoryLeakClass();
|
||||||
void CheckMemoryLeak();
|
void CheckMemoryLeak();
|
||||||
|
|
||||||
|
@ -73,6 +74,7 @@ private:
|
||||||
bool isclass( const std::string &typestr );
|
bool isclass( const std::string &typestr );
|
||||||
|
|
||||||
Tokenizer *_tokenizer;
|
Tokenizer *_tokenizer;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
Settings _settings;
|
Settings _settings;
|
||||||
std::list<AllocFunc> listallocfunc;
|
std::list<AllocFunc> listallocfunc;
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,7 +20,8 @@
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
#include "CheckOther.h"
|
#include "CheckOther.h"
|
||||||
|
|
||||||
#include "CommonCheck.h"
|
#include "CommonCheck.h"
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -35,9 +36,10 @@
|
||||||
// Warning on C-Style casts.. p = (kalle *)foo;
|
// Warning on C-Style casts.. p = (kalle *)foo;
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CheckOther::CheckOther( Tokenizer *tokenizer )
|
CheckOther::CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger )
|
||||||
{
|
{
|
||||||
_tokenizer = tokenizer;
|
_tokenizer = tokenizer;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckOther::~CheckOther()
|
CheckOther::~CheckOther()
|
||||||
|
@ -61,7 +63,7 @@ void CheckOther::WarningOldStylePointerCast()
|
||||||
|
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting";
|
ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +87,7 @@ void CheckOther::WarningIsDigit()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isdigit'";
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isdigit'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,7 +116,7 @@ void CheckOther::WarningIsAlpha()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isupper'";
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isupper'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +129,7 @@ void CheckOther::WarningIsAlpha()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'islower'";
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'islower'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +146,7 @@ void CheckOther::WarningIsAlpha()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isalpha'";
|
ostr << _tokenizer->fileLine(tok) << ": The condition can be simplified; use 'isalpha'";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,7 +200,7 @@ void CheckOther::WarningRedundantCode()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
|
ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,7 +242,7 @@ void CheckOther::WarningIf()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\"";
|
ostr << _tokenizer->fileLine(tok) << ": Found \"if (condition);\"";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -296,7 +298,7 @@ void CheckOther::WarningIf()
|
||||||
if (strcmp(cond, p[i]) == 0)
|
if (strcmp(cond, p[i]) == 0)
|
||||||
ostr << (i < 3 ? "True" : "False");
|
ostr << (i < 3 ? "True" : "False");
|
||||||
}
|
}
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
@ -336,7 +338,7 @@ void CheckOther::InvalidFunctionUsage()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok2) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36";
|
ostr << _tokenizer->fileLine(tok2) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -362,7 +364,7 @@ void CheckOther::CheckIfAssignment()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?";
|
ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -401,7 +403,7 @@ void CheckOther::CheckUnsignedDivision()
|
||||||
// One of the operands are signed, the other is unsigned..
|
// One of the operands are signed, the other is unsigned..
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok->next) << ": Warning: Division with signed and unsigned operators";
|
ostr << _tokenizer->fileLine(tok->next) << ": Warning: Division with signed and unsigned operators";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -413,7 +415,7 @@ void CheckOther::CheckUnsignedDivision()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
|
ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -425,7 +427,7 @@ void CheckOther::CheckUnsignedDivision()
|
||||||
{
|
{
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
|
ostr << _tokenizer->fileLine(tok->next) << ": Unsigned division. The result will be wrong.";
|
||||||
ReportErr(ostr.str());
|
_errorLogger->reportErr(ostr.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -585,7 +587,7 @@ void CheckOther::CheckVariableScope_LookupVar( const TOKEN *tok1, const char var
|
||||||
// Warning if "used" is true
|
// Warning if "used" is true
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok1) << " The scope of the variable '" << varname << "' can be limited";
|
errmsg << _tokenizer->fileLine(tok1) << " The scope of the variable '" << varname << "' can be limited";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -602,7 +604,7 @@ void CheckOther::CheckConstantFunctionParameter()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
|
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( Match(tok,"[,(] const %type% %var% [,)]") )
|
else if ( Match(tok,"[,(] const %type% %var% [,)]") )
|
||||||
|
@ -614,14 +616,14 @@ void CheckOther::CheckConstantFunctionParameter()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
pattern[0] = "struct";
|
pattern[0] = "struct";
|
||||||
if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) )
|
if ( Tokenizer::findtoken(_tokenizer->tokens(), pattern) )
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
errmsg << _tokenizer->fileLine(tok) << " " << Tokenizer::getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -682,7 +684,7 @@ void CheckOther::CheckStructMemberUsage()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok) << ": struct member '" << structname << "::" << varname << "' is never read";
|
errmsg << _tokenizer->fileLine(tok) << ": struct member '" << structname << "::" << varname << "' is never read";
|
||||||
ReportErr(errmsg.str());
|
_errorLogger->reportErr(errmsg.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -724,7 +726,7 @@ void CheckOther::CheckCharVariable()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok2->next) << ": Warning - using char variable as array index";
|
errmsg << _tokenizer->fileLine(tok2->next) << ": Warning - using char variable as array index";
|
||||||
ReportErr(errmsg.str());
|
_errorLogger->reportErr(errmsg.str());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -732,7 +734,7 @@ void CheckOther::CheckCharVariable()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation";
|
errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation";
|
||||||
ReportErr(errmsg.str());
|
_errorLogger->reportErr(errmsg.str());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -768,14 +770,14 @@ void CheckOther::CheckIncompleteStatement()
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant";
|
errmsg << _tokenizer->fileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant";
|
||||||
ReportErr(errmsg.str());
|
_errorLogger->reportErr(errmsg.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(Tokenizer::gettok(tok,3), ",") )
|
if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(Tokenizer::gettok(tok,3), ",") )
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant";
|
errmsg << _tokenizer->fileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant";
|
||||||
ReportErr(errmsg.str());
|
_errorLogger->reportErr(errmsg.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,13 @@
|
||||||
#define CheckOtherH
|
#define CheckOtherH
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
class CheckOther
|
class CheckOther
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CheckOther( Tokenizer *tokenizer );
|
CheckOther( Tokenizer *tokenizer, ErrorLogger *errorLogger );
|
||||||
~CheckOther();
|
~CheckOther();
|
||||||
|
|
||||||
// Casting
|
// Casting
|
||||||
|
@ -75,7 +76,8 @@ public:
|
||||||
private:
|
private:
|
||||||
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
|
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
|
||||||
|
|
||||||
Tokenizer *_tokenizer;
|
Tokenizer *_tokenizer;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -32,8 +32,7 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#endif
|
#endif
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
bool OnlyReportUniqueErrors;
|
|
||||||
std::ostringstream errout;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,18 +55,7 @@ bool SameFileName( const char fname1[], const char fname2[] )
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
std::list<std::string> ErrorList;
|
|
||||||
|
|
||||||
void ReportErr(const std::string &errmsg)
|
|
||||||
{
|
|
||||||
if ( OnlyReportUniqueErrors )
|
|
||||||
{
|
|
||||||
if ( std::find( ErrorList.begin(), ErrorList.end(), errmsg ) != ErrorList.end() )
|
|
||||||
return;
|
|
||||||
ErrorList.push_back( errmsg );
|
|
||||||
}
|
|
||||||
errout << errmsg << std::endl;
|
|
||||||
}
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
bool IsName(const char str[])
|
bool IsName(const char str[])
|
||||||
|
|
|
@ -32,25 +32,10 @@
|
||||||
|
|
||||||
// Are two filenames the same? Case insensitive on windows
|
// Are two filenames the same? Case insensitive on windows
|
||||||
bool SameFileName( const char fname1[], const char fname2[] );
|
bool SameFileName( const char fname1[], const char fname2[] );
|
||||||
|
|
||||||
extern bool OnlyReportUniqueErrors;
|
|
||||||
|
|
||||||
void ReportErr(const std::string &errmsg);
|
|
||||||
extern std::ostringstream errout;
|
|
||||||
|
|
||||||
|
|
||||||
bool IsName(const char str[]);
|
bool IsName(const char str[]);
|
||||||
bool IsNumber(const char str[]);
|
bool IsNumber(const char str[]);
|
||||||
|
|
||||||
bool IsStandardType(const char str[]);
|
bool IsStandardType(const char str[]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
#endif
|
#endif
|
||||||
|
|
30
cppcheck.cpp
30
cppcheck.cpp
|
@ -36,7 +36,7 @@
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
CppCheck::CppCheck()
|
CppCheck::CppCheck() : _tokenizer( this )
|
||||||
{
|
{
|
||||||
_settings._debug = false;
|
_settings._debug = false;
|
||||||
_settings._showAll = false;
|
_settings._showAll = false;
|
||||||
|
@ -138,7 +138,7 @@ void CppCheck::check(int argc, char* argv[])
|
||||||
|
|
||||||
std::ifstream fin( fname.c_str() );
|
std::ifstream fin( fname.c_str() );
|
||||||
std::map<std::string, std::string> code;
|
std::map<std::string, std::string> code;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess(fin, code, fname);
|
preprocessor.preprocess(fin, code, fname);
|
||||||
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
||||||
checkFile(it->second, filenames[c].c_str(), c);
|
checkFile(it->second, filenames[c].c_str(), c);
|
||||||
|
@ -182,9 +182,6 @@ void CppCheck::check(int argc, char* argv[])
|
||||||
|
|
||||||
void CppCheck::checkFile(const std::string &code, const char FileName[], unsigned int FileId)
|
void CppCheck::checkFile(const std::string &code, const char FileName[], unsigned int FileId)
|
||||||
{
|
{
|
||||||
|
|
||||||
OnlyReportUniqueErrors = true;
|
|
||||||
|
|
||||||
// Tokenize the file
|
// Tokenize the file
|
||||||
{
|
{
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
|
@ -196,13 +193,13 @@ 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, _settings );
|
CheckClass checkClass( &_tokenizer, _settings, this );
|
||||||
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( &_tokenizer );
|
CheckOther checkOther( &_tokenizer, this );
|
||||||
checkOther.CheckUnsignedDivision();
|
checkOther.CheckUnsignedDivision();
|
||||||
|
|
||||||
// Give warning when using char variable as array index
|
// Give warning when using char variable as array index
|
||||||
|
@ -222,11 +219,11 @@ void CppCheck::checkFile(const std::string &code, const char FileName[], unsigne
|
||||||
_tokenizer.SimplifyTokenList();
|
_tokenizer.SimplifyTokenList();
|
||||||
|
|
||||||
// Memory leak
|
// Memory leak
|
||||||
CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings );
|
CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings, this );
|
||||||
checkMemoryLeak.CheckMemoryLeak();
|
checkMemoryLeak.CheckMemoryLeak();
|
||||||
|
|
||||||
// Buffer overruns..
|
// Buffer overruns..
|
||||||
CheckBufferOverrunClass checkBufferOverrun( &_tokenizer );
|
CheckBufferOverrunClass checkBufferOverrun( &_tokenizer, this );
|
||||||
checkBufferOverrun.CheckBufferOverrun();
|
checkBufferOverrun.CheckBufferOverrun();
|
||||||
|
|
||||||
// Check that all class constructors are ok.
|
// Check that all class constructors are ok.
|
||||||
|
@ -298,4 +295,19 @@ void CppCheck::checkFile(const std::string &code, const char FileName[], unsigne
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void CppCheck::reportErr( const std::string &errmsg)
|
||||||
|
{
|
||||||
|
if ( /*OnlyReportUniqueErrors*/ true )
|
||||||
|
{
|
||||||
|
if ( std::find( _errorList.begin(), _errorList.end(), errmsg ) != _errorList.end() )
|
||||||
|
return;
|
||||||
|
_errorList.push_back( errmsg );
|
||||||
|
}
|
||||||
|
errout << errmsg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CppCheck::reportErr( const TOKEN *token, const std::string &errmsg)
|
||||||
|
{
|
||||||
|
std::string message = _tokenizer.fileLine( token ) + errmsg;
|
||||||
|
reportErr( message );
|
||||||
|
}
|
||||||
|
|
11
cppcheck.h
11
cppcheck.h
|
@ -20,24 +20,33 @@
|
||||||
#define CPPCHECK_H
|
#define CPPCHECK_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
#include <sstream>
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "tokenize.h" // <- Tokenizer
|
#include "tokenize.h" // <- Tokenizer
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the base class which will use other classes to do
|
* This is the base class which will use other classes to do
|
||||||
* static code analysis for C and C++ code to find possible
|
* static code analysis for C and C++ code to find possible
|
||||||
* errors or places that could be improved.
|
* errors or places that could be improved.
|
||||||
*/
|
*/
|
||||||
class CppCheck
|
class CppCheck : public ErrorLogger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CppCheck();
|
CppCheck();
|
||||||
virtual ~CppCheck();
|
virtual ~CppCheck();
|
||||||
void check(int argc, char* argv[]);
|
void check(int argc, char* argv[]);
|
||||||
|
|
||||||
|
virtual void reportErr( const TOKEN *token, const std::string &errmsg);
|
||||||
|
virtual void reportErr( const std::string &errmsg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void checkFile(const std::string &code, const char FileName[], unsigned int FileId);
|
void checkFile(const std::string &code, const char FileName[], unsigned int FileId);
|
||||||
Settings _settings;
|
Settings _settings;
|
||||||
Tokenizer _tokenizer;
|
Tokenizer _tokenizer;
|
||||||
|
std::list<std::string> _errorList;
|
||||||
|
std::ostringstream errout;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CPPCHECK_H
|
#endif // CPPCHECK_H
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* c++check - c/c++ syntax checking
|
||||||
|
* Copyright (C) 2007-2008 Daniel Marjamäki and Reijo Tomperi
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ERRORLOGGER_H
|
||||||
|
#define ERRORLOGGER_H
|
||||||
|
|
||||||
|
class TOKEN;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is an interface, which the class responsible of error logging
|
||||||
|
* should implement.
|
||||||
|
*/
|
||||||
|
class ErrorLogger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void reportErr( const std::string &errmsg) = 0;
|
||||||
|
virtual void reportErr( const TOKEN *token, const std::string &errmsg) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // #ifndef ERRORLOGGER_H
|
|
@ -29,7 +29,10 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
Preprocessor::Preprocessor( ErrorLogger *errorLogger )
|
||||||
|
{
|
||||||
|
_errorLogger = errorLogger;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the code for each configuration
|
* Extract the code for each configuration
|
||||||
|
@ -48,8 +51,10 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
|
||||||
{
|
{
|
||||||
if ( ch < 0 )
|
if ( ch < 0 )
|
||||||
{
|
{
|
||||||
// Bad content..
|
// Bad content..
|
||||||
errout << "[" << filename << ":" << lineno << "] Bad character found: " << int((unsigned char)ch) << std::endl;
|
std::ostringstream oss;
|
||||||
|
oss << "[" << filename << ":" << lineno << "] Bad character found: " << int((unsigned char)ch) << std::endl;
|
||||||
|
_errorLogger->reportErr( oss.str() );
|
||||||
result.clear();
|
result.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,12 @@
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
class Preprocessor
|
class Preprocessor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Preprocessor( ErrorLogger *errorLogger );
|
||||||
void preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename);
|
void preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename);
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
|
@ -44,6 +46,8 @@ private:
|
||||||
std::string getdef(std::string line, bool def);
|
std::string getdef(std::string line, bool def);
|
||||||
|
|
||||||
bool match_cfg_def( std::string cfg, const std::string &def );
|
bool match_cfg_def( std::string cfg, const std::string &def );
|
||||||
|
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "CommonCheck.h"
|
#include "CommonCheck.h"
|
||||||
#include "CheckBufferOverrun.h"
|
#include "CheckBufferOverrun.h"
|
||||||
#include "testsuite.h"
|
#include "testsuite.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -33,11 +33,14 @@ 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( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -53,7 +56,7 @@ private:
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
// Check for memory leaks..
|
// Check for memory leaks..
|
||||||
CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
|
CheckBufferOverrunClass checkBufferOverrun( &tokenizer, this );
|
||||||
checkBufferOverrun.CheckBufferOverrun();
|
checkBufferOverrun.CheckBufferOverrun();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -20,8 +20,7 @@
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "CommonCheck.h"
|
#include "CommonCheck.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;
|
||||||
|
@ -32,7 +31,9 @@ public:
|
||||||
TestCharVar() : TestFixture("TestCharVar")
|
TestCharVar() : TestFixture("TestCharVar")
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
TEST_CASE( array_index );
|
TEST_CASE( array_index );
|
||||||
|
@ -43,7 +44,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -58,7 +59,7 @@ private:
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
// Check for memory leaks..
|
// Check for memory leaks..
|
||||||
CheckOther checkOther( &tokenizer );
|
CheckOther checkOther( &tokenizer, this );
|
||||||
checkOther.CheckCharVariable();
|
checkOther.CheckCharVariable();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -20,8 +20,7 @@
|
||||||
|
|
||||||
#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;
|
||||||
|
@ -32,11 +31,13 @@ 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( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -48,7 +49,7 @@ private:
|
||||||
// Check for memory leaks..
|
// Check for memory leaks..
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings._checkCodingStyle = true;
|
settings._checkCodingStyle = true;
|
||||||
CheckClass checkClass( &tokenizer, settings );
|
CheckClass checkClass( &tokenizer, settings, this );
|
||||||
checkClass.CheckConstructors();
|
checkClass.CheckConstructors();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -39,7 +39,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -49,7 +49,7 @@ private:
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
// Check for memory leaks..
|
// Check for memory leaks..
|
||||||
CheckOther checkOther( &tokenizer );
|
CheckOther checkOther( &tokenizer, this );
|
||||||
checkOther.CheckUnsignedDivision();
|
checkOther.CheckUnsignedDivision();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -38,7 +38,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -48,7 +48,7 @@ private:
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
// Check for unused variables..
|
// Check for unused variables..
|
||||||
CheckOther checkOther( &tokenizer );
|
CheckOther checkOther( &tokenizer, this );
|
||||||
checkOther.CheckIncompleteStatement();
|
checkOther.CheckIncompleteStatement();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -38,7 +38,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -53,7 +53,7 @@ private:
|
||||||
settings._showAll = false;
|
settings._showAll = false;
|
||||||
tokenizer.settings( settings );
|
tokenizer.settings( settings );
|
||||||
tokenizer.FillFunctionList(0);
|
tokenizer.FillFunctionList(0);
|
||||||
CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings );
|
CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this );
|
||||||
checkMemoryLeak.CheckMemoryLeak();
|
checkMemoryLeak.CheckMemoryLeak();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -140,7 +140,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -164,7 +164,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -187,7 +187,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -213,7 +213,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -237,7 +237,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -264,7 +264,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -287,7 +287,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -310,7 +310,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -330,7 +330,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -355,7 +355,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -375,7 +375,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -400,7 +400,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
@ -420,7 +420,7 @@ private:
|
||||||
// Preprocess => actual result..
|
// Preprocess => actual result..
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
std::map<std::string, std::string> actual;
|
std::map<std::string, std::string> actual;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor( this );
|
||||||
preprocessor.preprocess( istr, actual, "" );
|
preprocessor.preprocess( istr, actual, "" );
|
||||||
|
|
||||||
// Compare results..
|
// Compare results..
|
||||||
|
|
|
@ -15,8 +15,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "testsuite.h"
|
#include "testsuite.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
extern std::ostringstream errout;
|
||||||
|
|
||||||
class TestSimplifyTokens : public TestFixture
|
class TestSimplifyTokens : public TestFixture
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -37,10 +39,10 @@ private:
|
||||||
TEST_CASE( sizeof1 );
|
TEST_CASE( sizeof1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string tok(const char code[])
|
std::string tok(const char code[])
|
||||||
{
|
{
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
tokenizer.SimplifyTokenList();
|
tokenizer.SimplifyTokenList();
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
std::ostringstream errout;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestRegistry
|
* TestRegistry
|
||||||
|
@ -130,4 +130,13 @@ void TestFixture::runTests(const char cmd[])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TestFixture::reportErr( const std::string &errmsg)
|
||||||
|
{
|
||||||
|
errout << errmsg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestFixture::reportErr( const TOKEN *token, const std::string &errmsg)
|
||||||
|
{
|
||||||
|
reportErr( errmsg );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
12
testsuite.h
12
testsuite.h
|
@ -20,9 +20,11 @@
|
||||||
|
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
|
class TOKEN;
|
||||||
|
|
||||||
|
class TestFixture : public ErrorLogger
|
||||||
class TestFixture
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
static std::ostringstream errmsg;
|
static std::ostringstream errmsg;
|
||||||
|
@ -37,7 +39,11 @@ protected:
|
||||||
bool runTest(const char testname[]);
|
bool runTest(const char testname[]);
|
||||||
void assertFail(const char *filename, int linenr);
|
void assertFail(const char *filename, int linenr);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void reportErr( const std::string &errmsg);
|
||||||
|
|
||||||
|
void reportErr( const TOKEN *token, const std::string &errmsg);
|
||||||
|
|
||||||
TestFixture(const std::string &_name);
|
TestFixture(const std::string &_name);
|
||||||
virtual ~TestFixture() { }
|
virtual ~TestFixture() { }
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,8 @@
|
||||||
#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;
|
||||||
class TestTokenizer : public TestFixture
|
class TestTokenizer : public TestFixture
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -62,7 +63,7 @@ private:
|
||||||
" \"def\"\n";
|
" \"def\"\n";
|
||||||
|
|
||||||
// tokenize..
|
// tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
tokenizer.TokenizeCode(istr, 0);
|
tokenizer.TokenizeCode(istr, 0);
|
||||||
|
@ -88,7 +89,7 @@ private:
|
||||||
std::string filedata(10000,'a');
|
std::string filedata(10000,'a');
|
||||||
|
|
||||||
// tokenize..
|
// tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
tokenizer.TokenizeCode(istr, 0);
|
tokenizer.TokenizeCode(istr, 0);
|
||||||
|
@ -112,7 +113,7 @@ private:
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
// tokenize..
|
// tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(filedata);
|
std::istringstream istr(filedata);
|
||||||
tokenizer.TokenizeCode(istr, 0);
|
tokenizer.TokenizeCode(istr, 0);
|
||||||
|
@ -145,7 +146,7 @@ private:
|
||||||
"void b()\n"
|
"void b()\n"
|
||||||
"{ }\n";
|
"{ }\n";
|
||||||
// tokenize..
|
// tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode(istr, 0);
|
tokenizer.TokenizeCode(istr, 0);
|
||||||
|
|
|
@ -44,7 +44,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -55,7 +55,7 @@ private:
|
||||||
// Check for unused private functions..
|
// Check for unused private functions..
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings._checkCodingStyle = true;
|
settings._checkCodingStyle = true;
|
||||||
CheckClass checkClass( &tokenizer, settings );
|
CheckClass checkClass( &tokenizer, settings, this );
|
||||||
checkClass.CheckUnusedPrivateFunctions();
|
checkClass.CheckUnusedPrivateFunctions();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
#include "CheckOther.h"
|
#include "CheckOther.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
extern std::ostringstream errout;
|
extern std::ostringstream errout;
|
||||||
|
|
||||||
class TestUnusedVar : public TestFixture
|
class TestUnusedVar : public TestFixture
|
||||||
|
@ -38,7 +37,7 @@ private:
|
||||||
void check( const char code[] )
|
void check( const char code[] )
|
||||||
{
|
{
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer( this );
|
||||||
tokenizer.getFiles()->push_back( "test.cpp" );
|
tokenizer.getFiles()->push_back( "test.cpp" );
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.TokenizeCode( istr );
|
tokenizer.TokenizeCode( istr );
|
||||||
|
@ -48,7 +47,7 @@ private:
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
// Check for unused variables..
|
// Check for unused variables..
|
||||||
CheckOther checkOther( &tokenizer );
|
CheckOther checkOther( &tokenizer, this );
|
||||||
checkOther.CheckStructMemberUsage();
|
checkOther.CheckStructMemberUsage();
|
||||||
|
|
||||||
tokenizer.DeallocateTokens();
|
tokenizer.DeallocateTokens();
|
||||||
|
|
12
tokenize.cpp
12
tokenize.cpp
|
@ -19,7 +19,8 @@
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "CommonCheck.h" // <- IsName
|
#include "CommonCheck.h" // <- IsName
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
@ -46,11 +47,12 @@
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
Tokenizer::Tokenizer()
|
Tokenizer::Tokenizer(ErrorLogger *errorLogger)
|
||||||
{
|
{
|
||||||
_tokens = 0;
|
_tokens = 0;
|
||||||
tokens_back = 0;
|
tokens_back = 0;
|
||||||
dsymlist = 0;
|
dsymlist = 0;
|
||||||
|
_errorLogger = errorLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tokenizer::~Tokenizer()
|
Tokenizer::~Tokenizer()
|
||||||
|
@ -1293,14 +1295,14 @@ void Tokenizer::CheckGlobalFunctionUsage(const std::vector<std::string> &filenam
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << "[" << filenames[func->file_id()] << "]: "
|
errmsg << "[" << filenames[func->file_id()] << "]: "
|
||||||
<< "The function '" << func->name() << "' is never used.";
|
<< "The function '" << func->name() << "' is never used.";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
else if ( ! UsedOtherFile )
|
else if ( ! UsedOtherFile )
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << "[" << filenames[func->file_id()] << "]: "
|
errmsg << "[" << filenames[func->file_id()] << "]: "
|
||||||
<< "The linkage of the function '" << func->name() << "' can be local (static) instead of global";
|
<< "The linkage of the function '" << func->name() << "' can be local (static) instead of global";
|
||||||
ReportErr( errmsg.str() );
|
_errorLogger->reportErr( errmsg.str() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,8 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "errorlogger.h"
|
||||||
|
|
||||||
class TOKEN
|
class TOKEN
|
||||||
{
|
{
|
||||||
|
@ -62,7 +63,7 @@ public:
|
||||||
class Tokenizer
|
class Tokenizer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Tokenizer();
|
Tokenizer(ErrorLogger *errorLogger);
|
||||||
~Tokenizer();
|
~Tokenizer();
|
||||||
|
|
||||||
void Tokenize(std::istream &code, const char FileName[]);
|
void Tokenize(std::istream &code, const char FileName[]);
|
||||||
|
@ -152,7 +153,8 @@ private:
|
||||||
|
|
||||||
|
|
||||||
struct DefineSymbol * dsymlist;
|
struct DefineSymbol * dsymlist;
|
||||||
TOKEN *_tokens;
|
TOKEN *_tokens;
|
||||||
|
ErrorLogger *_errorLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue