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