Refactoring: Removed 'HasErrors'. Better usage of 'Match'. Hid the 'FunctionList'

This commit is contained in:
Daniel Marjamäki 2008-03-28 17:40:24 +00:00
parent 4691999ede
commit f8569f0db4
5 changed files with 23 additions and 43 deletions

View File

@ -92,6 +92,12 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classname[], const char funcname[], unsigned int &indentlevel ) static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classname[], const char funcname[], unsigned int &indentlevel )
{ {
const char *_classname[2] = {0,0};
const char *_funcname[2] = {0,0};
_classname[0] = classname;
_funcname[0] = funcname;
while ( _tokens ) while ( _tokens )
{ {
if ( indentlevel > 0 ) if ( indentlevel > 0 )
@ -103,7 +109,7 @@ static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classna
else if ( indentlevel == 1 ) else if ( indentlevel == 1 )
{ {
// Member function is implemented in the class declaration.. // Member function is implemented in the class declaration..
if ( Match( _tokens, "%var% (" ) && strcmp(_tokens->str,funcname) == 0 ) if ( Match( _tokens, "%var1% (", _funcname ) )
{ {
const TOKEN *tok2 = _tokens; const TOKEN *tok2 = _tokens;
while ( tok2 && tok2->str[0] != '{' && tok2->str[0] != ';' ) while ( tok2 && tok2->str[0] != '{' && tok2->str[0] != ';' )
@ -114,17 +120,13 @@ static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classna
} }
} }
// Todo: Match the classname directly instead else if ( Match(_tokens, "class %var1% {", _classname) )
else if ( Match(_tokens, "class %var% {") && strcmp(getstr(_tokens,1),classname)==0 )
{ {
indentlevel = 1; indentlevel = 1;
_tokens = gettok( _tokens, 2 ); _tokens = gettok( _tokens, 2 );
} }
// Todo: Match the classname and funcname directly instead else if ( Match(_tokens, "%var1% :: %var2% (", _classname, _funcname) )
else if ( Match(_tokens, "%var% :: %var% (") &&
strcmp(_tokens->str,classname) == 0 &&
strcmp(getstr(_tokens,2),funcname) == 0 )
{ {
return _tokens; return _tokens;
} }

View File

@ -6,10 +6,9 @@
#include <list> #include <list>
#include <algorithm> #include <algorithm>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool HasErrors;
bool OnlyReportUniqueErrors; bool OnlyReportUniqueErrors;
std::ostringstream errout; std::ostringstream errout;
std::list<const TOKEN *> FunctionList; static std::list<const TOKEN *> FunctionList;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
std::string FileLine(const TOKEN *tok) std::string FileLine(const TOKEN *tok)
@ -31,7 +30,6 @@ void ReportErr(const std::string &errmsg)
ErrorList.push_back( errmsg ); ErrorList.push_back( errmsg );
} }
errout << errmsg << std::endl; errout << errmsg << std::endl;
HasErrors = true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -61,6 +59,8 @@ bool IsStandardType(const char str[])
void FillFunctionList() void FillFunctionList()
{ {
FunctionList.clear();
int indentlevel = 0; int indentlevel = 0;
for ( const TOKEN *tok = tokens; tok; tok = tok->next ) for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{ {
@ -121,7 +121,7 @@ const TOKEN *GetFunctionTokenByName( const char funcname[] )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool Match(const TOKEN *tok, const char pattern[], const char *varname[]) bool Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
{ {
if (!tok) if (!tok)
return false; return false;
@ -156,8 +156,13 @@ bool Match(const TOKEN *tok, const char pattern[], const char *varname[])
} }
// Variable name.. // Variable name..
else if (strcmp(str,"%var1%")==0) else if (strcmp(str,"%var1%")==0 || strcmp(str,"%var2%")==0)
{ {
const char **varname = (strcmp(str,"%var1%")==0) ? varname1 : varname2;
if ( ! varname )
return false;
if (strcmp(tok->str, varname[0]) != 0) if (strcmp(tok->str, varname[0]) != 0)
return false; return false;

View File

@ -9,8 +9,6 @@
struct TOKEN; struct TOKEN;
extern std::list<const TOKEN *> FunctionList;
std::string FileLine(const TOKEN *tok); std::string FileLine(const TOKEN *tok);
extern bool OnlyReportUniqueErrors; extern bool OnlyReportUniqueErrors;
@ -28,7 +26,7 @@ void FillFunctionList();
const TOKEN *GetFunctionTokenByName( const char funcname[] ); const TOKEN *GetFunctionTokenByName( const char funcname[] );
bool Match(const TOKEN *tok, const char pattern[], const char *varname[]=0); bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -164,11 +164,8 @@ int main(int argc, char* argv[])
// CppCheck - A function that checks a specified file // CppCheck - A function that checks a specified file
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
extern bool HasErrors;
static void CppCheck(const char FileName[]) static void CppCheck(const char FileName[])
{ {
HasErrors = false;
OnlyReportUniqueErrors = true; OnlyReportUniqueErrors = true;
std::cout << "Checking " << FileName << "...\n"; std::cout << "Checking " << FileName << "...\n";
@ -178,7 +175,7 @@ static void CppCheck(const char FileName[])
Files.clear(); Files.clear();
Tokenize(FileName); Tokenize(FileName);
FunctionList.clear(); FillFunctionList();
// 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.
@ -242,11 +239,7 @@ static void CppCheck(const char FileName[])
// Warning upon c-style pointer casts // Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.'); const char *ext = strrchr(FileName, '.');
#ifdef __linux__ if (ext && strcmp(ext,".cpp")==0)
if (ext && strcasecmp(ext,".c"))
#else
if (ext && stricmp(ext,".c"))
#endif
WarningOldStylePointerCast(); WarningOldStylePointerCast();
// Use standard functions instead // Use standard functions instead
@ -269,8 +262,7 @@ static void CppCheck(const char FileName[])
// Clean up tokens.. // Clean up tokens..
DeallocateTokens(); DeallocateTokens();
// Todo: How should this work? Activated by a command line switch? if ( errout.str().empty() )
if ( ! HasErrors )
std::cout << "No errors found\n"; std::cout << "No errors found\n";
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -278,19 +270,3 @@ static void CppCheck(const char FileName[])

View File

@ -86,7 +86,6 @@ static void check(void (chk)(),
if ( chk != CheckUnsignedDivision ) if ( chk != CheckUnsignedDivision )
SimplifyTokenList(); SimplifyTokenList();
FunctionList.clear();
FillFunctionList(); FillFunctionList();
// Check for buffer overruns.. // Check for buffer overruns..