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/StringMap.h"
13 #include "llvm/ADT/StringSet.h"
15 #include <mutex>
16 #include <string>
17 
18 namespace llvm {
19 class FileCollectorFileSystem;
20 class Twine;
21 
23 public:
25  virtual ~FileCollectorBase();
26 
27  void addFile(const Twine &file);
28  void addDirectory(const Twine &Dir);
29 
30 protected:
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 
40  addDirectoryImpl(const llvm::Twine &Dir,
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.
70 public:
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 
113 private:
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 
123 protected:
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
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:22
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::FileCollector::Root
const std::string Root
The directory where collected files are copied to in copyFiles().
Definition: FileCollector.h:132
llvm::FileCollectorBase::markAsSeen
bool markAsSeen(StringRef Path)
Definition: FileCollector.h:31
llvm::FileCollector::OverlayRoot
const std::string OverlayRoot
The root directory where the VFS overlay lives.
Definition: FileCollector.h:135
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:170
llvm::FileCollector::PathCanonicalizer::PathStorage
Definition: FileCollector.h:75
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:22
llvm::vfs::YAMLVFSWriter
Definition: VirtualFileSystem.h:915
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:141
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::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::FileCollectorBase::Seen
StringSet Seen
Tracks already seen files so they can be skipped.
Definition: FileCollector.h:48
llvm::FileCollectorFileSystem
Definition: FileCollector.cpp:255
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:45
VirtualFileSystem.h
llvm::FileCollector::PathCanonicalizer::PathStorage::VirtualPath
SmallString< 256 > VirtualPath
Definition: FileCollector.h:77
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:2267
llvm::sys::fs::is_directory
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1089
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:76
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:73
llvm::vfs::YAMLVFSWriter::addDirectoryMapping
void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath)
Definition: VirtualFileSystem.cpp:2271
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:69
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:138
llvm::IntrusiveRefCntPtr
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
Definition: IntrusiveRefCntPtr.h:163