2008-11-07 17:25:29 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef CheckMemoryLeakH
|
|
|
|
#define CheckMemoryLeakH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** \brief Check for memory leaks */
|
2008-11-22 21:00:36 +01:00
|
|
|
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "errorlogger.h"
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class CheckMemoryLeakClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CheckMemoryLeakClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger );
|
|
|
|
~CheckMemoryLeakClass();
|
|
|
|
void CheckMemoryLeak();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum AllocType { No, Malloc, gMalloc, New, NewA, FOPEN, POPEN };
|
|
|
|
|
|
|
|
// Extra allocation..
|
|
|
|
class AllocFunc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char *funcname;
|
|
|
|
AllocType alloctype;
|
|
|
|
|
|
|
|
AllocFunc(const char f[], AllocType a)
|
|
|
|
{
|
|
|
|
funcname = f;
|
|
|
|
alloctype = a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void CheckMemoryLeak_ClassMembers_Variable( const std::vector<const char *> &classname, const char varname[] );
|
|
|
|
void CheckMemoryLeak_ClassMembers_ParseClass( const TOKEN *tok1, std::vector<const char *> &classname );
|
|
|
|
void CheckMemoryLeak_ClassMembers();
|
|
|
|
void CheckMemoryLeak_InFunction();
|
|
|
|
void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] );
|
2008-12-01 23:33:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify code e.g. by replacing empty "{ }" with ";"
|
|
|
|
* @param tok first token. The tokens list can be modified.
|
|
|
|
*/
|
|
|
|
void simplifycode(TOKEN *tok);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete tokens between begin and end. E.g. if begin = 1
|
|
|
|
* and end = 5, tokens 2,3 and 4 would be erased.
|
|
|
|
*
|
|
|
|
* @param begin Tokens after this will be erased.
|
|
|
|
* @param end Tokens before this will be erased.
|
|
|
|
*/
|
2008-11-22 21:00:36 +01:00
|
|
|
void erase(TOKEN *begin, const TOKEN *end);
|
2008-12-01 23:33:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract a new tokens list that is easier to parse than the "tokens"
|
|
|
|
* @param tok start parse token
|
|
|
|
* @param callstack callstack
|
|
|
|
* @param varname name of variable
|
|
|
|
* @param alloctype
|
|
|
|
* @param dealloctype
|
|
|
|
* @return Newly allocated token array. Caller needs to release reserved
|
|
|
|
* memory by calling Tokenizer::deleteTokens(returnValue);
|
|
|
|
*/
|
2008-11-22 21:00:36 +01:00
|
|
|
TOKEN *getcode(const TOKEN *tok, std::list<const TOKEN *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype);
|
|
|
|
bool notvar(const TOKEN *tok, const char *varnames[]);
|
|
|
|
void instoken(TOKEN *tok, const char str[]);
|
|
|
|
void MemoryLeak( const TOKEN *tok, const char varname[] );
|
|
|
|
void MismatchError( const TOKEN *Tok1, const std::list<const TOKEN *> &callstack, const char varname[] );
|
|
|
|
const char * call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype );
|
|
|
|
AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[]);
|
|
|
|
AllocType GetAllocationType( const TOKEN *tok2 );
|
2008-12-06 14:33:22 +01:00
|
|
|
AllocType GetReallocationType( const TOKEN *tok2 );
|
2008-11-26 08:23:54 +01:00
|
|
|
bool isclass( const TOKEN *typestr );
|
2008-11-22 21:00:36 +01:00
|
|
|
|
|
|
|
const Tokenizer *_tokenizer;
|
|
|
|
ErrorLogger *_errorLogger;
|
2008-12-01 08:50:36 +01:00
|
|
|
const Settings _settings;
|
2008-11-23 12:08:07 +01:00
|
|
|
std::list<AllocFunc> _listAllocFunc;
|
2008-11-11 07:42:09 +01:00
|
|
|
};
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|