cppcheck/src/checkmemoryleak.h

189 lines
6.3 KiB
C
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
2008-12-18 22:28:57 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
//---------------------------------------------------------------------------
#ifndef checkmemoryleakH
#define checkmemoryleakH
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
/** \brief Check for memory leaks */
2009-03-20 18:16:21 +01:00
#include "check.h"
2008-12-18 22:28:57 +01:00
#include <list>
2009-03-20 18:16:21 +01:00
#include <string>
2008-12-18 22:28:57 +01:00
#include <vector>
2009-03-20 18:16:21 +01:00
class Token;
/** Base class for memory leaks checking */
class CheckMemoryLeak
{
protected:
CheckMemoryLeak() { }
/** What type of allocation are used.. the "Many" means that several types of allocation and deallocation are used */
enum AllocType { No, Malloc, gMalloc, New, NewArray, File, Pipe, Dir, Many };
void MemoryLeak(const Token *tok, const char varname[], AllocType alloctype, bool all);
void MismatchError(const Token *Tok1, const std::list<const Token *> &callstack, const char varname[]);
AllocType GetDeallocationType(const Token *tok, const char *varnames[]);
AllocType GetAllocationType(const Token *tok2);
AllocType GetReallocationType(const Token *tok2);
bool isclass(const Tokenizer *_tokenizer, const Token *typestr) const;
void memleakError(const Token *tok, const std::string &varname);
void memleakallError(const Token *tok, const std::string &varname);
void resourceLeakError(const Token *tok, const std::string &varname);
void deallocDeallocError(const Token *tok, const std::string &varname);
void deallocuseError(const Token *tok, const std::string &varname);
void mismatchSizeError(const Token *tok, const std::string &sz);
void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname);
// error message
virtual void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg) = 0;
virtual void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg) = 0;
};
class CheckMemoryLeakInFunction : public CheckMemoryLeak, public Check
2008-12-18 22:28:57 +01:00
{
public:
CheckMemoryLeakInFunction() : Check()
2009-03-20 18:16:21 +01:00
{ }
CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
2009-03-20 18:16:21 +01:00
: Check(tokenizer, settings, errorLogger)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
2009-03-20 18:16:21 +01:00
{
CheckMemoryLeakInFunction checkMemoryLeak(tokenizer, settings, errorLogger);
checkMemoryLeak.check();
2009-03-20 18:16:21 +01:00
}
#ifndef UNIT_TESTING
2008-12-18 22:28:57 +01:00
private:
#endif
void check();
2008-12-18 22:28:57 +01:00
private:
2008-12-18 22:28:57 +01:00
bool MatchFunctionsThatReturnArg(const Token *tok, const std::string &varname);
2008-12-18 22:28:57 +01:00
/**
* Check if there is a "!var" match inside a condition
* @param tok first token to match
* @param varnames the varname
* @param endpar if this is true the "!var" must be followed by ")"
* @return true if match
2008-12-18 22:28:57 +01:00
*/
bool notvar(const Token *tok, const char *varnames[], bool endpar = false);
2008-12-18 22:28:57 +01:00
const char * call_func(const Token *tok, std::list<const Token *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz);
2008-12-18 22:28:57 +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);
*/
Token *getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype, bool classmember, bool &all, unsigned int sz);
2009-02-04 07:11:36 +01:00
/**
* Simplify code e.g. by replacing empty "{ }" with ";"
* @param tok first token. The tokens list can be modified.
2009-02-04 07:11:36 +01:00
*/
void simplifycode(Token *tok, bool &all);
2008-12-18 22:28:57 +01:00
void checkScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz);
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(tok, severity, id, msg);
}
2009-03-21 17:58:13 +01:00
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(callstack, severity, id, msg);
}
void getErrorMessages()
{ }
};
class CheckMemoryLeakInClass : public CheckMemoryLeak, public Check
{
public:
CheckMemoryLeakInClass() : Check()
{ }
CheckMemoryLeakInClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckMemoryLeakInClass checkMemoryLeak(tokenizer, settings, errorLogger);
checkMemoryLeak.check();
}
#ifndef UNIT_TESTING
private:
#endif
void check();
private:
void parseClass(const Token *tok1, std::vector<const char *> &classname);
void variable(const char classname[], const Token *tokVarname);
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(tok, severity, id, msg);
}
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(callstack, severity, id, msg);
}
void getErrorMessages()
{ }
2008-12-18 22:28:57 +01:00
};
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
#endif