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() );
}
//---------------------------------------------------------------------------
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;
}
}
}
}
}
}
}