| 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 | // <vector> |
| 10 | |
| 11 | // Make sure we don't miscompile vector operations for types that shouldn't be considered |
| 12 | // trivially relocatable. |
| 13 | |
| 14 | #include <vector> |
| 15 | #include <cassert> |
| 16 | #include <cstddef> |
| 17 | #include <type_traits> |
| 18 | #include <utility> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | struct Tracker { |
| 23 | std::size_t move_constructs = 0; |
| 24 | }; |
| 25 | |
| 26 | struct [[clang::trivial_abi]] Inner { |
| 27 | TEST_CONSTEXPR_CXX20 explicit Inner(Tracker* tracker) : tracker_(tracker) {} |
| 28 | TEST_CONSTEXPR_CXX20 Inner(const Inner& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; } |
| 29 | TEST_CONSTEXPR_CXX20 Inner(Inner&& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; } |
| 30 | Tracker* tracker_; |
| 31 | }; |
| 32 | |
| 33 | // Even though this type contains a trivial_abi type, it is not trivially move-constructible, |
| 34 | // so we should not attempt to optimize its move construction + destroy using trivial relocation. |
| 35 | struct NotTriviallyMovable { |
| 36 | TEST_CONSTEXPR_CXX20 explicit NotTriviallyMovable(Tracker* tracker) : inner_(tracker) {} |
| 37 | TEST_CONSTEXPR_CXX20 NotTriviallyMovable(NotTriviallyMovable&& other) : inner_(std::move(other.inner_)) {} |
| 38 | Inner inner_; |
| 39 | }; |
| 40 | static_assert(!std::is_trivially_copyable<NotTriviallyMovable>::value, "" ); |
| 41 | LIBCPP_STATIC_ASSERT(!std::__libcpp_is_trivially_relocatable<NotTriviallyMovable>::value, "" ); |
| 42 | |
| 43 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 44 | Tracker track; |
| 45 | std::vector<NotTriviallyMovable> v; |
| 46 | |
| 47 | // Fill the vector at its capacity, such that any subsequent push_back would require growing. |
| 48 | v.reserve(n: 5); |
| 49 | std::size_t const capacity = v.capacity(); // could technically be more than 5 |
| 50 | while (v.size() < v.capacity()) { |
| 51 | v.emplace_back(args: &track); |
| 52 | } |
| 53 | assert(track.move_constructs == 0); |
| 54 | assert(v.capacity() == capacity); |
| 55 | assert(v.size() == capacity); |
| 56 | |
| 57 | // Force a reallocation of the buffer + relocalization of the elements. |
| 58 | // All the existing elements of the vector should be move-constructed to their new location. |
| 59 | v.emplace_back(args: &track); |
| 60 | assert(track.move_constructs == capacity); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | int main(int, char**) { |
| 66 | tests(); |
| 67 | #if TEST_STD_VER >= 20 |
| 68 | static_assert(tests()); |
| 69 | #endif |
| 70 | return 0; |
| 71 | } |
| 72 | |