From 03a92582ca6d06bb2e386f6b16a47f2f4f287c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 27 Nov 2010 11:09:42 +0100 Subject: [PATCH] Fixed #2235 (false Resource leak) --- lib/checkmemoryleak.cpp | 16 ++++++++++++++++ test/testmemleak.cpp | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index dbcb5cad1..631a52dcc 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1716,6 +1716,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) done = false; } + // Reduce "if continue ; if continue ;" => "if continue ;" + else if (Token::Match(tok2->next(), "if continue ; if continue ;")) + { + Token::eraseTokens(tok2, tok2->tokAt(4)); + done = false; + } + // Reduce "if return ; alloc ;" => "alloc ;" else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;")) { @@ -1759,6 +1766,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) done = false; } + // Remove "else" after "if continue|break|return" + else if (Token::Match(tok2->next(), "if continue|break|return ; else")) + { + tok2->tokAt(4)->deleteThis(); + done = false; + } + // Delete "if { dealloc|assign|use ; return ; }" else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }")) { @@ -2273,6 +2287,8 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string tok2->str("use"); else if (tok2->str() == "use_") tok2->str(";"); + else if (Token::simpleMatch(tok2, "loop use_ {")) + tok2->deleteNext(); else if (tok2->str() == "::use") // Some kind of member function usage. Not analyzed very well. tok2->str("use"); else if (tok2->str() == "recursive") diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index be43055c9..8dc80ed52 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -686,9 +686,9 @@ private: ASSERT_EQUALS("; alloc ;", simplifycode("; if { alloc; } else { return; }")); ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode("; alloc ; if(!var) { alloc ; } dealloc ;")); ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;")); - ASSERT_EQUALS("; break ;", simplifycode("; if break ; else break ;")); + ASSERT_EQUALS(";", simplifycode("; if break ; else break ;")); ASSERT_EQUALS("; alloc ; if return ;", simplifycode("; alloc ; loop { if return ; if continue ; }")); - ASSERT_EQUALS("; alloc ; if return ;", simplifycode("; alloc ; loop { if continue ; else return ; }")); + ASSERT_EQUALS("; alloc ; loop return ;", simplifycode("; alloc ; loop { if continue ; else return ; }")); ASSERT_EQUALS("; alloc ; if dealloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }")); ASSERT_EQUALS("; if alloc ; else assign ; return use ;", simplifycode("; callfunc ; if callfunc { alloc ; } else { assign ; } return use ;")); @@ -740,6 +740,9 @@ private: ASSERT_EQUALS("dealloc ; alloc ;", simplifycode("while1 { dealloc ; alloc ; }")); ASSERT_EQUALS("use ; }", simplifycode("loop { use ; callfunc ; } }")); + ASSERT_EQUALS(";", simplifycode("; loop { if { continue ; } else { if continue ; } }")); + ASSERT_EQUALS(";", simplifycode("; loop { { if continue ; if continue ; } }")); + // scope.. // current result - ok ASSERT_EQUALS("; assign ; dealloc ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));