]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/Basic/FileManager.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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 //  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
29 typedef unsigned short mode_t;
30 #endif
31
32 struct stat;
33
34 namespace llvm {
35 class MemoryBuffer;
36 namespace sys { class Path; }
37 }
38
39 namespace clang {
40 class FileManager;
41 class FileSystemStatCache;
42   
43 /// DirectoryEntry - Cached information about one directory (either on
44 /// the disk or in the virtual file system).
45 ///
46 class DirectoryEntry {
47   const char *Name;   // Name of the directory.
48   friend class FileManager;
49 public:
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 ///
58 class 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   
73 public:
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 ///
113 class FileManager : public llvm::RefCountedBase<FileManager> {
114   FileSystemOptions FileSystemOpts;
115
116   class UniqueDirContainer;
117   class UniqueFileContainer;
118
119   /// UniqueRealDirs/UniqueRealFiles - Cache for existing real directories/files.
120   ///
121   UniqueDirContainer &UniqueRealDirs;
122   UniqueFileContainer &UniqueRealFiles;
123
124   /// \brief The virtual directories that we have allocated.  For each
125   /// virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
126   /// directories (foo/ and foo/bar/) here.
127   SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
128   /// \brief The virtual files that we have allocated.
129   SmallVector<FileEntry*, 4> VirtualFileEntries;
130
131   /// SeenDirEntries/SeenFileEntries - This is a cache that maps paths
132   /// to directory/file entries (either real or virtual) we have
133   /// looked up.  The actual Entries for real directories/files are
134   /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
135   /// for virtual directories/files are owned by
136   /// VirtualDirectoryEntries/VirtualFileEntries above.
137   ///
138   llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
139   llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
140
141   /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
142   ///
143   unsigned NextFileUID;
144
145   // Statistics.
146   unsigned NumDirLookups, NumFileLookups;
147   unsigned NumDirCacheMisses, NumFileCacheMisses;
148
149   // Caching.
150   llvm::OwningPtr<FileSystemStatCache> StatCache;
151
152   bool getStatValue(const char *Path, struct stat &StatBuf,
153                     int *FileDescriptor);
154
155   /// Add all ancestors of the given path (pointing to either a file
156   /// or a directory) as virtual directories.
157   void addAncestorsAsVirtualDirs(StringRef Path);
158
159 public:
160   FileManager(const FileSystemOptions &FileSystemOpts);
161   ~FileManager();
162
163   /// \brief Installs the provided FileSystemStatCache object within
164   /// the FileManager.
165   ///
166   /// Ownership of this object is transferred to the FileManager.
167   ///
168   /// \param statCache the new stat cache to install. Ownership of this
169   /// object is transferred to the FileManager.
170   ///
171   /// \param AtBeginning whether this new stat cache must be installed at the
172   /// beginning of the chain of stat caches. Otherwise, it will be added to
173   /// the end of the chain.
174   void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
175
176   /// \brief Removes the specified FileSystemStatCache object from the manager.
177   void removeStatCache(FileSystemStatCache *statCache);
178
179   /// getDirectory - Lookup, cache, and verify the specified directory
180   /// (real or virtual).  This returns NULL if the directory doesn't exist.
181   ///
182   /// \param CacheFailure If true and the file does not exist, we'll cache
183   /// the failure to find this file.
184   const DirectoryEntry *getDirectory(StringRef DirName,
185                                      bool CacheFailure = true);
186
187   /// \brief Lookup, cache, and verify the specified file (real or
188   /// virtual).  This returns NULL if the file doesn't exist.
189   ///
190   /// \param OpenFile if true and the file exists, it will be opened.
191   ///
192   /// \param CacheFailure If true and the file does not exist, we'll cache
193   /// the failure to find this file.
194   const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
195                            bool CacheFailure = true);
196
197   /// \brief Returns the current file system options
198   const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
199
200   /// \brief Retrieve a file entry for a "virtual" file that acts as
201   /// if there were a file with the given name on disk. The file
202   /// itself is not accessed.
203   const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
204                                   time_t ModificationTime);
205
206   /// \brief Open the specified file as a MemoryBuffer, returning a new
207   /// MemoryBuffer if successful, otherwise returning null.
208   llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
209                                        std::string *ErrorStr = 0);
210   llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
211                                        std::string *ErrorStr = 0);
212
213   // getNoncachedStatValue - Will get the 'stat' information for the given path.
214   // If the path is relative, it will be resolved against the WorkingDir of the
215   // FileManager's FileSystemOptions.
216   bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf);
217
218   /// \brief If path is not absolute and FileSystemOptions set the working
219   /// directory, the path is modified to be relative to the given
220   /// working directory.
221   void FixupRelativePath(SmallVectorImpl<char> &path) const;
222
223   /// \brief Produce an array mapping from the unique IDs assigned to each
224   /// file to the corresponding FileEntry pointer.
225   void GetUniqueIDMapping(
226                     SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
227   
228   void PrintStats() const;
229 };
230
231 }  // end namespace clang
232
233 #endif