Fixed #700 (False positive: Memory leak)

This commit is contained in:
Daniel Marjamäki 2009-09-23 22:42:07 +02:00
parent 36c838c0e2
commit fa4e0a617e
2 changed files with 25 additions and 44 deletions

View File

@ -1084,22 +1084,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
tok2->str("return"); tok2->str("return");
} }
// If "--all" is given, remove all "callfunc"..
if (_settings->_showAll)
{
Token *tok2 = tok;
while (tok2)
{
if (tok2->str() == "callfunc")
{
tok2->deleteThis();
all = true;
}
else
tok2 = tok2->next();
}
}
// reduce the code.. // reduce the code..
bool done = false; bool done = false;
while (! done) while (! done)
@ -1138,10 +1122,17 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "if if" => "if" // reduce "; callfunc ; %var%"
else if (Token::Match(tok2, "; callfunc ; %type%"))
{
tok2->deleteNext();
done = false;
}
// Reduce "if if|callfunc" => "if"
else if (Token::Match(tok2, "if if|callfunc")) else if (Token::Match(tok2, "if if|callfunc"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
@ -1284,30 +1275,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "} else .." => "} if .."
if (Token::simpleMatch(tok2, "} else"))
{
int indentlevel = 0;
for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous())
{
if (tok3->str() == "{")
{
--indentlevel;
if (indentlevel == 0)
{
if (tok3->previous()->str() == "if")
tok2->next()->str("if");
}
if (indentlevel <= 0)
break;
}
else if (tok3->str() == "}")
{
++indentlevel;
}
}
}
// Remove "catch ;" // Remove "catch ;"
if (Token::simpleMatch(tok2->next(), "catch ;")) if (Token::simpleMatch(tok2->next(), "catch ;"))
{ {
@ -1616,9 +1583,21 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
} }
} }
// If "--all" is given, remove all "callfunc"..
if (done && _settings->_showAll)
{
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "callfunc")
{
tok2->deleteThis();
all = true;
done = false;
}
}
}
} }
} }

View File

@ -467,6 +467,8 @@ 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 ;"));
// "if ; .." // "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;")); ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;"));