[libcxx-commits] [libcxx] [libc++] Inline __has_feature and __has_extension uses (PR #133634)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Mar 30 02:58:36 PDT 2025
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/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.
>From 74841339823b116429f4441bafb36f8ac3a1ddfb Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 30 Mar 2025 11:57:35 +0200
Subject: [PATCH] [libc++] Inline __has_feature and __has_extension uses
---
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/__vector/vector.h | 12 ++++-----
libcxx/include/deque | 26 +++++++++---------
libcxx/include/string | 16 +++++------
.../is_trivially_relocatable.compile.pass.cpp | 2 +-
10 files changed, 43 insertions(+), 68 deletions(-)
diff --git a/libcxx/include/__config b/libcxx/include/__config
index 30fe0ef6a3b53..23ead799fe1b9 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__))
# define _LIBCPP_DISABLE_EXTENSION_WARNING __extension__
diff --git a/libcxx/include/__debug_utils/sanitizers.h b/libcxx/include/__debug_utils/sanitizers.h
index 73d192711eabb..058feab0269e1 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 f33f424c66c22..c224a0a8fba18 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 <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 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
// [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 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
}
_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 @@ class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base
# 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 98b08958a6a93..667071dfc6635 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 <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;
diff --git a/libcxx/include/__type_traits/is_pointer.h b/libcxx/include/__type_traits/is_pointer.h
index 5647bf4045ff3..671477ee05e84 100644
--- a/libcxx/include/__type_traits/is_pointer.h
+++ b/libcxx/include/__type_traits/is_pointer.h
@@ -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; };
diff --git a/libcxx/include/__type_traits/is_scalar.h b/libcxx/include/__type_traits/is_scalar.h
index 6ef57e1dd22d5..2e5eaecf07b39 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 <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
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 8818eb7dfe26e..8bfdb4f4be807 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -717,27 +717,27 @@ class _LIBCPP_TEMPLATE_VIS vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _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)
__annotate_contiguous_container(data() + capacity(), data() + __current_size);
#endif
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
-#if _LIBCPP_HAS_ASAN
+#if __has_feature(address_sanitizer)
__annotate_contiguous_container(data() + size(), data() + capacity());
#endif
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase(size_type __n) const _NOEXCEPT {
(void)__n;
-#if _LIBCPP_HAS_ASAN
+#if __has_feature(address_sanitizer)
__annotate_contiguous_container(data() + size(), data() + size() + __n);
#endif
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
(void)__old_size;
-#if _LIBCPP_HAS_ASAN
+#if __has_feature(address_sanitizer)
__annotate_contiguous_container(data() + __old_size, data() + size());
#endif
}
@@ -745,14 +745,14 @@ class _LIBCPP_TEMPLATE_VIS vector {
struct _ConstructTransaction {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(vector& __v, size_type __n)
: __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
-#if _LIBCPP_HAS_ASAN
+#if __has_feature(address_sanitizer)
__v_.__annotate_increase(__n);
#endif
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
__v_.__end_ = __pos_;
-#if _LIBCPP_HAS_ASAN
+#if __has_feature(address_sanitizer)
if (__pos_ != __new_end_) {
__v_.__annotate_shrink(__new_end_ - __v_.__begin_);
}
diff --git a/libcxx/include/deque b/libcxx/include/deque
index bab76d442a21b..49e80ebc48da7 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -925,7 +925,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
@@ -1018,23 +1018,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);
@@ -1043,19 +1043,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
}
@@ -1063,7 +1063,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
}
@@ -1071,7 +1071,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
}
@@ -1084,7 +1084,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);
@@ -1097,7 +1097,7 @@ private:
}
# endif
}
-# if _LIBCPP_HAS_ASAN
+# if __has_feature(address_sanitizer)
public:
_LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
@@ -1159,7 +1159,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 fa87dc2fddb59..3e7c259ab60ff 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -676,7 +676,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.
@@ -747,7 +747,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
@@ -762,7 +762,7 @@ public:
void>;
# endif
-# 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;
@@ -2153,7 +2153,7 @@ private:
__annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
(void)__old_mid;
(void)__new_mid;
-# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
+# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
# if defined(__APPLE__)
// TODO: remove after addressing issue #96099 (https://github.com/llvm/llvm-project/issues/96099)
if (!__is_long())
@@ -2165,14 +2165,14 @@ private:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
(void)__current_size;
-# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
+# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
if (!__libcpp_is_constant_evaluated())
__annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
# endif
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
-# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
+# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
if (!__libcpp_is_constant_evaluated())
__annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
# endif
@@ -2180,7 +2180,7 @@ private:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
(void)__n;
-# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
+# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
if (!__libcpp_is_constant_evaluated())
__annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
# endif
@@ -2188,7 +2188,7 @@ private:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
(void)__old_size;
-# if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
+# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN
if (!__libcpp_is_constant_evaluated())
__annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
# endif
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 213d06d314a07..8066925f2900a 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<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;
};
More information about the libcxx-commits
mailing list