From 34a2fdfb376d0a574fe43941eb760289e813e8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Mar 2008 13:27:34 +0000 Subject: [PATCH] Refactoring --- CheckBufferOverrun.cpp | 54 ++++++++++++++++++++---------------------- CheckOther.cpp | 11 ++------- CommonCheck.cpp | 29 +++++++++++++++++++++++ CommonCheck.h | 2 ++ 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index 4b2140e5e..9d479dff0 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -26,7 +26,7 @@ static const TOKEN *findfunction(const TOKEN *tok) if (!tok->next) break; - if (indentlevel==0 && parlevel==0 && IsName(tok->str) && tok->next->str[0]=='(') + if (indentlevel==0 && parlevel==0 && match(tok,"var (")) { for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) { @@ -174,10 +174,7 @@ static void CheckBufferOverrun_LocalVariable() break; // 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) @@ -217,10 +214,11 @@ static void CheckBufferOverrun_LocalVariable() // Loop.. - const char *strindex = 0; - int value = 0; if ( match(tok2, "for ( var = 0 ;") ) { + const char *strindex = 0; + int value = 0; + if (match(tok2,"for ( var = 0 ; var < num ; var + + )")) { strindex = getstr(tok2,2); @@ -241,30 +239,30 @@ static void CheckBufferOverrun_LocalVariable() strindex = getstr(tok2,2); value = 1 + atoi(getstr(tok2,8)); } - } - if (strindex && value>(int)size) - { - const TOKEN *tok3 = tok2; - while (tok3 && strcmp(tok3->str,")")) - tok3 = tok3->next; - if (!tok3) - break; - tok3 = tok3->next; - if (tok3->str[0] == '{') - tok3 = tok3->next; - while (tok3 && !strchr(";}",tok3->str[0])) + + if (strindex && value>(int)size) { - if (strcmp(tok3->str,varname)==0 && - strcmp(getstr(tok3,1),"[")==0 && - strcmp(getstr(tok3,2),strindex)==0 && - strcmp(getstr(tok3,3),"]")==0 ) - { - std::ostringstream ostr; - ostr << FileLine(tok3) << ": Buffer overrun"; - ReportErr(ostr.str()); + const TOKEN *tok3 = tok2; + while (tok3 && strcmp(tok3->str,")")) + tok3 = tok3->next; + if (!tok3) break; - } tok3 = tok3->next; + if (tok3->str[0] == '{') + tok3 = tok3->next; + while (tok3 && !strchr(";}",tok3->str[0])) + { + if ( match(tok3, "var [ var ]" && + strcmp(tok3->str,varname)==0 && + strcmp(getstr(tok3,2),strindex)==0 ) + { + std::ostringstream ostr; + ostr << FileLine(tok3) << ": Buffer overrun"; + ReportErr(ostr.str()); + break; + } + tok3 = tok3->next; + } } } diff --git a/CheckOther.cpp b/CheckOther.cpp index 45ad8bb74..720737312 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -16,7 +16,7 @@ void WarningOldStylePointerCast() { - for (const TOKEN *tok = tokens; tok; tok = tok->next) + for (const TOKEN *tok = FindMatchingToken(tokens); tok; tok = tok->next) { // Old style pointer casting.. if (!match(tok, "( type * ) var")) @@ -194,15 +194,8 @@ void WarningIf() } // Search for 'a=b; if (a==b)' - for (const TOKEN *tok = tokens; tok; tok = tok->next) + for (const TOKEN *tok = tokens; tok; GotoNextStatement(&tok)) { - // Begin statement? - if ( ! strchr(";{}", tok->str[0]) ) - continue; - tok = tok->next; - if ( ! tok ) - break; - if (!match(tok,"var = var ; if ( var")) continue; diff --git a/CommonCheck.cpp b/CommonCheck.cpp index 7db30cd9d..e321822f9 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -69,3 +69,32 @@ bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel ) return bool(tok->str[0]=='}' && indentlevel<=endlevel); } //--------------------------------------------------------------------------- + +void GotoNextStatement( const TOKEN **tok ) +{ + // Goto end of statement.. + while ( *tok && ! strchr("{};", (*tok)->str[0]) ) + *tok = (*tok)->next; + + // Goto next statement + if ( *tok ) + *tok = (*tok)->next; +} +//--------------------------------------------------------------------------- + +void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel ) +{ + while ( *tok ) + { + if ( setindentlevel( *tok, indentlevel, -1 ) ) + *tok = NULL; + + else if ( match( *tok, pattern ) ) + return; + + else + *tok = (*tok)->next; + } +} +//--------------------------------------------------------------------------- + diff --git a/CommonCheck.h b/CommonCheck.h index 5c2dbc1bc..2cbebf577 100644 --- a/CommonCheck.h +++ b/CommonCheck.h @@ -35,6 +35,8 @@ bool IsStandardType(const char str[]); //--------------------------------------------------------------------------- bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel ); +void GotoNextStatement( const TOKEN **tok ); +void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel ); //---------------------------------------------------------------------------