FileManager.h revision 6f42b62b6194f53bcbc349f5d17388e1936535d7
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// This file defines the FileManager interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_FILEMANAGER_H 15#define LLVM_CLANG_FILEMANAGER_H 16 17#include "clang/Basic/FileSystemOptions.h" 18#include "clang/Basic/LLVM.h" 19#include "llvm/ADT/IntrusiveRefCntPtr.h" 20#include "llvm/ADT/SmallVector.h" 21#include "llvm/ADT/StringMap.h" 22#include "llvm/ADT/StringRef.h" 23#include "llvm/ADT/OwningPtr.h" 24#include "llvm/Support/Allocator.h" 25// FIXME: Enhance libsystem to support inode and other fields in stat. 26#include <sys/types.h> 27 28#ifdef _MSC_VER 29typedef unsigned short mode_t; 30#endif 31 32struct stat; 33 34namespace llvm { 35class MemoryBuffer; 36namespace sys { class Path; } 37} 38 39namespace clang { 40class FileManager; 41class FileSystemStatCache; 42 43/// DirectoryEntry - Cached information about one directory (either on 44/// the disk or in the virtual file system). 45/// 46class DirectoryEntry { 47 const char *Name; // Name of the directory. 48 friend class FileManager; 49public: 50 DirectoryEntry() : Name(0) {} 51 const char *getName() const { return Name; } 52}; 53 54/// FileEntry - Cached information about one file (either on the disk 55/// or in the virtual file system). If the 'FD' member is valid, then 56/// this FileEntry has an open file descriptor for the file. 57/// 58class FileEntry { 59 const char *Name; // Name of the file. 60 off_t Size; // File size in bytes. 61 time_t ModTime; // Modification time of file. 62 const DirectoryEntry *Dir; // Directory file lives in. 63 unsigned UID; // A unique (small) ID for the file. 64 dev_t Device; // ID for the device containing the file. 65 ino_t Inode; // Inode number for the file. 66 mode_t FileMode; // The file mode as returned by 'stat'. 67 68 /// FD - The file descriptor for the file entry if it is opened and owned 69 /// by the FileEntry. If not, this is set to -1. 70 mutable int FD; 71 friend class FileManager; 72 73public: 74 FileEntry(dev_t device, ino_t inode, mode_t m) 75 : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {} 76 // Add a default constructor for use with llvm::StringMap 77 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {} 78 79 FileEntry(const FileEntry &FE) { 80 memcpy(this, &FE, sizeof(FE)); 81 assert(FD == -1 && "Cannot copy a file-owning FileEntry"); 82 } 83 84 void operator=(const FileEntry &FE) { 85 memcpy(this, &FE, sizeof(FE)); 86 assert(FD == -1 && "Cannot assign a file-owning FileEntry"); 87 } 88 89 ~FileEntry(); 90 91 const char *getName() const { return Name; } 92 off_t getSize() const { return Size; } 93 unsigned getUID() const { return UID; } 94 ino_t getInode() const { return Inode; } 95 dev_t getDevice() const { return Device; } 96 time_t getModificationTime() const { return ModTime; } 97 mode_t getFileMode() const { return FileMode; } 98 99 /// getDir - Return the directory the file lives in. 100 /// 101 const DirectoryEntry *getDir() const { return Dir; } 102 103 bool operator<(const FileEntry &RHS) const { 104 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode); 105 } 106}; 107 108/// FileManager - Implements support for file system lookup, file system 109/// caching, and directory search management. This also handles more advanced 110/// properties, such as uniquing files based on "inode", so that a file with two 111/// names (e.g. symlinked) will be treated as a single file. 112/// 113class FileManager : public llvm::RefCountedBase<FileManager> { 114 FileSystemOptions FileSystemOpts; 115 116 class UniqueDirContainer; 117 class UniqueFileContainer; 118 119 /// UniqueRealDirs/UniqueRealFiles - Cache for existing real 120 /// directories/files. 121 /// 122 UniqueDirContainer &UniqueRealDirs; 123 UniqueFileContainer &UniqueRealFiles; 124 125 /// \brief The virtual directories that we have allocated. For each 126 /// virtual file (e.g. foo/bar/baz.cpp), we add all of its parent 127 /// directories (foo/ and foo/bar/) here. 128 SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries; 129 /// \brief The virtual files that we have allocated. 130 SmallVector<FileEntry*, 4> VirtualFileEntries; 131 132 /// SeenDirEntries/SeenFileEntries - This is a cache that maps paths 133 /// to directory/file entries (either real or virtual) we have 134 /// looked up. The actual Entries for real directories/files are 135 /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries 136 /// for virtual directories/files are owned by 137 /// VirtualDirectoryEntries/VirtualFileEntries above. 138 /// 139 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries; 140 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries; 141 142 /// NextFileUID - Each FileEntry we create is assigned a unique ID #. 143 /// 144 unsigned NextFileUID; 145 146 // Statistics. 147 unsigned NumDirLookups, NumFileLookups; 148 unsigned NumDirCacheMisses, NumFileCacheMisses; 149 150 // Caching. 151 OwningPtr<FileSystemStatCache> StatCache; 152 153 bool getStatValue(const char *Path, struct stat &StatBuf, 154 int *FileDescriptor); 155 156 /// Add all ancestors of the given path (pointing to either a file 157 /// or a directory) as virtual directories. 158 void addAncestorsAsVirtualDirs(StringRef Path); 159 160public: 161 FileManager(const FileSystemOptions &FileSystemOpts); 162 ~FileManager(); 163 164 /// \brief Installs the provided FileSystemStatCache object within 165 /// the FileManager. 166 /// 167 /// Ownership of this object is transferred to the FileManager. 168 /// 169 /// \param statCache the new stat cache to install. Ownership of this 170 /// object is transferred to the FileManager. 171 /// 172 /// \param AtBeginning whether this new stat cache must be installed at the 173 /// beginning of the chain of stat caches. Otherwise, it will be added to 174 /// the end of the chain. 175 void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false); 176 177 /// \brief Removes the specified FileSystemStatCache object from the manager. 178 void removeStatCache(FileSystemStatCache *statCache); 179 180 /// getDirectory - Lookup, cache, and verify the specified directory 181 /// (real or virtual). This returns NULL if the directory doesn't exist. 182 /// 183 /// \param CacheFailure If true and the file does not exist, we'll cache 184 /// the failure to find this file. 185 const DirectoryEntry *getDirectory(StringRef DirName, 186 bool CacheFailure = true); 187 188 /// \brief Lookup, cache, and verify the specified file (real or 189 /// virtual). This returns NULL if the file doesn't exist. 190 /// 191 /// \param OpenFile if true and the file exists, it will be opened. 192 /// 193 /// \param CacheFailure If true and the file does not exist, we'll cache 194 /// the failure to find this file. 195 const FileEntry *getFile(StringRef Filename, bool OpenFile = false, 196 bool CacheFailure = true); 197 198 /// \brief Returns the current file system options 199 const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; } 200 201 /// \brief Retrieve a file entry for a "virtual" file that acts as 202 /// if there were a file with the given name on disk. The file 203 /// itself is not accessed. 204 const FileEntry *getVirtualFile(StringRef Filename, off_t Size, 205 time_t ModificationTime); 206 207 /// \brief Open the specified file as a MemoryBuffer, returning a new 208 /// MemoryBuffer if successful, otherwise returning null. 209 llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry, 210 std::string *ErrorStr = 0); 211 llvm::MemoryBuffer *getBufferForFile(StringRef Filename, 212 std::string *ErrorStr = 0); 213 214 // getNoncachedStatValue - Will get the 'stat' information for the given path. 215 // If the path is relative, it will be resolved against the WorkingDir of the 216 // FileManager's FileSystemOptions. 217 bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf); 218 219 /// \brief If path is not absolute and FileSystemOptions set the working 220 /// directory, the path is modified to be relative to the given 221 /// working directory. 222 void FixupRelativePath(SmallVectorImpl<char> &path) const; 223 224 /// \brief Produce an array mapping from the unique IDs assigned to each 225 /// file to the corresponding FileEntry pointer. 226 void GetUniqueIDMapping( 227 SmallVectorImpl<const FileEntry *> &UIDToFiles) const; 228 229 void PrintStats() const; 230}; 231 232} // end namespace clang 233 234#endif 235