1 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::FileManager interface and associated types.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_FILEMANAGER_H
16 #define LLVM_CLANG_BASIC_FILEMANAGER_H
18 #include "clang/Basic/FileSystemOptions.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/VirtualFileSystem.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/IntrusiveRefCntPtr.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/Allocator.h"
36 class FileSystemStatCache;
38 /// \brief Cached information about one directory (either on disk or in
39 /// the virtual file system).
40 class DirectoryEntry {
41 const char *Name; // Name of the directory.
42 friend class FileManager;
44 DirectoryEntry() : Name(nullptr) {}
45 const char *getName() const { return Name; }
48 /// \brief Cached information about one file (either on disk
49 /// or in the virtual file system).
51 /// If the 'File' member is valid, then this FileEntry has an open file
52 /// descriptor for the file.
54 const char *Name; // Name of the file.
55 std::string RealPathName; // Real path to the file; could be empty.
56 off_t Size; // File size in bytes.
57 time_t ModTime; // Modification time of file.
58 const DirectoryEntry *Dir; // Directory file lives in.
59 unsigned UID; // A unique (small) ID for the file.
60 llvm::sys::fs::UniqueID UniqueID;
63 bool IsValid; // Is this \c FileEntry initialized and valid?
65 /// \brief The open file, if it is owned by the \p FileEntry.
66 mutable std::unique_ptr<vfs::File> File;
67 friend class FileManager;
69 void operator=(const FileEntry &) = delete;
73 : UniqueID(0, 0), IsNamedPipe(false), InPCH(false), IsValid(false)
76 // FIXME: this is here to allow putting FileEntry in std::map. Once we have
77 // emplace, we shouldn't need a copy constructor anymore.
78 /// Intentionally does not copy fields that are not set in an uninitialized
80 FileEntry(const FileEntry &FE) : UniqueID(FE.UniqueID),
81 IsNamedPipe(FE.IsNamedPipe), InPCH(FE.InPCH), IsValid(FE.IsValid) {
82 assert(!isValid() && "Cannot copy an initialized FileEntry");
85 const char *getName() const { return Name; }
86 StringRef tryGetRealPathName() const { return RealPathName; }
87 bool isValid() const { return IsValid; }
88 off_t getSize() const { return Size; }
89 unsigned getUID() const { return UID; }
90 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
91 bool isInPCH() const { return InPCH; }
92 time_t getModificationTime() const { return ModTime; }
94 /// \brief Return the directory the file lives in.
95 const DirectoryEntry *getDir() const { return Dir; }
97 bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
99 /// \brief Check whether the file is a named pipe (and thus can't be opened by
100 /// the native FileManager methods).
101 bool isNamedPipe() const { return IsNamedPipe; }
103 void closeFile() const {
104 File.reset(); // rely on destructor to close File
110 /// \brief Implements support for file system lookup, file system caching,
111 /// and directory search management.
113 /// This also handles more advanced properties, such as uniquing files based
114 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
115 /// as a single file.
117 class FileManager : public RefCountedBase<FileManager> {
118 IntrusiveRefCntPtr<vfs::FileSystem> FS;
119 FileSystemOptions FileSystemOpts;
121 /// \brief Cache for existing real directories.
122 std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueRealDirs;
124 /// \brief Cache for existing real files.
125 std::map<llvm::sys::fs::UniqueID, FileEntry> UniqueRealFiles;
127 /// \brief The virtual directories that we have allocated.
129 /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
130 /// directories (foo/ and foo/bar/) here.
131 SmallVector<std::unique_ptr<DirectoryEntry>, 4> VirtualDirectoryEntries;
132 /// \brief The virtual files that we have allocated.
133 SmallVector<std::unique_ptr<FileEntry>, 4> VirtualFileEntries;
135 /// \brief A cache that maps paths to directory entries (either real or
136 /// virtual) we have looked up
138 /// The actual Entries for real directories/files are
139 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
140 /// for virtual directories/files are owned by
141 /// VirtualDirectoryEntries/VirtualFileEntries above.
143 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
145 /// \brief A cache that maps paths to file entries (either real or
146 /// virtual) we have looked up.
148 /// \see SeenDirEntries
149 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
151 /// \brief The canonical names of directories.
152 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames;
154 /// \brief Storage for canonical names that we have computed.
155 llvm::BumpPtrAllocator CanonicalNameStorage;
157 /// \brief Each FileEntry we create is assigned a unique ID #.
159 unsigned NextFileUID;
162 unsigned NumDirLookups, NumFileLookups;
163 unsigned NumDirCacheMisses, NumFileCacheMisses;
166 std::unique_ptr<FileSystemStatCache> StatCache;
168 bool getStatValue(const char *Path, FileData &Data, bool isFile,
169 std::unique_ptr<vfs::File> *F);
171 /// Add all ancestors of the given path (pointing to either a file
172 /// or a directory) as virtual directories.
173 void addAncestorsAsVirtualDirs(StringRef Path);
176 FileManager(const FileSystemOptions &FileSystemOpts,
177 IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
180 /// \brief Installs the provided FileSystemStatCache object within
183 /// Ownership of this object is transferred to the FileManager.
185 /// \param statCache the new stat cache to install. Ownership of this
186 /// object is transferred to the FileManager.
188 /// \param AtBeginning whether this new stat cache must be installed at the
189 /// beginning of the chain of stat caches. Otherwise, it will be added to
190 /// the end of the chain.
191 void addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
192 bool AtBeginning = false);
194 /// \brief Removes the specified FileSystemStatCache object from the manager.
195 void removeStatCache(FileSystemStatCache *statCache);
197 /// \brief Removes all FileSystemStatCache objects from the manager.
198 void clearStatCaches();
200 /// \brief Lookup, cache, and verify the specified directory (real or
203 /// This returns NULL if the directory doesn't exist.
205 /// \param CacheFailure If true and the file does not exist, we'll cache
206 /// the failure to find this file.
207 const DirectoryEntry *getDirectory(StringRef DirName,
208 bool CacheFailure = true);
210 /// \brief Lookup, cache, and verify the specified file (real or
213 /// This returns NULL if the file doesn't exist.
215 /// \param OpenFile if true and the file exists, it will be opened.
217 /// \param CacheFailure If true and the file does not exist, we'll cache
218 /// the failure to find this file.
219 const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
220 bool CacheFailure = true);
222 /// \brief Returns the current file system options
223 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
224 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
226 IntrusiveRefCntPtr<vfs::FileSystem> getVirtualFileSystem() const {
230 /// \brief Retrieve a file entry for a "virtual" file that acts as
231 /// if there were a file with the given name on disk.
233 /// The file itself is not accessed.
234 const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
235 time_t ModificationTime);
237 /// \brief Open the specified file as a MemoryBuffer, returning a new
238 /// MemoryBuffer if successful, otherwise returning null.
239 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
240 getBufferForFile(const FileEntry *Entry, bool isVolatile = false,
241 bool ShouldCloseOpenFile = true);
242 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
243 getBufferForFile(StringRef Filename);
245 /// \brief Get the 'stat' information for the given \p Path.
247 /// If the path is relative, it will be resolved against the WorkingDir of the
248 /// FileManager's FileSystemOptions.
250 /// \returns false on success, true on error.
251 bool getNoncachedStatValue(StringRef Path,
252 vfs::Status &Result);
254 /// \brief Remove the real file \p Entry from the cache.
255 void invalidateCache(const FileEntry *Entry);
257 /// \brief If path is not absolute and FileSystemOptions set the working
258 /// directory, the path is modified to be relative to the given
259 /// working directory.
260 /// \returns true if \c path changed.
261 bool FixupRelativePath(SmallVectorImpl<char> &path) const;
263 /// Makes \c Path absolute taking into account FileSystemOptions and the
264 /// working directory option.
265 /// \returns true if \c Path changed to absolute.
266 bool makeAbsolutePath(SmallVectorImpl<char> &Path) const;
268 /// \brief Produce an array mapping from the unique IDs assigned to each
269 /// file to the corresponding FileEntry pointer.
270 void GetUniqueIDMapping(
271 SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
273 /// \brief Modifies the size and modification time of a previously created
274 /// FileEntry. Use with caution.
275 static void modifyFileEntry(FileEntry *File, off_t Size,
276 time_t ModificationTime);
278 /// \brief Retrieve the canonical name for a given directory.
280 /// This is a very expensive operation, despite its results being cached,
281 /// and should only be used when the physical layout of the file system is
282 /// required, which is (almost) never.
283 StringRef getCanonicalName(const DirectoryEntry *Dir);
285 void PrintStats() const;
288 } // end namespace clang