diff --git a/libcxx/include/__config b/libcxx/include/__config index 08ca88bd773b..ffd37c1fbc1b 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -52,6 +52,11 @@ // provided under the alternate keyword __nullptr, which changes the mangling // of nullptr_t. This option is ABI incompatible with GCC in C++03 mode. #define _LIBCPP_ABI_ALWAYS_USE_CXX11_NULLPTR +// Define the `pointer_safety` enum as a C++11 strongly typed enumeration +// instead of as a class simulating an enum. If this option is enabled +// `pointer_safety` and `get_pointer_safety()` will no longer be available +// in C++03. +#define _LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE #elif _LIBCPP_ABI_VERSION == 1 #if !defined(_WIN32) // Enable compiling a definition of error_category() into the libc++ dylib. diff --git a/libcxx/include/memory b/libcxx/include/memory index d851295e0b72..3ea9a77d0d4e 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -5616,6 +5616,15 @@ atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) //enum class +#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) +# ifndef _LIBCPP_CXX03_LANG +enum class pointer_safety : unsigned char { + relaxed, + preferred, + strict +}; +# endif +#else struct _LIBCPP_TYPE_VIS pointer_safety { enum __lx @@ -5632,11 +5641,25 @@ struct _LIBCPP_TYPE_VIS pointer_safety _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} }; +#endif + +#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \ + defined(_LIBCPP_BUILDING_MEMORY) +_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT; +#else +// This function is only offered in C++03 under ABI v1. +# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG) +inline _LIBCPP_INLINE_VISIBILITY +pointer_safety get_pointer_safety() _NOEXCEPT { + return pointer_safety::relaxed; +} +# endif +#endif + _LIBCPP_FUNC_VIS void declare_reachable(void* __p); _LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n); _LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n); -_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT; _LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p); template diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp index 514a2ce22667..8569fafb337c 100644 --- a/libcxx/src/memory.cpp +++ b/libcxx/src/memory.cpp @@ -220,11 +220,12 @@ undeclare_no_pointers(char*, size_t) { } -pointer_safety -get_pointer_safety() _NOEXCEPT +#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) +pointer_safety get_pointer_safety() _NOEXCEPT { return pointer_safety::relaxed; } +#endif void* __undeclare_reachable(void* p) diff --git a/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_cxx03.pass.cpp b/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_cxx03.pass.cpp new file mode 100644 index 000000000000..04671c3a9679 --- /dev/null +++ b/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_cxx03.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// pointer_safety get_pointer_safety(); + +#include +#include + +int main() +{ + // Test that std::pointer_safety is still offered in C++03 under the old ABI. +#ifndef _LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE + std::pointer_safety r = std::get_pointer_safety(); + assert(r == std::pointer_safety::relaxed || + r == std::pointer_safety::preferred || + r == std::pointer_safety::strict); +#endif +} diff --git a/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_new_abi.pass.cpp b/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_new_abi.pass.cpp new file mode 100644 index 000000000000..752edb6dadd9 --- /dev/null +++ b/libcxx/test/libcxx/utilities/memory/util.dynamic.safety/get_pointer_safety_new_abi.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// pointer_safety get_pointer_safety(); + +// The pointer_safety interface is no longer provided in C++03 in the new ABI. +// XFAIL: c++98, c++03 + +// MODULES_DEFINES: _LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE +#define _LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE +#include +#include + +int main() +{ + { + static_assert(std::is_enum::value, ""); + static_assert(!std::is_convertible::value, ""); + static_assert(std::is_same< + std::underlying_type::type, + unsigned char + >::value, ""); + } + { + std::pointer_safety r = std::get_pointer_safety(); + assert(r == std::pointer_safety::relaxed || + r == std::pointer_safety::preferred || + r == std::pointer_safety::strict); + } +} diff --git a/libcxx/test/std/utilities/memory/util.dynamic.safety/get_pointer_safety.pass.cpp b/libcxx/test/std/utilities/memory/util.dynamic.safety/get_pointer_safety.pass.cpp index 1f27b45e8ab8..ee2f81713b29 100644 --- a/libcxx/test/std/utilities/memory/util.dynamic.safety/get_pointer_safety.pass.cpp +++ b/libcxx/test/std/utilities/memory/util.dynamic.safety/get_pointer_safety.pass.cpp @@ -11,6 +11,8 @@ // pointer_safety get_pointer_safety(); +// UNSUPPORTED: c++98, c++03 + #include #include