2008-11-10 19:56:02 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CommonCheck.h"
|
|
|
|
|
2008-08-20 09:32:07 +02:00
|
|
|
#include <stdlib.h> // free
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
#include <algorithm>
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
#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
|
|
|
|
2008-09-11 19:03:58 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#define _strdup(str) strdup(str)
|
|
|
|
#endif
|
|
|
|
|
2007-07-20 08:20:31 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-11-12 22:34:47 +01:00
|
|
|
|
|
|
|
CheckMemoryLeakClass::CheckMemoryLeakClass( Tokenizer *tokenizer )
|
|
|
|
{
|
|
|
|
_tokenizer = tokenizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckMemoryLeakClass::~CheckMemoryLeakClass()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2008-08-18 08:40:37 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
bool CheckMemoryLeakClass::isclass( const std::string &typestr )
|
2008-08-18 08:40:37 +02:00
|
|
|
{
|
|
|
|
if ( typestr == "char" ||
|
|
|
|
typestr == "short" ||
|
|
|
|
typestr == "int" ||
|
|
|
|
typestr == "long" ||
|
|
|
|
typestr == "float" ||
|
|
|
|
typestr == "double" )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::ostringstream pattern;
|
2008-11-07 18:24:19 +01:00
|
|
|
pattern << "struct " << typestr;
|
2008-08-18 08:40:37 +02:00
|
|
|
if ( findmatch( tokens, pattern.str().c_str() ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
|
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
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
AllocType CheckMemoryLeakClass::GetAllocationType( const TOKEN *tok2 )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
|
|
|
// 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",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-19 17:20:31 +02:00
|
|
|
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
|
|
|
|
const char *gmallocfunc[] = {"g_new",
|
|
|
|
"g_new0",
|
|
|
|
"g_renew",
|
|
|
|
"g_try_new",
|
|
|
|
"g_try_new0",
|
|
|
|
"g_try_renew",
|
|
|
|
"g_malloc",
|
|
|
|
"g_malloc0",
|
|
|
|
"g_realloc",
|
|
|
|
"g_try_malloc",
|
|
|
|
"g_try_malloc0",
|
|
|
|
"g_try_realloc",
|
|
|
|
"g_strdup",
|
|
|
|
"g_strndup",
|
|
|
|
0};
|
|
|
|
for ( unsigned int i = 0; gmallocfunc[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( strcmp(gmallocfunc[i], tok2->str) == 0 )
|
|
|
|
return gMalloc;
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
2008-08-12 20:24:42 +02:00
|
|
|
// Redundant condition..
|
|
|
|
if ( Match(tok, "if ( %var1% )", varnames) )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok( tok, 4 );
|
2008-08-12 20:24:42 +02:00
|
|
|
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-10-19 17:20:31 +02:00
|
|
|
Match(tok, "kfree ( %var1% ) ;", varnames) )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
|
|
|
return Malloc;
|
|
|
|
}
|
|
|
|
|
2008-10-19 17:20:31 +02:00
|
|
|
if ( Match(tok, "g_free ( %var1% ) ;", varnames) )
|
|
|
|
return gMalloc;
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
return No;
|
|
|
|
}
|
2008-11-09 17:34:18 +01:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static std::list<std::string> callstack;
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, const char *varnames[] )
|
2008-11-09 17:34:18 +01:00
|
|
|
{
|
2008-11-09 18:27:23 +01:00
|
|
|
if (Match(tok,"if") || Match(tok,"for") || Match(tok,"while"))
|
|
|
|
return 0;
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
if (GetAllocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const char *funcname = tok->str;
|
|
|
|
if ( std::find(callstack.begin(), callstack.end(), std::string(funcname)) != callstack.end() )
|
|
|
|
return "use";
|
|
|
|
callstack.push_back(funcname);
|
|
|
|
|
|
|
|
int par = 1;
|
|
|
|
int parlevel = 0;
|
|
|
|
for ( ; tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
if ( Match(tok, "(") )
|
|
|
|
++parlevel;
|
|
|
|
else if ( Match(tok, ")") )
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if ( parlevel < 1 )
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( parlevel == 1 )
|
|
|
|
{
|
|
|
|
if ( Match(tok, ",") )
|
|
|
|
++par;
|
|
|
|
if ( Match(tok, "[,()] %var1% [,()]", varnames) )
|
|
|
|
{
|
|
|
|
const TOKEN *ftok = GetFunctionTokenByName(funcname);
|
|
|
|
const char *parname = GetParameterName( ftok, par );
|
|
|
|
if ( ! parname )
|
|
|
|
return "use";
|
|
|
|
// Check if the function deallocates the variable..
|
|
|
|
while ( ftok && ! Match(ftok,"{") )
|
|
|
|
ftok = ftok->next;
|
|
|
|
TOKEN *func = getcode( Tokenizer::gettok(ftok,1), parname );
|
2008-11-09 18:36:53 +01:00
|
|
|
simplifycode( func );
|
2008-11-09 17:34:18 +01:00
|
|
|
const char *ret = 0;
|
2008-11-10 21:18:03 +01:00
|
|
|
if (findmatch(func, "goto"))
|
|
|
|
ret = 0; // TODO : "goto" isn't handled well
|
|
|
|
else if (findmatch(func, "use"))
|
2008-11-09 17:34:18 +01:00
|
|
|
ret = "use";
|
2008-11-10 21:18:03 +01:00
|
|
|
else if (findmatch(func, "dealloc"))
|
2008-11-09 17:34:18 +01:00
|
|
|
ret = "dealloc";
|
|
|
|
deleteTokens(func);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
2008-04-05 14:27:02 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::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-11-13 23:39:47 +01:00
|
|
|
errmsg << FileLine(Tok1, _tokenizer) << ": 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-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::MemoryLeak( const TOKEN *tok, const char varname[] )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-11-13 23:39:47 +01:00
|
|
|
errmsg << FileLine(tok, _tokenizer) << ": Memory leak: " << varname;
|
2008-04-04 06:29:28 +02:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-09-29 08:38:25 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::instoken(TOKEN *tok, const char str[])
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
|
|
|
TOKEN *newtok = new TOKEN;
|
2008-11-06 19:31:39 +01:00
|
|
|
newtok->setstr(str);
|
|
|
|
newtok->next = tok->next;
|
2008-08-24 10:01:58 +02:00
|
|
|
tok->next = newtok;
|
2008-08-31 09:42:54 +02:00
|
|
|
}
|
2008-08-24 10:01:58 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
bool CheckMemoryLeakClass::notvar(const TOKEN *tok, const char *varnames[])
|
2008-11-07 17:19:55 +01:00
|
|
|
{
|
2008-11-07 18:24:19 +01:00
|
|
|
return bool( Match(tok, "! %var1% [;)&|]", varnames) ||
|
2008-11-09 18:47:00 +01:00
|
|
|
Match(tok, "! ( %var1% )", varnames) ||
|
2008-11-07 17:19:55 +01:00
|
|
|
Match(tok, "unlikely ( ! %var1% )", varnames) ||
|
|
|
|
Match(tok, "unlikely ( %var1% == NULL )", varnames) ||
|
|
|
|
Match(tok, "%var1% == NULL", varnames) ||
|
2008-11-07 18:24:19 +01:00
|
|
|
Match(tok, "NULL == %var1% [;)&|]", varnames) ||
|
2008-11-09 18:47:00 +01:00
|
|
|
Match(tok, "%var1% == 0", varnames) );
|
2008-11-07 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
2008-08-24 10:01:58 +02:00
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
extern bool ShowAll;
|
|
|
|
|
2008-09-29 08:38:25 +02:00
|
|
|
/**
|
|
|
|
* Extract a new tokens list that is easier to parse than the "tokens"
|
|
|
|
* tok - start parse token
|
|
|
|
* varname - name of variable
|
|
|
|
*/
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
TOKEN *CheckMemoryLeakClass::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; \
|
2008-11-06 19:31:39 +01:00
|
|
|
newtok->setstr(_str); \
|
2008-08-16 14:44:46 +02:00
|
|
|
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
|
|
|
|
2008-11-06 20:16:22 +01:00
|
|
|
bool isloop = false;
|
|
|
|
|
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--;
|
2008-11-06 20:16:22 +01:00
|
|
|
isloop &= ( parlevel > 0 );
|
2008-08-16 14:44:46 +02:00
|
|
|
|
|
|
|
if ( parlevel == 0 && tok->str[0]==';')
|
|
|
|
addtoken(";");
|
|
|
|
|
2008-09-29 08:38:25 +02:00
|
|
|
if (Match(tok, "[(;{}] %var1% =", varnames))
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
AllocType alloc = GetAllocationType(Tokenizer::gettok(tok,3));
|
2008-08-18 08:40:37 +02:00
|
|
|
|
|
|
|
// If "--all" hasn't been given, don't check classes..
|
|
|
|
if ( alloc == New && ! ShowAll )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
if ( Match(Tokenizer::gettok(tok,3), "new %type% [(;]") )
|
2008-08-18 08:40:37 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
if ( isclass( Tokenizer::getstr(tok, 4) ) )
|
2008-08-18 08:40:37 +02:00
|
|
|
alloc = No;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
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
|
2008-08-19 19:16:55 +02:00
|
|
|
if ( Match(tok, "if ( %var1% )", varnames) ||
|
|
|
|
Match(tok, "if ( %var1% != NULL )", varnames) )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("if(var)");
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok(tok, 3); // Make sure the "use" will not be added
|
2008-08-16 19:49:40 +02:00
|
|
|
}
|
2008-11-09 08:19:53 +01:00
|
|
|
else if ( Match(tok, "if (") && notvar(Tokenizer::gettok(tok,2), varnames) )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
2008-08-23 15:16:25 +02:00
|
|
|
else if ( Match(tok, "if") ||
|
|
|
|
Match(tok, "else") ||
|
|
|
|
Match(tok, "switch") )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
2008-08-23 15:16:25 +02:00
|
|
|
addtoken(tok->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Match(tok, "case") )
|
|
|
|
{
|
|
|
|
addtoken("case");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Match(tok, "default") )
|
|
|
|
{
|
|
|
|
addtoken("case");
|
|
|
|
addtoken(";");
|
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..
|
2008-11-07 17:19:55 +01:00
|
|
|
if (Match(tok, "for") || Match(tok, "while") )
|
2008-11-06 20:16:22 +01:00
|
|
|
{
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("loop");
|
2008-11-07 17:19:55 +01:00
|
|
|
isloop = true;
|
|
|
|
}
|
|
|
|
if ( Match(tok, "do") )
|
2008-11-09 08:19:53 +01:00
|
|
|
{
|
2008-11-07 17:19:55 +01:00
|
|
|
addtoken("do");
|
2008-11-06 20:16:22 +01:00
|
|
|
}
|
2008-11-07 17:19:55 +01:00
|
|
|
if ( isloop && notvar(tok,varnames) )
|
2008-11-06 20:16:22 +01:00
|
|
|
addtoken("!var");
|
2008-08-16 19:49:40 +02:00
|
|
|
|
|
|
|
// continue / break..
|
|
|
|
if ( Match(tok, "continue") )
|
|
|
|
addtoken("continue");
|
|
|
|
if ( Match(tok, "break") )
|
|
|
|
addtoken("break");
|
2008-09-29 08:38:25 +02:00
|
|
|
|
|
|
|
// goto..
|
|
|
|
if ( Match(tok, "goto") )
|
2008-11-10 19:51:44 +01:00
|
|
|
{
|
2008-11-10 21:18:03 +01:00
|
|
|
addtoken("goto");
|
2008-11-10 19:51:44 +01:00
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
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-11-10 19:51:44 +01:00
|
|
|
// throw..
|
|
|
|
if ( Match(tok, "throw") )
|
|
|
|
addtoken("throw");
|
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
// Assignment..
|
2008-10-15 09:04:32 +02:00
|
|
|
if ( Match(tok,"[)=] %var1% [;)]", varnames) )
|
2008-08-16 14:44:46 +02:00
|
|
|
addtoken("use");
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
// Investigate function calls..
|
|
|
|
if ( Match(tok, "%var% (") )
|
|
|
|
{
|
|
|
|
const char *str = call_func(tok, varnames);
|
|
|
|
if ( str )
|
|
|
|
addtoken( str );
|
|
|
|
}
|
2008-08-16 15:13:36 +02:00
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
// Linux lists..
|
|
|
|
if ( Match( tok, "[=(,] & %var1% [.[]", varnames ) )
|
2008-10-15 09:04:32 +02:00
|
|
|
{
|
|
|
|
// todo: better checking
|
2008-08-16 15:13:36 +02:00
|
|
|
addtoken("use");
|
2008-10-15 09:04:32 +02:00
|
|
|
}
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rethead;
|
|
|
|
}
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::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;
|
2008-08-19 19:16:55 +02:00
|
|
|
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
|
|
|
|
2008-11-09 18:36:53 +01:00
|
|
|
/**
|
|
|
|
* Simplify code
|
|
|
|
* \param tok first token
|
|
|
|
*/
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
|
2008-11-09 18:36:53 +01:00
|
|
|
{
|
2008-11-07 17:19:55 +01:00
|
|
|
// Remove "do"...
|
|
|
|
// do { x } while (y);
|
|
|
|
// =>
|
|
|
|
// { x } while(y) { x }"
|
|
|
|
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( ! Match(tok2->next, "do") )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove the next token "do"
|
2008-11-09 08:19:53 +01:00
|
|
|
erase( tok2, Tokenizer::gettok(tok2, 2) );
|
2008-11-07 17:19:55 +01:00
|
|
|
tok2 = tok2->next;
|
|
|
|
|
|
|
|
// Find the end of the "do" block..
|
|
|
|
TOKEN *tok2_;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for ( tok2_ = tok2; tok2_ && indentlevel>=0; tok2_ = tok2_->next )
|
|
|
|
{
|
|
|
|
if ( Match(tok2_, "{") )
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if ( Match(tok2_, "}") )
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
else if ( indentlevel == 0 && Match(tok2_->next, ";") )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End not found?
|
|
|
|
if ( ! tok2_ )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Copy code..
|
|
|
|
indentlevel = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ( Match( tok2, "{" ) )
|
|
|
|
++indentlevel;
|
|
|
|
else if ( Match(tok2, "}") )
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
// Copy token..
|
|
|
|
instoken( tok2_, tok2->str );
|
|
|
|
|
|
|
|
// Next token..
|
|
|
|
tok2 = tok2->next;
|
|
|
|
tok2_ = tok2_->next;
|
|
|
|
}
|
|
|
|
while ( tok2 && indentlevel > 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
// 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;
|
|
|
|
|
2008-11-06 19:31:39 +01:00
|
|
|
for (TOKEN *tok2 = tok; tok2; tok2 = tok2 ? tok2->next : NULL )
|
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-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,2));
|
2008-08-16 18:47:54 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-20 08:51:26 +02:00
|
|
|
// Replace "{ }" with ";"
|
2008-08-16 21:19:56 +02:00
|
|
|
if ( Match(tok2->next, "{ }") )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->next->setstr(";");
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2->next, Tokenizer::gettok(tok2,3));
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-08-16 21:19:56 +02:00
|
|
|
// Delete braces around a single instruction..
|
|
|
|
if ( Match(tok2->next, "{ %var% ; }") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase( tok2, Tokenizer::gettok(tok2,2) );
|
|
|
|
erase( tok2->next->next, Tokenizer::gettok(tok2,4) );
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-19 06:53:41 +02:00
|
|
|
if ( Match(tok2->next, "{ return use ; }") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase( tok2, Tokenizer::gettok(tok2,2) );
|
|
|
|
erase( tok2->next->next->next, Tokenizer::gettok(tok2,5) );
|
2008-08-19 06:53:41 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-08-25 20:01:11 +02:00
|
|
|
// Delete empty if that is not followed by an else
|
2008-08-16 21:19:56 +02:00
|
|
|
if ( Match(tok2,"[;{}] if ;") ||
|
|
|
|
Match(tok2,"[;{}] if(var) ;") ||
|
|
|
|
Match(tok2,"[;{}] if(!var) ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
if ( ! Match(Tokenizer::gettok(tok2,3), "else") )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2, 3));
|
2008-08-16 19:49:40 +02:00
|
|
|
done = false;
|
2008-08-16 21:19:56 +02:00
|
|
|
continue;
|
2008-08-16 19:49:40 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-09 11:09:42 +01:00
|
|
|
|
|
|
|
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
|
|
|
|
if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
|
|
|
|
!Match(Tokenizer::gettok(tok2,4), "else"))
|
|
|
|
{
|
|
|
|
erase(tok2->next, Tokenizer::gettok(tok2,3));
|
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-08-19 19:16:55 +02:00
|
|
|
// Delete if block: "alloc; if return use ;"
|
2008-11-09 08:19:53 +01:00
|
|
|
if (Match(tok2,"alloc ; if return use ;") && !Match(Tokenizer::gettok(tok2,6),"else"))
|
2008-08-19 19:16:55 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,5));
|
2008-08-19 19:16:55 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-21 20:55:04 +02:00
|
|
|
|
2008-08-19 19:16:55 +02:00
|
|
|
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
|
|
|
|
if (Match(tok2,"[;{}] if alloc ; else return ;"))
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,2)); // Remove "if"
|
|
|
|
erase(tok2->next, Tokenizer::gettok(tok2,5)); // Remove "; else return"
|
2008-08-19 19:16:55 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-08-21 20:55:04 +02:00
|
|
|
// Replace "dealloc use ;" with "dealloc ;"
|
|
|
|
if ( Match(tok2, "dealloc use ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,2));
|
2008-08-21 20:55:04 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-25 20:01:11 +02:00
|
|
|
// Reducing if..
|
|
|
|
if (Match(tok2,"if dealloc ; else") || Match(tok2,"if use ; else"))
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2, 2));
|
2008-08-25 20:01:11 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-11-09 08:19:53 +01:00
|
|
|
if (Match(tok2,"[;{}] if { dealloc ; return ; }") && !Match(Tokenizer::gettok(tok2,8),"else"))
|
2008-08-25 20:01:11 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2,Tokenizer::gettok(tok2,8));
|
2008-08-25 20:01:11 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:19:19 +01:00
|
|
|
// Remove "if dealloc ;" if there is no else after it..
|
2008-11-09 08:19:53 +01:00
|
|
|
if (Match(tok2,"if dealloc ;") && !Match(Tokenizer::gettok(tok2,3),"else"))
|
2008-11-08 08:19:19 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase( tok2, Tokenizer::gettok(tok2, 2) );
|
2008-11-08 08:19:19 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-10-19 09:23:06 +02:00
|
|
|
// Replace "loop ;" with ";"
|
2008-08-16 21:19:56 +02:00
|
|
|
if ( Match(tok2->next, "loop ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,2));
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:46:28 +01:00
|
|
|
// Replace "loop !var ;" with ";"
|
|
|
|
if ( Match(tok2->next, "loop !var ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,4));
|
2008-11-07 08:46:28 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop !var alloc ;" with " alloc ;"
|
|
|
|
if ( Match(tok2->next, "loop !var alloc ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,3));
|
2008-11-07 08:46:28 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-17 19:17:17 +02:00
|
|
|
// Delete if block in "alloc ; if(!var) return ;"
|
|
|
|
if ( Match(tok2, "alloc ; if(!var) return ;") )
|
2008-08-16 21:19:56 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,4));
|
2008-08-17 19:17:17 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-23 15:16:25 +02:00
|
|
|
// Delete second use in "use ; use ;"
|
2008-11-09 11:09:42 +01:00
|
|
|
while (Match(tok2, "[;{}] use ; use ;"))
|
2008-08-17 19:17:17 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,3));
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
2008-08-16 18:47:54 +02:00
|
|
|
}
|
2008-08-23 15:16:25 +02:00
|
|
|
|
|
|
|
// Delete second case in "case ; case ;"
|
|
|
|
while (Match(tok2, "case ; case ;"))
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
erase(tok2, Tokenizer::gettok(tok2,3));
|
2008-08-23 15:16:25 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-09-29 08:38:25 +02:00
|
|
|
|
2008-08-24 10:01:58 +02:00
|
|
|
// Replace switch with if (if not complicated)
|
|
|
|
if (Match(tok2, "switch {"))
|
|
|
|
{
|
|
|
|
// Right now, I just handle if there are a few case and perhaps a default.
|
2008-08-31 09:42:54 +02:00
|
|
|
bool valid = false;
|
2008-08-24 10:01:58 +02:00
|
|
|
bool incase = false;
|
2008-11-09 08:19:53 +01:00
|
|
|
for ( const TOKEN * _tok = Tokenizer::gettok(tok2,2); _tok; _tok = _tok->next )
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
|
|
|
if ( _tok->str[0] == '{' )
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
2008-09-29 08:38:25 +02:00
|
|
|
else if ( _tok->str[0] == '}' )
|
2008-08-31 09:42:54 +02:00
|
|
|
{
|
2008-09-29 08:38:25 +02:00
|
|
|
valid = true;
|
|
|
|
break;
|
2008-08-31 09:42:54 +02:00
|
|
|
}
|
2008-08-24 10:01:58 +02:00
|
|
|
|
|
|
|
else if (strncmp(_tok->str,"if",2)==0)
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-09-29 08:38:25 +02:00
|
|
|
|
2008-08-31 09:42:54 +02:00
|
|
|
else if (strcmp(_tok->str,"switch")==0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (strcmp(_tok->str,"loop")==0)
|
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
|
|
|
else if (incase && Match(_tok,"case"))
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
|
|
|
incase |= Match(_tok,"case");
|
|
|
|
incase &= !Match(_tok,"break");
|
|
|
|
}
|
|
|
|
|
2008-09-02 09:54:10 +02:00
|
|
|
if ( !incase && valid )
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
|
|
|
done = false;
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(";");
|
2008-11-09 08:19:53 +01:00
|
|
|
erase( tok2, Tokenizer::gettok(tok2, 2) );
|
2008-08-24 10:01:58 +02:00
|
|
|
tok2 = tok2->next;
|
|
|
|
bool first = true;
|
|
|
|
while (Match(tok2,"case") || Match(tok2,"default"))
|
|
|
|
{
|
|
|
|
bool def = Match(tok2, "default");
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(first ? "if" : "}");
|
2008-08-24 10:01:58 +02:00
|
|
|
if ( first )
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
instoken( tok2, "{" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Insert "else [if] {
|
|
|
|
instoken( tok2, "{" );
|
|
|
|
if ( ! def )
|
|
|
|
instoken( tok2, "if" );
|
|
|
|
instoken( tok2, "else" );
|
|
|
|
}
|
2008-09-02 09:54:10 +02:00
|
|
|
while ( tok2 && tok2->str[0] != '}' && ! Match(tok2,"break ;") )
|
2008-09-29 08:38:25 +02:00
|
|
|
tok2 = tok2->next;
|
|
|
|
if (Match(tok2,"break ;"))
|
2008-09-02 09:54:10 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(";");
|
2008-09-29 08:38:25 +02:00
|
|
|
tok2 = tok2->next->next;
|
2008-09-02 09:54:10 +02:00
|
|
|
}
|
2008-08-24 10:01:58 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-29 08:38:25 +02:00
|
|
|
}
|
2008-11-10 19:51:44 +01:00
|
|
|
|
|
|
|
if ( Match(tok2, "throw") )
|
|
|
|
{
|
|
|
|
tok2->setstr( "return" );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
2008-08-19 19:16:55 +02:00
|
|
|
}
|
2008-11-09 18:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
|
2008-11-09 18:36:53 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-08-19 19:16:55 +02:00
|
|
|
|
2008-11-10 21:18:03 +01:00
|
|
|
// TODO : handle "goto"
|
|
|
|
if (findmatch(tok, "goto"))
|
|
|
|
{
|
|
|
|
deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-09 18:36:53 +01:00
|
|
|
simplifycode( tok );
|
2008-08-15 21:17:06 +02:00
|
|
|
|
2008-08-27 08:33:27 +02:00
|
|
|
if ( findmatch(tok, "loop alloc ;") )
|
|
|
|
{
|
|
|
|
MemoryLeak(findmatch(tok, "loop alloc ;"), varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( findmatch(tok, "alloc ; if continue ;") )
|
2008-08-17 19:17:17 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if continue ;"), 3), varname);
|
2008-08-17 20:01:37 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 20:46:08 +01:00
|
|
|
else if ( findmatch(tok, "alloc ; if break ;") )
|
|
|
|
{
|
|
|
|
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if break ;"), 3), varname);
|
|
|
|
}
|
|
|
|
|
2008-08-17 20:01:37 +02:00
|
|
|
else if ( findmatch(tok, "alloc ; if return ;") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if return ;"), 3), varname);
|
2008-08-17 19:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ( findmatch(tok, "alloc ; return ;") )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
MemoryLeak(Tokenizer::gettok(findmatch(tok,"alloc ; return ;"),2), varname);
|
2008-08-16 18:47:54 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 17:19:55 +01:00
|
|
|
else if ( findmatch(tok, "alloc ; alloc") )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
MemoryLeak(Tokenizer::gettok(findmatch(tok,"alloc ; alloc"),2), varname);
|
2008-11-07 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
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-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::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-11-09 08:19:53 +01:00
|
|
|
CheckMemoryLeak_CheckScope( tok->next, Tokenizer::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% [;=]") )
|
2008-11-09 08:19:53 +01:00
|
|
|
CheckMemoryLeak_CheckScope( tok->next, Tokenizer::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-05 14:27:02 +02:00
|
|
|
|
2008-04-04 06:29:28 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers()
|
2008-04-02 07:09:35 +02:00
|
|
|
{
|
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;
|
2008-11-09 08:19:53 +01:00
|
|
|
classname.push_back( Tokenizer::getstr(tok, 1) );
|
2008-04-04 06:29:28 +02:00
|
|
|
CheckMemoryLeak_ClassMembers_ParseClass( tok, classname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
classname.push_back( Tokenizer::getstr(tok, 1) );
|
2008-04-04 06:29:28 +02:00
|
|
|
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]) )
|
2008-08-18 08:40:37 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
if (ShowAll || !isclass(Tokenizer::getstr(tok,1)))
|
|
|
|
CheckMemoryLeak_ClassMembers_Variable( classname, Tokenizer::getstr(tok, 3) );
|
2008-08-18 08:40:37 +02:00
|
|
|
}
|
2008-04-05 14:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
|
|
|
// Function pattern.. Check if member function
|
2008-09-11 19:03:58 +02:00
|
|
|
std::ostringstream fpattern;
|
2008-04-05 14:27:02 +02:00
|
|
|
for ( unsigned int i = 0; i < classname.size(); i++ )
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
fpattern << classname[i] << " :: ";
|
2008-04-05 14:27:02 +02:00
|
|
|
}
|
2008-09-11 19:03:58 +02:00
|
|
|
fpattern << "%var% (";
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
// Destructor pattern.. Check if class destructor..
|
2008-09-11 19:03:58 +02:00
|
|
|
std::ostringstream destructor;
|
2008-04-05 14:27:02 +02:00
|
|
|
for ( unsigned int i = 0; i < classname.size(); i++ )
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
destructor << classname[i] << " :: ";
|
2008-04-05 14:27:02 +02:00
|
|
|
}
|
2008-09-11 19:03:58 +02:00
|
|
|
destructor << " ~" << classname.back() << " (";
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
// 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;
|
2008-09-11 19:03:58 +02:00
|
|
|
else if ( Match( tok, fpattern.str().c_str() ) || Match( tok, destructor.str().c_str() ) )
|
2008-04-05 14:27:02 +02:00
|
|
|
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-11-09 08:19:53 +01:00
|
|
|
AllocType alloc = GetAllocationType( Tokenizer::gettok( tok, 2 ) );
|
2008-04-05 14:27:02 +02:00
|
|
|
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..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::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
|
|
|
|