diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table index 2b246f82ce36d..74923ddb74e9c 100644 --- a/libcxx/include/__hash_table +++ b/libcxx/include/__hash_table @@ -44,6 +44,7 @@ #include <__utility/forward.h> #include <__utility/move.h> #include <__utility/pair.h> +#include <__utility/scope_guard.h> #include <__utility/swap.h> #include <__utility/try_key_extraction.h> #include @@ -1317,23 +1318,14 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u, max_load_factor() = __u.max_load_factor(); if (bucket_count() != 0) { __next_pointer __cache = __detach(); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - const_iterator __i = __u.begin(); - while (__cache != nullptr && __u.size() != 0) { - __assign_value(__cache->__upcast()->__get_value(), std::move(__u.remove(__i++)->__get_value())); - __next_pointer __next = __cache->__next_; - __node_insert_multi(__cache->__upcast()); - __cache = __next; - } -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __deallocate_node(__cache); - throw; + auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); }); + const_iterator __i = __u.begin(); + while (__cache != nullptr && __u.size() != 0) { + __assign_value(__cache->__upcast()->__get_value(), std::move(__u.remove(__i++)->__get_value())); + __next_pointer __next = __cache->__next_; + __node_insert_multi(__cache->__upcast()); + __cache = __next; } -#endif // _LIBCPP_HAS_EXCEPTIONS - __deallocate_node(__cache); } const_iterator __i = __u.begin(); while (__u.size() != 0) @@ -1361,22 +1353,13 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __ if (bucket_count() != 0) { __next_pointer __cache = __detach(); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - for (; __cache != nullptr && __first != __last; ++__first) { - __assign_value(__cache->__upcast()->__get_value(), *__first); - __next_pointer __next = __cache->__next_; - __node_insert_unique(__cache->__upcast()); - __cache = __next; - } -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __deallocate_node(__cache); - throw; + auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); }); + for (; __cache != nullptr && __first != __last; ++__first) { + __assign_value(__cache->__upcast()->__get_value(), *__first); + __next_pointer __next = __cache->__next_; + __node_insert_unique(__cache->__upcast()); + __cache = __next; } -#endif // _LIBCPP_HAS_EXCEPTIONS - __deallocate_node(__cache); } for (; __first != __last; ++__first) __emplace_unique(*__first); @@ -1391,22 +1374,13 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __f "__assign_multi may only be called with the containers value type or the nodes value type"); if (bucket_count() != 0) { __next_pointer __cache = __detach(); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - for (; __cache != nullptr && __first != __last; ++__first) { - __assign_value(__cache->__upcast()->__get_value(), *__first); - __next_pointer __next = __cache->__next_; - __node_insert_multi(__cache->__upcast()); - __cache = __next; - } -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __deallocate_node(__cache); - throw; + auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); }); + for (; __cache != nullptr && __first != __last; ++__first) { + __assign_value(__cache->__upcast()->__get_value(), *__first); + __next_pointer __next = __cache->__next_; + __node_insert_multi(__cache->__upcast()); + __cache = __next; } -#endif // _LIBCPP_HAS_EXCEPTIONS - __deallocate_node(__cache); } for (; __first != __last; ++__first) __emplace_multi(*__first); diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h index 0cbd995105671..e90db587d2836 100644 --- a/libcxx/include/__memory/shared_ptr.h +++ b/libcxx/include/__memory/shared_ptr.h @@ -54,6 +54,7 @@ #include <__type_traits/remove_extent.h> #include <__type_traits/remove_reference.h> #include <__utility/declval.h> +#include <__utility/exception_guard.h> #include <__utility/forward.h> #include <__utility/move.h> #include <__utility/swap.h> @@ -352,23 +353,16 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr { template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; - typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk; + auto __guard = std::__make_exception_guard([&] { __d(__p); }); + typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; + typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk; #ifndef _LIBCPP_CXX03_LANG - __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); + __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); #else __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); #endif // not _LIBCPP_CXX03_LANG - __enable_weak_this(__p, __p); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __d(__p); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + __enable_weak_this(__p, __p); + __guard.__complete(); } template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; - typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; - typedef __allocator_destructor<_A2> _D2; - _A2 __a2(__a); - unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); - ::new ((void*)std::addressof(*__hold2.get())) + auto __guard = std::__make_exception_guard([&] { __d(__p); }); + typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; + typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; + typedef __allocator_destructor<_A2> _D2; + _A2 __a2(__a); + unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); + ::new ((void*)std::addressof(*__hold2.get())) #ifndef _LIBCPP_CXX03_LANG - _CntrlBlk(__p, std::move(__d), __a); + _CntrlBlk(__p, std::move(__d), __a); #else _CntrlBlk(__p, __d, __a); #endif // not _LIBCPP_CXX03_LANG - __cntrl_ = std::addressof(*__hold2.release()); - __enable_weak_this(__p, __p); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __d(__p); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + __cntrl_ = std::addressof(*__hold2.release()); + __enable_weak_this(__p, __p); + __guard.__complete(); } template @@ -406,22 +393,15 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr { _Dp __d, __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag()) : __ptr_(nullptr) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; - typedef __shared_ptr_pointer _CntrlBlk; + auto __guard = std::__make_exception_guard([&] { __d(__p); }); + typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; + typedef __shared_ptr_pointer _CntrlBlk; #ifndef _LIBCPP_CXX03_LANG - __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); + __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); #else __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); #endif // not _LIBCPP_CXX03_LANG -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __d(__p); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); } template @@ -431,27 +411,20 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr { _Alloc __a, __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag()) : __ptr_(nullptr) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - typedef __shared_ptr_pointer _CntrlBlk; - typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; - typedef __allocator_destructor<_A2> _D2; - _A2 __a2(__a); - unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); - ::new ((void*)std::addressof(*__hold2.get())) + auto __guard = std::__make_exception_guard([&] { __d(__p); }); + typedef __shared_ptr_pointer _CntrlBlk; + typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; + typedef __allocator_destructor<_A2> _D2; + _A2 __a2(__a); + unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); + ::new ((void*)std::addressof(*__hold2.get())) #ifndef _LIBCPP_CXX03_LANG - _CntrlBlk(__p, std::move(__d), __a); + _CntrlBlk(__p, std::move(__d), __a); #else _CntrlBlk(__p, __d, __a); #endif // not _LIBCPP_CXX03_LANG - __cntrl_ = std::addressof(*__hold2.release()); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __d(__p); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + __cntrl_ = std::addressof(*__hold2.release()); + __guard.__complete(); } template diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h index e80236640052c..34d065dc973e5 100644 --- a/libcxx/include/__memory/uninitialized_algorithms.h +++ b/libcxx/include/__memory/uninitialized_algorithms.h @@ -61,17 +61,10 @@ template __uninitialized_copy( _InputIterator __ifirst, _Sentinel1 __ilast, _ForwardIterator __ofirst, _EndPredicate __stop_copying) { _ForwardIterator __idx = __ofirst; -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif - for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__ofirst, __idx); - throw; - } -#endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); + for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx) + ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); + __guard.__complete(); return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx)); } @@ -91,17 +84,10 @@ template __uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_copying) { _ForwardIterator __idx = __ofirst; -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif - for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__ofirst, __idx); - throw; - } -#endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); + for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n) + ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); + __guard.__complete(); return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx)); } @@ -121,17 +107,10 @@ template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) { _ForwardIterator __idx = __first; -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif - for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -#endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __idx != __last; ++__idx) + ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); + __guard.__complete(); return __idx; } @@ -149,17 +128,10 @@ template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { _ForwardIterator __idx = __first; -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif - for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -#endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __n > 0; ++__idx, (void)--__n) + ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); + __guard.__complete(); return __idx; } @@ -178,18 +150,11 @@ uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) { - auto __idx = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType; -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -# endif + auto __idx = __first; + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __idx != __last; ++__idx) + ::new (static_cast(std::addressof(*__idx))) _ValueType; + __guard.__complete(); return __idx; } @@ -205,17 +170,10 @@ inline _LIBCPP_HIDE_FROM_ABI void uninitialized_default_construct(_ForwardIterat template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) { auto __idx = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType; -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -# endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __n > 0; ++__idx, (void)--__n) + ::new (static_cast(std::addressof(*__idx))) _ValueType; + __guard.__complete(); return __idx; } @@ -231,18 +189,11 @@ inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator uninitialized_default_construct_n( template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) { - auto __idx = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -# endif + auto __idx = __first; + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __idx != __last; ++__idx) + ::new (static_cast(std::addressof(*__idx))) _ValueType(); + __guard.__complete(); return __idx; } @@ -258,17 +209,10 @@ inline _LIBCPP_HIDE_FROM_ABI void uninitialized_value_construct(_ForwardIterator template inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) { auto __idx = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__first, __idx); - throw; - } -# endif + auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); + for (; __n > 0; ++__idx, (void)--__n) + ::new (static_cast(std::addressof(*__idx))) _ValueType(); + __guard.__complete(); return __idx; } @@ -293,19 +237,12 @@ inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitiali _ForwardIterator __ofirst, _EndPredicate __stop_moving, _IterMove __iter_move) { - auto __idx = __ofirst; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) { - ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__ofirst, __idx); - throw; + auto __idx = __ofirst; + auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); + for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) { + ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); } -# endif + __guard.__complete(); return {std::move(__ifirst), std::move(__idx)}; } @@ -331,18 +268,11 @@ template inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitialized_move_n( _InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_moving, _IterMove __iter_move) { - auto __idx = __ofirst; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - std::__destroy(__ofirst, __idx); - throw; - } -# endif + auto __idx = __ofirst; + auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); + for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n) + ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); + __guard.__complete(); return {std::move(__ifirst), std::move(__idx)}; } diff --git a/libcxx/include/__utility/scope_guard.h b/libcxx/include/__utility/scope_guard.h index 3972102eee891..db4f0e4c73a2a 100644 --- a/libcxx/include/__utility/scope_guard.h +++ b/libcxx/include/__utility/scope_guard.h @@ -43,6 +43,8 @@ class __scope_guard { #endif }; +_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__scope_guard); + template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __scope_guard<_Func> __make_scope_guard(_Func __func) { return __scope_guard<_Func>(std::move(__func)); diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h index 66f5fd9498eec..b84bc02d6279b 100644 --- a/libcxx/include/__vector/vector_bool.h +++ b/libcxx/include/__vector/vector_bool.h @@ -960,21 +960,14 @@ vector::__insert_with_sentinel(const_iterator __position, _Inp } vector __v(get_allocator()); if (__first != __last) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - __v.__assign_with_sentinel(std::move(__first), std::move(__last)); - difference_type __old_size = static_cast(__old_end - begin()); - difference_type __old_p = __p - begin(); - reserve(__recommend(size() + __v.size())); - __p = begin() + __old_p; - __old_end = begin() + __old_size; -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - erase(__old_end, end()); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { erase(__old_end, end()); }); + __v.__assign_with_sentinel(std::move(__first), std::move(__last)); + difference_type __old_size = static_cast(__old_end - begin()); + difference_type __old_p = __p - begin(); + reserve(__recommend(size() + __v.size())); + __p = begin() + __old_p; + __old_end = begin() + __old_size; + __guard.__complete(); } __p = std::rotate(__p, __old_end, end()); insert(__p, __v.begin(), __v.end()); diff --git a/libcxx/include/deque b/libcxx/include/deque index cfb64b4f07332..d0c3679fbfc1d 100644 --- a/libcxx/include/deque +++ b/libcxx/include/deque @@ -235,6 +235,7 @@ template # include <__type_traits/is_swappable.h> # include <__type_traits/is_trivially_relocatable.h> # include <__type_traits/type_identity.h> +# include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/move.h> # include <__utility/pair.h> @@ -2135,22 +2136,17 @@ void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) { size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty(); __split_buffer __buf( std::max(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__get_allocator()); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (; __nb > 0; --__nb) { - __buf.emplace_back(__alloc_traits::allocate(__a, __block_size)); - // ASan: this is empty container, we have to poison whole block - __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size)); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { __annotate_delete(); for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i) __alloc_traits::deallocate(__a, *__i, __block_size); - throw; + }); + for (; __nb > 0; --__nb) { + __buf.emplace_back(__alloc_traits::allocate(__a, __block_size)); + // ASan: this is empty container, we have to poison whole block + __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size)); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); for (; __back_capacity > 0; --__back_capacity) { __buf.emplace_back(__map_.back()); __map_.pop_back(); @@ -2254,22 +2250,17 @@ void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) { std::max(2 * __map_.capacity(), __nb + __map_.size()), __map_.size() - __front_capacity, __map_.__get_allocator()); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (; __nb > 0; --__nb) { - __buf.emplace_back(__alloc_traits::allocate(__a, __block_size)); - // ASan: this is an empty container, we have to poison the whole block - __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size)); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { __annotate_delete(); for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i) __alloc_traits::deallocate(__a, *__i, __block_size); - throw; + }); + for (; __nb > 0; --__nb) { + __buf.emplace_back(__alloc_traits::allocate(__a, __block_size)); + // ASan: this is an empty container, we have to poison the whole block + __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size)); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); for (; __front_capacity > 0; --__front_capacity) { __buf.emplace_back(__map_.front()); __map_.pop_front(); diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list index 0a0bfa7a7f037..7fd111b373a72 100644 --- a/libcxx/include/forward_list +++ b/libcxx/include/forward_list @@ -235,6 +235,7 @@ template # include <__type_traits/is_swappable.h> # include <__type_traits/remove_cv.h> # include <__type_traits/type_identity.h> +# include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/move.h> # include <__utility/swap.h> @@ -1180,22 +1181,17 @@ forward_list<_Tp, _Alloc>::__insert_after(const_iterator __p, size_type __n, _Ar if (__n > 0) { __node_pointer __first = this->__create_node(/* next = */ nullptr, std::forward<_Args>(__args)...); __node_pointer __last = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (--__n; __n != 0; --__n, __last = __last->__next_) { - __last->__next_ = this->__create_node(/* next = */ nullptr, std::forward<_Args>(__args)...); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (__first != nullptr) { __node_pointer __next = __first->__next_; this->__delete_node(__first); __first = __next; } - throw; + }); + for (--__n; __n != 0; --__n, __last = __last->__next_) { + __last->__next_ = this->__create_node(/* next = */ nullptr, std::forward<_Args>(__args)...); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __last->__next_ = __r->__next_; __r->__next_ = __first; __r = std::__static_fancy_pointer_cast<__begin_node_pointer>(__last); @@ -1220,22 +1216,17 @@ forward_list<_Tp, _Alloc>::__insert_after_with_sentinel(const_iterator __p, _Inp __node_pointer __first = this->__create_node(/* next = */ nullptr, *__f); __node_pointer __last = __first; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (++__f; __f != __l; ++__f, ((void)(__last = __last->__next_))) { - __last->__next_ = this->__create_node(/* next = */ nullptr, *__f); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (__first != nullptr) { __node_pointer __next = __first->__next_; this->__delete_node(__first); __first = __next; } - throw; + }); + for (++__f; __f != __l; ++__f, ((void)(__last = __last->__next_))) { + __last->__next_ = this->__create_node(/* next = */ nullptr, *__f); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __last->__next_ = __r->__next_; __r->__next_ = __first; diff --git a/libcxx/include/future b/libcxx/include/future index 3df9dc9349a01..4b7c09841cbd3 100644 --- a/libcxx/include/future +++ b/libcxx/include/future @@ -403,6 +403,7 @@ template struct uses_allocator, Alloc>; # include <__type_traits/strip_signature.h> # include <__type_traits/underlying_type.h> # include <__utility/auto_cast.h> +# include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/move.h> # include <__utility/swap.h> @@ -1815,16 +1816,9 @@ template _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) { unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h( new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f))); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif - std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach(); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __h->__make_ready(); - throw; - } -# endif + auto __guard = std::__make_exception_guard([&] { __h->__make_ready(); }); + std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach(); + __guard.__complete(); return future<_Rp>(__h.get()); } diff --git a/libcxx/include/list b/libcxx/include/list index 5d8067545b9c7..4b0b8e046a94e 100644 --- a/libcxx/include/list +++ b/libcxx/include/list @@ -237,6 +237,7 @@ template # include <__type_traits/is_pointer.h> # include <__type_traits/is_same.h> # include <__type_traits/type_identity.h> +# include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/move.h> # include <__utility/swap.h> @@ -1233,14 +1234,7 @@ list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& _ ++__ds; __r = iterator(__node->__as_link()); iterator __e = __r; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { - __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (true) { __base_pointer __prev = __e.__ptr_->__prev_; __node_pointer __current = __e.__ptr_->__as_node(); @@ -1249,9 +1243,11 @@ list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& _ break; __e = iterator(__prev); } - throw; + }); + for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { + __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); this->__size_ += __ds; } @@ -1276,14 +1272,7 @@ list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Se ++__ds; __r = iterator(__node->__as_link()); iterator __e = __r; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) { - __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link(); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (true) { __base_pointer __prev = __e.__ptr_->__prev_; __node_pointer __current = __e.__ptr_->__as_node(); @@ -1292,9 +1281,11 @@ list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Se break; __e = iterator(__prev); } - throw; + }); + for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) { + __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link(); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); this->__size_ += __ds; } @@ -1452,14 +1443,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX26 void list<_Tp, _Alloc>::resize(size_type __n) { ++__ds; iterator __r = iterator(__node->__as_link()); iterator __e = __r; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { - __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link(); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (true) { __base_pointer __prev = __e.__ptr_->__prev_; __node_pointer __current = __e.__ptr_->__as_node(); @@ -1468,9 +1452,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX26 void list<_Tp, _Alloc>::resize(size_type __n) { break; __e = iterator(__prev); } - throw; + }); + for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { + __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link(); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __link_nodes_at_back(__r.__ptr_, __e.__ptr_); this->__size_ += __ds; } @@ -1488,14 +1474,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX26 void list<_Tp, _Alloc>::resize(size_type __n, cons __base_pointer __nl = __node->__as_link(); iterator __r = iterator(__nl); iterator __e = __r; -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { - __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); - } -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { + auto __guard = std::__make_exception_guard([&] { while (true) { __base_pointer __prev = __e.__ptr_->__prev_; __node_pointer __current = __e.__ptr_->__as_node(); @@ -1504,9 +1483,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX26 void list<_Tp, _Alloc>::resize(size_type __n, cons break; __e = iterator(__prev); } - throw; + }); + for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { + __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); } -# endif // _LIBCPP_HAS_EXCEPTIONS + __guard.__complete(); __link_nodes(__base::__end_as_link(), __r.__ptr_, __e.__ptr_); this->__size_ += __ds; } diff --git a/libcxx/include/string b/libcxx/include/string index cfd6861e5c9c2..abfb84ead2130 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -638,6 +638,7 @@ basic_string operator""s( const char32_t *str, size_t len ); # include <__type_traits/is_trivially_relocatable.h> # include <__type_traits/remove_cvref.h> # include <__utility/default_three_way_comparator.h> +# include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/is_pointer_in_range.h> # include <__utility/move.h> @@ -2636,17 +2637,10 @@ basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator _ __rep_ = __rep(); __annotate_new(0); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (; __first != __last; ++__first) - push_back(*__first); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __reset_internal_buffer(); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); }); + for (; __first != __last; ++__first) + push_back(*__first); + __guard.__complete(); } template @@ -2663,17 +2657,10 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) { pointer __p = __init_internal_buffer(__sz); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p)); - traits_type::assign(*__end, value_type()); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __reset_internal_buffer(); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); }); + auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p)); + traits_type::assign(*__end, value_type()); + __guard.__complete(); } template diff --git a/libcxx/include/valarray b/libcxx/include/valarray index 96501caaff041..215811d5ba475 100644 --- a/libcxx/include/valarray +++ b/libcxx/include/valarray @@ -362,6 +362,7 @@ template unspecified2 end(const valarray& v); # include <__memory/uninitialized_algorithms.h> # include <__type_traits/decay.h> # include <__type_traits/remove_reference.h> +# include <__utility/exception_guard.h> # include <__utility/move.h> # include <__utility/swap.h> # include @@ -1992,17 +1993,10 @@ template inline valarray<_Tp>::valarray(size_t __n) : __begin_(nullptr), __end_(nullptr) { if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (size_t __n_left = __n; __n_left; --__n_left, ++__end_) - ::new ((void*)__end_) value_type(); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + for (size_t __n_left = __n; __n_left; --__n_left, ++__end_) + ::new ((void*)__end_) value_type(); + __guard.__complete(); } } @@ -2015,17 +2009,10 @@ template valarray<_Tp>::valarray(const value_type* __p, size_t __n) : __begin_(nullptr), __end_(nullptr) { if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (size_t __n_left = __n; __n_left; ++__end_, ++__p, --__n_left) - ::new ((void*)__end_) value_type(*__p); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + for (size_t __n_left = __n; __n_left; ++__end_, ++__p, --__n_left) + ::new ((void*)__end_) value_type(*__p); + __guard.__complete(); } } @@ -2033,17 +2020,10 @@ template valarray<_Tp>::valarray(const valarray& __v) : __begin_(nullptr), __end_(nullptr) { if (__v.size()) { __begin_ = __end_ = allocator().allocate(__v.size()); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (value_type* __p = __v.__begin_; __p != __v.__end_; ++__end_, ++__p) - ::new ((void*)__end_) value_type(*__p); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__v.size()); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__v.size()); }); + for (value_type* __p = __v.__begin_; __p != __v.__end_; ++__end_, ++__p) + ::new ((void*)__end_) value_type(*__p); + __guard.__complete(); } } @@ -2059,18 +2039,11 @@ valarray<_Tp>::valarray(initializer_list __il) : __begin_(nullptr), const size_t __n = __il.size(); if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - size_t __n_left = __n; - for (const value_type* __p = __il.begin(); __n_left; ++__end_, ++__p, --__n_left) - ::new ((void*)__end_) value_type(*__p); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + size_t __n_left = __n; + for (const value_type* __p = __il.begin(); __n_left; ++__end_, ++__p, --__n_left) + ::new ((void*)__end_) value_type(*__p); + __guard.__complete(); } } @@ -2081,18 +2054,11 @@ valarray<_Tp>::valarray(const slice_array& __sa) : __begin_(nullptr) const size_t __n = __sa.__size_; if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - size_t __n_left = __n; - for (const value_type* __p = __sa.__vp_; __n_left; ++__end_, __p += __sa.__stride_, --__n_left) - ::new ((void*)__end_) value_type(*__p); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + size_t __n_left = __n; + for (const value_type* __p = __sa.__vp_; __n_left; ++__end_, __p += __sa.__stride_, --__n_left) + ::new ((void*)__end_) value_type(*__p); + __guard.__complete(); } } @@ -2101,19 +2067,12 @@ valarray<_Tp>::valarray(const gslice_array& __ga) : __begin_(nullptr const size_t __n = __ga.__1d_.size(); if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - typedef const size_t* _Ip; - const value_type* __s = __ga.__vp_; - for (_Ip __i = __ga.__1d_.__begin_, __e = __ga.__1d_.__end_; __i != __e; ++__i, ++__end_) - ::new ((void*)__end_) value_type(__s[*__i]); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + typedef const size_t* _Ip; + const value_type* __s = __ga.__vp_; + for (_Ip __i = __ga.__1d_.__begin_, __e = __ga.__1d_.__end_; __i != __e; ++__i, ++__end_) + ::new ((void*)__end_) value_type(__s[*__i]); + __guard.__complete(); } } @@ -2122,19 +2081,12 @@ valarray<_Tp>::valarray(const mask_array& __ma) : __begin_(nullptr), const size_t __n = __ma.__1d_.size(); if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - typedef const size_t* _Ip; - const value_type* __s = __ma.__vp_; - for (_Ip __i = __ma.__1d_.__begin_, __e = __ma.__1d_.__end_; __i != __e; ++__i, ++__end_) - ::new ((void*)__end_) value_type(__s[*__i]); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + typedef const size_t* _Ip; + const value_type* __s = __ma.__vp_; + for (_Ip __i = __ma.__1d_.__begin_, __e = __ma.__1d_.__end_; __i != __e; ++__i, ++__end_) + ::new ((void*)__end_) value_type(__s[*__i]); + __guard.__complete(); } } @@ -2143,19 +2095,12 @@ valarray<_Tp>::valarray(const indirect_array& __ia) : __begin_(nullp const size_t __n = __ia.__1d_.size(); if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - typedef const size_t* _Ip; - const value_type* __s = __ia.__vp_; - for (_Ip __i = __ia.__1d_.__begin_, __e = __ia.__1d_.__end_; __i != __e; ++__i, ++__end_) - ::new ((void*)__end_) value_type(__s[*__i]); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + typedef const size_t* _Ip; + const value_type* __s = __ia.__vp_; + for (_Ip __i = __ia.__1d_.__begin_, __e = __ia.__1d_.__end_; __i != __e; ++__i, ++__end_) + ::new ((void*)__end_) value_type(__s[*__i]); + __guard.__complete(); } } @@ -2644,17 +2589,10 @@ void valarray<_Tp>::resize(size_t __n, value_type __x) { __clear(size()); if (__n) { __begin_ = __end_ = allocator().allocate(__n); -# if _LIBCPP_HAS_EXCEPTIONS - try { -# endif // _LIBCPP_HAS_EXCEPTIONS - for (size_t __n_left = __n; __n_left; --__n_left, ++__end_) - ::new ((void*)__end_) value_type(__x); -# if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - __clear(__n); - throw; - } -# endif // _LIBCPP_HAS_EXCEPTIONS + auto __guard = std::__make_exception_guard([&] { __clear(__n); }); + for (size_t __n_left = __n; __n_left; --__n_left, ++__end_) + ::new ((void*)__end_) value_type(__x); + __guard.__complete(); } } diff --git a/libcxx/src/filesystem/error.h b/libcxx/src/filesystem/error.h index 52a18b2becdbf..db5d1ae9a7250 100644 --- a/libcxx/src/filesystem/error.h +++ b/libcxx/src/filesystem/error.h @@ -128,17 +128,8 @@ struct ErrorHandler { T report(const error_code& ec, const char* msg, ...) const { va_list ap; va_start(ap, msg); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - report_impl(ec, msg, ap); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - va_end(ap); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS - va_end(ap); + __scope_guard guard([&] { va_end(ap); }); + report_impl(ec, msg, ap); return error_value(); } @@ -148,17 +139,8 @@ struct ErrorHandler { T report(errc const& err, const char* msg, ...) const { va_list ap; va_start(ap, msg); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - report_impl(make_error_code(err), msg, ap); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - va_end(ap); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS - va_end(ap); + __scope_guard guard([&] { va_end(ap); }); + report_impl(make_error_code(err), msg, ap); return error_value(); } diff --git a/libcxx/src/filesystem/format_string.h b/libcxx/src/filesystem/format_string.h index e91475e440480..8d17b027a6e31 100644 --- a/libcxx/src/filesystem/format_string.h +++ b/libcxx/src/filesystem/format_string.h @@ -11,6 +11,7 @@ #include <__assert> #include <__config> +#include <__utility/scope_guard.h> #include #include #include @@ -55,17 +56,8 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 2) string format_string(const cha string ret; va_list ap; va_start(ap, msg); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - ret = detail::vformat_string(msg, ap); -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - va_end(ap); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS - va_end(ap); + __scope_guard guard([&] { va_end(ap); }); + ret = detail::vformat_string(msg, ap); return ret; } diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp index da735865c322c..0f695d4f1a229 100644 --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -215,63 +215,58 @@ locale::__imp::__imp(size_t refs) : facet(refs), facets_(N), name_("C") { } locale::__imp::__imp(const string& name, size_t refs) : facet(refs), facets_(N), name_(name) { -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - facets_ = locale::classic().__locale_->facets_; + __exception_guard guard([&] { for (unsigned i = 0; i < facets_.size(); ++i) if (facets_[i]) - facets_[i]->__add_shared(); - install(new collate_byname(name_)); + facets_[i]->__release_shared(); + }); + facets_ = locale::classic().__locale_->facets_; + for (unsigned i = 0; i < facets_.size(); ++i) + if (facets_[i]) + facets_[i]->__add_shared(); + install(new collate_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new collate_byname(name_)); + install(new collate_byname(name_)); #endif - install(new ctype_byname(name_)); + install(new ctype_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new ctype_byname(name_)); + install(new ctype_byname(name_)); #endif - install(new codecvt_byname(name_)); + install(new codecvt_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new codecvt_byname(name_)); + install(new codecvt_byname(name_)); #endif - _LIBCPP_SUPPRESS_DEPRECATED_PUSH - install(new codecvt_byname(name_)); - install(new codecvt_byname(name_)); - _LIBCPP_SUPPRESS_DEPRECATED_POP + _LIBCPP_SUPPRESS_DEPRECATED_PUSH + install(new codecvt_byname(name_)); + install(new codecvt_byname(name_)); + _LIBCPP_SUPPRESS_DEPRECATED_POP #if _LIBCPP_HAS_CHAR8_T - install(new codecvt_byname(name_)); - install(new codecvt_byname(name_)); + install(new codecvt_byname(name_)); + install(new codecvt_byname(name_)); #endif - install(new numpunct_byname(name_)); + install(new numpunct_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new numpunct_byname(name_)); + install(new numpunct_byname(name_)); #endif - install(new moneypunct_byname(name_)); - install(new moneypunct_byname(name_)); + install(new moneypunct_byname(name_)); + install(new moneypunct_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new moneypunct_byname(name_)); - install(new moneypunct_byname(name_)); + install(new moneypunct_byname(name_)); + install(new moneypunct_byname(name_)); #endif - install(new time_get_byname(name_)); + install(new time_get_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new time_get_byname(name_)); + install(new time_get_byname(name_)); #endif - install(new time_put_byname(name_)); + install(new time_put_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new time_put_byname(name_)); + install(new time_put_byname(name_)); #endif - install(new messages_byname(name_)); + install(new messages_byname(name_)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new messages_byname(name_)); + install(new messages_byname(name_)); #endif -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - for (unsigned i = 0; i < facets_.size(); ++i) - if (facets_[i]) - facets_[i]->__release_shared(); - throw; - } -#endif // _LIBCPP_HAS_EXCEPTIONS + guard.__complete(); } locale::__imp::__imp(const __imp& other) : facets_(max(N, other.facets_.size())), name_(other.name_) { @@ -287,71 +282,66 @@ locale::__imp::__imp(const __imp& other, const string& name, locale::category c) for (unsigned i = 0; i < facets_.size(); ++i) if (facets_[i]) facets_[i]->__add_shared(); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - if (c & locale::collate) { - install(new collate_byname(name)); + __exception_guard guard([&] { + for (unsigned i = 0; i < facets_.size(); ++i) + if (facets_[i]) + facets_[i]->__release_shared(); + }); + if (c & locale::collate) { + install(new collate_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new collate_byname(name)); + install(new collate_byname(name)); #endif - } - if (c & locale::ctype) { - install(new ctype_byname(name)); + } + if (c & locale::ctype) { + install(new ctype_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new ctype_byname(name)); + install(new ctype_byname(name)); #endif - install(new codecvt_byname(name)); + install(new codecvt_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new codecvt_byname(name)); + install(new codecvt_byname(name)); #endif - _LIBCPP_SUPPRESS_DEPRECATED_PUSH - install(new codecvt_byname(name)); - install(new codecvt_byname(name)); - _LIBCPP_SUPPRESS_DEPRECATED_POP + _LIBCPP_SUPPRESS_DEPRECATED_PUSH + install(new codecvt_byname(name)); + install(new codecvt_byname(name)); + _LIBCPP_SUPPRESS_DEPRECATED_POP #if _LIBCPP_HAS_CHAR8_T - install(new codecvt_byname(name)); - install(new codecvt_byname(name)); + install(new codecvt_byname(name)); + install(new codecvt_byname(name)); #endif - } - if (c & locale::monetary) { - install(new moneypunct_byname(name)); - install(new moneypunct_byname(name)); + } + if (c & locale::monetary) { + install(new moneypunct_byname(name)); + install(new moneypunct_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new moneypunct_byname(name)); - install(new moneypunct_byname(name)); + install(new moneypunct_byname(name)); + install(new moneypunct_byname(name)); #endif - } - if (c & locale::numeric) { - install(new numpunct_byname(name)); + } + if (c & locale::numeric) { + install(new numpunct_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new numpunct_byname(name)); + install(new numpunct_byname(name)); #endif - } - if (c & locale::time) { - install(new time_get_byname(name)); + } + if (c & locale::time) { + install(new time_get_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new time_get_byname(name)); + install(new time_get_byname(name)); #endif - install(new time_put_byname(name)); + install(new time_put_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new time_put_byname(name)); + install(new time_put_byname(name)); #endif - } - if (c & locale::messages) { - install(new messages_byname(name)); + } + if (c & locale::messages) { + install(new messages_byname(name)); #if _LIBCPP_HAS_WIDE_CHARACTERS - install(new messages_byname(name)); + install(new messages_byname(name)); #endif - } -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - for (unsigned i = 0; i < facets_.size(); ++i) - if (facets_[i]) - facets_[i]->__release_shared(); - throw; } -#endif // _LIBCPP_HAS_EXCEPTIONS + guard.__complete(); } template @@ -366,87 +356,83 @@ locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c) for (unsigned i = 0; i < facets_.size(); ++i) if (facets_[i]) facets_[i]->__add_shared(); -#if _LIBCPP_HAS_EXCEPTIONS - try { -#endif // _LIBCPP_HAS_EXCEPTIONS - if (c & locale::collate) { - install_from >(one); + __exception_guard guard([&] { + for (unsigned i = 0; i < facets_.size(); ++i) + if (facets_[i]) + facets_[i]->__release_shared(); + }); + + if (c & locale::collate) { + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } - if (c & locale::ctype) { - install_from >(one); + } + if (c & locale::ctype) { + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - install_from >(one); - _LIBCPP_SUPPRESS_DEPRECATED_PUSH - install_from >(one); - install_from >(one); - _LIBCPP_SUPPRESS_DEPRECATED_POP + install_from >(one); + _LIBCPP_SUPPRESS_DEPRECATED_PUSH + install_from >(one); + install_from >(one); + _LIBCPP_SUPPRESS_DEPRECATED_POP #if _LIBCPP_HAS_CHAR8_T - install_from >(one); - install_from >(one); + install_from >(one); + install_from >(one); #endif #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } - if (c & locale::monetary) { - install_from >(one); - install_from >(one); + } + if (c & locale::monetary) { + install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); - install_from >(one); + install_from >(one); + install_from >(one); #endif - install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } - if (c & locale::numeric) { - install_from >(one); + } + if (c & locale::numeric) { + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } - if (c & locale::time) { - install_from >(one); + } + if (c & locale::time) { + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - install_from >(one); + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } - if (c & locale::messages) { - install_from >(one); + } + if (c & locale::messages) { + install_from >(one); #if _LIBCPP_HAS_WIDE_CHARACTERS - install_from >(one); + install_from >(one); #endif - } -#if _LIBCPP_HAS_EXCEPTIONS - } catch (...) { - for (unsigned i = 0; i < facets_.size(); ++i) - if (facets_[i]) - facets_[i]->__release_shared(); - throw; } -#endif // _LIBCPP_HAS_EXCEPTIONS + guard.__complete(); } locale::__imp::__imp(const __imp& other, facet* f, long id) diff --git a/lldb/test/API/commands/expression/import-std-module/deque-basic/TestDequeFromStdModule.py b/lldb/test/API/commands/expression/import-std-module/deque-basic/TestDequeFromStdModule.py index 0fb6e883597d3..5e819b7f6e16f 100644 --- a/lldb/test/API/commands/expression/import-std-module/deque-basic/TestDequeFromStdModule.py +++ b/lldb/test/API/commands/expression/import-std-module/deque-basic/TestDequeFromStdModule.py @@ -11,6 +11,9 @@ class TestBasicDeque(TestBase): @add_test_categories(["libc++"]) @skipIf(compiler=no_match("clang")) @skipIf(macos_version=["<", "15.0"]) + @skipIf( + bugnumber="ASTImport of lambdas not supported: https://github.com/llvm/llvm-project/issues/149477" + ) def test(self): self.build() diff --git a/lldb/test/API/commands/expression/import-std-module/deque-dbg-info-content/TestDbgInfoContentDequeFromStdModule.py b/lldb/test/API/commands/expression/import-std-module/deque-dbg-info-content/TestDbgInfoContentDequeFromStdModule.py index e631a87376372..e8676b21bced3 100644 --- a/lldb/test/API/commands/expression/import-std-module/deque-dbg-info-content/TestDbgInfoContentDequeFromStdModule.py +++ b/lldb/test/API/commands/expression/import-std-module/deque-dbg-info-content/TestDbgInfoContentDequeFromStdModule.py @@ -12,6 +12,9 @@ class TestDbgInfoContentDeque(TestBase): @skipIf(compiler=no_match("clang")) @skipIf(compiler="clang", compiler_version=["<", "18.0"]) @skipIf(macos_version=["<", "15.0"]) + @skipIf( + bugnumber="ASTImport of lambdas not supported: https://github.com/llvm/llvm-project/issues/149477" + ) def test(self): self.build() diff --git a/lldb/test/API/commands/expression/import-std-module/forward_list/TestForwardListFromStdModule.py b/lldb/test/API/commands/expression/import-std-module/forward_list/TestForwardListFromStdModule.py index a6ba0810e68e3..9581155be2043 100644 --- a/lldb/test/API/commands/expression/import-std-module/forward_list/TestForwardListFromStdModule.py +++ b/lldb/test/API/commands/expression/import-std-module/forward_list/TestForwardListFromStdModule.py @@ -11,6 +11,9 @@ class TestBasicForwardList(TestBase): @add_test_categories(["libc++"]) @skipIf(compiler=no_match("clang")) @skipIf(macos_version=["<", "15.0"]) + @skipIf( + bugnumber="ASTImport of lambdas not supported: https://github.com/llvm/llvm-project/issues/149477" + ) def test(self): self.build() diff --git a/lldb/test/API/commands/expression/import-std-module/list-dbg-info-content/TestDbgInfoContentListFromStdModule.py b/lldb/test/API/commands/expression/import-std-module/list-dbg-info-content/TestDbgInfoContentListFromStdModule.py index b26bd7dedda86..923551ca64c3d 100644 --- a/lldb/test/API/commands/expression/import-std-module/list-dbg-info-content/TestDbgInfoContentListFromStdModule.py +++ b/lldb/test/API/commands/expression/import-std-module/list-dbg-info-content/TestDbgInfoContentListFromStdModule.py @@ -13,6 +13,9 @@ class TestDbgInfoContentList(TestBase): @skipIf(compiler=no_match("clang")) @skipIf(compiler="clang", compiler_version=["<", "12.0"]) @skipIf(macos_version=["<", "15.0"]) + @skipIf( + bugnumber="ASTImport of lambdas not supported: https://github.com/llvm/llvm-project/issues/149477" + ) def test(self): self.build() diff --git a/lldb/test/API/commands/expression/import-std-module/list/TestListFromStdModule.py b/lldb/test/API/commands/expression/import-std-module/list/TestListFromStdModule.py index 6253a35e926d7..d6c88926e3e49 100644 --- a/lldb/test/API/commands/expression/import-std-module/list/TestListFromStdModule.py +++ b/lldb/test/API/commands/expression/import-std-module/list/TestListFromStdModule.py @@ -11,6 +11,9 @@ class TestBasicList(TestBase): @add_test_categories(["libc++"]) @skipIf(compiler=no_match("clang")) @skipIf(macos_version=["<", "15.0"]) + @skipIf( + bugnumber="ASTImport of lambdas not supported: https://github.com/llvm/llvm-project/issues/149477" + ) def test(self): self.build()