From d942092ac01c53df205b36ed8a96deb2988bb88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 2 Sep 2008 07:54:10 +0000 Subject: [PATCH] memleak: better handling of switch blocks --- CheckMemoryLeak.cpp | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 9bd822a92..d0c443a9f 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -146,12 +146,12 @@ static void MemoryLeak( const TOKEN *tok, const char varname[] ) ReportErr( errmsg.str() ); } //--------------------------------------------------------------------------- - + static void instoken(TOKEN *tok, const char str[]) { TOKEN *newtok = new TOKEN; memcpy( newtok, tok, sizeof(TOKEN) ); - newtok->str = strdup(str); + newtok->str = strdup(str); tok->next = newtok; } //--------------------------------------------------------------------------- @@ -288,6 +288,10 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[]) addtoken("continue"); if ( Match(tok, "break") ) addtoken("break"); + + // goto.. + if ( Match(tok, "goto") ) + addtoken( "goto" ); // Return.. if ( Match(tok, "return") ) @@ -453,27 +457,27 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] erase(tok2, gettok(tok2,3)); done = false; } - + // Replace switch with if (if not complicated) if (Match(tok2, "switch {")) { // Right now, I just handle if there are a few case and perhaps a default. bool valid = false; bool incase = false; - for ( const TOKEN * _tok = gettok(tok2,2); valid && _tok; _tok = _tok->next ) + for ( const TOKEN * _tok = gettok(tok2,2); _tok; _tok = _tok->next ) { if ( _tok->str[0] == '{' ) break; - else if ( _tok->str[0] == '}' ) + else if ( _tok->str[0] == '}' ) { - valid = true; - break; + valid = true; + break; } else if (strncmp(_tok->str,"if",2)==0) break; - + else if (strcmp(_tok->str,"switch")==0) break; @@ -487,7 +491,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] incase &= !Match(_tok,"break"); } - if ( valid ) + if ( !incase && valid ) { done = false; free(tok2->str); @@ -513,15 +517,18 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] instoken( tok2, "if" ); instoken( tok2, "else" ); } - while ( tok2 && ! Match(tok2,"break ;") ) - tok2 = tok2->next; - free(tok2->str); - tok2->str = strdup(";"); - tok2 = tok2->next->next; + while ( tok2 && tok2->str[0] != '}' && ! Match(tok2,"break ;") ) + tok2 = tok2->next; + if (Match(tok2,"break ;")) + { + free(tok2->str); + tok2->str = strdup(";"); + tok2 = tok2->next->next; + } } } - } - + } + } }