Skip to content

[libc++] Inline __has_feature and __has_extension uses #133634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions libcxx/include/__debug_utils/sanitizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions libcxx/include/__functional/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__memory/addressof.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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>
inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
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;
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__type_traits/is_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__type_traits/is_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -1048,35 +1048,35 @@ 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
}

_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
}

_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
}
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
Loading