From 040e9e02ccbf69bebd293a4c389948d7ecbc7bd9 Mon Sep 17 00:00:00 2001 From: Nikolas Klauser Date: Tue, 10 Jun 2025 05:54:15 +0200 Subject: [PATCH] [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. --- libcxx/include/__config | 27 +------------------ libcxx/include/__debug_utils/sanitizers.h | 10 +++---- libcxx/include/__functional/function.h | 10 +++---- libcxx/include/__memory/addressof.h | 4 +-- libcxx/include/__type_traits/is_pointer.h | 2 +- libcxx/include/__type_traits/is_scalar.h | 2 +- libcxx/include/deque | 26 +++++++++--------- libcxx/include/string | 6 ++--- .../is_trivially_relocatable.compile.pass.cpp | 2 +- 9 files changed, 32 insertions(+), 57 deletions(-) diff --git a/libcxx/include/__config b/libcxx/include/__config index 110450f6e9c5..38c47e8d45c8 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -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) diff --git a/libcxx/include/__debug_utils/sanitizers.h b/libcxx/include/__debug_utils/sanitizers.h index 73d192711eab..058feab0269e 100644 --- a/libcxx/include/__debug_utils/sanitizers.h +++ b/libcxx/include/__debug_utils/sanitizers.h @@ -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; diff --git a/libcxx/include/__functional/function.h b/libcxx/include/__functional/function.h index e33c5ab9b886..733f321925a4 100644 --- a/libcxx/include/__functional/function.h +++ b/libcxx/include/__functional/function.h @@ -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 _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 diff --git a/libcxx/include/__memory/addressof.h b/libcxx/include/__memory/addressof.h index 98b08958a6a9..667071dfc663 100644 --- a/libcxx/include/__memory/addressof.h +++ b/libcxx/include/__memory/addressof.h @@ -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 @@ -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 inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT { return &__x; diff --git a/libcxx/include/__type_traits/is_pointer.h b/libcxx/include/__type_traits/is_pointer.h index 7bc78a601866..3c58656e0e61 100644 --- a/libcxx/include/__type_traits/is_pointer.h +++ b/libcxx/include/__type_traits/is_pointer.h @@ -40,7 +40,7 @@ template struct __libcpp_remove_objc_qualifiers { typedef _Tp type; }; -# if _LIBCPP_HAS_OBJC_ARC +# if __has_feature(objc_arc) // clang-format off template struct __libcpp_remove_objc_qualifiers<_Tp __strong> { typedef _Tp type; }; template struct __libcpp_remove_objc_qualifiers<_Tp __weak> { typedef _Tp type; }; diff --git a/libcxx/include/__type_traits/is_scalar.h b/libcxx/include/__type_traits/is_scalar.h index 102a0ca6f2fd..4cf8c3948d9f 100644 --- a/libcxx/include/__type_traits/is_scalar.h +++ b/libcxx/include/__type_traits/is_scalar.h @@ -37,7 +37,7 @@ _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_scalar_v = __is_scalar(_Tp); template struct __is_block : false_type {}; -# if _LIBCPP_HAS_EXTENSION_BLOCKS +# if __has_extension(blocks) template struct __is_block<_Rp (^)(_Args...)> : true_type {}; # endif diff --git a/libcxx/include/deque b/libcxx/include/deque index 5b9aaf8788a6..e33e7d31500a 100644 --- a/libcxx/include/deque +++ b/libcxx/include/deque @@ -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); diff --git a/libcxx/include/string b/libcxx/include/string index 67c4c537eb3b..c450ddcb4191 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -678,7 +678,7 @@ basic_string 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; diff --git a/libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp b/libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp index 213d06d314a0..8066925f2900 100644 --- a/libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp +++ b/libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp @@ -87,7 +87,7 @@ static_assert(!std::__libcpp_is_trivially_relocatable, 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; };