[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.
This commit is contained in:
Nikolas Klauser
2025-06-10 05:54:15 +02:00
committed by GitHub
parent b9d41328c6
commit 040e9e02cc
9 changed files with 32 additions and 57 deletions

View File

@@ -319,37 +319,12 @@ typedef __char32_t char32_t;
# define _LIBCPP_PREFERRED_ALIGNOF(_Tp) __alignof(_Tp)
// Objective-C++ features (opt-in)
# if __has_feature(objc_arc)
# define _LIBCPP_HAS_OBJC_ARC 1
# else
# define _LIBCPP_HAS_OBJC_ARC 0
# endif
# if __has_feature(objc_arc_weak)
# define _LIBCPP_HAS_OBJC_ARC_WEAK 1
# else
# define _LIBCPP_HAS_OBJC_ARC_WEAK 0
# endif
# if __has_extension(blocks)
# define _LIBCPP_HAS_EXTENSION_BLOCKS 1
# else
# define _LIBCPP_HAS_EXTENSION_BLOCKS 0
# endif
# if _LIBCPP_HAS_EXTENSION_BLOCKS && defined(__APPLE__)
# if __has_extension(blocks) && defined(__APPLE__)
# define _LIBCPP_HAS_BLOCKS_RUNTIME 1
# else
# define _LIBCPP_HAS_BLOCKS_RUNTIME 0
# endif
# if __has_feature(address_sanitizer)
# define _LIBCPP_HAS_ASAN 1
# else
# define _LIBCPP_HAS_ASAN 0
# endif
# define _LIBCPP_ALWAYS_INLINE __attribute__((__always_inline__))
# if defined(_LIBCPP_OBJECT_FORMAT_COFF)

View File

@@ -17,7 +17,7 @@
# pragma GCC system_header
#endif
#if _LIBCPP_HAS_ASAN
#if __has_feature(address_sanitizer)
extern "C" {
_LIBCPP_EXPORTED_FROM_ABI void
@@ -28,12 +28,12 @@ _LIBCPP_EXPORTED_FROM_ABI int
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
}
#endif // _LIBCPP_HAS_ASAN
#endif // __has_feature(address_sanitizer)
_LIBCPP_BEGIN_NAMESPACE_STD
// ASan choices
#if _LIBCPP_HAS_ASAN
#if __has_feature(address_sanitizer)
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
#endif
@@ -57,7 +57,7 @@ _LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
const void* __last_old_contained,
const void* __first_new_contained,
const void* __last_new_contained) {
#if !_LIBCPP_HAS_ASAN
#if !__has_feature(address_sanitizer)
(void)__first_storage;
(void)__last_storage;
(void)__first_old_contained;
@@ -86,7 +86,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __annotate_contiguous_c
const void* __last_storage,
const void* __old_last_contained,
const void* __new_last_contained) {
#if !_LIBCPP_HAS_ASAN
#if !__has_feature(address_sanitizer)
(void)__first_storage;
(void)__last_storage;
(void)__old_last_contained;

View File

@@ -122,7 +122,7 @@ _LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {
return !!__f;
}
# if _LIBCPP_HAS_EXTENSION_BLOCKS
# if __has_extension(blocks)
template <class _Rp, class... _Args>
_LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {
return __p;
@@ -757,7 +757,7 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
public:
_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
# if _LIBCPP_HAS_OBJC_ARC
# if __has_feature(objc_arc)
: __f_(__f)
# else
: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
@@ -768,7 +768,7 @@ public:
// [TODO] add && to save on a retain
_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& /* unused */)
# if _LIBCPP_HAS_OBJC_ARC
# if __has_feature(objc_arc)
: __f_(__f)
# else
: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
@@ -790,7 +790,7 @@ public:
}
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
# if !_LIBCPP_HAS_OBJC_ARC
# if !__has_feature(objc_arc)
if (__f_)
_Block_release(__f_);
# endif
@@ -822,7 +822,7 @@ public:
# endif // _LIBCPP_HAS_RTTI
};
# endif // _LIBCPP_HAS_EXTENSION_BLOCKS
# endif // _LIBCPP_HAS_BLOCKS_RUNTIME
} // namespace __function

View File

@@ -23,7 +23,7 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* a
return __builtin_addressof(__x);
}
#if _LIBCPP_HAS_OBJC_ARC
#if __has_feature(objc_arc)
// Objective-C++ Automatic Reference Counting uses qualified pointers
// that require special addressof() signatures.
template <class _Tp>
@@ -31,7 +31,7 @@ inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEP
return &__x;
}
# if _LIBCPP_HAS_OBJC_ARC_WEAK
# if __has_feature(objc_arc_weak)
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
return &__x;

