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 // FIXME: This is a lightweight shim that is used by FileManager to cache
75 // 'stat' system calls. We will use it with PTH to identify if caching
76 // stat calls in PTH files is a performance win.
77 class StatSysCallCache {
79 virtual ~StatSysCallCache() {}
80 virtual int stat(const char *path, struct stat *buf) = 0;
83 /// \brief A stat listener that can be used by FileManager to keep
84 /// track of the results of stat() calls that occur throughout the
85 /// execution of the front end.
86 class MemorizeStatCalls : public StatSysCallCache {
88 /// \brief The result of a stat() call.
90 /// The first member is the result of calling stat(). If stat()
91 /// found something, the second member is a copy of the stat
93 typedef std::pair<int, struct stat> StatResult;
95 /// \brief The set of stat() calls that have been
96 llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
98 typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
101 iterator begin() const { return StatCalls.begin(); }
102 iterator end() const { return StatCalls.end(); }
104 virtual int stat(const char *path, struct stat *buf);
107 /// FileManager - Implements support for file system lookup, file system
108 /// caching, and directory search management. This also handles more advanced
109 /// properties, such as uniquing files based on "inode", so that a file with two
110 /// names (e.g. symlinked) will be treated as a single file.
114 class UniqueDirContainer;
115 class UniqueFileContainer;
117 /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
119 UniqueDirContainer &UniqueDirs;
120 UniqueFileContainer &UniqueFiles;
122 /// DirEntries/FileEntries - This is a cache of directory/file entries we have
123 /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
125 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
126 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
128 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
130 unsigned NextFileUID;
133 unsigned NumDirLookups, NumFileLookups;
134 unsigned NumDirCacheMisses, NumFileCacheMisses;
137 llvm::OwningPtr<StatSysCallCache> StatCache;
139 int stat_cached(const char* path, struct stat* buf) {
140 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
147 /// setStatCache - Installs the provided StatSysCallCache object within
148 /// the FileManager. Ownership of this object is transferred to the
150 void setStatCache(StatSysCallCache *statCache) {
151 StatCache.reset(statCache);
154 /// getDirectory - Lookup, cache, and verify the specified directory. This
155 /// returns null if the directory doesn't exist.
157 const DirectoryEntry *getDirectory(const llvm::StringRef &Filename) {
158 return getDirectory(Filename.begin(), Filename.end());
160 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
162 /// getFile - Lookup, cache, and verify the specified file. This returns null
163 /// if the file doesn't exist.
165 const FileEntry *getFile(const llvm::StringRef &Filename) {
166 return getFile(Filename.begin(), Filename.end());
168 const FileEntry *getFile(const char *FilenameStart,
169 const char *FilenameEnd);
171 void PrintStats() const;
174 } // end namespace clang