clang  10.0.0svn
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/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/VirtualFileSystem.h"
28 #include <ctime>
29 #include <map>
30 #include <memory>
31 #include <string>
32 
33 namespace llvm {
34 
35 class MemoryBuffer;
36 
37 } // end namespace llvm
38 
39 namespace clang {
40 
41 class FileSystemStatCache;
42 
43 /// Cached information about one directory (either on disk or in
44 /// the virtual file system).
46  friend class FileManager;
47 
48  // FIXME: We should not be storing a directory entry name here.
49  StringRef Name; // Name of the directory.
50 
51 public:
52  StringRef getName() const { return Name; }
53 };
54 
55 /// A reference to a \c DirectoryEntry that includes the name of the directory
56 /// as it was accessed by the FileManager's client.
58 public:
59  const DirectoryEntry &getDirEntry() const { return *Entry->getValue(); }
60 
61  StringRef getName() const { return Entry->getKey(); }
62 
63 private:
64  friend class FileManager;
65 
67  llvm::StringMapEntry<llvm::ErrorOr<DirectoryEntry &>> *Entry)
68  : Entry(Entry) {}
69 
70  const llvm::StringMapEntry<llvm::ErrorOr<DirectoryEntry &>> *Entry;
71 };
72 
73 /// Cached information about one file (either on disk
74 /// or in the virtual file system).
75 ///
76 /// If the 'File' member is valid, then this FileEntry has an open file
77 /// descriptor for the file.
78 class FileEntry {
79  friend class FileManager;
80 
81  StringRef Name; // Name of the file.
82  std::string RealPathName; // Real path to the file; could be empty.
83  off_t Size; // File size in bytes.
84  time_t ModTime; // Modification time of file.
85  const DirectoryEntry *Dir; // Directory file lives in.
86  llvm::sys::fs::UniqueID UniqueID;
87  unsigned UID; // A unique (small) ID for the file.
88  bool IsNamedPipe;
89  bool IsValid; // Is this \c FileEntry initialized and valid?
90 
91  /// The open file, if it is owned by the \p FileEntry.
92  mutable std::unique_ptr<llvm::vfs::File> File;
93 
94 public:
96  : UniqueID(0, 0), IsNamedPipe(false), IsValid(false)
97  {}
98 
99  FileEntry(const FileEntry &) = delete;
100  FileEntry &operator=(const FileEntry &) = delete;
101 
102  StringRef getName() const { return Name; }
103  StringRef tryGetRealPathName() const { return RealPathName; }
104  bool isValid() const { return IsValid; }
105  off_t getSize() const { return Size; }
106  unsigned getUID() const { return UID; }
107  const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
108  time_t getModificationTime() const { return ModTime; }
109 
110  /// Return the directory the file lives in.
111  const DirectoryEntry *getDir() const { return Dir; }
112 
113  bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
114 
115  /// Check whether the file is a named pipe (and thus can't be opened by
116  /// the native FileManager methods).
117  bool isNamedPipe() const { return IsNamedPipe; }
118 
119  void closeFile() const {
120  File.reset(); // rely on destructor to close File
121  }
122 
123  // Only for use in tests to see if deferred opens are happening, rather than
124  // relying on RealPathName being empty.
125  bool isOpenForTests() const { return File != nullptr; }
126 };
127 
128 /// A reference to a \c FileEntry that includes the name of the file as it was
129 /// accessed by the FileManager's client.
131 public:
132  FileEntryRef() = delete;
133  FileEntryRef(StringRef Name, const FileEntry &Entry)
134  : Name(Name), Entry(&Entry) {}
135 
136  const StringRef getName() const { return Name; }
137 
138  bool isValid() const { return Entry->isValid(); }
139 
140  const FileEntry &getFileEntry() const { return *Entry; }
141 
142  off_t getSize() const { return Entry->getSize(); }
143 
144  unsigned getUID() const { return Entry->getUID(); }
145 
146  const llvm::sys::fs::UniqueID &getUniqueID() const {
147  return Entry->getUniqueID();
148  }
149 
150  time_t getModificationTime() const { return Entry->getModificationTime(); }
151 
152  friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
153  return LHS.Entry == RHS.Entry && LHS.Name == RHS.Name;
154  }
155  friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
156  return !(LHS == RHS);
157  }
158 
159 private:
160  StringRef Name;
161  const FileEntry *Entry;
162 };
163 
164 /// Implements support for file system lookup, file system caching,
165 /// and directory search management.
166 ///
167 /// This also handles more advanced properties, such as uniquing files based
168 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
169 /// as a single file.
170 ///
171 class FileManager : public RefCountedBase<FileManager> {
173  FileSystemOptions FileSystemOpts;
174 
175  /// Cache for existing real directories.
176  std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueRealDirs;
177 
178  /// Cache for existing real files.
179  std::map<llvm::sys::fs::UniqueID, FileEntry> UniqueRealFiles;
180 
181  /// The virtual directories that we have allocated.
182  ///
183  /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
184  /// directories (foo/ and foo/bar/) here.
185  SmallVector<std::unique_ptr<DirectoryEntry>, 4> VirtualDirectoryEntries;
186  /// The virtual files that we have allocated.
187  SmallVector<std::unique_ptr<FileEntry>, 4> VirtualFileEntries;
188 
189  /// A set of files that bypass the maps and uniquing. They can have
190  /// conflicting filenames.
191  SmallVector<std::unique_ptr<FileEntry>, 0> BypassFileEntries;
192 
193  /// A cache that maps paths to directory entries (either real or
194  /// virtual) we have looked up, or an error that occurred when we looked up
195  /// the directory.
196  ///
197  /// The actual Entries for real directories/files are
198  /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
199  /// for virtual directories/files are owned by
200  /// VirtualDirectoryEntries/VirtualFileEntries above.
201  ///
202  llvm::StringMap<llvm::ErrorOr<DirectoryEntry &>, llvm::BumpPtrAllocator>
203  SeenDirEntries;
204 
205  /// A reference to the file entry that is associated with a particular
206  /// filename, or a reference to another filename that should be looked up
207  /// instead of the accessed filename.
208  ///
209  /// The reference to another filename is specifically useful for Redirecting
210  /// VFSs that use external names. In that case, the \c FileEntryRef returned
211  /// by the \c FileManager will have the external name, and not the name that
212  /// was used to lookup the file.
213  using SeenFileEntryOrRedirect =
214  llvm::PointerUnion<FileEntry *, const StringRef *>;
215 
216  /// A cache that maps paths to file entries (either real or
217  /// virtual) we have looked up, or an error that occurred when we looked up
218  /// the file.
219  ///
220  /// \see SeenDirEntries
221  llvm::StringMap<llvm::ErrorOr<SeenFileEntryOrRedirect>,
222  llvm::BumpPtrAllocator>
223  SeenFileEntries;
224 
225  /// The canonical names of directories.
226  llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames;
227 
228  /// Storage for canonical names that we have computed.
229  llvm::BumpPtrAllocator CanonicalNameStorage;
230 
231  /// Each FileEntry we create is assigned a unique ID #.
232  ///
233  unsigned NextFileUID;
234 
235  // Statistics.
236  unsigned NumDirLookups, NumFileLookups;
237  unsigned NumDirCacheMisses, NumFileCacheMisses;
238 
239  // Caching.
240  std::unique_ptr<FileSystemStatCache> StatCache;
241 
242  std::error_code getStatValue(StringRef Path, llvm::vfs::Status &Status,
243  bool isFile,
244  std::unique_ptr<llvm::vfs::File> *F);
245 
246  /// Add all ancestors of the given path (pointing to either a file
247  /// or a directory) as virtual directories.
248  void addAncestorsAsVirtualDirs(StringRef Path);
249 
250  /// Fills the RealPathName in file entry.
251  void fillRealPathName(FileEntry *UFE, llvm::StringRef FileName);
252 
253 public:
254  /// Construct a file manager, optionally with a custom VFS.
255  ///
256  /// \param FS if non-null, the VFS to use. Otherwise uses
257  /// llvm::vfs::getRealFileSystem().
258  FileManager(const FileSystemOptions &FileSystemOpts,
260  ~FileManager();
261 
262  /// Installs the provided FileSystemStatCache object within
263  /// the FileManager.
264  ///
265  /// Ownership of this object is transferred to the FileManager.
266  ///
267  /// \param statCache the new stat cache to install. Ownership of this
268  /// object is transferred to the FileManager.
269  void setStatCache(std::unique_ptr<FileSystemStatCache> statCache);
270 
271  /// Removes the FileSystemStatCache object from the manager.
272  void clearStatCache();
273 
274  /// Returns the number of unique real file entries cached by the file manager.
275  size_t getNumUniqueRealFiles() const { return UniqueRealFiles.size(); }
276 
277  /// Lookup, cache, and verify the specified directory (real or
278  /// virtual).
279  ///
280  /// This returns a \c std::error_code if there was an error reading the
281  /// directory. On success, returns the reference to the directory entry
282  /// together with the exact path that was used to access a file by a
283  /// particular call to getDirectoryRef.
284  ///
285  /// \param CacheFailure If true and the file does not exist, we'll cache
286  /// the failure to find this file.
287  llvm::Expected<DirectoryEntryRef> getDirectoryRef(StringRef DirName,
288  bool CacheFailure = true);
289 
290  /// Get a \c DirectoryEntryRef if it exists, without doing anything on error.
292  getOptionalDirectoryRef(StringRef DirName, bool CacheFailure = true) {
293  return llvm::expectedToOptional(getDirectoryRef(DirName, CacheFailure));
294  }
295 
296  /// Lookup, cache, and verify the specified directory (real or
297  /// virtual).
298  ///
299  /// This function is deprecated and will be removed at some point in the
300  /// future, new clients should use
301  /// \c getDirectoryRef.
302  ///
303  /// This returns a \c std::error_code if there was an error reading the
304  /// directory. If there is no error, the DirectoryEntry is guaranteed to be
305  /// non-NULL.
306  ///
307  /// \param CacheFailure If true and the file does not exist, we'll cache
308  /// the failure to find this file.
309  llvm::ErrorOr<const DirectoryEntry *>
310  getDirectory(StringRef DirName, bool CacheFailure = true);
311 
312  /// Lookup, cache, and verify the specified file (real or
313  /// virtual).
314  ///
315  /// This function is deprecated and will be removed at some point in the
316  /// future, new clients should use
317  /// \c getFileRef.
318  ///
319  /// This returns a \c std::error_code if there was an error loading the file.
320  /// If there is no error, the FileEntry is guaranteed to be non-NULL.
321  ///
322  /// \param OpenFile if true and the file exists, it will be opened.
323  ///
324  /// \param CacheFailure If true and the file does not exist, we'll cache
325  /// the failure to find this file.
326  llvm::ErrorOr<const FileEntry *>
327  getFile(StringRef Filename, bool OpenFile = false, bool CacheFailure = true);
328 
329  /// Lookup, cache, and verify the specified file (real or virtual). Return the
330  /// reference to the file entry together with the exact path that was used to
331  /// access a file by a particular call to getFileRef. If the underlying VFS is
332  /// a redirecting VFS that uses external file names, the returned FileEntryRef
333  /// will use the external name instead of the filename that was passed to this
334  /// method.
335  ///
336  /// This returns a \c std::error_code if there was an error loading the file,
337  /// or a \c FileEntryRef otherwise.
338  ///
339  /// \param OpenFile if true and the file exists, it will be opened.
340  ///
341  /// \param CacheFailure If true and the file does not exist, we'll cache
342  /// the failure to find this file.
343  llvm::Expected<FileEntryRef> getFileRef(StringRef Filename,
344  bool OpenFile = false,
345  bool CacheFailure = true);
346 
347  /// Get a FileEntryRef if it exists, without doing anything on error.
349  bool OpenFile = false,
350  bool CacheFailure = true) {
351  return llvm::expectedToOptional(
352  getFileRef(Filename, OpenFile, CacheFailure));
353  }
354 
355  /// Returns the current file system options
356  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
357  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
358 
359  llvm::vfs::FileSystem &getVirtualFileSystem() const { return *FS; }
360 
362  this->FS = std::move(FS);
363  }
364 
365  /// Retrieve a file entry for a "virtual" file that acts as
366  /// if there were a file with the given name on disk.
367  ///
368  /// The file itself is not accessed.
369  const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
370  time_t ModificationTime);
371 
372  /// Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual
373  /// file entry, to access the real file. The returned FileEntry will have
374  /// the same filename as FE but a different identity and its own stat.
375  ///
376  /// This should be used only for rare error recovery paths because it
377  /// bypasses all mapping and uniquing, blindly creating a new FileEntry.
378  /// There is no attempt to deduplicate these; if you bypass the same file
379  /// twice, you get two new file entries.
380  llvm::Optional<FileEntryRef> getBypassFile(FileEntryRef VFE);
381 
382  /// Open the specified file as a MemoryBuffer, returning a new
383  /// MemoryBuffer if successful, otherwise returning null.
384  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
385  getBufferForFile(const FileEntry *Entry, bool isVolatile = false);
386  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
387  getBufferForFile(StringRef Filename, bool isVolatile = false) {
388  return getBufferForFileImpl(Filename, /*FileSize=*/-1, isVolatile);
389  }
390 
391 private:
392  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
393  getBufferForFileImpl(StringRef Filename, int64_t FileSize, bool isVolatile);
394 
395 public:
396  /// Get the 'stat' information for the given \p Path.
397  ///
398  /// If the path is relative, it will be resolved against the WorkingDir of the
399  /// FileManager's FileSystemOptions.
400  ///
401  /// \returns a \c std::error_code describing an error, if there was one
402  std::error_code getNoncachedStatValue(StringRef Path,
403  llvm::vfs::Status &Result);
404 
405  /// If path is not absolute and FileSystemOptions set the working
406  /// directory, the path is modified to be relative to the given
407  /// working directory.
408  /// \returns true if \c path changed.
409  bool FixupRelativePath(SmallVectorImpl<char> &path) const;
410 
411  /// Makes \c Path absolute taking into account FileSystemOptions and the
412  /// working directory option.
413  /// \returns true if \c Path changed to absolute.
414  bool makeAbsolutePath(SmallVectorImpl<char> &Path) const;
415 
416  /// Produce an array mapping from the unique IDs assigned to each
417  /// file to the corresponding FileEntry pointer.
418  void GetUniqueIDMapping(
419  SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
420 
421  /// Retrieve the canonical name for a given directory.
422  ///
423  /// This is a very expensive operation, despite its results being cached,
424  /// and should only be used when the physical layout of the file system is
425  /// required, which is (almost) never.
426  StringRef getCanonicalName(const DirectoryEntry *Dir);
427 
428  void PrintStats() const;
429 };
430 
431 } // end namespace clang
432 
433 #endif // LLVM_CLANG_BASIC_FILEMANAGER_H
StringRef tryGetRealPathName() const
Definition: FileManager.h:103
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:171
time_t getModificationTime() const
Definition: FileManager.h:108
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
void closeFile() const
Definition: FileManager.h:119
StringRef getName() const
Definition: FileManager.h:61
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager&#39;s...
Definition: FileManager.h:130
time_t getModificationTime() const
Definition: FileManager.h:150
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition: FileManager.h:107
llvm::Optional< DirectoryEntryRef > getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:292
const StringRef getName() const
Definition: FileManager.h:136
const DirectoryEntry & getDirEntry() const
Definition: FileManager.h:59
unsigned getUID() const
Definition: FileManager.h:106
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:348
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
FileEntryRef(StringRef Name, const FileEntry &Entry)
Definition: FileManager.h:133
bool isValid() const
Definition: FileManager.h:138
unsigned getUID() const
Definition: FileManager.h:144
StringRef Filename
Definition: Format.cpp:1756
size_t getNumUniqueRealFiles() const
Returns the number of unique real file entries cached by the file manager.
Definition: FileManager.h:275
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(StringRef Filename, bool isVolatile=false)
Definition: FileManager.h:387
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileManager.h:111
#define false
Definition: stdbool.h:17
StringRef getName() const
Definition: FileManager.h:102
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:356
bool isNamedPipe() const
Check whether the file is a named pipe (and thus can&#39;t be opened by the native FileManager methods)...
Definition: FileManager.h:117
off_t getSize() const
Definition: FileManager.h:142
bool operator<(const FileEntry &RHS) const
Definition: FileManager.h:113
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:78
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition: FileManager.h:146
const FileSystemOptions & getFileSystemOpts() const
Definition: FileManager.h:357
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Definition: FileManager.h:361
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS)
Definition: FileManager.h:155
bool isValid() const
Definition: FileManager.h:104
Dataflow Directional Tag Classes.
off_t getSize() const
Definition: FileManager.h:105
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Definition: FileManager.h:57
bool isOpenForTests() const
Definition: FileManager.h:125
friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS)
Definition: FileManager.h:152
Defines the clang::FileSystemOptions interface.
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:45
Keeps track of options that affect how file operations are performed.
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:359
StringRef getName() const
Definition: FileManager.h:52
const FileEntry & getFileEntry() const
Definition: FileManager.h:140