ADT: Allow IntrusiveRefCntPtr construction from std::unique_ptr, NFC
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 8 Dec 2020 22:54:55 +0000 (14:54 -0800)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Wed, 9 Dec 2020 01:33:19 +0000 (17:33 -0800)
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

clang-tools-extra/clangd/support/ThreadsafeFS.cpp
clang/lib/Tooling/AllTUsExecution.cpp
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp

index cadda8e..ae9450f 100644 (file)
@@ -87,8 +87,7 @@ RealThreadsafeFS::viewImpl() const {
   // Avoid using memory-mapped files.
   // FIXME: Try to use a similar approach in Sema instead of relying on
   //        propagation of the 'isVolatile' flag through all layers.
-  return new VolatileFileSystem(
-      llvm::vfs::createPhysicalFileSystem().release());
+  return new VolatileFileSystem(llvm::vfs::createPhysicalFileSystem());
 }
 } // namespace clangd
 } // namespace clang
index 7707c99..5565da9 100644 (file)
@@ -124,7 +124,7 @@ llvm::Error AllTUsToolExecutor::execute(
             // Each thread gets an indepent copy of a VFS to allow different
             // concurrent working directories.
             IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
-                llvm::vfs::createPhysicalFileSystem().release();
+                llvm::vfs::createPhysicalFileSystem();
             ClangTool Tool(Compilations, {Path},
                            std::make_shared<PCHContainerOperations>(), FS);
             Tool.appendArgumentsAdjuster(Action.second);
index f10b602..63264b0 100644 (file)
@@ -154,7 +154,7 @@ DependencyScanningWorker::DependencyScanningWorker(
     : Format(Service.getFormat()) {
   DiagOpts = new DiagnosticOptions();
   PCHContainerOps = std::make_shared<PCHContainerOperations>();
-  RealFS = llvm::vfs::createPhysicalFileSystem().release();
+  RealFS = llvm::vfs::createPhysicalFileSystem();
   if (Service.canSkipExcludedPPRanges())
     PPSkipMappings =
         std::make_unique<ExcludedPreprocessorDirectiveSkipMapping>();
index 173fad3..dcd3525 100644 (file)
@@ -58,6 +58,7 @@
 #include <atomic>
 #include <cassert>
 #include <cstddef>
+#include <memory>
 
 namespace llvm {
 
@@ -176,6 +177,11 @@ public:
   }
 
   template <class X>
+  IntrusiveRefCntPtr(std::unique_ptr<X> S) : Obj(S.release()) {
+    retain();
+  }
+
+  template <class X>
   IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) {
     retain();
   }
index 3d8041f..f692391 100644 (file)
@@ -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<TypeParam>();
+    IntrusiveRefCntPtr<TypeParam> R1 = std::move(S1);
+    EXPECT_EQ(1, NumInstances);
+    EXPECT_EQ(S1, nullptr);
+  }
+  EXPECT_EQ(0, NumInstances);
+}
+
 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
   InterceptRefCounted(bool *Released, bool *Retained)
     : Released(Released), Retained(Retained) {}