From fd7ce880aaffbb2369140f680745a0d9968bc900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 9 Nov 2008 17:36:53 +0000 Subject: [PATCH] Memory Leak: Improved checking of subfunctions. Simplify their code. --- CheckMemoryLeak.cpp | 41 ++++++++++++++++++++++++++++------------- testmemleak.cpp | 18 ++++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 0b75a7525..60d79336f 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -41,6 +41,7 @@ //--------------------------------------------------------------------------- static TOKEN *getcode(const TOKEN *tok, const char varname[]); +static void simplifycode(TOKEN *tok); static bool isclass( const std::string &typestr ) { @@ -219,6 +220,7 @@ static const char * call_func( const TOKEN *tok, const char *varnames[] ) while ( ftok && ! Match(ftok,"{") ) ftok = ftok->next; TOKEN *func = getcode( Tokenizer::gettok(ftok,1), parname ); + simplifycode( func ); const char *ret = 0; if ( findmatch(func, "use") ) ret = "use"; @@ -465,20 +467,13 @@ static void erase(TOKEN *begin, const TOKEN *end) } -// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" -static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) + +/** + * Simplify code + * \param tok first token + */ +static void simplifycode(TOKEN *tok) { - callstack.clear(); - - TOKEN *tok = getcode( Tok1, varname ); - - // If the variable is not allocated at all => no memory leak - if (findmatch(tok, "alloc") == 0) - { - deleteTokens(tok); - return; - } - // Remove "do"... // do { x } while (y); // => @@ -741,8 +736,28 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } } } +} + + + +// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" +static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) +{ + callstack.clear(); + + TOKEN *tok = getcode( Tok1, varname ); + + // If the variable is not allocated at all => no memory leak + if (findmatch(tok, "alloc") == 0) + { + deleteTokens(tok); + return; + } + + simplifycode( tok ); + if ( findmatch(tok, "loop alloc ;") ) { MemoryLeak(findmatch(tok, "loop alloc ;"), varname); diff --git a/testmemleak.cpp b/testmemleak.cpp index d2f78bbe9..ce0d9b458 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -102,6 +102,7 @@ private: TEST_CASE( func1 ); TEST_CASE( func2 ); TEST_CASE( func3 ); + TEST_CASE( func4 ); TEST_CASE( class1 ); TEST_CASE( class2 ); @@ -623,6 +624,23 @@ private: } + void func4() + { + check( "static void foo(char *str)\n" + "{\n" + " delete [] str;\n" + "}\n" + "\n" + "static void f()\n" + "{\n" + " char *p = new char[100];\n" + " foo(p);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + + /* void func3() {