Skip to content

Commit f6aaf6c

Browse files
IOBYTEdanmar
authored andcommitted
Unhandled exceptions: Dont warn when there are unhandled exceptions in main() function. cppcheck-opensource#5751
1 parent d2ebd71 commit f6aaf6c

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

lib/checkexceptionsafety.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ void CheckExceptionSafety::unhandledExceptionSpecification()
270270
for (std::size_t i = 0; i < functions; ++i) {
271271
const Scope * scope = symbolDatabase->functionScopes[i];
272272
// only check functions without exception epecification
273-
if (scope->function && !scope->function->isThrow) {
273+
if (scope->function && !scope->function->isThrow &&
274+
scope->className != "main" && scope->className != "wmain" &&
275+
scope->className != "_tmain" && scope->className != "WinMain") {
274276
for (const Token *tok = scope->function->functionScope->classStart->next();
275277
tok != scope->function->functionScope->classEnd; tok = tok->next()) {
276278
if (tok->str() == "try") {

test/testexceptionsafety.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class TestExceptionSafety : public TestFixture {
4444
TEST_CASE(catchExceptionByValue);
4545
TEST_CASE(noexceptThrow);
4646
TEST_CASE(nothrowThrow);
47-
TEST_CASE(unhandledExceptionSpecification); // #4800
47+
TEST_CASE(unhandledExceptionSpecification1); // #4800
48+
TEST_CASE(unhandledExceptionSpecification2);
4849
TEST_CASE(nothrowAttributeThrow);
4950
TEST_CASE(nothrowAttributeThrow2); // #5703
5051
}
@@ -344,7 +345,7 @@ class TestExceptionSafety : public TestFixture {
344345
ASSERT_EQUALS("", errout.str());
345346
}
346347

347-
void unhandledExceptionSpecification() { // #4800
348+
void unhandledExceptionSpecification1() { // #4800
348349
check("void myThrowingFoo() throw(MyException) {\n"
349350
" throw MyException();\n"
350351
"}\n"
@@ -359,6 +360,15 @@ class TestExceptionSafety : public TestFixture {
359360
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (warning) Unhandled exception specification when calling function myThrowingFoo().\n", errout.str());
360361
}
361362

363+
void unhandledExceptionSpecification2() {
364+
check("void f() const throw (std::runtime_error);\n"
365+
"int main()\n"
366+
"{\n"
367+
" f();\n"
368+
"}\n");
369+
ASSERT_EQUALS("", errout.str());
370+
}
371+
362372
void nothrowAttributeThrow() {
363373
check("void func1() throw(int) { throw 1; }\n"
364374
"void func2() __attribute((nothrow)); void func1() { throw 1; }\n"

0 commit comments

Comments
 (0)