Skip to content

Commit ae5ed5d

Browse files
committed
[libc++] Bypass calling exception-throwing functions in the dylib with -fno-exceptions
basic_string and vector currently have a hard dependency on the compiled library because they need to call __vector_base_common::__throw_xxx(), which are externally instantiated in the compiled library. That makes sense when exceptions are enabled (because we're trying to localize the exception-throwing code to the compiled library), but it doesn't really make sense when exceptions are disabled, and the __throw_xxx functions are just calling abort() anyways. This patch simply overrides the __throw_xxx() functions so that they don't rely on the compiled library when exceptions are disabled. Differential Revision: https://reviews.llvm.org/D108389 (cherry picked from commit f3bc0e5)
1 parent 23ba373 commit ae5ed5d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

libcxx/include/string

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++1
522522
#include <algorithm>
523523
#include <compare>
524524
#include <cstdio> // EOF
525+
#include <cstdlib>
525526
#include <cstring>
526527
#include <cwchar>
527528
#include <initializer_list>
@@ -1714,6 +1715,24 @@ private:
17141715
return data() <= __p && __p <= data() + size();
17151716
}
17161717

1718+
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1719+
void __throw_length_error() const {
1720+
#ifndef _LIBCPP_NO_EXCEPTIONS
1721+
__basic_string_common<true>::__throw_length_error();
1722+
#else
1723+
_VSTD::abort();
1724+
#endif
1725+
}
1726+
1727+
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1728+
void __throw_out_of_range() const {
1729+
#ifndef _LIBCPP_NO_EXCEPTIONS
1730+
__basic_string_common<true>::__throw_out_of_range();
1731+
#else
1732+
_VSTD::abort();
1733+
#endif
1734+
}
1735+
17171736
friend basic_string operator+<>(const basic_string&, const basic_string&);
17181737
friend basic_string operator+<>(const value_type*, const basic_string&);
17191738
friend basic_string operator+<>(value_type, const basic_string&);

libcxx/include/vector

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ erase_if(vector<T, Allocator>& c, Predicate pred); // C++20
281281
#include <algorithm>
282282
#include <climits>
283283
#include <compare>
284+
#include <cstdlib>
284285
#include <cstring>
285286
#include <initializer_list>
286287
#include <iosfwd> // for forward declaration of vector
@@ -390,6 +391,25 @@ protected:
390391
is_nothrow_move_assignable<allocator_type>::value)
391392
{__move_assign_alloc(__c, integral_constant<bool,
392393
__alloc_traits::propagate_on_container_move_assignment::value>());}
394+
395+
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
396+
void __throw_length_error() const {
397+
#ifndef _LIBCPP_NO_EXCEPTIONS
398+
__vector_base_common<true>::__throw_length_error();
399+
#else
400+
_VSTD::abort();
401+
#endif
402+
}
403+
404+
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
405+
void __throw_out_of_range() const {
406+
#ifndef _LIBCPP_NO_EXCEPTIONS
407+
__vector_base_common<true>::__throw_out_of_range();
408+
#else
409+
_VSTD::abort();
410+
#endif
411+
}
412+
393413
private:
394414
_LIBCPP_INLINE_VISIBILITY
395415
void __copy_assign_alloc(const __vector_base& __c, true_type)

0 commit comments

Comments
 (0)