Fixed #5826 (Change error message for 'throw in destructor' check)

This commit is contained in:
Daniel Marjamäki 2014-07-19 03:29:50 +02:00
parent 851f89d15f
commit af2b94e322
3 changed files with 14 additions and 7 deletions

View File

@ -32,6 +32,9 @@ namespace {
void CheckExceptionSafety::destructors() void CheckExceptionSafety::destructors()
{ {
if (!_settings->isEnabled("warning"))
return;
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
// Perform check.. // Perform check..
@ -57,7 +60,7 @@ void CheckExceptionSafety::destructors()
// throw found within a destructor // throw found within a destructor
if (tok->str() == "throw") { if (tok->str() == "throw") {
destructorsError(tok); destructorsError(tok, scope->className);
break; break;
} }
} }

View File

@ -82,8 +82,12 @@ public:
private: private:
/** Don't throw exceptions in destructors */ /** Don't throw exceptions in destructors */
void destructorsError(const Token * const tok) { void destructorsError(const Token * const tok, const std::string &className) {
reportError(tok, Severity::error, "exceptThrowInDestructor", "Exception thrown in destructor."); reportError(tok, Severity::warning, "exceptThrowInDestructor",
"Class " + className + " is not safe, destructor throws exception\n"
"The class " + className + " is not safe because its destructor "
"throws an exception. If " + className + " is used and an exception "
"is thrown that is caught in an outer scope the program will terminate.");
} }
void deallocThrowError(const Token * const tok, const std::string &varname) { void deallocThrowError(const Token * const tok, const std::string &varname) {
@ -140,7 +144,7 @@ private:
/** Generate all possible errors (for --errorlist) */ /** Generate all possible errors (for --errorlist) */
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckExceptionSafety c(0, settings, errorLogger); CheckExceptionSafety c(0, settings, errorLogger);
c.destructorsError(0); c.destructorsError(0, "Class");
c.deallocThrowError(0, "p"); c.deallocThrowError(0, "p");
c.rethrowCopyError(0, "varname"); c.rethrowCopyError(0, "varname");
c.catchExceptionByValueError(0); c.catchExceptionByValueError(0);

View File

@ -75,7 +75,7 @@ private:
" throw e;\n" " throw e;\n"
" }\n" " }\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", errout.str());
check("class x {\n" check("class x {\n"
" ~x();\n" " ~x();\n"
@ -83,12 +83,12 @@ private:
"x::~x() {\n" "x::~x() {\n"
" throw e;\n" " throw e;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Exception thrown in destructor.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (warning) Class x is not safe, destructor throws exception\n", errout.str());
check("x::~x() {\n" check("x::~x() {\n"
" throw e;\n" " throw e;\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", "", errout.str());
// #3858 - throwing exception in try block in destructor. // #3858 - throwing exception in try block in destructor.
check("class x {\n" check("class x {\n"