From d3b5c30c6c85fb7159b86e46e4236485dc660b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 27 Apr 2010 20:43:31 +0200 Subject: [PATCH] Fixed #1628 (false negative: memory leak when using redundant braces) --- lib/checkmemoryleak.cpp | 28 ++++++++++++++++++++++++++++ test/testmemleak.cpp | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index ebf8f0e26..7cf179206 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1283,6 +1283,34 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) tok2->deleteNext(); } + // remove redundant braces.. + for (Token *start = tok; start; start = start->next()) + { + if (Token::simpleMatch(start, "; {")) + { + // the "link" doesn't work here. Find the end brace.. + unsigned int indent = 0; + for (Token *end = start; end; end = end->next()) + { + if (end->str() == "{") + ++indent; + else if (end->str() == "}") + { + if (indent <= 1) + { + if (indent == 1 && Token::Match(end->previous(), "[;{}] } %any%")) + { + start->deleteNext(); + end->deleteThis(); + } + break; + } + --indent; + } + } + } + } + // reduce the code.. bool done = false; while (! done) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 29cc8bc0b..029e818bd 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -694,6 +694,12 @@ private: ASSERT_EQUALS(";", simplifycode("; do { dealloc ; alloc ; } while(var) ;")); + // scope.. + // current result - ok + ASSERT_EQUALS("; assign ; dealloc ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }")); + // wanted result - better + TODO_ASSERT_EQUALS("; assign ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }")); + // callfunc.. ASSERT_EQUALS("; callfunc ;\n;", simplifycode(";callfunc;"));