Skip to content

Commit 040e9e0

Browse files
authored
[libc++] Inline __has_feature and __has_extension uses (#133634)
Since GCC now supports `__has_feature` and `__has_extension` as well, there isn't much of a reason to define new macros to test for the features.
1 parent b9d4132 commit 040e9e0

File tree

9 files changed

+32
-57
lines changed

9 files changed

+32
-57
lines changed

libcxx/include/__config

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -319,37 +319,12 @@ typedef __char32_t char32_t;
319319

320320
# define _LIBCPP_PREFERRED_ALIGNOF(_Tp) __alignof(_Tp)
321321

322-
// Objective-C++ features (opt-in)
323-
# if __has_feature(objc_arc)
324-
# define _LIBCPP_HAS_OBJC_ARC 1
325-
# else
326-
# define _LIBCPP_HAS_OBJC_ARC 0
327-
# endif
328-
329-
# if __has_feature(objc_arc_weak)
330-
# define _LIBCPP_HAS_OBJC_ARC_WEAK 1
331-
# else
332-
# define _LIBCPP_HAS_OBJC_ARC_WEAK 0
333-
# endif
334-
335-
# if __has_extension(blocks)
336-
# define _LIBCPP_HAS_EXTENSION_BLOCKS 1
337-
# else
338-
# define _LIBCPP_HAS_EXTENSION_BLOCKS 0
339-
# endif
340-
341-
# if _LIBCPP_HAS_EXTENSION_BLOCKS && defined(__APPLE__)
322+
# if __has_extension(blocks) && defined(__APPLE__)
342323
# define _LIBCPP_HAS_BLOCKS_RUNTIME 1
343324
# else
344325
# define _LIBCPP_HAS_BLOCKS_RUNTIME 0
345326
# endif
346327

347-
# if __has_feature(address_sanitizer)
348-
# define _LIBCPP_HAS_ASAN 1
349-
# else
350-
# define _LIBCPP_HAS_ASAN 0
351-
# endif
352-
353328
# define _LIBCPP_ALWAYS_INLINE __attribute__((__always_inline__))
354329

355330
# if defined(_LIBCPP_OBJECT_FORMAT_COFF)

libcxx/include/__debug_utils/sanitizers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
#if _LIBCPP_HAS_ASAN
20+
#if __has_feature(address_sanitizer)
2121

2222
extern "C" {
2323
_LIBCPP_EXPORTED_FROM_ABI void
@@ -28,12 +28,12 @@ _LIBCPP_EXPORTED_FROM_ABI int
2828
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
2929
}
3030

31-
#endif // _LIBCPP_HAS_ASAN
31+
#endif // __has_feature(address_sanitizer)
3232

3333
_LIBCPP_BEGIN_NAMESPACE_STD
3434

3535
// ASan choices
36-
#if _LIBCPP_HAS_ASAN
36+
#if __has_feature(address_sanitizer)
3737
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
3838
#endif
3939

@@ -57,7 +57,7 @@ _LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
5757
const void* __last_old_contained,
5858
const void* __first_new_contained,
5959
const void* __last_new_contained) {
60-
#if !_LIBCPP_HAS_ASAN
60+
#if !__has_feature(address_sanitizer)
6161
(void)__first_storage;
6262
(void)__last_storage;
6363
(void)__first_old_contained;
@@ -86,7 +86,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __annotate_contiguous_c
8686
const void* __last_storage,
8787
const void* __old_last_contained,
8888
const void* __new_last_contained) {
89-
#if !_LIBCPP_HAS_ASAN
89+
#if !__has_feature(address_sanitizer)
9090
(void)__first_storage;
9191
(void)__last_storage;
9292
(void)__old_last_contained;

libcxx/include/__functional/function.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ _LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {
122122
return !!__f;
123123
}
124124

125-
# if _LIBCPP_HAS_EXTENSION_BLOCKS
125+
# if __has_extension(blocks)
126126
template <class _Rp, class... _Args>
127127
_LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {
128128
return __p;
@@ -757,7 +757,7 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
757757

758758
public:
759759
_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
760-
# if _LIBCPP_HAS_OBJC_ARC
760+
# if __has_feature(objc_arc)
761761
: __f_(__f)
762762
# else
763763
: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
@@ -768,7 +768,7 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
768768
// [TODO] add && to save on a retain
769769

770770
_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& /* unused */)
771-
# if _LIBCPP_HAS_OBJC_ARC
771+
# if __has_feature(objc_arc)
772772
: __f_(__f)
773773
# else
774774
: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
@@ -790,7 +790,7 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
790790
}
791791

