From 93604dd3441a82738ff3c3169a8d5e55c0a37100 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 10 Aug 2009 17:58:13 +0300 Subject: [PATCH] Fixed ticket #578 (missing virtual destructor not detected) http://sourceforge.net/apps/trac/cppcheck/ticket/578 --- src/checkclass.cpp | 15 +++++++++++++-- test/testclass.cpp | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/checkclass.cpp b/src/checkclass.cpp index 049983036..2e57d272f 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -710,9 +710,20 @@ void CheckClass::virtualDestructor() baseName[1] = 0; // Update derived so it's ready for the next loop. - derived = derived->next(); - if (derived->str() == ",") + do + { + if (derived->str() == "{") + break; + + if (derived->str() == ",") + { + derived = derived->next(); + break; + } + derived = derived->next(); + } + while (derived); // If not public inheritance, skip checking of this base class.. if (! isPublic) diff --git a/test/testclass.cpp b/test/testclass.cpp index 0f22dcd92..edb4e353d 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(virtualDestructor5); // Derived class has empty destructor => no error TEST_CASE(virtualDestructorProtected); TEST_CASE(virtualDestructorInherited); + TEST_CASE(virtualDestructorTemplate); TEST_CASE(uninitVar1); TEST_CASE(uninitVarEnum); @@ -307,6 +308,28 @@ private: TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Class A which is inherited by class B does not have a virtual destructor\n", errout.str()); } + void virtualDestructorTemplate() + { + // Base class has protected destructor, it makes Base *p = new Derived(); fail + // during compilation time, so error is not possible. => no error + checkVirtualDestructor("template class A\n" + "{\n" + " public:\n" + " virtual ~A(){}\n" + "};\n" + "template class AA\n" + "{\n" + " public:\n" + " ~AA(){}\n" + "};\n" + "class B : public A, public AA\n" + "{\n" + " public:\n" + " ~B(){int a;}\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:7]: (error) Class AA which is inherited by class B does not have a virtual destructor\n", errout.str()); + } + void checkUninitVar(const char code[]) { // Tokenize..