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