LLVM  14.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/SmallVector.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringSet.h"
16 #include <mutex>
17 #include <string>
18 
19 namespace llvm {
20 class FileCollectorFileSystem;
21 class Twine;
22 
24 public:
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 
41  addDirectoryImpl(const llvm::Twine &Dir,
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.
71 public:
72  /// Helper utility that encapsulates the logic for canonicalizing a virtual
73  /// path and a path to copy from.
75  public:
76  struct PathStorage {
79  };
80 
81  /// Canonicalize a pair of virtual and real paths.
83 
84  private:
85  /// Replace with a (mostly) real path, or don't modify. Resolves symlinks
86  /// in the directory, using \a CachedDirs to avoid redundant lookups, but
87  /// leaves the filename as a possible symlink.
88  void updateWithRealPath(SmallVectorImpl<char> &Path);
89 
90  StringMap<std::string> CachedDirs;
91  };
92 
93  /// \p Root is the directory where collected files are will be stored.
94  /// \p OverlayRoot is VFS mapping root.
95  /// \p Root directory gets created in copyFiles unless it already exists.
96  FileCollector(std::string Root, std::string OverlayRoot);
97 
98  /// Write the yaml mapping (for the VFS) to the given file.
99  std::error_code writeMapping(StringRef MappingFile);
100 
101  /// Copy the files into the root directory.
102  ///
103  /// When StopOnError is true (the default) we abort as soon as one file
104  /// cannot be copied. This is relatively common, for example when a file was
105  /// removed after it was added to the mapping.
106  std::error_code copyFiles(bool StopOnError = true);
107 
108  /// Create a VFS that uses \p Collector to collect files accessed via \p
109  /// BaseFS.
112  std::shared_ptr<FileCollector> Collector);
113 
114 private:
116 
117  void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
118  if (sys::fs::is_directory(VirtualPath))
119  VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
120  else
121  VFSWriter.addFileMapping(VirtualPath, RealPath);
122  }
123 
124 protected:
125  void addFileImpl(StringRef SrcPath) override;
126 
128  addDirectoryImpl(const llvm::Twine &Dir,
130  std::error_code &EC) override;
131 
132  /// The directory where collected files are copied to in copyFiles().
133  const std::string Root;
134 
135  /// The root directory where the VFS overlay lives.
136  const std::string OverlayRoot;
137 
138  /// The yaml mapping writer.
140 
141  /// Helper utility for canonicalizing paths.
143 };
144 
145 } // end namespace llvm
146 
147 #endif // LLVM_SUPPORT_FILECOLLECTOR_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::FileCollector::Root
const std::string Root
The directory where collected files are copied to in copyFiles().
Definition: FileCollector.h:133
llvm::FileCollectorBase::markAsSeen
bool markAsSeen(StringRef Path)
Definition: FileCollector.h:32
llvm::FileCollector::OverlayRoot
const std::string OverlayRoot
The root directory where the VFS overlay lives.
Definition: FileCollector.h:136
llvm::FileCollector::addFileImpl
void addFileImpl(StringRef SrcPath) override
Definition: FileCollector.cpp:120
llvm::FileCollector::createCollectorVFS
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.
Definition: FileCollector.cpp:312
llvm::vfs::directory_iterator
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
Definition: VirtualFileSystem.h:159
llvm::FileCollector::PathCanonicalizer::PathStorage
Definition: FileCollector.h:76
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
Collector
Register Usage Information Collector
Definition: RegUsageInfoCollector.cpp:74
llvm::FileCollectorBase::addFileImpl
virtual void addFileImpl(StringRef SrcPath)=0
llvm::FileCollectorBase
Definition: FileCollector.h:23
llvm::vfs::YAMLVFSWriter
Definition: VirtualFileSystem.h:883
llvm::FileCollector::writeMapping
std::error_code writeMapping(StringRef MappingFile)
Write the yaml mapping (for the VFS) to the given file.
Definition: FileCollector.cpp:236
llvm::FileCollector::addDirectoryImpl
llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC) override
Definition: FileCollector.cpp:135
llvm::FileCollector::Canonicalizer
PathCanonicalizer Canonicalizer
Helper utility for canonicalizing paths.
Definition: FileCollector.h:142
StringMap.h
llvm::FileCollectorBase::~FileCollectorBase
virtual ~FileCollectorBase()
llvm::StringMap< std::string >
llvm::SmallString< 256 >
llvm::FileCollector::copyFiles
std::error_code copyFiles(bool StopOnError=true)
Copy the files into the root directory.
Definition: FileCollector.cpp:176
file
dot regions Print regions of function to dot file(with no function bodies)"
llvm::FileCollectorBase::Seen
StringSet Seen
Tracks already seen files so they can be skipped.
Definition: FileCollector.h:49
llvm::FileCollectorFileSystem
Definition: FileCollector.cpp:255
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::FileCollectorBase::addFile
void addFile(const Twine &file)
Definition: FileCollector.cpp:21
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::FileCollectorBase::Mutex
std::mutex Mutex
Synchronizes access to internal data structures.
Definition: FileCollector.h:46
VirtualFileSystem.h
llvm::FileCollector::PathCanonicalizer::PathStorage::VirtualPath
SmallString< 256 > VirtualPath
Definition: FileCollector.h:78
StringSet.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::FileCollectorBase::FileCollectorBase
FileCollectorBase()
llvm::vfs::YAMLVFSWriter::addFileMapping
void addFileMapping(StringRef VirtualPath, StringRef RealPath)
Definition: VirtualFileSystem.cpp:2158
llvm::sys::fs::is_directory
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1088
llvm::FileCollectorBase::addDirectoryImpl
virtual llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC)=0
llvm::FileCollector::PathCanonicalizer::PathStorage::CopyFrom
SmallString< 256 > CopyFrom
Definition: FileCollector.h:77
SmallVector.h
llvm::FileCollector::PathCanonicalizer::canonicalize
PathStorage canonicalize(StringRef SrcPath)
Canonicalize a pair of virtual and real paths.
Definition: FileCollector.cpp:103
llvm::FileCollector::PathCanonicalizer
Helper utility that encapsulates the logic for canonicalizing a virtual path and a path to copy from.
Definition: FileCollector.h:74
llvm::vfs::YAMLVFSWriter::addDirectoryMapping
void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath)
Definition: VirtualFileSystem.cpp:2162
llvm::SmallVectorImpl< char >
llvm::FileCollector::FileCollector
FileCollector(std::string Root, std::string OverlayRoot)
Root is the directory where collected files are will be stored.
Definition: FileCollector.cpp:52
llvm::FileCollector
Captures file system interaction and generates data to be later replayed with the RedirectingFileSyst...
Definition: FileCollector.h:70
llvm::FileCollectorBase::addDirectory
void addDirectory(const Twine &Dir)
Definition: FileCollector.cpp:28
llvm::FileCollector::VFSWriter
vfs::YAMLVFSWriter VFSWriter
The yaml mapping writer.
Definition: FileCollector.h:139
llvm::IntrusiveRefCntPtr
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
Definition: IntrusiveRefCntPtr.h:163