1 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FileManager interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FILEMANAGER_H
15 #define LLVM_CLANG_FILEMANAGER_H
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>
30 /// DirectoryEntry - Cached information about one directory on the disk.
32 class DirectoryEntry {
33 const char *Name; // Name of the directory.
34 friend class FileManager;
36 DirectoryEntry() : Name(0) {}
37 const char *getName() const { return Name; }
40 /// FileEntry - Cached information about one file on the disk.
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;
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) {}
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; }
66 /// getDir - Return the directory the file lives in.
68 const DirectoryEntry *getDir() const { return Dir; }
70 bool operator<(const FileEntry& RHS) const {
71 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
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 {
80 llvm::OwningPtr<StatSysCallCache> NextStatCache;
83 virtual ~StatSysCallCache() {}
84 virtual int stat(const char *path, struct stat *buf) {
85 if (getNextStatCache())
86 return getNextStatCache()->stat(path, buf);
88 return ::stat(path, buf);
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);
97 /// \brief Retrieve the next stat call cache in the chain.
98 StatSysCallCache *getNextStatCache() { return NextStatCache.get(); }
100 /// \brief Retrieve the next stat call cache in the chain, transferring
101 /// ownership of this cache (and, transitively, all of the remaining caches)
103 StatSysCallCache *takeNextStatCache() { return NextStatCache.take(); }
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 {
111 /// \brief The result of a stat() call.
113 /// The first member is the result of calling stat(). If stat()
114 /// found something, the second member is a copy of the stat
116 typedef std::pair<int, struct stat> StatResult;
118 /// \brief The set of stat() calls that have been
119 llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
121 typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
124 iterator begin() const { return StatCalls.begin(); }
125 iterator end() const { return StatCalls.end(); }
127 virtual int stat(const char *path, struct stat *buf);
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.
137 class UniqueDirContainer;
138 class UniqueFileContainer;
140 /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
142 UniqueDirContainer &UniqueDirs;
143 UniqueFileContainer &UniqueFiles;
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.
148 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
149 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
151 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
153 unsigned NextFileUID;
155 /// \brief The virtual files that we have allocated.
156 llvm::SmallVector<FileEntry *, 4> VirtualFileEntries;
159 unsigned NumDirLookups, NumFileLookups;
160 unsigned NumDirCacheMisses, NumFileCacheMisses;
163 llvm::OwningPtr<StatSysCallCache> StatCache;
165 int stat_cached(const char* path, struct stat* buf) {
166 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
173 /// \brief Installs the provided StatSysCallCache object within
176 /// Ownership of this object is transferred to the FileManager.
178 /// \param statCache the new stat cache to install. Ownership of this
179 /// object is transferred to the FileManager.
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);
186 /// \brief Removes the provided StatSysCallCache object from the file manager.
187 void removeStatCache(StatSysCallCache *statCache);
189 /// getDirectory - Lookup, cache, and verify the specified directory. This
190 /// returns null if the directory doesn't exist.
192 const DirectoryEntry *getDirectory(llvm::StringRef Filename) {
193 return getDirectory(Filename.begin(), Filename.end());
195 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
197 /// getFile - Lookup, cache, and verify the specified file. This returns null
198 /// if the file doesn't exist.
200 const FileEntry *getFile(llvm::StringRef Filename) {
201 return getFile(Filename.begin(), Filename.end());
203 const FileEntry *getFile(const char *FilenameStart,
204 const char *FilenameEnd);
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;
214 } // end namespace clang