Debian Codegraph @

libcxx/test/libcxx-03/containers/associative/unord.map/abi.compile.pass.cpp

raw
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: libcpp-has-abi-fix-unordered-container-size-type, libcpp-abi-no-compressed-pair-padding
10
11 // std::unique_ptr is used as an implementation detail of the unordered containers, so the layout of
12 // unordered containers changes when bounded unique_ptr is enabled.
13 // UNSUPPORTED: libcpp-has-abi-bounded-unique_ptr
14
15 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
16
17 #include <cstdint>
18 #include <unordered_map>
19
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "test_macros.h"
23
24 template <class T>
25 class small_pointer {
26 std::uint16_t offset;
27 };
28
29 template <class T>
30 class small_iter_allocator {
31 public:
32 using value_type = T;
33 using pointer = small_pointer<T>;
34 using size_type = std::uint16_t;
35 using difference_type = std::int16_t;
36
37 small_iter_allocator() TEST_NOEXCEPT {}
38
39 template <class U>
40 small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {}
41
42 T* allocate(std::size_t n);
43 void deallocate(T* p, std::size_t);
44
45 friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; }
46 friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; }
47 };
48
49 template <class T>
50 class final_small_iter_allocator final {
51 public:
52 using value_type = T;
53 using pointer = small_pointer<T>;
54 using size_type = std::uint16_t;
55 using difference_type = std::int16_t;
56
57 final_small_iter_allocator() TEST_NOEXCEPT {}
58
59 template <class U>
60 final_small_iter_allocator(final_small_iter_allocator<U>) TEST_NOEXCEPT {}
61
62 T* allocate(std::size_t n);
63 void deallocate(T* p, std::size_t);
64
65 friend bool operator==(final_small_iter_allocator, final_small_iter_allocator) { return true; }
66 friend bool operator!=(final_small_iter_allocator, final_small_iter_allocator) { return false; }
67 };
68
69 template <class T, class Alloc>
70 using unordered_map_alloc = std::unordered_map<T, T, std::hash<T>, std::equal_to<T>, Alloc>;
71
72 #if __SIZE_WIDTH__ == 64
73
74 static_assert(sizeof(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 40, "");
75 static_assert(sizeof(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 40, "");
76 static_assert(sizeof(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 64, "");
77 static_assert(sizeof(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 12, "");
78 static_assert(sizeof(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 16, "");
79
80 static_assert(sizeof(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 40, "");
81 static_assert(sizeof(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 40, "");
82 static_assert(sizeof(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 64, "");
83 static_assert(sizeof(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 12, "");
84 static_assert(sizeof(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 16, "");
85
86 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 8, "");
87 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 8, "");
88 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 8, "");
89 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 4, "");
90 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 4, "");
91
92 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 8, "");
93 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 8, "");
94 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 8, "");
95 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 4, "");
96 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 4,
97 "");
98
99 struct TEST_ALIGNAS(32) AlignedHash {};
100 struct UnalignedEqualTo {};
101
102 // This part of the ABI has been broken between LLVM 19 and LLVM 20.
103 static_assert(sizeof(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 64, "");
104 static_assert(TEST_ALIGNOF(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 32, "");
105
106 #elif __SIZE_WIDTH__ == 32
107
108 static_assert(sizeof(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 20, "");
109 static_assert(sizeof(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 20, "");
110 static_assert(sizeof(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 44, "");
111 static_assert(sizeof(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 12, "");
112 static_assert(sizeof(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 16, "");
113
114 static_assert(sizeof(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 20, "");
115 static_assert(sizeof(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 20, "");
116 static_assert(sizeof(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 44, "");
117 static_assert(sizeof(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 12, "");
118 static_assert(sizeof(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 16, "");
119
120 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 4, "");
121 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 4, "");
122 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 4, "");
123 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 4, "");
124 static_assert(TEST_ALIGNOF(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 4, "");
125
126 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 4, "");
127 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 4, "");
128 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 4, "");
129 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 4, "");
130 static_assert(TEST_ALIGNOF(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 4,
131 "");
132
133 struct TEST_ALIGNAS(32) AlignedHash {};
134 struct UnalignedEqualTo {};
135
136 static_assert(sizeof(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 64);
137 static_assert(TEST_ALIGNOF(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 32);
138
139 #else
140 # error std::size_t has an unexpected size
141 #endif