clang  15.0.0git
FileManager.h
Go to the documentation of this file.
1 //===--- FileManager.h - File System Probing and Caching --------*- 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 /// \file
10 /// Defines the clang::FileManager interface and associated types.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_BASIC_FILEMANAGER_H
15 #define LLVM_CLANG_BASIC_FILEMANAGER_H
16 
18 #include "clang/Basic/FileEntry.h"
20 #include "clang/Basic/LLVM.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/IntrusiveRefCntPtr.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/VirtualFileSystem.h"
31 #include <ctime>
32 #include <map>
33 #include <memory>
34 #include <string>
35 
36 namespace llvm {
37 
38 class MemoryBuffer;
39 
40 } // end namespace llvm
41 
42 namespace clang {
43 
44 class FileSystemStatCache;
45 
46 /// Implements support for file system lookup, file system caching,
47 /// and directory search management.
48 ///
49 /// This also handles more advanced properties, such as uniquing files based
50 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
51 /// as a single file.
52 ///
53 class FileManager : public RefCountedBase<FileManager> {
55  FileSystemOptions FileSystemOpts;
56  llvm::SpecificBumpPtrAllocator<FileEntry> FilesAlloc;
57  llvm::SpecificBumpPtrAllocator<DirectoryEntry> DirsAlloc;
58 
59  /// Cache for existing real directories.
60  llvm::DenseMap<llvm::sys::fs::UniqueID, DirectoryEntry *> UniqueRealDirs;
61 
62  /// Cache for existing real files.
63  llvm::DenseMap<llvm::sys::fs::UniqueID, FileEntry *> UniqueRealFiles;
64 
65  /// The virtual directories that we have allocated.
66  ///
67  /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
68  /// directories (foo/ and foo/bar/) here.
69  SmallVector<DirectoryEntry *, 4> VirtualDirectoryEntries;
70  /// The virtual files that we have allocated.
71  SmallVector<FileEntry *, 4> VirtualFileEntries;
72 
73  /// A set of files that bypass the maps and uniquing. They can have
74  /// conflicting filenames.
75  SmallVector<FileEntry *, 0> BypassFileEntries;
76 
77  /// A cache that maps paths to directory entries (either real or
78  /// virtual) we have looked up, or an error that occurred when we looked up
79  /// the directory.
80  ///
81  /// The actual Entries for real directories/files are
82  /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
83  /// for virtual directories/files are owned by
84  /// VirtualDirectoryEntries/VirtualFileEntries above.
85  ///
86  llvm::StringMap<llvm::ErrorOr<DirectoryEntry &>, llvm::BumpPtrAllocator>
87  SeenDirEntries;
88 
89  /// A cache that maps paths to file entries (either real or
90  /// virtual) we have looked up, or an error that occurred when we looked up
91  /// the file.
92  ///
93  /// \see SeenDirEntries
94  llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>, llvm::BumpPtrAllocator>
95  SeenFileEntries;
96 
97  /// A mirror of SeenFileEntries to give fake answers for getBypassFile().
98  ///
99  /// Don't bother hooking up a BumpPtrAllocator. This should be rarely used,
100  /// and only on error paths.
101  std::unique_ptr<llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>
102  SeenBypassFileEntries;
103 
104  /// The file entry for stdin, if it has been accessed through the FileManager.
106 
107  /// The canonical names of files and directories .
108  llvm::DenseMap<const void *, llvm::StringRef> CanonicalNames;
109 
110  /// Storage for canonical names that we have computed.
111  llvm::BumpPtrAllocator CanonicalNameStorage;
112 
113  /// Each FileEntry we create is assigned a unique ID #.
114  ///
115  unsigned NextFileUID;
116 
117  // Caching.
118  std::unique_ptr<FileSystemStatCache> StatCache;
119 
120  std::error_code getStatValue(StringRef Path, llvm::vfs::Status &Status,
121  bool isFile,
122  std::unique_ptr<llvm::vfs::File> *F);
123 
124  /// Add all ancestors of the given path (pointing to either a file
125  /// or a directory) as virtual directories.
126  void addAncestorsAsVirtualDirs(StringRef Path);
127 
128  /// Fills the RealPathName in file entry.
129  void fillRealPathName(FileEntry *UFE, llvm::StringRef FileName);
130 
131 public:
132  /// Construct a file manager, optionally with a custom VFS.
133  ///
134  /// \param FS if non-null, the VFS to use. Otherwise uses
135  /// llvm::vfs::getRealFileSystem().
136  FileManager(const FileSystemOptions &FileSystemOpts,
138  ~FileManager();
139 
140  /// Installs the provided FileSystemStatCache object within
141  /// the FileManager.
142  ///
143  /// Ownership of this object is transferred to the FileManager.
144  ///
145  /// \param statCache the new stat cache to install. Ownership of this
146  /// object is transferred to the FileManager.
147  void setStatCache(std::unique_ptr<FileSystemStatCache> statCache);
148 
149  /// Removes the FileSystemStatCache object from the manager.
150  void clearStatCache();
151 
152  /// Returns the number of unique real file entries cached by the file manager.
153  size_t getNumUniqueRealFiles() const { return UniqueRealFiles.size(); }
154 
155  /// Lookup, cache, and verify the specified directory (real or
156  /// virtual).
157  ///
158  /// This returns a \c std::error_code if there was an error reading the
159  /// directory. On success, returns the reference to the directory entry
160  /// together with the exact path that was used to access a file by a
161  /// particular call to getDirectoryRef.
162  ///
163  /// \param CacheFailure If true and the file does not exist, we'll cache
164  /// the failure to find this file.
166  bool CacheFailure = true);
167 
168  /// Get a \c DirectoryEntryRef if it exists, without doing anything on error.
170  getOptionalDirectoryRef(StringRef DirName, bool CacheFailure = true) {
171  return llvm::expectedToOptional(getDirectoryRef(DirName, CacheFailure));
172  }
173 
174  /// Lookup, cache, and verify the specified directory (real or
175  /// virtual).
176  ///
177  /// This function is deprecated and will be removed at some point in the
178  /// future, new clients should use
179  /// \c getDirectoryRef.
180  ///
181  /// This returns a \c std::error_code if there was an error reading the
182  /// directory. If there is no error, the DirectoryEntry is guaranteed to be
183  /// non-NULL.
184  ///
185  /// \param CacheFailure If true and the file does not exist, we'll cache
186  /// the failure to find this file.
187  llvm::ErrorOr<const DirectoryEntry *>
188  getDirectory(StringRef DirName, bool CacheFailure = true);
189 
190  /// Lookup, cache, and verify the specified file (real or
191  /// virtual).
192  ///
193  /// This function is deprecated and will be removed at some point in the
194  /// future, new clients should use
195  /// \c getFileRef.
196  ///
197  /// This returns a \c std::error_code if there was an error loading the file.
198  /// If there is no error, the FileEntry is guaranteed to be non-NULL.
199  ///
200  /// \param OpenFile if true and the file exists, it will be opened.
201  ///
202  /// \param CacheFailure If true and the file does not exist, we'll cache
203  /// the failure to find this file.
204  llvm::ErrorOr<const FileEntry *>
205  getFile(StringRef Filename, bool OpenFile = false, bool CacheFailure = true);
206 
207  /// Lookup, cache, and verify the specified file (real or virtual). Return the
208  /// reference to the file entry together with the exact path that was used to
209  /// access a file by a particular call to getFileRef. If the underlying VFS is
210  /// a redirecting VFS that uses external file names, the returned FileEntryRef
211  /// will use the external name instead of the filename that was passed to this
212  /// method.
213  ///
214  /// This returns a \c std::error_code if there was an error loading the file,
215  /// or a \c FileEntryRef otherwise.
216  ///
217  /// \param OpenFile if true and the file exists, it will be opened.
218  ///
219  /// \param CacheFailure If true and the file does not exist, we'll cache
220  /// the failure to find this file.
222  bool OpenFile = false,
223  bool CacheFailure = true);
224 
225  /// Get the FileEntryRef for stdin, returning an error if stdin cannot be
226  /// read.
227  ///
228  /// This reads and caches stdin before returning. Subsequent calls return the
229  /// same file entry, and a reference to the cached input is returned by calls
230  /// to getBufferForFile.
232 
233  /// Get a FileEntryRef if it exists, without doing anything on error.
235  bool OpenFile = false,
236  bool CacheFailure = true) {
237  return llvm::expectedToOptional(
238  getFileRef(Filename, OpenFile, CacheFailure));
239  }
240 
241  /// Returns the current file system options
242  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
243  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
244 
245  llvm::vfs::FileSystem &getVirtualFileSystem() const { return *FS; }
246 
248  this->FS = std::move(FS);
249  }
250 
251  /// Retrieve a file entry for a "virtual" file that acts as
252  /// if there were a file with the given name on disk.
253  ///
254  /// The file itself is not accessed.
255  FileEntryRef getVirtualFileRef(StringRef Filename, off_t Size,
256  time_t ModificationTime);
257 
258  const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
259  time_t ModificationTime);
260 
261  /// Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual
262  /// file entry, to access the real file. The returned FileEntry will have
263  /// the same filename as FE but a different identity and its own stat.
264  ///
265  /// This should be used only for rare error recovery paths because it
266  /// bypasses all mapping and uniquing, blindly creating a new FileEntry.
267  /// There is no attempt to deduplicate these; if you bypass the same file
268  /// twice, you get two new file entries.
270 
271  /// Open the specified file as a MemoryBuffer, returning a new
272  /// MemoryBuffer if successful, otherwise returning null.
273  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
274  getBufferForFile(const FileEntry *Entry, bool isVolatile = false,
275  bool RequiresNullTerminator = true);
276  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
277  getBufferForFile(StringRef Filename, bool isVolatile = false,
278  bool RequiresNullTerminator = true) {
279  return getBufferForFileImpl(Filename, /*FileSize=*/-1, isVolatile,
280  RequiresNullTerminator);
281  }
282 
283 private:
284  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
285  getBufferForFileImpl(StringRef Filename, int64_t FileSize, bool isVolatile,
286  bool RequiresNullTerminator);
287 
288 public:
289  /// Get the 'stat' information for the given \p Path.
290  ///
291  /// If the path is relative, it will be resolved against the WorkingDir of the
292  /// FileManager's FileSystemOptions.
293  ///
294  /// \returns a \c std::error_code describing an error, if there was one
295  std::error_code getNoncachedStatValue(StringRef Path,
296  llvm::vfs::Status &Result);
297 
298  /// If path is not absolute and FileSystemOptions set the working
299  /// directory, the path is modified to be relative to the given
300  /// working directory.
301  /// \returns true if \c path changed.
302  bool FixupRelativePath(SmallVectorImpl<char> &path) const;
303 
304  /// Makes \c Path absolute taking into account FileSystemOptions and the
305  /// working directory option.
306  /// \returns true if \c Path changed to absolute.
307  bool makeAbsolutePath(SmallVectorImpl<char> &Path) const;
308 
309  /// Produce an array mapping from the unique IDs assigned to each
310  /// file to the corresponding FileEntry pointer.
311  void GetUniqueIDMapping(
312  SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
313 
314  /// Retrieve the canonical name for a given directory.
315  ///
316  /// This is a very expensive operation, despite its results being cached,
317  /// and should only be used when the physical layout of the file system is
318  /// required, which is (almost) never.
319  StringRef getCanonicalName(const DirectoryEntry *Dir);
320 
321  /// Retrieve the canonical name for a given file.
322  ///
323  /// This is a very expensive operation, despite its results being cached,
324  /// and should only be used when the physical layout of the file system is
325  /// required, which is (almost) never.
326  StringRef getCanonicalName(const FileEntry *File);
327 
328  void PrintStats() const;
329 };
330 
331 } // end namespace clang
332 
333 #endif // LLVM_CLANG_BASIC_FILEMANAGER_H
clang::FileManager::getNoncachedStatValue
std::error_code getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result)
Get the 'stat' information for the given Path.
Definition: FileManager.cpp:626
clang::FileManager::getOptionalFileRef
llvm::Optional< FileEntryRef > getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:234
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::FileManager::FileManager
FileManager(const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
Construct a file manager, optionally with a custom VFS.
Definition: FileManager.cpp:51
clang::FileEntryRef
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:60
clang::FileManager::GetUniqueIDMapping
void GetUniqueIDMapping(SmallVectorImpl< const FileEntry * > &UIDToFiles) const
Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntry poi...
Definition: FileManager.cpp:638
clang::FileManager::setStatCache
void setStatCache(std::unique_ptr< FileSystemStatCache > statCache)
Installs the provided FileSystemStatCache object within the FileManager.
Definition: FileManager.cpp:63
llvm::SmallVector
Definition: LLVM.h:38
clang::FileManager::clearStatCache
void clearStatCache()
Removes the FileSystemStatCache object from the manager.
Definition: FileManager.cpp:68
clang::FileManager::getFileRef
llvm::Expected< FileEntryRef > getFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:205
Filename
StringRef Filename
Definition: Format.cpp:2551
llvm::Optional< FileEntryRef >
DirectoryEntry.h
llvm::Expected
Definition: LLVM.h:41
clang::FileManager::getOptionalDirectoryRef
llvm::Optional< DirectoryEntryRef > getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:170
llvm::RefCountedBase
Definition: LLVM.h:49
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:22
FileEntry.h
clang::FileManager::getBufferForFile
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(StringRef Filename, bool isVolatile=false, bool RequiresNullTerminator=true)
Definition: FileManager.h:277
clang::FileManager::getFileSystemOpts
const FileSystemOptions & getFileSystemOpts() const
Definition: FileManager.h:243
clang::FileManager::getVirtualFileSystem
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:245
clang::DirectoryEntry
Cached information about one directory (either on disk or in the virtual file system).
Definition: DirectoryEntry.h:34
clang::FileManager::setVirtualFileSystem
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Definition: FileManager.h:247
clang::FileManager::getDirectoryRef
llvm::Expected< DirectoryEntryRef > getDirectoryRef(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Definition: FileManager.cpp:118
clang::FileManager::FixupRelativePath
bool FixupRelativePath(SmallVectorImpl< char > &path) const
If path is not absolute and FileSystemOptions set the working directory, the path is modified to be r...
Definition: FileManager.cpp:528
clang::FileManager::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:242
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:330
clang::FileManager::PrintStats
void PrintStats() const
Definition: FileManager.cpp:691
clang::FileManager::getVirtualFileRef
FileEntryRef getVirtualFileRef(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
Definition: FileManager.cpp:419
clang::FileManager::getBufferForFile
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: FileManager.cpp:564
clang::FileManager::getNumUniqueRealFiles
size_t getNumUniqueRealFiles() const
Returns the number of unique real file entries cached by the file manager.
Definition: FileManager.h:153
LLVM.h
clang::FileManager::~FileManager
~FileManager()
clang::FileManager::makeAbsolutePath
bool makeAbsolutePath(SmallVectorImpl< char > &Path) const
Makes Path absolute taking into account FileSystemOptions and the working directory option.
Definition: FileManager.cpp:541
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::FileManager::getCanonicalName
StringRef getCanonicalName(const DirectoryEntry *Dir)
Retrieve the canonical name for a given directory.
Definition: FileManager.cpp:659
clang::FileManager::getSTDIN
llvm::Expected< FileEntryRef > getSTDIN()
Get the FileEntryRef for stdin, returning an error if stdin cannot be read.
Definition: FileManager.cpp:395
clang::FileManager::getDirectory
llvm::ErrorOr< const DirectoryEntry * > getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Definition: FileManager.cpp:189
clang::FileManager::getFile
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:197
FileSystemOptions.h
llvm::SmallVectorImpl< char >
clang::FileManager::getVirtualFile
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Definition: FileManager.cpp:414
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >
clang::FileManager::getBypassFile
llvm::Optional< FileEntryRef > getBypassFile(FileEntryRef VFE)
Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry,...
Definition: FileManager.cpp:498