792792
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
793-
# if !_LIBCPP_HAS_OBJC_ARC
793+
# if !__has_feature(objc_arc)
794794
if (__f_)
795795
_Block_release(__f_);
796796
# endif
@@ -822,7 +822,7 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
822822
# endif // _LIBCPP_HAS_RTTI
823823
};
824824

825-
# endif // _LIBCPP_HAS_EXTENSION_BLOCKS
825+
# endif // _LIBCPP_HAS_BLOCKS_RUNTIME
826826

827827
} // namespace __function
828828

libcxx/include/__memory/addressof.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* a
2323
return __builtin_addressof(__x);
2424
}
2525

26-
#if _LIBCPP_HAS_OBJC_ARC
26+
#if __has_feature(objc_arc)
2727
// Objective-C++ Automatic Reference Counting uses qualified pointers
2828
// that require special addressof() signatures.
2929
template <class _Tp>
3030
inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
3131
return &__x;
3232
}
3333

34-
# if _LIBCPP_HAS_OBJC_ARC_WEAK
34+
# if __has_feature(objc_arc_weak)
3535
template <class _Tp>
3636
inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
3737
return &__x;

libcxx/include/__type_traits/is_pointer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ template <class _Tp>
4040
struct __libcpp_remove_objc_qualifiers {
4141
typedef _Tp type;
4242
};
43-
# if _LIBCPP_HAS_OBJC_ARC
43+
# if __has_feature(objc_arc)
4444
// clang-format off
4545
template <class _Tp> struct __libcpp_remove_objc_qualifiers<_Tp __strong> { typedef _Tp type; };
4646
template <class _Tp> struct __libcpp_remove_objc_qualifiers<_Tp __weak> { typedef _Tp type; };

libcxx/include/__type_traits/is_scalar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_scalar_v = __is_scalar(_Tp);
3737

3838
template <class _Tp>
3939
struct __is_block : false_type {};
40-
# if _LIBCPP_HAS_EXTENSION_BLOCKS
40+
# if __has_extension(blocks)
4141
template <class _Rp, class... _Args>
4242
struct __is_block<_Rp (^)(_Args...)> : true_type {};
4343
# endif

libcxx/include/deque

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ private:
930930
(void)__end;
931931
(void)__annotation_type;
932932
(void)__place;
933-
# if _LIBCPP_HAS_ASAN
933+
# if __has_feature(address_sanitizer)
934934
// __beg - index of the first item to annotate
935935
// __end - index behind the last item to annotate (so last item + 1)
936936
// __annotation_type - __asan_unposion or __asan_poison
@@ -1023,23 +1023,23 @@ private:
10231023
std::__annotate_double_ended_contiguous_container<_Allocator>(
10241024
__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
10251025
}
1026-
# endif // _LIBCPP_HAS_ASAN
1026+
# endif // __has_feature(address_sanitizer)
10271027
}
10281028

10291029
_LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
10301030
(void)__current_size;
1031-
# if _LIBCPP_HAS_ASAN
1031+
# if __has_feature(address_sanitizer)
10321032
if (__current_size == 0)
10331033
__annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
10341034
else {
10351035
__annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
10361036
__annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
10371037
}
1038-
# endif // _LIBCPP_HAS_ASAN
1038+
# endif // __has_feature(address_sanitizer)
10391039
}
10401040

