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