LLVM 19.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"
15#include <mutex>
16#include <string>
17
18namespace llvm {
19class FileCollectorFileSystem;
20class Twine;
21
23public:
26
27 void addFile(const Twine &file);
28 void addDirectory(const Twine &Dir);
29
30protected:
31 bool markAsSeen(StringRef Path) {
32 if (Path.empty())
33 return false;
34 return Seen.insert(Path).second;
35 }
36
37 virtual void addFileImpl(StringRef SrcPath) = 0;
38
42 std::error_code &EC) = 0;
43
44 /// Synchronizes access to internal data structures.
45 std::mutex Mutex;
46
47 /// Tracks already seen files so they can be skipped.
49};
50
51/// Captures file system interaction and generates data to be later replayed
52/// with the RedirectingFileSystem.
53///
54/// For any file that gets accessed we eventually create:
55/// - a copy of the file inside Root
56/// - a record in RedirectingFileSystem mapping that maps:
57/// current real path -> path to the copy in Root
58///
59/// That intent is that later when the mapping is used by RedirectingFileSystem
60/// it simulates the state of FS that we collected.
61///
62/// We generate file copies and mapping lazily - see writeMapping and copyFiles.
63/// We don't try to capture the state of the file at the exact time when it's
64/// accessed. Files might get changed, deleted ... we record only the "final"
65/// state.
66///
67/// In order to preserve the relative topology of files we use their real paths
68/// as relative paths inside of the Root.
70public:
71 /// Helper utility that encapsulates the logic for canonicalizing a virtual
72 /// path and a path to copy from.
74 public:
75 struct PathStorage {
78 };
79
80 /// Canonicalize a pair of virtual and real paths.
82
83 private:
84 /// Replace with a (mostly) real path, or don't modify. Resolves symlinks
85 /// in the directory, using \a CachedDirs to avoid redundant lookups, but
86 /// leaves the filename as a possible symlink.
87 void updateWithRealPath(SmallVectorImpl<char> &Path);
88
89 StringMap<std::string> CachedDirs;
90 };
91
92 /// \p Root is the directory where collected files are will be stored.
93 /// \p OverlayRoot is VFS mapping root.
94 /// \p Root directory gets created in copyFiles unless it already exists.
95 FileCollector(std::string Root, std::string OverlayRoot);
96
97 /// Write the yaml mapping (for the VFS) to the given file.
98 std::error_code writeMapping(StringRef MappingFile);
99
100 /// Copy the files into the root directory.
101 ///
102 /// When StopOnError is true (the default) we abort as soon as one file
103 /// cannot be copied. This is relatively common, for example when a file was
104 /// removed after it was added to the mapping.
105 std::error_code copyFiles(bool StopOnError = true);
106
107 /// Create a VFS that uses \p Collector to collect files accessed via \p
108 /// BaseFS.
111 std::shared_ptr<FileCollector> Collector);
112
113private:
115
116 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
117 if (sys::fs::is_directory(VirtualPath))
118 VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
119 else
120 VFSWriter.addFileMapping(VirtualPath, RealPath);
121 }
122
123protected:
124 void addFileImpl(StringRef SrcPath) override;
125
127 addDirectoryImpl(const llvm::Twine &Dir,
129 std::error_code &EC) override;
130
131 /// The directory where collected files are copied to in copyFiles().
132 const std::string Root;
133
134 /// The root directory where the VFS overlay lives.
135 const std::string OverlayRoot;
136
137 /// The yaml mapping writer.
139
140 /// Helper utility for canonicalizing paths.
142};
143
144} // end namespace llvm
145
146#endif // LLVM_SUPPORT_FILECOLLECTOR_H
This file defines the StringMap class.
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.
Definition: FileCollector.h:45
void addDirectory(const Twine &Dir)
void addFile(const Twine &file)
StringSet Seen
Tracks already seen files so they can be skipped.
Definition: FileCollector.h:48
virtual llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC)=0
bool markAsSeen(StringRef Path)
Definition: FileCollector.h:31
virtual void addFileImpl(StringRef SrcPath)=0
Helper utility that encapsulates the logic for canonicalizing a virtual path and a path to copy from.
Definition: FileCollector.h:73
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...
Definition: FileCollector.h:69
llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC) override
std::error_code writeMapping(StringRef MappingFile)
Write the yaml mapping (for the VFS) to the given file.
std::error_code copyFiles(bool StopOnError=true)
Copy the files into the root directory.
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.
void addFileImpl(StringRef SrcPath) override
static IntrusiveRefCntPtr< vfs::FileSystem > createCollectorVFS(IntrusiveRefCntPtr< vfs::FileSystem > BaseFS, std::shared_ptr< FileCollector > Collector)
Create a VFS that uses Collector to collect files accessed via BaseFS.
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...
Definition: SmallVector.h:586
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void addFileMapping(StringRef VirtualPath, StringRef RealPath)
void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath)
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1093
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18