From 913789605ffb1a3cdca023f0903981c923e9a3a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 13 Jul 2009 15:07:26 +0200 Subject: [PATCH] refactoring: Refactoring of the leak-checking classes --- src/checkmemoryleak.cpp | 40 ++++++++++++++++++++++++------ src/checkmemoryleak.h | 55 ++++++++++++++++------------------------- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index ec26a3881..5feb156d6 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -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 callstack; + callstack.push_back(loc); + + return ErrorLogger::ErrorMessage(callstack, Severity::stringify(severity), msg, id); +} + +ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const std::list &callstack, Severity::e severity, const std::string &id, const std::string &msg) const +{ + std::list locations; + + for (std::list::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 &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 diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index e6f6e5306..852b593f4 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -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 &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 &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 &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 &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 &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 &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)