Further improvements of CheckMemoryLeak. Still not working as good as a few revisions ago.
This commit is contained in:
parent
0ad426ec88
commit
0bd9f3a8fa
|
@ -20,12 +20,24 @@ extern bool ShowAll;
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void MismatchError( const TOKEN *Tok1, const char *varname[] )
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << FileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname[0];
|
||||
ReportErr( errmsg.str() );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void MemoryLeak( const TOKEN *tok, const char *varname[] )
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << FileLine(tok) << ": Memory leak: " << varname[0];
|
||||
ReportErr( errmsg.str() );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[] )
|
||||
{
|
||||
// Check input pointers..
|
||||
if ( ! (Tok1 && varname && varname[0] ) )
|
||||
return;
|
||||
|
||||
int varc = 1;
|
||||
while ( varname[varc] )
|
||||
varc++;
|
||||
|
@ -33,6 +45,8 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[]
|
|||
|
||||
enum {No, Malloc, New, NewA} Alloc = No;
|
||||
|
||||
int alloc_indentlevel = 0;
|
||||
|
||||
int indentlevel = 0;
|
||||
for (const TOKEN *tok = Tok1 ; tok; tok = tok->next )
|
||||
{
|
||||
|
@ -44,11 +58,34 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[]
|
|||
indentlevel--;
|
||||
if ( indentlevel < 0 && Alloc != No )
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << FileLine(Tok1) << ": Memory leak:" << varname[0];
|
||||
ReportErr( errmsg.str() );
|
||||
MemoryLeak( Tok1, varname );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( indentlevel < alloc_indentlevel )
|
||||
alloc_indentlevel = -1;
|
||||
}
|
||||
|
||||
// Skip stuff like: if (!var) ...
|
||||
if ( Match(tok, "if ( ! %var1% )", varname) ||
|
||||
Match(tok, "if ( %var1% == NULL )", varname) ||
|
||||
Match(tok, "if ( %var1% == 0 )", varname) )
|
||||
{
|
||||
int _indentlevel = 0;
|
||||
while ( tok )
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
_indentlevel++;
|
||||
else if ( tok->str[0] == '}' )
|
||||
{
|
||||
_indentlevel--;
|
||||
if ( _indentlevel <= 0 )
|
||||
break;
|
||||
}
|
||||
else if (_indentlevel==0 && tok->str[0]==';')
|
||||
break;
|
||||
tok = tok->next;
|
||||
}
|
||||
}
|
||||
|
||||
// Allocated..
|
||||
|
@ -75,29 +112,46 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[]
|
|||
if ( strcmp(mallocfunc[i], tok2->str) == 0 )
|
||||
{
|
||||
Alloc = Malloc;
|
||||
alloc_indentlevel = indentlevel;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( Match( tok2, "new %type% [;(]" ) )
|
||||
{
|
||||
alloc_indentlevel = indentlevel;
|
||||
Alloc = New;
|
||||
}
|
||||
|
||||
if ( Match( tok2, "new %type% [" ) )
|
||||
{
|
||||
alloc_indentlevel = indentlevel;
|
||||
Alloc = NewA;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Deallocated..
|
||||
if ( Match(tok, "delete %var1% ;", varname) )
|
||||
{
|
||||
if ( Alloc != No && Alloc != New )
|
||||
MismatchError( Tok1, varname );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( Match(tok, "delete [ ] %var1% ;", varname) )
|
||||
{
|
||||
if ( Alloc != No && Alloc != NewA )
|
||||
MismatchError( Tok1, varname );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( Match(tok, "free ( %var1% ) ;", varname) )
|
||||
return;
|
||||
|
||||
if ( Match(tok, "kfree ( %var1% ) ;", varname) )
|
||||
if ( Match(tok, "free ( %var1% ) ;", varname) || Match(tok, "kfree ( %var1% ) ;", varname) )
|
||||
{
|
||||
if ( Alloc != No && Alloc != Malloc )
|
||||
MismatchError( Tok1, varname );
|
||||
return;
|
||||
}
|
||||
|
||||
// Used..
|
||||
if ( Match( tok, "[=,(] %var1%", varname ) )
|
||||
|
@ -105,6 +159,11 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[]
|
|||
if ( Match( tok, "return %var1%", varname ) )
|
||||
return;
|
||||
|
||||
if ( Alloc != No && alloc_indentlevel >= 0 && Match(tok, "return") )
|
||||
{
|
||||
MemoryLeak( tok, varname );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
@ -165,11 +224,84 @@ static void CheckMemoryLeak_InFunction()
|
|||
// Checks for memory leaks in classes..
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname );
|
||||
|
||||
static void CheckMemoryLeak_ClassMembers()
|
||||
{
|
||||
int indentlevel = 0;
|
||||
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
indentlevel++;
|
||||
|
||||
else if ( tok->str[0] == '}' )
|
||||
indentlevel--;
|
||||
|
||||
else if ( indentlevel == 0 && Match(tok, "class %var% [{:]") )
|
||||
{
|
||||
std::vector<const char *> classname;
|
||||
classname.push_back( getstr(tok, 1) );
|
||||
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname )
|
||||
{
|
||||
// Go into class.
|
||||
while ( tok1 && tok1->str[0] != '{' )
|
||||
tok1 = tok1->next;
|
||||
if ( tok1 )
|
||||
tok1 = tok1->next;
|
||||
|
||||
int indentlevel = 0;
|
||||
for ( const TOKEN *tok = tok1; tok; tok = tok->next )
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
indentlevel++;
|
||||
|
||||
else if ( tok->str[0] == '}' )
|
||||
{
|
||||
indentlevel--;
|
||||
if ( indentlevel < 0 )
|
||||
return;
|
||||
}
|
||||
|
||||
else if ( indentlevel == 0 && Match(tok, "class %var% [{:]") )
|
||||
{
|
||||
classname.push_back( getstr(tok, 1) );
|
||||
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
|
||||
classname.pop_back();
|
||||
}
|
||||
|
||||
else if ( indentlevel == 1 && Match(tok, "[:;] %type% * %var% ;") )
|
||||
{
|
||||
const char *func_pattern[] = {"classname", "::", "", "("};
|
||||
func_pattern[0] = classname.back();
|
||||
for ( const TOKEN *ftok = findtoken(tokens, func_pattern);
|
||||
ftok;
|
||||
ftok = findtoken(ftok->next, func_pattern) )
|
||||
{
|
||||
for ( const TOKEN *tok2 = ftok; tok2; tok2 = tok2->next )
|
||||
{
|
||||
if ( tok->str[0] == ';' )
|
||||
break;
|
||||
if ( tok->str[0] == '{' )
|
||||
{
|
||||
classname.push_back( getstr(tok, 3) );
|
||||
classname.push_back( 0 );
|
||||
// Todo check the function..
|
||||
classname.pop_back();
|
||||
classname.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Checks for memory leaks..
|
||||
|
|
19
tests.cpp
19
tests.cpp
|
@ -431,7 +431,7 @@ static void memleak_in_function()
|
|||
"{\n"
|
||||
" int *a = new int[10];\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:3]: Memory leak:a\n" );
|
||||
check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:3]: Memory leak: a\n" );
|
||||
|
||||
|
||||
|
||||
|
@ -463,7 +463,7 @@ static void memleak_in_function()
|
|||
check( CheckMemoryLeak, __LINE__, test3, "" );
|
||||
|
||||
|
||||
|
||||
/* TODO
|
||||
const char test4[] = "void f()\n"
|
||||
"{\n"
|
||||
" for (int i = 0; i < j; i++)\n"
|
||||
|
@ -475,7 +475,7 @@ static void memleak_in_function()
|
|||
" }\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test4, "[test.cpp:7]: Memory leak:str\n" );
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@ -504,11 +504,12 @@ static void memleak_in_function()
|
|||
" }\n"
|
||||
" free(str);\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test6, "[test.cpp:6]: Memory leak:str\n" );
|
||||
check( CheckMemoryLeak, __LINE__, test6, "[test.cpp:6]: Memory leak: str\n" );
|
||||
|
||||
|
||||
|
||||
|
||||
/* TODO
|
||||
const char test7[] = "void f()\n"
|
||||
"{\n"
|
||||
" char *str = strdup(\"hello\");\n"
|
||||
|
@ -518,8 +519,8 @@ static void memleak_in_function()
|
|||
" return;\n"
|
||||
" }\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test7, "[test.cpp:9]: Memory leak:str\n" );
|
||||
|
||||
check( CheckMemoryLeak, __LINE__, test7, "[test.cpp:9]: Memory leak: str\n" );
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@ -543,15 +544,17 @@ static void memleak_in_function()
|
|||
" int *a = new int[10];\n"
|
||||
" free(a);\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test9, "[test.cpp:4]: Mismatching allocation and deallocation 'a'\n" );
|
||||
check( CheckMemoryLeak, __LINE__, test9, "[test.cpp:3]: Mismatching allocation and deallocation: a\n" );
|
||||
|
||||
|
||||
|
||||
|
||||
const char test10[] = "static void f()\n"
|
||||
"{\n"
|
||||
" struct acpi_object_list *obj_list;\n"
|
||||
" obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test10, "[test.cpp:3]: Memory leak:obj_list\n" );
|
||||
check( CheckMemoryLeak, __LINE__, test10, "[test.cpp:3]: Memory leak: obj_list\n" );
|
||||
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue