From f0d1822a836c2b192d1e9d2bc43208e7c6941ba9 Mon Sep 17 00:00:00 2001 From: Maksim Derbasov Date: Sat, 22 May 2021 09:36:28 +0300 Subject: [PATCH] Better handling functions with try block for throwInNoexceptFunction (#3264) --- lib/checkexceptionsafety.cpp | 9 +++------ test/testexceptionsafety.cpp | 7 ++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index c9dd3afb6..f8cc28470 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -207,10 +207,8 @@ static const Token * functionThrowsRecursive(const Function * function, std::set for (const Token *tok = function->functionScope->bodyStart->next(); tok != function->functionScope->bodyEnd; tok = tok->next()) { - if (tok->str() == "try") { - // just bail for now - break; - } + if (Token::simpleMatch(tok, "try {")) + tok = tok->linkAt(1); // skip till start of catch clauses if (tok->str() == "throw") { return tok; } else if (tok->function()) { @@ -232,7 +230,7 @@ static const Token * functionThrowsRecursive(const Function * function, std::set static const Token * functionThrows(const Function * function) { - std::set recursive; + std::set recursive; return functionThrowsRecursive(function, recursive); } @@ -306,4 +304,3 @@ void CheckExceptionSafety::unhandledExceptionSpecification() } } } - diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 3d6acd4f4..42c734665 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -321,7 +321,7 @@ private: } void noexceptThrow() { - check("void func1() noexcept(false) { throw 1; }\n" + check("void func1() noexcept(false) { try {} catch(...) {;} throw 1; }\n" "void func2() noexcept { throw 1; }\n" "void func3() noexcept(true) { throw 1; }\n" "void func4() noexcept(false) { throw 1; }\n" @@ -332,12 +332,13 @@ private: "[test.cpp:5]: (error) Exception thrown in function declared not to throw exceptions.\n", errout.str()); // avoid false positives - check("const char *func() noexcept { return 0; }"); + check("const char *func() noexcept { return 0; }\n" + "const char *func1() noexcept { try { throw 1; } catch(...) {} return 0; }"); ASSERT_EQUALS("", errout.str()); } void nothrowThrow() { - check("void func1() throw(int) { throw 1; }\n" + check("void func1() throw(int) { try {;} catch(...) { throw 1; } ; }\n" "void func2() throw() { throw 1; }\n" "void func3() throw(int) { throw 1; }\n" "void func4() throw() { func1(); }\n"