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 // This file defines the FileManager interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_FILEMANAGER_H 00015 #define LLVM_CLANG_FILEMANAGER_H 00016 00017 #include "clang/Basic/FileSystemOptions.h" 00018 #include "clang/Basic/LLVM.h" 00019 #include "llvm/ADT/IntrusiveRefCntPtr.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include "llvm/ADT/StringMap.h" 00022 #include "llvm/ADT/StringRef.h" 00023 #include "llvm/ADT/OwningPtr.h" 00024 #include "llvm/Support/Allocator.h" 00025 // FIXME: Enhance libsystem to support inode and other fields in stat. 00026 #include <sys/types.h> 00027 00028 #ifdef _MSC_VER 00029 typedef unsigned short mode_t; 00030 #endif 00031 00032 struct stat; 00033 00034 namespace llvm { 00035 class MemoryBuffer; 00036 namespace sys { class Path; } 00037 } 00038 00039 namespace clang { 00040 class FileManager; 00041 class FileSystemStatCache; 00042 00043 /// DirectoryEntry - Cached information about one directory (either on 00044 /// the disk or in the virtual file system). 00045 /// 00046 class DirectoryEntry { 00047 const char *Name; // Name of the directory. 00048 friend class FileManager; 00049 public: 00050 DirectoryEntry() : Name(0) {} 00051 const char *getName() const { return Name; } 00052 }; 00053 00054 /// FileEntry - Cached information about one file (either on the disk 00055 /// or in the virtual file system). If the 'FD' member is valid, then 00056 /// this FileEntry has an open file descriptor for the file. 00057 /// 00058 class FileEntry { 00059 const char *Name; // Name of the file. 00060 off_t Size; // File size in bytes. 00061 time_t ModTime; // Modification time of file. 00062 const DirectoryEntry *Dir; // Directory file lives in. 00063 unsigned UID; // A unique (small) ID for the file. 00064 dev_t Device; // ID for the device containing the file. 00065 ino_t Inode; // Inode number for the file. 00066 mode_t FileMode; // The file mode as returned by 'stat'. 00067 00068 /// FD - The file descriptor for the file entry if it is opened and owned 00069 /// by the FileEntry. If not, this is set to -1. 00070 mutable int FD; 00071 friend class FileManager; 00072 00073 public: 00074 FileEntry(dev_t device, ino_t inode, mode_t m) 00075 : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {} 00076 // Add a default constructor for use with llvm::StringMap 00077 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {} 00078 00079 FileEntry(const FileEntry &FE) { 00080 memcpy(this, &FE, sizeof(FE)); 00081 assert(FD == -1 && "Cannot copy a file-owning FileEntry"); 00082 } 00083 00084 void operator=(const FileEntry &FE) { 00085 memcpy(this, &FE, sizeof(FE)); 00086 assert(FD == -1 && "Cannot assign a file-owning FileEntry"); 00087 } 00088 00089 ~FileEntry(); 00090 00091 const char *getName() const { return Name; } 00092 off_t getSize() const { return Size; } 00093 unsigned getUID() const { return UID; } 00094 ino_t getInode() const { return Inode; } 00095 dev_t getDevice() const { return Device; } 00096 time_t getModificationTime() const { return ModTime; } 00097 mode_t getFileMode() const { return FileMode; } 00098 00099 /// getDir - Return the directory the file lives in. 00100 /// 00101 const DirectoryEntry *getDir() const { return Dir; } 00102 00103 bool operator<(const FileEntry &RHS) const { 00104 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode); 00105 } 00106 }; 00107 00108 /// FileManager - Implements support for file system lookup, file system 00109 /// caching, and directory search management. This also handles more advanced 00110 /// properties, such as uniquing files based on "inode", so that a file with two 00111 /// names (e.g. symlinked) will be treated as a single file. 00112 /// 00113 class FileManager : public RefCountedBase<FileManager> { 00114 FileSystemOptions FileSystemOpts; 00115 00116 class UniqueDirContainer; 00117 class UniqueFileContainer; 00118 00119 /// UniqueRealDirs/UniqueRealFiles - Cache for existing real 00120 /// directories/files. 00121 /// 00122 UniqueDirContainer &UniqueRealDirs; 00123 UniqueFileContainer &UniqueRealFiles; 00124 00125 /// \brief The virtual directories that we have allocated. For each 00126 /// virtual file (e.g. foo/bar/baz.cpp), we add all of its parent 00127 /// directories (foo/ and foo/bar/) here. 00128 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries; 00129 /// \brief The virtual files that we have allocated. 00130 SmallVector<FileEntry*, 4> VirtualFileEntries; 00131 00132 /// SeenDirEntries/SeenFileEntries - This is a cache that maps paths 00133 /// to directory/file entries (either real or virtual) we have 00134 /// looked up. The actual Entries for real directories/files are 00135 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries 00136 /// for virtual directories/files are owned by 00137 /// VirtualDirectoryEntries/VirtualFileEntries above. 00138 /// 00139 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries; 00140 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries; 00141 00142 /// NextFileUID - Each FileEntry we create is assigned a unique ID #. 00143 /// 00144 unsigned NextFileUID; 00145 00146 // Statistics. 00147 unsigned NumDirLookups, NumFileLookups; 00148 unsigned NumDirCacheMisses, NumFileCacheMisses; 00149 00150 // Caching. 00151 OwningPtr<FileSystemStatCache> StatCache; 00152 00153 bool getStatValue(const char *Path, struct stat &StatBuf, 00154 int *FileDescriptor); 00155 00156 /// Add all ancestors of the given path (pointing to either a file 00157 /// or a directory) as virtual directories. 00158 void addAncestorsAsVirtualDirs(StringRef Path); 00159 00160 public: 00161 FileManager(const FileSystemOptions &FileSystemOpts); 00162 ~FileManager(); 00163 00164 /// \brief Installs the provided FileSystemStatCache object within 00165 /// the FileManager. 00166 /// 00167 /// Ownership of this object is transferred to the FileManager. 00168 /// 00169 /// \param statCache the new stat cache to install. Ownership of this 00170 /// object is transferred to the FileManager. 00171 /// 00172 /// \param AtBeginning whether this new stat cache must be installed at the 00173 /// beginning of the chain of stat caches. Otherwise, it will be added to 00174 /// the end of the chain. 00175 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false); 00176 00177 /// \brief Removes the specified FileSystemStatCache object from the manager. 00178 void removeStatCache(FileSystemStatCache *statCache); 00179 00180 /// getDirectory - Lookup, cache, and verify the specified directory 00181 /// (real or virtual). This returns NULL if the directory doesn't exist. 00182 /// 00183 /// \param CacheFailure If true and the file does not exist, we'll cache 00184 /// the failure to find this file. 00185 const DirectoryEntry *getDirectory(StringRef DirName, 00186 bool CacheFailure = true); 00187 00188 /// \brief Lookup, cache, and verify the specified file (real or 00189 /// virtual). This returns NULL if the file doesn't exist. 00190 /// 00191 /// \param OpenFile if true and the file exists, it will be opened. 00192 /// 00193 /// \param CacheFailure If true and the file does not exist, we'll cache 00194 /// the failure to find this file. 00195 const FileEntry *getFile(StringRef Filename, bool OpenFile = false, 00196 bool CacheFailure = true); 00197 00198 /// \brief Returns the current file system options 00199 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; } 00200 00201 /// \brief Retrieve a file entry for a "virtual" file that acts as 00202 /// if there were a file with the given name on disk. The file 00203 /// itself is not accessed. 00204 const FileEntry *getVirtualFile(StringRef Filename, off_t Size, 00205 time_t ModificationTime); 00206 00207 /// \brief Open the specified file as a MemoryBuffer, returning a new 00208 /// MemoryBuffer if successful, otherwise returning null. 00209 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry, 00210 std::string *ErrorStr = 0); 00211 llvm::MemoryBuffer *getBufferForFile(StringRef Filename, 00212 std::string *ErrorStr = 0); 00213 00214 // getNoncachedStatValue - Will get the 'stat' information for the given path. 00215 // If the path is relative, it will be resolved against the WorkingDir of the 00216 // FileManager's FileSystemOptions. 00217 bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf); 00218 00219 /// \brief If path is not absolute and FileSystemOptions set the working 00220 /// directory, the path is modified to be relative to the given 00221 /// working directory. 00222 void FixupRelativePath(SmallVectorImpl<char> &path) const; 00223 00224 /// \brief Produce an array mapping from the unique IDs assigned to each 00225 /// file to the corresponding FileEntry pointer. 00226 void GetUniqueIDMapping( 00227 SmallVectorImpl<const FileEntry *> &UIDToFiles) const; 00228 00229 /// \brief Modifies the size and modification time of a previously created 00230 /// FileEntry. Use with caution. 00231 static void modifyFileEntry(FileEntry *File, off_t Size, 00232 time_t ModificationTime); 00233 00234 void PrintStats() const; 00235 }; 00236 00237 } // end namespace clang 00238 00239 #endif