From d7cd09cb6e5f307c00e3cfd973665bd858650bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 6 Mar 2009 07:22:07 +0100 Subject: [PATCH] added internal support for handling list of classes that are automaticly deallocated --- src/checkmemoryleak.cpp | 8 ++++---- src/settings.cpp | 25 ++++++++++++++++++++++++- src/settings.h | 17 ++++++++++++++--- test/testmemleak.cpp | 15 +++++++++++---- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 6c30e08ea..cd7361516 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -428,9 +428,9 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (_settings._showAll) { - if (_settings._vcl && Token::Match(tok->tokAt(4), "( %var%") && (tok->strAt(3)[0] == 'T')) + if (_settings.isAutoDealloc(tok->strAt(3))) { - // Guess this is a VCL class with automatic deallocation + // This class has automatic deallocation alloc = No; } else @@ -1440,8 +1440,8 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_ParseClass(const Token * // Declaring member variable.. check allocations and deallocations if (Token::Match(tok->next(), "%type% * %var% ;")) { - // No false positives for vcl classes.. - if (_settings._vcl && tok->next()->str()[0] == 'T') + // No false positives for auto deallocated classes.. + if (_settings.isAutoDealloc(tok->strAt(1))) continue; if (tok->isName() || Token::Match(tok, "[;}]")) diff --git a/src/settings.cpp b/src/settings.cpp index 3236440cb..ca91cc2d7 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -19,6 +19,8 @@ #include "settings.h" +#include + Settings::Settings() { _debug = false; @@ -30,7 +32,6 @@ Settings::Settings() _xml = false; _unusedFunctions = false; _security = false; - _vcl = false; _jobs = 1; _exitCode = 0; } @@ -39,3 +40,25 @@ Settings::~Settings() { } + + +void Settings::autoDealloc(std::istream &istr) +{ + std::string line; + while (getline(istr,line)) + { + // Check if line has a valid classname.. + if (line.empty()) + continue; + + // Add classname to list + _autoDealloc.push_back(line); + } +} + + +bool Settings::isAutoDealloc(const char classname[]) const +{ + return (std::find(_autoDealloc.begin(), _autoDealloc.end(), classname) != _autoDealloc.end()); +} + diff --git a/src/settings.h b/src/settings.h index 055c227cb..adb9fffe6 100644 --- a/src/settings.h +++ b/src/settings.h @@ -20,6 +20,10 @@ #ifndef SETTINGS_H #define SETTINGS_H +#include +#include +#include + /** * This is just a container for general settings so that we don't need * to pass individual values to functions or constructors now or in the @@ -27,6 +31,10 @@ */ class Settings { +private: + /** classes that are automaticly deallocated */ + std::list _autoDealloc; + public: Settings(); virtual ~Settings(); @@ -49,9 +57,6 @@ public: /** Security checks */ bool _security; - /** Disable warnings for VCL classes */ - bool _vcl; - /** How many processes/threads should do checking at the same time. Default is 1. */ unsigned int _jobs; @@ -59,6 +64,12 @@ public: /** If errors are found, this value is returned from main(). Default value is 0. */ int _exitCode; + + /** Fill list of automaticly deallocated classes */ + void autoDealloc(std::istream &istr); + + /** is a class automaticly deallocated? */ + bool isAutoDealloc(const char classname[]) const; }; #endif // SETTINGS_H diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index f0df73ca8..b72e89c92 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1931,12 +1931,14 @@ private: - void checkvcl(const char code[]) + void checkvcl(const char code[], const char _autoDealloc[]) { // Tokenize.. Tokenizer tokenizer; + { std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); + } tokenizer.setVarId(); tokenizer.simplifyTokenList(); @@ -1947,7 +1949,12 @@ private: Settings settings; settings._debug = true; settings._showAll = true; - settings._vcl = true; + + { + std::istringstream istr(_autoDealloc); + settings.autoDealloc(istr); + } + CheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this); checkMemoryLeak.CheckMemoryLeak(); } @@ -1959,7 +1966,7 @@ private: checkvcl("void Form1::foo()\n" "{\n" " TEdit *Edit1 = new TEdit(this);\n" - "}\n"); + "}\n", "TEdit\n"); ASSERT_EQUALS("", errout.str()); } @@ -1977,7 +1984,7 @@ private: "Fred::Fred()\n" "{\n" " button = new TButton(this);\n" - "}\n"); + "}\n", "TButton\n"); ASSERT_EQUALS("", errout.str()); } };