From 897c02e3ed374df650313a2982fcc0e59c2cb6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 Jan 2009 18:03:56 +0000 Subject: [PATCH] errmsg: added error message about virtual destructors --- src/checkclass.cpp | 8 ++------ src/cppcheck.cpp | 3 ++- src/errormessage.h | 9 +++++++++ tools/errmsg.cpp | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/checkclass.cpp b/src/checkclass.cpp index 7537e5df8..b123ec291 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -734,18 +734,14 @@ void CheckClass::virtualDestructor() base = Token::findmatch(_tokenizer->tokens(), (std::string("class ") + baseName[0] + " :|{").c_str()); if (base) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(base) << ": Class " << baseName[0] << " which is inherited by class " << derivedClass->str() << " does not have a virtual destructor"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::virtualDestructor(_tokenizer, base, baseName[0], derivedClass->str())); } } // There is a destructor. Check that it's virtual.. else if (base->str() != "virtual") { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(base) << ": Class " << baseName[0] << " which is inherited by class " << derivedClass->str() << " does not have a virtual destructor"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::virtualDestructor(_tokenizer, base, baseName[0], derivedClass->str())); } } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index c17a0f5c9..7d85d227f 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -276,7 +276,8 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) checkClass.constructors(); // Check that all base classes have virtual destructors - checkClass.virtualDestructor(); + if (ErrorMessage::virtualDestructor(_settings)) + checkClass.virtualDestructor(); if (_settings._showAll) { diff --git a/src/errormessage.h b/src/errormessage.h index 1b720200c..f883dd5f6 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -84,6 +84,15 @@ public: 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) { return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 4ed916577..c99464591 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -60,6 +60,7 @@ int main() 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("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.. err.push_back(Message("memleak", 0, "Memory leak: %1", "varname"));