Author: David Blaikie
Date: 2020-12-03T17:42:32-08:00
New Revision: 2e83ccc2ee333389110659f3cb313968a0c970d4

URL: 
https://github.com/llvm/llvm-project/commit/2e83ccc2ee333389110659f3cb313968a0c970d4
DIFF: 
https://github.com/llvm/llvm-project/commit/2e83ccc2ee333389110659f3cb313968a0c970d4.diff

LOG: ADT: Support copying of IntrusiveRefCntPtr objects

This was partially supported but untested for RefCountedBase (the
implicit copy assignment would've been problematic - so delete that) and
unsupported (would not have compiled, because std::atomic is
non-copyable) for ThreadSafeRefCountedBase (implement similar support
to RefCountedBase)

Fix the test that had a copy ctor for the derived object but called
RefCountBase's default ctor from that copy ctor - which meant it wasn't
actually testing RefCountBase's copy semantics.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
    llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h 
b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 6d97fe15db8b..42486d1616a8 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -73,6 +73,7 @@ template <class Derived> class RefCountedBase {
 public:
   RefCountedBase() = default;
   RefCountedBase(const RefCountedBase &) {}
+  RefCountedBase &operator=(const RefCountedBase &) = delete;
 
   void Retain() const { ++RefCount; }
 
@@ -89,6 +90,9 @@ template <class Derived> class ThreadSafeRefCountedBase {
 
 protected:
   ThreadSafeRefCountedBase() : RefCount(0) {}
+  ThreadSafeRefCountedBase(const ThreadSafeRefCountedBase &) {}
+  ThreadSafeRefCountedBase &
+  operator=(const ThreadSafeRefCountedBase &) = delete;
 
 public:
   void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }

diff  --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp 
b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
index c248b04ed5a2..3d8041fcbf48 100644
--- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -12,28 +12,34 @@
 namespace llvm {
 
 namespace {
-struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {
+int NumInstances = 0;
+template <template <typename> class Base>
+struct SimpleRefCounted : Base<SimpleRefCounted<Base>> {
   SimpleRefCounted() { ++NumInstances; }
-  SimpleRefCounted(const SimpleRefCounted &) : RefCountedBase() {
+  SimpleRefCounted(const SimpleRefCounted &RHS) : Base<SimpleRefCounted>(RHS) {
     ++NumInstances;
   }
   ~SimpleRefCounted() { --NumInstances; }
-
-  static int NumInstances;
 };
-int SimpleRefCounted::NumInstances = 0;
 } // anonymous namespace
 
-TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
-  EXPECT_EQ(0, SimpleRefCounted::NumInstances);
+template <typename T> struct IntrusiveRefCntPtrTest : testing::Test {};
+
+typedef ::testing::Types<SimpleRefCounted<RefCountedBase>,
+                         SimpleRefCounted<ThreadSafeRefCountedBase>>
+    IntrusiveRefCntTypes;
+TYPED_TEST_CASE(IntrusiveRefCntPtrTest, IntrusiveRefCntTypes);
+
+TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) {
+  EXPECT_EQ(0, NumInstances);
   {
-    SimpleRefCounted *S1 = new SimpleRefCounted;
-    IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
-    SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
-    IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
-    EXPECT_EQ(2, SimpleRefCounted::NumInstances);
+    TypeParam *S1 = new TypeParam;
+    IntrusiveRefCntPtr<TypeParam> R1 = S1;
+    TypeParam *S2 = new TypeParam(*S1);
+    IntrusiveRefCntPtr<TypeParam> R2 = S2;
+    EXPECT_EQ(2, NumInstances);
   }
-  EXPECT_EQ(0, SimpleRefCounted::NumInstances);
+  EXPECT_EQ(0, NumInstances);
 }
 
 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {


        
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to