10411041
_LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1042-
# if _LIBCPP_HAS_ASAN
1042+
# if __has_feature(address_sanitizer)
10431043
if (empty()) {
10441044
for (size_t __i = 0; __i < __map_.size(); ++__i) {
10451045
__annotate_whole_block(__i, __asan_unposion);
@@ -1048,35 +1048,35 @@ private:
10481048
__annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
10491049
__annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
10501050
}
1051-
# endif // _LIBCPP_HAS_ASAN
1051+
# endif // __has_feature(address_sanitizer)
10521052
}
10531053

10541054
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
10551055
(void)__n;
1056-
# if _LIBCPP_HAS_ASAN
1056+
# if __has_feature(address_sanitizer)
10571057
__annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
10581058
# endif
10591059
}
10601060

10611061
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
10621062
(void)__n;
1063-
# if _LIBCPP_HAS_ASAN
1063+
# if __has_feature(address_sanitizer)
10641064
__annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
10651065
# endif
10661066
}
10671067

10681068
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
10691069
(void)__old_size;
10701070
(void)__old_start;
1071-
# if _LIBCPP_HAS_ASAN
1071+
# if __has_feature(address_sanitizer)
10721072
__annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
10731073
# endif
10741074
}
10751075

10761076
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
10771077
(void)__old_size;
10781078
(void)__old_start;
1079-
# if _LIBCPP_HAS_ASAN
1079+
# if __has_feature(address_sanitizer)
10801080
__annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
10811081
# endif
10821082
}
@@ -1089,7 +1089,7 @@ private:
10891089
__annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
10901090
(void)__block_index;
10911091
(void)__annotation_type;
1092-
# if _LIBCPP_HAS_ASAN
1092+
# if __has_feature(address_sanitizer)
10931093
__map_const_iterator __block_it = __map_.begin() + __block_index;
10941094
const void* __block_start = std::__to_address(*__block_it);
10951095
const void* __block_end = std::__to_address(*__block_it + __block_size);
@@ -1102,7 +1102,7 @@ private:
11021102
}
11031103
# endif
11041104
}
1105-
# if _LIBCPP_HAS_ASAN
1105+
# if __has_feature(address_sanitizer)
11061106

11071107
public:
11081108
_LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
@@ -1164,7 +1164,7 @@ public:
11641164
}
11651165

11661166
private:
1167-
# endif // _LIBCPP_HAS_ASAN
1167+
# endif // __has_feature(address_sanitizer)
11681168
_LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
11691169
if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
11701170
__annotate_whole_block(0, __asan_unposion);

libcxx/include/string

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
678678
_LIBCPP_PUSH_MACROS
679679
# include <__undef_macros>
680680

681-
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
681+
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
682682
# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))
683683
// This macro disables AddressSanitizer (ASan) instrumentation for a specific function,
684684
// allowing memory accesses that would normally trigger ASan errors to proceed without crashing.
@@ -749,7 +749,7 @@ public:
749749
//
750750
// This string implementation doesn't contain any references into itself. It only contains a bit that says whether
751751
// it is in small or large string mode, so the entire structure is trivially relocatable if its members are.
752-
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
752+
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
753753
// When compiling with AddressSanitizer (ASan), basic_string cannot be trivially
754754
// relocatable. Because the object's memory might be poisoned when its content
755755
// is kept inside objects memory (short string optimization), instead of in allocated
@@ -771,7 +771,7 @@ public:
771771
basic_string,
772772
void>;
773773

774-
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
774+
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
775775
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __asan_volatile_wrapper(pointer const& __ptr) const {
776776
if (__libcpp_is_constant_evaluated())
777777
return __ptr;

libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static_assert(!std::__libcpp_is_trivially_relocatable<std::array<NotTriviallyCop
8787
static_assert(std::__libcpp_is_trivially_relocatable<std::array<std::unique_ptr<int>, 1> >::value, "");
8888

8989
// basic_string
90-
#if !_LIBCPP_HAS_ASAN || !_LIBCPP_INSTRUMENTED_WITH_ASAN
90+
#if !__has_feature(address_sanitizer) || !_LIBCPP_INSTRUMENTED_WITH_ASAN
9191
struct MyChar {
9292
char c;
9393
};

0 commit comments

Comments
 (0)