Memory Leak: Improved checking of subfunctions. Simplify their code.

This commit is contained in:
Daniel Marjamäki 2008-11-09 17:36:53 +00:00
parent 2c74d1c0de
commit fd7ce880aa
2 changed files with 46 additions and 13 deletions

View File

@ -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);

View File

@ -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()
{