From 385be6d0d942b90d506401a191fbe54fb356c155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Feb 2009 06:28:18 +0000 Subject: [PATCH] memory leak: Added command line flag '--vcl' that is used to suppress error messages for VCL code --- src/checkmemoryleak.cpp | 14 +++++++++++++- src/cppcheck.cpp | 7 ++++++- src/errorlogger.h | 2 +- src/settings.cpp | 1 + src/settings.h | 3 +++ test/testmemleak.cpp | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 680e9cdd0..3f6baf652 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -426,7 +426,19 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (isclass(tok->tokAt(3))) { if (_settings._showAll) - all = true; + { + + if (_settings._vcl && Token::Match(tok->tokAt(4), "( %var%") && (tok->strAt(3)[0] == 'T')) + { + // Guess this is a VCL class with 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; } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 475598183..4acddb0e0 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -104,6 +104,10 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[]) else if (strcmp(argv[i], "--unused-functions") == 0) _settings._unusedFunctions = true; + // Write results in results.xml + else if (strcmp(argv[i], "--vcl") == 0) + _settings._vcl = true; + // Print help else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { @@ -169,7 +173,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[]) "\n" "Syntax:\n" " cppcheck [--all] [--force] [--help] [-Idir] [--quiet] [--style]\n" - " [--unused-functions] [--verbose] [--xml]\n" + " [--unused-functions] [--verbose] [--vcl] [--xml]\n" " [file or path1] [file or path]\n" "\n" "If path is given instead of filename, *.cpp, *.cxx, *.cc, *.c++ and *.c files\n" @@ -186,6 +190,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[]) " -q, --quiet Only print error messages\n" " -s, --style Check coding style\n" " --unused-functions Check if there are unused functions\n" + " --vcl Suppress messages about memory leaks when using VCL classes\n" " -v, --verbose More detailed error reports\n" " --xml Write results in xml to error stream.\n" "\n" diff --git a/src/errorlogger.h b/src/errorlogger.h index df1df4188..9bc289e71 100644 --- a/src/errorlogger.h +++ b/src/errorlogger.h @@ -56,7 +56,7 @@ public: std::string toXML() const; std::string toText() const; std::string serialize() const; - bool deserialize( const std::string &data ); + bool deserialize(const std::string &data); private: std::list _callStack; std::string _severity; diff --git a/src/settings.cpp b/src/settings.cpp index 1a893041b..dc6282af4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -30,6 +30,7 @@ Settings::Settings() _xml = false; _unusedFunctions = false; _security = false; + _vcl = false; } Settings::~Settings() diff --git a/src/settings.h b/src/settings.h index 05803c4fe..b73d5cfa6 100644 --- a/src/settings.h +++ b/src/settings.h @@ -48,6 +48,9 @@ public: /** Security checks */ bool _security; + + /** Disable warnings for VCL classes */ + bool _vcl; }; #endif // SETTINGS_H diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index edaf79e87..504d3ca14 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -192,6 +192,9 @@ private: TEST_CASE(unknownFunction1); TEST_CASE(unknownFunction2); TEST_CASE(unknownFunction3); + + // VCL.. + TEST_CASE(vcl1); } @@ -1886,6 +1889,37 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str()); } + + + + void vcl1() + { + const char code[] = "void Form1::foo()\n" + "{\n" + " TEdit *Edit1 = new TEdit(this);\n" + "}\n"; + + // Tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + // Clear the error buffer.. + errout.str(""); + + // Check for memory leaks.. + Settings settings; + settings._debug = true; + settings._showAll = true; + settings._vcl = true; + CheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this); + checkMemoryLeak.CheckMemoryLeak(); + + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestMemleak)