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"
20 // FIXME: Enhance libsystem to support inode and other fields in stat.
21 #include <sys/types.h>
26 /// DirectoryEntry - Cached information about one directory on the disk.
28 class DirectoryEntry {
29 const char *Name; // Name of the directory.
30 friend class FileManager;
32 DirectoryEntry() : Name(0) {}
33 const char *getName() const { return Name; }
36 /// FileEntry - Cached information about one file on the disk.
39 const char *Name; // Name of the file.
40 off_t Size; // File size in bytes.
41 time_t ModTime; // Modification time of file.
42 const DirectoryEntry *Dir; // Directory file lives in.
43 unsigned UID; // A unique (small) ID for the file.
44 friend class FileManager;
46 FileEntry() : Name(0) {}
48 const char *getName() const { return Name; }
49 off_t getSize() const { return Size; }
50 unsigned getUID() const { return UID; }
51 time_t getModificationTime() const { return ModTime; }
53 /// getDir - Return the directory the file lives in.
55 const DirectoryEntry *getDir() const { return Dir; }
59 /// FileManager - Implements support for file system lookup, file system
60 /// caching, and directory search management. This also handles more advanced
61 /// properties, such as uniquing files based on "inode", so that a file with two
62 /// names (e.g. symlinked) will be treated as a single file.
65 /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
67 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
68 std::map<std::pair<dev_t, ino_t>, FileEntry> UniqueFiles;
70 /// DirEntries/FileEntries - This is a cache of directory/file entries we have
71 /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
73 llvm::StringMap<DirectoryEntry*> DirEntries;
74 llvm::StringMap<FileEntry*> FileEntries;
76 /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
81 unsigned NumDirLookups, NumFileLookups;
82 unsigned NumDirCacheMisses, NumFileCacheMisses;
84 FileManager() : DirEntries(64), FileEntries(64), NextFileUID(0) {
85 NumDirLookups = NumFileLookups = 0;
86 NumDirCacheMisses = NumFileCacheMisses = 0;
89 /// getDirectory - Lookup, cache, and verify the specified directory. This
90 /// returns null if the directory doesn't exist.
92 const DirectoryEntry *getDirectory(const std::string &Filename) {
93 return getDirectory(&Filename[0], &Filename[0] + Filename.size());
95 const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
97 /// getFile - Lookup, cache, and verify the specified file. This returns null
98 /// if the file doesn't exist.
100 const FileEntry *getFile(const std::string &Filename) {
101 return getFile(&Filename[0], &Filename[0] + Filename.size());
103 const FileEntry *getFile(const char *FilenameStart,
104 const char *FilenameEnd);
106 void PrintStats() const;
109 } // end namespace clang