Fixed #1628 (false negative: memory leak when using redundant braces)

This commit is contained in:
Daniel Marjamäki 2010-04-27 20:43:31 +02:00
parent f0018100e1
commit d3b5c30c6c
2 changed files with 34 additions and 0 deletions

View File

@ -1283,6 +1283,34 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
tok2->deleteNext(); tok2->deleteNext();
} }
// remove redundant braces..
for (Token *start = tok; start; start = start->next())
{
if (Token::simpleMatch(start, "; {"))
{
// the "link" doesn't work here. Find the end brace..
unsigned int indent = 0;
for (Token *end = start; end; end = end->next())
{
if (end->str() == "{")
++indent;
else if (end->str() == "}")
{
if (indent <= 1)
{
if (indent == 1 && Token::Match(end->previous(), "[;{}] } %any%"))
{
start->deleteNext();
end->deleteThis();
}
break;
}
--indent;
}
}
}
}
// reduce the code.. // reduce the code..
bool done = false; bool done = false;
while (! done) while (! done)

View File

@ -694,6 +694,12 @@ private:
ASSERT_EQUALS(";", simplifycode("; do { dealloc ; alloc ; } while(var) ;")); ASSERT_EQUALS(";", simplifycode("; do { dealloc ; alloc ; } while(var) ;"));
// scope..
// current result - ok
ASSERT_EQUALS("; assign ; dealloc ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));
// wanted result - better
TODO_ASSERT_EQUALS("; assign ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));
// callfunc.. // callfunc..
ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;")); ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));