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