Memory leaks: Better handling of switch

This commit is contained in:
Daniel Marjamäki 2010-01-10 10:37:54 +01:00
parent b82cb2e41e
commit 3fabe53570
2 changed files with 11 additions and 3 deletions

View File

@ -1698,8 +1698,11 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
else if (incase && _tok->str() == "case") else if (incase && _tok->str() == "case")
break; break;
else if (Token::Match(_tok, "return !!;"))
break;
incase |= (_tok->str() == "case"); incase |= (_tok->str() == "case");
incase &= (_tok->str() != "break"); incase &= (_tok->str() != "break" && _tok->str() != "return");
} }
if (!incase && valid) if (!incase && valid)
@ -1711,7 +1714,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
bool first = true; bool first = true;
while (Token::Match(tok2, "case|default")) while (Token::Match(tok2, "case|default"))
{ {
bool def(tok2->str() == "default"); const bool def(tok2->str() == "default");
tok2->str(first ? "if" : "}"); tok2->str(first ? "if" : "}");
if (first) if (first)
{ {
@ -1727,13 +1730,17 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
tok2->insertToken("else"); tok2->insertToken("else");
tok2 = tok2->next(); tok2 = tok2->next();
} }
while (tok2 && tok2->str() != "}" && ! Token::simpleMatch(tok2, "break ;")) while (tok2 && tok2->str() != "}" && ! Token::Match(tok2, "break|return ;"))
tok2 = tok2->next(); tok2 = tok2->next();
if (Token::simpleMatch(tok2, "break ;")) if (Token::simpleMatch(tok2, "break ;"))
{ {
tok2->str(";"); tok2->str(";");
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
} }
else if (tok2 && tok2->str() == "return")
{
tok2 = tok2->tokAt(2);
}
} }
} }
} }

View File

@ -592,6 +592,7 @@ private:
// switch.. // switch..
ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;")); ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;"));
ASSERT_EQUALS("; if return ; else use ; }", simplifycode("; switch { case ; return ; default ; use ; break ; } }"));
// loops.. // loops..
ASSERT_EQUALS(";", simplifycode("; loop { break; }")); ASSERT_EQUALS(";", simplifycode("; loop { break; }"));