CheckMemoryLeak: Refactoring further

This commit is contained in:
Daniel Marjamäki 2008-08-16 17:49:40 +00:00
parent b29c916d9f
commit 68f409aca5
2 changed files with 86 additions and 20 deletions

View File

@ -554,18 +554,51 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
dealloctype = dealloc; dealloctype = dealloc;
} }
static const int SIZE = 6; // if else switch
const char *str[SIZE] = {"if","else","for","while","switch","return"}; if ( Match(tok, "if ( %var1% )", varnames) )
for (int i = 0; i < SIZE; ++i)
{ {
if (strcmp(tok->str, str[i])==0) addtoken("if(var)");
addtoken(tok->str); }
else if ( Match(tok, "if ( ! %var1% )", varnames) ||
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
Match(tok, "if ( %var1% == NULL )", varnames) ||
Match(tok, "if ( NULL == %var1% )", varnames) ||
Match(tok, "if ( %var1% == 0 )", varnames) )
{
addtoken("if(!var)");
}
else
{
if (Match(tok, "if"))
addtoken("if");
if (Match(tok, "else"))
addtoken("else");
if (Match(tok, "switch"))
addtoken("switch");
} }
// Loops..
if ( Match(tok, "for") )
addtoken("loop");
if ( Match(tok, "while") )
addtoken("loop");
if ( Match(tok, "do") )
addtoken("loop");
// continue / break..
if ( Match(tok, "continue") )
addtoken("continue");
if ( Match(tok, "break") )
addtoken("break");
// Return.. // Return..
if ( Match(tok, "return %var1%", varnames) || if ( Match(tok, "return") )
Match(tok, "return & %var1%", varnames) ) {
addtoken("use"); addtoken("return");
if ( Match(tok, "return %var1%", varnames) ||
Match(tok, "return & %var1%", varnames) )
addtoken("use");
}
// Assignment.. // Assignment..
if ( Match(tok,"[)=] %var1%", varnames) ) if ( Match(tok,"[)=] %var1%", varnames) )
@ -583,12 +616,15 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
return rethead; return rethead;
} }
static void eraseNext(TOKEN *tok) static void erase(TOKEN *begin, const TOKEN *end)
{ {
if ( tok && tok->next ) if ( ! begin )
return;
while ( begin->next && begin->next != end )
{ {
TOKEN *next = tok->next; TOKEN *next = begin->next;
tok->next = tok->next->next; begin->next = begin->next->next;
delete next; delete next;
} }
} }
@ -621,19 +657,49 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
for (TOKEN *tok2 = tok ; tok2; tok2 = tok2->next ) for (TOKEN *tok2 = tok ; tok2; tok2 = tok2->next )
{ {
// Delete extra ";"
while (Match(tok2,"[;{}] ;")) while (Match(tok2,"[;{}] ;"))
{ {
eraseNext(tok2); erase(tok2, gettok(tok2,2));
done = false; done = false;
} }
// Delete else { } // Delete "else { }", "else if { }", "else ;" and "else if ;"
if ( Match(tok2->next, "else { }") ) if ( Match(tok2->next, "else") )
{ {
eraseNext(tok2); const TOKEN *_tok2 = gettok(tok2,2);
eraseNext(tok2);
eraseNext(tok2); // Delete optional "if"
done = false; if ( Match( _tok2, "if" ) )
_tok2 = _tok2->next;
// Delete "{ }" or ";"
if (Match(_tok2, "{ }"))
{
erase(tok2, _tok2->next->next);
done = false;
}
else if ( Match(_tok2, ";") )
{
erase(tok2, _tok2->next);
done = false;
}
}
// Delete "loop ;" and "loop { }"
if ( Match(tok2->next, "loop") )
{
if ( Match(gettok(tok2,2), ";") )
{
erase(tok2, gettok(tok2,3));
done = false;
}
else if ( Match(gettok(tok2,2), "{ }") )
{
erase(tok2, gettok(tok2,4));
done = false;
}
} }
} }
} }

View File

@ -747,7 +747,7 @@ static void memleak_in_function()
" struct abc *abc1 = new abc;\n" " struct abc *abc1 = new abc;\n"
" p = &abc1->a;\n" // p may be part of a garbage collector " p = &abc1->a;\n" // p may be part of a garbage collector
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, code, "" ); check_( CheckMemoryLeak, __LINE__, code, "" );