diff --git a/llvm/include/llvm/Support/FileCollector.h b/llvm/include/llvm/Support/FileCollector.h --- a/llvm/include/llvm/Support/FileCollector.h +++ b/llvm/include/llvm/Support/FileCollector.h @@ -20,6 +20,35 @@ class FileCollectorFileSystem; class Twine; +class FileCollectorBase { +public: + FileCollectorBase(); + virtual ~FileCollectorBase(); + + void addFile(const Twine &file); + void addDirectory(const Twine &Dir); + +protected: + bool markAsSeen(StringRef Path) { + if (Path.empty()) + return false; + return Seen.insert(Path).second; + } + + virtual void addFileImpl(StringRef SrcPath) = 0; + + virtual llvm::vfs::directory_iterator + addDirectoryImpl(const llvm::Twine &Dir, + IntrusiveRefCntPtr FS, + std::error_code &EC) = 0; + + /// Synchronizes access to internal data structures. + std::mutex Mutex; + + /// Tracks already seen files so they can be skipped. + StringSet<> Seen; +}; + /// Captures file system interaction and generates data to be later replayed /// with the RedirectingFileSystem. /// @@ -38,16 +67,13 @@ /// /// In order to preserve the relative topology of files we use their real paths /// as relative paths inside of the Root. -class FileCollector { +class FileCollector : public FileCollectorBase { public: /// \p Root is the directory where collected files are will be stored. /// \p OverlayRoot is VFS mapping root. /// \p Root directory gets created in copyFiles unless it already exists. FileCollector(std::string Root, std::string OverlayRoot); - void addFile(const Twine &file); - void addDirectory(const Twine &Dir); - /// Write the yaml mapping (for the VFS) to the given file. std::error_code writeMapping(StringRef MappingFile); @@ -67,12 +93,6 @@ private: friend FileCollectorFileSystem; - bool markAsSeen(StringRef Path) { - if (Path.empty()) - return false; - return Seen.insert(Path).second; - } - bool getRealPath(StringRef SrcPath, SmallVectorImpl &Result); void addFileToMapping(StringRef VirtualPath, StringRef RealPath) { @@ -83,14 +103,12 @@ } protected: - void addFileImpl(StringRef SrcPath); + void addFileImpl(StringRef SrcPath) override; llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, - IntrusiveRefCntPtr FS, std::error_code &EC); - - /// Synchronizes access to Seen, VFSWriter and SymlinkMap. - std::mutex Mutex; + IntrusiveRefCntPtr FS, + std::error_code &EC) override; /// The directory where collected files are copied to in copyFiles(). const std::string Root; @@ -98,9 +116,6 @@ /// The root directory where the VFS overlay lives. const std::string OverlayRoot; - /// Tracks already seen files so they can be skipped. - StringSet<> Seen; - /// The yaml mapping writer. vfs::YAMLVFSWriter VFSWriter; diff --git a/llvm/lib/Support/FileCollector.cpp b/llvm/lib/Support/FileCollector.cpp --- a/llvm/lib/Support/FileCollector.cpp +++ b/llvm/lib/Support/FileCollector.cpp @@ -15,6 +15,22 @@ using namespace llvm; +FileCollectorBase::FileCollectorBase() = default; +FileCollectorBase::~FileCollectorBase() = default; + +void FileCollectorBase::addFile(const Twine &File) { + std::lock_guard lock(Mutex); + std::string FileStr = File.str(); + if (markAsSeen(FileStr)) + addFileImpl(FileStr); +} + +void FileCollectorBase::addDirectory(const Twine &Dir) { + assert(sys::fs::is_directory(Dir)); + std::error_code EC; + addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC); +} + static bool isCaseSensitivePath(StringRef Path) { SmallString<256> TmpDest = Path, UpperDest, RealDest; @@ -61,19 +77,6 @@ return true; } -void FileCollector::addFile(const Twine &File) { - std::lock_guard lock(Mutex); - std::string FileStr = File.str(); - if (markAsSeen(FileStr)) - addFileImpl(FileStr); -} - -void FileCollector::addDirectory(const Twine &Dir) { - assert(sys::fs::is_directory(Dir)); - std::error_code EC; - addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC); -} - void FileCollector::addFileImpl(StringRef SrcPath) { // We need an absolute src path to append to the root. SmallString<256> AbsoluteSrc = SrcPath;