From 96b44c77f8fbc0c704c036b82ea173289b93aa72 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Mon, 29 Jul 2019 20:54:02 +0000 Subject: [PATCH] [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. llvm-svn: 367258 --- lldb/include/lldb/Host/FileSystem.h | 8 ++++---- lldb/include/lldb/Utility/Reproducer.h | 15 +++++++++------ lldb/source/Host/common/FileSystem.cpp | 2 +- .../ExpressionParser/Clang/ModuleDependencyCollector.h | 8 +++++--- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lldb/include/lldb/Host/FileSystem.h b/lldb/include/lldb/Host/FileSystem.h index a5f2e24..733a4a4 100644 --- a/lldb/include/lldb/Host/FileSystem.h +++ b/lldb/include/lldb/Host/FileSystem.h @@ -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 collector) + : m_fs(llvm::vfs::getRealFileSystem()), m_collector(collector), m_mapped(false) {} FileSystem(llvm::IntrusiveRefCntPtr 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 collector); static llvm::Error Initialize(const FileSpec &mapping); static void Initialize(llvm::IntrusiveRefCntPtr fs); static void Terminate(); @@ -188,7 +188,7 @@ public: private: static llvm::Optional &InstanceImpl(); llvm::IntrusiveRefCntPtr m_fs; - llvm::FileCollector *m_collector; + std::shared_ptr m_collector; bool m_mapped; }; } // namespace lldb_private diff --git a/lldb/include/lldb/Utility/Reproducer.h b/lldb/include/lldb/Utility/Reproducer.h index ecd6b6f..37f4720 100644 --- a/lldb/include/lldb/Utility/Reproducer.h +++ b/lldb/include/lldb/Utility/Reproducer.h @@ -91,23 +91,26 @@ public: FileProvider(const FileSpec &directory) : Provider(directory), - m_collector(directory.CopyByAppendingPathComponent("root").GetPath(), - directory.GetPath()) {} + m_collector(std::make_shared( + directory.CopyByAppendingPathComponent("root").GetPath(), + directory.GetPath())) {} - llvm::FileCollector &GetFileCollector() { return m_collector; } + std::shared_ptr 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 m_collector; }; /// Provider for the LLDB version number. diff --git a/lldb/source/Host/common/FileSystem.cpp b/lldb/source/Host/common/FileSystem.cpp index 3b4db43..0d94171 100644 --- a/lldb/source/Host/common/FileSystem.cpp +++ b/lldb/source/Host/common/FileSystem.cpp @@ -49,7 +49,7 @@ void FileSystem::Initialize() { InstanceImpl().emplace(); } -void FileSystem::Initialize(FileCollector &collector) { +void FileSystem::Initialize(std::shared_ptr collector) { lldbassert(!InstanceImpl() && "Already initialized."); InstanceImpl().emplace(collector); } diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h b/lldb/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h index 9660abf..7553860 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h +++ b/lldb/source/Plugins/ExpressionParser/Clang/ModuleDependencyCollector.h @@ -17,13 +17,15 @@ namespace lldb_private { class ModuleDependencyCollectorAdaptor : public clang::ModuleDependencyCollector { public: - ModuleDependencyCollectorAdaptor(llvm::FileCollector &file_collector) + ModuleDependencyCollectorAdaptor( + std::shared_ptr 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 m_file_collector; }; } // namespace lldb_private -- 2.7.4