Debian Codegraph @

libcxx/test/libcxx-03/containers/associative/unord.set/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_set>
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_set_alloc = std::unordered_set<T, std::hash<T>, std::equal_to<T>, Alloc>;
71
72 #if __SIZE_WIDTH__ == 64
73
74 static_assert(sizeof(unordered_set_alloc<int, std::allocator<int> >) == 40, "");
75 static_assert(sizeof(unordered_set_alloc<int, min_allocator<int> >) == 40, "");
76 static_assert(sizeof(unordered_set_alloc<int, test_allocator<int> >) == 64, "");
77 static_assert(sizeof(unordered_set_alloc<int, small_iter_allocator<int> >) == 12, "");
78 static_assert(sizeof(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 16, "");
79
80 static_assert(sizeof(unordered_set_alloc<char, std::allocator<char> >) == 40, "");
81 static_assert(sizeof(unordered_set_alloc<char, min_allocator<char> >) == 40, "");
82 static_assert(sizeof(unordered_set_alloc<char, test_allocator<char> >) == 64, "");
83 static_assert(sizeof(unordered_set_alloc<char, small_iter_allocator<char> >) == 12, "");
84 static_assert(sizeof(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 16, "");
85
86 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, std::allocator<int> >) == 8, "");
87 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, min_allocator<int> >) == 8, "");
88 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, test_allocator<int> >) == 8, "");
89 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, small_iter_allocator<int> >) == 4, "");
90 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 4, "");
91
92 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, std::allocator<char> >) == 8, "");
93 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, min_allocator<char> >) == 8, "");
94 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, test_allocator<char> >) == 8, "");
95 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, small_iter_allocator<char> >) == 4, "");
96 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 4, "");
97
98 struct TEST_ALIGNAS(32) AlignedHash {};
99 struct UnalignedEqualTo {};
100
101 // This part of the ABI has been broken between LLVM 19 and LLVM 20.
102 static_assert(sizeof(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 64, "");
103 static_assert(TEST_ALIGNOF(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 32, "");
104
105 #elif __SIZE_WIDTH__ == 32
106
107 static_assert(sizeof(unordered_set_alloc<int, std::allocator<int> >) == 20, "");
108 static_assert(sizeof(unordered_set_alloc<int, min_allocator<int> >) == 20, "");
109 static_assert(sizeof(unordered_set_alloc<int, test_allocator<int> >) == 44, "");
110 static_assert(sizeof(unordered_set_alloc<int, small_iter_allocator<int> >) == 12, "");
111 static_assert(sizeof(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 16, "");
112
113 static_assert(sizeof(unordered_set_alloc<char, std::allocator<char> >) == 20, "");
114 static_assert(sizeof(unordered_set_alloc<char, min_allocator<char> >) == 20, "");
115 static_assert(sizeof(unordered_set_alloc<char, test_allocator<char> >) == 44, "");
116 static_assert(sizeof(unordered_set_alloc<char, small_iter_allocator<char> >) == 12, "");
117 static_assert(sizeof(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 16, "");
118
119 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, std::allocator<int> >) == 4, "");
120 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, min_allocator<int> >) == 4, "");
121 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, test_allocator<int> >) == 4, "");
122 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, small_iter_allocator<int> >) == 4, "");
123 static_assert(TEST_ALIGNOF(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 4, "");
124
125 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, std::allocator<char> >) == 4, "");
126 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, min_allocator<char> >) == 4, "");
127 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, test_allocator<char> >) == 4, "");
128 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, small_iter_allocator<char> >) == 4, "");
129 static_assert(TEST_ALIGNOF(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 4, "");
130
131 struct TEST_ALIGNAS(32) AlignedHash {};
132 struct UnalignedEqualTo {};
133
134 static_assert(sizeof(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 64);
135 static_assert(TEST_ALIGNOF(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 32);
136
137 #else
138 # error std::size_t has an unexpected size
139 #endif