FileManager.h revision 96438f319bb07d9a40564b5e01333f82c0c8a61e
1//===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/ADT/OwningPtr.h"
19#include "llvm/Bitcode/SerializationFwd.h"
20#include "llvm/Support/Allocator.h"
21#include <map>
22#include <set>
23#include <string>
24// FIXME: Enhance libsystem to support inode and other fields in stat.
25#include <sys/types.h>
26#include <sys/stat.h>
27
28namespace clang {
29class FileManager;
30
31/// DirectoryEntry - Cached information about one directory on the disk.
32///
33class DirectoryEntry {
34  const char *Name;   // Name of the directory.
35  friend class FileManager;
36public:
37  DirectoryEntry() : Name(0) {}
38  const char *getName() const { return Name; }
39};
40
41/// FileEntry - Cached information about one file on the disk.
42///
43class FileEntry {
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;
53public:
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) {}
58
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; }
66
67  /// getDir - Return the directory the file lives in.
68  ///
69  const DirectoryEntry *getDir() const { return Dir; }
70
71  bool operator<(const FileEntry& RHS) const {
72    return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
73  }
74};
75
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.
79class StatSysCallCache {
80public:
81  virtual ~StatSysCallCache() {}
82  virtual int stat(const char *path, struct stat *buf) = 0;
83};
84
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.
89///
90class FileManager {
91
92  class UniqueDirContainer;
93  class UniqueFileContainer;
94
95  /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
96  ///
97  UniqueDirContainer &UniqueDirs;
98  UniqueFileContainer &UniqueFiles;
99
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.
102  ///
103  llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
104  llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
105
106  /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
107  ///
108  unsigned NextFileUID;
109
110  // Statistics.
111  unsigned NumDirLookups, NumFileLookups;
112  unsigned NumDirCacheMisses, NumFileCacheMisses;
113
114  // Caching.
115  llvm::OwningPtr<StatSysCallCache> StatCache;
116
117  int stat_cached(const char* path, struct stat* buf) {
118    return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
119  }
120
121public:
122  FileManager();
123  ~FileManager();
124
125  /// setStatCache - Installs the provided StatSysCallCache object within
126  ///  the FileManager.  Ownership of this object is transferred to the
127  ///  FileManager.
128  void setStatCache(StatSysCallCache *statCache) { StatCache.reset(statCache); }
129
130  /// getDirectory - Lookup, cache, and verify the specified directory.  This
131  /// returns null if the directory doesn't exist.
132  ///
133  const DirectoryEntry *getDirectory(const std::string &Filename) {
134    return getDirectory(&Filename[0], &Filename[0] + Filename.size());
135  }
136  const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
137
138  /// getFile - Lookup, cache, and verify the specified file.  This returns null
139  /// if the file doesn't exist.
140  ///
141  const FileEntry *getFile(const std::string &Filename) {
142    return getFile(&Filename[0], &Filename[0] + Filename.size());
143  }
144  const FileEntry *getFile(const char *FilenameStart,
145                           const char *FilenameEnd);
146
147  void PrintStats() const;
148};
149
150}  // end namespace clang
151
152#endif
153