[libcxx-commits] [libcxx] [libc++] Use std::__{scope, exception}_guard throughout the code base (PR #161322)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 29 23:29:52 PDT 2025
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/161322
This simplifies the code quite a bit and seems to improve code size slightly in some cases.
>From e2a68d149641032e6e0909f934dc3bf81c7f4bce Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 30 Sep 2025 08:29:05 +0200
Subject: [PATCH] [libc++] Use std::__{scope,exception}_guard throughout the
code base
---
libcxx/include/__hash_table | 66 ++----
libcxx/include/__memory/shared_ptr.h | 93 +++-----
.../__memory/uninitialized_algorithms.h | 154 ++++---------
libcxx/include/__utility/scope_guard.h | 2 +
libcxx/include/__vector/vector_bool.h | 23 +-
libcxx/include/deque | 39 ++--
libcxx/include/forward_list | 31 +--
libcxx/include/future | 14 +-
libcxx/include/list | 60 ++---
libcxx/include/string | 31 +--
libcxx/include/valarray | 152 ++++---------
libcxx/src/filesystem/error.h | 26 +--
libcxx/src/filesystem/format_string.h | 14 +-
libcxx/src/locale.cpp | 211 ++++++++----------
14 files changed, 313 insertions(+), 603 deletions(-)
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 <limits>
@@ -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 <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::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 <class _Yp,
@@ -376,28 +370,21 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
class _Alloc,
__enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::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 <class _Dp>
@@ -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<nullptr_t, _Dp, _AllocT> _CntrlBlk;
+ auto __guard = std::__make_exception_guard([&] { __d(__p); });
+ typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
+ typedef __shared_ptr_pointer<nullptr_t, _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
-#if _LIBCPP_HAS_EXCEPTIONS
- } catch (...) {
- __d(__p);
- throw;
- }
-#endif // _LIBCPP_HAS_EXCEPTIONS
+ __guard.__complete();
}
template <class _Dp, class _Alloc>
@@ -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<nullptr_t, _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<nullptr_t, _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());
-#if _LIBCPP_HAS_EXCEPTIONS
- } catch (...) {
- __d(__p);
- throw;
- }
-#endif // _LIBCPP_HAS_EXCEPTIONS
+ __cntrl_ = std::addressof(*__hold2.release());
+ __guard.__complete();
}
template <class _Yp>
diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h
index e80236640052c..b48d01bfe1a5e 100644
--- a/libcxx/include/__memory/uninitialized_algorithms.h
+++ b/libcxx/include/__memory/uninitialized_algorithms.h
@@ -61,17 +61,10 @@ template <class _ValueType, class _InputIterator, class _Sentinel1, class _Forwa
inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __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<void*>(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<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
+ __guard.__complete();
return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx));
}
@@ -91,17 +84,10 @@ template <class _ValueType, class _InputIterator, class _Size, class _ForwardIte
inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator>
__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<void*>(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<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
+ __guard.__complete();
return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx));
}
@@ -121,17 +107,10 @@ template <class _ValueType, class _ForwardIterator, class _Sentinel, class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
__uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) {
_ForwardIterator __idx = __first;
-#if _LIBCPP_HAS_EXCEPTIONS
- try {
-#endif
+ auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __idx != __last; ++__idx)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x);
-#if _LIBCPP_HAS_EXCEPTIONS
- } catch (...) {
- std::__destroy(__first, __idx);
- throw;
- }
-#endif
+ __guard.__complete();
return __idx;
}
@@ -149,17 +128,10 @@ template <class _ValueType, class _ForwardIterator, class _Size, class _Tp>
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<void*>(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<void*>(std::addressof(*__idx))) _ValueType(__x);
+ __guard.__complete();
return __idx;
}
@@ -178,18 +150,11 @@ uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
template <class _ValueType, class _ForwardIterator, class _Sentinel>
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<void*>(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<void*>(std::addressof(*__idx))) _ValueType;
+ __guard.__complete();
return __idx;
}
@@ -205,17 +170,10 @@ inline _LIBCPP_HIDE_FROM_ABI void uninitialized_default_construct(_ForwardIterat
template <class _ValueType, class _ForwardIterator, class _Size>
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<void*>(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<void*>(std::addressof(*__idx))) _ValueType;
+ __guard.__complete();
return __idx;
}
@@ -231,18 +189,11 @@ inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator uninitialized_default_construct_n(
template <class _ValueType, class _ForwardIterator, class _Sentinel>
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<void*>(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<void*>(std::addressof(*__idx))) _ValueType();
+ __guard.__complete();
return __idx;
}
@@ -258,17 +209,10 @@ inline _LIBCPP_HIDE_FROM_ABI void uninitialized_value_construct(_ForwardIterator
template <class _ValueType, class _ForwardIterator, class _Size>
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<void*>(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<void*>(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<void*>(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<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst));
}
-# endif
+ __guard.__complete();
return {std::move(__ifirst), std::move(__idx)};
}
@@ -331,18 +268,11 @@ template <class _ValueType,
class _IterMove>
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<void*>(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<void*>(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 <class _Func>
_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<bool, _Allocator>::__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<difference_type>(__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<difference_type>(__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 <class T, class Allocator, class Predicate>
# 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<pointer, __pointer_allocator&> __buf(
std::max<size_type>(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<size_type>(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 <class T, class Allocator, class Predicate>
# 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 <class R, class Alloc> struct uses_allocator<packaged_task<R>, 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 <class _Rp, class _Fp>
_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..8f404fc679b55 100644
--- a/libcxx/include/list
+++ b/libcxx/include/list
@@ -237,6 +237,7 @@ template <class T, class Allocator, class Predicate>
# 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,10 @@ _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
__link_nodes_at_back(__r.__ptr_, __e.__ptr_);
this->__size_ += __ds;
}
@@ -1488,14 +1473,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 +1482,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 729a420d47598..234723c7df718 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -638,6 +638,7 @@ basic_string<char32_t> 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 <class _CharT, class _Traits, class _Allocator>
@@ -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 <class _CharT, class _Traits, class _Allocator>
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 <class T> unspecified2 end(const valarray<T>& 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 <cmath>
@@ -1992,17 +1993,10 @@ template <class _Tp>
inline valarray<_Tp>::valarray(size_t __n) : __begin_(nullptr), __end_(nullptr) {
if (__n) {
__begin_ = __end_ = allocator<value_type>().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 <class _Tp>
valarray<_Tp>::valarray(const value_type* __p, size_t __n) : __begin_(nullptr), __end_(nullptr) {
if (__n) {
__begin_ = __end_ = allocator<value_type>().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 <class _Tp>
valarray<_Tp>::valarray(const valarray& __v) : __begin_(nullptr), __end_(nullptr) {
if (__v.size()) {
__begin_ = __end_ = allocator<value_type>().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<value_type> __il) : __begin_(nullptr),
const size_t __n = __il.size();
if (__n) {
__begin_ = __end_ = allocator<value_type>().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<value_type>& __sa) : __begin_(nullptr)
const size_t __n = __sa.__size_;
if (__n) {
__begin_ = __end_ = allocator<value_type>().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<value_type>& __ga) : __begin_(nullptr
const size_t __n = __ga.__1d_.size();
if (__n) {
__begin_ = __end_ = allocator<value_type>().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<value_type>& __ma) : __begin_(nullptr),
const size_t __n = __ma.__1d_.size();
if (__n) {
__begin_ = __end_ = allocator<value_type>().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<value_type>& __ia) : __begin_(nullp
const size_t __n = __ia.__1d_.size();
if (__n) {
__begin_ = __end_ = allocator<value_type>().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<value_type>().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<T>();
}
@@ -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<T>();
}
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 <array>
#include <cstdarg>
#include <cstddef>
@@ -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..a56494d1cd085 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -215,14 +215,16 @@ 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<char>(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<char>(name_));
#if _LIBCPP_HAS_WIDE_CHARACTERS
install(new collate_byname<wchar_t>(name_));
#endif
@@ -264,14 +266,6 @@ locale::__imp::__imp(const string& name, size_t refs) : facet(refs), facets_(N),
#if _LIBCPP_HAS_WIDE_CHARACTERS
install(new messages_byname<wchar_t>(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
}
locale::__imp::__imp(const __imp& other) : facets_(max<size_t>(N, other.facets_.size())), name_(other.name_) {
@@ -287,71 +281,65 @@ 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<char>(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<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new collate_byname<wchar_t>(name));
+ install(new collate_byname<wchar_t>(name));
#endif
- }
- if (c & locale::ctype) {
- install(new ctype_byname<char>(name));
+ }
+ if (c & locale::ctype) {
+ install(new ctype_byname<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new ctype_byname<wchar_t>(name));
+ install(new ctype_byname<wchar_t>(name));
#endif
- install(new codecvt_byname<char, char, mbstate_t>(name));
+ install(new codecvt_byname<char, char, mbstate_t>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
+ install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
#endif
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- install(new codecvt_byname<char16_t, char, mbstate_t>(name));
- install(new codecvt_byname<char32_t, char, mbstate_t>(name));
- _LIBCPP_SUPPRESS_DEPRECATED_POP
+ _LIBCPP_SUPPRESS_DEPRECATED_PUSH
+ install(new codecvt_byname<char16_t, char, mbstate_t>(name));
+ install(new codecvt_byname<char32_t, char, mbstate_t>(name));
+ _LIBCPP_SUPPRESS_DEPRECATED_POP
#if _LIBCPP_HAS_CHAR8_T
- install(new codecvt_byname<char16_t, char8_t, mbstate_t>(name));
- install(new codecvt_byname<char32_t, char8_t, mbstate_t>(name));
+ install(new codecvt_byname<char16_t, char8_t, mbstate_t>(name));
+ install(new codecvt_byname<char32_t, char8_t, mbstate_t>(name));
#endif
- }
- if (c & locale::monetary) {
- install(new moneypunct_byname<char, false>(name));
- install(new moneypunct_byname<char, true>(name));
+ }
+ if (c & locale::monetary) {
+ install(new moneypunct_byname<char, false>(name));
+ install(new moneypunct_byname<char, true>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new moneypunct_byname<wchar_t, false>(name));
- install(new moneypunct_byname<wchar_t, true>(name));
+ install(new moneypunct_byname<wchar_t, false>(name));
+ install(new moneypunct_byname<wchar_t, true>(name));
#endif
- }
- if (c & locale::numeric) {
- install(new numpunct_byname<char>(name));
+ }
+ if (c & locale::numeric) {
+ install(new numpunct_byname<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new numpunct_byname<wchar_t>(name));
+ install(new numpunct_byname<wchar_t>(name));
#endif
- }
- if (c & locale::time) {
- install(new time_get_byname<char>(name));
+ }
+ if (c & locale::time) {
+ install(new time_get_byname<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new time_get_byname<wchar_t>(name));
+ install(new time_get_byname<wchar_t>(name));
#endif
- install(new time_put_byname<char>(name));
+ install(new time_put_byname<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new time_put_byname<wchar_t>(name));
+ install(new time_put_byname<wchar_t>(name));
#endif
- }
- if (c & locale::messages) {
- install(new messages_byname<char>(name));
+ }
+ if (c & locale::messages) {
+ install(new messages_byname<char>(name));
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install(new messages_byname<wchar_t>(name));
+ install(new messages_byname<wchar_t>(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
}
template <class F>
@@ -366,87 +354,82 @@ 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<std::collate<char> >(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<std::collate<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<std::collate<wchar_t> >(one);
+ install_from<std::collate<wchar_t> >(one);
#endif
- }
- if (c & locale::ctype) {
- install_from<std::ctype<char> >(one);
+ }
+ if (c & locale::ctype) {
+ install_from<std::ctype<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<std::ctype<wchar_t> >(one);
+ install_from<std::ctype<wchar_t> >(one);
#endif
- install_from<std::codecvt<char, char, mbstate_t> >(one);
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- install_from<std::codecvt<char16_t, char, mbstate_t> >(one);
- install_from<std::codecvt<char32_t, char, mbstate_t> >(one);
- _LIBCPP_SUPPRESS_DEPRECATED_POP
+ install_from<std::codecvt<char, char, mbstate_t> >(one);
+ _LIBCPP_SUPPRESS_DEPRECATED_PUSH
+ install_from<std::codecvt<char16_t, char, mbstate_t> >(one);
+ install_from<std::codecvt<char32_t, char, mbstate_t> >(one);
+ _LIBCPP_SUPPRESS_DEPRECATED_POP
#if _LIBCPP_HAS_CHAR8_T
- install_from<std::codecvt<char16_t, char8_t, mbstate_t> >(one);
- install_from<std::codecvt<char32_t, char8_t, mbstate_t> >(one);
+ install_from<std::codecvt<char16_t, char8_t, mbstate_t> >(one);
+ install_from<std::codecvt<char32_t, char8_t, mbstate_t> >(one);
#endif
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<std::codecvt<wchar_t, char, mbstate_t> >(one);
+ install_from<std::codecvt<wchar_t, char, mbstate_t> >(one);
#endif
- }
- if (c & locale::monetary) {
- install_from<moneypunct<char, false> >(one);
- install_from<moneypunct<char, true> >(one);
+ }
+ if (c & locale::monetary) {
+ install_from<moneypunct<char, false> >(one);
+ install_from<moneypunct<char, true> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<moneypunct<wchar_t, false> >(one);
- install_from<moneypunct<wchar_t, true> >(one);
+ install_from<moneypunct<wchar_t, false> >(one);
+ install_from<moneypunct<wchar_t, true> >(one);
#endif
- install_from<money_get<char> >(one);
+ install_from<money_get<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<money_get<wchar_t> >(one);
+ install_from<money_get<wchar_t> >(one);
#endif
- install_from<money_put<char> >(one);
+ install_from<money_put<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<money_put<wchar_t> >(one);
+ install_from<money_put<wchar_t> >(one);
#endif
- }
- if (c & locale::numeric) {
- install_from<numpunct<char> >(one);
+ }
+ if (c & locale::numeric) {
+ install_from<numpunct<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<numpunct<wchar_t> >(one);
+ install_from<numpunct<wchar_t> >(one);
#endif
- install_from<num_get<char> >(one);
+ install_from<num_get<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<num_get<wchar_t> >(one);
+ install_from<num_get<wchar_t> >(one);
#endif
- install_from<num_put<char> >(one);
+ install_from<num_put<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<num_put<wchar_t> >(one);
+ install_from<num_put<wchar_t> >(one);
#endif
- }
- if (c & locale::time) {
- install_from<time_get<char> >(one);
+ }
+ if (c & locale::time) {
+ install_from<time_get<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<time_get<wchar_t> >(one);
+ install_from<time_get<wchar_t> >(one);
#endif
- install_from<time_put<char> >(one);
+ install_from<time_put<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<time_put<wchar_t> >(one);
+ install_from<time_put<wchar_t> >(one);
#endif
- }
- if (c & locale::messages) {
- install_from<std::messages<char> >(one);
+ }
+ if (c & locale::messages) {
+ install_from<std::messages<char> >(one);
#if _LIBCPP_HAS_WIDE_CHARACTERS
- install_from<std::messages<wchar_t> >(one);
+ install_from<std::messages<wchar_t> >(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
}
locale::__imp::__imp(const __imp& other, facet* f, long id)
More information about the libcxx-commits
mailing list