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
17#include "clang/Basic/FileSystemOptions.h"
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
33namespace llvm {
34
35class MemoryBuffer;
36
37} // end namespace llvm
38
39namespace clang {
40
41class FileSystemStatCache;
42
43/// Cached information about one directory (either on disk or in
44/// the virtual file system).
45class DirectoryEntry {
46 friend class FileManager;
47
48 StringRef Name; // Name of the directory.
49
50public:
51 StringRef getName() const { return Name; }
52};
53
54/// Cached information about one file (either on disk
55/// or in the virtual file system).
56///
57/// If the 'File' member is valid, then this FileEntry has an open file
58/// descriptor for the file.
59class FileEntry {
60 friend class FileManager;
61
62 StringRef Name; // Name of the file.
63 std::string RealPathName; // Real path to the file; could be empty.
64 off_t Size; // File size in bytes.
65 time_t ModTime; // Modification time of file.
66 const DirectoryEntry *Dir; // Directory file lives in.
67 unsigned UID; // A unique (small) ID for the file.
68 llvm::sys::fs::UniqueID UniqueID;
69 bool IsNamedPipe;
70 bool InPCH;
71 bool IsValid; // Is this \c FileEntry initialized and valid?
72
73 /// The open file, if it is owned by the \p FileEntry.
74 mutable std::unique_ptr<llvm::vfs::File> File;
75
76public:
77 FileEntry()
78 : UniqueID(0, 0), IsNamedPipe(false), InPCH(false), IsValid(false)
79 {}
80
81 FileEntry(const FileEntry &) = delete;
82 FileEntry &operator=(const FileEntry &) = delete;
83
84 StringRef getName() const { return Name; }
85 StringRef tryGetRealPathName() const { return RealPathName; }
86 bool isValid() const { return IsValid; }
87 off_t getSize() const { return Size; }
88 unsigned getUID() const { return UID; }
89 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
90 bool isInPCH() const { return InPCH; }
91 time_t getModificationTime() const { return ModTime; }
92
93 /// Return the directory the file lives in.
94 const DirectoryEntry *getDir() const { return Dir; }
95
96 bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
97
98 /// Check whether the file is a named pipe (and thus can't be opened by
99 /// the native FileManager methods).
100 bool isNamedPipe() const { return IsNamedPipe; }
101
102 void closeFile() const {
103 File.reset(); // rely on destructor to close File
104 }
105
106 // Only for use in tests to see if deferred opens are happening, rather than
107 // relying on RealPathName being empty.
108 bool isOpenForTests() const { return File != nullptr; }
109};
110
111struct FileData;
112
113/// Implements support for file system lookup, file system caching,
114/// and directory search management.
115///
116/// This also handles more advanced properties, such as uniquing files based
117/// on "inode", so that a file with two names (e.g. symlinked) will be treated
118/// as a single file.
119///
120class FileManager : public RefCountedBase<FileManager> {
121 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
122 FileSystemOptions FileSystemOpts;
123
124 /// Cache for existing real directories.
125 std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueRealDirs;
126
127 /// Cache for existing real files.
128 std::map<llvm::sys::fs::UniqueID, FileEntry> UniqueRealFiles;
129
130 /// The virtual directories that we have allocated.
131 ///
132 /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
133 /// directories (foo/ and foo/bar/) here.
134 SmallVector<std::unique_ptr<DirectoryEntry>, 4> VirtualDirectoryEntries;
135 /// The virtual files that we have allocated.
136 SmallVector<std::unique_ptr<FileEntry>, 4> VirtualFileEntries;
137
138 /// A cache that maps paths to directory entries (either real or
139 /// virtual) we have looked up
140 ///
141 /// The actual Entries for real directories/files are
142 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
143 /// for virtual directories/files are owned by
144 /// VirtualDirectoryEntries/VirtualFileEntries above.
145 ///
146 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
147
148 /// A cache that maps paths to file entries (either real or
149 /// virtual) we have looked up.
150 ///
151 /// \see SeenDirEntries
152 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
153
154 /// The canonical names of directories.
155 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames;
156
157 /// Storage for canonical names that we have computed.
158 llvm::BumpPtrAllocator CanonicalNameStorage;
159
160 /// Each FileEntry we create is assigned a unique ID #.
161 ///
162 unsigned NextFileUID;
163
164 // Statistics.
165 unsigned NumDirLookups, NumFileLookups;
166 unsigned NumDirCacheMisses, NumFileCacheMisses;
167
168 // Caching.
169 std::unique_ptr<FileSystemStatCache> StatCache;
170
171 bool getStatValue(StringRef Path, FileData &Data, bool isFile,
172 std::unique_ptr<llvm::vfs::File> *F);
173
174 /// Add all ancestors of the given path (pointing to either a file
175 /// or a directory) as virtual directories.
176 void addAncestorsAsVirtualDirs(StringRef Path);
177
178 /// Fills the RealPathName in file entry.
179 void fillRealPathName(FileEntry *UFE, llvm::StringRef FileName);
180
181public:
182 FileManager(const FileSystemOptions &FileSystemOpts,
183 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
184 ~FileManager();
185
186 /// Installs the provided FileSystemStatCache object within
187 /// the FileManager.
188 ///
189 /// Ownership of this object is transferred to the FileManager.
190 ///
191 /// \param statCache the new stat cache to install. Ownership of this
192 /// object is transferred to the FileManager.
193 void setStatCache(std::unique_ptr<FileSystemStatCache> statCache);
194
195 /// Removes the FileSystemStatCache object from the manager.
196 void clearStatCache();
197
198 /// Lookup, cache, and verify the specified directory (real or
199 /// virtual).
200 ///
201 /// This returns NULL if the directory doesn't exist.
202 ///
203 /// \param CacheFailure If true and the file does not exist, we'll cache
204 /// the failure to find this file.
205 const DirectoryEntry *getDirectory(StringRef DirName,
206 bool CacheFailure = true);
207
208 /// Lookup, cache, and verify the specified file (real or
209 /// virtual).
210 ///
211 /// This returns NULL if the file doesn't exist.
212 ///
213 /// \param OpenFile if true and the file exists, it will be opened.
214 ///
215 /// \param CacheFailure If true and the file does not exist, we'll cache
216 /// the failure to find this file.
217 const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
218 bool CacheFailure = true);
219
220 /// Returns the current file system options
221 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
222 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
223
224 IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVirtualFileSystem() const {
225 return FS;
226 }
227
228 /// Retrieve a file entry for a "virtual" file that acts as
229 /// if there were a file with the given name on disk.
230 ///
231 /// The file itself is not accessed.
232 const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
233 time_t ModificationTime);
234
235 /// Open the specified file as a MemoryBuffer, returning a new
236 /// MemoryBuffer if successful, otherwise returning null.
237 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
238 getBufferForFile(const FileEntry *Entry, bool isVolatile = false,
239 bool ShouldCloseOpenFile = true);
240 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
241 getBufferForFile(StringRef Filename, bool isVolatile = false);
242
243 /// Get the 'stat' information for the given \p Path.
244 ///
245 /// If the path is relative, it will be resolved against the WorkingDir of the
246 /// FileManager's FileSystemOptions.
247 ///
248 /// \returns false on success, true on error.
249 bool getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result);
250
251 /// Remove the real file \p Entry from the cache.
252 void invalidateCache(const FileEntry *Entry);
253
254 /// If path is not absolute and FileSystemOptions set the working
255 /// directory, the path is modified to be relative to the given
256 /// working directory.
257 /// \returns true if \c path changed.
258 bool FixupRelativePath(SmallVectorImpl<char> &path) const;
259
260 /// Makes \c Path absolute taking into account FileSystemOptions and the
261 /// working directory option.
262 /// \returns true if \c Path changed to absolute.
263 bool makeAbsolutePath(SmallVectorImpl<char> &Path) const;
264
265 /// Produce an array mapping from the unique IDs assigned to each
266 /// file to the corresponding FileEntry pointer.
267 void GetUniqueIDMapping(
268 SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
269
270 /// Modifies the size and modification time of a previously created
271 /// FileEntry. Use with caution.
272 static void modifyFileEntry(FileEntry *File, off_t Size,
273 time_t ModificationTime);
274
275 /// Retrieve the canonical name for a given directory.
276 ///
277 /// This is a very expensive operation, despite its results being cached,
278 /// and should only be used when the physical layout of the file system is
279 /// required, which is (almost) never.
280 StringRef getCanonicalName(const DirectoryEntry *Dir);
281
282 void PrintStats() const;
283};
284
285} // end namespace clang
286
287#endif // LLVM_CLANG_BASIC_FILEMANAGER_H
288