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