Refactoring (Replace '->' with '.', use matching function when possible)

This commit is contained in:
Daniel Marjamäki 2008-03-23 14:15:44 +00:00
parent 75debeba37
commit 44bee90d82
3 changed files with 37 additions and 48 deletions

View File

@ -192,10 +192,7 @@ static void CheckBufferOverrun_LocalVariable()
else else
{ {
// Array index.. // Array index..
if (strcmp(tok2->str,varname)==0 && if (strcmp(tok2->str,varname)==0 && match(tok2->next,"[ num ]"))
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
{ {
const char *str = getstr(tok2, 2); const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size) if (strtoul(str, NULL, 10) >= size)
@ -320,11 +317,10 @@ static void CheckBufferOverrun_LocalVariable()
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void CheckBufferOverrun_StructVariable_CheckVar( const TOKEN *tok1, const char varname[], const char dot[], const char arrname[], const int arrsize ) static void CheckBufferOverrun_StructVariable_CheckVar( const TOKEN *tok1, const char varname[], const char arrname[], const int arrsize )
{ {
const char *badpattern[] = {"varname",".","arrname","[","","]",NULL}; const char *badpattern[] = {"varname",".","arrname","[","","]",NULL};
badpattern[0] = varname; badpattern[0] = varname;
badpattern[1] = dot;
badpattern[2] = arrname; badpattern[2] = arrname;
const TOKEN *tok2 = findtoken( tok1, badpattern ); const TOKEN *tok2 = findtoken( tok1, badpattern );
while (tok2) while (tok2)
@ -378,14 +374,14 @@ static void CheckBufferOverrun_StructVariable()
if ( match( tok3->next, "var ;" ) ) if ( match( tok3->next, "var ;" ) )
{ {
const char *varname = tok3->next->str; const char *varname = tok3->next->str;
CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, ".", arrname, atoi(arrsize) ); CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, arrname, atoi(arrsize) );
} }
// Declare pointer: Fred *fred1 // Declare pointer: Fred *fred1
else if ( match(tok3->next, "* var") && tok3->next->next->next && strchr(",);=", tok3->next->next->next->str[0]) ) else if ( match(tok3->next, "* var") && tok3->next->next->next && strchr(",);=", tok3->next->next->next->str[0]) )
{ {
const char *varname = tok3->next->next->str; const char *varname = tok3->next->next->str;
CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, "->", arrname, atoi(arrsize) ); CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, arrname, atoi(arrsize) );
} }
} }
} }

View File

@ -135,9 +135,21 @@ static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classna
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void InitVar(struct VAR *varlist, const char varname[])
{
for (struct VAR *var = varlist; var; var = var->next)
{
if ( strcmp(var->name, varname) == 0 )
{
var->init = true;
break;
}
}
}
//---------------------------------------------------------------------------
static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varlist, const char classname[]) static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varlist, const char classname[])
{ {
bool BeginLine = false;
bool Assign = false; bool Assign = false;
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
for (; ftok; ftok = ftok->next) for (; ftok; ftok = ftok->next)
@ -149,17 +161,9 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl
// clKalle::clKalle() : var(value) { } // clKalle::clKalle() : var(value) { }
if (indentlevel==0) if (indentlevel==0)
{ {
if (Assign && if (Assign && match(ftok, "var ("))
IsName(ftok->str) &&
ftok->next->str[0]=='(')
{ {
for (struct VAR *var = varlist; var; var = var->next) InitVar( varlist, ftok->str );
{
if (strcmp(var->name,ftok->str))
continue;
var->init = true;
break;
}
} }
Assign |= (ftok->str[0] == ':'); Assign |= (ftok->str[0] == ':');
@ -179,8 +183,10 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl
indentlevel--; indentlevel--;
} }
if (BeginLine && indentlevel>=1 && IsName(ftok->str)) if (strchr("{};", ftok->str[0]) && indentlevel>=1 && IsName(ftok->next->str))
{ {
ftok = ftok->next;
// Clearing all variables.. // Clearing all variables..
if (match(ftok,"memset ( this ,")) if (match(ftok,"memset ( this ,"))
{ {
@ -189,7 +195,7 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl
} }
// Calling member function? // Calling member function?
if (ftok->next->str[0] == '(') else if (match(ftok, "var ("))
{ {
unsigned int i = 0; unsigned int i = 0;
const TOKEN *ftok2 = FindClassFunction( tokens, classname, ftok->str, i ); const TOKEN *ftok2 = FindClassFunction( tokens, classname, ftok->str, i );
@ -197,39 +203,17 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl
} }
// Assignment of member variable? // Assignment of member variable?
if (strcmp(ftok->next->str, "=") == 0) else if (match(ftok, "var ="))
{ {
for (struct VAR *var = varlist; var; var = var->next) InitVar( varlist, ftok->str );
{
if (strcmp(var->name,ftok->str))
continue;
var->init = true;
break;
}
} }
// Calling member function.. // The functions 'clear' and 'Clear' are supposed to initialize variable.
if (strcmp(ftok->next->str,".")==0 || strcmp(ftok->next->str,"->")==0) if (match(ftok,"var . clear (") || match(ftok,"var . Clear ("))
{ {
// The functions 'clear' and 'Clear' are supposed to initialize variable. InitVar( varlist, ftok->str );
#ifdef __linux__
if( strcasecmp( ftok->next->next->str,"clear") == 0)
#else
if (stricmp(ftok->next->next->str,"clear") == 0)
#endif
{
for (struct VAR *var = varlist; var; var = var->next)
{
if (strcmp(var->name,ftok->str))
continue;
var->init = true;
break;
}
}
} }
} }
BeginLine = (strchr("{};", ftok->str[0]));
} }
} }

View File

@ -538,6 +538,15 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex)
combine_2tokens(tok, "protected", ":"); combine_2tokens(tok, "protected", ":");
combine_2tokens(tok, "public", ":"); combine_2tokens(tok, "public", ":");
} }
// Replace "->" with "."
for ( TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( strcmp(tok->str, "->") == 0 )
{
strcpy( tok->str, "." );
}
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------