FileManager.h revision 7f1752797d4a4344b539ad9452471871c0eec64b
1//===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_FILEMANAGER_H
15#define LLVM_CLANG_FILEMANAGER_H
16
17#include "llvm/ADT/StringMap.h"
18#include "llvm/Bitcode/SerializationFwd.h"
19#include <map>
20#include <set>
21#include <string>
22// FIXME: Enhance libsystem to support inode and other fields in stat.
23#include <sys/types.h>
24
25namespace clang {
26class FileManager;
27
28/// DirectoryEntry - Cached information about one directory on the disk.
29///
30class DirectoryEntry {
31  const char *Name;   // Name of the directory.
32  friend class FileManager;
33public:
34  DirectoryEntry() : Name(0) {}
35  const char *getName() const { return Name; }
36};
37
38/// FileEntry - Cached information about one file on the disk.
39///
40class FileEntry {
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;
49public:
50  FileEntry(dev_t device, ino_t inode) : Name(0), Device(device), Inode(inode){}
51
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; }
58
59  /// getDir - Return the directory the file lives in.
60  ///
61  const DirectoryEntry *getDir() const { return Dir; }
62
63  bool operator<(const FileEntry& RHS) const {
64    return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
65  }
66};
67
68
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.
73///
74class FileManager {
75  /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
76  ///
77  std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
78  std::set<FileEntry> UniqueFiles;
79
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.
82  ///
83  llvm::StringMap<DirectoryEntry*> DirEntries;
84  llvm::StringMap<FileEntry*> FileEntries;
85
86  /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
87  ///
88  unsigned NextFileUID;
89
90  // Statistics.
91  unsigned NumDirLookups, NumFileLookups;
92  unsigned NumDirCacheMisses, NumFileCacheMisses;
93public:
94  FileManager() : DirEntries(64), FileEntries(64), NextFileUID(0) {
95    NumDirLookups = NumFileLookups = 0;
96    NumDirCacheMisses = NumFileCacheMisses = 0;
97  }
98
99  /// getDirectory - Lookup, cache, and verify the specified directory.  This
100  /// returns null if the directory doesn't exist.
101  ///
102  const DirectoryEntry *getDirectory(const std::string &Filename) {
103    return getDirectory(&Filename[0], &Filename[0] + Filename.size());
104  }
105  const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
106
107  /// getFile - Lookup, cache, and verify the specified file.  This returns null
108  /// if the file doesn't exist.
109  ///
110  const FileEntry *getFile(const std::string &Filename) {
111    return getFile(&Filename[0], &Filename[0] + Filename.size());
112  }
113  const FileEntry *getFile(const char *FilenameStart,
114                           const char *FilenameEnd);
115
116  void PrintStats() const;
117};
118
119}  // end namespace clang
120
121#endif
122