]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/tools/clang/include/clang/Basic/FileManager.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / tools / 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 /// \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 // FIXME: Enhance libsystem to support inode and other fields in stat.
28 #include <sys/types.h>
29
30 #ifdef _MSC_VER
31 typedef unsigned short mode_t;
32 #endif
33
34 struct stat;
35
36 namespace llvm {
37 class MemoryBuffer;
38 namespace sys { class Path; }
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   dev_t Device;               // ID for the device containing the file.
67   ino_t Inode;                // Inode number for the file.
68   mode_t FileMode;            // The file mode as returned by 'stat'.
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(dev_t device, ino_t inode, mode_t m)
77     : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
78   // Add a default constructor for use with llvm::StringMap
79   FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
80
81   FileEntry(const FileEntry &FE) {
82     memcpy(this, &FE, sizeof(FE));
83     assert(FD == -1 && "Cannot copy a file-owning FileEntry");
84   }
85
86   void operator=(const FileEntry &FE) {
87     memcpy(this, &FE, sizeof(FE));
88     assert(FD == -1 && "Cannot assign a file-owning FileEntry");
89   }
90
91   ~FileEntry();
92
93   const char *getName() const { return Name; }
94   off_t getSize() const { return Size; }
95   unsigned getUID() const { return UID; }
96   ino_t getInode() const { return Inode; }
97   dev_t getDevice() const { return Device; }
98   time_t getModificationTime() const { return ModTime; }
99   mode_t getFileMode() const { return FileMode; }
100
101   /// \brief Return the directory the file lives in.
102   const DirectoryEntry *getDir() const { return Dir; }
103
104   bool operator<(const FileEntry &RHS) const {
105     return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
106   }
107
108   /// \brief Check whether the file is a named pipe (and thus can't be opened by
109   /// the native FileManager methods).
110   bool isNamedPipe() const;
111 };
112
113 /// \brief Implements support for file system lookup, file system caching,
114 /// and directory search management.
115 ///
116 /// This also handles more advanced properties, such as uniquing files based
117 /// on "inode", so that a file with two names (e.g. symlinked) will be treated
118 /// as a single file.
119 ///
120 class FileManager : public RefCountedBase<FileManager> {
121   FileSystemOptions FileSystemOpts;
122
123   class UniqueDirContainer;
124   class UniqueFileContainer;
125
126   /// \brief Cache for existing real directories.
127   UniqueDirContainer &UniqueRealDirs;
128
129   /// \brief Cache for existing real files.
130   UniqueFileContainer &UniqueRealFiles;
131
132   /// \brief The virtual directories that we have allocated.
133   ///
134   /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
135   /// directories (foo/ and foo/bar/) here.
136   SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
137   /// \brief The virtual files that we have allocated.
138   SmallVector<FileEntry*, 4> VirtualFileEntries;
139
140   /// \brief A cache that maps paths to directory entries (either real or
141   /// virtual) we have looked up
142   ///
143   /// The actual Entries for real directories/files are
144   /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
145   /// for virtual directories/files are owned by
146   /// VirtualDirectoryEntries/VirtualFileEntries above.
147   ///
148   llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
149
150   /// \brief A cache that maps paths to file entries (either real or
151   /// virtual) we have looked up.
152   ///
153   /// \see SeenDirEntries
154   llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
155
156   /// \brief The canonical names of directories.
157   llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames;
158
159   /// \brief Storage for canonical names that we have computed.
160   llvm::BumpPtrAllocator CanonicalNameStorage;
161
162   /// \brief Each FileEntry we create is assigned a unique ID #.
163   ///
164   unsigned NextFileUID;
165
166   // Statistics.
167   unsigned NumDirLookups, NumFileLookups;
168   unsigned NumDirCacheMisses, NumFileCacheMisses;
169
170   // Caching.
171   OwningPtr<FileSystemStatCache> StatCache;
172
173   bool getStatValue(const char *Path, struct stat &StatBuf,
174                     bool isFile, int *FileDescriptor);
175
176   /// Add all ancestors of the given path (pointing to either a file
177   /// or a directory) as virtual directories.
178   void addAncestorsAsVirtualDirs(StringRef Path);
179
180 public:
181   FileManager(const FileSystemOptions &FileSystemOpts);
182   ~FileManager();
183
184   /// \brief Installs the provided FileSystemStatCache object within
185   /// the FileManager.
186   ///
187   /// Ownership of this object is transferred to the FileManager.
188   ///
189   /// \param statCache the new stat cache to install. Ownership of this
190   /// object is transferred to the FileManager.
191   ///
192   /// \param AtBeginning whether this new stat cache must be installed at the
193   /// beginning of the chain of stat caches. Otherwise, it will be added to
194   /// the end of the chain.
195   void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
196
197   /// \brief Removes the specified FileSystemStatCache object from the manager.
198   void removeStatCache(FileSystemStatCache *statCache);
199
200   /// \brief Removes all FileSystemStatCache objects from the manager.
201   void clearStatCaches();
202
203   /// \brief Lookup, cache, and verify the specified directory (real or
204   /// virtual).
205   ///
206   /// This returns NULL if the directory doesn't exist.
207   ///
208   /// \param CacheFailure If true and the file does not exist, we'll cache
209   /// the failure to find this file.
210   const DirectoryEntry *getDirectory(StringRef DirName,
211                                      bool CacheFailure = true);
212
213   /// \brief Lookup, cache, and verify the specified file (real or
214   /// virtual).
215   ///
216   /// This returns NULL if the file doesn't exist.
217   ///
218   /// \param OpenFile if true and the file exists, it will be opened.
219   ///
220   /// \param CacheFailure If true and the file does not exist, we'll cache
221   /// the failure to find this file.
222   const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
223                            bool CacheFailure = true);
224
225   /// \brief Returns the current file system options
226   const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
227
228   /// \brief Retrieve a file entry for a "virtual" file that acts as
229   /// if there were a file with the given name on disk.
230   ///
231   /// The file itself is not accessed.
232   const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
233                                   time_t ModificationTime);
234
235   /// \brief Open the specified file as a MemoryBuffer, returning a new
236   /// MemoryBuffer if successful, otherwise returning null.
237   llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
238                                        std::string *ErrorStr = 0,
239                                        bool isVolatile = false);
240   llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
241                                        std::string *ErrorStr = 0);
242
243   /// \brief Get the 'stat' information for the given \p Path.
244   ///
245   /// If the path is relative, it will be resolved against the WorkingDir of the
246   /// FileManager's FileSystemOptions.
247   bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf);
248
249   /// \brief Remove the real file \p Entry from the cache.
250   void invalidateCache(const FileEntry *Entry);
251
252   /// \brief If path is not absolute and FileSystemOptions set the working
253   /// directory, the path is modified to be relative to the given
254   /// working directory.
255   void FixupRelativePath(SmallVectorImpl<char> &path) const;
256
257   /// \brief Produce an array mapping from the unique IDs assigned to each
258   /// file to the corresponding FileEntry pointer.
259   void GetUniqueIDMapping(
260                     SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
261
262   /// \brief Modifies the size and modification time of a previously created
263   /// FileEntry. Use with caution.
264   static void modifyFileEntry(FileEntry *File, off_t Size,
265                               time_t ModificationTime);
266
267   /// \brief Retrieve the canonical name for a given directory.
268   ///
269   /// This is a very expensive operation, despite its results being cached,
270   /// and should only be used when the physical layout of the file system is
271   /// required, which is (almost) never.
272   StringRef getCanonicalName(const DirectoryEntry *Dir);
273
274   void PrintStats() const;
275 };
276
277 }  // end namespace clang
278
279 #endif