memleak: better handling of switch blocks

This commit is contained in:
Daniel Marjamäki 2008-09-02 07:54:10 +00:00
parent deca59aa86
commit d942092ac0
1 changed files with 23 additions and 16 deletions

View File

@ -146,12 +146,12 @@ static void MemoryLeak( const TOKEN *tok, const char varname[] )
ReportErr( errmsg.str() ); ReportErr( errmsg.str() );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void instoken(TOKEN *tok, const char str[]) static void instoken(TOKEN *tok, const char str[])
{ {
TOKEN *newtok = new TOKEN; TOKEN *newtok = new TOKEN;
memcpy( newtok, tok, sizeof(TOKEN) ); memcpy( newtok, tok, sizeof(TOKEN) );
newtok->str = strdup(str); newtok->str = strdup(str);
tok->next = newtok; tok->next = newtok;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -288,6 +288,10 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
addtoken("continue"); addtoken("continue");
if ( Match(tok, "break") ) if ( Match(tok, "break") )
addtoken("break"); addtoken("break");
// goto..
if ( Match(tok, "goto") )
addtoken( "goto" );
// Return.. // Return..
if ( Match(tok, "return") ) if ( Match(tok, "return") )
@ -453,27 +457,27 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
erase(tok2, gettok(tok2,3)); erase(tok2, gettok(tok2,3));
done = false; done = false;
} }
// Replace switch with if (if not complicated) // Replace switch with if (if not complicated)
if (Match(tok2, "switch {")) if (Match(tok2, "switch {"))
{ {
// Right now, I just handle if there are a few case and perhaps a default. // Right now, I just handle if there are a few case and perhaps a default.
bool valid = false; bool valid = false;
bool incase = 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] == '{' ) if ( _tok->str[0] == '{' )
break; break;
else if ( _tok->str[0] == '}' ) else if ( _tok->str[0] == '}' )
{ {
valid = true; valid = true;
break; break;
} }
else if (strncmp(_tok->str,"if",2)==0) else if (strncmp(_tok->str,"if",2)==0)
break; break;
else if (strcmp(_tok->str,"switch")==0) else if (strcmp(_tok->str,"switch")==0)
break; break;
@ -487,7 +491,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
incase &= !Match(_tok,"break"); incase &= !Match(_tok,"break");
} }
if ( valid ) if ( !incase && valid )
{ {
done = false; done = false;
free(tok2->str); free(tok2->str);
@ -513,15 +517,18 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
instoken( tok2, "if" ); instoken( tok2, "if" );
instoken( tok2, "else" ); instoken( tok2, "else" );
} }
while ( tok2 && ! Match(tok2,"break ;") ) while ( tok2 && tok2->str[0] != '}' && ! Match(tok2,"break ;") )
tok2 = tok2->next; tok2 = tok2->next;
free(tok2->str); if (Match(tok2,"break ;"))
tok2->str = strdup(";"); {
tok2 = tok2->next->next; free(tok2->str);
tok2->str = strdup(";");
tok2 = tok2->next->next;
}
} }
} }
} }
} }
} }