diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index d6c1ef4d2..af7f642e1 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1222,6 +1222,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) 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.. bool done = false; while (! done) @@ -1229,6 +1236,39 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) //tok->printOut("simplifycode loop.."); 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) { // Delete extra ";" @@ -1474,6 +1514,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) 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 ;" if (Token::Match(tok2, "alloc ; if return use ; !!else")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 9f310df92..d199d0812 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -580,7 +580,7 @@ private: 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("; while1 continue ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }")); + ASSERT_EQUALS("; dealloc ; return ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }")); // "if ; .." ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); @@ -607,7 +607,9 @@ private: ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;")); 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.. ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));