File indexing completed on 2026-05-10 08:44:30
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_SUPPORT_FILECOLLECTOR_H
0010 #define LLVM_SUPPORT_FILECOLLECTOR_H
0011
0012 #include "llvm/ADT/StringMap.h"
0013 #include "llvm/ADT/StringSet.h"
0014 #include "llvm/Support/VirtualFileSystem.h"
0015 #include <mutex>
0016 #include <string>
0017
0018 namespace llvm {
0019 class FileCollectorFileSystem;
0020 class Twine;
0021
0022 class FileCollectorBase {
0023 public:
0024 FileCollectorBase();
0025 virtual ~FileCollectorBase();
0026
0027 void addFile(const Twine &file);
0028 void addDirectory(const Twine &Dir);
0029
0030 protected:
0031 bool markAsSeen(StringRef Path) {
0032 if (Path.empty())
0033 return false;
0034 return Seen.insert(Path).second;
0035 }
0036
0037 virtual void addFileImpl(StringRef SrcPath) = 0;
0038
0039 virtual llvm::vfs::directory_iterator
0040 addDirectoryImpl(const llvm::Twine &Dir,
0041 IntrusiveRefCntPtr<vfs::FileSystem> FS,
0042 std::error_code &EC) = 0;
0043
0044
0045 std::mutex Mutex;
0046
0047
0048 StringSet<> Seen;
0049 };
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 class FileCollector : public FileCollectorBase {
0070 public:
0071
0072
0073 class PathCanonicalizer {
0074 public:
0075 struct PathStorage {
0076 SmallString<256> CopyFrom;
0077 SmallString<256> VirtualPath;
0078 };
0079
0080
0081 PathStorage canonicalize(StringRef SrcPath);
0082
0083 private:
0084
0085
0086
0087 void updateWithRealPath(SmallVectorImpl<char> &Path);
0088
0089 StringMap<std::string> CachedDirs;
0090 };
0091
0092
0093
0094
0095 FileCollector(std::string Root, std::string OverlayRoot);
0096
0097
0098 std::error_code writeMapping(StringRef MappingFile);
0099
0100
0101
0102
0103
0104
0105 std::error_code copyFiles(bool StopOnError = true);
0106
0107
0108
0109 static IntrusiveRefCntPtr<vfs::FileSystem>
0110 createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
0111 std::shared_ptr<FileCollector> Collector);
0112
0113 private:
0114 friend FileCollectorFileSystem;
0115
0116 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
0117 if (sys::fs::is_directory(VirtualPath))
0118 VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
0119 else
0120 VFSWriter.addFileMapping(VirtualPath, RealPath);
0121 }
0122
0123 protected:
0124 void addFileImpl(StringRef SrcPath) override;
0125
0126 llvm::vfs::directory_iterator
0127 addDirectoryImpl(const llvm::Twine &Dir,
0128 IntrusiveRefCntPtr<vfs::FileSystem> FS,
0129 std::error_code &EC) override;
0130
0131
0132 const std::string Root;
0133
0134
0135 const std::string OverlayRoot;
0136
0137
0138 vfs::YAMLVFSWriter VFSWriter;
0139
0140
0141 PathCanonicalizer Canonicalizer;
0142 };
0143
0144 }
0145
0146 #endif