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/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>
29 /// DirectoryEntry - Cached information about one directory on the disk.
31 class DirectoryEntry {
32 const char *Name; // Name of the directory.
33 friend class FileManager;
35 DirectoryEntry() : Name(0) {}
36 const char *getName() const { return Name; }
39 /// FileEntry - Cached information about one file on the disk.
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;
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) {}
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; }
65 /// getDir - Return the directory the file lives in.
67 const DirectoryEntry *getDir() const { return Dir; }
69 bool operator<(const FileEntry& RHS) const {
70 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
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 {
79 llvm::OwningPtr<StatSysCallCache> NextStatCache;
82 virtual ~StatSysCallCache() {}
83 virtual int stat(const char *path, struct stat *buf) {
84 if (getNextStatCache())
85 return getNextStatCache()->stat(path, buf);
87 return ::stat(path, buf);
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);
96 /// \brief Retrieve the next stat call cache in the chain.
97 StatSysCallCache *getNextStatCache() { return NextStatCache.get(); }
99 /// \brief Retrieve the next stat call cache in the chain, transferring
100 /// ownership of this cache (and, transitively, all of the remaining caches)
102 StatSysCallCache *takeNextStatCache() { return NextStatCache.take(); }
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 {
110 /// \brief The result of a stat() call.
112 /// The first member is the result of calling stat(). If stat()
113 /// found something, the second member is a copy of the stat
115 typedef std::pair<int, struct stat> StatResult;
117 /// \brief The set of stat() calls that have been
118 llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
120 typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
123 iterator begin() const { return StatCalls.begin(); }
124 iterator end() const { return StatCalls.end(); }
126 virtual int stat(const char *path, struct stat *buf);
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.
136 class UniqueDirContainer;
137 class UniqueFileContainer;
139 /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
141 UniqueDirContainer &UniqueDirs;
142 UniqueFileContainer &UniqueFiles;
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.
147 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
148 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
150 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
152 unsigned NextFileUID;
155 unsigned NumDirLookups, NumFileLookups;
156 unsigned NumDirCacheMisses, NumFileCacheMisses;
159 llvm::OwningPtr<StatSysCallCache> StatCache;
161 int stat_cached(const char* path, struct stat* buf) {
162 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
169 /// \brief Installs the provided StatSysCallCache object within
172 /// Ownership of this object is transferred to the FileManager.
174 /// \param statCache the new stat cache to install. Ownership of this
175 /// object is transferred to the FileManager.
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);
182 /// \brief Removes the provided StatSysCallCache object from the file manager.
183 void removeStatCache(StatSysCallCache *statCache);
185 /// getDirectory - Lookup, cache, and verify the specified directory. This
186 /// returns null if the directory doesn't exist.
188 const DirectoryEntry *getDirectory(const llvm::StringRef &Filename) {
189 return getDirectory(Filename.begin(), Filename.end());
191 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
193 /// getFile - Lookup, cache, and verify the specified file. This returns null
194 /// if the file doesn't exist.
196 const FileEntry *getFile(const llvm::StringRef &Filename) {
197 return getFile(Filename.begin(), Filename.end());
199 const FileEntry *getFile(const char *FilenameStart,
200 const char *FilenameEnd);
202 void PrintStats() const;
205 } // end namespace clang