Home | History | Annotate | Download | only in Basic
      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_FILEMANAGER_H
     16 #define LLVM_CLANG_FILEMANAGER_H
     17 
     18 #include "clang/Basic/FileSystemOptions.h"
     19 #include "clang/Basic/LLVM.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     22 #include "llvm/ADT/OwningPtr.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 "llvm/Support/FileSystem.h"
     28 // FIXME: Enhance libsystem to support inode and other fields in stat.
     29 #include <sys/types.h>
     30 
     31 #ifdef _MSC_VER
     32 typedef unsigned short mode_t;
     33 #endif
     34 
     35 struct stat;
     36 
     37 namespace llvm {
     38 class MemoryBuffer;
     39 }
     40 
     41 namespace clang {
     42 class FileManager;
     43 class FileSystemStatCache;
     44 
     45 /// \brief Cached information about one directory (either on disk or in
     46 /// the virtual file system).
     47 class DirectoryEntry {
     48   const char *Name;   // Name of the directory.
     49   friend class FileManager;
     50 public:
     51   DirectoryEntry() : Name(0) {}
     52   const char *getName() const { return Name; }
     53 };
     54 
     55 /// \brief Cached information about one file (either on disk
     56 /// or in the virtual file system).
     57 ///
     58 /// If the 'FD' member is valid, then this FileEntry has an open file
     59 /// descriptor for the file.
     60 class FileEntry {
     61   const char *Name;           // Name of the file.
     62   off_t Size;                 // File size in bytes.
     63   time_t ModTime;             // Modification time of file.
     64   const DirectoryEntry *Dir;  // Directory file lives in.
     65   unsigned UID;               // A unique (small) ID for the file.
     66   llvm::sys::fs::UniqueID UniqueID;
     67   bool IsNamedPipe;
     68   bool InPCH;
     69 
     70   /// FD - The file descriptor for the file entry if it is opened and owned
     71   /// by the FileEntry.  If not, this is set to -1.
     72   mutable int FD;
     73   friend class FileManager;
     74 
     75 public:
     76   FileEntry(llvm::sys::fs::UniqueID UniqueID, bool IsNamedPipe, bool InPCH)
     77       : Name(0), UniqueID(UniqueID), IsNamedPipe(IsNamedPipe), InPCH(InPCH),
     78         FD(-1) {}
     79   // Add a default constructor for use with llvm::StringMap
     80   FileEntry()
     81       : Name(0), UniqueID(0, 0), IsNamedPipe(false), InPCH(false), FD(-1) {}
     82 
     83   FileEntry(const FileEntry &FE) {
     84     memcpy(this, &FE, sizeof(FE));
     85     assert(FD == -1 && "Cannot copy a file-owning FileEntry");
     86   }
     87 
     88   void operator=(const FileEntry &FE) {
     89     memcpy(this, &FE, sizeof(FE));
     90     assert(FD == -1 && "Cannot assign a file-owning FileEntry");
     91   }
     92 
     93   ~FileEntry();
     94 
     95   const char *getName() const { return Name; }
     96   off_t getSize() const { return Size; }
     97   unsigned getUID() const { return UID; }
     98   const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
     99   bool isInPCH() const { return InPCH; }
    100   time_t getModificationTime() const { return ModTime; }
    101 
    102   /// \brief Return the directory the file lives in.
    103   const DirectoryEntry *getDir() const { return Dir; }
    104 
    105   bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
    106 
    107   /// \brief Check whether the file is a named pipe (and thus can't be opened by
    108   /// the native FileManager methods).
    109   bool isNamedPipe() const { return IsNamedPipe; }
    110 };
    111 
    112 struct FileData;
    113 
    114 /// \brief Implements support for file system lookup, file system caching,
    115 /// and directory search management.
    116 ///
    117 /// This also handles more advanced properties, such as uniquing files based
    118 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
    119 /// as a single file.
    120 ///
    121 class FileManager : public RefCountedBase<FileManager> {
    122   FileSystemOptions FileSystemOpts;
    123 
    124   class UniqueDirContainer;
    125   class UniqueFileContainer;
    126 
    127   /// \brief Cache for existing real directories.
    128   UniqueDirContainer &UniqueRealDirs;
    129 
    130   /// \brief Cache for existing real files.
    131   UniqueFileContainer &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   OwningPtr<FileSystemStatCache> StatCache;
    173 
    174   bool getStatValue(const char *Path, FileData &Data, bool isFile,
    175                     int *FileDescriptor);
    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   ~FileManager();
    184 
    185   /// \brief Installs the provided FileSystemStatCache object within
    186   /// the FileManager.
    187   ///
    188   /// Ownership of this object is transferred to the FileManager.
    189   ///
    190   /// \param statCache the new stat cache to install. Ownership of this
    191   /// object is transferred to the FileManager.
    192   ///
    193   /// \param AtBeginning whether this new stat cache must be installed at the
    194   /// beginning of the chain of stat caches. Otherwise, it will be added to
    195   /// the end of the chain.
    196   void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
    197 
    198   /// \brief Removes the specified FileSystemStatCache object from the manager.
    199   void removeStatCache(FileSystemStatCache *statCache);
    200 
    201   /// \brief Removes all FileSystemStatCache objects from the manager.
    202   void clearStatCaches();
    203 
    204   /// \brief Lookup, cache, and verify the specified directory (real or
    205   /// virtual).
    206   ///
    207   /// This returns NULL if the directory doesn't exist.
    208   ///
    209   /// \param CacheFailure If true and the file does not exist, we'll cache
    210   /// the failure to find this file.
    211   const DirectoryEntry *getDirectory(StringRef DirName,
    212                                      bool CacheFailure = true);
    213 
    214   /// \brief Lookup, cache, and verify the specified file (real or
    215   /// virtual).
    216   ///
    217   /// This returns NULL if the file doesn't exist.
    218   ///
    219   /// \param OpenFile if true and the file exists, it will be opened.
    220   ///
    221   /// \param CacheFailure If true and the file does not exist, we'll cache
    222   /// the failure to find this file.
    223   const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
    224                            bool CacheFailure = true);
    225 
    226   /// \brief Returns the current file system options
    227   const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
    228 
    229   /// \brief Retrieve a file entry for a "virtual" file that acts as
    230   /// if there were a file with the given name on disk.
    231   ///
    232   /// The file itself is not accessed.
    233   const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
    234                                   time_t ModificationTime);
    235 
    236   /// \brief Open the specified file as a MemoryBuffer, returning a new
    237   /// MemoryBuffer if successful, otherwise returning null.
    238   llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
    239                                        std::string *ErrorStr = 0,
    240                                        bool isVolatile = false);
    241   llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
    242                                        std::string *ErrorStr = 0);
    243 
    244   /// \brief Get the 'stat' information for the given \p Path.
    245   ///
    246   /// If the path is relative, it will be resolved against the WorkingDir of the
    247   /// FileManager's FileSystemOptions.
    248   bool getNoncachedStatValue(StringRef Path,
    249                              llvm::sys::fs::file_status &Result);
    250 
    251   /// \brief Remove the real file \p Entry from the cache.
    252   void invalidateCache(const FileEntry *Entry);
    253 
    254   /// \brief 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   void FixupRelativePath(SmallVectorImpl<char> &path) const;
    258 
    259   /// \brief Produce an array mapping from the unique IDs assigned to each
    260   /// file to the corresponding FileEntry pointer.
    261   void GetUniqueIDMapping(
    262                     SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
    263 
    264   /// \brief Modifies the size and modification time of a previously created
    265   /// FileEntry. Use with caution.
    266   static void modifyFileEntry(FileEntry *File, off_t Size,
    267                               time_t ModificationTime);
    268 
    269   /// \brief Retrieve the canonical name for a given directory.
    270   ///
    271   /// This is a very expensive operation, despite its results being cached,
    272   /// and should only be used when the physical layout of the file system is
    273   /// required, which is (almost) never.
    274   StringRef getCanonicalName(const DirectoryEntry *Dir);
    275 
    276   void PrintStats() const;
    277 };
    278 
    279 }  // end namespace clang
    280 
    281 #endif
    282