From a0cee01d3520ad4cc3dcb6c6032a7fc3d87621b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Dec 2008 21:52:40 +0000 Subject: [PATCH] Memory leak : Updated the multipass checking a little --- checkmemoryleak.cpp | 39 ++++++++++++++++++++++++++++++++++++++- checkmemoryleak.h | 2 +- testmemleakmp.cpp | 35 ++++++++++++----------------------- 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index 6f60b6e69..8f11cf7e2 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -1348,8 +1348,45 @@ void CheckMemoryLeakClass::CheckMemoryLeak() // Non-recursive function analysis //--------------------------------------------------------------------------- -TOKEN * CheckMemoryLeakClass::functionCode(const char funcname[]) +TOKEN * CheckMemoryLeakClass::functionParameterCode(const TOKEN *ftok, int parameter) { + int param = 1; // First parameter has index 1 + + // Extract the code for specified parameter... + for ( ; ftok; ftok = ftok->next() ) + { + if ( ftok->str() == ")" ) + break; + + if ( ftok->str() == "," ) + { + ++param; + if ( param > parameter ) + break; + } + + if ( param != parameter ) + continue; + + if ( ! TOKEN::Match(ftok, "* %var% [,)]") ) + continue; + + // Extract and return the code for this parameter.. + const char *parname = ftok->strAt(1); + + // Goto function implementation.. + while ( ftok && ftok->str() != "{" ) + ftok = ftok->next(); + ftok = ftok ? ftok->next() : NULL; + + // Return the code.. + AllocType alloc=No, dealloc=No; + std::list callstack; + TOKEN *code = getcode( ftok, callstack, parname, alloc, dealloc ); + simplifycode( code ); + return code; + } + return NULL; } diff --git a/checkmemoryleak.h b/checkmemoryleak.h index 12ae961c8..444d10b97 100644 --- a/checkmemoryleak.h +++ b/checkmemoryleak.h @@ -107,7 +107,7 @@ private: #ifdef UNIT_TESTING public: #endif - TOKEN * functionCode(const char funcname[]); + TOKEN *functionParameterCode(const TOKEN *ftok, int parameter); }; //--------------------------------------------------------------------------- diff --git a/testmemleakmp.cpp b/testmemleakmp.cpp index 2d5a321e9..862601f1d 100644 --- a/testmemleakmp.cpp +++ b/testmemleakmp.cpp @@ -36,12 +36,16 @@ private: void run() { - // TODO TEST_CASE( alloc1 ); + TEST_CASE( param1 ); } - // Check that base classes have virtual destructors - std::string functionCode(const char code[], const char funcname[]) + void param1() { + const char code[] = "void f(char *s)\n" + "{\n" + " ;\n" + "}\n"; + // Tokenize.. Tokenizer tokenizer; std::istringstream istr(code); @@ -54,28 +58,13 @@ private: // Check.. Settings settings; CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this ); - TOKEN *tok = checkMemoryLeak.functionCode(funcname); + TOKEN *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1); - // Return tokens.. - std::ostringstream ret; + // Compare tokens.. + std::string s; for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) - ret << tok2->str() << " "; - while ( tok ) - { - TOKEN *tok_ = tok; - tok = tok->next(); - delete tok_; - } - return ret.str(); - } - - void alloc1() - { - const char code[] = "char *f()\n" - "{\n" - " return malloc(100);\n" - "}\n"; - ASSERT_EQUALS( "alloc ;", functionCode(code, "f") ); + s += tok2->str() + " "; + ASSERT_EQUALS( "; } ", s ); } };