Skip to content

Commit 4ea2c73

Browse files
[libc++] Deprecate and remove std::uncaught_exception (#101830)
Works towards P0619R4/#99985. - 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. - _LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION restores std::uncaught_exception. - As a drive-by, this patch updates the C++20 status page to explain that D.11 is already done, since it was done in 578d09c.
1 parent fc51797 commit 4ea2c73

File tree

12 files changed

+43
-5
lines changed

12 files changed

+43
-5
lines changed

libcxx/docs/ReleaseNotes/20.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Improvements and New Features
4747
- The ``lexicographical_compare`` and ``ranges::lexicographical_compare`` algorithms have been optimized for trivially
4848
equality comparable types, resulting in a performance improvement of up to 40x.
4949

50+
- The ``_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION`` macro has been added to make ``std::uncaught_exception`` available in C++20 and later modes.
51+
5052

5153
Deprecations and Removals
5254
-------------------------

libcxx/docs/Status/Cxx17.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Paper Status
3838

3939
.. note::
4040

41+
.. [#note-N4259] N4259: ``std::uncaught_exception`` is deprecated since LLVM 20.0.
4142
.. [#note-P0067R5] P0067R5: ``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``.
4243
.. [#note-P0226] P0226: Progress is tracked `here <https://github.com/llvm/llvm-project/issues/99939>`_.
4344
.. [#note-P0607] P0607: The parts of P0607 that are not done are the ``<regex>`` bits.

libcxx/docs/Status/Cxx17Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"`N4169 <https://wg21.link/N4169>`__","A proposal to add invoke function template","2014-11 (Urbana)","|Complete|","3.7",""
55
"`N4190 <https://wg21.link/N4190>`__","Removing auto_ptr, random_shuffle(), And Old <functional> Stuff.","2014-11 (Urbana)","|Complete|","15.0",""
66
"`N4258 <https://wg21.link/N4258>`__","Cleaning-up noexcept in the Library.","2014-11 (Urbana)","|In Progress|","3.7",""
7-
"`N4259 <https://wg21.link/N4259>`__","Wording for std::uncaught_exceptions","2014-11 (Urbana)","|Complete|","3.7",""
7+
"`N4259 <https://wg21.link/N4259>`__","Wording for std::uncaught_exceptions","2014-11 (Urbana)","|Complete| [#note-N4259]_","3.7",""
88
"`N4277 <https://wg21.link/N4277>`__","TriviallyCopyable ``reference_wrapper``\ .","2014-11 (Urbana)","|Complete|","3.2",""
99
"`N4279 <https://wg21.link/N4279>`__","Improved insertion interface for unique-key maps.","2014-11 (Urbana)","|Complete|","3.7",""
1010
"`N4280 <https://wg21.link/N4280>`__","Non-member size() and more","2014-11 (Urbana)","|Complete|","3.6",""

libcxx/docs/Status/Cxx20.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Paper Status
4242
.. [#note-P0645] P0645: The implementation was complete since Clang 14,
4343
except the feature-test macro was not set until Clang 19.
4444
.. [#note-P0966] P0966: It was previously erroneously marked as complete in version 8.0. See `bug 45368 <https://llvm.org/PR45368>`__.
45-
.. [#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.
45+
.. [#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.
4646
.. [#note-P0883.1] P0883: shared_ptr and floating-point changes weren't applied as they themselves aren't implemented yet.
4747
.. [#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.
4848
.. [#note-P0660] P0660: The paper is implemented but the features are experimental and can be enabled via ``-fexperimental-library``.

libcxx/docs/UserDocumentation.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ C++17 Specific Configuration Macros
163163

164164
C++20 Specific Configuration Macros
165165
-----------------------------------
166+
**_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION**:
167+
This macro is used to re-enable `uncaught_exception`.
168+
166169
**_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE**:
167170
This macro is used to re-enable the function
168171
``std::shared_ptr<...>::unique()``.

libcxx/include/__exception/operations.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ using terminate_handler = void (*)();
2929
_LIBCPP_EXPORTED_FROM_ABI terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
3030
_LIBCPP_EXPORTED_FROM_ABI terminate_handler get_terminate() _NOEXCEPT;
3131

32-
_LIBCPP_EXPORTED_FROM_ABI bool uncaught_exception() _NOEXCEPT;
32+
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION)
33+
_LIBCPP_EXPORTED_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 bool uncaught_exception() _NOEXCEPT;
34+
#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION)
3335
_LIBCPP_EXPORTED_FROM_ABI int uncaught_exceptions() _NOEXCEPT;
3436

3537
class _LIBCPP_EXPORTED_FROM_ABI exception_ptr;

libcxx/include/__ostream/basic_ostream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& _
152152

153153
template <class _CharT, class _Traits>
154154
basic_ostream<_CharT, _Traits>::sentry::~sentry() {
155-
if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {
155+
if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) {
156156
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
157157
try {
158158
#endif // _LIBCPP_HAS_NO_EXCEPTIONS

libcxx/include/exception

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ terminate_handler set_terminate(terminate_handler f ) noexcept;
4747
terminate_handler get_terminate() noexcept;
4848
[[noreturn]] void terminate() noexcept;
4949
50-
bool uncaught_exception() noexcept;
50+
bool uncaught_exception() noexcept; // deprecated in C++17, removed in C++20
5151
int uncaught_exceptions() noexcept; // C++17
5252
5353
typedef unspecified exception_ptr;

libcxx/modules/std/exception.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export namespace std {
2121
using std::terminate;
2222
using std::terminate_handler;
2323
using std::throw_with_nested;
24+
#ifdef _LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
2425
using std::uncaught_exception;
26+
#endif
2527
using std::uncaught_exceptions;
2628
} // namespace std

libcxx/src/exception.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#define _LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
10+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
11+
912
#include <exception>
1013
#include <new>
1114
#include <typeinfo>

libcxx/test/std/language.support/support.exception/uncaught/uncaught_exception.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
// UNSUPPORTED: no-exceptions
10+
11+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
12+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
13+
1014
// test uncaught_exception
1115

1216
#include <exception>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <exception>
10+
11+
// uncaught_exception
12+
// deprecated in C++17
13+
14+
// UNSUPPORTED: c++03, c++11, c++14
15+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION
16+
17+
#include <exception>
18+
19+
void test() {
20+
(void)std::uncaught_exception(); // expected-warning {{'uncaught_exception' is deprecated}}
21+
}

0 commit comments

Comments
 (0)