View File

@@ -40,7 +40,7 @@ template <class _Tp>
struct __libcpp_remove_objc_qualifiers {
typedef _Tp type;
};
# if _LIBCPP_HAS_OBJC_ARC
# if __has_feature(objc_arc)
// clang-format off
template <class _Tp> struct __libcpp_remove_objc_qualifiers<_Tp __strong> { typedef _Tp type; };
template <class _Tp> struct __libcpp_remove_objc_qualifiers<_Tp __weak> { typedef _Tp type; };

View File

@@ -37,7 +37,7 @@ _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_scalar_v = __is_scalar(_Tp);
template <class _Tp>
struct __is_block : false_type {};
# if _LIBCPP_HAS_EXTENSION_BLOCKS
# if __has_extension(blocks)
template <class _Rp, class... _Args>
struct __is_block<_Rp (^)(_Args...)> : true_type {};
# endif

View File

@@ -930,7 +930,7 @@ private:
(void)__end;
(void)__annotation_type;
(void)__place;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
// __beg - index of the first item to annotate
// __end - index behind the last item to annotate (so last item + 1)
// __annotation_type - __asan_unposion or __asan_poison
@@ -1023,23 +1023,23 @@ private:
std::__annotate_double_ended_contiguous_container<_Allocator>(
__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
}
# endif // _LIBCPP_HAS_ASAN
# endif // __has_feature(address_sanitizer)
}
_LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
(void)__current_size;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
if (__current_size == 0)
__annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
else {
__annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
__annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
}
# endif // _LIBCPP_HAS_ASAN
# endif // __has_feature(address_sanitizer)
}
_LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
if (empty()) {
for (size_t __i = 0; __i < __map_.size(); ++__i) {
__annotate_whole_block(__i, __asan_unposion);
@@ -1048,19 +1048,19 @@ private:
__annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
__annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
}
# endif // _LIBCPP_HAS_ASAN
# endif // __has_feature(address_sanitizer)
}
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
(void)__n;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
__annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
# endif
}
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
(void)__n;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
__annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
# endif
}
@@ -1068,7 +1068,7 @@ private:
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
(void)__old_size;
(void)__old_start;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
__annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
# endif
}
@@ -1076,7 +1076,7 @@ private:
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
(void)__old_size;
(void)__old_start;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
__annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
# endif
}
@@ -1089,7 +1089,7 @@ private:
__annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
(void)__block_index;
(void)__annotation_type;
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
__map_const_iterator __block_it = __map_.begin() + __block_index;
const void* __block_start = std::__to_address(*__block_it);
const void* __block_end = std::__to_address(*__block_it + __block_size);
@@ -1102,7 +1102,7 @@ private:
}
# endif
}
# if _LIBCPP_HAS_ASAN
# if __has_feature(address_sanitizer)
public:
_LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
@@ -1164,7 +1164,7 @@ public:
}
private:
# endif // _LIBCPP_HAS_ASAN
# endif // __has_feature(address_sanitizer)
_LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
__annotate_whole_block(0, __asan_unposion);

View File

@@ -678,7 +678,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
_LIBCPP_PUSH_MACROS
# include <__undef_macros>
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))
// This macro disables AddressSanitizer (ASan) instrumentation for a specific function,
// allowing memory accesses that would normally trigger ASan errors to proceed without crashing.
@@ -749,7 +749,7 @@ public:
//
// This string implementation doesn't contain any references into itself. It only contains a bit that says whether
// it is in small or large string mode, so the entire structure is trivially relocatable if its members are.
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
// When compiling with AddressSanitizer (ASan), basic_string cannot be trivially
// relocatable. Because the object's memory might be poisoned when its content
// is kept inside objects memory (short string optimization), instead of in allocated
@@ -771,7 +771,7 @@ public:
basic_string,
void>;
# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __asan_volatile_wrapper(pointer const& __ptr) const {
if (__libcpp_is_constant_evaluated())
return __ptr;

View File

@@ -87,7 +87,7 @@ static_assert(!std::__libcpp_is_trivially_relocatable<std::array<NotTriviallyCop
static_assert(std::__libcpp_is_trivially_relocatable<std::array<std::unique_ptr<int>, 1> >::value, "");
// basic_string
#if !_LIBCPP_HAS_ASAN || !_LIBCPP_INSTRUMENTED_WITH_ASAN
#if !__has_feature(address_sanitizer) || !_LIBCPP_INSTRUMENTED_WITH_ASAN
struct MyChar {
char c;
};