CheckMemoryLeak: Removed false positives about not deleting class instances

This commit is contained in:
Daniel Marjamäki 2008-09-29 06:38:25 +00:00
parent 13f5196e52
commit c9fc2594e8
1 changed files with 25 additions and 19 deletions

View File

@ -150,12 +150,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;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -163,6 +163,12 @@ static void instoken(TOKEN *tok, const char str[])
extern bool ShowAll; extern bool ShowAll;
/**
* Extract a new tokens list that is easier to parse than the "tokens"
* tok - start parse token
* varname - name of variable
*/
static TOKEN *getcode(const TOKEN *tok, const char varname[]) static TOKEN *getcode(const TOKEN *tok, const char varname[])
{ {
const char *varnames[2]; const char *varnames[2];
@ -173,7 +179,7 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
#define addtoken(_str) \ #define addtoken(_str) \
{ \ { \
TOKEN *newtok = new TOKEN; \ TOKEN *newtok = new TOKEN; \
newtok->str = _strdup(_str); \ newtok->str = _strdup(_str); \
newtok->linenr = tok->linenr; \ newtok->linenr = tok->linenr; \
newtok->FileIndex = tok->FileIndex; \ newtok->FileIndex = tok->FileIndex; \
newtok->next = 0; \ newtok->next = 0; \
@ -212,14 +218,14 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
if ( parlevel == 0 && tok->str[0]==';') if ( parlevel == 0 && tok->str[0]==';')
addtoken(";"); addtoken(";");
if (Match(tok, "[(;{}] %var1% = ", varnames)) if (Match(tok, "[(;{}] %var1% =", varnames))
{ {
AllocType alloc = GetAllocationType(gettok(tok,3)); AllocType alloc = GetAllocationType(gettok(tok,3));
// If "--all" hasn't been given, don't check classes.. // If "--all" hasn't been given, don't check classes..
if ( alloc == New && ! ShowAll ) if ( alloc == New && ! ShowAll )
{ {
if ( Match(gettok(tok,3), "new %var% ;") ) if ( Match(gettok(tok,3), "new %type% [(;]") )
{ {
if ( isclass( getstr(tok, 4) ) ) if ( isclass( getstr(tok, 4) ) )
alloc = No; alloc = No;
@ -292,10 +298,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.. // goto..
if ( Match(tok, "goto") ) if ( Match(tok, "goto") )
addtoken( "goto" ); addtoken( "goto" );
// Return.. // Return..
if ( Match(tok, "return") ) if ( Match(tok, "return") )
@ -461,7 +467,7 @@ 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 {"))
{ {
@ -473,15 +479,15 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
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;
@ -522,17 +528,17 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
instoken( tok2, "else" ); instoken( tok2, "else" );
} }
while ( tok2 && tok2->str[0] != '}' && ! Match(tok2,"break ;") ) while ( tok2 && tok2->str[0] != '}' && ! Match(tok2,"break ;") )
tok2 = tok2->next; tok2 = tok2->next;
if (Match(tok2,"break ;")) if (Match(tok2,"break ;"))
{ {
free(tok2->str); free(tok2->str);
tok2->str = _strdup(";"); tok2->str = _strdup(";");
tok2 = tok2->next->next; tok2 = tok2->next->next;
} }
} }
} }
} }
} }
} }