[libcxx-commits] [libcxx] [libc++] Deprecate and remove `uncaught_exception` (PR #101830)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 4 06:13:26 PDT 2024


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/101830

>From 1e9c654671d32bdeed867d16889f6451135cdbdb Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sat, 3 Aug 2024 23:07:00 +0800
Subject: [PATCH 1/4] [libc++] Remove and deprecate `uncaught_exception`

Works towards P0619R4.

- `std::uncaught_exception` was not previously deprecated. This patch deprecates it since C++17 as per N4259. `std::uncaught_exceptions` is used instead as libc++ unconditionally provides this function.

Escape hatches:
- `_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION` restores `std::uncaught_exception`.

Drive-by changes:
- In Cxx20.rst, says that D.11 is already done. It is done in 578d09c1b195d859ca7e62840ff6bb83421a77b5.
---
 libcxx/docs/ReleaseNotes/20.rst                               | 3 +++
 libcxx/docs/Status/Cxx17.rst                                  | 1 +
 libcxx/docs/Status/Cxx17Papers.csv                            | 2 +-
 libcxx/docs/Status/Cxx20.rst                                  | 2 +-
 libcxx/docs/UsingLibcxx.rst                                   | 3 +++
 libcxx/include/__exception/operations.h                       | 4 +++-
 libcxx/include/__ostream/basic_ostream.h                      | 2 +-
 libcxx/include/exception                                      | 2 +-
 libcxx/modules/std/exception.inc                              | 1 -
 libcxx/src/exception.cpp                                      | 3 +++
 .../support.exception/uncaught/uncaught_exception.pass.cpp    | 4 ++++
 11 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/libcxx/docs/ReleaseNotes/20.rst b/libcxx/docs/ReleaseNotes/20.rst
index 960fdd7ce0562..79079df6e061d 100644
--- a/libcxx/docs/ReleaseNotes/20.rst
+++ b/libcxx/docs/ReleaseNotes/20.rst
@@ -44,6 +44,9 @@ Implemented Papers
 Improvements and New Features
 -----------------------------
 
+- The ``_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION`` macro has been added to make ``std::uncaught_exception``
+  available.
+
 - TODO
 
 
diff --git a/libcxx/docs/Status/Cxx17.rst b/libcxx/docs/Status/Cxx17.rst
index 3f1f2071300c8..6bccd4cf854c4 100644
--- a/libcxx/docs/Status/Cxx17.rst
+++ b/libcxx/docs/Status/Cxx17.rst
@@ -40,6 +40,7 @@ Paper Status
 
 .. note::
 
+   .. [#note-N4259] N4259: ``std::uncaught_exception`` is deprecated since version 20.0.
    .. [#note-P0067] P0067: ``std::(to|from)_chars`` for integrals has been available since version 7.0. ``std::to_chars`` for ``float`` and ``double`` since version 14.0 ``std::to_chars`` for ``long double`` uses the implementation for ``double``.
    .. [#note-P0226] P0226: Progress is tracked `here <https://https://libcxx.llvm.org/Status/SpecialMath.html>`_.
    .. [#note-P0607] P0607: The parts of P0607 that are not done are the ``<regex>`` bits.
diff --git a/libcxx/docs/Status/Cxx17Papers.csv b/libcxx/docs/Status/Cxx17Papers.csv
index 232feea985545..afdfa6fd0f4d6 100644
--- a/libcxx/docs/Status/Cxx17Papers.csv
+++ b/libcxx/docs/Status/Cxx17Papers.csv
@@ -4,7 +4,7 @@
 "`N4169 <https://wg21.link/N4169>`__","A proposal to add invoke function template","Urbana","|Complete|","3.7",""
 "`N4190 <https://wg21.link/N4190>`__","Removing auto_ptr, random_shuffle(), And Old <functional> Stuff.","Urbana","|Complete|","15.0",""
 "`N4258 <https://wg21.link/N4258>`__","Cleaning-up noexcept in the Library.","Urbana","|In Progress|","3.7",""
-"`N4259 <https://wg21.link/N4259>`__","Wording for std::uncaught_exceptions","Urbana","|Complete|","3.7",""
+"`N4259 <https://wg21.link/N4259>`__","Wording for std::uncaught_exceptions","Urbana","|Complete| [#note-N4259]_","3.7",""
 "`N4277 <https://wg21.link/N4277>`__","TriviallyCopyable ``reference_wrapper``\ .","Urbana","|Complete|","3.2",""
 "`N4279 <https://wg21.link/N4279>`__","Improved insertion interface for unique-key maps.","Urbana","|Complete|","3.7",""
 "`N4280 <https://wg21.link/N4280>`__","Non-member size() and more","Urbana","|Complete|","3.6",""
diff --git a/libcxx/docs/Status/Cxx20.rst b/libcxx/docs/Status/Cxx20.rst
index b76e30fbb3712..951d8cde18338 100644
--- a/libcxx/docs/Status/Cxx20.rst
+++ b/libcxx/docs/Status/Cxx20.rst
@@ -44,7 +44,7 @@ Paper Status
    .. [#note-P0645] P0645: The paper is implemented but still marked as an incomplete feature
       (the feature-test macro is not set).
    .. [#note-P0966] P0966: It was previously erroneously marked as complete in version 8.0. See `bug 45368 <https://llvm.org/PR45368>`__.
-   .. [#note-P0619] P0619: Only sections D.8, D.9, D.10 and D.13 are implemented. Sections D.4, D.7, D.11, and D.12 remain undone.
+   .. [#note-P0619] P0619: Only sections D.7, D.8, D.9, D.10, D.11 and D.13 are implemented. Sections D.4 and D.12 remain undone.
    .. [#note-P0883.1] P0883: shared_ptr and floating-point changes weren't applied as they themselves aren't implemented yet.
    .. [#note-P0883.2] P0883: ``ATOMIC_FLAG_INIT`` was marked deprecated in version 14.0, but was undeprecated with the implementation of LWG3659 in version 15.0.
    .. [#note-P0660] P0660: The paper is implemented but the features are experimental and can be enabled via ``-fexperimental-library``.
diff --git a/libcxx/docs/UsingLibcxx.rst b/libcxx/docs/UsingLibcxx.rst
index df08875c13bea..cf10fe728fca0 100644
--- a/libcxx/docs/UsingLibcxx.rst
+++ b/libcxx/docs/UsingLibcxx.rst
@@ -215,6 +215,9 @@ C++17 Specific Configuration Macros
 
 C++20 Specific Configuration Macros
 -----------------------------------
+**_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION**:
+  This macro is used to re-enable `uncaught_exception`.
+
 **_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE**:
   This macro is used to re-enable the function
   ``std::shared_ptr<...>::unique()``.
diff --git a/libcxx/include/__exception/operations.h b/libcxx/include/__exception/operations.h
index 0a9c7a7c7f0d8..4a0a697c00e6e 100644
--- a/libcxx/include/__exception/operations.h
+++ b/libcxx/include/__exception/operations.h
@@ -29,7 +29,9 @@ using terminate_handler = void (*)();
 _LIBCPP_EXPORTED_FROM_ABI terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
 _LIBCPP_EXPORTED_FROM_ABI terminate_handler get_terminate() _NOEXCEPT;
 
-_LIBCPP_EXPORTED_FROM_ABI bool uncaught_exception() _NOEXCEPT;
+#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION)
+_LIBCPP_EXPORTED_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 bool uncaught_exception() _NOEXCEPT;
+#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION)
 _LIBCPP_EXPORTED_FROM_ABI int uncaught_exceptions() _NOEXCEPT;
 
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr;
diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index 178359d681567..e0698ccb4842a 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -152,7 +152,7 @@ basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& _
 
 template <class _CharT, class _Traits>
 basic_ostream<_CharT, _Traits>::sentry::~sentry() {
-  if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {
+  if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) {
 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
     try {
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
diff --git a/libcxx/include/exception b/libcxx/include/exception
index 5eff8e3f8a4bf..64463e02cb16a 100644
--- a/libcxx/include/exception
+++ b/libcxx/include/exception
@@ -47,7 +47,7 @@ terminate_handler set_terminate(terminate_handler  f ) noexcept;
 terminate_handler get_terminate() noexcept;
 [[noreturn]] void terminate() noexcept;
 
-bool uncaught_exception()  noexcept;
+bool uncaught_exception()  noexcept;  // deprecated in C++17, removed in C++20
 int  uncaught_exceptions() noexcept;  // C++17
 
 typedef unspecified exception_ptr;
diff --git a/libcxx/modules/std/exception.inc b/libcxx/modules/std/exception.inc
index 8e802c7065e0f..0c110e6e743fb 100644
--- a/libcxx/modules/std/exception.inc
+++ b/libcxx/modules/std/exception.inc
@@ -21,6 +21,5 @@ export namespace std {
   using std::terminate;
   using std::terminate_handler;
   using std::throw_with_nested;
-  using std::uncaught_exception;
   using std::uncaught_exceptions;
 } // namespace std
diff --git a/libcxx/src/exception.cpp b/libcxx/src/exception.cpp
index ddb186bf8048f..ac6324cd9fe35 100644
--- a/libcxx/src/exception.cpp
+++ b/libcxx/src/exception.cpp
@@ -6,6 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define _LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
+#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
+
 #include <exception>
 #include <new>
 #include <typeinfo>
diff --git a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exception.pass.cpp b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exception.pass.cpp
index e368ce177b317..6fa4cb22070c8 100644
--- a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exception.pass.cpp
+++ b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exception.pass.cpp
@@ -7,6 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: no-exceptions
+
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
+
 // test uncaught_exception
 
 #include <exception>

>From efcd48865bc6fec7fc7fca7358bc9e40c15e4823 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sun, 4 Aug 2024 00:29:36 +0800
Subject: [PATCH 2/4] Address @mordante's review comments

- Remove meaningless `TODO`.
- Add `uncaught_exeption.depr_in_cxx17.verify.cpp`
---
 libcxx/docs/ReleaseNotes/20.rst               |  2 --
 ...uncaught_exeption.depr_in_cxx17.verify.cpp | 21 +++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp

diff --git a/libcxx/docs/ReleaseNotes/20.rst b/libcxx/docs/ReleaseNotes/20.rst
index 79079df6e061d..a6ec2d19e96b2 100644
--- a/libcxx/docs/ReleaseNotes/20.rst
+++ b/libcxx/docs/ReleaseNotes/20.rst
@@ -47,8 +47,6 @@ Improvements and New Features
 - The ``_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION`` macro has been added to make ``std::uncaught_exception``
   available.
 
-- TODO
-
 
 Deprecations and Removals
 -------------------------
diff --git a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp
new file mode 100644
index 0000000000000..a106b08f4689b
--- /dev/null
+++ b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <exception>
+
+// uncaught_exception
+//  deprecated in C++17
+
+// UNSUPPORTED: c++03, c++11, c++14
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
+
+#include <exception>
+
+void test() {
+    (void) std::uncaught_exception(); // expected-warning {{'uncaught_exception' is deprecated}}
+}

>From 5d8ef45d8b29a496088bdc0ba099eb979f09bb82 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sun, 4 Aug 2024 00:34:54 +0800
Subject: [PATCH 3/4] Clang-format

---
 .../uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp
index a106b08f4689b..070b582ffd086 100644
--- a/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp
+++ b/libcxx/test/std/language.support/support.exception/uncaught/uncaught_exeption.depr_in_cxx17.verify.cpp
@@ -17,5 +17,5 @@
 #include <exception>
 
 void test() {
-    (void) std::uncaught_exception(); // expected-warning {{'uncaught_exception' is deprecated}}
+  (void)std::uncaught_exception(); // expected-warning {{'uncaught_exception' is deprecated}}
 }

>From 7922ea66916747748fa34eeee06e582688d38232 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sun, 4 Aug 2024 21:13:08 +0800
Subject: [PATCH 4/4] Make `uncaught_exception` restorable in std modules

---
 libcxx/modules/std/exception.inc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libcxx/modules/std/exception.inc b/libcxx/modules/std/exception.inc
index 0c110e6e743fb..02b0f80190e5b 100644
--- a/libcxx/modules/std/exception.inc
+++ b/libcxx/modules/std/exception.inc
@@ -21,5 +21,8 @@ export namespace std {
   using std::terminate;
   using std::terminate_handler;
   using std::throw_with_nested;
+#ifdef _LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
+  using std::uncaught_exception;
+#endif
   using std::uncaught_exceptions;
 } // namespace std



More information about the libcxx-commits mailing list