From 9e38f4d97347fcee7fb461d2ba6ac6882d34d17f Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 29 Jul 2019 23:38:30 +0000 Subject: [FileCollector] Add a VFS that records FS accesses using the FileCollector This patch adds a VFS that can be overlaid on top of another VFS to record file system accesses using the FileCollector. This can help to gather files that are needed for reproducers. Differential Revision: https://reviews.llvm.org/D65411 llvm-svn: 367278 --- llvm/lib/Support/FileCollector.cpp | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'llvm/lib/Support/FileCollector.cpp') diff --git a/llvm/lib/Support/FileCollector.cpp b/llvm/lib/Support/FileCollector.cpp index 7e14282af08..556a3c03db5 100644 --- a/llvm/lib/Support/FileCollector.cpp +++ b/llvm/lib/Support/FileCollector.cpp @@ -187,3 +187,82 @@ std::error_code FileCollector::writeMapping(StringRef mapping_file) { return {}; } + +namespace { + +class FileCollectorFileSystem : public vfs::FileSystem { +public: + explicit FileCollectorFileSystem(IntrusiveRefCntPtr FS, + std::shared_ptr Collector) + : FS(std::move(FS)), Collector(std::move(Collector)) {} + + llvm::ErrorOr status(const Twine &Path) override { + auto Result = FS->status(Path); + if (Result && Result->exists()) + Collector->addFile(Path); + return Result; + } + + llvm::ErrorOr> + openFileForRead(const Twine &Path) override { + auto Result = FS->openFileForRead(Path); + if (Result && *Result) + Collector->addFile(Path); + return Result; + } + + llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir, + std::error_code &EC) override { + auto It = FS->dir_begin(Dir, EC); + if (EC) + return It; + // Collect everything that's listed in case the user needs it. + Collector->addFile(Dir); + for (; !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) { + if (It->type() == sys::fs::file_type::regular_file || + It->type() == sys::fs::file_type::directory_file || + It->type() == sys::fs::file_type::symlink_file) { + Collector->addFile(It->path()); + } + } + if (EC) + return It; + // Return a new iterator. + return FS->dir_begin(Dir, EC); + } + + std::error_code getRealPath(const Twine &Path, + SmallVectorImpl &Output) const override { + auto EC = FS->getRealPath(Path, Output); + if (!EC) { + Collector->addFile(Path); + if (Output.size() > 0) + Collector->addFile(Output); + } + return EC; + } + + std::error_code isLocal(const Twine &Path, bool &Result) override { + return FS->isLocal(Path, Result); + } + + llvm::ErrorOr getCurrentWorkingDirectory() const override { + return FS->getCurrentWorkingDirectory(); + } + + std::error_code setCurrentWorkingDirectory(const llvm::Twine &Path) override { + return FS->setCurrentWorkingDirectory(Path); + } + +private: + IntrusiveRefCntPtr FS; + std::shared_ptr Collector; +}; + +} // end anonymous namespace + +IntrusiveRefCntPtr +FileCollector::createCollectorVFS(IntrusiveRefCntPtr BaseFS, + std::shared_ptr Collector) { + return new FileCollectorFileSystem(std::move(BaseFS), std::move(Collector)); +} -- cgit v1.2.3