Skip to content

Commit f44fb13

Browse files
committed
[FileCollector] Move interface into FileCollectorBase (NFC)
For the reproducers in LLDB we want to switch to an "immediate mode" FileCollector that writes every file encountered straight to disk so we can generate the actual mapping out-of-process. This patch moves the interface into a separate base class. Differential revision: https://reviews.llvm.org/D89742
1 parent a10a64e commit f44fb13

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

llvm/include/llvm/Support/FileCollector.h

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ namespace llvm {
2020
class FileCollectorFileSystem;
2121
class Twine;
2222

23+
class FileCollectorBase {
24+
public:
25+
FileCollectorBase();
26+
virtual ~FileCollectorBase();
27+
28+
void addFile(const Twine &file);
29+
void addDirectory(const Twine &Dir);
30+
31+
protected:
32+
bool markAsSeen(StringRef Path) {
33+
if (Path.empty())
34+
return false;
35+
return Seen.insert(Path).second;
36+
}
37+
38+
virtual void addFileImpl(StringRef SrcPath) = 0;
39+
40+
virtual llvm::vfs::directory_iterator
41+
addDirectoryImpl(const llvm::Twine &Dir,
42+
IntrusiveRefCntPtr<vfs::FileSystem> FS,
43+
std::error_code &EC) = 0;
44+
45+
/// Synchronizes access to internal data structures.
46+
std::mutex Mutex;
47+
48+
/// Tracks already seen files so they can be skipped.
49+
StringSet<> Seen;
50+
};
51+
2352
/// Captures file system interaction and generates data to be later replayed
2453
/// with the RedirectingFileSystem.
2554
///
@@ -38,16 +67,13 @@ class Twine;
3867
///
3968
/// In order to preserve the relative topology of files we use their real paths
4069
/// as relative paths inside of the Root.
41-
class FileCollector {
70+
class FileCollector : public FileCollectorBase {
4271
public:
4372
/// \p Root is the directory where collected files are will be stored.
4473
/// \p OverlayRoot is VFS mapping root.
4574
/// \p Root directory gets created in copyFiles unless it already exists.
4675
FileCollector(std::string Root, std::string OverlayRoot);
4776

48-
void addFile(const Twine &file);
49-
void addDirectory(const Twine &Dir);
50-
5177
/// Write the yaml mapping (for the VFS) to the given file.
5278
std::error_code writeMapping(StringRef MappingFile);
5379

@@ -67,12 +93,6 @@ class FileCollector {
6793
private:
6894
friend FileCollectorFileSystem;
6995

70-
bool markAsSeen(StringRef Path) {
71-
if (Path.empty())
72-
return false;
73-
return Seen.insert(Path).second;
74-
}
75-
7696
bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
7797

7898
void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
@@ -83,24 +103,19 @@ class FileCollector {
83103
}
84104

85105
protected:
86-
void addFileImpl(StringRef SrcPath);
106+
void addFileImpl(StringRef SrcPath) override;
87107

88108
llvm::vfs::directory_iterator
89109
addDirectoryImpl(const llvm::Twine &Dir,
90-
IntrusiveRefCntPtr<vfs::FileSystem> FS, std::error_code &EC);
91-
92-
/// Synchronizes access to Seen, VFSWriter and SymlinkMap.
93-
std::mutex Mutex;
110+
IntrusiveRefCntPtr<vfs::FileSystem> FS,
111+
std::error_code &EC) override;
94112

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

98116
/// The root directory where the VFS overlay lives.
99117
const std::string OverlayRoot;
100118

101-
/// Tracks already seen files so they can be skipped.
102-
StringSet<> Seen;
103-
104119
/// The yaml mapping writer.
105120
vfs::YAMLVFSWriter VFSWriter;
106121

llvm/lib/Support/FileCollector.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515

1616
using namespace llvm;
1717

18+
FileCollectorBase::FileCollectorBase() = default;
19+
FileCollectorBase::~FileCollectorBase() = default;
20+
21+
void FileCollectorBase::addFile(const Twine &File) {
22+
std::lock_guard<std::mutex> lock(Mutex);
23+
std::string FileStr = File.str();
24+
if (markAsSeen(FileStr))
25+
addFileImpl(FileStr);
26+
}
27+
28+
void FileCollectorBase::addDirectory(const Twine &Dir) {
29+
assert(sys::fs::is_directory(Dir));
30+
std::error_code EC;
31+
addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC);
32+
}
33+
1834
static bool isCaseSensitivePath(StringRef Path) {
1935
SmallString<256> TmpDest = Path, UpperDest, RealDest;
2036

@@ -61,19 +77,6 @@ bool FileCollector::getRealPath(StringRef SrcPath,
6177
return true;
6278
}
6379

64-
void FileCollector::addFile(const Twine &File) {
65-
std::lock_guard<std::mutex> lock(Mutex);
66-
std::string FileStr = File.str();
67-
if (markAsSeen(FileStr))
68-
addFileImpl(FileStr);
69-
}
70-
71-
void FileCollector::addDirectory(const Twine &Dir) {
72-
assert(sys::fs::is_directory(Dir));
73-
std::error_code EC;
74-
addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC);
75-
}
76-
7780
void FileCollector::addFileImpl(StringRef SrcPath) {
7881
// We need an absolute src path to append to the root.
7982
SmallString<256> AbsoluteSrc = SrcPath;

0 commit comments

Comments
 (0)