Skip to content

[FileCollector] Move interface into FileCollectorBase (NFC) #2041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions llvm/include/llvm/Support/FileCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ namespace llvm {
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<vfs::FileSystem> 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.
///
Expand All @@ -38,16 +67,13 @@ class Twine;
///
/// 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);

Expand All @@ -67,12 +93,6 @@ class FileCollector {
private:
friend FileCollectorFileSystem;

bool markAsSeen(StringRef Path) {
if (Path.empty())
return false;
return Seen.insert(Path).second;
}

bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);

void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
Expand All @@ -83,24 +103,19 @@ class FileCollector {
}

protected:
void addFileImpl(StringRef SrcPath);
void addFileImpl(StringRef SrcPath) override;

llvm::vfs::directory_iterator
addDirectoryImpl(const llvm::Twine &Dir,
IntrusiveRefCntPtr<vfs::FileSystem> FS, std::error_code &EC);

/// Synchronizes access to Seen, VFSWriter and SymlinkMap.
std::mutex Mutex;
IntrusiveRefCntPtr<vfs::FileSystem> FS,
std::error_code &EC) override;

/// The directory where collected files are copied to in copyFiles().
const std::string Root;

/// 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;

Expand Down
29 changes: 16 additions & 13 deletions llvm/lib/Support/FileCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@

using namespace llvm;

FileCollectorBase::FileCollectorBase() = default;
FileCollectorBase::~FileCollectorBase() = default;

void FileCollectorBase::addFile(const Twine &File) {
std::lock_guard<std::mutex> 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;

Expand Down Expand Up @@ -61,19 +77,6 @@ bool FileCollector::getRealPath(StringRef SrcPath,
return true;
}

void FileCollector::addFile(const Twine &File) {
std::lock_guard<std::mutex> 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;
Expand Down