Skip to content

Commit 552e394

Browse files
committed
[clang][Sema] Diagnose exceptions only in non-dependent context in discarded try/catch/throw blocks
Resolves #138939 When enabling `--fno-exceptions` flag, discarded statements containing `try/catch/throw` in an independent context can be avoided from being rejected.
1 parent f9f2bf8 commit 552e394

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
854854
// Don't report an error if 'throw' is used in system headers or in an OpenMP
855855
// target region compiled for a GPU architecture.
856856
if (!IsOpenMPGPUTarget && !getLangOpts().CXXExceptions &&
857-
!getSourceManager().isInSystemHeader(OpLoc) && !getLangOpts().CUDA) {
857+
!getSourceManager().isInSystemHeader(OpLoc) && !getLangOpts().CUDA &&
858+
!CurContext->isDependentContext()) {
858859
// Delay error emission for the OpenMP device code.
859860
targetDiag(OpLoc, diag::err_exceptions_disabled) << "throw";
860861
}

clang/lib/Sema/SemaStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4305,7 +4305,8 @@ StmtResult Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
43054305
// Don't report an error if 'try' is used in system headers or in an OpenMP
43064306
// target region compiled for a GPU architecture.
43074307
if (!IsOpenMPGPUTarget && !getLangOpts().CXXExceptions &&
4308-
!getSourceManager().isInSystemHeader(TryLoc) && !getLangOpts().CUDA) {
4308+
!getSourceManager().isInSystemHeader(TryLoc) && !getLangOpts().CUDA &&
4309+
!CurContext->isDependentContext()) {
43094310
// Delay error emission for the OpenMP device code.
43104311
targetDiag(TryLoc, diag::err_exceptions_disabled) << "try";
43114312
}

clang/test/SemaCXX/no-exceptions.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
22

33
// Various tests for -fno-exceptions
44

@@ -32,3 +32,28 @@ void g() {
3232
}
3333

3434
}
35+
36+
namespace test2 {
37+
template <auto enable> void foo(auto &&Fnc) {
38+
if constexpr (enable)
39+
try {
40+
Fnc();
41+
} catch (...) {
42+
}
43+
else
44+
Fnc();
45+
}
46+
47+
void bar1() {
48+
foo<false>([] {});
49+
}
50+
51+
template <typename T> void foo() {
52+
try {
53+
} catch (...) {
54+
}
55+
throw 1;
56+
}
57+
void bar2() { foo<int>(); }
58+
59+
}

0 commit comments

Comments
 (0)