Skip to content

Commit ee42c2c

Browse files
Fix #14526 FN throwInNoexceptFunction with throw in member function (regression), add test for #9380 (#8253)
1 parent f04dccd commit ee42c2c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/checkexceptionsafety.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ static const Token * functionThrowsRecursive(const Function * function, std::set
257257
tok = tok->linkAt(1); // skip till start of catch clauses
258258
if (tok->str() == "throw")
259259
return tok;
260-
if (tok->function() && Token::simpleMatch(tok->astParent(), "(")) {
260+
if (tok->function() && (Token::simpleMatch(tok->astParent(), "(") ||
261+
(Token::simpleMatch(tok->astParent(), ".") && Token::simpleMatch(tok->astParent()->astParent(), "(")))) {
261262
const Function * called = tok->function();
262263
// check if called function has an exception specification
263264
if (called->isThrow() && called->throwArg)

test/testexceptionsafety.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,21 @@ class TestExceptionSafety : public TestFixture {
356356
check("const char *func() noexcept { return 0; }\n"
357357
"const char *func1() noexcept { try { throw 1; } catch(...) {} return 0; }");
358358
ASSERT_EQUALS("", errout_str());
359+
360+
check("struct A {\n" // #14526
361+
" void f(int = 0, int = 1) { throw 0; }\n"
362+
"};\n"
363+
"void g() noexcept {\n"
364+
" A a;\n"
365+
" a.f();\n"
366+
"}\n"
367+
"void h() noexcept {\n"
368+
" A a;\n"
369+
" a.f(1, 3);\n"
370+
"}\n");
371+
ASSERT_EQUALS("[test.cpp:6:7]: (error) Unhandled exception thrown in function declared not to throw exceptions. [throwInNoexceptFunction]\n"
372+
"[test.cpp:10:7]: (error) Unhandled exception thrown in function declared not to throw exceptions. [throwInNoexceptFunction]\n",
373+
errout_str());
359374
}
360375

361376
void nothrowThrow() {
@@ -518,6 +533,15 @@ class TestExceptionSafety : public TestFixture {
518533
" }\n"
519534
"}\n");
520535
ASSERT_EQUALS("", errout_str());
536+
537+
check("void f(int i) {\n" // #9380
538+
" throw i;\n"
539+
"}\n"
540+
"int main() {\n"
541+
" f(123);\n"
542+
"}\n");
543+
ASSERT_EQUALS("[test.cpp:5:5]: (error) Unhandled exception thrown in function that is an entry point. [throwInEntryPoint]\n",
544+
errout_str());
521545
}
522546
};
523547

0 commit comments

Comments
 (0)