| 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: sanitizer-new-delete |
| 10 | |
| 11 | // <memory> |
| 12 | |
| 13 | // template <class Y, class D> shared_ptr(unique_ptr<Y, D>&&r); |
| 14 | |
| 15 | #include <memory> |
| 16 | #include <new> |
| 17 | #include <cstdlib> |
| 18 | #include <cassert> |
| 19 | #include <utility> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | #include "count_new.h" |
| 23 | |
| 24 | struct B |
| 25 | { |
| 26 | static int count; |
| 27 | |
| 28 | B() {++count;} |
| 29 | B(const B&) {++count;} |
| 30 | virtual ~B() {--count;} |
| 31 | }; |
| 32 | |
| 33 | int B::count = 0; |
| 34 | |
| 35 | struct A |
| 36 | : public B |
| 37 | { |
| 38 | static int count; |
| 39 | |
| 40 | A() {++count;} |
| 41 | A(const A& other) : B(other) {++count;} |
| 42 | ~A() {--count;} |
| 43 | }; |
| 44 | |
| 45 | int A::count = 0; |
| 46 | |
| 47 | void fn ( const std::shared_ptr<int> &) {} |
| 48 | void fn ( const std::shared_ptr<B> &) { assert (false); } |
| 49 | |
| 50 | template <typename T> |
| 51 | void assert_deleter ( T * ) { assert(false); } |
| 52 | |
| 53 | namespace adl { |
| 54 | struct D { |
| 55 | void operator()(int *) const {} |
| 56 | }; |
| 57 | void ref(D); |
| 58 | } |
| 59 | |
| 60 | template <class T> |
| 61 | struct StatefulDeleter { |
| 62 | int state = 0; |
| 63 | |
| 64 | StatefulDeleter(int val = 0) : state(val) {} |
| 65 | StatefulDeleter(StatefulDeleter const&) { assert(false); } |
| 66 | |
| 67 | void operator()(T* ptr) { |
| 68 | assert(state == 42); |
| 69 | delete ptr; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | template <class T> |
| 74 | struct StatefulArrayDeleter { |
| 75 | int state = 0; |
| 76 | |
| 77 | StatefulArrayDeleter(int val = 0) : state(val) {} |
| 78 | StatefulArrayDeleter(StatefulArrayDeleter const&) { assert(false); } |
| 79 | |
| 80 | void operator()(T* ptr) { |
| 81 | assert(state == 42); |
| 82 | delete []ptr; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | struct MovingDeleter { |
| 87 | explicit MovingDeleter(int *moves) : moves_(moves) {} |
| 88 | MovingDeleter(MovingDeleter&& rhs) : moves_(rhs.moves_) { *moves_ += 1; } |
| 89 | void operator()(int*) const {} |
| 90 | int *moves_; |
| 91 | }; |
| 92 | |
| 93 | // https://llvm.org/PR53368 |
| 94 | // Bogus unique_ptr-to-shared_ptr conversions should be forbidden |
| 95 | #if TEST_STD_VER >= 17 |
| 96 | static_assert( std::is_constructible<std::shared_ptr<A>, std::unique_ptr<A>&&>::value, "" ); |
| 97 | static_assert( std::is_constructible<std::shared_ptr<A[]>, std::unique_ptr<A[]>&&>::value, "" ); |
| 98 | static_assert(!std::is_constructible<std::shared_ptr<A>, std::unique_ptr<A[]>&&>::value, "" ); |
| 99 | static_assert(!std::is_constructible<std::shared_ptr<B[]>, std::unique_ptr<A[]>&&>::value, "" ); |
| 100 | static_assert(!std::is_constructible<std::shared_ptr<B>, std::unique_ptr<A[]>&&>::value, "" ); |
| 101 | #endif |
| 102 | |
| 103 | int main(int, char**) |
| 104 | { |
| 105 | { |
| 106 | std::unique_ptr<A> ptr(new A); |
| 107 | A* raw_ptr = ptr.get(); |
| 108 | std::shared_ptr<B> p(std::move(ptr)); |
| 109 | assert(A::count == 1); |
| 110 | assert(B::count == 1); |
| 111 | assert(p.use_count() == 1); |
| 112 | assert(p.get() == raw_ptr); |
| 113 | assert(ptr.get() == 0); |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | std::unique_ptr<A const> ptr(new A); |
| 118 | A const* raw_ptr = ptr.get(); |
| 119 | std::shared_ptr<B const> p(std::move(ptr)); |
| 120 | assert(A::count == 1); |
| 121 | assert(B::count == 1); |
| 122 | assert(p.use_count() == 1); |
| 123 | assert(p.get() == raw_ptr); |
| 124 | assert(ptr.get() == 0); |
| 125 | } |
| 126 | |
| 127 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 128 | assert(A::count == 0); |
| 129 | { |
| 130 | std::unique_ptr<A> ptr(new A); |
| 131 | A* raw_ptr = ptr.get(); |
| 132 | globalMemCounter.throw_after = 0; |
| 133 | try |
| 134 | { |
| 135 | std::shared_ptr<B> p(std::move(ptr)); |
| 136 | assert(false); |
| 137 | } |
| 138 | catch (...) |
| 139 | { |
| 140 | assert(A::count == 1); |
| 141 | assert(B::count == 1); |
| 142 | assert(ptr.get() == raw_ptr); |
| 143 | } |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #if TEST_STD_VER > 14 |
| 148 | { |
| 149 | std::unique_ptr<int> ptr; |
| 150 | std::shared_ptr<int> p(std::move(ptr)); |
| 151 | assert(p.get() == 0); |
| 152 | assert(p.use_count() == 0); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | { |
| 157 | StatefulDeleter<A> d; |
| 158 | std::unique_ptr<A, StatefulDeleter<A>&> u(new A, d); |
| 159 | std::shared_ptr<A> p(std::move(u)); |
| 160 | d.state = 42; |
| 161 | assert(A::count == 1); |
| 162 | } |
| 163 | assert(A::count == 0); |
| 164 | |
| 165 | { // LWG 2399 |
| 166 | fn(std::unique_ptr<int>(new int)); |
| 167 | } |
| 168 | #if TEST_STD_VER >= 14 |
| 169 | { // LWG 2415 |
| 170 | std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>); |
| 171 | std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | { |
| 176 | adl::D d; |
| 177 | std::unique_ptr<int, adl::D&> u(nullptr, d); |
| 178 | std::shared_ptr<int> s = std::move(u); |
| 179 | } |
| 180 | |
| 181 | assert(A::count == 0); |
| 182 | |
| 183 | #if TEST_STD_VER > 14 |
| 184 | { |
| 185 | StatefulArrayDeleter<A> d; |
| 186 | std::unique_ptr<A[], StatefulArrayDeleter<A>&> u(new A[4], d); |
| 187 | std::shared_ptr<A[]> p(std::move(u)); |
| 188 | d.state = 42; |
| 189 | assert(A::count == 4); |
| 190 | } |
| 191 | assert(A::count == 0); |
| 192 | assert(B::count == 0); |
| 193 | |
| 194 | { |
| 195 | std::unique_ptr<A[]> ptr(new A[8]); |
| 196 | A* raw_ptr = ptr.get(); |
| 197 | std::shared_ptr<A[]> p(std::move(ptr)); |
| 198 | assert(A::count == 8); |
| 199 | assert(p.use_count() == 1); |
| 200 | assert(p.get() == raw_ptr); |
| 201 | assert(ptr.get() == 0); |
| 202 | } |
| 203 | assert(A::count == 0); |
| 204 | |
| 205 | { |
| 206 | int *p = new int[8]; |
| 207 | std::unique_ptr<int[]> u(p); |
| 208 | std::shared_ptr<int[]> s(std::move(u)); |
| 209 | assert(u == nullptr); |
| 210 | assert(s.get() == p); |
| 211 | } |
| 212 | #endif // TEST_STD_VER > 14 |
| 213 | |
| 214 | { // LWG 3548 |
| 215 | { |
| 216 | int moves = 0; |
| 217 | int i = 42; |
| 218 | std::unique_ptr<int, MovingDeleter> u(&i, MovingDeleter(&moves)); |
| 219 | assert(moves == 1); |
| 220 | std::shared_ptr<int> s(std::move(u)); |
| 221 | assert(moves >= 2); |
| 222 | assert(u == nullptr); |
| 223 | assert(s.get() == &i); |
| 224 | } |
| 225 | |
| 226 | #if TEST_STD_VER > 14 |
| 227 | { |
| 228 | int moves = 0; |
| 229 | int a[8]; |
| 230 | std::unique_ptr<int[], MovingDeleter> u(a, MovingDeleter(&moves)); |
| 231 | assert(moves == 1); |
| 232 | std::shared_ptr<int[]> s = std::move(u); |
| 233 | assert(moves >= 2); |
| 234 | assert(u == nullptr); |
| 235 | assert(s.get() == a); |
| 236 | } |
| 237 | #endif // TEST_STD_VER > 14 |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |