From 44bee90d827f0102e23867c07b733a1d4dec9e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Mar 2008 14:15:44 +0000 Subject: [PATCH] Refactoring (Replace '->' with '.', use matching function when possible) --- CheckBufferOverrun.cpp | 12 +++----- CheckClass.cpp | 64 ++++++++++++++++-------------------------- tokenize.cpp | 9 ++++++ 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index 79c4f1787..f39dc0b14 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -192,10 +192,7 @@ static void CheckBufferOverrun_LocalVariable() else { // Array index.. - if (strcmp(tok2->str,varname)==0 && - strcmp(getstr(tok2,1),"[")==0 && - IsNumber(getstr(tok2,2)) && - strcmp(getstr(tok2,3),"]")==0 ) + if (strcmp(tok2->str,varname)==0 && match(tok2->next,"[ num ]")) { const char *str = getstr(tok2, 2); 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}; badpattern[0] = varname; - badpattern[1] = dot; badpattern[2] = arrname; const TOKEN *tok2 = findtoken( tok1, badpattern ); while (tok2) @@ -378,14 +374,14 @@ static void CheckBufferOverrun_StructVariable() if ( match( tok3->next, "var ;" ) ) { 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 else if ( match(tok3->next, "* var") && tok3->next->next->next && strchr(",);=", tok3->next->next->next->str[0]) ) { const char *varname = tok3->next->next->str; - CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, "->", arrname, atoi(arrsize) ); + CheckBufferOverrun_StructVariable_CheckVar( tok3, varname, arrname, atoi(arrsize) ); } } } diff --git a/CheckClass.cpp b/CheckClass.cpp index a5c2e8dbe..d94dc5938 100644 --- a/CheckClass.cpp +++ b/CheckClass.cpp @@ -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[]) { - bool BeginLine = false; bool Assign = false; unsigned int indentlevel = 0; for (; ftok; ftok = ftok->next) @@ -149,17 +161,9 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl // clKalle::clKalle() : var(value) { } if (indentlevel==0) { - if (Assign && - IsName(ftok->str) && - ftok->next->str[0]=='(') + if (Assign && match(ftok, "var (")) { - for (struct VAR *var = varlist; var; var = var->next) - { - if (strcmp(var->name,ftok->str)) - continue; - var->init = true; - break; - } + InitVar( varlist, ftok->str ); } Assign |= (ftok->str[0] == ':'); @@ -179,8 +183,10 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl 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.. if (match(ftok,"memset ( this ,")) { @@ -189,7 +195,7 @@ static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varl } // Calling member function? - if (ftok->next->str[0] == '(') + else if (match(ftok, "var (")) { unsigned int i = 0; 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? - if (strcmp(ftok->next->str, "=") == 0) + else if (match(ftok, "var =")) { - for (struct VAR *var = varlist; var; var = var->next) - { - if (strcmp(var->name,ftok->str)) - continue; - var->init = true; - break; - } + InitVar( varlist, ftok->str ); } - // Calling member function.. - if (strcmp(ftok->next->str,".")==0 || strcmp(ftok->next->str,"->")==0) + // The functions 'clear' and 'Clear' are supposed to initialize variable. + if (match(ftok,"var . clear (") || match(ftok,"var . Clear (")) { - // The functions 'clear' and 'Clear' are supposed to initialize variable. -#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; - } - } + InitVar( varlist, ftok->str ); } } - - BeginLine = (strchr("{};", ftok->str[0])); } } diff --git a/tokenize.cpp b/tokenize.cpp index 546e2eae0..55d2ca4de 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -538,6 +538,15 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex) combine_2tokens(tok, "protected", ":"); combine_2tokens(tok, "public", ":"); } + + // Replace "->" with "." + for ( TOKEN *tok = tokens; tok; tok = tok->next ) + { + if ( strcmp(tok->str, "->") == 0 ) + { + strcpy( tok->str, "." ); + } + } } //---------------------------------------------------------------------------