Fixed #1264 (False positive: Resource leak with if-else in while)

This commit is contained in:
Daniel Marjamäki 2010-01-14 21:41:50 +01:00
parent a7bddc8fd3
commit ac077d3160
2 changed files with 52 additions and 2 deletions

View File

@ -1222,6 +1222,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
tok2->str("return"); tok2->str("return");
} }
// reduce "if callfunc {" => "if {"
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, "if|ifv callfunc {"))
tok2->deleteNext();
}
// reduce the code.. // reduce the code..
bool done = false; bool done = false;
while (! done) while (! done)
@ -1229,6 +1236,39 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
//tok->printOut("simplifycode loop.."); //tok->printOut("simplifycode loop..");
done = true; done = true;
// simplify "while1" contents..
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (Token::simpleMatch(tok2, "while1 {"))
{
unsigned int indentlevel = 0;
for (Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next())
{
if (tok3->str() == "{")
++indentlevel;
else if (tok3->str() == "}")
{
if (indentlevel == 0)
break;
--indentlevel;
}
while (indentlevel == 0 && Token::Match(tok3, "[{};] if|ifv|else { continue ; }"))
{
Token::eraseTokens(tok3, tok3->tokAt(6));
if (Token::simpleMatch(tok3->next(), "else"))
tok3->deleteNext();
}
}
if (Token::Match(tok2, "while1 { if { dealloc ; return ; } }"))
{
tok2->str(";");
Token::eraseTokens(tok2, tok2->tokAt(4));
Token::eraseTokens(tok2->tokAt(4), tok2->tokAt(7));
}
}
}
for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL) for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL)
{ {
// Delete extra ";" // Delete extra ";"
@ -1474,6 +1514,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "while1 if break ;" => ";"
if (Token::simpleMatch(tok2, "while1 if break ;"))
{
tok2->str(";");
Token::eraseTokens(tok2, tok2->tokAt(3));
done = false;
}
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (Token::Match(tok2, "alloc ; if return use ; !!else")) if (Token::Match(tok2, "alloc ; if return use ; !!else"))
{ {

View File

@ -580,7 +580,7 @@ private:
ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }")); ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }"));
ASSERT_EQUALS("; if alloc ; else assign ; return use ;", simplifycode("; callfunc ; if callfunc { alloc ; } else { assign ; } return use ;")); ASSERT_EQUALS("; if alloc ; else assign ; return use ;", simplifycode("; callfunc ; if callfunc { alloc ; } else { assign ; } return use ;"));
ASSERT_EQUALS("; while1 continue ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }")); ASSERT_EQUALS("; dealloc ; return ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }"));
// "if ; .." // "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
@ -607,7 +607,9 @@ private:
ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;")); ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;"));
ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;")); ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;"));
ASSERT_EQUALS("; alloc ; use ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } }")); ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } }"));
ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { continue ; } }"));
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { break ; } }"));
// callfunc.. // callfunc..
ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;")); ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));