]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/FileManager.h
Merge ^vendor/binutils/dist@214082 into contrib/binutils.
[FreeBSD/FreeBSD.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 "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Config/config.h" // for mode_t
23 // FIXME: Enhance libsystem to support inode and other fields in stat.
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 namespace clang {
28 class FileManager;
29
30 /// DirectoryEntry - Cached information about one directory on the disk.
31 ///
32 class DirectoryEntry {
33   const char *Name;   // Name of the directory.
34   friend class FileManager;
35 public:
36   DirectoryEntry() : Name(0) {}
37   const char *getName() const { return Name; }
38 };
39
40 /// FileEntry - Cached information about one file on the disk.
41 ///
42 class FileEntry {
43   const char *Name;           // Name of the file.
44   off_t Size;                 // File size in bytes.
45   time_t ModTime;             // Modification time of file.
46   const DirectoryEntry *Dir;  // Directory file lives in.
47   unsigned UID;               // A unique (small) ID for the file.
48   dev_t Device;               // ID for the device containing the file.
49   ino_t Inode;                // Inode number for the file.
50   mode_t FileMode;            // The file mode as returned by 'stat'.
51   friend class FileManager;
52 public:
53   FileEntry(dev_t device, ino_t inode, mode_t m)
54     : Name(0), Device(device), Inode(inode), FileMode(m) {}
55   // Add a default constructor for use with llvm::StringMap
56   FileEntry() : Name(0), Device(0), Inode(0), FileMode(0) {}
57
58   const char *getName() const { return Name; }
59   off_t getSize() const { return Size; }
60   unsigned getUID() const { return UID; }
61   ino_t getInode() const { return Inode; }
62   dev_t getDevice() const { return Device; }
63   time_t getModificationTime() const { return ModTime; }
64   mode_t getFileMode() const { return FileMode; }
65
66   /// getDir - Return the directory the file lives in.
67   ///
68   const DirectoryEntry *getDir() const { return Dir; }
69
70   bool operator<(const FileEntry& RHS) const {
71     return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
72   }
73 };
74
75 /// \brief Abstract interface for introducing a FileManager cache for 'stat'
76 /// system calls, which is used by precompiled and pretokenized headers to
77 /// improve performance.
78 class StatSysCallCache {
79 protected:
80   llvm::OwningPtr<StatSysCallCache> NextStatCache;
81   
82 public:
83   virtual ~StatSysCallCache() {}
84   virtual int stat(const char *path, struct stat *buf) {
85     if (getNextStatCache())
86       return getNextStatCache()->stat(path, buf);
87     
88     return ::stat(path, buf);
89   }
90   
91   /// \brief Sets the next stat call cache in the chain of stat caches.
92   /// Takes ownership of the given stat cache.
93   void setNextStatCache(StatSysCallCache *Cache) {
94     NextStatCache.reset(Cache);
95   }
96   
97   /// \brief Retrieve the next stat call cache in the chain.
98   StatSysCallCache *getNextStatCache() { return NextStatCache.get(); }
99
100   /// \brief Retrieve the next stat call cache in the chain, transferring
101   /// ownership of this cache (and, transitively, all of the remaining caches)
102   /// to the caller.
103   StatSysCallCache *takeNextStatCache() { return NextStatCache.take(); }
104 };
105
106 /// \brief A stat "cache" that can be used by FileManager to keep
107 /// track of the results of stat() calls that occur throughout the
108 /// execution of the front end.
109 class MemorizeStatCalls : public StatSysCallCache {
110 public:
111   /// \brief The result of a stat() call.
112   ///
113   /// The first member is the result of calling stat(). If stat()
114   /// found something, the second member is a copy of the stat
115   /// structure.
116   typedef std::pair<int, struct stat> StatResult;
117
118   /// \brief The set of stat() calls that have been
119   llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
120
121   typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
122     iterator;
123
124   iterator begin() const { return StatCalls.begin(); }
125   iterator end() const { return StatCalls.end(); }
126
127   virtual int stat(const char *path, struct stat *buf);
128 };
129
130 /// FileManager - Implements support for file system lookup, file system
131 /// caching, and directory search management.  This also handles more advanced
132 /// properties, such as uniquing files based on "inode", so that a file with two
133 /// names (e.g. symlinked) will be treated as a single file.
134 ///
135 class FileManager {
136
137   class UniqueDirContainer;
138   class UniqueFileContainer;
139
140   /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
141   ///
142   UniqueDirContainer &UniqueDirs;
143   UniqueFileContainer &UniqueFiles;
144
145   /// DirEntries/FileEntries - This is a cache of directory/file entries we have
146   /// looked up.  The actual Entry is owned by UniqueFiles/UniqueDirs above.
147   ///
148   llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
149   llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
150
151   /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
152   ///
153   unsigned NextFileUID;
154
155   /// \brief The virtual files that we have allocated.
156   llvm::SmallVector<FileEntry *, 4> VirtualFileEntries;
157
158   // Statistics.
159   unsigned NumDirLookups, NumFileLookups;
160   unsigned NumDirCacheMisses, NumFileCacheMisses;
161
162   // Caching.
163   llvm::OwningPtr<StatSysCallCache> StatCache;
164
165   int stat_cached(const char* path, struct stat* buf) {
166     return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
167   }
168
169 public:
170   FileManager();
171   ~FileManager();
172
173   /// \brief Installs the provided StatSysCallCache object within
174   /// the FileManager. 
175   ///
176   /// Ownership of this object is transferred to the FileManager.
177   ///
178   /// \param statCache the new stat cache to install. Ownership of this
179   /// object is transferred to the FileManager.
180   ///
181   /// \param AtBeginning whether this new stat cache must be installed at the
182   /// beginning of the chain of stat caches. Otherwise, it will be added to
183   /// the end of the chain.
184   void addStatCache(StatSysCallCache *statCache, bool AtBeginning = false);
185
186   /// \brief Removes the provided StatSysCallCache object from the file manager.
187   void removeStatCache(StatSysCallCache *statCache);
188   
189   /// getDirectory - Lookup, cache, and verify the specified directory.  This
190   /// returns null if the directory doesn't exist.
191   ///
192   const DirectoryEntry *getDirectory(llvm::StringRef Filename) {
193     return getDirectory(Filename.begin(), Filename.end());
194   }
195   const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
196
197   /// getFile - Lookup, cache, and verify the specified file.  This returns null
198   /// if the file doesn't exist.
199   ///
200   const FileEntry *getFile(llvm::StringRef Filename) {
201     return getFile(Filename.begin(), Filename.end());
202   }
203   const FileEntry *getFile(const char *FilenameStart,
204                            const char *FilenameEnd);
205
206   /// \brief Retrieve a file entry for a "virtual" file that acts as
207   /// if there were a file with the given name on disk. The file
208   /// itself is not accessed.
209   const FileEntry *getVirtualFile(llvm::StringRef Filename, off_t Size,
210                                   time_t ModificationTime);
211   void PrintStats() const;
212 };
213
214 }  // end namespace clang
215
216 #endif