From d5d2f0671cb262b75304ee7a330ef1c3bfa62770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 16 Nov 2008 06:34:22 +0000 Subject: [PATCH] Memory leak: reverted most of change 461. --- CheckMemoryLeak.cpp | 31 ++++++++++--------------------- CheckMemoryLeak.h | 6 +++--- testmemleak.cpp | 18 ------------------ 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index d91d3b1b8..36923922a 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -188,7 +188,7 @@ AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const cha static std::list callstack; -const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, const char typestr[], const char *varnames[] ) +const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, const char *varnames[] ) { if (Match(tok,"if") || Match(tok,"for") || Match(tok,"while")) return 0; @@ -227,7 +227,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, const char types // Check if the function deallocates the variable.. while ( ftok && ! Match(ftok,"{") ) ftok = ftok->next; - TOKEN *func = getcode( Tokenizer::gettok(ftok,1), typestr, parname ); + TOKEN *func = getcode( Tokenizer::gettok(ftok,1), parname ); simplifycode( func ); const char *ret = 0; if (findmatch(func, "goto")) @@ -290,7 +290,7 @@ extern bool ShowAll; * varname - name of variable */ -TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, const char typestr[], const char varname[]) +TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, const char varname[]) { const char *varnames[2]; varnames[0] = varname; @@ -453,7 +453,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, const char typestr[], con // Investigate function calls.. if ( Match(tok, "%var% (") ) { - const char *str = call_func(tok, typestr, varnames); + const char *str = call_func(tok, varnames); if ( str ) addtoken( str ); } @@ -461,19 +461,8 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, const char typestr[], con // Linux lists.. if ( Match( tok, "[=(,] & %var1% [.[]", varnames ) ) { - // Linux list -> the first member of the struct - std::string pattern("struct " + std::string(typestr) + " {"); - const TOKEN *tok2 = findmatch(tokens, pattern.c_str()); - if ( ! tok2 ) - { - addtoken("use"); - } - else - { - tok2 = findmatch(tok2, "%var% [;,]"); - if ( !tok2 || Match(tok2, Tokenizer::getstr(tok, 4)) ) - addtoken("use"); - } + // todo: better checking + addtoken("use"); } } @@ -776,11 +765,11 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) // Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" -void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char typestr[], const char varname[] ) +void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) { callstack.clear(); - TOKEN *tok = getcode( Tok1, typestr, varname ); + TOKEN *tok = getcode( Tok1, varname ); // If the variable is not allocated at all => no memory leak if (findmatch(tok, "alloc") == 0) @@ -876,10 +865,10 @@ void CheckMemoryLeakClass::CheckMemoryLeak_InFunction() if (indentlevel>0 && infunc) { if ( Match(tok, "[{};] %type% * %var% [;=]") ) - CheckMemoryLeak_CheckScope( tok->next, Tokenizer::getstr(tok, 1), Tokenizer::getstr(tok, 3) ); + CheckMemoryLeak_CheckScope( tok->next, Tokenizer::getstr(tok, 3) ); else if ( Match(tok, "[{};] %type% %type% * %var% [;=]") ) - CheckMemoryLeak_CheckScope( tok->next, Tokenizer::getstr(tok, 2), Tokenizer::getstr(tok, 4) ); + CheckMemoryLeak_CheckScope( tok->next, Tokenizer::getstr(tok, 4) ); } } } diff --git a/CheckMemoryLeak.h b/CheckMemoryLeak.h index 27557cc44..8a3192f4a 100644 --- a/CheckMemoryLeak.h +++ b/CheckMemoryLeak.h @@ -42,16 +42,16 @@ private: void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector &classname ); void CheckMemoryLeak_ClassMembers(); void CheckMemoryLeak_InFunction(); - void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char typestr[], const char varname[] ); + void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ); void simplifycode(TOKEN *tok); void erase(TOKEN *begin, const TOKEN *end); - TOKEN *getcode(const TOKEN *tok, const char typestr[], const char varname[]); + TOKEN *getcode(const TOKEN *tok, const char varname[]); bool notvar(const TOKEN *tok, const char *varnames[]); void instoken(TOKEN *tok, const char str[]); void MemoryLeak( const TOKEN *tok, const char varname[] ); void MismatchError( const TOKEN *Tok1, const char varname[] ); - const char * call_func( const TOKEN *tok, const char typestr[], const char *varnames[] ); + const char * call_func( const TOKEN *tok, const char *varnames[] ); AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] ); AllocType GetAllocationType( const TOKEN *tok2 ); bool isclass( const std::string &typestr ); diff --git a/testmemleak.cpp b/testmemleak.cpp index ee7bb083b..96298d631 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -117,7 +117,6 @@ private: TEST_CASE( throw1 ); TEST_CASE( linux_list_1 ); - TEST_CASE( linux_list_2 ); TEST_CASE( sizeof1 ); } @@ -858,23 +857,6 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } - void linux_list_2() - { - check( "struct AB\n" - "{\n" - " int a;\n" - " int b;\n" - "};\n" - "void foo()\n" - "{\n" - " struct AB *ab = new AB;\n" - " func(&ab->b);\n" - "}\n" ); - - ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: ab\n"), errout.str() ); - } - - void sizeof1()