Skip to content

[libc++] Remove macros for keeping std::allocator members and void specialization after C++20 #85806

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 5 commits into from
Mar 21, 2024
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
3 changes: 2 additions & 1 deletion libcxx/docs/ReleaseNotes/19.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ Deprecations and Removals
- The ``_LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT`` macro that changed the behavior for narrowing conversions
in ``std::variant`` has been removed in LLVM 19.

- TODO: The ``_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`` macro has been removed in LLVM 19.
- The ``_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`` and ``_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION``
macros have been removed in LLVM 19.

- TODO: The ``_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES`` and ``_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES`` macros have
been removed in LLVM 19. C++17 and C++20 removed features can still be re-enabled individually.
Expand Down
12 changes: 0 additions & 12 deletions libcxx/docs/UsingLibcxx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,6 @@ C++20 Specific Configuration Macros
This macro is deprecated and will be removed in LLVM-19. Use the
individual macros listed below.

**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**:
This macro is used to re-enable redundant members of `allocator<T>`,
including `pointer`, `reference`, `rebind`, `address`, `max_size`,
`construct`, `destroy`, and the two-argument overload of `allocate`.
This macro has been deprecated and will be removed in LLVM-19.

**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**:
This macro is used to re-enable the library-provided specializations of
`allocator<void>` and `allocator<const void>`.
Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`
to ensure that removed members of `allocator<void>` can be accessed.

**_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**:
This macro is used to re-enable the `argument_type`, `result_type`,
`first_argument_type`, and `second_argument_type` members of class
Expand Down
2 changes: 0 additions & 2 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,6 @@ __sanitizer_verify_double_ended_contiguous_container(const void*, const void*, c
# endif // _LIBCPP_ENABLE_CXX17_REMOVED_FEATURES

# if defined(_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES)
# define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
# define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION
# define _LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS
# define _LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS
# define _LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR
Expand Down
16 changes: 5 additions & 11 deletions libcxx/include/__memory/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp>
class allocator;

#if defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS)
# pragma clang deprecated( \
_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS, \
"_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS is deprecated in LLVM 18 and will be removed in LLVM 19")
#endif

#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION)
#if _LIBCPP_STD_VER <= 17
// These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
// Specializing allocator<void> is deprecated, but not using it.
template <>
class _LIBCPP_TEMPLATE_VIS allocator<void> {
# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
# if _LIBCPP_STD_VER <= 17

public:
_LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
Expand All @@ -58,7 +52,7 @@ class _LIBCPP_TEMPLATE_VIS allocator<void> {

template <>
class _LIBCPP_TEMPLATE_VIS allocator<const void> {
# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
# if _LIBCPP_STD_VER <= 17

public:
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
Expand Down Expand Up @@ -141,7 +135,7 @@ class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::v
}

// C++20 Removed members
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
#if _LIBCPP_STD_VER <= 17
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
Expand Down Expand Up @@ -221,7 +215,7 @@ class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
}

// C++20 Removed members
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
#if _LIBCPP_STD_VER <= 17
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// pointer address(reference x) const;
// const_pointer address(const_reference x) const;

// In C++20, parts of std::allocator<T> have been removed.
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
// is defined before including <memory>, then removed members will be restored.
// Removed in C++20, deprecated in C++17.

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
// REQUIRES: c++03 || c++11 || c++14 || c++17
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS

#include <memory>
Expand All @@ -25,25 +23,22 @@
#include "test_macros.h"

template <class T>
void test_address()
{
T* tp = new T();
const T* ctp = tp;
const std::allocator<T> a;
assert(a.address(*tp) == tp);
assert(a.address(*ctp) == tp);
delete tp;
void test_address() {
T* tp = new T();
const T* ctp = tp;
const std::allocator<T> a;
assert(a.address(*tp) == tp);
assert(a.address(*ctp) == tp);
delete tp;
}

struct A
{
void operator&() const {}
struct A {
void operator&() const {}
};

int main(int, char**)
{
test_address<int>();
test_address<A>();
int main(int, char**) {
test_address<int>();
test_address<A>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <memory>

// allocator:
// pointer address(reference x) const;
// const_pointer address(const_reference x) const;

// In C++20, parts of std::allocator<T> have been removed.
// UNSUPPORTED: c++03, c++11, c++14, c++17

#include <memory>
#include <cassert>

#include "test_macros.h"

template <class T>
void test_address() {
T* tp = new T();
const T* ctp = tp;
const std::allocator<T> a;
assert(a.address(*tp) == tp); // expected-error 2 {{no member}}
assert(a.address(*ctp) == tp); // expected-error 2 {{no member}}
delete tp;
}

struct A {
void operator&() const {}
};

int main(int, char**) {
test_address<int>();
test_address<A>();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

// Deprecated in C++17

// UNSUPPORTED: c++03, c++11, c++14

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS -Wno-deprecated-pragma
// REQUIRES: c++17

#include <memory>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
// allocator:
// T* allocate(size_t n, const void* hint);

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
// Removed in C++20, deprecated in C++17.

// REQUIRES: c++03 || c++11 || c++14 || c++17
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS

#include <memory>
#include <cassert>
#include <cstddef> // for std::max_align_t
#include <cstddef> // for std::max_align_t

#include "test_macros.h"
#include "count_new.h"


#ifdef TEST_HAS_NO_ALIGNED_ALLOCATION
static const bool UsingAlignedNew = false;
#else
Expand All @@ -36,7 +37,6 @@ static const std::size_t MaxAligned = std::alignment_of<std::max_align_t>::value

static const std::size_t OverAligned = MaxAligned * 2;


template <std::size_t Align>
struct TEST_ALIGNAS(Align) AlignedType {
char data;
Expand All @@ -48,19 +48,18 @@ struct TEST_ALIGNAS(Align) AlignedType {
template <std::size_t Align>
int AlignedType<Align>::constructed = 0;


template <std::size_t Align>
void test_aligned() {
typedef AlignedType<Align> T;
T::constructed = 0;
globalMemCounter.reset();
std::allocator<T> a;
const bool IsOverAlignedType = Align > MaxAligned;
const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;
const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;
{
globalMemCounter.last_new_size = 0;
globalMemCounter.last_new_size = 0;
globalMemCounter.last_new_align = 0;
T* ap2 = a.allocate(11, (const void*)5);
T* ap2 = a.allocate(11, (const void*)5);
DoNotOptimize(ap2);
assert(globalMemCounter.checkOutstandingNewEq(1));
assert(globalMemCounter.checkNewCalledEq(1));
Expand All @@ -80,14 +79,14 @@ void test_aligned() {
}

int main(int, char**) {
test_aligned<1>();
test_aligned<2>();
test_aligned<4>();
test_aligned<8>();
test_aligned<16>();
test_aligned<MaxAligned>();
test_aligned<OverAligned>();
test_aligned<OverAligned * 2>();
test_aligned<1>();
test_aligned<2>();
test_aligned<4>();
test_aligned<8>();
test_aligned<16>();
test_aligned<MaxAligned>();
test_aligned<OverAligned>();
test_aligned<OverAligned * 2>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// 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, c++11, c++14, c++17

// <memory>

// allocator:
// T* allocate(size_t n, const void* hint);

// Removed in C++20.

#include <memory>

void f() {
std::allocator<int> a;
a.allocate(3, nullptr); // expected-error {{too many arguments to function call}}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

// Deprecated in C++17

// UNSUPPORTED: c++03, c++11, c++14

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS -Wno-deprecated-pragma
// REQUIRES: c++17

#include <memory>

Expand Down
Loading