Skip to content

Commit d8ada86

Browse files
committed
[libc++] Add macros for try/catch and rethrow
1 parent fdf2b0a commit d8ada86

28 files changed

+320
-785
lines changed

libcxx/.clang-format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ IndentWrappedFunctionNames: false
5959
IndentRequires: true
6060
InsertTrailingCommas: Wrapped
6161
KeepEmptyLinesAtTheStartOfBlocks: false
62+
Macros:
63+
- _LIBCPP_TRY=if (true)
64+
- _LIBCPP_CATCH(a)=else if (false)
65+
6266
MaxEmptyLinesToKeep: 1
6367
PackConstructorInitializers: NextLine
6468

libcxx/include/__config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,16 @@ typedef __char32_t char32_t;
12321232
# define _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER 0
12331233
# endif
12341234

1235+
# if _LIBCPP_HAS_EXCEPTIONS
1236+
# define _LIBCPP_TRY try
1237+
# define _LIBCPP_CATCH(...) catch(__VA_ARGS__)
1238+
# define _LIBCPP_RETHROW throw
1239+
# else
1240+
# define _LIBCPP_TRY if (true)
1241+
# define _LIBCPP_CATCH(...) if (false)
1242+
# define _LIBCPP_RETHROW ((void)0)
1243+
# endif
1244+
12351245
#endif // __cplusplus
12361246

12371247
#endif // _LIBCPP___CONFIG

