[Lldb-commits] [lldb] r367258 - [Reproducers] Pass FileCollector around as a shared_ptr (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon Jul 29 13:54:02 PDT 2019


Author: jdevlieghere
Date: Mon Jul 29 13:54:02 2019
New Revision: 367258

URL: http://llvm.org/viewvc/llvm-project?rev=367258&view=rev
Log:
[Reproducers] Pass FileCollector around as a shared_ptr (NFC)

Instead of passing the FileCollector around as a reference or raw
pointer, use a shared_ptr. This change's motivation is twofold. First it
adds compatibility for the newly added `FileCollectorFileSystem`.
Secondly, it addresses a lifetime issue we only see when LLDB is used
from Xcode, where a reference to the FileCollector outlives the
reproducer instance.

Modified:
    lldb/trunk/include/lldb/Host/FileSystem.h
    lldb/trunk/include/lldb/Utility/Reproducer.h
    lldb/trunk/source/Host/common/FileSystem.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h

Modified: lldb/trunk/include/lldb/Host/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSystem.h?rev=367258&r1=367257&r2=367258&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSystem.h (original)
+++ lldb/trunk/include/lldb/Host/FileSystem.h Mon Jul 29 13:54:02 2019
@@ -34,8 +34,8 @@ public:
   FileSystem()
       : m_fs(llvm::vfs::getRealFileSystem()), m_collector(nullptr),
         m_mapped(false) {}
-  FileSystem(llvm::FileCollector &collector)
-      : m_fs(llvm::vfs::getRealFileSystem()), m_collector(&collector),
+  FileSystem(std::shared_ptr<llvm::FileCollector> collector)
+      : m_fs(llvm::vfs::getRealFileSystem()), m_collector(collector),
         m_mapped(false) {}
   FileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
              bool mapped = false)
@@ -47,7 +47,7 @@ public:
   static FileSystem &Instance();
 
   static void Initialize();
-  static void Initialize(llvm::FileCollector &collector);
+  static void Initialize(std::shared_ptr<llvm::FileCollector> collector);
   static llvm::Error Initialize(const FileSpec &mapping);
   static void Initialize(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs);
   static void Terminate();
@@ -188,7 +188,7 @@ public:
 private:
   static llvm::Optional<FileSystem> &InstanceImpl();
   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
-  llvm::FileCollector *m_collector;
+  std::shared_ptr<llvm::FileCollector> m_collector;
   bool m_mapped;
 };
 } // namespace lldb_private

Modified: lldb/trunk/include/lldb/Utility/Reproducer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Reproducer.h?rev=367258&r1=367257&r2=367258&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Reproducer.h (original)
+++ lldb/trunk/include/lldb/Utility/Reproducer.h Mon Jul 29 13:54:02 2019
@@ -91,23 +91,26 @@ public:
 
   FileProvider(const FileSpec &directory)
       : Provider(directory),
-        m_collector(directory.CopyByAppendingPathComponent("root").GetPath(),
-                    directory.GetPath()) {}
+        m_collector(std::make_shared<llvm::FileCollector>(
+            directory.CopyByAppendingPathComponent("root").GetPath(),
+            directory.GetPath())) {}
 
-  llvm::FileCollector &GetFileCollector() { return m_collector; }
+  std::shared_ptr<llvm::FileCollector> GetFileCollector() {
+    return m_collector;
+  }
 
   void Keep() override {
     auto mapping = GetRoot().CopyByAppendingPathComponent(Info::file);
     // Temporary files that are removed during execution can cause copy errors.
-    if (auto ec = m_collector.copyFiles(/*stop_on_error=*/false))
+    if (auto ec = m_collector->copyFiles(/*stop_on_error=*/false))
       return;
-    m_collector.writeMapping(mapping.GetPath());
+    m_collector->writeMapping(mapping.GetPath());
   }
 
   static char ID;
 
 private:
-  llvm::FileCollector m_collector;
+  std::shared_ptr<llvm::FileCollector> m_collector;
 };
 
 /// Provider for the LLDB version number.

Modified: lldb/trunk/source/Host/common/FileSystem.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSystem.cpp?rev=367258&r1=367257&r2=367258&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSystem.cpp (original)
+++ lldb/trunk/source/Host/common/FileSystem.cpp Mon Jul 29 13:54:02 2019
@@ -49,7 +49,7 @@ void FileSystem::Initialize() {
   InstanceImpl().emplace();
 }
 
-void FileSystem::Initialize(FileCollector &collector) {
+void FileSystem::Initialize(std::shared_ptr<FileCollector> collector) {
   lldbassert(!InstanceImpl() && "Already initialized.");
   InstanceImpl().emplace(collector);
 }

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h?rev=367258&r1=367257&r2=367258&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h Mon Jul 29 13:54:02 2019
@@ -17,13 +17,15 @@ namespace lldb_private {
 class ModuleDependencyCollectorAdaptor
     : public clang::ModuleDependencyCollector {
 public:
-  ModuleDependencyCollectorAdaptor(llvm::FileCollector &file_collector)
+  ModuleDependencyCollectorAdaptor(
+      std::shared_ptr<llvm::FileCollector> file_collector)
       : clang::ModuleDependencyCollector(""), m_file_collector(file_collector) {
   }
 
   void addFile(llvm::StringRef Filename,
                llvm::StringRef FileDst = {}) override {
-    m_file_collector.addFile(Filename);
+    if (m_file_collector)
+      m_file_collector->addFile(Filename);
   }
 
   bool insertSeen(llvm::StringRef Filename) override { return false; }
@@ -31,7 +33,7 @@ public:
   void writeFileMap() override {}
 
 private:
-  llvm::FileCollector &m_file_collector;
+  std::shared_ptr<llvm::FileCollector> m_file_collector;
 };
 } // namespace lldb_private
 




More information about the lldb-commits mailing list