From: Stephan Bergmann Date: Thu, 29 Jun 2017 17:58:59 +0000 (+0000) Subject: Fixed -Wexceptions derived-to-base false positives X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da1f3cf54166a2718e6ab7a28ddf2a10babe3345;p=clang Fixed -Wexceptions derived-to-base false positives ...as introduced with recent "Emit warning when throw exception in destruct or dealloc functions which has a (possible implicit) noexcept specifier". (The equivalent of the goodReference case hit when building LibreOffice.) (These warnings are apparently only emitted when no errors have yet been encountered, so it didn't work to add the test code to the end of the existing clang/test/SemaCXX/exceptions.cpp.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@306715 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index 934e13e72d..fd2d07957c 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -305,10 +305,14 @@ static bool isThrowCaught(const CXXThrowExpr *Throw, CaughtType = CaughtType->castAs() ->getPointeeType() ->getUnqualifiedDesugaredType(); + if (ThrowType->isPointerType() && CaughtType->isPointerType()) { + ThrowType = ThrowType->getPointeeType()->getUnqualifiedDesugaredType(); + CaughtType = CaughtType->getPointeeType()->getUnqualifiedDesugaredType(); + } if (CaughtType == ThrowType) return true; const CXXRecordDecl *CaughtAsRecordType = - CaughtType->getPointeeCXXRecordDecl(); + CaughtType->getAsCXXRecordDecl(); const CXXRecordDecl *ThrowTypeAsRecordType = ThrowType->getAsCXXRecordDecl(); if (CaughtAsRecordType && ThrowTypeAsRecordType) return ThrowTypeAsRecordType->isDerivedFrom(CaughtAsRecordType); diff --git a/test/SemaCXX/exception-warnings.cpp b/test/SemaCXX/exception-warnings.cpp new file mode 100644 index 0000000000..f6c646e5a4 --- /dev/null +++ b/test/SemaCXX/exception-warnings.cpp @@ -0,0 +1,36 @@ +// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s + +struct B {}; +struct D: B {}; +void goodPlain() throw () { + try { + throw D(); + } catch (B) {} +} +void goodReference() throw () { + try { + throw D(); + } catch (B &) {} +} +void goodPointer() throw () { + D d; + try { + throw &d; + } catch (B *) {} +} +void badPlain() throw () { // expected-note {{non-throwing function declare here}} + try { + throw B(); // expected-warning {{'badPlain' has a non-throwing exception specification but can still throw, resulting in unexpected program termination}} + } catch (D) {} +} +void badReference() throw () { // expected-note {{non-throwing function declare here}} + try { + throw B(); // expected-warning {{'badReference' has a non-throwing exception specification but can still throw, resulting in unexpected program termination}} + } catch (D &) {} +} +void badPointer() throw () { // expected-note {{non-throwing function declare here}} + B b; + try { + throw &b; // expected-warning {{'badPointer' has a non-throwing exception specification but can still throw, resulting in unexpected program termination}} + } catch (D *) {} +}