From: Erich Keane Date: Tue, 17 Oct 2017 20:57:24 +0000 (+0000) Subject: [CFG] Relax Wexceptions warning on rethrow X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2301481f441e83fc5eda437a0def0404c318d0a5;p=clang [CFG] Relax Wexceptions warning on rethrow As reported here: https://bugs.llvm.org/show_bug.cgi?id=34973 "catch(...)" should catch EVERYTHING, even a rethrow. This patch changes the order in which things are checked to ensure that a '...' catch will get a rethrow. Differential Revision: https://reviews.llvm.org/D39013 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316030 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index bdfed6ead1..7a31711e72 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -289,14 +289,14 @@ enum ThrowState { static bool isThrowCaught(const CXXThrowExpr *Throw, const CXXCatchStmt *Catch) { + const Type *CaughtType = Catch->getCaughtType().getTypePtrOrNull(); + if (!CaughtType) + return true; const Type *ThrowType = nullptr; if (Throw->getSubExpr()) ThrowType = Throw->getSubExpr()->getType().getTypePtrOrNull(); if (!ThrowType) return false; - const Type *CaughtType = Catch->getCaughtType().getTypePtrOrNull(); - if (!CaughtType) - return true; if (ThrowType->isReferenceType()) ThrowType = ThrowType->castAs() ->getPointeeType() diff --git a/test/SemaCXX/warn-throw-out-noexcept-func.cpp b/test/SemaCXX/warn-throw-out-noexcept-func.cpp index 5e1c571449..a6c23ddc6c 100644 --- a/test/SemaCXX/warn-throw-out-noexcept-func.cpp +++ b/test/SemaCXX/warn-throw-out-noexcept-func.cpp @@ -239,13 +239,30 @@ void n_ShouldNotDiag() noexcept { } catch (const S &s) { } } -void o_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}} +// As seen in p34973, this should not throw the warning. If there is an active +// exception, catch(...) catches everything. +void o_ShouldNotDiag() noexcept { try { - throw; //expected-warning {{has a non-throwing exception specification but}} + throw; } catch (...) { } } +void p_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}} + try { + throw; //expected-warning {{has a non-throwing exception specification but}} + } catch (int){ + } +} + +void q_ShouldNotDiag() noexcept { + try { + throw; + } catch (int){ + } catch (...){ + } +} + #define NOEXCEPT noexcept void with_macro() NOEXCEPT { //expected-note {{function declared non-throwing here}} throw 1; // expected-warning {{has a non-throwing exception specification but}}