clang  14.0.0git
FileManager.cpp
Go to the documentation of this file.
1 //===--- FileManager.cpp - File System Probing and Caching ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the FileManager interface.
10 //
11 //===----------------------------------------------------------------------===//
12 //
13 // TODO: This should index all interesting directories with dirent calls.
14 // getdirentries ?
15 // opendir/readdir_r/closedir ?
16 //
17 //===----------------------------------------------------------------------===//
18 
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <climits>
32 #include <cstdint>
33 #include <cstdlib>
34 #include <string>
35 #include <utility>
36 
37 using namespace clang;
38 
39 #define DEBUG_TYPE "file-search"
40 
41 ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
42 ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
43 ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
44  "Number of directory cache misses.");
45 ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
46 
47 //===----------------------------------------------------------------------===//
48 // Common logic.
49 //===----------------------------------------------------------------------===//
50 
53  : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
54  SeenFileEntries(64), NextFileUID(0) {
55  // If the caller doesn't provide a virtual file system, just grab the real
56  // file system.
57  if (!this->FS)
58  this->FS = llvm::vfs::getRealFileSystem();
59 }
60 
61 FileManager::~FileManager() = default;
62 
63 void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
64  assert(statCache && "No stat cache provided?");
65  StatCache = std::move(statCache);
66 }
67 
68 void FileManager::clearStatCache() { StatCache.reset(); }
69 
70 /// Retrieve the directory that the given file name resides in.
71 /// Filename can point to either a real file or a virtual file.
74  bool CacheFailure) {
75  if (Filename.empty())
76  return llvm::errorCodeToError(
77  make_error_code(std::errc::no_such_file_or_directory));
78 
79  if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
80  return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory));
81 
82  StringRef DirName = llvm::sys::path::parent_path(Filename);
83  // Use the current directory if file has no path component.
84  if (DirName.empty())
85  DirName = ".";
86 
87  return FileMgr.getDirectoryRef(DirName, CacheFailure);
88 }
89 
90 /// Add all ancestors of the given path (pointing to either a file or
91 /// a directory) as virtual directories.
92 void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
93  StringRef DirName = llvm::sys::path::parent_path(Path);
94  if (DirName.empty())
95  DirName = ".";
96 
97  auto &NamedDirEnt = *SeenDirEntries.insert(
98  {DirName, std::errc::no_such_file_or_directory}).first;
99 
100  // When caching a virtual directory, we always cache its ancestors
101  // at the same time. Therefore, if DirName is already in the cache,
102  // we don't need to recurse as its ancestors must also already be in
103  // the cache (or it's a known non-virtual directory).
104  if (NamedDirEnt.second)
105  return;
106 
107  // Add the virtual directory to the cache.
108  auto UDE = std::make_unique<DirectoryEntry>();
109  UDE->Name = NamedDirEnt.first();
110  NamedDirEnt.second = *UDE.get();
111  VirtualDirectoryEntries.push_back(std::move(UDE));
112 
113  // Recursively add the other ancestors.
114  addAncestorsAsVirtualDirs(DirName);
115 }
116 
118 FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
119  // stat doesn't like trailing separators except for root directory.
120  // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
121  // (though it can strip '\\')
122  if (DirName.size() > 1 &&
123  DirName != llvm::sys::path::root_path(DirName) &&
124  llvm::sys::path::is_separator(DirName.back()))
125  DirName = DirName.substr(0, DirName.size()-1);
126  Optional<std::string> DirNameStr;
127  if (is_style_windows(llvm::sys::path::Style::native)) {
128  // Fixing a problem with "clang C:test.c" on Windows.
129  // Stat("C:") does not recognize "C:" as a valid directory
130  if (DirName.size() > 1 && DirName.back() == ':' &&
131  DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) {
132  DirNameStr = DirName.str() + '.';
133  DirName = *DirNameStr;
134  }
135  }
136 
137  ++NumDirLookups;
138 
139  // See if there was already an entry in the map. Note that the map
140  // contains both virtual and real directories.
141  auto SeenDirInsertResult =
142  SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
143  if (!SeenDirInsertResult.second) {
144  if (SeenDirInsertResult.first->second)
145  return DirectoryEntryRef(*SeenDirInsertResult.first);
146  return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
147  }
148 
149  // We've not seen this before. Fill it in.
150  ++NumDirCacheMisses;
151  auto &NamedDirEnt = *SeenDirInsertResult.first;
152  assert(!NamedDirEnt.second && "should be newly-created");
153 
154  // Get the null-terminated directory name as stored as the key of the
155  // SeenDirEntries map.
156  StringRef InterndDirName = NamedDirEnt.first();
157 
158  // Check to see if the directory exists.
159  llvm::vfs::Status Status;
160  auto statError = getStatValue(InterndDirName, Status, false,
161  nullptr /*directory lookup*/);
162  if (statError) {
163  // There's no real directory at the given path.
164  if (CacheFailure)
165  NamedDirEnt.second = statError;
166  else
167  SeenDirEntries.erase(DirName);
168  return llvm::errorCodeToError(statError);
169  }
170 
171  // It exists. See if we have already opened a directory with the
172  // same inode (this occurs on Unix-like systems when one dir is
173  // symlinked to another, for example) or the same path (on
174  // Windows).
175  DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()];
176 
177  NamedDirEnt.second = UDE;
178  if (UDE.getName().empty()) {
179  // We don't have this directory yet, add it. We use the string
180  // key from the SeenDirEntries map as the string.
181  UDE.Name = InterndDirName;
182  }
183 
184  return DirectoryEntryRef(NamedDirEnt);
185 }
186 
187 llvm::ErrorOr<const DirectoryEntry *>
188 FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
189  auto Result = getDirectoryRef(DirName, CacheFailure);
190  if (Result)
191  return &Result->getDirEntry();
192  return llvm::errorToErrorCode(Result.takeError());
193 }
194 
195 llvm::ErrorOr<const FileEntry *>
196 FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
197  auto Result = getFileRef(Filename, openFile, CacheFailure);
198  if (Result)
199  return &Result->getFileEntry();
200  return llvm::errorToErrorCode(Result.takeError());
201 }
202 
204 FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
205  ++NumFileLookups;
206 
207  // See if there is already an entry in the map.
208  auto SeenFileInsertResult =
209  SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
210  if (!SeenFileInsertResult.second) {
211  if (!SeenFileInsertResult.first->second)
212  return llvm::errorCodeToError(
213  SeenFileInsertResult.first->second.getError());
214  // Construct and return and FileEntryRef, unless it's a redirect to another
215  // filename.
216  FileEntryRef::MapValue Value = *SeenFileInsertResult.first->second;
217  if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
218  return FileEntryRef(*SeenFileInsertResult.first);
219  return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
220  Value.V.get<const void *>()));
221  }
222 
223  // We've not seen this before. Fill it in.
224  ++NumFileCacheMisses;
225  auto *NamedFileEnt = &*SeenFileInsertResult.first;
226  assert(!NamedFileEnt->second && "should be newly-created");
227 
228  // Get the null-terminated file name as stored as the key of the
229  // SeenFileEntries map.
230  StringRef InterndFileName = NamedFileEnt->first();
231 
232  // Look up the directory for the file. When looking up something like
233  // sys/foo.h we'll discover all of the search directories that have a 'sys'
234  // subdirectory. This will let us avoid having to waste time on known-to-fail
235  // searches when we go to find sys/bar.h, because all the search directories
236  // without a 'sys' subdir will get a cached failure result.
237  auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
238  if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
239  std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError());
240  if (CacheFailure)
241  NamedFileEnt->second = Err;
242  else
243  SeenFileEntries.erase(Filename);
244 
245  return llvm::errorCodeToError(Err);
246  }
247  DirectoryEntryRef DirInfo = *DirInfoOrErr;
248 
249  // FIXME: Use the directory info to prune this, before doing the stat syscall.
250  // FIXME: This will reduce the # syscalls.
251 
252  // Check to see if the file exists.
253  std::unique_ptr<llvm::vfs::File> F;
254  llvm::vfs::Status Status;
255  auto statError = getStatValue(InterndFileName, Status, true,
256  openFile ? &F : nullptr);
257  if (statError) {
258  // There's no real file at the given path.
259  if (CacheFailure)
260  NamedFileEnt->second = statError;
261  else
262  SeenFileEntries.erase(Filename);
263 
264  return llvm::errorCodeToError(statError);
265  }
266 
267  assert((openFile || !F) && "undesired open file");
268 
269  // It exists. See if we have already opened a file with the same inode.
270  // This occurs when one dir is symlinked to another, for example.
271  FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()];
272 
273  if (Status.getName() == Filename) {
274  // The name matches. Set the FileEntry.
275  NamedFileEnt->second = FileEntryRef::MapValue(UFE, DirInfo);
276  } else {
277  // Name mismatch. We need a redirect. First grab the actual entry we want
278  // to return.
279  //
280  // This redirection logic intentionally leaks the external name of a
281  // redirected file that uses 'use-external-name' in \a
282  // vfs::RedirectionFileSystem. This allows clang to report the external
283  // name to users (in diagnostics) and to tools that don't have access to
284  // the VFS (in debug info and dependency '.d' files).
285  //
286  // FIXME: This is pretty complicated. It's also inconsistent with how
287  // "real" filesystems behave and confuses parts of clang expect to see the
288  // name-as-accessed on the \a FileEntryRef. Maybe the returned \a
289  // FileEntryRef::getName() could return the accessed name unmodified, but
290  // make the external name available via a separate API.
291  auto &Redirection =
292  *SeenFileEntries
293  .insert({Status.getName(), FileEntryRef::MapValue(UFE, DirInfo)})
294  .first;
295  assert(Redirection.second->V.is<FileEntry *>() &&
296  "filename redirected to a non-canonical filename?");
297  assert(Redirection.second->V.get<FileEntry *>() == &UFE &&
298  "filename from getStatValue() refers to wrong file");
299 
300  // Cache the redirection in the previously-inserted entry, still available
301  // in the tentative return value.
302  NamedFileEnt->second = FileEntryRef::MapValue(Redirection);
303 
304  // Fix the tentative return value.
305  NamedFileEnt = &Redirection;
306  }
307 
308  FileEntryRef ReturnedRef(*NamedFileEnt);
309  if (UFE.isValid()) { // Already have an entry with this inode, return it.
310 
311  // FIXME: this hack ensures that if we look up a file by a virtual path in
312  // the VFS that the getDir() will have the virtual path, even if we found
313  // the file by a 'real' path first. This is required in order to find a
314  // module's structure when its headers/module map are mapped in the VFS.
315  // We should remove this as soon as we can properly support a file having
316  // multiple names.
317  if (&DirInfo.getDirEntry() != UFE.Dir && Status.IsVFSMapped)
318  UFE.Dir = &DirInfo.getDirEntry();
319 
320  // Always update LastRef to the last name by which a file was accessed.
321  // FIXME: Neither this nor always using the first reference is correct; we
322  // want to switch towards a design where we return a FileName object that
323  // encapsulates both the name by which the file was accessed and the
324  // corresponding FileEntry.
325  // FIXME: LastRef should be removed from FileEntry once all clients adopt
326  // FileEntryRef.
327  UFE.LastRef = ReturnedRef;
328 
329  return ReturnedRef;
330  }
331 
332  // Otherwise, we don't have this file yet, add it.
333  UFE.LastRef = ReturnedRef;
334  UFE.Size = Status.getSize();
335  UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
336  UFE.Dir = &DirInfo.getDirEntry();
337  UFE.UID = NextFileUID++;
338  UFE.UniqueID = Status.getUniqueID();
339  UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
340  UFE.File = std::move(F);
341  UFE.IsValid = true;
342 
343  if (UFE.File) {
344  if (auto PathName = UFE.File->getName())
345  fillRealPathName(&UFE, *PathName);
346  } else if (!openFile) {
347  // We should still fill the path even if we aren't opening the file.
348  fillRealPathName(&UFE, InterndFileName);
349  }
350  return ReturnedRef;
351 }
352 
354  // Only read stdin once.
355  if (STDIN)
356  return *STDIN;
357 
358  std::unique_ptr<llvm::MemoryBuffer> Content;
359  if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN())
360  Content = std::move(*ContentOrError);
361  else
362  return llvm::errorCodeToError(ContentOrError.getError());
363 
364  STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
365  Content->getBufferSize(), 0);
366  FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
367  FE.Content = std::move(Content);
368  FE.IsNamedPipe = true;
369  return *STDIN;
370 }
371 
372 const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size,
373  time_t ModificationTime) {
374  return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry();
375 }
376 
378  time_t ModificationTime) {
379  ++NumFileLookups;
380 
381  // See if there is already an entry in the map for an existing file.
382  auto &NamedFileEnt = *SeenFileEntries.insert(
383  {Filename, std::errc::no_such_file_or_directory}).first;
384  if (NamedFileEnt.second) {
385  FileEntryRef::MapValue Value = *NamedFileEnt.second;
386  if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
387  return FileEntryRef(NamedFileEnt);
388  return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
389  Value.V.get<const void *>()));
390  }
391 
392  // We've not seen this before, or the file is cached as non-existent.
393  ++NumFileCacheMisses;
394  addAncestorsAsVirtualDirs(Filename);
395  FileEntry *UFE = nullptr;
396 
397  // Now that all ancestors of Filename are in the cache, the
398  // following call is guaranteed to find the DirectoryEntry from the
399  // cache. A virtual file can also have an empty filename, that could come
400  // from a source location preprocessor directive with an empty filename as
401  // an example, so we need to pretend it has a name to ensure a valid directory
402  // entry can be returned.
403  auto DirInfo = expectedToOptional(getDirectoryFromFile(
404  *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
405  assert(DirInfo &&
406  "The directory of a virtual file should already be in the cache.");
407 
408  // Check to see if the file exists. If so, drop the virtual file
409  llvm::vfs::Status Status;
410  const char *InterndFileName = NamedFileEnt.first().data();
411  if (!getStatValue(InterndFileName, Status, true, nullptr)) {
412  UFE = &UniqueRealFiles[Status.getUniqueID()];
413  Status = llvm::vfs::Status(
414  Status.getName(), Status.getUniqueID(),
415  llvm::sys::toTimePoint(ModificationTime),
416  Status.getUser(), Status.getGroup(), Size,
417  Status.getType(), Status.getPermissions());
418 
419  NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
420 
421  // If we had already opened this file, close it now so we don't
422  // leak the descriptor. We're not going to use the file
423  // descriptor anyway, since this is a virtual file.
424  if (UFE->File)
425  UFE->closeFile();
426 
427  // If we already have an entry with this inode, return it.
428  //
429  // FIXME: Surely this should add a reference by the new name, and return
430  // it instead...
431  if (UFE->isValid())
432  return FileEntryRef(NamedFileEnt);
433 
434  UFE->UniqueID = Status.getUniqueID();
435  UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
436  fillRealPathName(UFE, Status.getName());
437  } else {
438  VirtualFileEntries.push_back(std::make_unique<FileEntry>());
439  UFE = VirtualFileEntries.back().get();
440  NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
441  }
442 
443  UFE->LastRef = FileEntryRef(NamedFileEnt);
444  UFE->Size = Size;
445  UFE->ModTime = ModificationTime;
446  UFE->Dir = &DirInfo->getDirEntry();
447  UFE->UID = NextFileUID++;
448  UFE->IsValid = true;
449  UFE->File.reset();
450  return FileEntryRef(NamedFileEnt);
451 }
452 
454  // Stat of the file and return nullptr if it doesn't exist.
455  llvm::vfs::Status Status;
456  if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
457  return None;
458 
459  if (!SeenBypassFileEntries)
460  SeenBypassFileEntries = std::make_unique<
461  llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
462 
463  // If we've already bypassed just use the existing one.
464  auto Insertion = SeenBypassFileEntries->insert(
465  {VF.getName(), std::errc::no_such_file_or_directory});
466  if (!Insertion.second)
467  return FileEntryRef(*Insertion.first);
468 
469  // Fill in the new entry from the stat.
470  BypassFileEntries.push_back(std::make_unique<FileEntry>());
471  const FileEntry &VFE = VF.getFileEntry();
472  FileEntry &BFE = *BypassFileEntries.back();
473  Insertion.first->second = FileEntryRef::MapValue(BFE, VF.getDir());
474  BFE.LastRef = FileEntryRef(*Insertion.first);
475  BFE.Size = Status.getSize();
476  BFE.Dir = VFE.Dir;
477  BFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
478  BFE.UID = NextFileUID++;
479  BFE.IsValid = true;
480 
481  // Save the entry in the bypass table and return.
482  return FileEntryRef(*Insertion.first);
483 }
484 
486  StringRef pathRef(path.data(), path.size());
487 
488  if (FileSystemOpts.WorkingDir.empty()
489  || llvm::sys::path::is_absolute(pathRef))
490  return false;
491 
492  SmallString<128> NewPath(FileSystemOpts.WorkingDir);
493  llvm::sys::path::append(NewPath, pathRef);
494  path = NewPath;
495  return true;
496 }
497 
499  bool Changed = FixupRelativePath(Path);
500 
501  if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
502  FS->makeAbsolute(Path);
503  Changed = true;
504  }
505 
506  return Changed;
507 }
508 
509 void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
510  llvm::SmallString<128> AbsPath(FileName);
511  // This is not the same as `VFS::getRealPath()`, which resolves symlinks
512  // but can be very expensive on real file systems.
513  // FIXME: the semantic of RealPathName is unclear, and the name might be
514  // misleading. We need to clean up the interface here.
515  makeAbsolutePath(AbsPath);
516  llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
517  UFE->RealPathName = std::string(AbsPath.str());
518 }
519 
520 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
521 FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
522  bool RequiresNullTerminator) {
523  // If the content is living on the file entry, return a reference to it.
524  if (Entry->Content)
525  return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
526 
527  uint64_t FileSize = Entry->getSize();
528  // If there's a high enough chance that the file have changed since we
529  // got its size, force a stat before opening it.
530  if (isVolatile || Entry->isNamedPipe())
531  FileSize = -1;
532 
533  StringRef Filename = Entry->getName();
534  // If the file is already open, use the open file descriptor.
535  if (Entry->File) {
536  auto Result = Entry->File->getBuffer(Filename, FileSize,
537  RequiresNullTerminator, isVolatile);
538  Entry->closeFile();
539  return Result;
540  }
541 
542  // Otherwise, open the file.
543  return getBufferForFileImpl(Filename, FileSize, isVolatile,
544  RequiresNullTerminator);
545 }
546 
547 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
548 FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
549  bool isVolatile,
550  bool RequiresNullTerminator) {
551  if (FileSystemOpts.WorkingDir.empty())
552  return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
553  isVolatile);
554 
555  SmallString<128> FilePath(Filename);
556  FixupRelativePath(FilePath);
557  return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
558  isVolatile);
559 }
560 
561 /// getStatValue - Get the 'stat' information for the specified path,
562 /// using the cache to accelerate it if possible. This returns true
563 /// if the path points to a virtual file or does not exist, or returns
564 /// false if it's an existent real file. If FileDescriptor is NULL,
565 /// do directory look-up instead of file look-up.
566 std::error_code
567 FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
568  bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
569  // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
570  // absolute!
571  if (FileSystemOpts.WorkingDir.empty())
572  return FileSystemStatCache::get(Path, Status, isFile, F,
573  StatCache.get(), *FS);
574 
575  SmallString<128> FilePath(Path);
576  FixupRelativePath(FilePath);
577 
578  return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
579  StatCache.get(), *FS);
580 }
581 
582 std::error_code
584  llvm::vfs::Status &Result) {
585  SmallString<128> FilePath(Path);
586  FixupRelativePath(FilePath);
587 
588  llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
589  if (!S)
590  return S.getError();
591  Result = *S;
592  return std::error_code();
593 }
594 
596  SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
597  UIDToFiles.clear();
598  UIDToFiles.resize(NextFileUID);
599 
600  // Map file entries
601  for (llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>,
602  llvm::BumpPtrAllocator>::const_iterator
603  FE = SeenFileEntries.begin(),
604  FEEnd = SeenFileEntries.end();
605  FE != FEEnd; ++FE)
606  if (llvm::ErrorOr<FileEntryRef::MapValue> Entry = FE->getValue()) {
607  if (const auto *FE = Entry->V.dyn_cast<FileEntry *>())
608  UIDToFiles[FE->getUID()] = FE;
609  }
610 
611  // Map virtual file entries
612  for (const auto &VFE : VirtualFileEntries)
613  UIDToFiles[VFE->getUID()] = VFE.get();
614 }
615 
617  llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
618  = CanonicalNames.find(Dir);
619  if (Known != CanonicalNames.end())
620  return Known->second;
621 
622  StringRef CanonicalName(Dir->getName());
623 
624  SmallString<4096> CanonicalNameBuf;
625  if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
626  CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
627 
628  CanonicalNames.insert({Dir, CanonicalName});
629  return CanonicalName;
630 }
631 
632 StringRef FileManager::getCanonicalName(const FileEntry *File) {
633  llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
634  = CanonicalNames.find(File);
635  if (Known != CanonicalNames.end())
636  return Known->second;
637 
638  StringRef CanonicalName(File->getName());
639 
640  SmallString<4096> CanonicalNameBuf;
641  if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
642  CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
643 
644  CanonicalNames.insert({File, CanonicalName});
645  return CanonicalName;
646 }
647 
649  llvm::errs() << "\n*** File Manager Stats:\n";
650  llvm::errs() << UniqueRealFiles.size() << " real files found, "
651  << UniqueRealDirs.size() << " real dirs found.\n";
652  llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
653  << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
654  llvm::errs() << NumDirLookups << " dir lookups, "
655  << NumDirCacheMisses << " dir cache misses.\n";
656  llvm::errs() << NumFileLookups << " file lookups, "
657  << NumFileCacheMisses << " file cache misses.\n";
658 
659  //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
660 }
clang::FileManager::getNoncachedStatValue
std::error_code getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result)
Get the 'stat' information for the given Path.
Definition: FileManager.cpp:583
clang::FileManager::FileManager
FileManager(const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
Construct a file manager, optionally with a custom VFS.
Definition: FileManager.cpp:51
clang::FileEntryRef
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:60
ALWAYS_ENABLED_STATISTIC
ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.")
clang::DeclaratorContext::File
@ File
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::FileManager::GetUniqueIDMapping
void GetUniqueIDMapping(SmallVectorImpl< const FileEntry * > &UIDToFiles) const
Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntry poi...
Definition: FileManager.cpp:595
clang::FileManager::setStatCache
void setStatCache(std::unique_ptr< FileSystemStatCache > statCache)
Installs the provided FileSystemStatCache object within the FileManager.
Definition: FileManager.cpp:63
clang::DirectoryEntryRef
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Definition: DirectoryEntry.h:45
clang::DirectoryEntry::getName
StringRef getName() const
Definition: DirectoryEntry.h:40
clang::FileManager::clearStatCache
void clearStatCache()
Removes the FileSystemStatCache object from the manager.
Definition: FileManager.cpp:68
getDirectoryFromFile
static llvm::Expected< DirectoryEntryRef > getDirectoryFromFile(FileManager &FileMgr, StringRef Filename, bool CacheFailure)
Retrieve the directory that the given file name resides in.
Definition: FileManager.cpp:73
clang::FileManager::getFileRef
llvm::Expected< FileEntryRef > getFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:204
Filename
StringRef Filename
Definition: Format.cpp:2379
llvm::Optional< std::string >
llvm::Expected
Definition: LLVM.h:41
clang::make_error_code
std::error_code make_error_code(BuildPreambleError Error)
Definition: PrecompiledPreamble.cpp:863
clang::FileSystemOptions::WorkingDir
std::string WorkingDir
If set, paths are resolved as if the working directory was set to the value of WorkingDir.
Definition: FileSystemOptions.h:26
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:22
clang::DirectoryEntry
Cached information about one directory (either on disk or in the virtual file system).
Definition: DirectoryEntry.h:33
clang::FileEntry::closeFile
void closeFile() const
Definition: FileEntry.cpp:24
clang::FileManager::getDirectoryRef
llvm::Expected< DirectoryEntryRef > getDirectoryRef(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Definition: FileManager.cpp:118
clang::FileManager::FixupRelativePath
bool FixupRelativePath(SmallVectorImpl< char > &path) const
If path is not absolute and FileSystemOptions set the working directory, the path is modified to be r...
Definition: FileManager.cpp:485
llvm::SmallString< 128 >
clang::FileSystemStatCache::get
static std::error_code get(StringRef Path, llvm::vfs::Status &Status, bool isFile, std::unique_ptr< llvm::vfs::File > *F, FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS)
Get the 'stat' information for the specified path, using the cache to accelerate it if possible.
Definition: FileSystemStatCache.cpp:34
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::DirectoryEntryRef::getDirEntry
const DirectoryEntry & getDirEntry() const
Definition: DirectoryEntry.h:47
clang::FileEntryRef::getName
StringRef getName() const
Definition: FileEntry.h:62
clang::FileManager::PrintStats
void PrintStats() const
Definition: FileManager.cpp:648
clang::FileManager::getVirtualFileRef
FileEntryRef getVirtualFileRef(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
Definition: FileManager.cpp:377
FileSystemStatCache.h
clang::FileEntry::isValid
bool isValid() const
Definition: FileEntry.h:368
Value
Value
Definition: UninitializedValues.cpp:102
clang::FileManager::getBufferForFile
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: FileManager.cpp:521
clang::FileEntryRef::MapValue
Type stored in the StringMap.
Definition: FileEntry.h:109
std
Definition: Format.h:4125
clang::FileManager::~FileManager
~FileManager()
clang::FileManager::makeAbsolutePath
bool makeAbsolutePath(SmallVectorImpl< char > &Path) const
Makes Path absolute taking into account FileSystemOptions and the working directory option.
Definition: FileManager.cpp:498
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::FileManager::getCanonicalName
StringRef getCanonicalName(const DirectoryEntry *Dir)
Retrieve the canonical name for a given directory.
Definition: FileManager.cpp:616
clang::FileEntry::getName
StringRef getName() const
Definition: FileEntry.h:364
clang::FileManager::getSTDIN
llvm::Expected< FileEntryRef > getSTDIN()
Get the FileEntryRef for stdin, returning an error if stdin cannot be read.
Definition: FileManager.cpp:353
clang::FileManager::getDirectory
llvm::ErrorOr< const DirectoryEntry * > getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Definition: FileManager.cpp:188
clang::FileEntryRef::getFileEntry
const FileEntry & getFileEntry() const
Definition: FileEntry.h:63
clang::FileManager::getFile
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:196
llvm::SmallVectorImpl< char >
FileManager.h
clang::FileEntry::isNamedPipe
bool isNamedPipe() const
Check whether the file is a named pipe (and thus can't be opened by the native FileManager methods).
Definition: FileEntry.h:381
clang::FileEntry::getSize
off_t getSize() const
Definition: FileEntry.h:369
clang::FileEntryRef::getDir
DirectoryEntryRef getDir() const
Definition: FileEntry.h:66
clang::FileEntryRef::MapEntry
llvm::StringMapEntry< llvm::ErrorOr< MapValue > > MapEntry
Type used in the StringMap.
Definition: FileEntry.h:106
clang::FileManager::getVirtualFile
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Definition: FileManager.cpp:372
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >
clang::FileManager::getBypassFile
llvm::Optional< FileEntryRef > getBypassFile(FileEntryRef VFE)
Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry,...
Definition: FileManager.cpp:453