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