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