errmsg: added error message about virtual destructors

This commit is contained in:
Daniel Marjamäki 2009-01-10 18:03:56 +00:00
parent 6acb123f1d
commit 897c02e3ed
4 changed files with 14 additions and 7 deletions

View File

@ -734,18 +734,14 @@ void CheckClass::virtualDestructor()
base = Token::findmatch(_tokenizer->tokens(), (std::string("class ") + baseName[0] + " :|{").c_str()); base = Token::findmatch(_tokenizer->tokens(), (std::string("class ") + baseName[0] + " :|{").c_str());
if (base) if (base)
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::virtualDestructor(_tokenizer, base, baseName[0], derivedClass->str()));
errmsg << _tokenizer->fileLine(base) << ": Class " << baseName[0] << " which is inherited by class " << derivedClass->str() << " does not have a virtual destructor";
_errorLogger->reportErr(errmsg.str());
} }
} }
// There is a destructor. Check that it's virtual.. // There is a destructor. Check that it's virtual..
else if (base->str() != "virtual") else if (base->str() != "virtual")
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::virtualDestructor(_tokenizer, base, baseName[0], derivedClass->str()));
errmsg << _tokenizer->fileLine(base) << ": Class " << baseName[0] << " which is inherited by class " << derivedClass->str() << " does not have a virtual destructor";
_errorLogger->reportErr(errmsg.str());
} }
} }
} }

View File

@ -276,7 +276,8 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
checkClass.constructors(); checkClass.constructors();
// Check that all base classes have virtual destructors // Check that all base classes have virtual destructors
checkClass.virtualDestructor(); if (ErrorMessage::virtualDestructor(_settings))
checkClass.virtualDestructor();
if (_settings._showAll) if (_settings._showAll)
{ {

View File

@ -84,6 +84,15 @@ public:
return s._checkCodingStyle; return s._checkCodingStyle;
} }
static std::string virtualDestructor(const Tokenizer *tokenizer, const Token *Location, const std::string &Base, const std::string &Derived)
{
return msg1(tokenizer, Location) + "Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor";
}
static bool virtualDestructor(const Settings &s)
{
return true;
}
static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{ {
return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; return msg1(tokenizer, Location) + "Memory leak: " + varname + "";

View File

@ -60,6 +60,7 @@ int main()
err.push_back(Message("memsetClass", 0, "Using '%1' on class", "memfunc")); err.push_back(Message("memsetClass", 0, "Using '%1' on class", "memfunc"));
err.push_back(Message("memsetStruct", 0, "Using '%1' on struct that contains a 'std::%2'", "memfunc", "classname")); err.push_back(Message("memsetStruct", 0, "Using '%1' on struct that contains a 'std::%2'", "memfunc", "classname"));
err.push_back(Message("operatorEq", Message::STYLE, "'operator=' should return something")); err.push_back(Message("operatorEq", Message::STYLE, "'operator=' should return something"));
err.push_back(Message("virtualDestructor", 0, "Class %1 which is inherited by class %2 does not have a virtual destructor", "Base", "Derived"));
// checkmemoryleak.cpp.. // checkmemoryleak.cpp..
err.push_back(Message("memleak", 0, "Memory leak: %1", "varname")); err.push_back(Message("memleak", 0, "Memory leak: %1", "varname"));