FileManager.h revision 5f016e2cb5d11daeb237544de1c5d59f20fe1a6e
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 <map>
19#include <string>
20// FIXME: Enhance libsystem to support inode and other fields in stat.
21#include <sys/types.h>
22
23namespace clang {
24class FileManager;
25
26/// DirectoryEntry - Cached information about one directory on the disk.
27///
28class DirectoryEntry {
29  const char *Name;   // Name of the directory.
30  friend class FileManager;
31public:
32  DirectoryEntry() : Name(0) {}
33  const char *getName() const { return Name; }
34};
35
36/// FileEntry - Cached information about one file on the disk.
37///
38class FileEntry {
39  const char *Name;           // Name of the directory.
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;
45public:
46  FileEntry() : Name(0) {}
47
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; }
52
53  /// getDir - Return the directory the file lives in.
54  ///
55  const DirectoryEntry *getDir() const { return Dir; }
56};
57
58
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.
63///
64class FileManager {
65  /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
66  ///
67  std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
68  std::map<std::pair<dev_t, ino_t>, FileEntry> UniqueFiles;
69
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.
72  ///
73  llvm::StringMap<DirectoryEntry*> DirEntries;
74  llvm::StringMap<FileEntry*> FileEntries;
75
76  /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
77  ///
78  unsigned NextFileUID;
79
80  // Statistics.
81  unsigned NumDirLookups, NumFileLookups;
82  unsigned NumDirCacheMisses, NumFileCacheMisses;
83public:
84  FileManager() : DirEntries(64), FileEntries(64), NextFileUID(0) {
85    NumDirLookups = NumFileLookups = 0;
86    NumDirCacheMisses = NumFileCacheMisses = 0;
87  }
88
89  /// getDirectory - Lookup, cache, and verify the specified directory.  This
90  /// returns null if the directory doesn't exist.
91  ///
92  const DirectoryEntry *getDirectory(const std::string &Filename) {
93    return getDirectory(&Filename[0], &Filename[0] + Filename.size());
94  }
95  const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
96
97  /// getFile - Lookup, cache, and verify the specified file.  This returns null
98  /// if the file doesn't exist.
99  ///
100  const FileEntry *getFile(const std::string &Filename) {
101    return getFile(&Filename[0], &Filename[0] + Filename.size());
102  }
103  const FileEntry *getFile(const char *FilenameStart,
104                           const char *FilenameEnd);
105
106  void PrintStats() const;
107};
108
109}  // end namespace clang
110
111#endif
112