Skip to content

Commit

Permalink
[libc++] Remove workarounds for missing __builtin_addressof
Browse files Browse the repository at this point in the history
All supported compilers implement __builtin_addressof. Even MSVC implements
addressof as a simple call to __builtin_addressof, so it would work if we
were to port libc++ to that compiler.

Differential Revision: https://reviews.llvm.org/D107905
  • Loading branch information
ldionne committed Aug 11, 2021
1 parent 2c1789b commit 7c81024
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 221 deletions.
4 changes: 0 additions & 4 deletions libcxx/include/__config
Expand Up @@ -1283,10 +1283,6 @@ extern "C" _LIBCPP_FUNC_VIS void __sanitizer_annotate_contiguous_container(
# define _LIBCPP_SAFE_STATIC
#endif

#if !__has_builtin(__builtin_addressof) && _GNUC_VER < 700
#define _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
#endif

#if __has_attribute(diagnose_if) && !defined(_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS)
# define _LIBCPP_DIAGNOSE_WARNING(...) \
__attribute__((diagnose_if(__VA_ARGS__, "warning")))
Expand Down
15 changes: 0 additions & 15 deletions libcxx/include/__memory/addressof.h
Expand Up @@ -21,8 +21,6 @@ _LIBCPP_PUSH_MACROS

_LIBCPP_BEGIN_NAMESPACE_STD

#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF

template <class _Tp>
inline _LIBCPP_CONSTEXPR_AFTER_CXX14
_LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
Expand All @@ -32,19 +30,6 @@ addressof(_Tp& __x) _NOEXCEPT
return __builtin_addressof(__x);
}

#else

template <class _Tp>
inline _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
_Tp*
addressof(_Tp& __x) _NOEXCEPT
{
return reinterpret_cast<_Tp *>(
const_cast<char *>(&reinterpret_cast<const volatile char &>(__x)));
}

#endif // _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF

#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
// Objective-C++ Automatic Reference Counting uses qualified pointers
// that require special addressof() signatures. When
Expand Down
25 changes: 0 additions & 25 deletions libcxx/include/optional
Expand Up @@ -883,11 +883,7 @@ public:
operator->() const
{
_LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value");
#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
return _VSTD::addressof(this->__get());
#else
return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
#endif
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -896,11 +892,7 @@ public:
operator->()
{
_LIBCPP_ASSERT(this->has_value(), "optional operator-> called on a disengaged value");
#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
return _VSTD::addressof(this->__get());
#else
return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
#endif
}

_LIBCPP_INLINE_VISIBILITY
Expand Down Expand Up @@ -1006,23 +998,6 @@ public:
}

using __base::reset;

private:
template <class _Up>
_LIBCPP_INLINE_VISIBILITY
static _LIBCPP_CONSTEXPR_AFTER_CXX17 _Up*
__operator_arrow(true_type, _Up& __x)
{
return _VSTD::addressof(__x);
}

template <class _Up>
_LIBCPP_INLINE_VISIBILITY
static constexpr _Up*
__operator_arrow(false_type, _Up& __x)
{
return &__x;
}
};

#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
Expand Down
34 changes: 0 additions & 34 deletions libcxx/include/type_traits
Expand Up @@ -4349,40 +4349,6 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename __sfinae_underlying_type<_Tp>::__promoted_type
__convert_to_integral(_Tp __val) { return __val; }

#ifndef _LIBCPP_CXX03_LANG

template <class _Tp>
struct __has_operator_addressof_member_imp
{
template <class _Up>
static auto __test(int)
-> typename __select_2nd<decltype(declval<_Up>().operator&()), true_type>::type;
template <class>
static auto __test(long) -> false_type;

static const bool value = decltype(__test<_Tp>(0))::value;
};

template <class _Tp>
struct __has_operator_addressof_free_imp
{
template <class _Up>
static auto __test(int)
-> typename __select_2nd<decltype(operator&(declval<_Up>())), true_type>::type;
template <class>
static auto __test(long) -> false_type;

static const bool value = decltype(__test<_Tp>(0))::value;
};

template <class _Tp>
struct __has_operator_addressof
: public integral_constant<bool, __has_operator_addressof_member_imp<_Tp>::value
|| __has_operator_addressof_free_imp<_Tp>::value>
{};

#endif // _LIBCPP_CXX03_LANG

// is_scoped_enum [meta.unary.prop]

#if _LIBCPP_STD_VER > 20
Expand Down
4 changes: 1 addition & 3 deletions libcxx/include/version
Expand Up @@ -199,9 +199,7 @@ __cpp_lib_void_t 201411L <type_traits>
#endif

#if _LIBCPP_STD_VER > 14
# if !defined(_LIBCPP_HAS_NO_BUILTIN_ADDRESSOF)
# define __cpp_lib_addressof_constexpr 201603L
# endif
# define __cpp_lib_addressof_constexpr 201603L
# define __cpp_lib_allocator_traits_is_always_equal 201411L
# define __cpp_lib_any 201606L
# define __cpp_lib_apply 201603L
Expand Down

This file was deleted.

Expand Up @@ -178,17 +178,11 @@

#elif TEST_STD_VER == 17

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++17"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++17"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++17"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++17"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down Expand Up @@ -274,17 +268,11 @@

#elif TEST_STD_VER == 20

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++20"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++20"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++20"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++20"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down Expand Up @@ -424,17 +412,11 @@

#elif TEST_STD_VER > 20

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++2b"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++2b"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++2b"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++2b"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down
Expand Up @@ -1285,17 +1285,11 @@

#elif TEST_STD_VER == 17

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++17"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++17"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++17"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++17"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down Expand Up @@ -2091,17 +2085,11 @@

#elif TEST_STD_VER == 20

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++20"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++20"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++20"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++20"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down Expand Up @@ -3254,17 +3242,11 @@

#elif TEST_STD_VER > 20

# if TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++2b"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++2b"
# endif
# else
# ifdef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should not be defined when TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700 is not defined!"
# endif
# ifndef __cpp_lib_addressof_constexpr
# error "__cpp_lib_addressof_constexpr should be defined in c++2b"
# endif
# if __cpp_lib_addressof_constexpr != 201603L
# error "__cpp_lib_addressof_constexpr should have the value 201603L in c++2b"
# endif

# ifndef __cpp_lib_allocator_traits_is_always_equal
Expand Down
2 changes: 0 additions & 2 deletions libcxx/utils/generate_feature_test_macro_components.py
Expand Up @@ -66,8 +66,6 @@ def add_version_header(tc):
"name": "__cpp_lib_addressof_constexpr",
"values": { "c++17": 201603 },
"headers": ["memory"],
"test_suite_guard": "TEST_HAS_BUILTIN(__builtin_addressof) || TEST_GCC_VER >= 700",
"libcxx_guard": "!defined(_LIBCPP_HAS_NO_BUILTIN_ADDRESSOF)",
}, {
"name": "__cpp_lib_allocator_traits_is_always_equal",
"values": { "c++17": 201411 },
Expand Down

0 comments on commit 7c81024

Please sign in to comment.