refactoring: Refactoring of the leak-checking classes
This commit is contained in:
parent
f7cb7da560
commit
913789605f
|
@ -222,41 +222,67 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const char varname[], AllocTy
|
|||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const
|
||||
{
|
||||
ErrorLogger::ErrorMessage::FileLocation loc;
|
||||
loc.line = tok->linenr();
|
||||
loc.file = tokenizer->file(tok);
|
||||
|
||||
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack;
|
||||
callstack.push_back(loc);
|
||||
|
||||
return ErrorLogger::ErrorMessage(callstack, Severity::stringify(severity), msg, id);
|
||||
}
|
||||
|
||||
ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const std::list<const Token *> &callstack, Severity::e severity, const std::string &id, const std::string &msg) const
|
||||
{
|
||||
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return ErrorLogger::ErrorMessage(locations, Severity::stringify(severity), msg, id);
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname)
|
||||
{
|
||||
error(tok, Severity::error, "memleak", "Memory leak: " + varname);
|
||||
errorLogger->reportErr(errmsg(tok, Severity::error, "memleak", "Memory leak: " + varname));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::memleakallError(const Token *tok, const std::string &varname)
|
||||
{
|
||||
error(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname);
|
||||
errorLogger->reportErr(errmsg(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
|
||||
{
|
||||
error(tok, Severity::error, "resourceLeak", "Resource leak: " + varname);
|
||||
errorLogger->reportErr(errmsg(tok, Severity::error, "resourceLeak", "Resource leak: " + varname));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname)
|
||||
{
|
||||
error(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname);
|
||||
errorLogger->reportErr(errmsg(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname)
|
||||
{
|
||||
error(tok, Severity::error, "deallocuse", "Using '" + varname + "' after it is deallocated / released");
|
||||
errorLogger->reportErr(errmsg(tok, Severity::error, "deallocuse", "Using '" + varname + "' after it is deallocated / released"));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz)
|
||||
{
|
||||
error(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching");
|
||||
errorLogger->reportErr(errmsg(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching"));
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
|
||||
{
|
||||
error(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
|
||||
errorLogger->reportErr(errmsg(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname));
|
||||
}
|
||||
|
||||
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) const
|
||||
|
|
|
@ -43,8 +43,19 @@ class Token;
|
|||
|
||||
class CheckMemoryLeak
|
||||
{
|
||||
private:
|
||||
const Tokenizer * const tokenizer;
|
||||
ErrorLogger * const errorLogger;
|
||||
|
||||
ErrorLogger::ErrorMessage errmsg(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const;
|
||||
ErrorLogger::ErrorMessage errmsg(const std::list<const Token *> &callstack, Severity::e severity, const std::string &id, const std::string &msg) const;
|
||||
|
||||
public:
|
||||
CheckMemoryLeak() { }
|
||||
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e)
|
||||
: tokenizer(t), errorLogger(e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/** 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, Fd, Pipe, Dir, Many };
|
||||
|
@ -64,10 +75,6 @@ public:
|
|||
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 Severity::e severity, const std::string &id, const std::string &msg) = 0;
|
||||
virtual void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg) = 0;
|
||||
|
||||
/** What type of allocated memory does the given function return? */
|
||||
AllocType functionReturnType(const Token *tok) const;
|
||||
};
|
||||
|
@ -84,14 +91,14 @@ public:
|
|||
* 4. finally, check if the simplified token list contain any leaks.
|
||||
*/
|
||||
|
||||
class CheckMemoryLeakInFunction : public CheckMemoryLeak, public Check
|
||||
class CheckMemoryLeakInFunction : public Check, private CheckMemoryLeak
|
||||
{
|
||||
public:
|
||||
CheckMemoryLeakInFunction() : Check()
|
||||
CheckMemoryLeakInFunction() : Check(), CheckMemoryLeak(0, 0)
|
||||
{ }
|
||||
|
||||
CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger)
|
||||
{ }
|
||||
|
||||
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
|
@ -141,16 +148,6 @@ private:
|
|||
|
||||
void checkScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz);
|
||||
|
||||
void error(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
|
||||
{
|
||||
reportError(tok, severity, id, msg);
|
||||
}
|
||||
|
||||
void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg)
|
||||
{
|
||||
reportError(callstack, severity, id, msg);
|
||||
}
|
||||
|
||||
void getErrorMessages()
|
||||
{ }
|
||||
|
||||
|
@ -172,14 +169,14 @@ private:
|
|||
* variables that are allocated in the constructor should be deallocated in the destructor
|
||||
*/
|
||||
|
||||
class CheckMemoryLeakInClass : public CheckMemoryLeak, public Check
|
||||
class CheckMemoryLeakInClass : public Check, private CheckMemoryLeak
|
||||
{
|
||||
public:
|
||||
CheckMemoryLeakInClass() : Check()
|
||||
CheckMemoryLeakInClass() : Check(), CheckMemoryLeak(0,0)
|
||||
{ }
|
||||
|
||||
CheckMemoryLeakInClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger)
|
||||
{ }
|
||||
|
||||
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
|
@ -197,16 +194,6 @@ 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 Severity::e severity, const std::string &id, const std::string &msg)
|
||||
{
|
||||
reportError(tok, severity, id, msg);
|
||||
}
|
||||
|
||||
void error(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, const std::string &msg)
|
||||
{
|
||||
reportError(callstack, severity, id, msg);
|
||||
}
|
||||
|
||||
void getErrorMessages()
|
||||
{ }
|
||||
|
||||
|
@ -228,14 +215,14 @@ private:
|
|||
* variables that are allocated in the constructor should be deallocated in the destructor
|
||||
*/
|
||||
|
||||
class CheckMemoryLeakStructMember : public CheckMemoryLeak, public Check
|
||||
class CheckMemoryLeakStructMember : public Check, private CheckMemoryLeak
|
||||
{
|
||||
public:
|
||||
CheckMemoryLeakStructMember() : Check()
|
||||
CheckMemoryLeakStructMember() : Check(), CheckMemoryLeak(0,0)
|
||||
{ }
|
||||
|
||||
CheckMemoryLeakStructMember(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger)
|
||||
: Check(tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger)
|
||||
{ }
|
||||
|
||||
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
||||
|
|
Loading…
Reference in New Issue