[libcxx-commits] [libcxx] 7c81024 - [libc++] Remove workarounds for missing __builtin_addressof

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 11 14:06:25 PDT 2021


Author: Louis Dionne
Date: 2021-08-11T17:05:12-04:00
New Revision: 7c81024a06d6ab5c3743ab3fb494d172fd308e49

URL: https://github.com/llvm/llvm-project/commit/7c81024a06d6ab5c3743ab3fb494d172fd308e49
DIFF: https://github.com/llvm/llvm-project/commit/7c81024a06d6ab5c3743ab3fb494d172fd308e49.diff

LOG: [libc++] Remove workarounds for missing __builtin_addressof

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

Added: 
    

Modified: 
    libcxx/include/__config
    libcxx/include/__memory/addressof.h
    libcxx/include/optional
    libcxx/include/type_traits
    libcxx/include/version
    libcxx/test/std/language.support/support.limits/support.limits.general/memory.version.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/__has_operator_addressof.pass.cpp


################################################################################
diff  --git a/libcxx/include/__config b/libcxx/include/__config
index ca7d9ed435442..a573629994316 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -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")))

diff  --git a/libcxx/include/__memory/addressof.h b/libcxx/include/__memory/addressof.h
index 5efdb5878625c..3ce7d0830dfdb 100644
--- a/libcxx/include/__memory/addressof.h
+++ b/libcxx/include/__memory/addressof.h
@@ -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
@@ -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

diff  --git a/libcxx/include/optional b/libcxx/include/optional
index 118db66a4abc9..5aec96927acf1 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -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
@@ -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
@@ -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

diff  --git a/libcxx/include/type_traits b/libcxx/include/type_traits
index af2f620ba9471..e685a126cc85c 100644
--- a/libcxx/include/type_traits
+++ b/libcxx/include/type_traits
@@ -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

diff  --git a/libcxx/include/version b/libcxx/include/version
index 6fae89110fd62..8db08efbadc82 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -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

diff  --git a/libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/__has_operator_addressof.pass.cpp b/libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/__has_operator_addressof.pass.cpp
deleted file mode 100644
index b0c2ff7f9354e..0000000000000
--- a/libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/__has_operator_addressof.pass.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03
-
-// type_traits
-
-// extension
-
-// template <typename _Tp> struct __has_operator_addressof
-
-
-#include <type_traits>
-
-#include "test_macros.h"
-
-
-struct A
-{
-};
-
-struct B
-{
-    constexpr B* operator&() const;
-};
-
-struct D;
-
-struct C
-{
-    template <class U>
-    D operator,(U&&);
-};
-
-struct E
-{
-    constexpr C operator&() const;
-};
-
-struct F {};
-constexpr F* operator&(F const &) { return nullptr; }
-
-struct G {};
-constexpr G* operator&(G &&) { return nullptr; }
-
-struct H {};
-constexpr H* operator&(H const &&) { return nullptr; }
-
-struct J
-{
-    constexpr J* operator&() const &&;
-};
-
-
-int main(int, char**)
-{
-    static_assert(std::__has_operator_addressof<int>::value == false, "");
-    static_assert(std::__has_operator_addressof<A>::value == false, "");
-    static_assert(std::__has_operator_addressof<B>::value == true, "");
-    static_assert(std::__has_operator_addressof<E>::value == true, "");
-    static_assert(std::__has_operator_addressof<F>::value == true, "");
-    static_assert(std::__has_operator_addressof<G>::value == true, "");
-    static_assert(std::__has_operator_addressof<H>::value == true, "");
-    static_assert(std::__has_operator_addressof<J>::value == true, "");
-
-  return 0;
-}

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/memory.version.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/memory.version.pass.cpp
index 8f085e0cdec06..7ebef80226db2 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/memory.version.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/memory.version.pass.cpp
@@ -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
@@ -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
@@ -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

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp
index 03ed1cac8726b..b7054f9aa64c3 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp
@@ -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
@@ -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
@@ -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

diff  --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index da3681fd779c7..4153790848669 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -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 },


        


More information about the libcxx-commits mailing list