Coverage Report

Created: 2018-12-11 17:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Basic/FileManager.h
Line
Count
Source (jump to first uncovered line)
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
/// \file
11
/// Defines the clang::FileManager interface and associated types.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_BASIC_FILEMANAGER_H
16
#define LLVM_CLANG_BASIC_FILEMANAGER_H
17
18
#include "clang/Basic/FileSystemOptions.h"
19
#include "clang/Basic/LLVM.h"
20
#include "llvm/ADT/DenseMap.h"
21
#include "llvm/ADT/IntrusiveRefCntPtr.h"
22
#include "llvm/ADT/SmallVector.h"
23
#include "llvm/ADT/StringMap.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/Support/Allocator.h"
26
#include "llvm/Support/ErrorOr.h"
27
#include "llvm/Support/FileSystem.h"
28
#include "llvm/Support/VirtualFileSystem.h"
29
#include <ctime>
30
#include <map>
31
#include <memory>
32
#include <string>
33
34
namespace llvm {
35
36
class MemoryBuffer;
37
38
} // end namespace llvm
39
40
namespace clang {
41
42
class FileSystemStatCache;
43
44
/// Cached information about one directory (either on disk or in
45
/// the virtual file system).
46
class DirectoryEntry {
47
  friend class FileManager;
48
49
  StringRef Name; // Name of the directory.
50
51
public:
52
6.30M
  StringRef getName() const { return Name; }
53
};
54
55
/// Cached information about one file (either on disk
56
/// or in the virtual file system).
57
///
58
/// If the 'File' member is valid, then this FileEntry has an open file
59
/// descriptor for the file.
60
class FileEntry {
61
  friend class FileManager;
62
63
  StringRef Name;             // Name of the file.
64
  std::string RealPathName;   // Real path to the file; could be empty.
65
  off_t Size;                 // File size in bytes.
66
  time_t ModTime;             // Modification time of file.
67
  const DirectoryEntry *Dir;  // Directory file lives in.
68
  unsigned UID;               // A unique (small) ID for the file.
69
  llvm::sys::fs::UniqueID UniqueID;
70
  bool IsNamedPipe;
71
  bool InPCH;
72
  bool IsValid;               // Is this \c FileEntry initialized and valid?
73
  bool DeferredOpen;          // Created by getFile(OpenFile=0); may open later.
74
75
  /// The open file, if it is owned by the \p FileEntry.
76
  mutable std::unique_ptr<llvm::vfs::File> File;
77
78
public:
79
  FileEntry()
80
      : UniqueID(0, 0), IsNamedPipe(false), InPCH(false), IsValid(false),
81
779k
        DeferredOpen(false) {}
82
83
  FileEntry(const FileEntry &) = delete;
84
  FileEntry &operator=(const FileEntry &) = delete;
85
86
53.6M
  StringRef getName() const { return Name; }
87
2.68M
  StringRef tryGetRealPathName() const { return RealPathName; }
88
784k
  bool isValid() const { return IsValid; }
89
6.24M
  off_t getSize() const { return Size; }
90
11.7M
  unsigned getUID() const { return UID; }
91
141
  const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
92
0
  bool isInPCH() const { return InPCH; }
93
4.08M
  time_t getModificationTime() const { return ModTime; }
94
95
  /// Return the directory the file lives in.
96
1.37M
  const DirectoryEntry *getDir() const { return Dir; }
97
98
  bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
99
100
  /// Check whether the file is a named pipe (and thus can't be opened by
101
  /// the native FileManager methods).
102
49.4k
  bool isNamedPipe() const { return IsNamedPipe; }
103
104
666k
  void closeFile() const {
105
666k
    File.reset(); // rely on destructor to close File
106
666k
  }
107
108
  // Only for use in tests to see if deferred opens are happening, rather than
109
  // relying on RealPathName being empty.
110
  bool isOpenForTests() const { return File != nullptr; }
111
};
112
113
struct FileData;
114
115
/// Implements support for file system lookup, file system caching,
116
/// and directory search management.
117
///
118
/// This also handles more advanced properties, such as uniquing files based
119
/// on "inode", so that a file with two names (e.g. symlinked) will be treated
120
/// as a single file.
121
///
122
class FileManager : public RefCountedBase<FileManager> {
123
  IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
124
  FileSystemOptions FileSystemOpts;
125
126
  /// Cache for existing real directories.
127
  std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueRealDirs;
128
129
  /// Cache for existing real files.
130
  std::map<llvm::sys::fs::UniqueID, FileEntry> UniqueRealFiles;
131
132
  /// The virtual directories that we have allocated.
133
  ///
134
  /// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
135
  /// directories (foo/ and foo/bar/) here.
136
  SmallVector<std::unique_ptr<DirectoryEntry>, 4> VirtualDirectoryEntries;
137
  /// The virtual files that we have allocated.
138
  SmallVector<std::unique_ptr<FileEntry>, 4> VirtualFileEntries;
139
140
  /// A cache that maps paths to directory entries (either real or
141
  /// virtual) we have looked up
142
  ///
143
  /// The actual Entries for real directories/files are
144
  /// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
145
  /// for virtual directories/files are owned by
146
  /// VirtualDirectoryEntries/VirtualFileEntries above.
147
  ///
148
  llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
149
150
  /// A cache that maps paths to file entries (either real or
151
  /// virtual) we have looked up.
152
  ///
153
  /// \see SeenDirEntries
154
  llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
155
156
  /// The canonical names of directories.
157
  llvm::DenseMap<const DirectoryEntry *, llvm::StringRef> CanonicalDirNames;
158
159
  /// Storage for canonical names that we have computed.
160
  llvm::BumpPtrAllocator CanonicalNameStorage;
161
162
  /// Each FileEntry we create is assigned a unique ID #.
163
  ///
164
  unsigned NextFileUID;
165
166
  // Statistics.
167
  unsigned NumDirLookups, NumFileLookups;
168
  unsigned NumDirCacheMisses, NumFileCacheMisses;
169
170
  // Caching.
171
  std::unique_ptr<FileSystemStatCache> StatCache;
172
173
  bool getStatValue(StringRef Path, FileData &Data, bool isFile,
174
                    std::unique_ptr<llvm::vfs::File> *F);
175
176
  /// Add all ancestors of the given path (pointing to either a file
177
  /// or a directory) as virtual directories.
178
  void addAncestorsAsVirtualDirs(StringRef Path);
179
180
  /// Fills the RealPathName in file entry.
181
  void fillRealPathName(FileEntry *UFE, llvm::StringRef FileName);
182
183
public:
184
  FileManager(const FileSystemOptions &FileSystemOpts,
185
              IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
186
  ~FileManager();
187
188
  /// Installs the provided FileSystemStatCache object within
189
  /// the FileManager.
190
  ///
191
  /// Ownership of this object is transferred to the FileManager.
192
  ///
193
  /// \param statCache the new stat cache to install. Ownership of this
194
  /// object is transferred to the FileManager.
195
  ///
196
  /// \param AtBeginning whether this new stat cache must be installed at the
197
  /// beginning of the chain of stat caches. Otherwise, it will be added to
198
  /// the end of the chain.
199
  void addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
200
                    bool AtBeginning = false);
201
202
  /// Removes the specified FileSystemStatCache object from the manager.
203
  void removeStatCache(FileSystemStatCache *statCache);
204
205
  /// Removes all FileSystemStatCache objects from the manager.
206
  void clearStatCaches();
207
208
  /// Lookup, cache, and verify the specified directory (real or
209
  /// virtual).
210
  ///
211
  /// This returns NULL if the directory doesn't exist.
212
  ///
213
  /// \param CacheFailure If true and the file does not exist, we'll cache
214
  /// the failure to find this file.
215
  const DirectoryEntry *getDirectory(StringRef DirName,
216
                                     bool CacheFailure = true);
217
218
  /// Lookup, cache, and verify the specified file (real or
219
  /// virtual).
220
  ///
221
  /// This returns NULL if the file doesn't exist.
222
  ///
223
  /// \param OpenFile if true and the file exists, it will be opened.
224
  ///
225
  /// \param CacheFailure If true and the file does not exist, we'll cache
226
  /// the failure to find this file.
227
  const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
228
                           bool CacheFailure = true);
229
230
  /// Returns the current file system options
231
9.92k
  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
232
0
  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
233
234
17.5k
  IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVirtualFileSystem() const {
235
17.5k
    return FS;
236
17.5k
  }
237
238
  /// Retrieve a file entry for a "virtual" file that acts as
239
  /// if there were a file with the given name on disk.
240
  ///
241
  /// The file itself is not accessed.
242
  const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
243
                                  time_t ModificationTime);
