From a50ec2eded2ba95e6c82d0242adcf5387203cd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 10 Jan 2010 15:40:50 +0100 Subject: [PATCH] memory leaks: better handling of loops and switch --- lib/checkmemoryleak.cpp | 25 +++++++++++++++++-------- test/testmemleak.cpp | 2 ++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f3314dc41..7cb4d0a45 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1528,10 +1528,10 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } - // Reduce "{ loop ;" => ";" - if (Token::Match(tok2, "{ loop ;")) + // Reduce "loop|do ;" => ";" + if (Token::Match(tok2, "loop|do ;")) { - tok2->deleteNext(); + tok2->deleteThis(); done = false; } @@ -1693,9 +1693,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) break; } - else if (strncmp(_tok->str().c_str(), "if", 2) == 0) - break; - else if (_tok->str() == "switch") break; @@ -1708,6 +1705,9 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) else if (Token::Match(_tok, "return !!;")) break; + if (Token::Match(_tok, "if return|break use| ;")) + _tok = _tok->tokAt(2); + incase |= (_tok->str() == "case"); incase &= (_tok->str() != "break" && _tok->str() != "return"); } @@ -1737,8 +1737,17 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) tok2->insertToken("else"); tok2 = tok2->next(); } - while (tok2 && tok2->str() != "}" && ! Token::Match(tok2, "break|return ;")) - tok2 = tok2->next(); + while (tok2) + { + if (tok2->str() == "}") + break; + if (Token::Match(tok2, "break|return ;")) + break; + if (Token::Match(tok2, "if return|break use| ;")) + tok2 = tok2->tokAt(2); + else + tok2 = tok2->next(); + } if (Token::simpleMatch(tok2, "break ;")) { tok2->str(";"); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 8a0798e4e..b1b7ebf8a 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -593,10 +593,12 @@ private: // switch.. ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;")); ASSERT_EQUALS("; if return ; }", simplifycode("; switch { case ; return ; default ; break ; } }")); + ASSERT_EQUALS("; if return ; }", simplifycode("; switch { case ; if { return ; } break ; default ; break ; } }")); ASSERT_EQUALS("; if return ; else use ; }", simplifycode("; switch { case ; return ; default ; use ; break ; } }")); ASSERT_EQUALS("; use ; }", simplifycode("; while1 { loop { ; } switch { case ; dealloc ; return ; default ; break ; } } }")); // loops.. + ASSERT_EQUALS(";", simplifycode("; loop { ; }")); ASSERT_EQUALS(";", simplifycode("; loop { break; }")); ASSERT_EQUALS(";", simplifycode("; loop { if { break; } }")); ASSERT_EQUALS("; loop alloc ;", simplifycode("; loop { alloc ; }"));