1 //===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/Bitcode/SerializationFwd.h"
22 // FIXME: Enhance libsystem to support inode and other fields in stat.
23 #include <sys/types.h>
28 /// DirectoryEntry - Cached information about one directory on the disk.
30 class DirectoryEntry {
31 const char *Name; // Name of the directory.
32 friend class FileManager;
34 DirectoryEntry() : Name(0) {}
35 const char *getName() const { return Name; }
38 /// FileEntry - Cached information about one file on the disk.
41 const char *Name; // Name of the file.
42 off_t Size; // File size in bytes.
43 time_t ModTime; // Modification time of file.
44 const DirectoryEntry *Dir; // Directory file lives in.
45 unsigned UID; // A unique (small) ID for the file.
46 dev_t Device; // ID for the device containing the file.
47 ino_t Inode; // Inode number for the file.
48 friend class FileManager;
50 FileEntry(dev_t device, ino_t inode) : Name(0), Device(device), Inode(inode){}
52 const char *getName() const { return Name; }
53 off_t getSize() const { return Size; }
54 unsigned getUID() const { return UID; }
55 ino_t getInode() const { return Inode; }
56 dev_t getDevice() const { return Device; }
57 time_t getModificationTime() const { return ModTime; }
59 /// getDir - Return the directory the file lives in.
61 const DirectoryEntry *getDir() const { return Dir; }
63 bool operator<(const FileEntry& RHS) const {
64 return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
69 /// FileManager - Implements support for file system lookup, file system
70 /// caching, and directory search management. This also handles more advanced
71 /// properties, such as uniquing files based on "inode", so that a file with two
72 /// names (e.g. symlinked) will be treated as a single file.
75 /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
77 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
78 std::set<FileEntry> UniqueFiles;
80 /// DirEntries/FileEntries - This is a cache of directory/file entries we have
81 /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
83 llvm::StringMap<DirectoryEntry*> DirEntries;
84 llvm::StringMap<FileEntry*> FileEntries;
86 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
91 unsigned NumDirLookups, NumFileLookups;
92 unsigned NumDirCacheMisses, NumFileCacheMisses;
94 FileManager() : DirEntries(64), FileEntries(64), NextFileUID(0) {
95 NumDirLookups = NumFileLookups = 0;
96 NumDirCacheMisses = NumFileCacheMisses = 0;
99 /// getDirectory - Lookup, cache, and verify the specified directory. This
100 /// returns null if the directory doesn't exist.
102 const DirectoryEntry *getDirectory(const std::string &Filename) {
103 return getDirectory(&Filename[0], &Filename[0] + Filename.size());
105 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
107 /// getFile - Lookup, cache, and verify the specified file. This returns null
108 /// if the file doesn't exist.
110 const FileEntry *getFile(const std::string &Filename) {
111 return getFile(&Filename[0], &Filename[0] + Filename.size());
113 const FileEntry *getFile(const char *FilenameStart,
114 const char *FilenameEnd);
116 void PrintStats() const;
119 } // end namespace clang