CheckMemoryLeak: By default skip class memory leaks because there may be various types of garbage collectors
This commit is contained in:
parent
0679b89e84
commit
9276783ab9
|
@ -16,6 +16,27 @@
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
static bool isclass( const std::string &typestr )
|
||||
{
|
||||
if ( typestr == "char" ||
|
||||
typestr == "short" ||
|
||||
typestr == "int" ||
|
||||
typestr == "long" ||
|
||||
typestr == "float" ||
|
||||
typestr == "double" )
|
||||
return false;
|
||||
|
||||
std::ostringstream pattern;
|
||||
pattern << "struct " << typestr << " [;{]";
|
||||
if ( findmatch( tokens, pattern.str().c_str() ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
enum AllocType { No, Malloc, New, NewA };
|
||||
|
||||
// Extra allocation..
|
||||
|
@ -532,6 +553,17 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
|
|||
if (Match(tok, "[(;{}] %var1% = ", varnames))
|
||||
{
|
||||
AllocType alloc = GetAllocationType(gettok(tok,3));
|
||||
|
||||
// If "--all" hasn't been given, don't check classes..
|
||||
if ( alloc == New && ! ShowAll )
|
||||
{
|
||||
if ( Match(gettok(tok,3), "new %var% ;") )
|
||||
{
|
||||
if ( isclass( getstr(tok, 4) ) )
|
||||
alloc = No;
|
||||
}
|
||||
}
|
||||
|
||||
if ( alloc != No )
|
||||
{
|
||||
addtoken("alloc");
|
||||
|
@ -868,9 +900,12 @@ static void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vec
|
|||
if ( Match(tok->next, "%type% * %var% ;") )
|
||||
{
|
||||
if ( IsName(tok->str) || strchr(";}", tok->str[0]) )
|
||||
{
|
||||
if (ShowAll || !isclass(getstr(tok,1)))
|
||||
CheckMemoryLeak_ClassMembers_Variable( classname, getstr(tok, 3) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] )
|
||||
|
|
12
tests.cpp
12
tests.cpp
|
@ -535,6 +535,16 @@ static void memleak_in_function()
|
|||
check_( CheckMemoryLeak, __LINE__, code, "" );
|
||||
|
||||
|
||||
// Fred may be deleted automaticly by a garbage collector..
|
||||
code = "class Fred;\n"
|
||||
"void foo()\n"
|
||||
"{\n"
|
||||
" Fred *f = new Fred;\n"
|
||||
"}\n";
|
||||
ShowAll = false;
|
||||
check( CheckMemoryLeak, __LINE__, code, "" );
|
||||
ShowAll = true;
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// if else
|
||||
|
@ -780,7 +790,7 @@ static void memleak_in_function()
|
|||
" Fred *fred = new Fred;\n"
|
||||
" free( fred->Name );\n"
|
||||
"}\n";
|
||||
check_( CheckMemoryLeak, __LINE__, code, "[test.cpp:5]: Memory leak: fred\n" );
|
||||
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:5]: Memory leak: fred\n" );
|
||||
|
||||
|
||||
/* TODO
|
||||
|
|
Loading…
Reference in New Issue