From 997f4e6165c9096d79711c1c407492d20747716b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 26 Nov 2008 18:13:36 +0000 Subject: [PATCH] Memory leaks: Made the checking a lot more sensitive --- CheckMemoryLeak.cpp | 48 ++++++++++++++++++++++++++++++++++++++------- testmemleak.cpp | 12 +++++++----- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index d76f55b79..4dfb97af8 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -22,7 +22,8 @@ #include // free #include - + +#include #include #ifdef __BORLANDC__ @@ -219,14 +220,31 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::liststr() != "{") ) ftok = ftok->next; TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype ); - simplifycode( func ); + simplifycode( func ); + const TOKEN *func_ = func; + while ( func_ && func_->str() == ";" ) + func_ = func_->next; + + /* + for (const TOKEN *t = func_; t; t = t->next) + { + std::cout << t->str() << "\n"; + } + */ + const char *ret = 0; - if (TOKEN::findmatch(func, "goto")) - ret = "dealloc"; // TODO : "goto" isn't handled well - else if (TOKEN::findmatch(func, "use")) - ret = "use"; - else if (TOKEN::findmatch(func, "dealloc")) + if (TOKEN::findmatch(func_, "goto")) + { + // TODO : "goto" isn't handled well + if ( TOKEN::findmatch(func_, "dealloc") ) + ret = "dealloc"; + else if ( TOKEN::findmatch(func_, "use") ) + ret = "use"; + } + else if (TOKEN::Match(func_, "dealloc")) ret = "dealloc"; + else if (TOKEN::Match(func_, "use")) + ret = "use"; Tokenizer::deleteTokens(func); return ret; } @@ -613,6 +631,22 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) continue; } } + + // Delete "if ; else ;" + if ( TOKEN::Match(tok2->next, "if ; else ;") ) + { + erase( tok2, tok2->tokAt(4) ); + done = false; + } + + // TODO Make this more generic. Delete "if ; else use ; use" + if ( TOKEN::Match(tok2, "; if ; else use ; use") || + TOKEN::Match(tok2, "; if use ; else ; use") ) + { + erase( tok2, tok2->tokAt(4) ); + done = false; + } + // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. // This may cause false positives diff --git a/testmemleak.cpp b/testmemleak.cpp index 5859b8487..7f8fa986c 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -23,7 +23,8 @@ #include "tokenize.h" #include "CheckMemoryLeak.h" #include "testsuite.h" - + +#include #include extern std::ostringstream errout; @@ -111,7 +112,7 @@ private: TEST_CASE( func4 ); TEST_CASE( func5 ); TEST_CASE( func6 ); - // TODO TEST_CASE( func7 ); + TEST_CASE( func7 ); TEST_CASE( class1 ); TEST_CASE( class2 ); @@ -779,7 +780,7 @@ private: " foo(p);\n" "}\n" ); std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); + ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: p\n"), err ); } @@ -797,8 +798,9 @@ private: " char *p = new char[100];\n" " foo(p);\n" "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: p\n"), err ); + std::string err( errout.str() ); + std::cout << err << "\n"; + ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err ); }