|
| 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-abi-no-compressed-pair-padding |
| 10 | + |
| 11 | +// XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 12 | + |
| 13 | +#include <cstdint> |
| 14 | +#include <map> |
| 15 | + |
| 16 | +#include "min_allocator.h" |
| 17 | +#include "test_allocator.h" |
| 18 | +#include "test_macros.h" |
| 19 | + |
| 20 | +template <class T> |
| 21 | +class small_pointer { |
| 22 | + std::uint16_t offset; |
| 23 | +}; |
| 24 | + |
| 25 | +template <class T> |
| 26 | +class small_iter_allocator { |
| 27 | +public: |
| 28 | + using value_type = T; |
| 29 | + using pointer = small_pointer<T>; |
| 30 | + using size_type = std::uint16_t; |
| 31 | + using difference_type = std::int16_t; |
| 32 | + |
| 33 | + small_iter_allocator() TEST_NOEXCEPT {} |
| 34 | + |
| 35 | + template <class U> |
| 36 | + small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {} |
| 37 | + |
| 38 | + T* allocate(std::size_t n); |
| 39 | + void deallocate(T* p, std::size_t); |
| 40 | + |
| 41 | + friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; } |
| 42 | + friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; } |
| 43 | +}; |
| 44 | + |
| 45 | +template <class T> |
| 46 | +class final_small_iter_allocator final { |
| 47 | +public: |
| 48 | + using value_type = T; |
| 49 | + using pointer = small_pointer<T>; |
| 50 | + using size_type = std::uint16_t; |
| 51 | + using difference_type = std::int16_t; |
| 52 | + |
| 53 | + final_small_iter_allocator() TEST_NOEXCEPT {} |
| 54 | + |
| 55 | + template <class U> |
| 56 | + final_small_iter_allocator(final_small_iter_allocator<U>) TEST_NOEXCEPT {} |
| 57 | + |
| 58 | + T* allocate(std::size_t n); |
| 59 | + void deallocate(T* p, std::size_t); |
| 60 | + |
| 61 | + friend bool operator==(final_small_iter_allocator, final_small_iter_allocator) { return true; } |
| 62 | + friend bool operator!=(final_small_iter_allocator, final_small_iter_allocator) { return false; } |
| 63 | +}; |
| 64 | + |
| 65 | +struct allocator_base {}; |
| 66 | + |
| 67 | +// Make sure that types with a common base type don't get broken. See https://llvm.org/PR154146 |
| 68 | +template <class T> |
| 69 | +struct common_base_allocator : allocator_base { |
| 70 | + using value_type = T; |
| 71 | + |
| 72 | + common_base_allocator() TEST_NOEXCEPT {} |
| 73 | + |
| 74 | + template <class U> |
| 75 | + common_base_allocator(common_base_allocator<U>) TEST_NOEXCEPT {} |
| 76 | + |
| 77 | + T* allocate(std::size_t n); |
| 78 | + void deallocate(T* p, std::size_t); |
| 79 | + |
| 80 | + friend bool operator==(common_base_allocator, common_base_allocator) { return true; } |
| 81 | + friend bool operator!=(common_base_allocator, common_base_allocator) { return false; } |
| 82 | +}; |
| 83 | + |
| 84 | +template <class T, class Alloc> |
| 85 | +using map_alloc = std::map<T, T, std::less<T>, Alloc>; |
| 86 | + |
| 87 | +struct user_struct { |
| 88 | + map_alloc<int, common_base_allocator<std::pair<const int, int> > > v; |
| 89 | + [[no_unique_address]] common_base_allocator<int> a; |
| 90 | +}; |
| 91 | + |
| 92 | +#if __SIZE_WIDTH__ == 64 |
| 93 | +static_assert(sizeof(user_struct) == 32, ""); |
| 94 | +static_assert(TEST_ALIGNOF(user_struct) == 8, ""); |
| 95 | + |
| 96 | +static_assert(sizeof(map_alloc<int, std::allocator<std::pair<const int, int> > >) == 24, ""); |
| 97 | +static_assert(sizeof(map_alloc<int, min_allocator<std::pair<const int, int> > >) == 24, ""); |
| 98 | +static_assert(sizeof(map_alloc<int, test_allocator<std::pair<const int, int> > >) == 40, ""); |
| 99 | +static_assert(sizeof(map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 6, ""); |
| 100 | +static_assert(sizeof(map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 8, ""); |
| 101 | + |
| 102 | +static_assert(sizeof(map_alloc<char, std::allocator<std::pair<const char, char> > >) == 24, ""); |
| 103 | +static_assert(sizeof(map_alloc<char, min_allocator<std::pair<const char, char> > >) == 24, ""); |
| 104 | +static_assert(sizeof(map_alloc<char, test_allocator<std::pair<const char, char> > >) == 40, ""); |
| 105 | +static_assert(sizeof(map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 6, ""); |
| 106 | +static_assert(sizeof(map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 8, ""); |
| 107 | + |
| 108 | +static_assert(TEST_ALIGNOF(map_alloc<int, std::allocator<std::pair<const int, int> > >) == 8, ""); |
| 109 | +static_assert(TEST_ALIGNOF(map_alloc<int, min_allocator<std::pair<const int, int> > >) == 8, ""); |
| 110 | +static_assert(TEST_ALIGNOF(map_alloc<int, test_allocator<std::pair<const int, int> > >) == 8, ""); |
| 111 | +static_assert(TEST_ALIGNOF(map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 2, ""); |
| 112 | +static_assert(TEST_ALIGNOF(map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 2, ""); |
| 113 | + |
| 114 | +static_assert(TEST_ALIGNOF(map_alloc<char, std::allocator<std::pair<const char, char> > >) == 8, ""); |
| 115 | +static_assert(TEST_ALIGNOF(map_alloc<char, min_allocator<std::pair<const char, char> > >) == 8, ""); |
| 116 | +static_assert(TEST_ALIGNOF(map_alloc<char, test_allocator<std::pair<const char, char> > >) == 8, ""); |
| 117 | +static_assert(TEST_ALIGNOF(map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 2, ""); |
| 118 | +static_assert(TEST_ALIGNOF(map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 2, ""); |
| 119 | + |
| 120 | +struct TEST_ALIGNAS(32) AlignedLess {}; |
| 121 | + |
| 122 | +// This part of the ABI has been broken between LLVM 19 and LLVM 20. |
| 123 | +static_assert(sizeof(std::map<int, int, AlignedLess>) == 64, ""); |
| 124 | +static_assert(TEST_ALIGNOF(std::map<int, int, AlignedLess>) == 32, ""); |
| 125 | + |
| 126 | +#elif __SIZE_WIDTH__ == 32 |
| 127 | +static_assert(sizeof(user_struct) == 16, ""); |
| 128 | +static_assert(TEST_ALIGNOF(user_struct) == 4, ""); |
| 129 | + |
| 130 | +static_assert(sizeof(map_alloc<int, std::allocator<std::pair<const int, int> > >) == 12, ""); |
| 131 | +static_assert(sizeof(map_alloc<int, min_allocator<std::pair<const int, int> > >) == 12, ""); |
| 132 | +static_assert(sizeof(map_alloc<int, test_allocator<std::pair<const int, int> > >) == 24, ""); |
| 133 | +static_assert(sizeof(map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 6, ""); |
| 134 | +static_assert(sizeof(map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 8, ""); |
| 135 | + |
| 136 | +static_assert(sizeof(map_alloc<char, std::allocator<std::pair<const char, char> > >) == 12, ""); |
| 137 | +static_assert(sizeof(map_alloc<char, min_allocator<std::pair<const char, char> > >) == 12, ""); |
| 138 | +static_assert(sizeof(map_alloc<char, test_allocator<std::pair<const char, char> > >) == 24, ""); |
| 139 | +static_assert(sizeof(map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 6, ""); |
| 140 | +static_assert(sizeof(map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 8, ""); |
| 141 | + |
| 142 | +static_assert(TEST_ALIGNOF(map_alloc<int, std::allocator<std::pair<const int, int> > >) == 4, ""); |
| 143 | +static_assert(TEST_ALIGNOF(map_alloc<int, min_allocator<std::pair<const int, int> > >) == 4, ""); |
| 144 | +static_assert(TEST_ALIGNOF(map_alloc<int, test_allocator<std::pair<const int, int> > >) == 4, ""); |
| 145 | +static_assert(TEST_ALIGNOF(map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 2, ""); |
| 146 | +static_assert(TEST_ALIGNOF(map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 2, ""); |
| 147 | + |
| 148 | +static_assert(TEST_ALIGNOF(map_alloc<char, std::allocator<std::pair<const char, char> > >) == 4, ""); |
| 149 | +static_assert(TEST_ALIGNOF(map_alloc<char, min_allocator<std::pair<const char, char> > >) == 4, ""); |
| 150 | +static_assert(TEST_ALIGNOF(map_alloc<char, test_allocator<std::pair<const char, char> > >) == 4, ""); |
| 151 | +static_assert(TEST_ALIGNOF(map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 2, ""); |
| 152 | +static_assert(TEST_ALIGNOF(map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 2, ""); |
| 153 | + |
| 154 | +struct TEST_ALIGNAS(32) AlignedLess {}; |
| 155 | + |
| 156 | +static_assert(sizeof(std::map<int, int, AlignedLess>) == 64); |
| 157 | +static_assert(TEST_ALIGNOF(std::map<int, int, AlignedLess>) == 32); |
| 158 | + |
| 159 | +#else |
| 160 | +# error std::size_t has an unexpected size |
| 161 | +#endif |
0 commit comments