Cppcheck
Public Types | Public Member Functions | Private Member Functions | Private Attributes
CheckMemoryLeak Class Reference

Base class for memory leaks checking. More...

#include <checkmemoryleak.h>

Inheritance diagram for CheckMemoryLeak:
CheckMemoryLeakInClass CheckMemoryLeakInFunction CheckMemoryLeakNoVar CheckMemoryLeakStructMember

List of all members.

Public Types

enum  AllocType {
  No, Malloc, gMalloc, New,
  NewArray, File, Fd, Pipe,
  Dir, Many
}
 What type of allocation are used. More...

Public Member Functions

 CheckMemoryLeak (const Tokenizer *t, ErrorLogger *e, const Standards &s)
void memoryLeak (const Token *tok, const std::string &varname, AllocType alloctype)
AllocType getDeallocationType (const Token *tok, const std::string &varname) const
 Get type of deallocation at given position.
AllocType getDeallocationType (const Token *tok, unsigned int varid) const
 Get type of deallocation at given position.
AllocType getAllocationType (const Token *tok2, unsigned int varid, std::list< const Function * > *callstack=NULL) const
 Get type of allocation at given position.
AllocType getReallocationType (const Token *tok2, unsigned int varid) const
 Get type of reallocation at given position.
bool isclass (const Token *tok, unsigned int varid) const
 Is a typename the name of a class?
void memleakError (const Token *tok, const std::string &varname) const
 Report that there is a memory leak (new/malloc/etc)
void resourceLeakError (const Token *tok, const std::string &varname) const
 Report that there is a resource leak (fopen/popen/etc)
void deallocDeallocError (const Token *tok, const std::string &varname) const
 Report error: deallocating a deallocated pointer.
void deallocuseError (const Token *tok, const std::string &varname) const
void mismatchSizeError (const Token *tok, const std::string &sz) const
void mismatchAllocDealloc (const std::list< const Token * > &callstack, const std::string &varname) const
void memleakUponReallocFailureError (const Token *tok, const std::string &varname) const
AllocType functionReturnType (const Function *func, std::list< const Function * > *callstack=NULL) const
 What type of allocated memory does the given function return?
const char * functionArgAlloc (const Function *func, unsigned int targetpar, AllocType &allocType) const
 Function allocates pointed-to argument (a la asprintf)?

Private Member Functions

 CheckMemoryLeak ()
 Disable the default constructors.
 CheckMemoryLeak (const CheckMemoryLeak &)
 Disable the default constructors.
void operator= (const CheckMemoryLeak &)
 disable assignment operator
