From d7b117402c7413364f8aef532495704fb17e64cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=E4ki?= Date: Sun, 16 May 2010 07:15:31 +0200 Subject: [PATCH] Code cleanup: Removed autodealloc handling --- lib/checkexceptionsafety.cpp | 8 ++++- lib/checkmemoryleak.cpp | 25 ++------------ lib/cppcheck.cpp | 17 +--------- lib/settings.cpp | 25 -------------- lib/settings.h | 12 ------- test/testexceptionsafety.cpp | 18 +++++----- test/testmemleak.cpp | 65 +----------------------------------- 7 files changed, 20 insertions(+), 150 deletions(-) diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index f96b77907..4d813195c 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -105,10 +105,13 @@ void CheckExceptionSafety::unsafeNew() unsafeNewError(tok->previous(), varname); break; } + /* + // TODO: check if class is autodeallocated+might throw exception in constructor if (!_settings->isAutoDealloc(tok->strAt(2))) { varname = tok->strAt(-1); } + */ } tok = tok->link(); tok = tok ? tok->next() : 0; @@ -152,8 +155,11 @@ void CheckExceptionSafety::unsafeNew() unsafeNewError(tok, varname); break; } + /* + TODO: check is class is autodeallocated + might throw exceptions in the constructor if (!_settings->isAutoDealloc(tok->strAt(3))) varname = tok->str(); + */ } } } @@ -170,7 +176,7 @@ void CheckExceptionSafety::unsafeNew() } if (Token::Match(tok, "[;{}] %type% * %var% ;") && - !_settings->isAutoDealloc(tok->strAt(1))) + tok->tokAt(1)->isStandardType()) { tok = tok->tokAt(3); if (tok->varId()) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index b3f9012a9..8a8964222 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -815,29 +815,14 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(2), "new %type% [(;]")) { if (isclass(_tokenizer, tok->tokAt(3))) { - if (_settings->inconclusive) - { - - if (_settings->isAutoDealloc(tok->strAt(3))) - { - // This class has automatic deallocation - alloc = No; - } - else - { - // The checking will proceed.. but any error messages that are shown are shown thanks to "--all" - all = true; - } - } - else - alloc = No; + alloc = No; } } } @@ -2366,11 +2351,7 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vectorisStandardType()) checkPublicFunctions(tok1, tok->tokAt(2)->varId()); - // No false positives for auto deallocated classes.. - if (_settings->isAutoDealloc(tok->str().c_str())) - continue; - - if (_settings->inconclusive || !isclass(_tokenizer, tok)) + if (!isclass(_tokenizer, tok)) variable(classname.back(), tok->tokAt(2)); } } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index faa7756aa..145cb6d79 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -464,25 +464,10 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) } } - // auto deallocated classes.. + // deprecated: auto deallocated classes.. else if (strcmp(argv[i], "--auto-dealloc") == 0) { ++i; - - if (i >= argc || !strstr(argv[i], ".lst")) - { - reportOut("cppcheck: No .lst file specified for the --auto-dealloc option"); - return false; - } - - std::ifstream f(argv[i]); - if (!f.is_open()) - { - reportOut("cppcheck: couldn't open the file \"" + std::string(argv[i+1]) + "\""); - return false; - } - - _settings.autoDealloc(f); } // print all possible error messages.. diff --git a/lib/settings.cpp b/lib/settings.cpp index e141b1b14..e1d604184 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -40,21 +40,6 @@ Settings::Settings() inconclusive = false; } - -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.insert(line); - } -} - bool Settings::Suppressions::parseFile(std::istream &istr) { std::string line; @@ -168,16 +153,6 @@ bool Settings::isEnabled(const std::string &str) const return bool(_enabled.find(str) != _enabled.end()); } -void Settings::addAutoAllocClass(const std::string &name) -{ - _autoDealloc.insert(name); -} - -bool Settings::isAutoDealloc(const std::string &classname) const -{ - return (_autoDealloc.find(classname) != _autoDealloc.end()); -} - void Settings::append(const std::string &filename) { diff --git a/lib/settings.h b/lib/settings.h index bd784e1c4..772748e75 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -37,9 +37,6 @@ class Settings { private: - /** @brief classes that are automaticly deallocated */ - std::set _autoDealloc; - /** @brief Code to append in the checks */ std::string _append; @@ -107,15 +104,6 @@ public: for finding include files inside source files. (-I) */ std::list _includePaths; - /** @brief Fill list of automaticly deallocated classes (--auto-dealloc) */ - void autoDealloc(std::istream &istr); - - /** @brief Add class to list of automatically deallocated classes */ - void addAutoAllocClass(const std::string &name); - - /** @brief is a class automaticly deallocated? */ - bool isAutoDealloc(const std::string &classname) const; - /** @brief assign append code (--append) */ void append(const std::string &filename); diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index ecf222cc0..7604178e9 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -41,7 +41,7 @@ private: TEST_CASE(deallocThrow); } - void check(const std::string &code, const std::string &autodealloc = "") + void check(const std::string &code) { // Tokenize.. Tokenizer tokenizer; @@ -55,8 +55,6 @@ private: // Check char variable usage.. Settings settings; settings.addEnabled("all"); - std::istringstream istr2(autodealloc.c_str()); - settings.autoDealloc(istr2); CheckExceptionSafety checkExceptionSafety(&tokenizer, &settings, this); checkExceptionSafety.runSimplifiedChecks(&tokenizer, &settings, this); } @@ -73,24 +71,24 @@ private: void newnew() { check("C::C() : a(new A), b(new B) { }"); - ASSERT_EQUALS("[test.cpp:1]: (style) Upon exception there is memory leak: a\n", errout.str()); - - check("C::C() : a(new A), b(new B) { }", "A\nB\n"); ASSERT_EQUALS("", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:1]: (style) Upon exception there is memory leak: a\n", errout.str()); check("C::C()\n" "{\n" " a = new A;\n" " b = new B;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a\n", errout.str()); check("void a()\n" "{\n" " A *a1 = new A;\n" " A *a2 = new A;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a1\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a1\n", errout.str()); check("void a()\n" "{\n" @@ -112,7 +110,7 @@ private: "{\n" " A *a = new A;\n" " B *b = new B;\n" - "}\n", "A\n"); + "}\n"); ASSERT_EQUALS("", errout.str()); // passing pointer to unknown function.. the pointer may be added to some list etc.. @@ -121,7 +119,7 @@ private: " A *a1 = new A;\n" " add(a1);\n" " A *a2 = new A;\n" - "}\n", ""); + "}\n"); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 4863bad19..d34519d29 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -340,10 +340,6 @@ private: TEST_CASE(unknownFunction4); TEST_CASE(unknownFunction5); - // VCL.. - TEST_CASE(vcl1); - TEST_CASE(vcl2); - // detect leak in class member function.. TEST_CASE(class1); @@ -2084,12 +2080,6 @@ private: " Fred *f = new Fred;\n" "}\n", false); ASSERT_EQUALS("", errout.str()); - - check("void foo()\n" - "{\n" - " Fred *f = new Fred;\n" - "}\n", true); - ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: f\n", errout.str()); } @@ -2184,7 +2174,7 @@ private: } - void checkvcl(const char code[], const char _autoDealloc[]) + void checkvcl(const char code[]) { // Tokenize.. Tokenizer tokenizer; @@ -2202,45 +2192,11 @@ private: Settings settings; settings.inconclusive = true; - { - std::istringstream istr(_autoDealloc); - settings.autoDealloc(istr); - } - CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this); checkMemoryLeak.check(); } - - void vcl1() - { - checkvcl("void Form1::foo()\n" - "{\n" - " TEdit *Edit1 = new TEdit(this);\n" - "}\n", "TEdit\n"); - ASSERT_EQUALS("", errout.str()); - } - - - void vcl2() - { - checkvcl("class Fred\n" - "{\n" - "private:\n" - " TButton *button;\n" - "public:\n" - " Fred();\n" - "};\n" - "\n" - "Fred::Fred()\n" - "{\n" - " button = new TButton(this);\n" - "}\n", "TButton\n"); - ASSERT_EQUALS("", errout.str()); - } - - void class1() { check("class Fred\n" @@ -2811,8 +2767,6 @@ private: TEST_CASE(staticvar); - TEST_CASE(use); - TEST_CASE(free_member_in_sub_func); TEST_CASE(mismatch1); @@ -3126,23 +3080,6 @@ private: } - void use() - { - check("class A\n" - "{\n" - "public:\n" - " Fred * fred;\n" - " A();\n" - "};\n" - "A::A()\n" - "{\n" - " fred = new Fred;\n" - " list->push_back(fred);\n" - "}", true); - TODO_ASSERT_EQUALS("", errout.str()); - } - - void free_member_in_sub_func() { // Member function