Memory leak: Further improvements of "simplifycode"

This commit is contained in:
Daniel Marjamäki 2008-12-02 17:22:01 +00:00
parent 8578df698f
commit d5506618b6
1 changed files with 33 additions and 16 deletions

View File

@ -672,17 +672,17 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
if ( TOKEN::Match(tok2->next, "do { alloc ; }") ) if ( TOKEN::Match(tok2->next, "do { alloc ; }") )
{ {
erase(tok2, tok2->tokAt(3)); erase(tok2, tok2->tokAt(3));
erase(tok2->next, tok2->tokAt(3)); erase(tok2->next->next, tok2->tokAt(4));
done = false; done = false;
} }
// Reduce "loop { if break ; } => ";" // Reduce "loop if break ; => ";"
if ( TOKEN::Match( tok2, "loop {" ) && if ( TOKEN::Match( tok2->next, "loop %var%" ) &&
strncmp(tok2->strAt(2), "if", 2) == 0 && tok2->tokAt(2)->str().find("if") == 0 &&
(TOKEN::Match( tok2->tokAt(3), "break ; }") || TOKEN::Match( tok2->tokAt(3), "continue ; }"))) (TOKEN::Match( tok2->tokAt(3), "break ; ") || TOKEN::Match( tok2->tokAt(3), "continue ;")) &&
!TOKEN::Match(tok2->tokAt(5),"else") )
{ {
tok2->setstr(";"); erase( tok2, tok2->tokAt(4) );
erase( tok2, tok2->tokAt(6) );
done = false; done = false;
} }
@ -728,6 +728,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase(tok2, tok2->tokAt(4)); erase(tok2, tok2->tokAt(4));
done = false; done = false;
} }
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else")) if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{ {
@ -742,6 +743,20 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Reduce "[;{}] return ; %var%" => "[;{}] return ;"
if ( TOKEN::Match(tok2, "[;{}] return ; %var%") )
{
erase( tok2->next->next, tok2->tokAt(4) );
done = false;
}
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") )
{
erase( tok2->next->next->next, tok2->tokAt(5) );
done = false;
}
// Reduce "if(var) return use ;" => "return use ;" // Reduce "if(var) return use ;" => "return use ;"
if ( TOKEN::Match(tok2->next, "if(var) return use ;") && !TOKEN::Match(tok2->tokAt(5),"else")) if ( TOKEN::Match(tok2->next, "if(var) return use ;") && !TOKEN::Match(tok2->tokAt(5),"else"))
{ {
@ -940,6 +955,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
MemoryLeak(last, varname); MemoryLeak(last, varname);
} }
// detect cases that "simplifycode" don't handle well..
else if ( _settings._debug ) else if ( _settings._debug )
{ {
TOKEN *first = tok; TOKEN *first = tok;
@ -947,13 +963,15 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
first = first->next; first = first->next;
bool noerr = false; bool noerr = false;
noerr |= TOKEN::Match( first, "alloc ; dealloc ;" ); noerr |= TOKEN::Match( first, "alloc ; }" );
noerr |= TOKEN::Match( first, "alloc ; return use ;" ); noerr |= TOKEN::Match( first, "alloc ; dealloc ; }" );
noerr |= TOKEN::Match( first, "alloc ; use ;" ); noerr |= TOKEN::Match( first, "alloc ; return use ; }" );
noerr |= TOKEN::Match( first, "if alloc ; dealloc ;" ); noerr |= TOKEN::Match( first, "alloc ; use ; }" );
noerr |= TOKEN::Match( first, "if alloc ; return use ;" ); noerr |= TOKEN::Match( first, "if alloc ; dealloc ; }" );
noerr |= TOKEN::Match( first, "if alloc ; use ;" ); noerr |= TOKEN::Match( first, "if alloc ; return use ; }" );
noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ;" ); noerr |= TOKEN::Match( first, "if alloc ; use ; }" );
noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ; }" );
noerr |= TOKEN::Match( first, "alloc ; if return ; dealloc; }" );
// Unhandled case.. // Unhandled case..
if ( ! noerr ) if ( ! noerr )
@ -965,7 +983,6 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
} }
} }
Tokenizer::deleteTokens(tok); Tokenizer::deleteTokens(tok);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------