Skip to content

Commit 5b1e5b4

Browse files
mparkldionne
authored andcommitted
[libc++][P0174] Deprecated/removed parts of default allocator.
Differential Revision: https://reviews.llvm.org/D70117
1 parent 0ab109d commit 5b1e5b4

32 files changed

+579
-172
lines changed

libcxx/include/memory

Lines changed: 158 additions & 100 deletions
Large diffs are not rendered by default.

libcxx/test/std/utilities/memory/default.allocator/allocator.members/address.pass.cpp renamed to libcxx/test/libcxx/depr/depr.default.allocator/allocator.members/address.cxx2a.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
// pointer address(reference x) const;
1313
// const_pointer address(const_reference x) const;
1414

15+
// In C++20, parts of std::allocator<T> have been removed.
16+
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
17+
// is defined before including <memory>, then removed members will be restored.
18+
19+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
20+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
21+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
22+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
23+
1524
#include <memory>
1625
#include <cassert>
1726

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <memory>
10+
11+
// allocator:
12+
// pointer address(reference x) const;
13+
// const_pointer address(const_reference x) const;
14+
15+
// Deprecated in C++17
16+
17+
// UNSUPPORTED: c++98, c++03, c++11, c++14
18+
// REQUIRES: verify-support
19+
20+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
21+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
22+
23+
#include <memory>
24+
#include "test_macros.h"
25+
26+
int main(int, char**)
27+
{
28+
int x = 0;
29+
std::allocator<int> a;
30+
31+
int* p = a.address(x); // expected-error{{'address' is deprecated}}
32+
(void)p;
33+
34+
return 0;
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5, clang-3.6, clang-3.7, clang-3.8
11+
12+
// <memory>
13+
14+
// allocator:
15+
// T* allocate(size_t n, const void* hint);
16+
17+
// In C++20, parts of std::allocator<T> have been removed.
18+
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
19+
// is defined before including <memory>, then removed members will be restored.
20+
21+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
22+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
23+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
24+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
25+
26+
#include <memory>
27+
#include <cassert>
28+
29+
#include "test_macros.h"
30+
31+
int main(int, char**)
32+
{
33+
std::allocator<int> a;
34+
a.allocate(3, nullptr); // expected-error {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
36+
return 0;
37+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <memory>
10+
11+
// allocator:
12+
// T* allocate(size_t n, const void* hint);
13+
14+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
15+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
16+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
17+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
18+
19+
#include <memory>
20+
#include <cassert>
21+
#include <cstddef> // for std::max_align_t
22+
23+
#include "test_macros.h"
24+
#include "count_new.h"
25+
26+
27+
#ifdef TEST_HAS_NO_ALIGNED_ALLOCATION
28+
static const bool UsingAlignedNew = false;
29+
#else
30+
static const bool UsingAlignedNew = true;
31+
#endif
32+
33+
#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
34+
static const size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__;
35+
#else
36+
static const size_t MaxAligned = std::alignment_of<std::max_align_t>::value;
37+
#endif
38+
39+
static const size_t OverAligned = MaxAligned * 2;
40+
41+
42+
template <size_t Align>
43+
struct TEST_ALIGNAS(Align) AlignedType {
44+
char data;
45+
static int constructed;
46+
AlignedType() { ++constructed; }
47+
AlignedType(AlignedType const&) { ++constructed; }
48+
~AlignedType() { --constructed; }
49+
};
50+
template <size_t Align>
51+
int AlignedType<Align>::constructed = 0;
52+
53+
54+
template <size_t Align>
55+
void test_aligned() {
56+
typedef AlignedType<Align> T;
57+
T::constructed = 0;
58+
globalMemCounter.reset();
59+
std::allocator<T> a;
60+
const bool IsOverAlignedType = Align > MaxAligned;
61+
const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;
62+
{
63+
globalMemCounter.last_new_size = 0;
64+
globalMemCounter.last_new_align = 0;
65+
T* volatile ap2 = a.allocate(11, (const void*)5);
66+
DoNotOptimize(ap2);
67+
assert(globalMemCounter.checkOutstandingNewEq(1));
68+
assert(globalMemCounter.checkNewCalledEq(1));
69+
assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
70+
assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T)));
71+
assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
72+
assert(T::constructed == 0);
73+
globalMemCounter.last_delete_align = 0;
74+
a.deallocate(ap2, 11);
75+
DoNotOptimize(ap2);
76+
assert(globalMemCounter.checkOutstandingNewEq(0));
77+
assert(globalMemCounter.checkDeleteCalledEq(1));
78+
assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
79+
assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
80+
assert(T::constructed == 0);
81+
}
82+
}
83+
84+
int main(int, char**) {
85+
test_aligned<1>();
86+
test_aligned<2>();
87+
test_aligned<4>();
88+
test_aligned<8>();
89+
test_aligned<16>();
90+
test_aligned<MaxAligned>();
91+
test_aligned<OverAligned>();
92+
test_aligned<OverAligned * 2>();
93+
94+
return 0;
95+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <memory>
10+
11+
// allocator:
12+
// T* allocate(size_t n, const void* hint);
13+
14+
// Deprecated in C++17
15+
16+
// UNSUPPORTED: c++98, c++03, c++11, c++14
17+
// REQUIERS: verify-support
18+
19+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
20+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
21+
22+
#include <memory>
23+
#include "test_macros.h"
24+
25+
int main(int, char**)
26+
{
27+
std::allocator<int> a;
28+
TEST_IGNORE_NODISCARD a.allocate(3, nullptr); // expected-error {{'allocate' is deprecated}}
29+
30+
return 0;
31+
}

libcxx/test/std/utilities/memory/default.allocator/allocator.members/construct.pass.cpp renamed to libcxx/test/libcxx/depr/depr.default.allocator/allocator.members/construct.cxx2a.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
// allocator:
1212
// template <class... Args> void construct(pointer p, Args&&... args);
1313

14+
// In C++20, parts of std::allocator<T> have been removed.
15+
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
16+
// is defined before including <memory>, then removed members will be restored.
17+
18+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
19+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
20+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
21+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
22+
1423
#include <memory>
1524
#include <cassert>
1625

libcxx/test/std/utilities/memory/default.allocator/allocator.members/max_size.pass.cpp renamed to libcxx/test/libcxx/depr/depr.default.allocator/allocator.members/max_size.cxx2a.pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
// allocator:
1212
// size_type max_size() const throw();
1313

14+
// In C++20, parts of std::allocator<T> have been removed.
15+
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
16+
// is defined before including <memory>, then removed members will be restored.
17+
18+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
19+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
20+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
21+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
22+
1423
#include <memory>
1524
#include <limits>
1625
#include <cstddef>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <memory>
10+
11+
// check nested types:
12+
13+
// template <class T>
14+
// class allocator
15+
// {
16+
// public:
17+
// typedef size_t size_type;
18+
// typedef ptrdiff_t difference_type;
19+
// typedef T* pointer;
20+
// typedef const T* const_pointer;
21+
// typedef typename add_lvalue_reference<T>::type reference;
22+
// typedef typename add_lvalue_reference<const T>::type const_reference;
23+
//
24+
// template <class U> struct rebind {typedef allocator<U> other;};
25+
// ...
26+
// };
27+
28+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
29+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
30+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
31+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
32+
33+
#include <memory>
34+
#include <type_traits>
35+
#include <cstddef>
36+
37+
#include "test_macros.h"
38+
39+
int main(int, char**)
40+
{
41+
static_assert((std::is_same<std::allocator<char>::size_type, std::size_t>::value), "");
42+
static_assert((std::is_same<std::allocator<char>::difference_type, std::ptrdiff_t>::value), "");
43+
static_assert((std::is_same<std::allocator<char>::pointer, char*>::value), "");
44+
static_assert((std::is_same<std::allocator<char>::const_pointer, const char*>::value), "");
45+
static_assert((std::is_same<std::allocator<char>::reference, char&>::value), "");
46+
static_assert((std::is_same<std::allocator<char>::const_reference, const char&>::value), "");
47+
static_assert((std::is_same<std::allocator<char>::rebind<int>::other,
48+
std::allocator<int> >::value), "");
49+
50+
return 0;
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <memory>
10+
11+
// check nested types:
12+
13+
// template <class T>
14+
// class allocator
15+
// {
16+
// public:
17+
// typedef size_t size_type;
18+
// typedef ptrdiff_t difference_type;
19+
// typedef T* pointer;
20+
// typedef const T* const_pointer;
21+
// typedef typename add_lvalue_reference<T>::type reference;
22+
// typedef typename add_lvalue_reference<const T>::type const_reference;
23+
//
24+
// template <class U> struct rebind {typedef allocator<U> other;};
25+
// ...
26+
// };
27+
28+
// Deprecated in C++17
29+
30+
// UNSUPPORTED: c++98, c++03, c++11, c++14
31+
// REQUIRES: verify-support
32+
33+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
34+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
35+
36+
#include <memory>
37+
#include "test_macros.h"
38+
39+
int main(int, char**)
40+
{
41+
typedef std::allocator<char>::size_type AST; // expected-error{{'size_type' is deprecated}}
42+
typedef std::allocator<char>::difference_type ADT; // expected-error{{'difference_type' is deprecated}}
43+
typedef std::allocator<char>::pointer AP; // expected-error{{'pointer' is deprecated}}
44+
typedef std::allocator<char>::const_pointer ACP; // expected-error{{'const_pointer' is deprecated}}
45+
typedef std::allocator<char>::reference AR; // expected-error{{'reference' is deprecated}}
46+
typedef std::allocator<char>::const_reference ACR; // expected-error{{'const_reference' is deprecated}}
47+
typedef std::allocator<char>::rebind<int>::other ARO; // expected-error{{'rebind<int>' is deprecated}}
48+
49+
return 0;
50+
}

libcxx/test/std/utilities/memory/default.allocator/allocator_void.pass.cpp renamed to libcxx/test/libcxx/depr/depr.default.allocator/allocator_void.cxx2a.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
// template <class _Up> struct rebind {typedef allocator<_Up> other;};
2020
// };
2121

22+
// MODULES_DEFINES: _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
23+
// MODULES_DEFINES: _LIBCPP_DISABLE_DEPRECATION_WARNINGS
24+
#define _LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
25+
#define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
26+
2227
#include <memory>
2328
#include <type_traits>
2429

0 commit comments

Comments
 (0)