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
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-15 21:34:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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-08-22 20:23:03 +02:00
|
|
|
#include <cstdlib>
|
2009-01-15 21:34:39 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2009-07-22 08:30:51 +02:00
|
|
|
#include <set>
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
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-06-08 20:20:43 +02:00
|
|
|
CheckMemoryLeakInFunction instance1;
|
|
|
|
CheckMemoryLeakInClass instance2;
|
2009-07-19 16:51:31 +02:00
|
|
|
CheckMemoryLeakStructMember instance3;
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-08-22 20:23:03 +02:00
|
|
|
// This list needs to be alphabetically sorted so we can run bsearch on it
|
|
|
|
static const char * const call_func_white_list[] =
|
|
|
|
{
|
2009-08-28 23:26:40 +02:00
|
|
|
"asprintf", "atof", "atoi", "atol", "clearerr", "delete", "fchmod", "fcntl"
|
2009-08-22 20:23:03 +02:00
|
|
|
, "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets"
|
2009-08-30 21:11:15 +02:00
|
|
|
, "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "fseek"
|
2009-08-22 20:23:03 +02:00
|
|
|
, "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate"
|
|
|
|
, "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy"
|
|
|
|
, "memmove", "memset", "posix_fadvise", "posix_fallocate", "pread"
|
2009-11-07 07:21:39 +01:00
|
|
|
, "printf", "puts", "pwrite", "read", "readahead", "readdir", "readdir_r", "readv"
|
2009-08-22 20:23:03 +02:00
|
|
|
, "realloc", "return", "rewind", "rewinddir", "scandir", "seekdir"
|
2009-08-28 23:26:40 +02:00
|
|
|
, "setbuf", "setbuffer", "setlinebuf", "setvbuf", "snprintf", "sprintf", "strcasecmp"
|
2009-08-22 20:23:03 +02:00
|
|
|
, "strcat", "strchr", "strcmp", "strcpy", "stricmp", "strncat", "strncmp"
|
|
|
|
, "strncpy", "strrchr", "strstr", "strtod", "strtol", "strtoul", "switch"
|
2009-10-01 10:56:59 +02:00
|
|
|
, "sync_file_range", "telldir", "typeid", "while", "write", "writev"
|
2009-08-22 20:23:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int call_func_white_list_compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp((const char *)a, *(const char **)b);
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
bool CheckMemoryLeak::isclass(const Tokenizer *_tokenizer, const Token *tok) const
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-08-25 21:25:27 +02:00
|
|
|
CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, unsigned int varid) const
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// 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() == "(")
|
|
|
|
{
|
2009-08-17 22:23:37 +02:00
|
|
|
tok2 = tok2->link();
|
2009-01-15 21:34:39 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-10 22:04:28 +02:00
|
|
|
// Using realloc..
|
2009-08-19 19:42:07 +02:00
|
|
|
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
|
2009-08-10 22:04:28 +02:00
|
|
|
return Malloc;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// 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-06-05 06:03:48 +02:00
|
|
|
if (Token::Match(tok2, "new %type% [;()]") ||
|
|
|
|
Token::Match(tok2, "new ( std :: nothrow ) %type% [;()]") ||
|
|
|
|
Token::Match(tok2, "new ( nothrow ) %type% [;()]"))
|
2009-01-15 21:34:39 +01:00
|
|
|
return New;
|
|
|
|
|
2009-06-05 06:03:48 +02:00
|
|
|
if (Token::Match(tok2, "new %type% [") ||
|
|
|
|
Token::Match(tok2, "new ( std :: nothrow ) %type% [") ||
|
|
|
|
Token::Match(tok2, "new ( nothrow ) %type% ["))
|
2009-01-31 14:57:27 +01:00
|
|
|
return NewArray;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-06-01 12:40:24 +02:00
|
|
|
if (Token::Match(tok2, "fopen|tmpfile ("))
|
2009-05-21 12:16:42 +02:00
|
|
|
return File;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-06-21 17:01:43 +02:00
|
|
|
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp ("))
|
|
|
|
return Fd;
|
|
|
|
|
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
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-19 19:42:07 +02:00
|
|
|
|
2009-08-25 21:25:27 +02:00
|
|
|
CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok2, unsigned int varid) const
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// What we may have...
|
|
|
|
// * var = (char *)realloc(..;
|
|
|
|
if (tok2 && tok2->str() == "(")
|
|
|
|
{
|
2009-08-17 22:23:37 +02:00
|
|
|
tok2 = tok2->link();
|
2009-01-15 21:34:39 +01:00
|
|
|
tok2 = tok2 ? tok2->next() : NULL;
|
|
|
|
}
|
|
|
|
if (! tok2)
|
|
|
|
return No;
|
|
|
|
|
2009-08-19 19:42:07 +02:00
|
|
|
if (! Token::Match(tok2, "%var% ( %varid% [,)]", varid))
|
2009-08-18 20:49:08 +02:00
|
|
|
return No;
|
|
|
|
|
|
|
|
if (tok2->str() == "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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-25 21:25:27 +02:00
|
|
|
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const
|
2009-08-19 19:42:07 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok, "delete %varid% ;", varid))
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "delete [ ] %varid% ;", varid))
|
|
|
|
return NewArray;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "delete ( %varid% ) ;", varid))
|
|
|
|
return New;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "delete [ ] ( %varid% ) ;", varid))
|
|
|
|
return NewArray;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "free ( %varid% ) ;", varid) ||
|
|
|
|
Token::Match(tok, "kfree ( %varid% ) ;", varid))
|
|
|
|
return Malloc;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "g_free ( %varid% ) ;", varid))
|
|
|
|
return gMalloc;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "fclose ( %varid% )", varid) ||
|
|
|
|
Token::Match(tok, "fcloseall ( )"))
|
|
|
|
return File;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "close ( %varid% )", varid))
|
|
|
|
return Fd;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "pclose ( %varid% )", varid))
|
|
|
|
return Pipe;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "closedir ( %varid% )", varid))
|
|
|
|
return Dir;
|
|
|
|
|
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const char *varnames[]) const
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2009-06-21 17:01:43 +02:00
|
|
|
if (Token::simpleMatch(tok, std::string("close ( " + names + " )").c_str()))
|
|
|
|
return Fd;
|
|
|
|
|
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-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2009-09-10 21:22:57 +02:00
|
|
|
void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype, bool all)
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
|
|
|
if (alloctype == CheckMemoryLeak::File ||
|
|
|
|
alloctype == CheckMemoryLeak::Pipe ||
|
2009-06-21 17:01:43 +02:00
|
|
|
alloctype == CheckMemoryLeak::Fd ||
|
2009-06-08 20:20:43 +02:00
|
|
|
alloctype == CheckMemoryLeak::Dir)
|
2009-09-19 08:23:10 +02:00
|
|
|
resourceLeakError(tok, varname.c_str(), all);
|
2009-06-08 20:20:43 +02:00
|
|
|
else
|
2009-09-19 08:23:10 +02:00
|
|
|
memleakError(tok, varname.c_str(), all);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2009-08-04 21:36:55 +02:00
|
|
|
void CheckMemoryLeak::reportErr(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const
|
2009-07-13 15:07:26 +02:00
|
|
|
{
|
2009-08-04 21:36:55 +02:00
|
|
|
std::list<const Token *> callstack;
|
2009-07-13 15:07:26 +02:00
|
|
|
|
2009-08-04 21:36:55 +02:00
|
|
|
if (tok)
|
|
|
|
callstack.push_back(tok);
|
2009-07-13 15:07:26 +02:00
|
|
|
|
2009-08-04 21:36:55 +02:00
|
|
|
reportErr(callstack, severity, id, msg);
|
2009-07-13 15:07:26 +02:00
|
|
|
}
|
|
|
|
|
2009-08-04 21:36:55 +02:00
|
|
|
void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Severity::e severity, const std::string &id, const std::string &msg) const
|
2009-07-13 15:07:26 +02:00
|
|
|
{
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-07-13 15:07:26 +02:00
|
|
|
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
|
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = (*it)->linenr();
|
|
|
|
loc.file = tokenizer->file(*it);
|
|
|
|
|
|
|
|
locations.push_back(loc);
|
|
|
|
}
|
|
|
|
|
2009-08-04 21:36:55 +02:00
|
|
|
const ErrorLogger::ErrorMessage errmsg(locations, Severity::stringify(severity), msg, id);
|
|
|
|
|
|
|
|
if (errorLogger)
|
|
|
|
errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
std::cout << errmsg.toXML() << std::endl;
|
2009-07-13 15:07:26 +02:00
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-09-19 08:23:10 +02:00
|
|
|
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname, bool all)
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
2009-09-19 08:23:10 +02:00
|
|
|
reportErr(tok, all ? Severity::possibleError : Severity::error, "memleak", "Memory leak: " + varname);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
2009-09-19 08:23:10 +02:00
|
|
|
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname, bool all)
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
std::string errmsg("Resource leak");
|
|
|
|
if (!varname.empty())
|
|
|
|
errmsg += ": " + varname;
|
|
|
|
reportErr(tok, all ? Severity::possibleError : Severity::error, "resourceLeak", errmsg);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
2009-08-04 21:36:55 +02:00
|
|
|
reportErr(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname)
|
|
|
|
{
|
2009-08-04 21:36:55 +02:00
|
|
|
reportErr(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released");
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz)
|
|
|
|
{
|
2009-08-04 21:36:55 +02:00
|
|
|
reportErr(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching");
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
|
|
|
|
{
|
2009-08-04 21:36:55 +02:00
|
|
|
reportErr(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) const
|
|
|
|
{
|
|
|
|
// Locate the start of the function..
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
if (tok->str() == "{" || tok->str() == "}")
|
|
|
|
return No;
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
if (tok->str() == "(")
|
|
|
|
{
|
|
|
|
if (parlevel != 0)
|
|
|
|
return No;
|
|
|
|
++parlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel != 1)
|
|
|
|
return No;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2009-06-15 21:13:39 +02:00
|
|
|
|
|
|
|
// Is this the start of a function?
|
|
|
|
if (!Token::Match(tok, ") const| {"))
|
|
|
|
return No;
|
|
|
|
|
|
|
|
while (tok->str() != "{")
|
|
|
|
tok = tok->next();
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
|
|
|
|
// Inspect the statements..
|
2009-08-19 19:42:07 +02:00
|
|
|
unsigned int varid = 0;
|
2009-06-15 21:13:39 +02:00
|
|
|
AllocType allocType = No;
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
// variable declaration..
|
|
|
|
if (Token::Match(tok, "%type% * %var% ;"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(4);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-19 19:42:07 +02:00
|
|
|
if (varid == 0 && tok->varId() && Token::Match(tok, "%var% ="))
|
2009-06-15 21:13:39 +02:00
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
varid = tok->varId();
|
|
|
|
allocType = getAllocationType(tok->tokAt(2), varid);
|
2009-06-15 21:13:39 +02:00
|
|
|
if (allocType == No)
|
|
|
|
return No;
|
|
|
|
while (tok && tok->str() != ";")
|
|
|
|
tok = tok->next();
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "return")
|
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
if (varid > 0 && Token::Match(tok->next(), "%varid% ;", varid))
|
2009-06-15 21:13:39 +02:00
|
|
|
return allocType;
|
2009-10-06 22:14:32 +02:00
|
|
|
|
|
|
|
return getAllocationType(tok->next(), varid);
|
2009-06-15 21:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return No;
|
|
|
|
}
|
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
return No;
|
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-11-14 09:06:28 +01:00
|
|
|
|
|
|
|
void CheckMemoryLeakInFunction::parse_noreturn()
|
|
|
|
{
|
2009-11-15 09:53:38 +01:00
|
|
|
noreturn.insert("exit");
|
|
|
|
noreturn.insert("_exit");
|
|
|
|
noreturn.insert("_Exit");
|
|
|
|
noreturn.insert("abort");
|
|
|
|
noreturn.insert("err");
|
|
|
|
noreturn.insert("verr");
|
|
|
|
noreturn.insert("errx");
|
|
|
|
noreturn.insert("verrx");
|
|
|
|
|
2009-11-14 09:06:28 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
tok = tok->link();
|
|
|
|
if (tok->str() == "(")
|
|
|
|
{
|
|
|
|
const std::string function_name((tok->previous() && tok->previous()->isName()) ? tok->strAt(-1) : "");
|
|
|
|
|
|
|
|
tok = tok->link();
|
|
|
|
|
|
|
|
if (!function_name.empty() && Token::simpleMatch(tok, ") {"))
|
|
|
|
{
|
|
|
|
// parse this function to check if it contains an "exit" call..
|
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2, "[;{}] exit ("))
|
|
|
|
{
|
|
|
|
noreturn.insert(function_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-25 21:25:27 +02:00
|
|
|
bool CheckMemoryLeakInFunction::matchFunctionsThatReturnArg(const Token *tok, unsigned int varid) const
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
return Token::Match(tok, "; %varid% = strcat|memcpy|memmove|strcpy ( %varid% ,", varid);
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-25 21:25:27 +02:00
|
|
|
bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid, bool endpar) const
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
|
|
|
const std::string end(endpar ? " &&|)" : " [;)&|]");
|
2009-08-19 19:42:07 +02:00
|
|
|
return bool(Token::Match(tok, ("! %varid%" + end).c_str(), varid) ||
|
|
|
|
Token::Match(tok, ("! ( %varid% )" + end).c_str(), varid));
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-12 17:31:29 +02:00
|
|
|
static int countParameters(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "%var% ("))
|
|
|
|
return -1;
|
|
|
|
if (Token::Match(tok->tokAt(2), "void| )"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int numpar = 1;
|
|
|
|
int parlevel = 0;
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel <= 1)
|
|
|
|
return numpar;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (parlevel == 1 && tok->str() == ",")
|
|
|
|
++numpar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-10-15 18:52:29 +02:00
|
|
|
const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-08-28 10:22:40 +02:00
|
|
|
if (std::bsearch(tok->strAt(0), call_func_white_list,
|
2009-08-28 22:29:08 +02:00
|
|
|
sizeof(call_func_white_list) / sizeof(call_func_white_list[0]),
|
|
|
|
sizeof(call_func_white_list[0]), call_func_white_list_compare))
|
2009-02-14 07:54:23 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-11-14 09:06:28 +01:00
|
|
|
if (noreturn.find(tok->str()) != noreturn.end())
|
|
|
|
return "exit";
|
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid > 0 && (getAllocationType(tok, varid) != No || getReallocationType(tok, varid) != No || getDeallocationType(tok, varid) != No))
|
2009-01-15 21:34:39 +01:00
|
|
|
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)
|
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
if ((*it) && (*it)->str() == funcname)
|
2009-01-15 21:34:39 +01:00
|
|
|
return "recursive";
|
|
|
|
}
|
|
|
|
callstack.push_back(tok);
|
2009-06-15 17:44:59 +02:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
// lock/unlock..
|
|
|
|
if (varid == 0)
|
|
|
|
{
|
|
|
|
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
|
|
|
|
while (ftok && (ftok->str() != "{"))
|
|
|
|
ftok = ftok->next();
|
|
|
|
if (!ftok)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Token *func = getcode(ftok->tokAt(1), callstack, 0, alloctype, dealloctype, false, all, 1);
|
|
|
|
simplifycode(func, all);
|
|
|
|
const char *ret = 0;
|
|
|
|
if (Token::simpleMatch(func, "; alloc ; }"))
|
|
|
|
ret = "alloc";
|
|
|
|
else if (Token::simpleMatch(func, "; dealloc ; }"))
|
|
|
|
ret = "dealloc";
|
|
|
|
Tokenizer::deleteTokens(func);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
// Check if this is a function that allocates memory..
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
|
2009-06-15 17:44:59 +02:00
|
|
|
AllocType a = functionReturnType(ftok);
|
|
|
|
if (a != No)
|
|
|
|
return "alloc";
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-06-12 17:31:29 +02:00
|
|
|
// how many parameters is there in the function call?
|
|
|
|
int numpar = countParameters(tok);
|
|
|
|
if (numpar <= 0)
|
|
|
|
return "callfunc";
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
int par = 1;
|
|
|
|
int parlevel = 0;
|
|
|
|
|
2009-10-12 21:36:28 +02:00
|
|
|
const bool dot(tok->previous()->str() == ".");
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
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-10-04 13:10:08 +02:00
|
|
|
return (_settings && _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;
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid > 0 && Token::Match(tok, "[,()] %varid% [,()]", varid))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-12 21:36:28 +02:00
|
|
|
if (dot)
|
|
|
|
return "use";
|
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
|
2009-06-23 20:37:34 +02:00
|
|
|
if (!ftok)
|
|
|
|
return "use";
|
2009-06-12 17:31:29 +02:00
|
|
|
|
|
|
|
// how many parameters does the function want?
|
|
|
|
if (numpar != countParameters(ftok))
|
|
|
|
return "recursive";
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
const char *parname = Tokenizer::getParameterName(ftok, par);
|
|
|
|
if (! parname)
|
|
|
|
return "recursive";
|
2009-08-19 19:42:07 +02:00
|
|
|
unsigned int parameterVarid = 0;
|
|
|
|
{
|
|
|
|
const Token *partok = Token::findmatch(ftok, parname);
|
|
|
|
if (partok)
|
|
|
|
parameterVarid = partok->varId();
|
|
|
|
}
|
|
|
|
if (parameterVarid == 0)
|
|
|
|
return "recursive";
|
2009-01-15 21:34:39 +01:00
|
|
|
// Check if the function deallocates the variable..
|
|
|
|
while (ftok && (ftok->str() != "{"))
|
|
|
|
ftok = ftok->next();
|
2009-08-19 19:42:07 +02:00
|
|
|
Token *func = getcode(ftok->tokAt(1), callstack, parameterVarid, 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;
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @todo handle "goto" */
|
2009-01-15 21:34:39 +01:00
|
|
|
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-02-04 07:11:36 +01:00
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-15 18:52:29 +02:00
|
|
|
Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, CheckMemoryLeak::AllocType &alloctype, CheckMemoryLeak::AllocType &dealloctype, bool classmember, bool &all, unsigned int sz)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
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-10-03 21:46:22 +02:00
|
|
|
if (varid == 0)
|
2009-08-29 17:33:57 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
if (!callstack.empty() && Token::Match(tok, "[;{}] __cppcheck_lock|__cppcheck_unlock ( ) ;"))
|
2009-08-29 07:02:36 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
// Type of leak = Resource leak
|
|
|
|
alloctype = dealloctype = CheckMemoryLeak::File;
|
|
|
|
|
|
|
|
if (tok->next()->str() == "__cppcheck_lock")
|
|
|
|
{
|
|
|
|
addtoken("alloc");
|
|
|
|
}
|
|
|
|
else
|
2009-08-29 07:26:32 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("dealloc");
|
2009-08-29 07:26:32 +02:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
continue;
|
2009-08-29 07:02:36 +02:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
|
2009-10-04 19:30:25 +02:00
|
|
|
if (Token::simpleMatch(tok, "if ("))
|
2009-08-29 07:02:36 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("if");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
2009-08-29 07:02:36 +02:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// var = strcpy|.. ( var ,
|
|
|
|
if (Token::Match(tok, "[;{}] %varid% = memcpy|memmove|memset|strcpy|strncpy|strcat|strncat ( %varid% ,", varid))
|
2009-02-07 11:54:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
tok = tok->tokAt(4)->link();
|
|
|
|
continue;
|
2009-02-07 11:54:39 +01:00
|
|
|
}
|
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (Token::Match(tok->previous(), "[(;{}] %varid% =", varid) ||
|
|
|
|
Token::Match(tok, "asprintf ( & %varid% ,", varid))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
CheckMemoryLeak::AllocType alloc;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "asprintf ("))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
// todo: check how the return value is used.
|
|
|
|
if (!Token::Match(tok->previous(), "[;{}]"))
|
|
|
|
{
|
|
|
|
Tokenizer::deleteTokens(rethead);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
alloc = Malloc;
|
|
|
|
tok = tok->next()->link();
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
alloc = getAllocationType(tok->tokAt(2), varid);
|
|
|
|
}
|
|
|
|
bool realloc = false;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (sz > 1 &&
|
|
|
|
Token::Match(tok->tokAt(2), "malloc ( %num% )") &&
|
|
|
|
(MathLib::toLongNumber(tok->strAt(4)) % sz) != 0)
|
|
|
|
{
|
|
|
|
mismatchSizeError(tok->tokAt(4), tok->strAt(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alloc == CheckMemoryLeak::No)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
alloc = getReallocationType(tok->tokAt(2), varid);
|
|
|
|
if (alloc != CheckMemoryLeak::No)
|
2009-01-31 14:57:27 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("realloc");
|
|
|
|
addtoken(";");
|
|
|
|
realloc = true;
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-02-20 07:28:18 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
// If "--all" hasn't been given, don't check classes..
|
|
|
|
if (alloc == CheckMemoryLeak::New)
|
|
|
|
{
|
|
|
|
if (Token::Match(tok->tokAt(2), "new %type% [(;]"))
|
|
|
|
{
|
|
|
|
if (isclass(_tokenizer, tok->tokAt(3)))
|
|
|
|
{
|
|
|
|
if (_settings->_showAll)
|
2009-02-20 07:28:18 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
|
|
|
|
if (_settings->isAutoDealloc(tok->strAt(3)))
|
|
|
|
{
|
|
|
|
// This class has automatic deallocation
|
|
|
|
alloc = No;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The checking will proceed.. but any error messages that are shown are shown thanks to "--all"
|
|
|
|
all = true;
|
|
|
|
}
|
2009-02-20 07:28:18 +01:00
|
|
|
}
|
|
|
|
else
|
2009-10-03 21:46:22 +02:00
|
|
|
alloc = No;
|
2009-02-20 07:28:18 +01:00
|
|
|
}
|
2009-01-31 14:57:27 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (alloc != No)
|
|
|
|
{
|
|
|
|
if (! realloc)
|
|
|
|
addtoken("alloc");
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (alloctype != No && alloctype != alloc)
|
|
|
|
alloc = Many;
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc)
|
|
|
|
{
|
|
|
|
callstack.push_back(tok);
|
|
|
|
mismatchAllocDealloc(callstack, Token::findmatch(_tokenizer->tokens(), "%varid%", varid)->str());
|
|
|
|
callstack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
alloctype = alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (matchFunctionsThatReturnArg(tok->previous(), varid))
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("use");
|
2009-02-01 16:47:36 +01:00
|
|
|
}
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
// assignment..
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// is the pointer in rhs?
|
|
|
|
bool rhs = false;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (Token::Match(tok2, "[=+] %varid%", varid))
|
|
|
|
{
|
|
|
|
rhs = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addtoken((rhs ? "use" : "assign"));
|
|
|
|
}
|
2009-01-25 21:57:34 +01:00
|
|
|
}
|
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (Token::Match(tok->previous(), "[;{})=] %var%"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
AllocType dealloc = getDeallocationType(tok, varid);
|
|
|
|
if (dealloc != No)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("dealloc");
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (dealloctype != No && dealloctype != dealloc)
|
|
|
|
dealloc = Many;
|
|
|
|
|
|
|
|
if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
callstack.push_back(tok);
|
|
|
|
mismatchAllocDealloc(callstack, Token::findmatch(_tokenizer->tokens(), "%varid%", varid)->str());
|
|
|
|
callstack.pop_back();
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
dealloctype = dealloc;
|
|
|
|
continue;
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
// if else switch
|
|
|
|
if (tok->str() == "if")
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
if (alloctype == Fd)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
2009-10-04 07:51:12 +02:00
|
|
|
if (Token::Match(tok, "if ( 0 <= %varid% )", varid) ||
|
2009-10-03 21:46:22 +02:00
|
|
|
Token::Match(tok, "if ( %varid% != -1 )", varid))
|
|
|
|
{
|
|
|
|
addtoken("if(var)");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "if ( %varid% == -1 )", varid) ||
|
|
|
|
Token::Match(tok, "if ( %varid% < 0 )", varid))
|
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-01 16:47:36 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-03 21:46:22 +02:00
|
|
|
if (Token::Match(tok, "if ( %varid% )", varid))
|
2009-08-29 09:18:21 +02:00
|
|
|
{
|
|
|
|
addtoken("if(var)");
|
2009-10-03 21:46:22 +02:00
|
|
|
|
|
|
|
// Make sure the "use" will not be added
|
2009-08-29 09:18:21 +02:00
|
|
|
tok = tok->next()->link();
|
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varid, true))
|
2009-08-29 09:18:21 +02:00
|
|
|
{
|
|
|
|
addtoken("if(!var)");
|
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
// Check if the condition depends on var somehow..
|
|
|
|
bool dep = false;
|
|
|
|
int parlevel = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2, "close|fclose|closedir ( %varid% )", varid))
|
|
|
|
{
|
|
|
|
addtoken("dealloc");
|
|
|
|
addtoken(";");
|
|
|
|
dep = true;
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
|
|
|
if (Token::Match(tok2, ". %varid% !!.", varid))
|
|
|
|
{
|
|
|
|
dep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (alloctype == Fd && Token::Match(tok2, "%varid% !=|>=", varid))
|
|
|
|
{
|
|
|
|
dep = true;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
|
|
|
|
if (Token::Match(tok, "if ( ! %varid% &&", varid))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("if(!var)");
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else if (tok->next() &&
|
|
|
|
tok->next()->link() &&
|
|
|
|
Token::Match(tok->next()->link()->tokAt(-3), "&& ! %varid%", varid))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken("if(!var)");
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else
|
2009-08-29 09:18:21 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken((dep ? "ifv" : "if"));
|
2009-08-29 09:18:21 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-06-06 20:55:16 +02:00
|
|
|
else if ((tok->str() == "case"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("case");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
2009-06-06 20:55:16 +02:00
|
|
|
else if ((tok->str() == "default"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("default");
|
|
|
|
addtoken(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loops..
|
2009-06-06 20:55:16 +02:00
|
|
|
else if ((tok->str() == "for") || (tok->str() == "while"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-04 19:58:41 +02:00
|
|
|
if (Token::simpleMatch(tok, "while ( true )") ||
|
|
|
|
Token::simpleMatch(tok, "for ( ; ; )"))
|
2009-09-27 21:49:07 +02:00
|
|
|
{
|
|
|
|
addtoken("while1");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("loop");
|
|
|
|
isloop = true;
|
|
|
|
}
|
2009-06-06 20:55:16 +02:00
|
|
|
else if ((tok->str() == "do"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("do");
|
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid > 0 && isloop && notvar(tok, varid))
|
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("!var");
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// continue / break..
|
|
|
|
if (tok->str() == "continue")
|
2009-10-03 21:46:22 +02:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("continue");
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
|
|
|
else if (tok->str() == "break")
|
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("break");
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
|
|
|
else if (tok->str() == "goto")
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("goto");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return..
|
2009-06-06 20:55:16 +02:00
|
|
|
else if (tok->str() == "return")
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("return");
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid == 0)
|
|
|
|
{
|
|
|
|
addtoken(";");
|
|
|
|
while (tok && tok->str() != ";")
|
|
|
|
tok = tok->next();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
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();
|
2009-08-19 19:42:07 +02:00
|
|
|
if (Token::Match(tok2, "> ( %varid% )", varid))
|
2009-04-28 20:01:35 +02:00
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
tok = tok2->tokAt(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 21:44:19 +02:00
|
|
|
else if (varid && Token::Match(tok, "return &| %varid%", varid))
|
2009-04-28 20:01:35 +02:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken("use");
|
2009-04-28 20:01:35 +02:00
|
|
|
}
|
|
|
|
|
2009-08-25 21:44:19 +02:00
|
|
|
else if (varid && Token::Match(tok, "return strcpy|strncpy|memcpy ( %varid%", varid))
|
2009-07-24 21:55:35 +02:00
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-07-24 21:55:35 +02:00
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-07-24 21:55:35 +02:00
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-08-19 19:42:07 +02:00
|
|
|
if (tok2->varId() == varid)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("use");
|
2009-08-29 06:42:24 +02:00
|
|
|
tok = tok2;
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// throw..
|
2009-06-06 20:55:16 +02:00
|
|
|
else if (Token::Match(tok, "try|throw|catch"))
|
2009-01-15 21:34:39 +01:00
|
|
|
addtoken(tok->strAt(0));
|
|
|
|
|
|
|
|
// Assignment..
|
2009-08-25 21:44:19 +02:00
|
|
|
if (varid)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-08-25 21:44:19 +02:00
|
|
|
if (Token::Match(tok, "[)=] %varid% [+;)]", varid) ||
|
|
|
|
Token::Match(tok, "%varid% +=|-=", varid) ||
|
|
|
|
Token::Match(tok, "+=|<< %varid% ;", varid) ||
|
|
|
|
Token::Match(tok, "= strcpy|strcat|memmove|memcpy ( %varid% ,", varid))
|
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok->previous(), "[;{}=(,+-*/] %varid% [", varid))
|
|
|
|
{
|
|
|
|
addtoken("use_");
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Investigate function calls..
|
|
|
|
if (Token::Match(tok, "%var% ("))
|
|
|
|
{
|
2009-11-15 10:30:00 +01:00
|
|
|
// A function call should normally be followed by ";"
|
|
|
|
if (Token::simpleMatch(tok->next()->link(), ") {"))
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "if|for|while|switch"))
|
|
|
|
{
|
|
|
|
addtoken("exit");
|
|
|
|
addtoken(";");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// 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-11-15 10:30:00 +01:00
|
|
|
else if (classmember)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2009-08-19 19:42:07 +02:00
|
|
|
if (tok2->varId() == varid)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-01-26 19:15:44 +01:00
|
|
|
addtoken("::use");
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2009-11-18 17:17:37 +01:00
|
|
|
if (varid > 0 && Token::Match(tok, "%var% ( close|fclose|pclose ( %varid% ) ) ;", varid))
|
2009-10-04 13:10:08 +02:00
|
|
|
{
|
|
|
|
addtoken("dealloc");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-19 19:42:07 +02:00
|
|
|
const char *str = call_func(tok, callstack, varid, alloctype, dealloctype, all, sz);
|
2009-01-15 21:34:39 +01:00
|
|
|
if (str)
|
2009-08-02 22:30:43 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid == 0)
|
|
|
|
{
|
2009-08-05 21:18:16 +02:00
|
|
|
addtoken(str);
|
2009-10-03 21:46:22 +02:00
|
|
|
}
|
|
|
|
else if (str != std::string("alloc") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%varid% =", varid))
|
2009-08-02 22:30:43 +02:00
|
|
|
{
|
2009-10-03 21:46:22 +02:00
|
|
|
addtoken(str);
|
2009-08-02 22:30:43 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-03 21:46:22 +02:00
|
|
|
else if (varid > 0 &&
|
|
|
|
getReallocationType(tok, varid) != No &&
|
|
|
|
tok->tokAt(2)->varId() == varid)
|
|
|
|
{
|
|
|
|
addtoken("if");
|
|
|
|
addtoken("{");
|
|
|
|
addtoken("dealloc");
|
|
|
|
addtoken(";");
|
|
|
|
addtoken("}");
|
|
|
|
tok = tok->next()->link();
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback..
|
2009-07-20 22:24:23 +02:00
|
|
|
bool matchFirst = Token::Match(tok, "( %var%");
|
|
|
|
if (matchFirst || Token::Match(tok, "( * %var%"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
int tokIdx = matchFirst ? 2 : 3;
|
|
|
|
|
2009-07-27 11:25:41 +02:00
|
|
|
while (Token::Match(tok->tokAt(tokIdx), ". %var%"))
|
2009-01-15 21:34:39 +01:00
|
|
|
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;
|
2009-08-19 19:42:07 +02:00
|
|
|
else if (tok2->varId() == varid)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("use");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linux lists..
|
2009-10-03 21:46:22 +02:00
|
|
|
if (varid > 0 && Token::Match(tok, "[=(,] & %varid% [.[,)]", varid))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
addtoken("&use");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rethead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckMemoryLeakInFunction::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");
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduce the code..
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
2009-06-20 13:58:30 +02:00
|
|
|
//tok->printOut("simplifycode loop..");
|
2009-01-15 21:34:39 +01:00
|
|
|
done = true;
|
|
|
|
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL)
|
|
|
|
{
|
|
|
|
// Delete extra ";"
|
|
|
|
while (Token::Match(tok2, "[;{}] ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
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(";");
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2->next(), tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete braces around a single instruction..
|
|
|
|
if (Token::Match(tok2->next(), "{ %var% ; }"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-06-14 08:20:51 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2->next(), "{ %var% %var% ; }"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-06-14 08:12:04 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-23 22:42:07 +02:00
|
|
|
// reduce "; callfunc ; %var%"
|
|
|
|
else if (Token::Match(tok2, "; callfunc ; %type%"))
|
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if if|callfunc" => "if"
|
2009-06-16 19:05:05 +02:00
|
|
|
else if (Token::Match(tok2, "if if|callfunc"))
|
|
|
|
{
|
2009-09-23 22:42:07 +02:00
|
|
|
tok2->deleteNext();
|
2009-06-16 19:05:05 +02:00
|
|
|
done = false;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-07-27 11:24:24 +02:00
|
|
|
else if (tok2->next() && tok2->next()->str() == "if")
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// Delete empty if that is not followed by an else
|
|
|
|
if (Token::Match(tok2->next(), "if ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-26 07:19:22 +02:00
|
|
|
// Reduce "if X ; else X ;" => "X ;"
|
|
|
|
else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") &&
|
|
|
|
std::string(tok2->strAt(2)) == std::string(tok2->strAt(5)))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(5));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// 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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok2, "; if ; else assign|use ; assign|use") ||
|
|
|
|
Token::Match(tok2, "; if assign|use ; else ; assign|use"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
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-11-13 20:22:28 +01:00
|
|
|
if (_settings->_showAll && tok2->tokAt(2)->str() != "assign" && !Token::simpleMatch(tok2->tokAt(2), "dealloc ; dealloc"))
|
2009-02-09 21:16:00 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(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-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-02-09 21:16:00 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if return ; alloc ;" => "alloc ;"
|
2009-09-01 20:12:53 +02:00
|
|
|
else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
|
|
|
|
else if (Token::Match(tok2, "[;{}] if alloc ; else return ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2)); // Remove "if"
|
|
|
|
Token::eraseTokens(tok2->next(), tok2->tokAt(5)); // Remove "; else return"
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if ; else %var% ;" => "if %var% ;"
|
|
|
|
else if (Token::Match(tok2->next(), "if ; else %var% ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2->next(), tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-08-29 16:27:16 +02:00
|
|
|
// Reduce "if ; else" => "if"
|
|
|
|
else if (Token::simpleMatch(tok2->next(), "if ; else"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2->next(), tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-01 20:02:28 +02:00
|
|
|
// Delete "if { dealloc|assign|use ; return ; }"
|
|
|
|
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(8));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-02-08 19:27:09 +01:00
|
|
|
// Remove "if { dealloc ; callfunc ; } !!else"
|
2009-06-23 20:40:27 +02:00
|
|
|
else if (Token::Match(tok2->next(), "if { dealloc|assign ; callfunc ; } !!else"))
|
2009-02-08 19:27:09 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(8));
|
2009-02-08 19:27:09 +01:00
|
|
|
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-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(8));
|
2009-01-15 21:34:39 +01:00
|
|
|
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"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;"
|
|
|
|
if (Token::Match(tok2, "; if(!var) alloc ; !!else"))
|
|
|
|
{
|
|
|
|
// Remove the "if(!var)"
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// Insert "dealloc ;" before the "alloc ;"
|
|
|
|
tok2->insertToken(";");
|
|
|
|
tok2->insertToken("dealloc");
|
|
|
|
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-08-29 16:03:23 +02:00
|
|
|
// Reduce "; if(!var) exit ;" => ";"
|
|
|
|
if (Token::Match(tok2, "; if(!var) exit ;"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Remove "catch ;"
|
|
|
|
if (Token::simpleMatch(tok2->next(), "catch ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-08-29 16:27:16 +02:00
|
|
|
// Reduce "if* ;"..
|
|
|
|
if (Token::Match(tok2->next(), "if(var)|if(!var)|ifv ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-08-29 16:27:16 +02:00
|
|
|
// Followed by else..
|
|
|
|
if (Token::simpleMatch(tok2->tokAt(3), "else"))
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (tok2->str() == "if(var)")
|
|
|
|
tok2->str("if(!var)");
|
|
|
|
else if (tok2->str() == "if(!var)")
|
|
|
|
tok2->str("if(var)");
|
|
|
|
|
|
|
|
// remove the "; else"
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remove the "if* ;"
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-27 21:49:07 +02:00
|
|
|
// Reduce "while1 ;" => "use ;"
|
|
|
|
if (Token::simpleMatch(tok2, "while1 ;"))
|
|
|
|
{
|
|
|
|
tok2->str("use");
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Delete if block: "alloc; if return use ;"
|
|
|
|
if (Token::Match(tok2, "alloc ; if return use ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(5));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-22 18:09:29 +02:00
|
|
|
// Reduce "alloc|dealloc|use|callfunc ; exit ;" => "; exit ;"
|
2009-08-04 21:34:14 +02:00
|
|
|
if (Token::Match(tok2, "alloc|dealloc|use|callfunc ; exit ;"))
|
2009-07-23 16:30:30 +02:00
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-22 18:09:29 +02:00
|
|
|
// Reduce "if loop ; exit ;" => "; exit ;"
|
|
|
|
if (Token::Match(tok2, "if loop ; exit ;"))
|
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
tok2->deleteThis();
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-07-23 16:30:30 +02:00
|
|
|
// Reduce "alloc|dealloc|use ; if(var) exit ;"
|
|
|
|
if (Token::Match(tok2, "alloc|dealloc|use ; if(var) exit ;"))
|
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "if exit ;"
|
|
|
|
if (Token::simpleMatch(tok2, "if exit ;"))
|
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
tok2->deleteThis();
|
|
|
|
done = false;
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2009-06-14 08:12:04 +02:00
|
|
|
tok2 = tok2->tokAt(3);
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "do { alloc ; } " => "alloc ;"
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @todo If the loop "do { alloc ; }" can be executed twice, reduce it to "loop alloc ;" */
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "do { alloc ; }"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-06-14 08:20:51 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-26 07:19:22 +02:00
|
|
|
// Reduce "loop break ; => ";"
|
|
|
|
if (Token::Match(tok2->next(), "loop break|continue ;"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Reduce "loop if break ; => ";"
|
|
|
|
if (Token::Match(tok2->next(), "loop if break|continue ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
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 {"
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
// erase "if break|continue ; }"
|
2009-06-14 08:09:02 +02:00
|
|
|
tok2 = tok2->tokAt(4);
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(5));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "loop { X ; break ; }" with "X ;"
|
|
|
|
if (Token::Match(tok2->next(), "loop { %var% ; break ; }"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-06-14 08:20:51 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(6));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-15 22:26:38 +02:00
|
|
|
// Replace "do ; loop ;" with ";"
|
|
|
|
if (Token::Match(tok2->next(), "%any% ; loop ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-09-15 22:26:38 +02:00
|
|
|
if (tok2->next()->str() == "do")
|
|
|
|
tok2->deleteNext();
|
|
|
|
else
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-09-15 22:26:38 +02:00
|
|
|
// Replace "do ; loop !var ;" with ";"
|
|
|
|
if (Token::Match(tok2->next(), "%any% ; loop !var ;"))
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-09-15 22:26:38 +02:00
|
|
|
if (tok2->next()->str() == "do")
|
|
|
|
tok2->deleteNext();
|
|
|
|
else
|
|
|
|
tok2 = tok2->next();
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-17 08:55:40 +01:00
|
|
|
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 ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete if block: "alloc; if return use ;"
|
|
|
|
if (Token::Match(tok2, "alloc ; if return use ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(5));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "[;{}] return ; %var%" => "[;{}] return ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] return ; %var%"))
|
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] return use ; %var%"))
|
|
|
|
{
|
2009-06-14 08:12:04 +02:00
|
|
|
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(var) return use ;" => "return use ;"
|
|
|
|
if (Token::Match(tok2->next(), "if(var) return use ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "if(var) assign|dealloc|use ;" => "assign|dealloc|use ;"
|
|
|
|
if (Token::Match(tok2->next(), "if(var) assign|dealloc|use ; !!else"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
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-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2->next(), tok2->tokAt(6));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete second use in "use ; use ;"
|
|
|
|
while (Token::Match(tok2, "[;{}] use ; use ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first part in "use ; dealloc ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] use ; dealloc ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete first part in "use ; return use ;"
|
|
|
|
if (Token::Match(tok2, "[;{}] use ; return use ;"))
|
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2009-06-21 21:03:58 +02:00
|
|
|
// Delete "callfunc ;" that is followed by "use|if|callfunc"
|
|
|
|
// If the function doesn't throw exception or exit the application, then the "callfunc" is not needed
|
|
|
|
if (Token::Match(tok2, "callfunc ; use|if|callfunc"))
|
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
done = false;
|
|
|
|
}
|
2009-06-21 14:12:59 +02:00
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// 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
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
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-07-27 11:24:24 +02:00
|
|
|
else if (incase && _tok->str() == "case")
|
2009-01-15 21:34:39 +01:00
|
|
|
break;
|
|
|
|
|
2009-07-27 11:24:24 +02:00
|
|
|
incase |= (_tok->str() == "case");
|
|
|
|
incase &= (_tok->str() != "break");
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!incase && valid)
|
|
|
|
{
|
|
|
|
done = false;
|
|
|
|
tok2->str(";");
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
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-07-27 11:24:24 +02:00
|
|
|
bool def(tok2->str() == "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(";");
|
2009-06-14 08:20:51 +02:00
|
|
|
tok2 = tok2->tokAt(2);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-23 16:30:30 +02:00
|
|
|
|
2009-09-23 22:42:07 +02:00
|
|
|
// If "--all" is given, remove all "callfunc"..
|
|
|
|
if (done && _settings->_showAll)
|
|
|
|
{
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "callfunc")
|
|
|
|
{
|
|
|
|
tok2->deleteThis();
|
|
|
|
all = true;
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-23 15:48:25 +02:00
|
|
|
const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens, bool all)
|
|
|
|
{
|
2009-10-08 22:30:33 +02:00
|
|
|
const Token *result;
|
2009-08-23 15:48:25 +02:00
|
|
|
|
|
|
|
if ((result = Token::findmatch(tokens, "loop alloc ;")) != NULL)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-11-15 10:30:00 +01:00
|
|
|
if (Token::Match(tokens, "alloc ; if|if(var)|ifv break|continue|return ;"))
|
|
|
|
{
|
|
|
|
return tokens->tokAt(3);
|
|
|
|
}
|
|
|
|
|
2009-10-27 23:08:11 +01:00
|
|
|
if ((result = Token::findmatch(tokens, "alloc ; if|if(var)|ifv return ;")) != NULL)
|
2009-08-23 15:48:25 +02:00
|
|
|
{
|
|
|
|
return result->tokAt(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all && (result = Token::findmatch(tokens, "alloc ; ifv break|continue|return ;")) != NULL)
|
|
|
|
{
|
|
|
|
return result->tokAt(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((result = Token::findmatch(tokens, "alloc ; alloc|assign|return callfunc| ;")) != NULL)
|
|
|
|
{
|
|
|
|
return result->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2009-09-01 19:33:17 +02:00
|
|
|
if ((result = Token::findmatch(tokens, "alloc ; if assign ;")) != NULL)
|
|
|
|
{
|
|
|
|
return result->tokAt(3);
|
|
|
|
}
|
|
|
|
|
2009-08-23 15:48:25 +02:00
|
|
|
if ((result = Token::findmatch(tokens, "alloc ; }")) != NULL)
|
|
|
|
{
|
|
|
|
if (result->tokAt(3) == NULL)
|
|
|
|
return result->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No deallocation / usage => report leak at the last token
|
|
|
|
if (!Token::findmatch(tokens, "dealloc|use"))
|
|
|
|
{
|
|
|
|
const Token *last = tokens;
|
|
|
|
while (last->next())
|
|
|
|
last = last->next();
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Check for memory leaks for a function variable.
|
2009-09-10 21:22:57 +02:00
|
|
|
void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string &varname, unsigned int varid, 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-08-19 19:42:07 +02:00
|
|
|
Token *tok = getcode(Tok1, callstack, varid, alloctype, dealloctype, classmember, all, sz);
|
2009-08-02 22:30:43 +02:00
|
|
|
//tok->printOut((std::string("Checkmemoryleak: getcode result for: ") + varname).c_str());
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// Simplify the code and check if freed memory is used..
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
while (Token::Match(tok2, "[;{}] ;"))
|
2009-06-08 20:20:43 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(2));
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-07-21 13:04:13 +02:00
|
|
|
if ((result = Token::findmatch(tok, "[;{}] dealloc ; use_ ;")) != NULL)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-05-30 20:30:44 +02:00
|
|
|
deallocuseError(result->tokAt(3), 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-10-08 20:49:27 +02:00
|
|
|
// If the variable is not allocated at all => no memory leak
|
|
|
|
if (Token::findmatch(tok, "alloc") == 0)
|
|
|
|
{
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:16:00 +01:00
|
|
|
simplifycode(tok, all);
|
2009-08-02 21:23:47 +02:00
|
|
|
|
2009-09-01 19:54:40 +02:00
|
|
|
if (_settings->_debug && _settings->_verbose)
|
2009-08-02 21:23:47 +02:00
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
tok->printOut(("Checkmemoryleak: simplifycode result for: " + varname).c_str());
|
2009-08-02 21:23:47 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @todo handle "goto" */
|
2009-01-15 21:34:39 +01:00
|
|
|
if (Token::findmatch(tok, "goto"))
|
|
|
|
{
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-23 15:48:25 +02:00
|
|
|
if ((result = findleak(tok, _settings->_showAll)) != NULL)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
memoryLeak(result, 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks inside function..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
void CheckMemoryLeakInFunction::check()
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-11-14 09:06:28 +01:00
|
|
|
// Parse the tokens and fill the "noreturn"
|
|
|
|
parse_noreturn();
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
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;
|
|
|
|
|
2009-07-31 23:42:21 +02:00
|
|
|
// Skip these weird blocks... "( { ... } )"
|
|
|
|
else if (Token::simpleMatch(tok, "( {"))
|
|
|
|
{
|
|
|
|
tok = tok->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// In function..
|
|
|
|
if (indentlevel == 0)
|
|
|
|
{
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
2009-10-03 21:46:22 +02:00
|
|
|
{
|
2009-01-15 21:34:39 +01:00
|
|
|
infunc = true;
|
2009-10-03 21:46:22 +02:00
|
|
|
checkScope(tok->tokAt(2), "", 0, classmember, 1);
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
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-10-07 09:54:34 +02:00
|
|
|
unsigned int sz = _tokenizer->sizeOfType(tok->tokAt(1));
|
2009-02-07 11:54:39 +01:00
|
|
|
if (sz < 1)
|
|
|
|
sz = 1;
|
|
|
|
|
2009-07-06 12:20:13 +02:00
|
|
|
if (!Token::Match(tok, "[{};] %type%"))
|
|
|
|
continue;
|
|
|
|
|
2009-10-04 14:24:41 +02:00
|
|
|
// Don't check static/extern variables
|
|
|
|
if (Token::Match(tok->next(), "static|extern"))
|
2009-07-06 12:20:13 +02:00
|
|
|
continue;
|
|
|
|
|
2009-08-29 15:20:37 +02:00
|
|
|
// return/else is not part of a variable declaration..
|
|
|
|
if (Token::Match(tok->next(), "return|else"))
|
|
|
|
continue;
|
|
|
|
|
2009-06-14 19:32:34 +02:00
|
|
|
if (Token::Match(tok, "[{};] %type% * const| %var% [;=]"))
|
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
const Token *vartok = tok->tokAt(tok->tokAt(3)->str() != "const" ? 3 : 4);
|
|
|
|
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
|
2009-06-14 19:32:34 +02:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-06-14 19:32:34 +02:00
|
|
|
else if (Token::Match(tok, "[{};] %type% %type% * const| %var% [;=]"))
|
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
const Token *vartok = tok->tokAt(tok->tokAt(4)->str() != "const" ? 4 : 5);
|
|
|
|
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
|
2009-06-14 19:32:34 +02:00
|
|
|
}
|
2009-06-21 17:01:43 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tok, "[{};] int %var% [;=]"))
|
|
|
|
{
|
2009-08-19 19:42:07 +02:00
|
|
|
const Token *vartok = tok->tokAt(2);
|
|
|
|
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
|
2009-06-21 17:01:43 +02:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks in classes..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
void CheckMemoryLeakInClass::check()
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
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));
|
2009-06-08 20:20:43 +02:00
|
|
|
parseClass(tok, classname);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<const char *> &classname)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2009-10-06 18:25:00 +02:00
|
|
|
// skip static variables..
|
|
|
|
if (Token::Match(tok, "static %type% * %var% ;"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(4);
|
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Declaring subclass.. recursive checking
|
|
|
|
if (Token::Match(tok, "class %var% [{:]"))
|
|
|
|
{
|
|
|
|
classname.push_back(tok->strAt(1));
|
2009-06-08 20:20:43 +02:00
|
|
|
parseClass(tok, classname);
|
2009-01-15 21:34:39 +01:00
|
|
|
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-06-08 20:20:43 +02:00
|
|
|
if (_settings->_showAll || !isclass(_tokenizer, tok->tokAt(1)))
|
|
|
|
variable(classname.back(), tok->tokAt(3));
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVarname)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-09-12 21:13:47 +02:00
|
|
|
if (!_settings->_showAll)
|
|
|
|
return;
|
|
|
|
|
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..
|
2009-06-08 20:20:43 +02:00
|
|
|
CheckMemoryLeak::AllocType Alloc = CheckMemoryLeak::No;
|
|
|
|
CheckMemoryLeak::AllocType Dealloc = CheckMemoryLeak::No;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-10-23 20:04:47 +02:00
|
|
|
bool allocInConstructor = false;
|
|
|
|
bool deallocInDestructor = false;
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
// Loop through all tokens. Inspect member functions
|
2009-03-14 18:21:37 +01:00
|
|
|
int indent_ = 0;
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token *functionToken = Tokenizer::findClassFunction(_tokenizer->tokens(), classname, "~| %var%", indent_);
|
2009-03-14 18:21:37 +01:00
|
|
|
while (functionToken)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-10-23 20:04:47 +02:00
|
|
|
const bool constructor(Token::Match(functionToken, (classname + std::string(" :: ") + classname + " (").c_str()));
|
2009-10-22 21:51:58 +02:00
|
|
|
const bool destructor(functionToken->tokAt(2)->str() == "~");
|
|
|
|
|
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-08-19 19:42:07 +02:00
|
|
|
AllocType alloc = getAllocationType(tok->tokAt((indent > 0) ? 2 : 3), 0);
|
2009-06-08 20:20:43 +02:00
|
|
|
if (alloc != CheckMemoryLeak::No)
|
2009-03-14 18:21:37 +01:00
|
|
|
{
|
2009-10-23 20:04:47 +02:00
|
|
|
if (constructor)
|
|
|
|
allocInConstructor = true;
|
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
if (Alloc != No && Alloc != alloc)
|
2009-06-08 20:20:43 +02:00
|
|
|
alloc = CheckMemoryLeak::Many;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-03-14 18:21:37 +01:00
|
|
|
std::list<const Token *> callstack;
|
2009-06-08 20:20:43 +02:00
|
|
|
if (alloc != CheckMemoryLeak::Many && Dealloc != CheckMemoryLeak::No && Dealloc != CheckMemoryLeak::Many && Dealloc != alloc)
|
2009-03-14 18:21:37 +01:00
|
|
|
{
|
|
|
|
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;
|
2009-07-05 22:16:43 +02:00
|
|
|
AllocType dealloc = getDeallocationType(tok, varnames);
|
2009-03-14 18:21:37 +01:00
|
|
|
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;
|
2009-07-05 22:16:43 +02:00
|
|
|
dealloc = getDeallocationType(tok, varnames);
|
2009-03-14 18:21:37 +01:00
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
if (dealloc != CheckMemoryLeak::No)
|
2009-03-14 18:21:37 +01:00
|
|
|
{
|
2009-10-23 20:04:47 +02:00
|
|
|
if (destructor)
|
|
|
|
deallocInDestructor = true;
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
if (Dealloc != CheckMemoryLeak::No && Dealloc != dealloc)
|
|
|
|
dealloc = CheckMemoryLeak::Many;
|
2009-02-06 07:11:47 +01:00
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
std::list<const Token *> callstack;
|
2009-06-08 20:20:43 +02:00
|
|
|
if (dealloc != CheckMemoryLeak::Many && Alloc != CheckMemoryLeak::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-10-24 15:07:14 +02:00
|
|
|
|
|
|
|
// Function call in destructor .. possible deallocation
|
|
|
|
else if (destructor && Token::Match(tok->previous(), "[{};] %var% ("))
|
|
|
|
{
|
|
|
|
if (!std::bsearch(tok->strAt(0), call_func_white_list,
|
|
|
|
sizeof(call_func_white_list) / sizeof(call_func_white_list[0]),
|
|
|
|
sizeof(call_func_white_list[0]), call_func_white_list_compare))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 20:24:52 +01:00
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
2009-03-14 18:21:37 +01:00
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
functionToken = Tokenizer::findClassFunction(functionToken->next(), classname, "~| %var%", indent_);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-10-23 20:04:47 +02:00
|
|
|
if (allocInConstructor && !deallocInDestructor)
|
|
|
|
{
|
|
|
|
memoryLeak(tokVarname, (std::string(classname) + "::" + varname).c_str(), Alloc, true);
|
|
|
|
}
|
|
|
|
else if (Alloc != CheckMemoryLeak::No && Dealloc == CheckMemoryLeak::No)
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
memoryLeak(tokVarname, (std::string(classname) + "::" + varname).c_str(), Alloc, true);
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
void CheckMemoryLeakStructMember::check()
|
|
|
|
{
|
2009-07-22 08:30:51 +02:00
|
|
|
// This should be in the CheckMemoryLeak base class
|
|
|
|
std::set<std::string> ignoredFunctions;
|
|
|
|
ignoredFunctions.insert("if");
|
|
|
|
ignoredFunctions.insert("for");
|
|
|
|
ignoredFunctions.insert("while");
|
|
|
|
ignoredFunctions.insert("malloc");
|
|
|
|
|
2009-09-28 22:58:06 +02:00
|
|
|
|
|
|
|
unsigned int indentlevel1 = 0;
|
2009-07-19 16:51:31 +02:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
2009-09-28 22:58:06 +02:00
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel1;
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel1;
|
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
// Locate struct variables..
|
2009-09-28 22:58:06 +02:00
|
|
|
if (indentlevel1 > 0 && Token::Match(tok, "struct|;|{|} %type% * %var% [=;]"))
|
2009-07-19 16:51:31 +02:00
|
|
|
{
|
2009-07-29 11:38:20 +02:00
|
|
|
const Token * const vartok = tok->tokAt(3);
|
2009-07-19 16:51:31 +02:00
|
|
|
if (vartok->varId() == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that struct is allocated..
|
|
|
|
{
|
|
|
|
const unsigned int varid(vartok->varId());
|
|
|
|
bool alloc = false;
|
|
|
|
unsigned int indentlevel2 = 0;
|
|
|
|
for (const Token *tok2 = vartok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel2;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel2 == 0)
|
|
|
|
break;
|
|
|
|
--indentlevel2;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "= %varid% [;=]", varid))
|
|
|
|
{
|
|
|
|
alloc = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", varid))
|
|
|
|
{
|
|
|
|
alloc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!alloc)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check struct..
|
|
|
|
unsigned int indentlevel2 = 0;
|
2009-07-20 14:39:24 +02:00
|
|
|
for (const Token *tok2 = vartok; tok2; tok2 = tok2->next())
|
2009-07-19 16:51:31 +02:00
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel2;
|
|
|
|
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel2 == 0)
|
|
|
|
break;
|
|
|
|
--indentlevel2;
|
|
|
|
}
|
|
|
|
|
2009-07-29 11:38:20 +02:00
|
|
|
// Unknown usage of struct
|
|
|
|
/** @todo Check how the struct is used. Only bail out if necessary */
|
|
|
|
else if (Token::Match(tok2, "[(,] %varid% [,)]", vartok->varId()))
|
|
|
|
break;
|
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
// Struct member is allocated => check if it is also properly deallocated..
|
|
|
|
else if (Token::Match(tok2, "%varid% . %var% = malloc|strdup|kmalloc (", vartok->varId()))
|
|
|
|
{
|
|
|
|
const unsigned int structid(vartok->varId());
|
|
|
|
const unsigned int structmemberid(tok2->tokAt(2)->varId());
|
|
|
|
|
|
|
|
// This struct member is allocated.. check that it is deallocated
|
|
|
|
unsigned int indentlevel3 = indentlevel2;
|
|
|
|
for (const Token *tok3 = tok2; tok3; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
++indentlevel3;
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel3 == 0)
|
|
|
|
{
|
|
|
|
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--indentlevel3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deallocating the struct member..
|
|
|
|
else if (Token::Match(tok3, "free|kfree ( %var% . %varid% )", structmemberid))
|
2009-07-20 14:39:24 +02:00
|
|
|
{
|
|
|
|
// If the deallocation happens at the base level, don't check this member anymore
|
|
|
|
if (indentlevel3 == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// deallocating and then returning from function in a conditional block =>
|
|
|
|
// skip ahead out of the block
|
|
|
|
bool ret = false;
|
|
|
|
while (tok3)
|
|
|
|
{
|
|
|
|
// debug info
|
|
|
|
const std::string tok3str_(tok3->str());
|
|
|
|
if (tok3->str() == "return")
|
|
|
|
ret = true;
|
|
|
|
else if (tok3->str() == "{" || tok3->str() == "}")
|
|
|
|
break;
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
|
|
|
if (!ret || !tok3 || tok3->str() != "}")
|
|
|
|
break;
|
|
|
|
--indentlevel3;
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-19 16:51:31 +02:00
|
|
|
|
|
|
|
// Deallocating the struct..
|
2009-07-24 09:05:40 +02:00
|
|
|
else if (indentlevel2 == 0 && Token::Match(tok3, "free|kfree ( %varid% )", structid))
|
2009-07-19 16:51:31 +02:00
|
|
|
{
|
|
|
|
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed allocation => skip code..
|
|
|
|
else if (Token::Match(tok3, "if ( ! %var% . %varid% )", structmemberid))
|
|
|
|
{
|
|
|
|
// Goto the ")"
|
2009-08-17 22:23:37 +02:00
|
|
|
tok3 = tok3->next()->link();
|
2009-07-19 16:51:31 +02:00
|
|
|
|
2009-08-17 22:23:37 +02:00
|
|
|
// make sure we have ") {".. it should be
|
|
|
|
if (!Token::simpleMatch(tok3, ") {"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Goto the "}"
|
|
|
|
tok3 = tok3->next()->link();
|
2009-07-19 16:51:31 +02:00
|
|
|
}
|
|
|
|
|
2009-09-28 22:41:45 +02:00
|
|
|
// succeeded allocation
|
|
|
|
else if (Token::Match(tok3, "if ( %var% . %varid% ) {", structmemberid))
|
|
|
|
{
|
|
|
|
// goto the ")"
|
|
|
|
tok3 = tok3->next()->link();
|
|
|
|
|
|
|
|
// check if the variable is deallocated or returned..
|
|
|
|
unsigned int indentlevel4 = 0;
|
|
|
|
for (const Token *tok4 = tok3; tok4; tok4 = tok4->next())
|
|
|
|
{
|
|
|
|
if (tok4->str() == "{")
|
|
|
|
++indentlevel4;
|
|
|
|
else if (tok4->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel4;
|
|
|
|
if (indentlevel4 == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok4, "free|kfree ( %var% . %varid% )", structmemberid))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// was there a proper deallocation?
|
|
|
|
if (indentlevel4 > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
// Returning from function..
|
|
|
|
else if (tok3->str() == "return")
|
|
|
|
{
|
|
|
|
// Returning from function without deallocating struct member?
|
|
|
|
if (!Token::Match(tok3, "return %varid% ;", structid))
|
|
|
|
{
|
|
|
|
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// goto isn't handled well.. bail out even though there might be leaks
|
|
|
|
else if (tok3->str() == "goto")
|
|
|
|
break;
|
2009-07-22 08:30:51 +02:00
|
|
|
|
|
|
|
// using struct in a function call..
|
|
|
|
else if (Token::Match(tok3, "%var% ("))
|
|
|
|
{
|
|
|
|
// Calling non-function / function that doesn't deallocate?
|
|
|
|
if (ignoredFunctions.find(tok3->str()) != ignoredFunctions.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if the struct is used..
|
|
|
|
bool deallocated = false;
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
for (const Token *tok4 = tok3; tok4; tok4 = tok4->next())
|
|
|
|
{
|
|
|
|
if (tok4->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
|
|
|
|
else if (tok4->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel <= 1)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok4, "[(,] %varid% [,)]", structid))
|
|
|
|
{
|
|
|
|
/** @todo check if the function deallocates the memory */
|
|
|
|
deallocated = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deallocated)
|
|
|
|
break;
|
|
|
|
}
|
2009-07-19 16:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-03-21 17:58:13 +01:00
|
|
|
|
|
|
|
|