From 7308ff63abc95d8ec2a0b7b54c169cb549c2529a Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Tue, 8 Dec 2020 14:54:55 -0800 Subject: [PATCH] ADT: Allow IntrusiveRefCntPtr construction from std::unique_ptr, NFC Allow a `std::unique_ptr` to be moved into the an `IntrusiveRefCntPtr`, and remove a couple of now-unnecessary `release()` calls. Differential Revision: https://reviews.llvm.org/D92888 --- include/llvm/ADT/IntrusiveRefCntPtr.h | 6 ++++++ unittests/ADT/IntrusiveRefCntPtrTest.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/llvm/ADT/IntrusiveRefCntPtr.h b/include/llvm/ADT/IntrusiveRefCntPtr.h index 173fad3aeaf..dcd35253c5f 100644 --- a/include/llvm/ADT/IntrusiveRefCntPtr.h +++ b/include/llvm/ADT/IntrusiveRefCntPtr.h @@ -58,6 +58,7 @@ #include #include #include +#include namespace llvm { @@ -175,6 +176,11 @@ public: S.Obj = nullptr; } + template + IntrusiveRefCntPtr(std::unique_ptr S) : Obj(S.release()) { + retain(); + } + template IntrusiveRefCntPtr(const IntrusiveRefCntPtr &S) : Obj(S.get()) { retain(); diff --git a/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/unittests/ADT/IntrusiveRefCntPtrTest.cpp index 3d8041fcbf4..f69239162e3 100644 --- a/unittests/ADT/IntrusiveRefCntPtrTest.cpp +++ b/unittests/ADT/IntrusiveRefCntPtrTest.cpp @@ -42,6 +42,17 @@ TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) { EXPECT_EQ(0, NumInstances); } +TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) { + EXPECT_EQ(0, NumInstances); + { + auto S1 = std::make_unique(); + IntrusiveRefCntPtr R1 = std::move(S1); + EXPECT_EQ(1, NumInstances); + EXPECT_EQ(S1, nullptr); + } + EXPECT_EQ(0, NumInstances); +} + struct InterceptRefCounted : public RefCountedBase { InterceptRefCounted(bool *Released, bool *Retained) : Released(Released), Retained(Retained) {}