Skip to content

Commit 4f8fe85

Browse files
committed
[libc++] Use availability to rely on key functions for bad_expected_access and bad_function_call
This patch uses our availability machinery to allow defining a key function for bad_function_call and bad_expected_access at all times but only rely on it when we can. This prevents compilers from complaining about weak vtables and reduces code bloat and the amount of work done by the dynamic linker. rdar://111917845
1 parent 0cd44ff commit 4f8fe85

8 files changed

+51
-24
lines changed

libcxx/include/__availability

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@
160160
# define _LIBCPP_AVAILABILITY_HAS_TZDB 1
161161
# define _LIBCPP_AVAILABILITY_TZDB
162162

163+
// These macros determine whether we assume that std::bad_function_call and
164+
// std::bad_expected_access provide a key function in the dylib. This allows
165+
// centralizing their vtable and typeinfo instead of having all TUs provide
166+
// a weak definition that then gets deduplicated.
167+
# define _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION 1
168+
# define _LIBCPP_AVAILABILITY_BAD_FUNCTION_CALL_KEY_FUNCTION
169+
# define _LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION 1
170+
# define _LIBCPP_AVAILABILITY_BAD_EXPECTED_ACCESS_KEY_FUNCTION
171+
163172
#elif defined(__APPLE__)
164173

165174
# define _LIBCPP_AVAILABILITY_HAS_BAD_OPTIONAL_ACCESS \
@@ -290,6 +299,13 @@
290299
# else
291300
# define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 1
292301
# endif
302+
303+
# define _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION 0
304+
# define _LIBCPP_AVAILABILITY_BAD_FUNCTION_CALL_KEY_FUNCTION __attribute__((unavailable))
305+
306+
# define _LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION 0
307+
# define _LIBCPP_AVAILABILITY_BAD_EXPECTED_ACCESS_KEY_FUNCTION __attribute__((unavailable))
308+
293309
#else
294310

295311
// ...New vendors can add availability markup here...

libcxx/include/__config

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@
120120
# define _LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB
121121
# define _LIBCPP_ABI_FORWARD_LIST_REMOVE_NODE_POINTER_UB
122122
# define _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
123-
// Define a key function for `bad_function_call` in the library, to centralize
124-
// its vtable and typeinfo to libc++ rather than having all other libraries
125-
// using that class define their own copies.
126-
# define _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
127123
// Override the default return value of exception::what() for
128124
// bad_function_call::what() with a string that is specific to
129125
// bad_function_call (see http://wg21.link/LWG2233). This is an ABI break
@@ -197,19 +193,6 @@
197193
# if defined(__FreeBSD__) && __FreeBSD__ < 14
198194
# define _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
199195
# endif
200-
// For XCOFF linkers, we have problems if we see a weak hidden version of a symbol
201-
// in user code (like you get with -fvisibility-inlines-hidden) and then a strong def
202-
// in the library, so we need to always rely on the library version.
203-
# if defined(_AIX)
204-
# define _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
205-
# endif
206-
# endif
207-
208-
# if defined(_LIBCPP_BUILDING_LIBRARY) || _LIBCPP_ABI_VERSION >= 2
209-
// Define a key function for `bad_function_call` in the library, to centralize
210-
// its vtable and typeinfo to libc++ rather than having all other libraries
211-
// using that class define their own copies.
212-
# define _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
213196
# endif
214197

215198
// We had some bugs where we use [[no_unique_address]] together with construct_at,

libcxx/include/__expected/bad_expected_access.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
1010
#define _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
1111

12+
#include <__availability>
1213
#include <__config>
1314
#include <__exception/exception.h>
1415
#include <__utility/move.h>
@@ -27,6 +28,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2728
template <class _Err>
2829
class bad_expected_access;
2930

