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 )
{
const char *_classname[2] = {0,0};
const char *_funcname[2] = {0,0};
_classname[0] = classname;
_funcname[0] = funcname;
while ( _tokens )
{
if ( indentlevel > 0 )
@ -103,7 +109,7 @@ static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classna
else if ( indentlevel == 1 )
{
// 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;
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 %var% {") && strcmp(getstr(_tokens,1),classname)==0 )
else if ( Match(_tokens, "class %var1% {", _classname) )
{
indentlevel = 1;
_tokens = gettok( _tokens, 2 );
}
// Todo: Match the classname and funcname directly instead
else if ( Match(_tokens, "%var% :: %var% (") &&
strcmp(_tokens->str,classname) == 0 &&
strcmp(getstr(_tokens,2),funcname) == 0 )
else if ( Match(_tokens, "%var1% :: %var2% (", _classname, _funcname) )
{
return _tokens;
}

View File

@ -6,10 +6,9 @@
#include <list>
#include <algorithm>
//---------------------------------------------------------------------------
bool HasErrors;
bool OnlyReportUniqueErrors;
std::ostringstream errout;
std::list<const TOKEN *> FunctionList;
static std::list<const TOKEN *> FunctionList;
//---------------------------------------------------------------------------
std::string FileLine(const TOKEN *tok)
@ -31,7 +30,6 @@ void ReportErr(const std::string &errmsg)
ErrorList.push_back( errmsg );
}
errout << errmsg << std::endl;
HasErrors = true;
}
//---------------------------------------------------------------------------
@ -61,6 +59,8 @@ bool IsStandardType(const char str[])
void FillFunctionList()
{
FunctionList.clear();
int indentlevel = 0;
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)
return false;
@ -156,8 +156,13 @@ bool Match(const TOKEN *tok, const char pattern[], const char *varname[])
}
// 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)
return false;

View File

@ -9,8 +9,6 @@
struct TOKEN;
extern std::list<const TOKEN *> FunctionList;
std::string FileLine(const TOKEN *tok);
extern bool OnlyReportUniqueErrors;
@ -28,7 +26,7 @@ void FillFunctionList();
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
//---------------------------------------------------------------------------
extern bool HasErrors;
static void CppCheck(const char FileName[])
{
HasErrors = false;
OnlyReportUniqueErrors = true;
std::cout << "Checking " << FileName << "...\n";
@ -178,7 +175,7 @@ static void CppCheck(const char FileName[])
Files.clear();
Tokenize(FileName);
FunctionList.clear();
FillFunctionList();
// Check that the memsets are valid.
// 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
const char *ext = strrchr(FileName, '.');
#ifdef __linux__
if (ext && strcasecmp(ext,".c"))
#else
if (ext && stricmp(ext,".c"))
#endif
if (ext && strcmp(ext,".cpp")==0)
WarningOldStylePointerCast();
// Use standard functions instead
@ -269,8 +262,7 @@ static void CppCheck(const char FileName[])
// Clean up tokens..
DeallocateTokens();
// Todo: How should this work? Activated by a command line switch?
if ( ! HasErrors )
if ( errout.str().empty() )
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 )
SimplifyTokenList();
FunctionList.clear();
FillFunctionList();
// Check for buffer overruns..