244
245
  /// Open the specified file as a MemoryBuffer, returning a new
246
  /// MemoryBuffer if successful, otherwise returning null.
247
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
248
  getBufferForFile(const FileEntry *Entry, bool isVolatile = false,
249
                   bool ShouldCloseOpenFile = true);
250
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
251
  getBufferForFile(StringRef Filename, bool isVolatile = false);
252
253
  /// Get the 'stat' information for the given \p Path.
254
  ///
255
  /// If the path is relative, it will be resolved against the WorkingDir of the
256
  /// FileManager's FileSystemOptions.
257
  ///
258
  /// \returns false on success, true on error.
259
  bool getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result);
260
261
  /// Remove the real file \p Entry from the cache.
262
  void invalidateCache(const FileEntry *Entry);
263
264
  /// If path is not absolute and FileSystemOptions set the working
265
  /// directory, the path is modified to be relative to the given
266
  /// working directory.
267
  /// \returns true if \c path changed.
268
  bool FixupRelativePath(SmallVectorImpl<char> &path) const;
269
270
  /// Makes \c Path absolute taking into account FileSystemOptions and the
271
  /// working directory option.
272
  /// \returns true if \c Path changed to absolute.
273
  bool makeAbsolutePath(SmallVectorImpl<char> &Path) const;
274
275
  /// Produce an array mapping from the unique IDs assigned to each
276
  /// file to the corresponding FileEntry pointer.
277
  void GetUniqueIDMapping(
278
                    SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
279
280
  /// Modifies the size and modification time of a previously created
281
  /// FileEntry. Use with caution.
282
  static void modifyFileEntry(FileEntry *File, off_t Size,
283
                              time_t ModificationTime);
284
285
  /// Retrieve the canonical name for a given directory.
286
  ///
287
  /// This is a very expensive operation, despite its results being cached,
288
  /// and should only be used when the physical layout of the file system is
289
  /// required, which is (almost) never.
290
  StringRef getCanonicalName(const DirectoryEntry *Dir);
291
292
  void PrintStats() const;
293
};
294
295
} // end namespace clang
296
297
#endif // LLVM_CLANG_BASIC_FILEMANAGER_H