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-08-20 09:32:07 +02:00
|
|
|
#include <stdlib.h> // free
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
#include <algorithm>
|
2008-11-26 19:13:36 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
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-16 06:57:41 +01:00
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
CheckMemoryLeakClass::CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
|
|
|
|
: _settings(settings)
|
2008-11-16 06:57:41 +01:00
|
|
|
{
|
2008-11-22 21:00:36 +01:00
|
|
|
_tokenizer = tokenizer;
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger = errorLogger;
|
2008-11-16 06:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckMemoryLeakClass::~CheckMemoryLeakClass()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2008-08-18 08:40:37 +02:00
|
|
|
|
2008-11-26 08:23:54 +01:00
|
|
|
bool CheckMemoryLeakClass::isclass( const TOKEN *tok )
|
|
|
|
{
|
|
|
|
if ( tok->isStandardType() )
|
2008-08-18 08:40:37 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
std::ostringstream pattern;
|
2008-11-26 08:23:54 +01:00
|
|
|
pattern << "struct " << tok->str();
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::findmatch( _tokenizer->tokens(), pattern.str().c_str() ) )
|
2008-08-18 08:40:37 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-21 19:17:40 +01:00
|
|
|
CheckMemoryLeakClass::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");
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( tok2 && tok2->str() == "(" )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
while ( tok2 && tok2->str() != ")" )
|
2008-04-05 14:27:02 +02:00
|
|
|
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",
|
|
|
|
"strdup",
|
|
|
|
"kmalloc",
|
|
|
|
"kzalloc",
|
2008-11-22 22:10:12 +01:00
|
|
|
"kcalloc",
|
2008-04-06 12:36:41 +02:00
|
|
|
0};
|
2008-04-05 14:27:02 +02:00
|
|
|
for ( unsigned int i = 0; mallocfunc[i]; i++ )
|
|
|
|
{
|
2008-12-05 19:39:23 +01:00
|
|
|
if ( tok2->str() == mallocfunc[i] )
|
2008-04-05 14:27:02 +02:00
|
|
|
return Malloc;
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
2008-04-05 14:27:02 +02:00
|
|
|
|
2008-12-06 12:41:08 +01:00
|
|
|
// Does tok2 point on "g_malloc", "g_strdup", ..
|
2008-10-19 17:20:31 +02:00
|
|
|
const char *gmallocfunc[] = {"g_new",
|
|
|
|
"g_new0",
|
|
|
|
"g_try_new",
|
|
|
|
"g_try_new0",
|
|
|
|
"g_malloc",
|
|
|
|
"g_malloc0",
|
|
|
|
"g_try_malloc",
|
|
|
|
"g_try_malloc0",
|
|
|
|
"g_strdup",
|
|
|
|
"g_strndup",
|
|
|
|
0};
|
|
|
|
for ( unsigned int i = 0; gmallocfunc[i]; i++ )
|
|
|
|
{
|
2008-12-05 19:39:23 +01:00
|
|
|
if ( tok2->str() == gmallocfunc[i] )
|
2008-10-19 17:20:31 +02:00
|
|
|
return gMalloc;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok2, "new %type% [;(]" ) )
|
2008-04-05 14:27:02 +02:00
|
|
|
return New;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok2, "new %type% [" ) )
|
2008-08-07 20:53:35 +02:00
|
|
|
return NewA;
|
2008-11-22 21:00:36 +01:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok2, "fopen (" ) )
|
2008-11-22 21:00:36 +01:00
|
|
|
return FOPEN;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok2, "popen (" ) )
|
2008-11-22 21:00:36 +01:00
|
|
|
return POPEN;
|
2008-08-07 20:53:35 +02:00
|
|
|
|
|
|
|
// Userdefined allocation function..
|
2008-11-23 12:08:07 +01:00
|
|
|
std::list<AllocFunc>::const_iterator it = _listAllocFunc.begin();
|
|
|
|
while ( it != _listAllocFunc.end() )
|
2008-08-07 20:53:35 +02:00
|
|
|
{
|
2008-12-05 19:39:23 +01:00
|
|
|
if ( tok2->str() == it->funcname )
|
2008-08-07 20:53:35 +02:00
|
|
|
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-21 19:17:40 +01:00
|
|
|
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "delete %var1% ;", varnames) )
|
2008-04-05 14:27:02 +02:00
|
|
|
return New;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "delete [ ] %var1% ;", varnames) )
|
2008-04-05 14:27:02 +02:00
|
|
|
return NewA;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "free ( %var1% ) ;", varnames) ||
|
|
|
|
TOKEN::Match(tok, "kfree ( %var1% ) ;", varnames) )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
|
|
|
return Malloc;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "g_free ( %var1% ) ;", varnames) )
|
2008-10-19 17:20:31 +02:00
|
|
|
return gMalloc;
|
2008-11-22 21:00:36 +01:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "fclose ( %var1% )", varnames) )
|
2008-11-22 21:00:36 +01:00
|
|
|
return FOPEN;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "pclose ( %var1% )", varnames) )
|
2008-11-22 21:00:36 +01:00
|
|
|
return POPEN;
|
2008-10-19 17:20:31 +02:00
|
|
|
|
2008-04-05 14:27:02 +02:00
|
|
|
return No;
|
|
|
|
}
|
2008-11-09 17:34:18 +01:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2008-11-16 14:55:16 +01:00
|
|
|
const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype )
|
2008-11-09 17:34:18 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok,"if") || TOKEN::Match(tok,"for") || TOKEN::Match(tok,"while"))
|
2008-11-09 18:27:23 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
if (GetAllocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No)
|
|
|
|
return 0;
|
2008-11-22 21:00:36 +01:00
|
|
|
|
|
|
|
if ( callstack.size() > 2 )
|
|
|
|
return "dealloc";
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
const char *funcname = tok->aaaa();
|
2008-11-22 21:00:36 +01:00
|
|
|
for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it )
|
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( (*it)->str() == funcname )
|
2008-11-22 21:00:36 +01:00
|
|
|
return "dealloc";
|
2008-11-17 21:29:50 +01:00
|
|
|
}
|
2008-11-16 14:55:16 +01:00
|
|
|
callstack.push_back(tok);
|
2008-11-09 17:34:18 +01:00
|
|
|
|
|
|
|
int par = 1;
|
|
|
|
int parlevel = 0;
|
|
|
|
for ( ; tok; tok = tok->next )
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "(" )
|
2008-11-09 17:34:18 +01:00
|
|
|
++parlevel;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->str() == ")" )
|
2008-11-09 17:34:18 +01:00
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if ( parlevel < 1 )
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( parlevel == 1 )
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "," )
|
2008-11-09 17:34:18 +01:00
|
|
|
++par;
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "[,()] %var1% [,()]", varnames) )
|
2008-11-09 17:34:18 +01:00
|
|
|
{
|
2008-11-15 23:41:56 +01:00
|
|
|
const TOKEN *ftok = _tokenizer->GetFunctionTokenByName(funcname);
|
2008-11-20 21:54:52 +01:00
|
|
|
const char *parname = Tokenizer::getParameterName( ftok, par );
|
2008-11-09 17:34:18 +01:00
|
|
|
if ( ! parname )
|
|
|
|
return "use";
|
|
|
|
// Check if the function deallocates the variable..
|
2008-11-24 20:38:08 +01:00
|
|
|
while ( ftok && (ftok->str() != "{") )
|
2008-11-09 17:34:18 +01:00
|
|
|
ftok = ftok->next;
|
2008-11-23 11:09:16 +01:00
|
|
|
TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype );
|
2008-11-26 19:13:36 +01:00
|
|
|
simplifycode( func );
|
|
|
|
const TOKEN *func_ = func;
|
|
|
|
while ( func_ && func_->str() == ";" )
|
|
|
|
func_ = func_->next;
|
2008-11-27 19:57:07 +01:00
|
|
|
/*
|
2008-11-27 08:27:50 +01:00
|
|
|
for (const TOKEN *t = func; t; t = t->next)
|
2008-11-26 19:13:36 +01:00
|
|
|
{
|
|
|
|
std::cout << t->str() << "\n";
|
2008-11-27 19:57:07 +01:00
|
|
|
}*/
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
const char *ret = 0;
|
2008-11-26 19:13:36 +01:00
|
|
|
if (TOKEN::findmatch(func_, "goto"))
|
|
|
|
{
|
|
|
|
// TODO : "goto" isn't handled well
|
|
|
|
if ( TOKEN::findmatch(func_, "dealloc") )
|
|
|
|
ret = "dealloc";
|
|
|
|
else if ( TOKEN::findmatch(func_, "use") )
|
|
|
|
ret = "use";
|
2008-11-27 08:27:50 +01:00
|
|
|
}
|
|
|
|
else if ( TOKEN::findmatch(func_, "dealloc") )
|
|
|
|
ret = "dealloc";
|
|
|
|
else if ( TOKEN::findmatch(func_, "use") )
|
|
|
|
ret = "use";
|
|
|
|
|
2008-11-20 21:54:52 +01:00
|
|
|
Tokenizer::deleteTokens(func);
|
2008-11-09 17:34:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
2008-04-05 14:27:02 +02:00
|
|
|
|
2008-11-16 14:55:16 +01:00
|
|
|
void CheckMemoryLeakClass::MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] )
|
2007-05-24 15:08:51 +02:00
|
|
|
{
|
2008-11-22 21:00:36 +01:00
|
|
|
std::ostringstream errmsg;
|
|
|
|
for ( std::list<const TOKEN *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok )
|
2008-11-20 21:54:52 +01:00
|
|
|
errmsg << _tokenizer->fileLine(*tok) << " -> ";
|
|
|
|
errmsg << _tokenizer->fileLine(Tok1) << ": Mismatching allocation and deallocation: " << varname;
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
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-11-22 21:00:36 +01:00
|
|
|
{
|
2008-04-04 06:29:28 +02:00
|
|
|
std::ostringstream errmsg;
|
2008-11-20 21:54:52 +01:00
|
|
|
errmsg << _tokenizer->fileLine(tok) << ": Memory leak: " << varname;
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr( errmsg.str() );
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
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-22 23:49:14 +01:00
|
|
|
return bool( TOKEN::Match(tok, "! %var1% [;)&|]", varnames) ||
|
|
|
|
TOKEN::Match(tok, "! ( %var1% )", varnames) ||
|
|
|
|
TOKEN::Match(tok, "unlikely ( ! %var1% )", varnames) ||
|
|
|
|
TOKEN::Match(tok, "unlikely ( %var1% == 0 )", varnames) ||
|
|
|
|
TOKEN::Match(tok, "0 == %var1% [;)&|]", varnames) ||
|
|
|
|
TOKEN::Match(tok, "%var1% == 0", varnames) );
|
2008-11-07 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
2008-11-16 14:55:16 +01:00
|
|
|
TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype)
|
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-11-27 08:27:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
// The first token should be ";"
|
|
|
|
addtoken(";");
|
|
|
|
|
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-12-05 20:37:11 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
|
|
|
addtoken( "{" );
|
2008-08-15 21:17:06 +02:00
|
|
|
indentlevel++;
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
2008-12-05 20:37:11 +01:00
|
|
|
else if ( tok->str() == "}" )
|
2008-08-15 21:17:06 +02:00
|
|
|
{
|
2008-12-01 19:52:40 +01:00
|
|
|
addtoken( "}" );
|
2008-08-16 14:44:46 +02:00
|
|
|
if ( indentlevel <= 0 )
|
|
|
|
break;
|
2008-08-15 21:17:06 +02:00
|
|
|
indentlevel--;
|
2008-08-16 14:44:46 +02:00
|
|
|
}
|
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
if ( tok->str() == "(" )
|
2008-08-16 14:44:46 +02:00
|
|
|
parlevel++;
|
2008-12-01 08:50:36 +01:00
|
|
|
else if ( tok->str() == ")" )
|
2008-08-16 14:44:46 +02:00
|
|
|
parlevel--;
|
2008-11-06 20:16:22 +01:00
|
|
|
isloop &= ( parlevel > 0 );
|
2008-08-16 14:44:46 +02:00
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
if ( parlevel == 0 && tok->str()==";")
|
2008-08-16 14:44:46 +02:00
|
|
|
addtoken(";");
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "[(;{}] %var1% =", varnames))
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
AllocType alloc = GetAllocationType(tok->tokAt(3));
|
2008-08-18 08:40:37 +02:00
|
|
|
|
|
|
|
// If "--all" hasn't been given, don't check classes..
|
2008-11-16 16:18:50 +01:00
|
|
|
if ( alloc == New && ! _settings._showAll )
|
2008-08-18 08:40:37 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
if ( TOKEN::Match(tok->tokAt(3), "new %type% [(;]") )
|
2008-11-22 23:49:14 +01:00
|
|
|
{
|
2008-11-26 08:23:54 +01:00
|
|
|
if ( isclass( tok->tokAt(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)
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError(tok, callstack, varname);
|
2008-08-16 14:44:46 +02:00
|
|
|
if (dealloctype!=No && dealloctype!=alloc)
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError(tok, callstack, varname);
|
2008-08-16 14:44:46 +02:00
|
|
|
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)
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError(tok, callstack, varname);
|
2008-08-16 14:44:46 +02:00
|
|
|
if (dealloctype!=No && dealloctype!=dealloc)
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError(tok, callstack, varname);
|
2008-08-16 14:44:46 +02:00
|
|
|
dealloctype = dealloc;
|
2008-08-15 21:17:06 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
// if else switch
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "if ( %var1% )", varnames) ||
|
|
|
|
TOKEN::Match(tok, "if ( %var1% != 0 )", varnames) ||
|
|
|
|
TOKEN::Match(tok, "if ( 0 != %var1% )", varnames) )
|
2008-08-16 14:44:46 +02:00
|
|
|
{
|
2008-12-01 08:50:36 +01:00
|
|
|
addtoken("if(var)");
|
2008-12-01 23:33:21 +01:00
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
// Make sure the "use" will not be added
|
|
|
|
while ( tok->str() != ")" )
|
|
|
|
tok = tok->next;
|
2008-08-16 19:49:40 +02:00
|
|
|
}
|
2008-11-23 11:09:16 +01:00
|
|
|
else if ( TOKEN::Match(tok, "if (") && notvar(tok->tokAt(2), varnames) )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok, "if ( true )") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
|
|
|
addtoken("if(true)");
|
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok, "if ( false )") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
|
|
|
addtoken("if(false)");
|
|
|
|
}
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok, "if") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
|
|
|
// Check if the condition depends on var somehow..
|
|
|
|
bool dep = false;
|
|
|
|
int parlevel = 0;
|
|
|
|
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2->str() == "(" )
|
2008-11-22 21:00:36 +01:00
|
|
|
++parlevel;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2->str() == ")" )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if ( parlevel <= 0 )
|
|
|
|
break;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok2->str() != ".") &&
|
2008-11-22 23:49:14 +01:00
|
|
|
TOKEN::Match(tok2->next, "%var1%", varnames) &&
|
|
|
|
!TOKEN::Match(tok2->next, "%var1% .", varnames) )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
|
|
|
dep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addtoken( (dep ? "ifv" : "if") );
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( (tok->str() == "else") || (tok->str() == "switch") )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
addtoken(tok->aaaa());
|
2008-08-23 15:16:25 +02:00
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok->str() == "case") )
|
2008-08-23 15:16:25 +02:00
|
|
|
{
|
|
|
|
addtoken("case");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok->str() == "default") )
|
2008-08-23 15:16:25 +02:00
|
|
|
{
|
|
|
|
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-24 20:38:08 +01:00
|
|
|
if ((tok->str() == "for") || (tok->str() == "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;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( (tok->str() == "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..
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "continue" )
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("continue");
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "break" )
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("break");
|
2008-09-29 08:38:25 +02:00
|
|
|
|
|
|
|
// goto..
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "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-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "return" )
|
2008-08-16 19:49:40 +02:00
|
|
|
{
|
|
|
|
addtoken("return");
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "return %var1%", varnames) ||
|
|
|
|
TOKEN::Match(tok, "return & %var1%", varnames) )
|
2008-08-16 19:49:40 +02:00
|
|
|
addtoken("use");
|
|
|
|
}
|
2008-08-16 18:47:54 +02:00
|
|
|
|
2008-11-10 19:51:44 +01:00
|
|
|
// throw..
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->str() == "throw" )
|
2008-11-10 19:51:44 +01:00
|
|
|
addtoken("throw");
|
|
|
|
|
2008-08-15 21:17:06 +02:00
|
|
|
// Assignment..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::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..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "%var% (") )
|
2008-11-09 17:34:18 +01:00
|
|
|
{
|
2008-11-16 14:55:16 +01:00
|
|
|
const char *str = call_func(tok, callstack, varnames, alloctype, dealloctype);
|
2008-11-09 17:34:18 +01:00
|
|
|
if ( str )
|
|
|
|
addtoken( str );
|
2008-11-29 18:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Callback..
|
|
|
|
if ( TOKEN::Match(tok, "( * %var% ) (") )
|
|
|
|
{
|
|
|
|
for ( const TOKEN *tok2 = tok->tokAt(5); tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok2, ";{") )
|
|
|
|
break;
|
|
|
|
else if ( tok2->str() == varname )
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-09 17:34:18 +01:00
|
|
|
}
|
2008-08-16 15:13:36 +02:00
|
|
|
|
2008-08-16 18:47:54 +02:00
|
|
|
// Linux lists..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) )
|
2008-11-16 06:57:41 +01:00
|
|
|
{
|
2008-11-16 07:34:22 +01:00
|
|
|
// todo: better checking
|
|
|
|
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-11-11 07:42:09 +01:00
|
|
|
void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
|
2008-11-09 18:36:53 +01:00
|
|
|
{
|
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-11-22 23:49:14 +01:00
|
|
|
while (TOKEN::Match(tok2,"[;{}] ;"))
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(2));
|
2008-08-16 18:47:54 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-20 08:51:26 +02:00
|
|
|
// Replace "{ }" with ";"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "{ }") )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->next->setstr(";");
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2->next, tok2->tokAt(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..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "{ %var% ; }") )
|
2008-08-16 21:19:56 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
erase( tok2->next->next, tok2->tokAt(4) );
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-12-01 19:52:40 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "{ %var% %var% ; }") )
|
2008-08-19 06:53:41 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
erase( tok2->next->next->next, tok2->tokAt(5) );
|
2008-08-19 06:53:41 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-11-27 08:27:50 +01:00
|
|
|
// Delete empty if that is not followed by an else
|
2008-11-27 19:57:07 +01:00
|
|
|
if (tok2->tokAt(2) &&
|
2008-11-27 08:27:50 +01:00
|
|
|
(tok2->next->str().find("if") == 0) &&
|
2008-11-27 19:57:07 +01:00
|
|
|
TOKEN::Match(tok2->tokAt(2), ";") &&
|
|
|
|
!TOKEN::Match(tok2->tokAt(3), "else"))
|
2008-11-27 08:27:50 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
2008-08-16 19:49:40 +02:00
|
|
|
}
|
2008-11-26 19:13:36 +01:00
|
|
|
|
|
|
|
// Delete "if ; else ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "if ; else ;") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(4) );
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Make this more generic. Delete "if ; else use ; use"
|
|
|
|
if ( TOKEN::Match(tok2, "; if ; else use ; use") ||
|
|
|
|
TOKEN::Match(tok2, "; if use ; else ; use") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(4) );
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
// Reduce "if dealloc ;" and "if use ;" that is not followed by an else..
|
|
|
|
// If "--all" has been given these are deleted
|
|
|
|
// Otherwise, ony the "if" will be deleted
|
|
|
|
if ((TOKEN::Match(tok2, "[;{}] if dealloc ;") || TOKEN::Match(tok2, "[;{}] if use ;")) &&
|
2008-11-23 11:09:16 +01:00
|
|
|
!TOKEN::Match(tok2->tokAt(4), "else"))
|
2008-12-01 08:50:36 +01:00
|
|
|
{
|
|
|
|
if ( _settings._showAll )
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
else
|
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
done = false;
|
2008-11-09 11:09:42 +01:00
|
|
|
}
|
2008-08-16 19:49:40 +02:00
|
|
|
|
2008-12-01 19:52:40 +01:00
|
|
|
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
|
|
|
|
if ((TOKEN::Match(tok2, "[;{}] if(var) dealloc ;") || TOKEN::Match(tok2, "[;{}] if(var) use ;")) &&
|
|
|
|
!TOKEN::Match(tok2->tokAt(4), "else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if if" => "if"
|
|
|
|
if ( TOKEN::Match(tok2, "if if") )
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "else ;" => ";"
|
|
|
|
if ( TOKEN::Match(tok2->next, "else ;") )
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
2008-12-01 23:33:21 +01:00
|
|
|
}
|
2008-12-01 19:52:40 +01:00
|
|
|
|
2008-08-19 19:16:55 +02:00
|
|
|
// Delete if block: "alloc; if return use ;"
|
2008-11-23 11:09:16 +01:00
|
|
|
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
|
2008-08-19 19:16:55 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(5));
|
2008-08-19 19:16:55 +02:00
|
|
|
done = false;
|
2008-12-01 23:33:21 +01:00
|
|
|
}
|
2008-12-01 19:52:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Reduce "if return ; alloc ;" => "alloc ;"
|
|
|
|
if (TOKEN::Match(tok2, "[;{}] if return ; alloc ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
2008-08-21 20:55:04 +02:00
|
|
|
|
2008-08-19 19:16:55 +02:00
|
|
|
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2,"[;{}] if alloc ; else return ;"))
|
2008-08-19 19:16:55 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(2)); // Remove "if"
|
|
|
|
erase(tok2->next, tok2->tokAt(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 ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, "dealloc use ;") )
|
2008-08-21 20:55:04 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(2));
|
2008-08-21 20:55:04 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-25 20:01:11 +02:00
|
|
|
// Reducing if..
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2,"if dealloc ; else") || TOKEN::Match(tok2,"if use ; else"))
|
2008-08-25 20:01:11 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(2));
|
2008-08-25 20:01:11 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2008-11-23 11:09:16 +01:00
|
|
|
if (TOKEN::Match(tok2,"[;{}] if { dealloc ; return ; }") && !TOKEN::Match(tok2->tokAt(8),"else"))
|
2008-08-25 20:01:11 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2,tok2->tokAt(8));
|
2008-08-25 20:01:11 +02:00
|
|
|
done = false;
|
2008-12-02 19:00:58 +01:00
|
|
|
}
|
2008-12-05 19:39:23 +01:00
|
|
|
|
2008-12-02 19:00:58 +01:00
|
|
|
// Reduce "if ; else %var% ;" => "if %var% ;"
|
|
|
|
if ( TOKEN::Match(tok2, "if ; else %var% ;") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(3) );
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if ; else return use ;" => "if return use ;"
|
|
|
|
if ( TOKEN::Match(tok2, "if ; else %var% ;") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(3) );
|
|
|
|
done = false;
|
2008-08-25 20:01:11 +02:00
|
|
|
}
|
2008-11-30 20:00:07 +01:00
|
|
|
|
|
|
|
// Reduce "do { alloc ; } " => "alloc ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "do { alloc ; }") )
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
2008-12-02 18:22:01 +01:00
|
|
|
erase(tok2->next->next, tok2->tokAt(4));
|
2008-11-30 20:00:07 +01:00
|
|
|
done = false;
|
|
|
|
}
|
2008-08-25 20:01:11 +02:00
|
|
|
|
2008-12-02 18:22:01 +01:00
|
|
|
// Reduce "loop if break ; => ";"
|
|
|
|
if ( TOKEN::Match( tok2->next, "loop %var%" ) &&
|
|
|
|
tok2->tokAt(2)->str().find("if") == 0 &&
|
|
|
|
(TOKEN::Match( tok2->tokAt(3), "break ; ") || TOKEN::Match( tok2->tokAt(3), "continue ;")) &&
|
|
|
|
!TOKEN::Match(tok2->tokAt(5),"else") )
|
2008-12-01 08:50:36 +01:00
|
|
|
{
|
2008-12-02 18:22:01 +01:00
|
|
|
erase( tok2, tok2->tokAt(4) );
|
2008-12-01 08:50:36 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(true) X ;" => "X ;"
|
|
|
|
if (TOKEN::Match(tok2->next, "if(true) %var% ;") && !TOKEN::Match(tok2->tokAt(4),"else"))
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop { X ; break ; }" with "X ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "loop { %var% ; break ; }") )
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
erase(tok2->next->next, tok2->tokAt(6));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-10-19 09:23:06 +02:00
|
|
|
// Replace "loop ;" with ";"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "loop ;") )
|
2008-08-16 21:19:56 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(2));
|
2008-08-16 21:19:56 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:46:28 +01:00
|
|
|
// Replace "loop !var ;" with ";"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "loop !var ;") )
|
2008-11-07 08:46:28 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(4));
|
2008-11-07 08:46:28 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop !var alloc ;" with " alloc ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2->next, "loop !var alloc ;") )
|
2008-11-07 08:46:28 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(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 ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, "alloc ; if(!var) return ;") )
|
2008-08-16 21:19:56 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(4));
|
2008-08-17 19:17:17 +02:00
|
|
|
done = false;
|
2008-12-01 08:50:36 +01:00
|
|
|
}
|
2008-12-05 19:39:23 +01:00
|
|
|
|
2008-12-02 18:22:01 +01:00
|
|
|
// Delete if block: "alloc; if return use ;"
|
2008-12-01 19:52:40 +01:00
|
|
|
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if return ; if return ;" => "if return ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "if return ; if return ;") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(4) );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-12-05 19:39:23 +01:00
|
|
|
|
2008-12-02 18:22:01 +01:00
|
|
|
// Reduce "[;{}] return ; %var%" => "[;{}] return ;"
|
|
|
|
if ( TOKEN::Match(tok2, "[;{}] return ; %var%") )
|
|
|
|
{
|
|
|
|
erase( tok2->next->next, tok2->tokAt(4) );
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
|
|
|
|
if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") )
|
|
|
|
{
|
|
|
|
erase( tok2->next->next->next, tok2->tokAt(5) );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-12-01 19:52:40 +01:00
|
|
|
|
2008-12-01 08:50:36 +01:00
|
|
|
// Reduce "if(var) return use ;" => "return use ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "if(var) return use ;") && !TOKEN::Match(tok2->tokAt(5),"else"))
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
done = false;
|
2008-08-17 19:17:17 +02:00
|
|
|
}
|
2008-12-01 08:50:36 +01:00
|
|
|
|
|
|
|
// Reduce "if(var) use ;" => "use ;"
|
|
|
|
if ( TOKEN::Match(tok2->next, "if(var) use ;") && !TOKEN::Match(tok2->tokAt(4),"else"))
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(2) );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-12-01 19:52:40 +01:00
|
|
|
|
2008-12-02 19:00:58 +01:00
|
|
|
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]"
|
|
|
|
if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(5) );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-12-05 19:39:23 +01:00
|
|
|
|
2008-12-01 19:52:40 +01:00
|
|
|
// Reduce "if* alloc ; dealloc ;" => ";"
|
|
|
|
if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") &&
|
|
|
|
tok2->next->str().find("if") == 0 )
|
|
|
|
{
|
|
|
|
erase( tok2, tok2->tokAt(5) );
|
|
|
|
done = false;
|
|
|
|
}
|
2008-08-17 19:17:17 +02:00
|
|
|
|
2008-08-23 15:16:25 +02:00
|
|
|
// Delete second use in "use ; use ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
while (TOKEN::Match(tok2, "[;{}] use ; use ;"))
|
2008-08-17 19:17:17 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(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
|
|
|
|
2008-12-02 19:00:58 +01:00
|
|
|
// Delete second use in "use ; use ;"
|
|
|
|
if (TOKEN::Match(tok2, "[;{}] use ; dealloc ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first use in "use ; return use ;"
|
|
|
|
if (TOKEN::Match(tok2, "[;{}] use ; return use ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-08-23 15:16:25 +02:00
|
|
|
// Delete second case in "case ; case ;"
|
2008-11-22 23:49:14 +01:00
|
|
|
while (TOKEN::Match(tok2, "case ; case ;"))
|
2008-08-23 15:16:25 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
erase(tok2, tok2->tokAt(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)
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok2, "switch {"))
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
|
|
|
// 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-23 11:09:16 +01:00
|
|
|
for ( const TOKEN * _tok = tok2->tokAt(2); _tok; _tok = _tok->next )
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( _tok->str() == "{" )
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if ( _tok->str() == "}" )
|
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
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (strncmp(_tok->aaaa(),"if",2)==0)
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-09-29 08:38:25 +02:00
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if (_tok->str() == "switch")
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if (_tok->str() == "loop")
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (incase && TOKEN::Match(_tok,"case"))
|
2008-08-31 09:42:54 +02:00
|
|
|
break;
|
2008-08-24 10:01:58 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
incase |= TOKEN::Match(_tok,"case");
|
|
|
|
incase &= !TOKEN::Match(_tok,"break");
|
2008-08-24 10:01:58 +02:00
|
|
|
}
|
|
|
|
|
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-23 11:09:16 +01:00
|
|
|
erase( tok2, tok2->tokAt(2) );
|
2008-08-24 10:01:58 +02:00
|
|
|
tok2 = tok2->next;
|
|
|
|
bool first = true;
|
2008-11-22 23:49:14 +01:00
|
|
|
while (TOKEN::Match(tok2,"case") || TOKEN::Match(tok2,"default"))
|
2008-08-24 10:01:58 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
bool def = TOKEN::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-12-05 20:37:11 +01:00
|
|
|
while ( tok2 && tok2->str() != "}" && ! TOKEN::Match(tok2,"break ;") )
|
2008-09-29 08:38:25 +02:00
|
|
|
tok2 = tok2->next;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::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
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok2, "throw") )
|
2008-11-10 19:51:44 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-02 17:58:43 +01:00
|
|
|
// Check for memory leaks for a function variable.
|
2008-11-16 07:34:22 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-16 14:55:16 +01:00
|
|
|
std::list<const TOKEN *> callstack;
|
2008-11-09 18:36:53 +01:00
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
AllocType alloctype = No;
|
|
|
|
AllocType dealloctype = No;
|
|
|
|
|
2008-11-16 14:55:16 +01:00
|
|
|
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
|
2008-12-01 19:52:40 +01:00
|
|
|
simplifycode( tok );
|
2008-11-09 18:36:53 +01:00
|
|
|
|
|
|
|
// If the variable is not allocated at all => no memory leak
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::findmatch(tok, "alloc") == 0)
|
2008-11-09 18:36:53 +01:00
|
|
|
{
|
2008-11-20 21:54:52 +01:00
|
|
|
Tokenizer::deleteTokens(tok);
|
2008-11-09 18:36:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-08-19 19:16:55 +02:00
|
|
|
|
2008-11-10 21:18:03 +01:00
|
|
|
// TODO : handle "goto"
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::findmatch(tok, "goto"))
|
2008-11-10 21:18:03 +01:00
|
|
|
{
|
2008-11-20 21:54:52 +01:00
|
|
|
Tokenizer::deleteTokens(tok);
|
2008-11-10 21:18:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::findmatch(tok, "loop alloc ;") )
|
2008-08-27 08:33:27 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
MemoryLeak(TOKEN::findmatch(tok, "loop alloc ;"), varname);
|
2008-08-27 08:33:27 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::findmatch(tok, "alloc ; if continue ;") )
|
2008-08-17 19:17:17 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
// MemoryLeak(Tokenizer::gettok(TOKEN::findmatch(tok, "alloc ; if continue ;"), 3), varname);
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if continue ;"))->tokAt(3), varname);
|
2008-08-17 20:01:37 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::findmatch(tok, "alloc ; if break ;") )
|
2008-11-11 20:46:08 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if break ;"))->tokAt(3), varname);
|
2008-11-11 20:46:08 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::findmatch(tok, "alloc ; if return ;") )
|
2008-08-17 20:01:37 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if return ;"))->tokAt(3), varname);
|
2008-08-17 19:17:17 +02:00
|
|
|
}
|
2008-11-22 21:00:36 +01:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv continue ;") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv continue ;"))->tokAt(3), varname);
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv break ;") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv break ;"))->tokAt(3), varname);
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv return ;") )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv return ;"))->tokAt(3), varname);
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
2008-08-17 19:17:17 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::findmatch(tok, "alloc ; return ;") )
|
2008-08-16 18:47:54 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok,"alloc ; return ;"))->tokAt(2), varname);
|
2008-08-16 18:47:54 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::findmatch(tok, "alloc ; alloc") )
|
2008-11-07 17:19:55 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
MemoryLeak((TOKEN::findmatch(tok,"alloc ; alloc"))->tokAt(2), varname);
|
2008-11-07 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( ! TOKEN::findmatch(tok,"dealloc") &&
|
|
|
|
! TOKEN::findmatch(tok,"use") &&
|
|
|
|
! TOKEN::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-12-01 08:50:36 +01:00
|
|
|
|
2008-12-02 18:22:01 +01:00
|
|
|
// detect cases that "simplifycode" don't handle well..
|
2008-12-01 08:50:36 +01:00
|
|
|
else if ( _settings._debug )
|
|
|
|
{
|
|
|
|
TOKEN *first = tok;
|
|
|
|
while ( first && first->str() == ";" )
|
|
|
|
first = first->next;
|
|
|
|
|
|
|
|
bool noerr = false;
|
2008-12-02 18:22:01 +01:00
|
|
|
noerr |= TOKEN::Match( first, "alloc ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "alloc ; dealloc ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "alloc ; return use ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "alloc ; use ; }" );
|
2008-12-02 19:00:58 +01:00
|
|
|
noerr |= TOKEN::Match( first, "alloc ; use ; return ; }" );
|
2008-12-02 18:22:01 +01:00
|
|
|
noerr |= TOKEN::Match( first, "if alloc ; dealloc ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "if alloc ; return use ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "if alloc ; use ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ; }" );
|
|
|
|
noerr |= TOKEN::Match( first, "alloc ; if return ; dealloc; }" );
|
2008-12-01 08:50:36 +01:00
|
|
|
|
|
|
|
// Unhandled case..
|
|
|
|
if ( ! noerr )
|
|
|
|
{
|
|
|
|
std::cout << "Token listing..\n ";
|
|
|
|
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
|
|
|
std::cout << " " << tok2->str();
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 21:54:52 +01:00
|
|
|
Tokenizer::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;
|
2008-11-16 16:58:52 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next)
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if (tok->str() == "{")
|
2008-04-02 07:09:35 +02:00
|
|
|
indentlevel++;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if (tok->str() == "}")
|
2008-04-02 07:09:35 +02:00
|
|
|
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-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, ") {") )
|
2008-04-03 08:15:26 +02:00
|
|
|
infunc = true;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "[;}]") )
|
2008-04-03 08:15:26 +02:00
|
|
|
infunc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a local variable => Check
|
|
|
|
if (indentlevel>0 && infunc)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "[{};] %type% * %var% [;=]") )
|
2008-11-23 11:09:16 +01:00
|
|
|
CheckMemoryLeak_CheckScope( tok->next, tok->strAt(3) );
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(tok, "[{};] %type% %type% * %var% [;=]") )
|
2008-11-23 11:09:16 +01:00
|
|
|
CheckMemoryLeak_CheckScope( tok->next, tok->strAt(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;
|
2008-11-16 16:58:52 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-04-04 06:29:28 +02:00
|
|
|
indentlevel++;
|
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if ( tok->str() == "}" )
|
2008-04-04 06:29:28 +02:00
|
|
|
indentlevel--;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( indentlevel == 0 && TOKEN::Match(tok, "class %var% [{:]") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
std::vector<const char *> classname;
|
2008-11-23 11:09:16 +01:00
|
|
|
classname.push_back( tok->strAt(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.
|
2008-12-05 20:37:11 +01:00
|
|
|
while ( tok1 && tok1->str() != "{" )
|
2008-04-04 06:29:28 +02:00
|
|
|
tok1 = tok1->next;
|
|
|
|
if ( tok1 )
|
|
|
|
tok1 = tok1->next;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
for ( const TOKEN *tok = tok1; tok; tok = tok->next )
|
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-04-04 06:29:28 +02:00
|
|
|
indentlevel++;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if ( tok->str() == "}" )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
|
|
|
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
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "class %var% [{:]") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
classname.push_back( tok->strAt(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
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok->next, "%type% * %var% ;") )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( tok->isName() || TOKEN::Match(tok, "[;}]"))
|
2008-08-18 08:40:37 +02:00
|
|
|
{
|
2008-11-26 08:23:54 +01:00
|
|
|
if (_settings._showAll || !isclass(tok->tokAt(1)))
|
2008-11-23 11:09:16 +01:00
|
|
|
CheckMemoryLeak_ClassMembers_Variable( classname, tok->strAt(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;
|
2008-11-16 16:58:52 +01:00
|
|
|
for ( const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next )
|
2008-04-05 14:27:02 +02:00
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( tok->str() == "{" )
|
2008-04-05 14:27:02 +02:00
|
|
|
indentlevel++;
|
|
|
|
|
2008-12-05 20:37:11 +01:00
|
|
|
else if ( tok->str() == "}" )
|
2008-04-05 14:27:02 +02:00
|
|
|
indentlevel--;
|
|
|
|
|
|
|
|
// Set the 'memberfunction' variable..
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
{
|
2008-12-05 20:37:11 +01:00
|
|
|
if ( TOKEN::Match(tok, "[;}]") )
|
2008-04-05 14:27:02 +02:00
|
|
|
memberfunction = false;
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match( tok, fpattern.str().c_str() ) || TOKEN::Match( tok, destructor.str().c_str() ) )
|
2008-04-05 14:27:02 +02:00
|
|
|
memberfunction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse member function..
|
|
|
|
if ( indentlevel > 0 && memberfunction )
|
|
|
|
{
|
|
|
|
// Allocate..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok, varname_eq.str().c_str() ) )
|
2008-04-04 06:29:28 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
AllocType alloc = GetAllocationType( tok->tokAt(2) );
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( alloc != No )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-16 14:55:16 +01:00
|
|
|
std::list<const TOKEN *> callstack;
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Dealloc != No && Dealloc != alloc )
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError( tok, callstack, FullVariableName.str().c_str() );
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Alloc != No && Alloc != alloc )
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError( tok, callstack, FullVariableName.str().c_str() );
|
2008-04-05 14:27:02 +02:00
|
|
|
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 )
|
2008-11-22 21:00:36 +01:00
|
|
|
{
|
2008-11-16 14:55:16 +01:00
|
|
|
std::list<const TOKEN *> callstack;
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Dealloc != No && Dealloc != dealloc )
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError( tok, callstack, FullVariableName.str().c_str() );
|
2008-04-05 14:27:02 +02:00
|
|
|
if ( Alloc != No && Alloc != dealloc )
|
2008-11-16 14:55:16 +01:00
|
|
|
MismatchError( tok, callstack, FullVariableName.str().c_str() );
|
2008-04-05 14:27:02 +02:00
|
|
|
Dealloc = dealloc;
|
|
|
|
}
|
2008-04-04 06:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-05 14:27:02 +02:00
|
|
|
|
|
|
|
if ( Alloc != No && Dealloc == No )
|
|
|
|
{
|
2008-11-16 16:58:52 +01:00
|
|
|
MemoryLeak( _tokenizer->tokens(), FullVariableName.str().c_str() );
|
2008-04-05 14:27:02 +02:00
|
|
|
}
|
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
|
|
|
{
|
2008-11-23 12:08:07 +01: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
|
|
|
|
2008-11-28 18:16:28 +01:00
|
|
|
// Check that all class members are deallocated..
|
|
|
|
if ( _settings._showAll )
|
|
|
|
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
|
|
|
|