[clang] [clang][Sema] Diagnose exceptions only in non-dependent context in discarded `try/catch/throw` blocks (PR #139859)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 14 01:24:18 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Rajveer Singh Bharadwaj (Rajveer100)
<details>
<summary>Changes</summary>
Resolves #<!-- -->138939
When enabling `--fno-exceptions` flag, discarded statements containing `try/catch/throw` in an independent context can be avoided from being rejected.
---
Full diff: https://github.com/llvm/llvm-project/pull/139859.diff
3 Files Affected:
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+2-1)
- (modified) clang/lib/Sema/SemaStmt.cpp (+2-1)
- (modified) clang/test/SemaCXX/no-exceptions.cpp (+26-1)
``````````diff
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index b2a982e953012..16a4da40eec17 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -854,7 +854,8 @@ ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
// Don't report an error if 'throw' is used in system headers or in an OpenMP
// target region compiled for a GPU architecture.
if (!IsOpenMPGPUTarget && !getLangOpts().CXXExceptions &&
- !getSourceManager().isInSystemHeader(OpLoc) && !getLangOpts().CUDA) {
+ !getSourceManager().isInSystemHeader(OpLoc) && !getLangOpts().CUDA &&
+ !CurContext->isDependentContext()) {
// Delay error emission for the OpenMP device code.
targetDiag(OpLoc, diag::err_exceptions_disabled) << "throw";
}
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index e8c1f8490342a..2a0c8b017e2e0 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4305,7 +4305,8 @@ StmtResult Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
// Don't report an error if 'try' is used in system headers or in an OpenMP
// target region compiled for a GPU architecture.
if (!IsOpenMPGPUTarget && !getLangOpts().CXXExceptions &&
- !getSourceManager().isInSystemHeader(TryLoc) && !getLangOpts().CUDA) {
+ !getSourceManager().isInSystemHeader(TryLoc) && !getLangOpts().CUDA &&
+ !CurContext->isDependentContext()) {
// Delay error emission for the OpenMP device code.
targetDiag(TryLoc, diag::err_exceptions_disabled) << "try";
}
diff --git a/clang/test/SemaCXX/no-exceptions.cpp b/clang/test/SemaCXX/no-exceptions.cpp
index 097123d3fe523..665d43e260601 100644
--- a/clang/test/SemaCXX/no-exceptions.cpp
+++ b/clang/test/SemaCXX/no-exceptions.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
// Various tests for -fno-exceptions
@@ -32,3 +32,28 @@ void g() {
}
}
+
+namespace test2 {
+template <auto enable> void foo(auto &&Fnc) {
+ if constexpr (enable)
+ try {
+ Fnc();
+ } catch (...) {
+ }
+ else
+ Fnc();
+}
+
+void bar1() {
+ foo<false>([] {});
+}
+
+template <typename T> void foo() {
+ try {
+ } catch (...) {
+ }
+ throw 1;
+}
+void bar2() { foo<int>(); }
+
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/139859
More information about the cfe-commits
mailing list