Memory leak: reverted most of change 461.

This commit is contained in:
Daniel Marjamäki 2008-11-16 06:34:22 +00:00
parent d11e93f475
commit d5d2f0671c
3 changed files with 13 additions and 42 deletions

View File

@ -188,7 +188,7 @@ AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const cha
static std::list<std::string> 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,20 +461,9 @@ 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 )
{
// todo: better checking
addtoken("use");
}
else
{
tok2 = findmatch(tok2, "%var% [;,]");
if ( !tok2 || Match(tok2, Tokenizer::getstr(tok, 4)) )
addtoken("use");
}
}
}
return rethead;
@ -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) );
}
}
}

View File

@ -42,16 +42,16 @@ private:
void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &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 );

View File

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