2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
|
2007-07-17 08:39:34 +02:00
|
|
|
#include "tokenize.h"
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CommonCheck.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
|
2008-04-06 08:26:11 +02:00
|
|
|
#ifdef __BORLANDC__
|
2007-05-24 15:08:51 +02:00
|
|
|
#include <mem.h> // <- memset
|
2008-04-06 08:26:11 +02:00
|
|
|
#else
|
|
|
|
#include <string.h>
|
2007-07-17 08:15:50 +02:00
|
|
|
#endif
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2007-07-20 08:20:31 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-08-07 20:53:35 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
enum AllocType { No, Malloc, New, NewA };
|
2008-08-07 20:53:35 +02:00
|
|
|
|
|
|
|
// Extra allocation..
|
|
|
|
class AllocFunc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char *funcname;
|
|
|
|
AllocType alloctype;
|
|
|
|
|
|
|
|
AllocFunc(const char f[], AllocType a)
|
|
|
|
{
|
|
|
|
funcname = f;
|
|
|
|
alloctype = a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static std::list<AllocFunc> listallocfunc;
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
static AllocType GetAllocationType( const TOKEN *tok2 )
|
|
|
|
{
|
|
|
|
// What we may have...
|
|
|
|
// * var = (char *)malloc(10);
|
|
|
|
// * var = new char[10];
|
|
|
|
// * var = strdup("hello");
|
|
|
|
if ( tok2 && tok2->str[0] == '(' )
|
|
|
|
{
|
|
|
|
while ( tok2 && tok2->str[0] != ')' )
|
|
|
|
tok2 = tok2->next;
|
|
|
|
tok2 = tok2 ? tok2->next : NULL;
|
|
|
|
}
|
|
|
|
if ( ! tok2 )
|
|
|
|
return No;
|
|
|
|
|
|
|
|
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
|
2008-04-06 12:36:41 +02:00
|
|
|
const char *mallocfunc[] = {"malloc",
|
|
|
|
"calloc",
|
|
|
|
"realloc",
|
|
|
|
"strdup",
|
|
|
|
"kmalloc",
|
|
|
|
"kzalloc",
|
|
|
|
"g_malloc",
|
|
|
|
0};
|
2008-04-05 14:27:02 +02:00
|
|
|
for ( unsigned int i = 0; mallocfunc[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( strcmp(mallocfunc[i], tok2->str) == 0 )
|
|
|
|
return Malloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Match( tok2, "new %type% [;(]" ) )
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if ( Match( tok2, "new %type% [" ) )
|
2008-08-07 20:53:35 +02:00
|
|
|
return NewA;
|
|
|
|
|
|
|
|
// Userdefined allocation function..
|
|
|
|
std::list<AllocFunc>::const_iterator it = listallocfunc.begin();
|
|
|
|
while ( it != listallocfunc.end() )
|
|
|
|
{
|
|
|
|
if ( strcmp(tok2->str, it->funcname) == 0 )
|
|
|
|
return it->alloctype;
|
|
|
|
++it;
|
2008-05-10 09:33:50 +02:00
|
|
|
}
|
2008-08-07 20:53:35 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] )
|
|
|
|
{
|
2008-08-12 20:24:42 +02:00
|
|
|
// Redundant condition..
|
|
|
|
if ( Match(tok, "if ( %var1% )", varnames) )
|
|
|
|
{
|
|
|
|
tok = gettok( tok, 4 );
|
|
|
|
if ( Match(tok,"{") )
|
|
|
|
tok = tok->next;
|
2008-08-13 18:41:27 +02:00
|
|
|
}
|
2008-08-12 20:24:42 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Match(tok, "delete %var1% ;", varnames) )
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if ( Match(tok, "delete [ ] %var1% ;", varnames) )
|
|
|
|
return NewA;
|
|
|
|
|
|
|
|
if ( Match(tok, "free ( %var1% ) ;", varnames) ||
|
2008-04-06 12:36:41 +02:00
|
|
|
Match(tok, "kfree ( %var1% ) ;", varnames) ||
|
|
|
|
Match(tok, "g_free ( %var1% ) ;", varnames) )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
|
|
|
return Malloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return No;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void MismatchError( const TOKEN *Tok1, const char varname[] )
|
2007-05-24 15:08:51 +02:00
|
|
|
{
|
2008-04-04 06:29:28 +02:00
|
|
|
std::ostringstream errmsg;
|
2008-04-05 14:27:02 +02:00
|
|
|
errmsg << FileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
|
2008-04-04 06:29:28 +02:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-04-03 08:15:26 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
static void MemoryLeak( const TOKEN *tok, const char varname[] )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-04-05 14:27:02 +02:00
|
|
|
errmsg << FileLine(tok) << ": Memory leak: " << varname;
|
2008-04-04 06:29:28 +02:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
static void CheckMemoryLeak_CheckScope_All( const TOKEN *Tok1, const char varname[] )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-05 14:27:02 +02:00
|
|
|
const char *varnames[2];
|
|
|
|
varnames[0] = varname;
|
|
|
|
varnames[1] = 0;
|
2008-04-03 08:15:26 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
AllocType Alloc = No;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-08-09 10:34:05 +02:00
|
|
|
int alloc_indentlevel = -1;
|
|
|
|
int dealloc_indentlevel = -1;
|
|
|
|
std::list<int> loop_indentlevel;
|
2008-08-14 08:41:26 +02:00
|
|
|
std::list<int> switch_indentlevel;
|
2008-04-04 06:29:28 +02:00
|
|
|
|
2008-04-14 20:42:30 +02:00
|
|
|
bool isif = false;
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
int indentlevel = 0;
|
2008-04-03 08:15:26 +02:00
|
|
|
for (const TOKEN *tok = Tok1 ; tok; tok = tok->next )
|
2007-05-24 15:08:51 +02:00
|
|
|
{
|
2008-04-02 07:09:35 +02:00
|
|
|
if (tok->str[0]=='{')
|
|
|
|
indentlevel++;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
else if (tok->str[0]=='}')
|
|
|
|
{
|
|
|
|
indentlevel--;
|
2008-04-14 08:25:34 +02:00
|
|
|
if ( indentlevel < 0 )
|
2008-04-02 07:09:35 +02:00
|
|
|
{
|
2008-04-14 08:25:34 +02:00
|
|
|
if ( Alloc != No )
|
2008-08-15 21:17:06 +02:00
|
|
|
MemoryLeak( tok, varname );
|
2008-04-02 07:09:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-04-04 06:29:28 +02:00
|
|
|
|
2008-08-09 10:34:05 +02:00
|
|
|
if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() )
|
|
|
|
loop_indentlevel.pop_back();
|
|
|
|
|
2008-08-14 08:41:26 +02:00
|
|
|
if ( !switch_indentlevel.empty() && indentlevel <= switch_indentlevel.back() )
|
|
|
|
switch_indentlevel.pop_back();
|
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
if ( indentlevel < alloc_indentlevel )
|
|
|
|
alloc_indentlevel = -1;
|
2008-08-07 21:50:01 +02:00
|
|
|
|
|
|
|
if ( indentlevel < dealloc_indentlevel )
|
|
|
|
dealloc_indentlevel = -1;
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 19:16:17 +02:00
|
|
|
if ( Alloc != No && Match(tok, ". %var% (") )
|
|
|
|
{
|
|
|
|
bool isused = false;
|
|
|
|
while (tok && !Match(tok, "[;{]"))
|
|
|
|
{
|
|
|
|
if ( Match(tok, "[(,] %var1% [,)]", varnames) )
|
|
|
|
isused = true;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't know what happens, assume that it's deallocated.
|
|
|
|
if ( isused )
|
|
|
|
{
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
dealloc_indentlevel = indentlevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 20:24:42 +02:00
|
|
|
// Check subfunction...
|
|
|
|
if (Alloc != No && Match(tok,"[{};] %var% ("))
|
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
AllocType dealloc = GetDeallocationType(tok->next, varnames);
|
2008-08-12 20:24:42 +02:00
|
|
|
|
2008-08-13 18:41:27 +02:00
|
|
|
const char *funcname = getstr( tok, 1 );
|
|
|
|
if (dealloc==No && strcmp(funcname,"if") && strcmp(funcname,"for") && strcmp(funcname,"while"))
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
unsigned int param = 0;
|
|
|
|
for (const TOKEN *tok2 = gettok(tok,2); tok2; tok2 = tok2->next)
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( tok2->str[0] == ';' || tok2->str[0] == ')' )
|
2008-08-12 20:24:42 +02:00
|
|
|
break;
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( tok2->str[0] == ',' )
|
|
|
|
param++;
|
2008-08-12 20:24:42 +02:00
|
|
|
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( Match(tok2, "[(,] %var1% [,)]", varnames) )
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
// Find function..
|
|
|
|
const TOKEN *ftok = GetFunctionTokenByName( funcname );
|
|
|
|
ftok = gettok(ftok,2);
|
|
|
|
if ( ! ftok )
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
// Can't find the function but to avoid false
|
|
|
|
// positives it is assumed that the variable is
|
|
|
|
// deallocated..
|
|
|
|
|
|
|
|
// Deallocated at same indentlevel as the allocation => no memory leak
|
|
|
|
if ( alloc_indentlevel == indentlevel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
dealloc_indentlevel = indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Goto function parameter..
|
|
|
|
for ( unsigned int fparam = 0; ftok && fparam < param; ftok = ftok->next )
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( ftok->str[0] == ',' )
|
|
|
|
++fparam;
|
2008-08-12 20:24:42 +02:00
|
|
|
}
|
2008-08-13 18:41:27 +02:00
|
|
|
for ( ; ftok; ftok = ftok->next )
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( ! Match(ftok,"%var% [,)]") )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *paramname[2] = {0};
|
|
|
|
paramname[0] = ftok->str;
|
|
|
|
// parse function and check if it deallocates the parameter..
|
|
|
|
int _indentlevel = 0;
|
|
|
|
while (_indentlevel>=0 && ftok)
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( ftok->str[0] == '{' )
|
|
|
|
_indentlevel++;
|
|
|
|
else if ( ftok->str[0] == '}' )
|
2008-08-12 20:24:42 +02:00
|
|
|
{
|
2008-08-13 18:41:27 +02:00
|
|
|
_indentlevel--;
|
|
|
|
if ( _indentlevel <= 0 )
|
|
|
|
break;
|
2008-08-12 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 18:41:27 +02:00
|
|
|
if ( _indentlevel >= 1 )
|
|
|
|
{
|
|
|
|
AllocType dealloc = GetDeallocationType(ftok,paramname);
|
|
|
|
if ( dealloc != No )
|
|
|
|
{
|
|
|
|
if ( Alloc != No && Alloc != dealloc )
|
|
|
|
{
|
|
|
|
MismatchError( Tok1, varname );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deallocated at same indentlevel as the allocation => no memory leak
|
|
|
|
if ( alloc_indentlevel == indentlevel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
dealloc_indentlevel = indentlevel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-12 20:24:42 +02:00
|
|
|
|
2008-08-13 18:41:27 +02:00
|
|
|
ftok = ftok->next;
|
2008-08-12 20:24:42 +02:00
|
|
|
}
|
2008-08-13 18:41:27 +02:00
|
|
|
break;
|
2008-08-12 20:24:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 10:34:05 +02:00
|
|
|
// for, while set loop level..
|
2008-08-12 20:24:42 +02:00
|
|
|
if ( Match(tok,"while") || Match(tok,"for") )
|
2008-08-09 10:34:05 +02:00
|
|
|
loop_indentlevel.push_back( indentlevel );
|
|
|
|
|
2008-08-14 08:41:26 +02:00
|
|
|
// switch..
|
|
|
|
if (Match(tok,"switch"))
|
|
|
|
switch_indentlevel.push_back( indentlevel );
|
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
// Skip stuff like: if (!var) ...
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Match(tok, "if ( ! %var1% )", varnames) ||
|
2008-05-03 09:16:22 +02:00
|
|
|
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
|
2008-04-05 14:27:02 +02:00
|
|
|
Match(tok, "if ( %var1% == NULL )", varnames) ||
|
2008-04-14 08:25:34 +02:00
|
|
|
Match(tok, "if ( NULL == %var1% )", varnames) ||
|
2008-04-05 14:27:02 +02:00
|
|
|
Match(tok, "if ( %var1% == 0 )", varnames) )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-04-02 07:09:35 +02:00
|
|
|
}
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-14 20:42:30 +02:00
|
|
|
// if..
|
|
|
|
if ( Match(tok,"if") )
|
|
|
|
isif = true;
|
|
|
|
if ( strchr(";{}", tok->str[0]) )
|
|
|
|
isif = false;
|
|
|
|
|
2008-04-03 08:15:26 +02:00
|
|
|
// Allocated..
|
2008-05-03 09:16:22 +02:00
|
|
|
if ( Match(tok, "[(;{}] %var1% =", varnames) )
|
2008-04-03 08:15:26 +02:00
|
|
|
{
|
2008-05-03 09:16:22 +02:00
|
|
|
AllocType alloc = GetAllocationType( gettok(tok, 3) );
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( alloc != No )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-05-03 09:16:22 +02:00
|
|
|
tok = tok->next;
|
2008-04-05 14:27:02 +02:00
|
|
|
Alloc = alloc;
|
2008-04-04 06:29:28 +02:00
|
|
|
alloc_indentlevel = indentlevel;
|
2008-04-14 20:42:30 +02:00
|
|
|
|
|
|
|
if ( isif )
|
|
|
|
{
|
|
|
|
while ( tok )
|
|
|
|
{
|
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
{
|
|
|
|
indentlevel++;
|
|
|
|
}
|
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel <= alloc_indentlevel )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if ( tok->str[0] == ';' && indentlevel == alloc_indentlevel )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
}
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
2008-04-03 08:15:26 +02:00
|
|
|
}
|
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
// Deallocated..
|
2008-04-05 14:27:02 +02:00
|
|
|
AllocType dealloc = GetDeallocationType( tok, varnames );
|
|
|
|
if ( dealloc != No )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Alloc != No && Alloc != dealloc )
|
2008-08-07 21:50:01 +02:00
|
|
|
{
|
2008-08-15 21:17:06 +02:00
|
|
|
MismatchError( tok, varname );
|
2008-08-07 21:50:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deallocated at same indentlevel as the allocation => no memory leak
|
|
|
|
if ( alloc_indentlevel == indentlevel )
|
|
|
|
return;
|
|
|
|
|
|
|
|
dealloc_indentlevel = indentlevel;
|
|
|
|
while ( tok && tok->str[0] != ';' )
|
|
|
|
tok = tok->next;
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
// Used..
|
2008-04-08 09:03:32 +02:00
|
|
|
// list.push_back( var1 );
|
|
|
|
// listtail->next = var1;
|
2008-08-12 20:24:42 +02:00
|
|
|
if ( Match( tok, "[=] %var1% [;]", varnames ) )
|
2008-08-07 21:50:01 +02:00
|
|
|
{
|
2008-04-14 20:42:30 +02:00
|
|
|
return;
|
2008-08-07 21:50:01 +02:00
|
|
|
}
|
2008-04-16 17:22:50 +02:00
|
|
|
if ( Match( tok, "[=,(] ( %type% * ) %var1% [,);]", varnames ) )
|
2008-08-07 21:50:01 +02:00
|
|
|
{
|
2008-04-16 17:22:50 +02:00
|
|
|
return;
|
2008-08-07 21:50:01 +02:00
|
|
|
}
|
2008-04-16 17:22:50 +02:00
|
|
|
if ( Match( tok, "[=,(] ( %type% %type% * ) %var1% [,);]", varnames ) )
|
2008-08-07 21:50:01 +02:00
|
|
|
{
|
2008-04-02 07:09:35 +02:00
|
|
|
return;
|
2008-08-07 21:50:01 +02:00
|
|
|
}
|
2008-04-12 13:33:48 +02:00
|
|
|
|
2008-05-04 12:15:39 +02:00
|
|
|
// Used. Todo: check if "p" is the first member in the struct.
|
|
|
|
// p = &var1->p;
|
|
|
|
if ( Match( tok, "= & %var1% . %var% ;", varnames ) )
|
2008-08-07 21:50:01 +02:00
|
|
|
{
|
2008-05-04 12:15:39 +02:00
|
|
|
return;
|
2008-08-07 21:50:01 +02:00
|
|
|
}
|
2008-05-04 12:15:39 +02:00
|
|
|
|
2008-05-10 09:33:50 +02:00
|
|
|
// Linux lists.. todo: check if the first struct member is passed
|
2008-08-07 20:53:35 +02:00
|
|
|
if ( Match( tok, "%var% ( & %var1% .", varnames ) ||
|
2008-05-10 10:33:22 +02:00
|
|
|
Match( tok, ", & %var1% .", varnames ) )
|
2008-04-16 17:22:50 +02:00
|
|
|
{
|
2008-05-10 10:33:22 +02:00
|
|
|
return;
|
2008-04-16 17:22:50 +02:00
|
|
|
}
|
2008-04-14 20:42:30 +02:00
|
|
|
|
2008-04-12 14:07:35 +02:00
|
|
|
// continue/break loop..
|
|
|
|
if (Alloc != No &&
|
2008-08-09 10:34:05 +02:00
|
|
|
loop_indentlevel.empty() &&
|
2008-08-14 08:41:26 +02:00
|
|
|
switch_indentlevel.empty() &&
|
2008-04-12 14:07:35 +02:00
|
|
|
(Match(tok,"continue") || Match(tok,"break")))
|
|
|
|
{
|
|
|
|
MemoryLeak( tok, varname );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-12 13:33:48 +02:00
|
|
|
// Return without deallocating the memory..
|
2008-08-13 21:08:24 +02:00
|
|
|
if ( Alloc != No && (indentlevel==0 || (alloc_indentlevel >= 0 && dealloc_indentlevel <= 0)) && Match(tok, "return") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-12 13:44:32 +02:00
|
|
|
bool retvar = false;
|
2008-04-12 13:33:48 +02:00
|
|
|
for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( Match( tok2, "%var1%", varnames ) )
|
2008-04-12 13:44:32 +02:00
|
|
|
{
|
|
|
|
retvar = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-12 13:33:48 +02:00
|
|
|
if ( tok2->str[0] == ';' )
|
2008-04-12 13:44:32 +02:00
|
|
|
{
|
2008-04-12 13:33:48 +02:00
|
|
|
break;
|
2008-04-12 13:44:32 +02:00
|
|
|
}
|
2008-04-12 13:33:48 +02:00
|
|
|
}
|
2008-04-12 13:44:32 +02:00
|
|
|
|
|
|
|
if ( ! retvar )
|
2008-08-07 20:53:35 +02:00
|
|
|
MemoryLeak( tok, varname );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The allocated memory is returned.. check that it is deallocated
|
|
|
|
|
|
|
|
// Get function name..
|
|
|
|
const char *funcname = 0;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for ( const TOKEN *ftok = tokens; ftok && ftok != tok; ftok = ftok->next )
|
|
|
|
{
|
|
|
|
if ( ftok->str[0] == '{' )
|
|
|
|
indentlevel++;
|
|
|
|
|
|
|
|
else if ( ftok->str[0] == '}' )
|
|
|
|
indentlevel--;
|
|
|
|
|
|
|
|
if ( indentlevel <= 0 )
|
|
|
|
{
|
|
|
|
if ( Match(ftok, "[};]") )
|
|
|
|
funcname = 0;
|
|
|
|
else if ( Match(ftok, "%var% (") )
|
|
|
|
funcname = ftok->str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( funcname )
|
|
|
|
{
|
|
|
|
listallocfunc.push_back( AllocFunc(funcname, Alloc) );
|
|
|
|
}
|
|
|
|
}
|
2008-04-12 13:44:32 +02:00
|
|
|
|
2008-08-13 21:08:24 +02:00
|
|
|
if ( indentlevel == 0 )
|
2008-04-12 13:44:32 +02:00
|
|
|
return;
|
2008-08-13 21:08:24 +02:00
|
|
|
|
|
|
|
if ( indentlevel <= alloc_indentlevel )
|
|
|
|
{
|
|
|
|
Alloc = No;
|
|
|
|
alloc_indentlevel = -1;
|
|
|
|
dealloc_indentlevel = -1;
|
|
|
|
}
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
2007-05-24 15:08:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
extern bool ShowAll;
|
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
static TOKEN *getcode(const TOKEN *tok, const char varname[])
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
|
|
|
const char *varnames[2];
|
|
|
|
varnames[0] = varname;
|
|
|
|
varnames[1] = 0;
|
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
TOKEN *rethead = 0, *rettail = 0;
|
|
|
|
#define addtoken(_str) \
|
|
|
|
{ \
|
|
|
|
TOKEN *newtok = new TOKEN; \
|
|
|
|
newtok->str = _str; \
|
|
|
|
newtok->linenr = tok->linenr; \
|
|
|
|
newtok->FileIndex = tok->FileIndex; \
|
|
|
|
newtok->next = 0; \
|
|
|
|
if (rettail) \
|
|
|
|
rettail->next = newtok; \
|
|
|
|
else \
|
|
|
|
rethead = newtok; \
|
|
|
|
rettail=newtok; \
|
|
|
|
}
|
2008-08-15 21:17:06 +02:00
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
AllocType alloctype = No;
|
|
|
|
AllocType dealloctype = No;
|
2008-08-15 21:17:06 +02:00
|
|
|
|
|
|
|
int indentlevel = 0;
|
2008-08-16 14:44:46 +02:00
|
|
|
int parlevel = 0;
|
|
|
|
for ( ; tok; tok = tok->next )
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
2008-08-16 14:44:46 +02:00
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
{
|
|
|
|
addtoken( "{" );
|
2008-08-15 21:17:06 +02:00
|
|
|
indentlevel++;
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
|
|
|
else if ( tok->str[0] == '}' )
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
2008-08-16 14:44:46 +02:00
|
|
|
addtoken( "}" );
|
|
|
|
if ( indentlevel <= 0 )
|
|
|
|
break;
|
2008-08-15 21:17:06 +02:00
|
|
|
indentlevel--;
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( tok->str[0] == '(' )
|
|
|
|
parlevel++;
|
|
|
|
else if ( tok->str[0] == ')' )
|
|
|
|
parlevel--;
|
|
|
|
|
|
|
|
if ( parlevel == 0 && tok->str[0]==';')
|
|
|
|
addtoken(";");
|
|
|
|
|
|
|
|
if (Match(tok, "[(;{}] %var1% = ", varnames))
|
|
|
|
{
|
|
|
|
AllocType alloc = GetAllocationType(gettok(tok,3));
|
|
|
|
if ( alloc != No )
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
2008-08-16 14:44:46 +02:00
|
|
|
addtoken("alloc");
|
|
|
|
if (alloctype!=No && alloctype!=alloc)
|
|
|
|
MismatchError(tok, varname);
|
|
|
|
if (dealloctype!=No && dealloctype!=alloc)
|
|
|
|
MismatchError(tok, varname);
|
|
|
|
alloctype = alloc;
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
AllocType dealloc = GetDeallocationType(tok, varnames);
|
2008-08-15 21:17:06 +02:00
|
|
|
if ( dealloc != No )
|
|
|
|
{
|
2008-08-16 14:44:46 +02:00
|
|
|
addtoken("dealloc");
|
|
|
|
if (alloctype!=No && alloctype!=dealloc)
|
|
|
|
MismatchError(tok, varname);
|
|
|
|
if (dealloctype!=No && dealloctype!=dealloc)
|
|
|
|
MismatchError(tok, varname);
|
|
|
|
dealloctype = dealloc;
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-16 19:49:40 +02:00
|
|
|
// if else switch
|
|
|
|
if ( Match(tok, "if ( %var1% )", varnames) )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("if(var)");
|
|
|
|
}
|
|
|
|
else if ( Match(tok, "if ( ! %var1% )", varnames) ||
|
|
|
|
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
|
|
|
|
Match(tok, "if ( %var1% == NULL )", varnames) ||
|
|
|
|
Match(tok, "if ( NULL == %var1% )", varnames) ||
|
|
|
|
Match(tok, "if ( %var1% == 0 )", varnames) )
|
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Match(tok, "if"))
|
|
|
|
addtoken("if");
|
|
|
|
if (Match(tok, "else"))
|
|
|
|
addtoken("else");
|
|
|
|
if (Match(tok, "switch"))
|
|
|
|
addtoken("switch");
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
2008-08-15 21:17:06 +02:00
|
|
|
|
2008-08-16 19:49:40 +02:00
|
|
|
// Loops..
|
|
|
|
if ( Match(tok, "for") )
|
|
|
|
addtoken("loop");
|
|
|
|
if ( Match(tok, "while") )
|
|
|
|
addtoken("loop");
|
|
|
|
if ( Match(tok, "do") )
|
|
|
|
addtoken("loop");
|
|
|
|
|
|
|
|
// continue / break..
|
|
|
|
if ( Match(tok, "continue") )
|
|
|
|
addtoken("continue");
|
|
|
|
if ( Match(tok, "break") )
|
|
|
|
addtoken("break");
|
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
// Return..
|
2008-08-16 19:49:40 +02:00
|
|
|
if ( Match(tok, "return") )
|
|
|
|
{
|
|
|
|
addtoken("return");
|
|
|
|
if ( Match(tok, "return %var1%", varnames) ||
|
|
|
|
Match(tok, "return & %var1%", varnames) )
|
|
|
|
addtoken("use");
|
|
|
|
}
|
2008-08-16 18:47:54 +02:00
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
// Assignment..
|
2008-08-16 14:44:46 +02:00
|
|
|
if ( Match(tok,"[)=] %var1%", varnames) )
|
|
|
|
addtoken("use");
|
|
|
|
|
|
|
|
// Function parameter..
|
|
|
|
if ( Match(tok, "[(,] %var1% [,)]", varnames) )
|
|
|
|
addtoken("use");
|
2008-08-16 15:13:36 +02:00
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
// Linux lists..
|
|
|
|
if ( Match( tok, "[=(,] & %var1% [.[]", varnames ) )
|
2008-08-16 15:13:36 +02:00
|
|
|
addtoken("use");
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rethead;
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:49:40 +02:00
|
|
|
static void erase(TOKEN *begin, const TOKEN *end)
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
if ( ! begin )
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ( begin->next && begin->next != end )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
TOKEN *next = begin->next;
|
|
|
|
begin->next = begin->next->next;
|
2008-08-16 18:47:54 +02:00
|
|
|
delete next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
|
|
|
|
static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
|
|
|
|
{
|
|
|
|
if ( ShowAll )
|
|
|
|
{
|
|
|
|
CheckMemoryLeak_CheckScope_All(Tok1, varname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN *tok = getcode( Tok1, varname );
|
|
|
|
|
|
|
|
// If the variable is not allocated at all => no memory leak
|
|
|
|
if (findmatch(tok, "alloc") == 0)
|
|
|
|
{
|
|
|
|
deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// reduce the code..
|
2008-08-16 18:47:54 +02:00
|
|
|
bool done = false;
|
|
|
|
while ( ! done )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-08-16 18:47:54 +02:00
|
|
|
done = true;
|
|
|
|
|
|
|
|
for (TOKEN *tok2 = tok ; tok2; tok2 = tok2->next )
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
// Delete extra ";"
|
2008-08-16 18:47:54 +02:00
|
|
|
while (Match(tok2,"[;{}] ;"))
|
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
erase(tok2, gettok(tok2,2));
|
2008-08-16 18:47:54 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:49:40 +02:00
|
|
|
// Delete "else { }", "else if { }", "else ;" and "else if ;"
|
|
|
|
if ( Match(tok2->next, "else") )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
const TOKEN *_tok2 = gettok(tok2,2);
|
|
|
|
|
|
|
|
// Delete optional "if"
|
|
|
|
if ( Match( _tok2, "if" ) )
|
|
|
|
_tok2 = _tok2->next;
|
|
|
|
|
|
|
|
// Delete "{ }" or ";"
|
|
|
|
if (Match(_tok2, "{ }"))
|
|
|
|
{
|
|
|
|
erase(tok2, _tok2->next->next);
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
else if ( Match(_tok2, ";") )
|
|
|
|
{
|
|
|
|
erase(tok2, _tok2->next);
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete "loop ;" and "loop { }"
|
|
|
|
if ( Match(tok2->next, "loop") )
|
|
|
|
{
|
|
|
|
if ( Match(gettok(tok2,2), ";") )
|
|
|
|
{
|
|
|
|
erase(tok2, gettok(tok2,3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( Match(gettok(tok2,2), "{ }") )
|
|
|
|
{
|
|
|
|
erase(tok2, gettok(tok2,4));
|
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 18:47:54 +02:00
|
|
|
}
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
2008-08-15 21:17:06 +02:00
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
if ( findmatch(tok,"alloc ; return ;") )
|
|
|
|
{
|
|
|
|
MemoryLeak(gettok(findmatch(tok,"alloc ; return ;"),2), varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( ! findmatch(tok,"dealloc") &&
|
|
|
|
! findmatch(tok,"use") &&
|
|
|
|
! findmatch(tok,"return use ;") )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
|
|
|
const TOKEN *last = tok;
|
|
|
|
while (last->next)
|
|
|
|
last = last->next;
|
|
|
|
MemoryLeak(last, varname);
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
2008-08-16 14:44:46 +02:00
|
|
|
|
|
|
|
deleteTokens(tok);
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
|
2007-06-10 20:25:39 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-04-02 07:09:35 +02:00
|
|
|
// Checks for memory leaks inside function..
|
2007-06-10 20:25:39 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
static void CheckMemoryLeak_InFunction()
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
bool infunc = false;
|
2008-04-02 07:09:35 +02:00
|
|
|
int indentlevel = 0;
|
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-02 07:09:35 +02:00
|
|
|
if (tok->str[0]=='{')
|
|
|
|
indentlevel++;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
else if (tok->str[0]=='}')
|
|
|
|
indentlevel--;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-03 08:15:26 +02:00
|
|
|
|
|
|
|
// In function..
|
|
|
|
if ( indentlevel == 0 )
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
if ( Match(tok, ") {") )
|
|
|
|
infunc = true;
|
|
|
|
|
|
|
|
if ( Match(tok, "[;}]") )
|
|
|
|
infunc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a local variable => Check
|
|
|
|
if (indentlevel>0 && infunc)
|
|
|
|
{
|
|
|
|
if ( Match(tok, "[{};] %type% * %var% [;=]") )
|
2008-04-05 14:27:02 +02:00
|
|
|
CheckMemoryLeak_CheckScope( tok->next, getstr(tok, 3) );
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
else if ( Match(tok, "[{};] %type% %type% * %var% [;=]") )
|
|
|
|
CheckMemoryLeak_CheckScope( tok->next, getstr(tok, 4) );
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 07:09:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-06-10 20:25:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks in classes..
|
|
|
|
//---------------------------------------------------------------------------
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
static void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname );
|
2008-04-05 14:27:02 +02:00
|
|
|
static void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] );
|
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
static void CheckMemoryLeak_ClassMembers()
|
|
|
|
{
|
2008-04-04 06:29:28 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
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++;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel < 0 )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
// Only parse this particular class.. not subclasses
|
|
|
|
if ( indentlevel > 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Declaring subclass.. recursive checking
|
|
|
|
if ( Match(tok, "class %var% [{:]") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
classname.push_back( getstr(tok, 1) );
|
|
|
|
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
|
|
|
|
classname.pop_back();
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
// Declaring member variable.. check allocations and deallocations
|
|
|
|
if ( Match(tok->next, "%type% * %var% ;") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( IsName(tok->str) || strchr(";}", tok->str[0]) )
|
|
|
|
CheckMemoryLeak_ClassMembers_Variable( classname, getstr(tok, 3) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] )
|
|
|
|
{
|
|
|
|
// Function pattern.. Check if member function
|
|
|
|
char fpattern[500] = {0};
|
|
|
|
for ( unsigned int i = 0; i < classname.size(); i++ )
|
|
|
|
{
|
|
|
|
strcat( fpattern, classname[i] );
|
|
|
|
strcat( fpattern, " :: " );
|
|
|
|
}
|
|
|
|
strcat( fpattern, "%var% (" );
|
|
|
|
|
|
|
|
// Destructor pattern.. Check if class destructor..
|
|
|
|
char destructor[500] = {0};
|
|
|
|
for ( unsigned int i = 0; i < classname.size(); i++ )
|
|
|
|
{
|
|
|
|
strcat( destructor, classname[i] );
|
|
|
|
strcat( destructor, " :: " );
|
|
|
|
}
|
|
|
|
strcat( destructor, " ~" );
|
|
|
|
strcat( destructor, classname.back() );
|
|
|
|
strcat( destructor, " (" );
|
|
|
|
|
|
|
|
// Pattern used in member function. "Var = ..."
|
|
|
|
std::ostringstream varname_eq;
|
|
|
|
varname_eq << varname << " =";
|
|
|
|
|
|
|
|
// Full variable name..
|
|
|
|
std::ostringstream FullVariableName;
|
|
|
|
for ( unsigned int i = 0; i < classname.size(); i++ )
|
|
|
|
FullVariableName << classname[i] << "::";
|
|
|
|
FullVariableName << varname;
|
|
|
|
|
|
|
|
// Check if member variable has been allocated and deallocated..
|
|
|
|
AllocType Alloc = No;
|
|
|
|
AllocType Dealloc = No;
|
|
|
|
|
|
|
|
// Loop through all tokens. Inspect member functions
|
|
|
|
bool memberfunction = false;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
indentlevel++;
|
|
|
|
|
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
indentlevel--;
|
|
|
|
|
|
|
|
// Set the 'memberfunction' variable..
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
{
|
|
|
|
if ( strchr(";}", tok->str[0]) )
|
|
|
|
memberfunction = false;
|
|
|
|
else if ( Match( tok, fpattern ) || Match( tok, destructor ) )
|
|
|
|
memberfunction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse member function..
|
|
|
|
if ( indentlevel > 0 && memberfunction )
|
|
|
|
{
|
|
|
|
// Allocate..
|
|
|
|
if ( Match( tok, varname_eq.str().c_str() ) )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-05 14:27:02 +02:00
|
|
|
AllocType alloc = GetAllocationType( gettok( tok, 2 ) );
|
|
|
|
if ( alloc != No )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Dealloc != No && Dealloc != alloc )
|
|
|
|
MismatchError( tok, FullVariableName.str().c_str() );
|
|
|
|
if ( Alloc != No && Alloc != alloc )
|
|
|
|
MismatchError( tok, FullVariableName.str().c_str() );
|
|
|
|
Alloc = alloc;
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
// Deallocate..
|
|
|
|
const char *varnames[2] = { "var", 0 };
|
|
|
|
varnames[0] = varname;
|
|
|
|
AllocType dealloc = GetDeallocationType( tok, varnames );
|
|
|
|
if ( dealloc != No )
|
|
|
|
{
|
|
|
|
if ( Dealloc != No && Dealloc != dealloc )
|
|
|
|
MismatchError( tok, FullVariableName.str().c_str() );
|
|
|
|
if ( Alloc != No && Alloc != dealloc )
|
|
|
|
MismatchError( tok, FullVariableName.str().c_str() );
|
|
|
|
Dealloc = dealloc;
|
|
|
|
}
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
if ( Alloc != No && Dealloc == No )
|
|
|
|
{
|
|
|
|
MemoryLeak( tokens, FullVariableName.str().c_str() );
|
|
|
|
}
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
|
|
|
|
|
2007-06-10 20:25:39 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckMemoryLeak()
|
2008-08-07 20:53:35 +02:00
|
|
|
{
|
|
|
|
listallocfunc.clear();
|
2008-05-10 10:33:22 +02:00
|
|
|
|
2007-06-10 20:25:39 +02:00
|
|
|
// Check for memory leaks inside functions..
|
2008-04-02 07:09:35 +02:00
|
|
|
CheckMemoryLeak_InFunction();
|
2007-06-10 20:25:39 +02:00
|
|
|
|
|
|
|
// Check that all class members are deallocated..
|
2008-04-02 07:09:35 +02:00
|
|
|
CheckMemoryLeak_ClassMembers();
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
|