LLVM 22.0.0git
FileCollector.h
Go to the documentation of this file.
1//===-- FileCollector.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_SUPPORT_FILECOLLECTOR_H
10#define LLVM_SUPPORT_FILECOLLECTOR_H
11
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringSet.h"
16#include <mutex>
17#include <string>
18
19namespace llvm {
21class Twine;
22
24public:
27
28 void addFile(const Twine &file);
29 void addDirectory(const Twine &Dir);
30
31protected:
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
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.
50};
51
52/// Captures file system interaction and generates data to be later replayed
53/// with the RedirectingFileSystem.
54///
55/// For any file that gets accessed we eventually create:
56/// - a copy of the file inside Root
57/// - a record in RedirectingFileSystem mapping that maps:
58/// current real path -> path to the copy in Root
59///
60/// That intent is that later when the mapping is used by RedirectingFileSystem
61/// it simulates the state of FS that we collected.
62///
63/// We generate file copies and mapping lazily - see writeMapping and copyFiles.
64/// We don't try to capture the state of the file at the exact time when it's
65/// accessed. Files might get changed, deleted ... we record only the "final"
66/// state.
67///
68/// In order to preserve the relative topology of files we use their real paths
69/// as relative paths inside of the Root.
71public:
72 /// Helper utility that encapsulates the logic for canonicalizing a virtual
73 /// path and a path to copy from.
75 public:
80
81 /// Canonicalize a pair of virtual and real paths.
83
86
87 private:
88 /// Replace with a (mostly) real path, or don't modify. Resolves symlinks
89 /// in the directory, using \a CachedDirs to avoid redundant lookups, but
90 /// leaves the filename as a possible symlink.
91 void updateWithRealPath(SmallVectorImpl<char> &Path);
92
94
95 StringMap<std::string> CachedDirs;
96 };
97
98 /// \p Root is the directory where collected files are will be stored.
99 /// \p OverlayRoot is VFS mapping root.
100 /// \p Root directory gets created in copyFiles unless it already exists.
101 FileCollector(std::string Root, std::string OverlayRoot,
103
104 /// Write the yaml mapping (for the VFS) to the given file.
105 std::error_code writeMapping(StringRef MappingFile);
106
107 /// Copy the files into the root directory.
108 ///
109 /// When StopOnError is true (the default) we abort as soon as one file
110 /// cannot be copied. This is relatively common, for example when a file was
111 /// removed after it was added to the mapping.
112 std::error_code copyFiles(bool StopOnError = true);
113
114 /// Create a VFS that uses \p Collector to collect files accessed via \p
115 /// BaseFS.
117 createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
118 std::shared_ptr<FileCollector> Collector);
119
120private:
122
123 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
124 if (sys::fs::is_directory(VirtualPath))
125 VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
126 else
127 VFSWriter.addFileMapping(VirtualPath, RealPath);
128 }
129
130protected:
131 void addFileImpl(StringRef SrcPath) override;
132
134 addDirectoryImpl(const llvm::Twine &Dir,
135 IntrusiveRefCntPtr<vfs::FileSystem> FS,
136 std::error_code &EC) override;
137
138 /// The directory where collected files are copied to in copyFiles().
139 const std::string Root;
140
141 /// The root directory where the VFS overlay lives.
142 const std::string OverlayRoot;
143
144 /// The yaml mapping writer.
146
147 /// Helper utility for canonicalizing paths.
149};
150
151} // end namespace llvm
152
153#endif // LLVM_SUPPORT_FILECOLLECTOR_H
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:213
Register Usage Information Collector
dot regions Print regions of function to dot file(with no function bodies)"
StringSet - A set-like wrapper for the StringMap.
Defines the virtual file system interface vfs::FileSystem.
std::mutex Mutex
Synchronizes access to internal data structures.
void addDirectory(const Twine &Dir)
void addFile(const Twine &file)
StringSet Seen
Tracks already seen files so they can be skipped.
virtual llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC)=0
bool markAsSeen(StringRef Path)
virtual void addFileImpl(StringRef SrcPath)=0
Helper utility that encapsulates the logic for canonicalizing a virtual path and a path to copy from.
PathCanonicalizer(IntrusiveRefCntPtr< vfs::FileSystem > VFS)
LLVM_ABI PathStorage canonicalize(StringRef SrcPath)
Canonicalize a pair of virtual and real paths.
Captures file system interaction and generates data to be later replayed with the RedirectingFileSyst...
FileCollector(std::string Root, std::string OverlayRoot, IntrusiveRefCntPtr< vfs::FileSystem > VFS)
Root is the directory where collected files are will be stored.
const std::string OverlayRoot
The root directory where the VFS overlay lives.
PathCanonicalizer Canonicalizer
Helper utility for canonicalizing paths.
const std::string Root
The directory where collected files are copied to in copyFiles().
vfs::YAMLVFSWriter VFSWriter
The yaml mapping writer.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
LLVM_ABI bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition Path.cpp:1092
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1847
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870