2009-01-15 21:34:39 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
2009-03-01 20:52:33 +01:00
|
|
|
* Leandro Penz, Kimmo Varis, Vesa Pikki
|
2009-01-15 21:34:39 +01:00
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "checkmemoryleak.h"
|
2009-05-02 10:45:15 +02:00
|
|
|
#include "mathlib.h"
|
2009-03-20 18:16:21 +01:00
|
|
|
#include "tokenize.h"
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
#include <algorithm>
|
2009-01-23 21:55:06 +01:00
|
|
|
#include <cstring>
|
2009-01-15 21:34:39 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
// Register this check class (by creating a static instance of it)
|
|
|
|
namespace
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-03-20 18:16:21 +01:00
|
|
|
CheckMemoryLeakClass instance;
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
bool CheckMemoryLeakClass::isclass(const Token *tok)
|
|
|
|
{
|
|
|
|
if (tok->isStandardType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::ostringstream pattern;
|
|
|
|
pattern << "struct " << tok->str();
|
|
|
|
if (Token::findmatch(_tokenizer->tokens(), pattern.str().c_str()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const Token *tok2)
|
|
|
|
{
|
|
|
|
// What we may have...
|
|
|
|
// * var = (char *)malloc(10);
|
|
|
|
// * var = new char[10];
|
|
|
|
// * var = strdup("hello");
|
2009-05-19 22:29:10 +02:00
|
|
|
// * var = strndup("hello", 3);
|
2009-01-15 21:34:39 +01:00
|
|
|
if (tok2 && tok2->str() == "(")
|
|
|
|
{
|
|
|
|
while (tok2 && tok2->str() != ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2 = tok2 ? tok2->next() : NULL;
|
|
|
|
}
|
|
|
|
if (! tok2)
|
|
|
|
return No;
|
|
|
|
if (! tok2->isName())
|
|
|
|
return No;
|
|
|
|
|
|
|
|
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
|
2009-05-21 12:26:44 +02:00
|
|
|
static const char * const mallocfunc[] = {"malloc",
|
|
|
|
"calloc",
|
|
|
|
"strdup",
|
|
|
|
"strndup",
|
|
|
|
"kmalloc",
|
|
|
|
"kzalloc",
|
|
|
|
"kcalloc",
|
|
|
|
0
|
|
|
|
};
|
2009-01-15 21:34:39 +01:00
|
|
|
for (unsigned int i = 0; mallocfunc[i]; i++)
|
|
|
|
{
|
|
|
|
if (tok2->str() == mallocfunc[i])
|
|
|
|
return Malloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does tok2 point on "g_malloc", "g_strdup", ..
|
2009-05-21 12:26:44 +02:00
|
|
|
static const char * const 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
|
|
|
|
};
|
2009-01-15 21:34:39 +01:00
|
|
|
for (unsigned int i = 0; gmallocfunc[i]; i++)
|
|
|
|
{
|
|
|
|
if (tok2->str() == gmallocfunc[i])
|
|
|
|
return gMalloc;
|
|
|
|
}
|
|
|
|
|
2009-03-14 20:19:36 +01:00
|
|
|
if (Token::Match(tok2, "new %type% [;()]"))
|
2009-01-15 21:34:39 +01:00
|
|
|
return New;
|
|
|
|
|
|
|
|
if (Token::Match(tok2, "new %type% ["))
|
2009-01-31 14:57:27 +01:00
|
|
|
return NewArray;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "fopen ("))
|
2009-05-21 12:16:42 +02:00
|
|
|
return File;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "popen ("))
|
2009-05-21 12:16:42 +02:00
|
|
|
return Pipe;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-05-23 22:45:14 +02:00
|
|
|
if (Token::Match(tok2, "opendir|fdopendir ("))
|
2009-05-22 09:24:03 +02:00
|
|
|
return Dir;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Userdefined allocation function..
|
|
|
|
std::list<AllocFunc>::const_iterator it = _listAllocFunc.begin();
|
|
|
|
while (it != _listAllocFunc.end())
|
|
|
|
{
|
|
|
|
if (tok2->str() == it->funcname)
|
|
|
|
return it->alloctype;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType(const Token *tok2)
|
|
|
|
{
|
|
|
|
// What we may have...
|
|
|
|
// * var = (char *)realloc(..;
|
|
|
|
if (tok2 && tok2->str() == "(")
|
|
|
|
{
|
|
|
|
while (tok2 && tok2->str() != ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2 = tok2 ? tok2->next() : NULL;
|
|
|
|
}
|
|
|
|
if (! tok2)
|
|
|
|
return No;
|
|
|
|
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "realloc"))
|
2009-01-15 21:34:39 +01:00
|
|
|
return Malloc;
|
|
|
|
|
|
|
|
// GTK memory reallocation..
|
|
|
|
if (Token::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew"))
|
|
|
|
return gMalloc;
|
|
|
|
|
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType(const Token *tok, const char *varnames[])
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
std::string names;
|
|
|
|
while (varnames[i])
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
names += " . ";
|
|
|
|
|
|
|
|
names += varnames[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("delete " + names + " ;").c_str()))
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("delete [ ] " + names + " ;").c_str()))
|
2009-01-31 14:57:27 +01:00
|
|
|
return NewArray;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-02-14 07:54:23 +01:00
|
|
|
if (Token::simpleMatch(tok, std::string("delete ( " + names + " ) ;").c_str()))
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("delete [ ] ( " + names + " ) ;").c_str()))
|
|
|
|
return NewArray;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
if (Token::simpleMatch(tok, std::string("free ( " + names + " ) ;").c_str()) ||
|
|
|
|
Token::simpleMatch(tok, std::string("kfree ( " + names + " ) ;").c_str()))
|
|
|
|
return Malloc;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("g_free ( " + names + " ) ;").c_str()))
|
|
|
|
return gMalloc;
|
|
|
|
|
2009-05-22 16:47:40 +02:00
|
|
|
if (Token::simpleMatch(tok, std::string("fclose ( " + names + " )").c_str()) ||
|
|
|
|
Token::simpleMatch(tok, "fcloseall ( )"))
|
2009-05-21 12:16:42 +02:00
|
|
|
return File;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("pclose ( " + names + " )").c_str()))
|
2009-05-21 12:16:42 +02:00
|
|
|
return Pipe;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-05-22 09:24:03 +02:00
|
|
|
if (Token::simpleMatch(tok, std::string("closedir ( " + names + " )").c_str()))
|
|
|
|
return Dir;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
return No;
|
|
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2009-02-07 11:54:39 +01:00
|
|
|
const char * CheckMemoryLeakClass::call_func(const Token *tok, std::list<const Token *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// Keywords that are not function calls..
|
2009-02-08 12:59:04 +01:00
|
|
|
if (Token::Match(tok, "if|for|while|return|switch"))
|
2009-01-15 21:34:39 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// String functions that are not allocating nor deallocating memory..
|
|
|
|
if (Token::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Memory functions that are not allocating nor deallocating memory..
|
|
|
|
if (Token::Match(tok, "memset|memcpy|memmove|memchr"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// I/O functions that are not allocating nor deallocating memory..
|
2009-05-23 13:01:18 +02:00
|
|
|
if (Token::Match(tok, "fgets|fgetc|fputs|fputc|feof|ferror|clearerr|printf|fprintf") ||
|
|
|
|
Token::Match(tok, "fread|fwrite|fflush|fseek|fseeko|ftell|ftello|fsetpos|fgetpos") ||
|
|
|
|
Token::Match(tok, "setvbuf|setbuf|setbuffer|setlinebuf|rewind"))
|
2009-01-15 21:34:39 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-05-22 09:24:03 +02:00
|
|
|
// Functions to work with directories that are not allocating nor
|
|
|
|
// deallocating memory..
|
|
|
|
if (Token::Match(tok, "readdir|readdir_r|rewinddir|telldir|seekdir|scandir"))
|
|
|
|
return 0;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Convert functions that are not allocating nor deallocating memory..
|
|
|
|
if (Token::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod"))
|
|
|
|
return 0;
|
|
|
|
|
2009-02-14 07:54:23 +01:00
|
|
|
// This is not an unknown function neither
|
|
|
|
if (tok->str() == "delete")
|
|
|
|
return 0;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
if (GetAllocationType(tok) != No || GetReallocationType(tok) != No || GetDeallocationType(tok, varnames) != No)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (callstack.size() > 2)
|
|
|
|
return "dealloc_";
|
|
|
|
|
2009-02-08 12:59:04 +01:00
|
|
|
const std::string funcname(tok->str());
|
2009-01-15 21:34:39 +01:00
|
|
|
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
|
|
|
|
{
|
|
|
|
if ((*it)->str() == funcname)
|
|
|
|
return "recursive";
|
|
|
|
}
|
|
|
|
callstack.push_back(tok);
|
|
|
|
|
|
|
|
int par = 1;
|
|
|
|
int parlevel = 0;
|
|
|
|
std::string pattern = "[,()] ";
|
|
|
|
for (int i = 0; varnames[i]; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
pattern += " . ";
|
|
|
|
|
|
|
|
pattern += varnames[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern += " [,()]";
|
|
|
|
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel < 1)
|
2009-02-08 12:59:04 +01:00
|
|
|
{
|
2009-03-20 18:16:21 +01:00
|
|
|
return _settings->_showAll ? 0 : "callfunc";
|
2009-02-08 12:59:04 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parlevel == 1)
|
|
|
|
{
|
|
|
|
if (tok->str() == ",")
|
|
|
|
++par;
|
|
|
|
if (Token::Match(tok, pattern.c_str()))
|
|
|
|
{
|
2009-02-08 12:59:04 +01:00
|
|
|
const Token *ftok = _tokenizer->GetFunctionTokenByName(funcname.c_str());
|
2009-01-15 21:34:39 +01:00
|
|
|
const char *parname = Tokenizer::getParameterName(ftok, par);
|
|
|
|
if (! parname)
|
|
|
|
return "recursive";
|
|
|
|
// Check if the function deallocates the variable..
|
|
|
|
while (ftok && (ftok->str() != "{"))
|
|
|
|
ftok = ftok->next();
|
2009-02-07 11:54:39 +01:00
|
|
|
Token *func = getcode(ftok->tokAt(1), callstack, parname, alloctype, dealloctype, false, all, sz);
|
2009-02-09 21:16:00 +01:00
|
|
|
simplifycode(func, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
const Token *func_ = func;
|
|
|
|
while (func_ && func_->str() == ";")
|
|
|
|
func_ = func_->next();
|
|
|
|
|
|
|
|
const char *ret = 0;
|
|
|
|
// TODO : "goto" isn't handled well
|
|
|
|
if (Token::findmatch(func_, "dealloc"))
|
|
|
|
ret = "dealloc";
|
|
|
|
else if (Token::findmatch(func_, "use"))
|
|
|
|
ret = "use";
|
|
|
|
else if (Token::findmatch(func_, "&use"))
|
|
|
|
ret = "&use";
|
|
|
|
|
|
|
|
Tokenizer::deleteTokens(func);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2009-01-31 14:57:27 +01:00
|
|
|
void CheckMemoryLeakClass::MemoryLeak(const Token *tok, const char varname[], AllocType alloctype, bool all)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-05-21 12:16:42 +02:00
|
|
|
if (alloctype == CheckMemoryLeakClass::File ||
|
2009-05-22 09:24:03 +02:00
|
|
|
alloctype == CheckMemoryLeakClass::Pipe ||
|
|
|
|
alloctype == CheckMemoryLeakClass::Dir)
|
2009-03-21 17:58:13 +01:00
|
|
|
resourceLeakError(tok, varname);
|
2009-01-31 14:57:27 +01:00
|
|
|
else if (all)
|
2009-03-21 17:58:13 +01:00
|
|
|
memleakallError(tok, varname);
|
2009-01-15 21:34:39 +01:00
|
|
|
else
|
2009-03-21 17:58:13 +01:00
|
|
|
memleakError(tok, varname);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-01-31 22:12:24 +01:00
|
|
|
bool CheckMemoryLeakClass::MatchFunctionsThatReturnArg(const Token *tok, const std::string &varname)
|
2009-01-25 21:57:34 +01:00
|
|
|
{
|
2009-01-28 01:23:56 +01:00
|
|
|
return Token::Match(tok, std::string("; " + varname + " = strcat|memcpy|memmove|strcpy ( " + varname + " ,").c_str());
|
2009-01-25 21:57:34 +01:00
|
|
|
}
|
|
|
|
|
2009-02-04 07:11:36 +01:00
|
|
|
bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[], bool endpar)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
std::string varname;
|
|
|
|
for (int i = 0; varnames[i]; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
varname += " . ";
|
|
|
|
|
|
|
|
varname += varnames[i];
|
|
|
|
}
|
|
|
|
|
2009-05-01 11:28:33 +02:00
|
|
|
const std::string end(endpar ? " &&|)" : " [;)&|]");
|
2009-02-04 07:11:36 +01:00
|
|
|
|
2009-03-23 18:34:59 +01:00
|
|
|
return bool(Token::Match(tok, ("! " + varname + end).c_str()) ||
|
2009-05-01 13:39:57 +02:00
|
|
|
Token::Match(tok, ("! ( " + varname + " )" + end).c_str()));
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-02-07 11:54:39 +01:00
|
|
|
Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype, bool classmember, bool &all, unsigned int sz)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
const char *varnames[2];
|
|
|
|
varnames[0] = varname;
|
|
|
|
varnames[1] = 0;
|
|
|
|
std::string varnameStr = varname;
|
|
|
|
|
|
|
|
Token *rethead = 0, *rettail = 0;
|
|
|
|
#define addtoken(_str) \
|
|
|
|
{ \
|
|
|
|
if (rettail) \
|
|
|
|
{ \
|
|
|
|
rettail->insertToken(_str); \
|
|
|
|
rettail = rettail->next(); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
rethead = new Token; \
|
|
|
|
rettail = rethead; \
|
|
|
|
rettail->str(_str); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
rettail->linenr( tok->linenr() ); \
|
|
|
|
rettail->fileIndex( tok->fileIndex() ); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first token should be ";"
|
|
|
|
addtoken(";");
|
|
|
|
|
|
|
|
bool isloop = false;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
int parlevel = 0;
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
addtoken("{");
|
|
|
|
++indentlevel;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
addtoken("}");
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
--parlevel;
|
|
|
|
isloop &= (parlevel > 0);
|
|
|
|
|
|
|
|
if (parlevel == 0 && tok->str() == ";")
|
|
|
|
addtoken(";");
|
|
|
|
|
2009-02-07 10:44:57 +01:00
|
|
|
if (Token::Match(tok->previous(), std::string("[(;{}] " + varnameStr + " =").c_str()))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
AllocType alloc = GetAllocationType(tok->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
bool realloc = false;
|
|
|
|
|
2009-02-07 11:54:39 +01:00
|
|
|
if (sz > 1 &&
|
|
|
|
Token::Match(tok->tokAt(2), "malloc ( %num% )") &&
|
2009-05-02 10:45:15 +02:00
|
|
|
(MathLib::toLongNumber(tok->strAt(4)) % sz) != 0)
|
2009-02-07 11:54:39 +01:00
|
|
|
{
|
2009-03-21 17:58:13 +01:00
|
|
|
mismatchSizeError(tok->tokAt(4), tok->strAt(4));
|
2009-02-07 11:54:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
if (alloc == No)
|
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
alloc = GetReallocationType(tok->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
if (alloc != No)
|
|
|
|
{
|
|
|
|
addtoken("realloc");
|
|
|
|
addtoken(";");
|
|
|
|
realloc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "--all" hasn't been given, don't check classes..
|
2009-01-31 14:57:27 +01:00
|
|
|
if (alloc == New)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
if (Token::Match(tok->tokAt(2), "new %type% [(;]"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
if (isclass(tok->tokAt(3)))
|
2009-01-31 14:57:27 +01:00
|
|
|
{
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->_showAll)
|
2009-02-20 07:28:18 +01:00
|
|
|
{
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->isAutoDealloc(tok->strAt(3)))
|
2009-02-20 07:28:18 +01:00
|
|
|
{
|
2009-03-06 07:22:07 +01:00
|
|
|
// This class has automatic deallocation
|
2009-02-20 07:28:18 +01:00
|
|
|
alloc = No;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The checking will proceed.. but any error messages that are shown are shown thanks to "--all"
|
|
|
|
all = true;
|
|
|
|
}
|
|
|
|
}
|
2009-01-31 14:57:27 +01:00
|
|
|
else
|
|
|
|
alloc = No;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alloc != No)
|
|
|
|
{
|
|
|
|
if (! realloc)
|
|
|
|
addtoken("alloc");
|
2009-02-06 07:11:47 +01:00
|
|
|
|
|
|
|
if (alloctype != No && alloctype != alloc)
|
|
|
|
alloc = Many;
|
|
|
|
|
|
|
|
if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
|
|
|
callstack.push_back(tok);
|
2009-03-21 21:33:27 +01:00
|
|
|
mismatchAllocDealloc(callstack, varname);
|
2009-02-01 16:47:36 +01:00
|
|
|
callstack.pop_back();
|
|
|
|
}
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
alloctype = alloc;
|
|
|
|
}
|
|
|
|
|
2009-02-07 10:44:57 +01:00
|
|
|
else if (MatchFunctionsThatReturnArg(tok->previous(), std::string(varname)))
|
2009-01-25 21:57:34 +01:00
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// assignment..
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// is the pointer in rhs?
|
|
|
|
bool rhs = false;
|
|
|
|
std::string pattern("[=+] " + std::string(varname));
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Token::Match(tok2, pattern.c_str()))
|
|
|
|
{
|
|
|
|
rhs = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addtoken((rhs ? "use" : "assign"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 21:31:31 +02:00
|
|
|
if (Token::Match(tok->previous(), "[;{})=] %var%"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
AllocType dealloc = GetDeallocationType(tok, varnames);
|
|
|
|
if (dealloc != No)
|
|
|
|
{
|
|
|
|
addtoken("dealloc");
|
2009-02-06 07:11:47 +01:00
|
|
|
|
|
|
|
if (dealloctype != No && dealloctype != dealloc)
|
|
|
|
dealloc = Many;
|
|
|
|
|
|
|
|
if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
|
|
|
callstack.push_back(tok);
|
2009-03-21 21:33:27 +01:00
|
|
|
mismatchAllocDealloc(callstack, varname);
|
2009-02-01 16:47:36 +01:00
|
|
|
callstack.pop_back();
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
dealloctype = dealloc;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if else switch
|
|
|
|
if (tok->str() == "if")
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " )").c_str()) ||
|
|
|
|
Token::simpleMatch(tok, std::string("if ( " + varnameStr + " != 0 )").c_str()) ||
|
|
|
|
Token::simpleMatch(tok, std::string("if ( 0 != " + varnameStr + " )").c_str()))
|
|
|
|
{
|
|
|
|
addtoken("if(var)");
|
|
|
|
|
|
|
|
// Make sure the "use" will not be added
|
|
|
|
while (tok->str() != ")")
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2009-02-04 07:11:36 +01:00
|
|
|
else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varnames, true))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if the condition depends on var somehow..
|
|
|
|
bool dep = false;
|
|
|
|
int parlevel = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-05-22 09:24:03 +02:00
|
|
|
if (Token::Match(tok2, std::string("fclose|closedir ( " + varnameStr + " )").c_str()))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("dealloc");
|
|
|
|
addtoken(";");
|
|
|
|
dep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((tok2->str() != ".") &&
|
|
|
|
Token::simpleMatch(tok2->next(), varnameStr.c_str()) &&
|
|
|
|
!Token::simpleMatch(tok2->next(), std::string(varnameStr + " .").c_str()))
|
|
|
|
{
|
|
|
|
dep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-05-01 13:39:57 +02:00
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, std::string("if ( ! " + varnameStr + " &&").c_str()))
|
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
|
|
|
else if (tok->next() &&
|
|
|
|
tok->next()->link() &&
|
|
|
|
Token::simpleMatch(tok->next()->link()->previous()->previous()->previous(), std::string("&& ! " + varnameStr).c_str()))
|
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addtoken((dep ? "ifv" : "if"));
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tok->str() == "else") || (tok->str() == "switch"))
|
|
|
|
{
|
2009-05-03 20:10:59 +02:00
|
|
|
addtoken(tok->str().c_str());
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((tok->str() == "case"))
|
|
|
|
{
|
|
|
|
addtoken("case");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tok->str() == "default"))
|
|
|
|
{
|
|
|
|
addtoken("default");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loops..
|
|
|
|
if ((tok->str() == "for") || (tok->str() == "while"))
|
|
|
|
{
|
|
|
|
addtoken("loop");
|
|
|
|
isloop = true;
|
|
|
|
}
|
|
|
|
if ((tok->str() == "do"))
|
|
|
|
{
|
|
|
|
addtoken("do");
|
|
|
|
}
|
|
|
|
if (isloop && notvar(tok, varnames))
|
|
|
|
addtoken("!var");
|
|
|
|
|
|
|
|
// continue / break..
|
|
|
|
if (tok->str() == "continue")
|
|
|
|
addtoken("continue");
|
|
|
|
if (tok->str() == "break")
|
|
|
|
addtoken("break");
|
|
|
|
|
|
|
|
// goto..
|
|
|
|
if (tok->str() == "goto")
|
|
|
|
{
|
|
|
|
addtoken("goto");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return..
|
|
|
|
if (tok->str() == "return")
|
|
|
|
{
|
|
|
|
addtoken("return");
|
2009-04-28 20:01:35 +02:00
|
|
|
|
|
|
|
// Returning a auto_ptr of this allocated variable..
|
|
|
|
if (Token::simpleMatch(tok->next(), "std :: auto_ptr <"))
|
|
|
|
{
|
|
|
|
const Token *tok2 = tok->tokAt(5);
|
|
|
|
while (tok2 && tok2->str() != ">")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (Token::simpleMatch(tok2, ("> ( " + varnameStr + " )").c_str()))
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
tok = tok2->tokAt(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, ("return &| " + varnameStr).c_str()))
|
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("use");
|
2009-04-28 20:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::simpleMatch(tok->next(), "("))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(" || tok2->str() == ")")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tok2->str() == varname)
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-03 21:39:17 +01:00
|
|
|
else if (Token::Match(tok, ("return strcpy|strncpy|memcpy ( " + varnameStr).c_str()))
|
2009-02-24 07:23:21 +01:00
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// throw..
|
|
|
|
if (Token::Match(tok, "try|throw|catch"))
|
|
|
|
addtoken(tok->strAt(0));
|
|
|
|
|
2009-05-10 08:01:38 +02:00
|
|
|
// exit..
|
|
|
|
if (Token::Match(tok->previous(), "[{};] exit ( %any% ) ;"))
|
|
|
|
addtoken("exit");
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Assignment..
|
|
|
|
if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) ||
|
|
|
|
Token::Match(tok, std::string(varnameStr + " +=|-=").c_str()) ||
|
|
|
|
Token::Match(tok, std::string("+=|<< " + varnameStr + " ;").c_str()))
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
}
|
2009-02-05 21:17:01 +01:00
|
|
|
else if (Token::Match(tok->previous(), std::string("[;{}=(,+-*/] " + varnameStr + " [").c_str()))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("use_");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Investigate function calls..
|
|
|
|
if (Token::Match(tok, "%var% ("))
|
|
|
|
{
|
|
|
|
// Inside class function.. if the var is passed as a parameter then
|
2009-01-26 19:15:44 +01:00
|
|
|
// just add a "::use"
|
|
|
|
// The "::use" means that a member function was probably called but it wasn't analyzed further
|
2009-01-15 21:34:39 +01:00
|
|
|
if (classmember)
|
|
|
|
{
|
|
|
|
int parlevel = 1;
|
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == varnameStr)
|
|
|
|
{
|
2009-01-26 19:15:44 +01:00
|
|
|
addtoken("::use");
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2009-02-07 11:54:39 +01:00
|
|
|
const char *str = call_func(tok, callstack, varnames, alloctype, dealloctype, all, sz);
|
2009-01-15 21:34:39 +01:00
|
|
|
if (str)
|
|
|
|
addtoken(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback..
|
|
|
|
bool matchFirst;
|
|
|
|
if ((matchFirst = Token::Match(tok, "( %var%")) ||
|
|
|
|
Token::Match(tok, "( * %var%"))
|
|
|
|
{
|
|
|
|
int tokIdx = matchFirst ? 2 : 3;
|
|
|
|
|
|
|
|
while (Token::simpleMatch(tok->tokAt(tokIdx), ".") &&
|
|
|
|
Token::Match(tok->tokAt(tokIdx + 1), "%var%"))
|
|
|
|
tokIdx += 2;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok->tokAt(tokIdx), ") ("))
|
|
|
|
{
|
|
|
|
for (const Token *tok2 = tok->tokAt(tokIdx + 2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "[;{]"))
|
|
|
|
break;
|
|
|
|
else if (tok2->str() == varname)
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linux lists..
|
2009-01-24 19:55:56 +01:00
|
|
|
if (Token::Match(tok, std::string("[=(,] & " + varnameStr + " [.[,)]").c_str()))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("&use");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rethead;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::erase(Token *begin, const Token *end)
|
|
|
|
{
|
|
|
|
Token::eraseTokens(begin, end);
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:16:00 +01:00
|
|
|
void CheckMemoryLeakClass::simplifycode(Token *tok, bool &all)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// Replace "throw" that is not in a try block with "return"
|
|
|
|
int indentlevel = 0;
|
|
|
|
int trylevel = -1;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= trylevel)
|
|
|
|
trylevel = -1;
|
|
|
|
}
|
|
|
|
else if (trylevel == -1 && tok2->str() == "try")
|
|
|
|
trylevel = indentlevel;
|
|
|
|
else if (trylevel == -1 && tok2->str() == "throw")
|
|
|
|
tok2->str("return");
|
|
|
|
}
|
|
|
|
|
2009-05-10 08:01:38 +02:00
|
|
|
|
|
|
|
// simplify "exit".. remove everything in its execution path
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() != "exit")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Found an "exit".. try to remove everything before it
|
|
|
|
const Token *tokEnd = tok2->next();
|
|
|
|
while (tok2->previous() && !Token::Match(tok2->previous(), "[{}]"))
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
if (tok2->previous())
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
|
|
|
|
Token::eraseTokens(tok2, tokEnd);
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// reduce the code..
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL)
|
|
|
|
{
|
|
|
|
// Delete extra ";"
|
|
|
|
while (Token::Match(tok2, "[;{}] ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "{ }" with ";"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "{ }"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
tok2->next()->str(";");
|
|
|
|
erase(tok2->next(), tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete braces around a single instruction..
|
|
|
|
if (Token::Match(tok2->next(), "{ %var% ; }"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
erase(tok2->next()->next(), tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2->next(), "{ %var% %var% ; }"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
erase(tok2->next()->next()->next(), tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok2->next(), "if"))
|
|
|
|
{
|
|
|
|
// Delete empty if that is not followed by an else
|
|
|
|
if (Token::Match(tok2->next(), "if ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete "if ; else ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (Token::simpleMatch(tok2->next(), "if ; else ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two "if alloc ;" after one another.. perhaps only one of them can be executed each time
|
2009-03-20 18:16:21 +01:00
|
|
|
else if (!_settings->_showAll && Token::Match(tok2, "[;{}] if alloc ; if alloc ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Make this more generic. Delete "if ; else use ; use"
|
|
|
|
else if (Token::Match(tok2, "; if ; else assign|use ; assign|use") ||
|
|
|
|
Token::Match(tok2, "; if assign|use ; else ; assign|use"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reduce "if assign|dealloc|use ;" that is not followed by an else..
|
|
|
|
// If "--all" has been given these are deleted
|
|
|
|
// Otherwise, only the "if" will be deleted
|
|
|
|
else if (Token::Match(tok2, "[;{}] if assign|dealloc|use ; !!else"))
|
|
|
|
{
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->_showAll)
|
2009-02-09 21:16:00 +01:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
erase(tok2, tok2->tokAt(3));
|
2009-02-09 21:16:00 +01:00
|
|
|
all = true;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
else
|
2009-02-09 21:16:00 +01:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
erase(tok2, tok2->tokAt(2));
|
2009-02-09 21:16:00 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if if" => "if"
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (Token::simpleMatch(tok2, "if if"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if return ; alloc ;" => "alloc ;"
|
|
|
|
else if (Token::Match(tok2, "[;{}] if return ; alloc ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
|
|
|
|
else if (Token::Match(tok2, "[;{}] if alloc ; else return ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2)); // Remove "if"
|
|
|
|
erase(tok2->next(), tok2->tokAt(5)); // Remove "; else return"
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if ; else %var% ;" => "if %var% ;"
|
|
|
|
else if (Token::Match(tok2->next(), "if ; else %var% ;"))
|
|
|
|
{
|
|
|
|
erase(tok2->next(), tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if ; else return use ;" => "if return use ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (Token::simpleMatch(tok2->next(), "if ; else return use ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2->next(), tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if return ; if return ;" => "if return ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (Token::simpleMatch(tok2->next(), "if return ; if return ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first if in .. "if { dealloc|assign|use ; return ; } if return ;"
|
|
|
|
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; } if return ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(8));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-02-08 19:27:09 +01:00
|
|
|
// Remove "if { dealloc ; callfunc ; } !!else"
|
|
|
|
else if (Token::Match(tok2->next(), "if { dealloc|assign|use ; callfunc ; } !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(8));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Reducing if..
|
2009-03-20 18:16:21 +01:00
|
|
|
else if (_settings->_showAll)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "[;{}] if { assign|dealloc|use ; return ; } !!else"))
|
|
|
|
{
|
2009-02-09 21:16:00 +01:00
|
|
|
all = true;
|
2009-01-15 21:34:39 +01:00
|
|
|
erase(tok2, tok2->tokAt(8));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
|
|
|
|
if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;"
|
|
|
|
if (Token::Match(tok2, "; if(!var) alloc ; !!else"))
|
|
|
|
{
|
|
|
|
// Remove the "if(!var)"
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
|
|
|
|
// Insert "dealloc ;" before the "alloc ;"
|
|
|
|
tok2->insertToken(";");
|
|
|
|
tok2->insertToken("dealloc");
|
|
|
|
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "catch ;"
|
|
|
|
if (Token::simpleMatch(tok2->next(), "catch ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if* ;" that is not followed by an else..
|
|
|
|
if (Token::Match(tok2->next(), "if(var)|if(!var)|ifv ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "else ;" => ";"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "else ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete if block: "alloc; if return use ;"
|
|
|
|
if (Token::Match(tok2, "alloc ; if return use ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Replace "dealloc use ;" with "dealloc ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "dealloc use ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the "if break|continue ;" that follows "dealloc ; alloc ;"
|
2009-03-20 18:16:21 +01:00
|
|
|
if (! _settings->_showAll && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->next()->next()->next();
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "do { alloc ; } " => "alloc ;"
|
|
|
|
// TODO: If the loop can be executed twice reduce to "loop alloc ;" instead
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "do { alloc ; }"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
erase(tok2->next()->next(), tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "loop if break ; => ";"
|
|
|
|
if (Token::Match(tok2->next(), "loop if break|continue ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "loop { assign|dealloc|use ; alloc ; if break ; }" to "assign|dealloc|use ; alloc ;"
|
|
|
|
if (Token::Match(tok2->next(), "loop { assign|dealloc|use ; alloc ; if break|continue ; }"))
|
|
|
|
{
|
|
|
|
// erase "loop {"
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
// erase "if break|continue ; }"
|
|
|
|
tok2 = tok2->next()->next()->next()->next();
|
|
|
|
erase(tok2, tok2->tokAt(5));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop ;" with ";"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "loop ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop !var ;" with ";"
|
|
|
|
if (Token::Match(tok2->next(), "loop !var ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop !var alloc ;" with " alloc ;"
|
|
|
|
if (Token::Match(tok2->next(), "loop !var alloc ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
2009-01-17 08:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop if return ;" with "if return ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "loop if return"))
|
2009-01-17 08:55:40 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete if block in "alloc ; if(!var) return ;"
|
|
|
|
if (Token::Match(tok2, "alloc ; if(!var) return ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(4));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete if block: "alloc; if return use ;"
|
|
|
|
if (Token::Match(tok2, "alloc ; if return use ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(var) return use ;" => "return use ;"
|
|
|
|
if (Token::Match(tok2->next(), "if(var) return use ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(var) assign|dealloc|use ;" => "assign|dealloc|use ;"
|
|
|
|
if (Token::Match(tok2->next(), "if(var) assign|dealloc|use ; !!else"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-02-07 10:44:57 +01:00
|
|
|
// malloc - realloc => alloc ; dealloc ; alloc ;
|
|
|
|
// Reduce "[;{}] alloc ; dealloc ; alloc ;" => "[;{}] alloc ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] alloc ; dealloc ; alloc ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
erase(tok2->next(), tok2->tokAt(6));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if* alloc ; dealloc ;" => ";"
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->tokAt(2), "alloc ; dealloc ;") &&
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2->next()->str().find("if") == 0)
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete second use in "use ; use ;"
|
|
|
|
while (Token::Match(tok2, "[;{}] use ; use ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first part in "use ; dealloc ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] use ; dealloc ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first part in "use ; return use ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] use ; return use ;"))
|
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete second case in "case ; case ;"
|
2009-05-21 17:55:52 +02:00
|
|
|
while (Token::simpleMatch(tok2, "case ; case ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
erase(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace switch with if (if not complicated)
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "switch {"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// Right now, I just handle if there are a few case and perhaps a default.
|
|
|
|
bool valid = false;
|
|
|
|
bool incase = false;
|
|
|
|
for (const Token * _tok = tok2->tokAt(2); _tok; _tok = _tok->next())
|
|
|
|
{
|
|
|
|
if (_tok->str() == "{")
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (_tok->str() == "}")
|
|
|
|
{
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-03 20:10:59 +02:00
|
|
|
else if (strncmp(_tok->str().c_str(), "if", 2) == 0)
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
else if (_tok->str() == "switch")
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (_tok->str() == "loop")
|
|
|
|
break;
|
|
|
|
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (incase && Token::simpleMatch(_tok, "case"))
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
|
2009-05-21 17:55:52 +02:00
|
|
|
incase |= Token::simpleMatch(_tok, "case");
|
|
|
|
incase &= !Token::simpleMatch(_tok, "break");
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!incase && valid)
|
|
|
|
{
|
|
|
|
done = false;
|
|
|
|
tok2->str(";");
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
tok2 = tok2->next();
|
|
|
|
bool first = true;
|
2009-05-22 17:03:42 +02:00
|
|
|
while (Token::Match(tok2, "case|default"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-05-21 17:55:52 +02:00
|
|
|
bool def = Token::simpleMatch(tok2, "default");
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2->str(first ? "if" : "}");
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
tok2->insertToken("{");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Insert "else [if] {
|
|
|
|
tok2->insertToken("{");
|
|
|
|
if (! def)
|
|
|
|
tok2->insertToken("if");
|
|
|
|
tok2->insertToken("else");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2009-05-21 17:55:52 +02:00
|
|
|
while (tok2 && tok2->str() != "}" && ! Token::simpleMatch(tok2, "break ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2 = tok2->next();
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2, "break ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
tok2->str(";");
|
|
|
|
tok2 = tok2->next()->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check for memory leaks for a function variable.
|
2009-02-07 11:54:39 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
std::list<const Token *> callstack;
|
|
|
|
|
|
|
|
AllocType alloctype = No;
|
|
|
|
AllocType dealloctype = No;
|
|
|
|
|
2009-01-31 14:57:27 +01:00
|
|
|
bool all = false;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
const Token *result;
|
|
|
|
|
2009-02-07 11:54:39 +01:00
|
|
|
Token *tok = getcode(Tok1, callstack, varname, alloctype, dealloctype, classmember, all, sz);
|
2009-01-15 21:34:39 +01:00
|
|
|
//tok->printOut( "getcode result" );
|
|
|
|
|
|
|
|
// Simplify the code and check if freed memory is used..
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
while (Token::Match(tok2, "[;{}] ;"))
|
|
|
|
erase(tok2, tok2->tokAt(2));
|
|
|
|
}
|
|
|
|
if ((result = Token::findmatch(tok, "dealloc [;{}] use|use_ ;")) != NULL)
|
|
|
|
{
|
2009-03-21 17:58:13 +01:00
|
|
|
deallocuseError(result->tokAt(2), varname);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 19:55:56 +01:00
|
|
|
// Replace "&use" with "use". Replace "use_" with ";"
|
2009-01-15 21:34:39 +01:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "&use")
|
|
|
|
tok2->str("use");
|
2009-01-24 19:55:56 +01:00
|
|
|
else if (tok2->str() == "use_")
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2->str(";");
|
2009-01-26 19:15:44 +01:00
|
|
|
else if (tok2->str() == "::use") // Some kind of member function usage. Not analyzed very well.
|
|
|
|
tok2->str("use");
|
2009-01-16 17:29:41 +01:00
|
|
|
else if (tok2->str() == "recursive")
|
|
|
|
tok2->str("use");
|
|
|
|
else if (tok2->str() == "dealloc_")
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2->str("dealloc");
|
|
|
|
else if (tok2->str() == "realloc")
|
|
|
|
{
|
|
|
|
tok2->str("dealloc");
|
|
|
|
tok2->insertToken("alloc");
|
|
|
|
tok2->insertToken(";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:16:00 +01:00
|
|
|
simplifycode(tok, all);
|
2009-05-01 13:39:57 +02:00
|
|
|
// tok->printOut("simplifycode result");
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// If the variable is not allocated at all => no memory leak
|
|
|
|
if (Token::findmatch(tok, "alloc") == 0)
|
|
|
|
{
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : handle "goto"
|
|
|
|
if (Token::findmatch(tok, "goto"))
|
|
|
|
{
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((result = Token::findmatch(tok, "loop alloc ;")) != NULL)
|
|
|
|
{
|
2009-01-31 14:57:27 +01:00
|
|
|
MemoryLeak(result, varname, alloctype, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ((result = Token::findmatch(tok, "alloc ; if break|continue|return ;")) != NULL
|
|
|
|
&& Token::findmatch(tok, "dealloc ; alloc ; if continue ;") == NULL)
|
|
|
|
{
|
2009-01-31 14:57:27 +01:00
|
|
|
MemoryLeak(result->tokAt(3), varname, alloctype, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
else if (_settings->_showAll && (result = Token::findmatch(tok, "alloc ; ifv break|continue|return ;")) != NULL)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-01-31 14:57:27 +01:00
|
|
|
MemoryLeak(result->tokAt(3), varname, alloctype, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ((result = Token::findmatch(tok, "alloc ; alloc|assign|return ;")) != NULL)
|
|
|
|
{
|
2009-01-31 14:57:27 +01:00
|
|
|
MemoryLeak(result->tokAt(2), varname, alloctype, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ((result = Token::findmatch(tok, "dealloc ; dealloc ;")) != NULL)
|
|
|
|
{
|
2009-03-21 17:58:13 +01:00
|
|
|
deallocDeallocError(result->tokAt(2), varname);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (! Token::findmatch(tok, "dealloc") &&
|
|
|
|
! Token::findmatch(tok, "use") &&
|
|
|
|
! Token::findmatch(tok, "return use ;"))
|
|
|
|
{
|
|
|
|
const Token *last = tok;
|
|
|
|
while (last->next())
|
|
|
|
last = last->next();
|
2009-01-31 14:57:27 +01:00
|
|
|
MemoryLeak(last, varname, alloctype, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// detect cases that "simplifycode" don't handle well..
|
2009-03-20 18:16:21 +01:00
|
|
|
else if (_settings->_debug)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
Token *first = tok;
|
|
|
|
while (first && first->str() == ";")
|
|
|
|
first = first->next();
|
|
|
|
|
|
|
|
bool noerr = false;
|
2009-05-21 17:55:52 +02:00
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; dealloc ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; return use ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; use ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; use ; return ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "if alloc ; dealloc ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "if alloc ; return use ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "if alloc ; use ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; ifv return ; dealloc ; }");
|
|
|
|
noerr |= Token::simpleMatch(first, "alloc ; if return ; dealloc; }");
|
2009-01-15 21:34:39 +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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks inside function..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_InFunction()
|
|
|
|
{
|
|
|
|
bool classmember = false;
|
2009-05-21 21:51:19 +02:00
|
|
|
bool beforeParameters = false;
|
2009-01-15 21:34:39 +01:00
|
|
|
bool infunc = false;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
// In function..
|
|
|
|
if (indentlevel == 0)
|
|
|
|
{
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
2009-01-15 21:34:39 +01:00
|
|
|
infunc = true;
|
|
|
|
|
2009-05-21 21:51:19 +02:00
|
|
|
else if (tok->str() == "(")
|
|
|
|
beforeParameters = false;
|
|
|
|
|
|
|
|
else if (tok->str() == "::" && beforeParameters)
|
2009-03-10 20:44:24 +01:00
|
|
|
classmember = true;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
else if (Token::Match(tok, "[;}]"))
|
2009-05-21 21:51:19 +02:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
infunc = classmember = false;
|
2009-05-21 21:51:19 +02:00
|
|
|
beforeParameters = true;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a local variable => Check
|
|
|
|
if (indentlevel > 0 && infunc)
|
|
|
|
{
|
2009-02-07 11:54:39 +01:00
|
|
|
unsigned int sz = _tokenizer->SizeOfType(tok->strAt(1));
|
|
|
|
if (sz < 1)
|
|
|
|
sz = 1;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
if (Token::Match(tok, "[{};] %type% * %var% [;=]"))
|
2009-02-07 11:54:39 +01:00
|
|
|
CheckMemoryLeak_CheckScope(tok->next(), tok->strAt(3), classmember, sz);
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
else if (Token::Match(tok, "[{};] %type% %type% * %var% [;=]"))
|
2009-02-07 11:54:39 +01:00
|
|
|
CheckMemoryLeak_CheckScope(tok->next(), tok->strAt(4), classmember, sz);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks in classes..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers()
|
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, "class %var% [{:]"))
|
|
|
|
{
|
|
|
|
std::vector<const char *> classname;
|
|
|
|
classname.push_back(tok->strAt(1));
|
|
|
|
CheckMemoryLeak_ClassMembers_ParseClass(tok, classname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass(const Token *tok1, std::vector<const char *> &classname)
|
|
|
|
{
|
|
|
|
// Go into class.
|
|
|
|
while (tok1 && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
if (tok1)
|
|
|
|
tok1 = tok1->next();
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = tok1; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only parse this particular class.. not subclasses
|
|
|
|
if (indentlevel > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Declaring subclass.. recursive checking
|
|
|
|
if (Token::Match(tok, "class %var% [{:]"))
|
|
|
|
{
|
|
|
|
classname.push_back(tok->strAt(1));
|
|
|
|
CheckMemoryLeak_ClassMembers_ParseClass(tok, classname);
|
|
|
|
classname.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declaring member variable.. check allocations and deallocations
|
|
|
|
if (Token::Match(tok->next(), "%type% * %var% ;"))
|
|
|
|
{
|
2009-03-06 07:22:07 +01:00
|
|
|
// No false positives for auto deallocated classes..
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->isAutoDealloc(tok->strAt(1)))
|
2009-02-23 20:32:54 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
if (tok->isName() || Token::Match(tok, "[;}]"))
|
|
|
|
{
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->_showAll || !isclass(tok->tokAt(1)))
|
2009-03-14 18:21:37 +01:00
|
|
|
CheckMemoryLeak_ClassMembers_Variable(classname.back(), tok->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char classname[], const Token *tokVarname)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-03-01 21:34:04 +01:00
|
|
|
const char *varname = tokVarname->strAt(0);
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Check if member variable has been allocated and deallocated..
|
|
|
|
AllocType Alloc = No;
|
|
|
|
AllocType Dealloc = No;
|
|
|
|
|
|
|
|
// Loop through all tokens. Inspect member functions
|
2009-03-14 18:21:37 +01:00
|
|
|
int indent_ = 0;
|
|
|
|
const Token *functionToken = Tokenizer::FindClassFunction(_tokenizer->tokens(), classname, "~| %var%", indent_);
|
|
|
|
while (functionToken)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-03-14 18:21:37 +01:00
|
|
|
int indent = 0;
|
2009-03-14 20:19:36 +01:00
|
|
|
bool initlist = false;
|
2009-03-14 18:21:37 +01:00
|
|
|
for (const Token *tok = functionToken; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indent;
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indent;
|
|
|
|
if (indent <= 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-03-14 20:19:36 +01:00
|
|
|
else if (indent == 0 && Token::simpleMatch(tok, ") :"))
|
|
|
|
initlist = true;
|
|
|
|
else if (initlist || indent > 0)
|
2009-03-14 18:21:37 +01:00
|
|
|
{
|
2009-03-14 20:19:36 +01:00
|
|
|
if (indent == 0)
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, (":|, " + std::string(varname) + " (").c_str()))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
// Allocate..
|
2009-03-14 20:19:36 +01:00
|
|
|
if (indent == 0 || Token::Match(tok, (std::string(varname) + " =").c_str()))
|
2009-03-14 18:21:37 +01:00
|
|
|
{
|
2009-03-15 00:39:45 +01:00
|
|
|
AllocType alloc = GetAllocationType(tok->tokAt((indent > 0) ? 2 : 3));
|
2009-03-14 18:21:37 +01:00
|
|
|
if (alloc != No)
|
|
|
|
{
|
|
|
|
if (Alloc != No && Alloc != alloc)
|
|
|
|
alloc = Many;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
std::list<const Token *> callstack;
|
|
|
|
if (alloc != Many && Dealloc != No && Dealloc != Many && Dealloc != alloc)
|
|
|
|
{
|
|
|
|
callstack.push_back(tok);
|
2009-03-21 21:33:27 +01:00
|
|
|
mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str());
|
2009-03-14 18:21:37 +01:00
|
|
|
callstack.pop_back();
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
Alloc = alloc;
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-03-14 20:19:36 +01:00
|
|
|
if (indent == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
// Deallocate..
|
|
|
|
const char *varnames[3] = { "var", 0, 0 };
|
|
|
|
varnames[0] = varname;
|
|
|
|
AllocType dealloc = GetDeallocationType(tok, varnames);
|
|
|
|
if (dealloc == No)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-03-14 18:21:37 +01:00
|
|
|
varnames[0] = "this";
|
|
|
|
varnames[1] = varname;
|
|
|
|
dealloc = GetDeallocationType(tok, varnames);
|
|
|
|
}
|
|
|
|
if (dealloc != No)
|
|
|
|
{
|
|
|
|
if (Dealloc != No && Dealloc != dealloc)
|
|
|
|
dealloc = Many;
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
std::list<const Token *> callstack;
|
2009-03-14 18:21:37 +01:00
|
|
|
if (dealloc != Many && Alloc != No && Alloc != Many && Alloc != dealloc)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
|
|
|
callstack.push_back(tok);
|
2009-03-21 21:33:27 +01:00
|
|
|
mismatchAllocDealloc(callstack, (std::string(classname) + "::" + varname).c_str());
|
2009-02-01 16:47:36 +01:00
|
|
|
callstack.pop_back();
|
|
|
|
}
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
Dealloc = dealloc;
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-23 20:24:52 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-03-14 18:21:37 +01:00
|
|
|
|
|
|
|
functionToken = Tokenizer::FindClassFunction(functionToken->next(), classname, "~| %var%", indent_);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Alloc != No && Dealloc == No)
|
|
|
|
{
|
2009-03-14 18:21:37 +01:00
|
|
|
MemoryLeak(tokVarname, (std::string(classname) + "::" + varname).c_str(), Alloc, true);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::CheckMemoryLeak()
|
|
|
|
{
|
|
|
|
_listAllocFunc.clear();
|
|
|
|
|
|
|
|
// Check for memory leaks inside functions..
|
|
|
|
CheckMemoryLeak_InFunction();
|
|
|
|
|
|
|
|
// Check that all class members are deallocated..
|
2009-03-20 18:16:21 +01:00
|
|
|
if (_settings->_showAll)
|
2009-01-15 21:34:39 +01:00
|
|
|
CheckMemoryLeak_ClassMembers();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Non-recursive function analysis
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Token * CheckMemoryLeakClass::functionParameterCode(const Token *ftok, int parameter)
|
|
|
|
{
|
|
|
|
int param = 1; // First parameter has index 1
|
|
|
|
|
|
|
|
// Extract the code for specified parameter...
|
|
|
|
for (; ftok; ftok = ftok->next())
|
|
|
|
{
|
|
|
|
if (ftok->str() == ")")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ftok->str() == ",")
|
|
|
|
{
|
|
|
|
++param;
|
|
|
|
if (param > parameter)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param != parameter)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! Token::Match(ftok, "* %var% [,)]"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Extract and return the code for this parameter..
|
|
|
|
const char *parname = ftok->strAt(1);
|
|
|
|
|
|
|
|
// Goto function implementation..
|
|
|
|
while (ftok && ftok->str() != "{")
|
|
|
|
ftok = ftok->next();
|
|
|
|
ftok = ftok ? ftok->next() : NULL;
|
|
|
|
|
|
|
|
// Return the code..
|
|
|
|
AllocType alloc = No, dealloc = No;
|
2009-01-31 14:57:27 +01:00
|
|
|
bool all = false;
|
2009-01-15 21:34:39 +01:00
|
|
|
std::list<const Token *> callstack;
|
2009-02-07 11:54:39 +01:00
|
|
|
Token *code = getcode(ftok, callstack, parname, alloc, dealloc, false, all, 1);
|
2009-02-09 21:16:00 +01:00
|
|
|
simplifycode(code, all);
|
2009-01-15 21:34:39 +01:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-21 17:58:13 +01:00
|
|
|
|
|
|
|
void CheckMemoryLeakClass::memleakError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(tok, "error", "memleak", "Memory leak: " + varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::memleakallError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(tok, "all", "memleakall", "Memory leak: " + varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::resourceLeakError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(tok, "error", "resourceLeak", "Resource leak: " + varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::deallocDeallocError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(tok, "error", "deallocDealloc", "Deallocating a deallocated pointer: " + varname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::deallocuseError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(tok, "error", "deallocuse", "Using '" + varname + "' after it is deallocated / released");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeakClass::mismatchSizeError(const Token *tok, const std::string &sz)
|
|
|
|
{
|
|
|
|
reportError(tok, "error", "mismatchSize", "The given size " + sz + " is mismatching");
|
|
|
|
}
|
|
|
|
|
2009-03-21 21:33:27 +01:00
|
|
|
void CheckMemoryLeakClass::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
|
|
|
|
{
|
|
|
|
reportError(callstack, "error", "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
|
|
|
|
}
|