clang API Documentation
00001 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief Defines the clang::FileManager interface and associated types. 00012 /// 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_BASIC_FILEMANAGER_H 00016 #define LLVM_CLANG_BASIC_FILEMANAGER_H 00017 00018 #include "clang/Basic/FileSystemOptions.h" 00019 #include "clang/Basic/LLVM.h" 00020 #include "clang/Basic/VirtualFileSystem.h" 00021 #include "llvm/ADT/DenseMap.h" 00022 #include "llvm/ADT/IntrusiveRefCntPtr.h" 00023 #include "llvm/ADT/SmallVector.h" 00024 #include "llvm/ADT/StringMap.h" 00025 #include "llvm/ADT/StringRef.h" 00026 #include "llvm/Support/Allocator.h" 00027 #include <memory> 00028 // FIXME: Enhance libsystem to support inode and other fields in stat. 00029 #include <sys/types.h> 00030 #include <map> 00031 00032 #ifdef _MSC_VER 00033 typedef unsigned short mode_t; 00034 #endif 00035 00036 struct stat; 00037 00038 namespace llvm { 00039 class MemoryBuffer; 00040 } 00041 00042 namespace clang { 00043 class FileManager; 00044 class FileSystemStatCache; 00045 00046 /// \brief Cached information about one directory (either on disk or in 00047 /// the virtual file system). 00048 class DirectoryEntry { 00049 const char *Name; // Name of the directory. 00050 friend class FileManager; 00051 public: 00052 DirectoryEntry() : Name(nullptr) {} 00053 const char *getName() const { return Name; } 00054 }; 00055 00056 /// \brief Cached information about one file (either on disk 00057 /// or in the virtual file system). 00058 /// 00059 /// If the 'File' member is valid, then this FileEntry has an open file 00060 /// descriptor for the file. 00061 class FileEntry { 00062 const char *Name; // Name of the file. 00063 off_t Size; // File size in bytes. 00064 time_t ModTime; // Modification time of file. 00065 const DirectoryEntry *Dir; // Directory file lives in. 00066 unsigned UID; // A unique (small) ID for the file. 00067 llvm::sys::fs::UniqueID UniqueID; 00068 bool IsNamedPipe; 00069 bool InPCH; 00070 bool IsValid; // Is this \c FileEntry initialized and valid? 00071 00072 /// \brief The open file, if it is owned by the \p FileEntry. 00073 mutable std::unique_ptr<vfs::File> File; 00074 friend class FileManager; 00075 00076 void closeFile() const { 00077 File.reset(); // rely on destructor to close File 00078 } 00079 00080 void operator=(const FileEntry &) LLVM_DELETED_FUNCTION; 00081 00082 public: 00083 FileEntry() 00084 : UniqueID(0, 0), IsNamedPipe(false), InPCH(false), IsValid(false) 00085 {} 00086 00087 // FIXME: this is here to allow putting FileEntry in std::map. Once we have 00088 // emplace, we shouldn't need a copy constructor anymore. 00089 /// Intentionally does not copy fields that are not set in an uninitialized 00090 /// \c FileEntry. 00091 FileEntry(const FileEntry &FE) : UniqueID(FE.UniqueID), 00092 IsNamedPipe(FE.IsNamedPipe), InPCH(FE.InPCH), IsValid(FE.IsValid) { 00093 assert(!isValid() && "Cannot copy an initialized FileEntry"); 00094 } 00095 00096 const char *getName() const { return Name; } 00097 bool isValid() const { return IsValid; } 00098 off_t getSize() const { return Size; } 00099 unsigned getUID() const { return UID; } 00100 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; } 00101 bool isInPCH() const { return InPCH; } 00102 time_t getModificationTime() const { return ModTime; } 00103 00104 /// \brief Return the directory the file lives in. 00105 const DirectoryEntry *getDir() const { return Dir; } 00106 00107 bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; } 00108 00109 /// \brief Check whether the file is a named pipe (and thus can't be opened by 00110 /// the native FileManager methods). 00111 bool isNamedPipe() const { return IsNamedPipe; } 00112 }; 00113 00114 struct FileData; 00115 00116 /// \brief Implements support for file system lookup, file system caching, 00117 /// and directory search management. 00118 /// 00119 /// This also handles more advanced properties, such as uniquing files based 00120 /// on "inode", so that a file with two names (e.g. symlinked) will be treated 00121 /// as a single file. 00122 /// 00123 class FileManager : public RefCountedBase<FileManager> { 00124 IntrusiveRefCntPtr<vfs::FileSystem> FS; 00125 FileSystemOptions FileSystemOpts; 00126 00127 /// \brief Cache for existing real directories. 00128 std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueRealDirs; 00129 00130 /// \brief Cache for existing real files. 00131 std::map<llvm::sys::fs::UniqueID, FileEntry> UniqueRealFiles; 00132 00133 /// \brief The virtual directories that we have allocated. 00134 /// 00135 /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent 00136 /// directories (foo/ and foo/bar/) here. 00137 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries; 00138 /// \brief The virtual files that we have allocated. 00139 SmallVector<FileEntry*, 4> VirtualFileEntries; 00140 00141 /// \brief A cache that maps paths to directory entries (either real or 00142 /// virtual) we have looked up 00143 /// 00144 /// The actual Entries for real directories/files are 00145 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries 00146 /// for virtual directories/files are owned by 00147 /// VirtualDirectoryEntries/VirtualFileEntries above. 00148 /// 00149 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries; 00150 00151 /// \brief A cache that maps paths to file entries (either real or 00152 /// virtual) we have looked up. 00153 /// 00154 /// \see SeenDirEntries 00155 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries; 00156 00157 /// \brief The canonical names of directories. 00158 llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames; 00159 00160 /// \brief Storage for canonical names that we have computed. 00161 llvm::BumpPtrAllocator CanonicalNameStorage; 00162 00163 /// \brief Each FileEntry we create is assigned a unique ID #. 00164 /// 00165 unsigned NextFileUID; 00166 00167 // Statistics. 00168 unsigned NumDirLookups, NumFileLookups; 00169 unsigned NumDirCacheMisses, NumFileCacheMisses; 00170 00171 // Caching. 00172 std::unique_ptr<FileSystemStatCache> StatCache; 00173 00174 bool getStatValue(const char *Path, FileData &Data, bool isFile, 00175 std::unique_ptr<vfs::File> *F); 00176 00177 /// Add all ancestors of the given path (pointing to either a file 00178 /// or a directory) as virtual directories. 00179 void addAncestorsAsVirtualDirs(StringRef Path); 00180 00181 public: 00182 FileManager(const FileSystemOptions &FileSystemOpts, 00183 IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); 00184 ~FileManager(); 00185 00186 /// \brief Installs the provided FileSystemStatCache object within 00187 /// the FileManager. 00188 /// 00189 /// Ownership of this object is transferred to the FileManager. 00190 /// 00191 /// \param statCache the new stat cache to install. Ownership of this 00192 /// object is transferred to the FileManager. 00193 /// 00194 /// \param AtBeginning whether this new stat cache must be installed at the 00195 /// beginning of the chain of stat caches. Otherwise, it will be added to 00196 /// the end of the chain. 00197 void addStatCache(std::unique_ptr<FileSystemStatCache> statCache, 00198 bool AtBeginning = false); 00199 00200 /// \brief Removes the specified FileSystemStatCache object from the manager. 00201 void removeStatCache(FileSystemStatCache *statCache); 00202 00203 /// \brief Removes all FileSystemStatCache objects from the manager. 00204 void clearStatCaches(); 00205 00206 /// \brief Lookup, cache, and verify the specified directory (real or 00207 /// virtual). 00208 /// 00209 /// This returns NULL if the directory doesn't exist. 00210 /// 00211 /// \param CacheFailure If true and the file does not exist, we'll cache 00212 /// the failure to find this file. 00213 const DirectoryEntry *getDirectory(StringRef DirName, 00214 bool CacheFailure = true); 00215 00216 /// \brief Lookup, cache, and verify the specified file (real or 00217 /// virtual). 00218 /// 00219 /// This returns NULL if the file doesn't exist. 00220 /// 00221 /// \param OpenFile if true and the file exists, it will be opened. 00222 /// 00223 /// \param CacheFailure If true and the file does not exist, we'll cache 00224 /// the failure to find this file. 00225 const FileEntry *getFile(StringRef Filename, bool OpenFile = false, 00226 bool CacheFailure = true); 00227 00228 /// \brief Returns the current file system options 00229 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; } 00230 00231 IntrusiveRefCntPtr<vfs::FileSystem> getVirtualFileSystem() const { 00232 return FS; 00233 } 00234 00235 /// \brief Retrieve a file entry for a "virtual" file that acts as 00236 /// if there were a file with the given name on disk. 00237 /// 00238 /// The file itself is not accessed. 00239 const FileEntry *getVirtualFile(StringRef Filename, off_t Size, 00240 time_t ModificationTime); 00241 00242 /// \brief Open the specified file as a MemoryBuffer, returning a new 00243 /// MemoryBuffer if successful, otherwise returning null. 00244 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 00245 getBufferForFile(const FileEntry *Entry, bool isVolatile = false, 00246 bool ShouldCloseOpenFile = true); 00247 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 00248 getBufferForFile(StringRef Filename); 00249 00250 /// \brief Get the 'stat' information for the given \p Path. 00251 /// 00252 /// If the path is relative, it will be resolved against the WorkingDir of the 00253 /// FileManager's FileSystemOptions. 00254 /// 00255 /// \returns false on success, true on error. 00256 bool getNoncachedStatValue(StringRef Path, 00257 vfs::Status &Result); 00258 00259 /// \brief Remove the real file \p Entry from the cache. 00260 void invalidateCache(const FileEntry *Entry); 00261 00262 /// \brief If path is not absolute and FileSystemOptions set the working 00263 /// directory, the path is modified to be relative to the given 00264 /// working directory. 00265 void FixupRelativePath(SmallVectorImpl<char> &path) const; 00266 00267 /// \brief Produce an array mapping from the unique IDs assigned to each 00268 /// file to the corresponding FileEntry pointer. 00269 void GetUniqueIDMapping( 00270 SmallVectorImpl<const FileEntry *> &UIDToFiles) const; 00271 00272 /// \brief Modifies the size and modification time of a previously created 00273 /// FileEntry. Use with caution. 00274 static void modifyFileEntry(FileEntry *File, off_t Size, 00275 time_t ModificationTime); 00276 00277 /// \brief Retrieve the canonical name for a given directory. 00278 /// 00279 /// This is a very expensive operation, despite its results being cached, 00280 /// and should only be used when the physical layout of the file system is 00281 /// required, which is (almost) never. 00282 StringRef getCanonicalName(const DirectoryEntry *Dir); 00283 00284 void PrintStats() const; 00285 }; 00286 00287 } // end namespace clang 00288 00289 #endif