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/OwningPtr.h"
19 #include "llvm/Bitcode/SerializationFwd.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Config/config.h" // for mode_t
25 // FIXME: Enhance libsystem to support inode and other fields in stat.
26 #include <sys/types.h>
32 /// DirectoryEntry - Cached information about one directory on the disk.
34 class DirectoryEntry {
35 const char *Name; // Name of the directory.
36 friend class FileManager;
38 DirectoryEntry() : Name(0) {}
39 const char *getName() const { return Name; }
42 /// FileEntry - Cached information about one file on the disk.
45 const char *Name; // Name of the file.
46 off_t Size; // File size in bytes.
47 time_t ModTime; // Modification time of file.
48 const DirectoryEntry *Dir; // Directory file lives in.
49 unsigned UID; // A unique (small) ID for the file.
50 dev_t Device; // ID for the device containing the file.
51 ino_t Inode; // Inode number for the file.
52 mode_t FileMode; // The file mode as returned by 'stat'.
53 friend class FileManager;
55 FileEntry(dev_t device, ino_t inode, mode_t m)
56 : Name(0), Device(device), Inode(inode), FileMode(m) {}
57 // Add a default constructor for use with llvm::StringMap
58 FileEntry() : Name(0), Device(0), Inode(0), FileMode(0) {}
60 const char *getName() const { return Name; }
61 off_t getSize() const { return Size; }
62 unsigned getUID() const { return UID; }
63 ino_t getInode() const { return Inode; }
64 dev_t getDevice() const { return Device; }
65 time_t getModificationTime() const { return ModTime; }
66 mode_t getFileMode() const { return FileMode; }
68 /// getDir - Return the directory the file lives in.
70 const DirectoryEntry *getDir() const { return Dir; }
72 bool operator<(const FileEntry& RHS) const {
73 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
77 // FIXME: This is a lightweight shim that is used by FileManager to cache
78 // 'stat' system calls. We will use it with PTH to identify if caching
79 // stat calls in PTH files is a performance win.
80 class StatSysCallCache {
82 virtual ~StatSysCallCache() {}
83 virtual int stat(const char *path, struct stat *buf) = 0;
86 /// FileManager - Implements support for file system lookup, file system
87 /// caching, and directory search management. This also handles more advanced
88 /// properties, such as uniquing files based on "inode", so that a file with two
89 /// names (e.g. symlinked) will be treated as a single file.
93 class UniqueDirContainer;
94 class UniqueFileContainer;
96 /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
98 UniqueDirContainer &UniqueDirs;
99 UniqueFileContainer &UniqueFiles;
101 /// DirEntries/FileEntries - This is a cache of directory/file entries we have
102 /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
104 llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
105 llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
107 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
109 unsigned NextFileUID;
112 unsigned NumDirLookups, NumFileLookups;
113 unsigned NumDirCacheMisses, NumFileCacheMisses;
116 llvm::OwningPtr<StatSysCallCache> StatCache;
118 int stat_cached(const char* path, struct stat* buf) {
119 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
126 /// setStatCache - Installs the provided StatSysCallCache object within
127 /// the FileManager. Ownership of this object is transferred to the
129 void setStatCache(StatSysCallCache *statCache) {
130 StatCache.reset(statCache);
133 /// getDirectory - Lookup, cache, and verify the specified directory. This
134 /// returns null if the directory doesn't exist.
136 const DirectoryEntry *getDirectory(const std::string &Filename) {
137 return getDirectory(&Filename[0], &Filename[0] + Filename.size());
139 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
141 /// getFile - Lookup, cache, and verify the specified file. This returns null
142 /// if the file doesn't exist.
144 const FileEntry *getFile(const std::string &Filename) {
145 return getFile(&Filename[0], &Filename[0] + Filename.size());
147 const FileEntry *getFile(const char *FilenameStart,
148 const char *FilenameEnd);
150 void PrintStats() const;
153 } // end namespace clang