31+
# if !_LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION
32+
_LIBCPP_DIAGNOSTIC_PUSH
33+
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
34+
# endif
3035
template <>
3136
class bad_expected_access<void> : public exception {
3237
protected:
@@ -38,12 +43,15 @@ class bad_expected_access<void> : public exception {
3843
_LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_expected_access() override = default;
3944

4045
public:
41-
// The way this has been designed (by using a class template below) means that we'll already
42-
// have a profusion of these vtables in TUs, and the dynamic linker will already have a bunch
43-
// of work to do. So it is not worth hiding the <void> specialization in the dylib, given that
44-
// it adds deployment target restrictions.
46+
# if _LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION
47+
const char* what() const noexcept override;
48+
# else
4549
_LIBCPP_HIDE_FROM_ABI_VIRTUAL const char* what() const noexcept override { return "bad access to std::expected"; }
50+
# endif
4651
};
52+
# if !_LIBCPP_AVAILABILITY_HAS_BAD_EXPECTED_ACCESS_KEY_FUNCTION
53+
_LIBCPP_DIAGNOSTIC_POP
54+
# endif
4755

4856
template <class _Err>
4957
class bad_expected_access : public bad_expected_access<void> {

libcxx/include/__functional/function.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___FUNCTIONAL_FUNCTION_H
1212

1313
#include <__assert>
14+
#include <__availability>
1415
#include <__config>
1516
#include <__exception/exception.h>
1617
#include <__functional/binary_function.h>
@@ -54,8 +55,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
5455

5556
// bad_function_call
5657

58+
# if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
5759
_LIBCPP_DIAGNOSTIC_PUSH
5860
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
61+
# endif
5962
class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
6063
public:
6164
_LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
@@ -64,7 +67,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
6467
// Note that when a key function is not used, every translation unit that uses
6568
// bad_function_call will end up containing a weak definition of the vtable and
6669
// typeinfo.
67-
# ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
70+
# if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
6871
~bad_function_call() _NOEXCEPT override;
6972
# else
7073
_LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
@@ -74,7 +77,9 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
7477
const char* what() const _NOEXCEPT override;
7578
# endif
7679
};
80+
# if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
7781
_LIBCPP_DIAGNOSTIC_POP
82+
# endif
7883

7984
_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {
8085
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS

libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,7 @@
20732073
{'is_defined': True, 'name': '__ZTINSt3__117moneypunct_bynameIwLb1EEE', 'size': 0, 'type': 'OBJECT'}
20742074
{'is_defined': True, 'name': '__ZTINSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
20752075
{'is_defined': True, 'name': '__ZTINSt3__119__shared_weak_countE', 'size': 0, 'type': 'OBJECT'}
2076+
{'is_defined': True, 'name': '__ZTINSt3__119bad_expected_accessIvEE', 'size': 0, 'type': 'OBJECT'}
20762077
{'is_defined': True, 'name': '__ZTINSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
20772078
{'is_defined': True, 'name': '__ZTINSt3__119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
20782079
{'is_defined': True, 'name': '__ZTINSt3__120__codecvt_utf8_utf16IDiEE', 'size': 0, 'type': 'OBJECT'}
@@ -2264,6 +2265,7 @@
22642265
{'is_defined': True, 'name': '__ZTSNSt3__117moneypunct_bynameIwLb0EEE', 'size': 0, 'type': 'OBJECT'}
22652266
{'is_defined': True, 'name': '__ZTSNSt3__117moneypunct_bynameIwLb1EEE', 'size': 0, 'type': 'OBJECT'}
22662267
{'is_defined': True, 'name': '__ZTSNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
2268+
{'is_defined': True, 'name': '__ZTSNSt3__119bad_expected_accessIvEE', 'size': 0, 'type': 'OBJECT'}
22672269
{'is_defined': True, 'name': '__ZTSNSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
22682270
{'is_defined': True, 'name': '__ZTSNSt3__119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
22692271
{'is_defined': True, 'name': '__ZTSNSt3__13pmr15memory_resourceE', 'size': 0, 'type': 'OBJECT'}
@@ -2482,6 +2484,7 @@
24822484
{'is_defined': True, 'name': '__ZTVNSt3__117moneypunct_bynameIwLb1EEE', 'size': 0, 'type': 'OBJECT'}
24832485
{'is_defined': True, 'name': '__ZTVNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
24842486
{'is_defined': True, 'name': '__ZTVNSt3__119__shared_weak_countE', 'size': 0, 'type': 'OBJECT'}
2487+
{'is_defined': True, 'name': '__ZTVNSt3__119bad_expected_accessIvEE', 'size': 0, 'type': 'OBJECT'}
24852488
{'is_defined': True, 'name': '__ZTVNSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
24862489
{'is_defined': True, 'name': '__ZTVNSt3__119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE', 'size': 0, 'type': 'OBJECT'}
24872490
{'is_defined': True, 'name': '__ZTVNSt3__120__codecvt_utf8_utf16IDiEE', 'size': 0, 'type': 'OBJECT'}

libcxx/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(LIBCXX_SOURCES
1010
chrono.cpp
1111
error_category.cpp
1212
exception.cpp
13+
expected.cpp
1314
filesystem/filesystem_clock.cpp
1415
filesystem/filesystem_error.cpp
1516
filesystem/path_parser.h

libcxx/src/expected.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
#include <expected>
10+
11+
_LIBCPP_BEGIN_NAMESPACE_STD
12+
const char* bad_expected_access<void>::what() const noexcept { return "bad access to std::expected"; }
13+
_LIBCPP_END_NAMESPACE_STD

libcxx/src/functional.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
_LIBCPP_BEGIN_NAMESPACE_STD
1212

13-
#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1413
bad_function_call::~bad_function_call() noexcept {}
15-
#endif
1614

1715
#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
1816
const char* bad_function_call::what() const noexcept { return "std::bad_function_call"; }

0 commit comments

Comments
 (0)