void reportErr (const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
 Report error.
void reportErr (const std::list< const Token * > &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
 Report error.

Private Attributes

const Tokenizer *const tokenizer
 For access to the tokens.
ErrorLogger *const errorLogger
 ErrorLogger used to report errors.
const Standardsstandards
 Enabled standards.

Detailed Description

Base class for memory leaks checking.

Definition at line 52 of file checkmemoryleak.h.


Member Enumeration Documentation

What type of allocation are used.

. the "Many" means that several types of allocation and deallocation are used

Enumerator:
No 
Malloc 
gMalloc 
New 
NewArray 
File 
Fd 
Pipe 
Dir 
Many 

Definition at line 96 of file checkmemoryleak.h.


Constructor & Destructor Documentation

Disable the default constructors.

Disable the default constructors.

CheckMemoryLeak::CheckMemoryLeak ( const Tokenizer t,
ErrorLogger e,
const Standards s 
) [inline]

Definition at line 91 of file checkmemoryleak.h.


Member Function Documentation

void CheckMemoryLeak::deallocDeallocError ( const Token tok,
const std::string &  varname 
) const

Report error: deallocating a deallocated pointer.

Parameters:
toktoken where error occurs
varnamename of variable

Definition at line 402 of file checkmemoryleak.cpp.

References Severity::error, and reportErr().

Referenced by CheckMemoryLeakInFunction::checkScope(), and CheckMemoryLeakInFunction::getErrorMessages().

void CheckMemoryLeak::deallocuseError ( const Token tok,
const std::string &  varname 
) const
const char * CheckMemoryLeak::functionArgAlloc ( const Function func,
unsigned int  targetpar,
AllocType allocType 
) const
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType ( const Function func,
std::list< const Function * > *  callstack = NULL 
) const
CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType ( const Token tok2,
unsigned int  varid,
std::list< const Function * > *  callstack = NULL 
) const
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType ( const Token tok,
const std::string &  varname 
) const

Get type of deallocation at given position.

Parameters:
tokposition
varnamevariable name
Returns:
type of deallocation

Definition at line 308 of file checkmemoryleak.cpp.

References Dir, Fd, File, gMalloc, Malloc, Token::Match(), New, NewArray, No, Pipe, and Token::simpleMatch().

Referenced by CheckMemoryLeakInFunction::call_func(), CheckMemoryLeakInFunction::getcode(), and CheckMemoryLeakInClass::variable().

CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType ( const Token tok,
unsigned int  varid 
) const

Get type of deallocation at given position.

Parameters:
tokposition
varidvariable id
Returns:
type of deallocation

Definition at line 266 of file checkmemoryleak.cpp.

References Dir, Fd, File, gMalloc, Malloc, Token::Match(), New, NewArray, Token::next(), No, Pipe, Token::simpleMatch(), and Token::str().

CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType ( const Token tok2,
unsigned int  varid 
) const

Get type of reallocation at given position.

Definition at line 241 of file checkmemoryleak.cpp.

References gMalloc, Token::link(), Malloc, Token::Match(), Token::next(), No, and Token::str().

Referenced by CheckMemoryLeakInFunction::call_func(), functionArgAlloc(), and CheckMemoryLeakInFunction::getcode().

bool CheckMemoryLeak::isclass ( const Token tok,
unsigned int  varid 
) const

Is a typename the name of a class?

Parameters:
toktype token
varidvariable id
Returns:
true if the type name is the name of a class
Todo:
false negative: check base class for side effects
Todo:
false negative: check constructors for side effects

Definition at line 107 of file checkmemoryleak.cpp.

References Type::derivedFrom, Tokenizer::getSymbolDatabase(), SymbolDatabase::getVariableFromVarId(), Token::isStandardType(), Type::needInitialization, Scope::numConstructors, tokenizer, Type::True, Variable::type(), Variable::typeScope(), and Scope::varlist.

Referenced by CheckMemoryLeakInFunction::getcode().

void CheckMemoryLeak::memleakError ( const Token tok,
const std::string &  varname 
) const

Report that there is a memory leak (new/malloc/etc)

Parameters:
toktoken where memory is leaked
varnamename of variable

Definition at line 384 of file checkmemoryleak.cpp.

References Severity::error, and reportErr().

Referenced by CheckMemoryLeakInFunction::getErrorMessages(), CheckLeakAutoVar::leakError(), and memoryLeak().

void CheckMemoryLeak::memleakUponReallocFailureError ( const Token tok,
const std::string &  varname 
) const
void CheckMemoryLeak::memoryLeak ( const Token tok,
const std::string &  varname,
AllocType  alloctype 
)
void CheckMemoryLeak::mismatchAllocDealloc ( const std::list< const Token * > &  callstack,
const std::string &  varname 
) const
void CheckMemoryLeak::mismatchSizeError ( const Token tok,
const std::string &  sz 
) const
void CheckMemoryLeak::operator= ( const CheckMemoryLeak ) [private]

disable assignment operator

void CheckMemoryLeak::reportErr ( const Token location,
Severity::SeverityType  severity,
const std::string &  id,
const std::string &  msg 
) const [private]

Report error.

Similar with the function Check::reportError

Parameters:
locationthe token where the error occurs
severitythe severity of the bug
idtype of message
msgtext

Definition at line 364 of file checkmemoryleak.cpp.

Referenced by deallocDeallocError(), deallocuseError(), memleakError(), memleakUponReallocFailureError(), mismatchAllocDealloc(), mismatchSizeError(), and resourceLeakError().

void CheckMemoryLeak::reportErr ( const std::list< const Token * > &  callstack,
Severity::SeverityType  severity,
const std::string &  id,
const std::string &  msg 
) const [private]

Report error.

Similar with the function Check::reportError

Parameters:
callstackcallstack of error
severitythe severity of the bug
idtype of message
msgtext

Definition at line 374 of file checkmemoryleak.cpp.

References errorLogger, Tokenizer::list, ErrorLogger::reportErr(), Check::reportError(), and tokenizer.

void CheckMemoryLeak::resourceLeakError ( const Token tok,
const std::string &  varname 
) const

Report that there is a resource leak (fopen/popen/etc)

Parameters:
toktoken where resource is leaked
varnamename of variable

Definition at line 394 of file checkmemoryleak.cpp.

References Severity::error, and reportErr().

Referenced by CheckMemoryLeakInFunction::getErrorMessages(), CheckLeakAutoVar::leakError(), and memoryLeak().


Member Data Documentation

ErrorLogger used to report errors.

Definition at line 58 of file checkmemoryleak.h.

Referenced by reportErr().

Enabled standards.

Definition at line 61 of file checkmemoryleak.h.

Referenced by getAllocationType().

const Tokenizer* const CheckMemoryLeak::tokenizer [private]

For access to the tokens.

Definition at line 55 of file checkmemoryleak.h.

Referenced by functionReturnType(), getAllocationType(), isclass(), and reportErr().


The documentation for this class was generated from the following files: