virtual destructor : iterate through base classes for a derived class to check that the destructor in each base class i virtual

This commit is contained in:
Daniel Marjamäki 2008-12-06 19:26:13 +00:00
parent 8a06b71949
commit 984cd47aac
2 changed files with 37 additions and 25 deletions

View File

@ -672,10 +672,13 @@ void CheckClass::virtualDestructor()
{ {
const TOKEN *derived = TOKEN::findmatch(_tokenizer->tokens(), "class %var% : public %var%"); const TOKEN *derived = TOKEN::findmatch(_tokenizer->tokens(), "class %var% : public %var%");
while (derived) while (derived)
{
// Iterate through each base class...
for ( derived = derived->tokAt(3); TOKEN::Match(derived, "public %var%"); derived = derived->tokAt(3) )
{ {
// Name of base class.. // Name of base class..
const char *baseName[2]; const char *baseName[2];
baseName[0] = derived->strAt( 4 ); baseName[0] = derived->strAt(1);
baseName[1] = 0; baseName[1] = 0;
// Find the destructor for the base class. // Find the destructor for the base class.
@ -703,8 +706,9 @@ void CheckClass::virtualDestructor()
errmsg << _tokenizer->fileLine(base) << ": The destructor for the base class " << baseName[0] << " is not virtual"; errmsg << _tokenizer->fileLine(base) << ": The destructor for the base class " << baseName[0] << " is not virtual";
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
}
derived = TOKEN::findmatch(derived->next, "class %var% : public %var%"); derived = TOKEN::findmatch(derived, "class %var% : public %var%");
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -39,6 +39,7 @@ private:
TEST_CASE( virtualDestructor1 ); TEST_CASE( virtualDestructor1 );
TEST_CASE( virtualDestructor2 ); TEST_CASE( virtualDestructor2 );
TEST_CASE( virtualDestructor3 ); TEST_CASE( virtualDestructor3 );
TEST_CASE( virtualDestructor4 );
} }
// Check that base classes have virtual destructors // Check that base classes have virtual destructors
@ -79,6 +80,13 @@ private:
"class Derived : public Base { };"); "class Derived : public Base { };");
ASSERT_EQUALS( std::string("[test.cpp:1]: The destructor for the base class Base is not virtual\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:1]: The destructor for the base class Base is not virtual\n"), errout.str() );
} }
void virtualDestructor4()
{
checkVirtualDestructor("class Base { public: ~Base(); };\n"
"class Derived : public Fred, public Base { };");
ASSERT_EQUALS( std::string("[test.cpp:1]: The destructor for the base class Base is not virtual\n"), errout.str() );
}
}; };
REGISTER_TEST( TestClass ) REGISTER_TEST( TestClass )