libcxx/include/__hash_table

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,22 +1210,18 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u,
12101210
max_load_factor() = __u.max_load_factor();
12111211
if (bucket_count() != 0) {
12121212
__next_pointer __cache = __detach();
1213-
#if _LIBCPP_HAS_EXCEPTIONS
1214-
try {
1215-
#endif // _LIBCPP_HAS_EXCEPTIONS
1213+
_LIBCPP_TRY {
12161214
const_iterator __i = __u.begin();
12171215
while (__cache != nullptr && __u.size() != 0) {
12181216
__cache->__upcast()->__get_value() = std::move(__u.remove(__i++)->__get_value());
12191217
__next_pointer __next = __cache->__next_;
12201218
__node_insert_multi(__cache->__upcast());
12211219
__cache = __next;
12221220
}
1223-
#if _LIBCPP_HAS_EXCEPTIONS
1224-
} catch (...) {
1221+
} _LIBCPP_CATCH(...) {
12251222
__deallocate_node(__cache);
1226-
throw;
1223+
_LIBCPP_RETHROW;
12271224
}
1228-
#endif // _LIBCPP_HAS_EXCEPTIONS
12291225
__deallocate_node(__cache);
12301226
}
12311227
const_iterator __i = __u.begin();
@@ -1256,21 +1252,17 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __
12561252

12571253
if (bucket_count() != 0) {
12581254
__next_pointer __cache = __detach();
1259-
#if _LIBCPP_HAS_EXCEPTIONS
1260-
try {
1261-
#endif // _LIBCPP_HAS_EXCEPTIONS
1255+
_LIBCPP_TRY {
12621256
for (; __cache != nullptr && __first != __last; ++__first) {
12631257
__cache->__upcast()->__get_value() = *__first;
12641258
__next_pointer __next = __cache->__next_;
12651259
__node_insert_unique(__cache->__upcast());
12661260
__cache = __next;
12671261
}
1268-
#if _LIBCPP_HAS_EXCEPTIONS
1269-
} catch (...) {
1262+
} _LIBCPP_CATCH(...) {
12701263
__deallocate_node(__cache);
1271-
throw;
1264+
_LIBCPP_RETHROW;
12721265
}
1273-
#endif // _LIBCPP_HAS_EXCEPTIONS
12741266
__deallocate_node(__cache);
12751267
}
12761268
for (; __first != __last; ++__first)
@@ -1288,21 +1280,17 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __f
12881280
" or the nodes value type");
12891281
if (bucket_count() != 0) {
12901282
__next_pointer __cache = __detach();
1291-
#if _LIBCPP_HAS_EXCEPTIONS
1292-
try {
1293-
#endif // _LIBCPP_HAS_EXCEPTIONS
1283+
_LIBCPP_TRY {
12941284
for (; __cache != nullptr && __first != __last; ++__first) {
12951285
__cache->__upcast()->__get_value() = *__first;
12961286
__next_pointer __next = __cache->__next_;
12971287
__node_insert_multi(__cache->__upcast());
12981288
__cache = __next;
12991289
}
1300-
#if _LIBCPP_HAS_EXCEPTIONS
1301-
} catch (...) {
1290+
} _LIBCPP_CATCH(...) {
13021291
__deallocate_node(__cache);
1303-
throw;
1292+
_LIBCPP_RETHROW;
13041293
}
1305-
#endif // _LIBCPP_HAS_EXCEPTIONS
13061294
__deallocate_node(__cache);
13071295
}
13081296
for (; __first != __last; ++__first)

libcxx/include/__iterator/counted_iterator.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,12 @@ class counted_iterator
132132
_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
133133
_LIBCPP_ASSERT_UNCATEGORIZED(__count_ > 0, "Iterator already at or past end.");
134134
--__count_;
135-
# if _LIBCPP_HAS_EXCEPTIONS
136-
try {
135+
_LIBCPP_TRY {
137136
return __current_++;
138-
} catch (...) {
137+
} _LIBCPP_CATCH(...) {
139138
++__count_;
140-
throw;
139+
_LIBCPP_RETHROW;
141140
}
142-
# else
143-
return __current_++;
144-
# endif // _LIBCPP_HAS_EXCEPTIONS
145141
}
146142

147143
_LIBCPP_HIDE_FROM_ABI constexpr counted_iterator operator++(int)

libcxx/include/__memory/shared_ptr.h

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,7 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
457457

458458
template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
459459
_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) {
460-
#if _LIBCPP_HAS_EXCEPTIONS
461-
try {
462-
#endif // _LIBCPP_HAS_EXCEPTIONS
460+
_LIBCPP_TRY {
463461
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
464462
typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
465463
#ifndef _LIBCPP_CXX03_LANG
@@ -468,22 +466,18 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
468466
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
469467
#endif // not _LIBCPP_CXX03_LANG
470468
__enable_weak_this(__p, __p);
471-
#if _LIBCPP_HAS_EXCEPTIONS
472-
} catch (...) {
469+
} _LIBCPP_CATCH(...) {
473470
__d(__p);
474-
throw;
471+
_LIBCPP_RETHROW;
475472
}
476-
#endif // _LIBCPP_HAS_EXCEPTIONS
477473
}
478474

479475
template <class _Yp,
480476
class _Dp,
481477
class _Alloc,
482478
__enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
483479
_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
484-
#if _LIBCPP_HAS_EXCEPTIONS
485-
try {
486-
#endif // _LIBCPP_HAS_EXCEPTIONS
480+
_LIBCPP_TRY {
487481
typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
488482
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
489483
typedef __allocator_destructor<_A2> _D2;
@@ -497,12 +491,10 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
497491
#endif // not _LIBCPP_CXX03_LANG
498492
__cntrl_ = std::addressof(*__hold2.release());
499493
__enable_weak_this(__p, __p);
500-
#if _LIBCPP_HAS_EXCEPTIONS
501-
} catch (...) {
494+
} _LIBCPP_CATCH(...) {
502495
__d(__p);
503-
throw;
496+
_LIBCPP_RETHROW;
504497
}
505-
#endif // _LIBCPP_HAS_EXCEPTIONS
506498
}
507499

508500
template <class _Dp>
@@ -511,22 +503,18 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
511503
_Dp __d,
512504
__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
513505
: __ptr_(nullptr) {
514-
#if _LIBCPP_HAS_EXCEPTIONS
515-
try {
516-
#endif // _LIBCPP_HAS_EXCEPTIONS
506+
_LIBCPP_TRY {
517507
typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
518508
typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
519509
#ifndef _LIBCPP_CXX03_LANG
520510
__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
521511
#else
522512
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
523513
#endif // not _LIBCPP_CXX03_LANG
524-
#if _LIBCPP_HAS_EXCEPTIONS
525-
} catch (...) {
514+
} _LIBCPP_CATCH(...) {
526515
__d(__p);
527-
throw;
516+
_LIBCPP_RETHROW;
528517
}
529-
#endif // _LIBCPP_HAS_EXCEPTIONS
530518
}
531519

532520
template <class _Dp, class _Alloc>
@@ -536,9 +524,7 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
536524
_Alloc __a,
537525
__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
538526
: __ptr_(nullptr) {
539-
#if _LIBCPP_HAS_EXCEPTIONS
540-
try {
541-
#endif // _LIBCPP_HAS_EXCEPTIONS
527+
_LIBCPP_TRY {
542528
typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
543529
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
544530
typedef __allocator_destructor<_A2> _D2;
@@ -551,12 +537,10 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
551537
_CntrlBlk(__p, __d, __a);
552538
#endif // not _LIBCPP_CXX03_LANG
553539
__cntrl_ = std::addressof(*__hold2.release());
554-
#if _LIBCPP_HAS_EXCEPTIONS
555-
} catch (...) {
540+
} _LIBCPP_CATCH(...) {
556541
__d(__p);
557-
throw;
542+
_LIBCPP_RETHROW;
558543
}
559-
#endif // _LIBCPP_HAS_EXCEPTIONS
560544
}
561545

562546
template <class _Yp>

0 commit comments

Comments
 (0)