xref: /llvm-project/clang/lib/Basic/FileManager.cpp (revision 6c1dbd5359c4336d03b11faeaea8459b421f2c5c)
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 
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/FileSystemStatCache.h"
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 <optional>
35 #include <string>
36 #include <utility>
37 
38 using namespace clang;
39 
40 #define DEBUG_TYPE "file-search"
41 
42 ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
43 ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
44 ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
45                          "Number of directory cache misses.");
46 ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
47 
48 //===----------------------------------------------------------------------===//
49 // Common logic.
50 //===----------------------------------------------------------------------===//
51 
52 FileManager::FileManager(const FileSystemOptions &FSO,
53                          IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
54     : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
55       SeenFileEntries(64), NextFileUID(0) {
56   // If the caller doesn't provide a virtual file system, just grab the real
57   // file system.
58   if (!this->FS)
59     this->FS = llvm::vfs::getRealFileSystem();
60 }
61 
62 FileManager::~FileManager() = default;
63 
64 void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
65   assert(statCache && "No stat cache provided?");
66   StatCache = std::move(statCache);
67 }
68 
69 void FileManager::clearStatCache() { StatCache.reset(); }
70 
71 /// Retrieve the directory that the given file name resides in.
72 /// Filename can point to either a real file or a virtual file.
73 static llvm::Expected<DirectoryEntryRef>
74 getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
75                      bool CacheFailure) {
76   if (Filename.empty())
77     return llvm::errorCodeToError(
78         make_error_code(std::errc::no_such_file_or_directory));
79 
80   if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
81     return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory));
82 
83   StringRef DirName = llvm::sys::path::parent_path(Filename);
84   // Use the current directory if file has no path component.
85   if (DirName.empty())
86     DirName = ".";
87 
88   return FileMgr.getDirectoryRef(DirName, CacheFailure);
89 }
90 
91 /// Add all ancestors of the given path (pointing to either a file or
92 /// a directory) as virtual directories.
93 void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
94   StringRef DirName = llvm::sys::path::parent_path(Path);
95   if (DirName.empty())
96     DirName = ".";
97 
98   auto &NamedDirEnt = *SeenDirEntries.insert(
99         {DirName, std::errc::no_such_file_or_directory}).first;
100 
101   // When caching a virtual directory, we always cache its ancestors
102   // at the same time.  Therefore, if DirName is already in the cache,
103   // we don't need to recurse as its ancestors must also already be in
104   // the cache (or it's a known non-virtual directory).
105   if (NamedDirEnt.second)
106     return;
107 
108   // Add the virtual directory to the cache.
109   auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
110   NamedDirEnt.second = *UDE;
111   VirtualDirectoryEntries.push_back(UDE);
112 
113   // Recursively add the other ancestors.
114   addAncestorsAsVirtualDirs(DirName);
115 }
116 
117 llvm::Expected<DirectoryEntryRef>
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   std::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   if (!UDE) {
178     // We don't have this directory yet, add it.  We use the string
179     // key from the SeenDirEntries map as the string.
180     UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
181   }
182   NamedDirEnt.second = *UDE;
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 
203 llvm::Expected<FileEntryRef>
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     return FileEntryRef(*SeenFileInsertResult.first);
215   }
216 
217   // We've not seen this before. Fill it in.
218   ++NumFileCacheMisses;
219   auto *NamedFileEnt = &*SeenFileInsertResult.first;
220   assert(!NamedFileEnt->second && "should be newly-created");
221 
222   // Get the null-terminated file name as stored as the key of the
223   // SeenFileEntries map.
224   StringRef InterndFileName = NamedFileEnt->first();
225 
226   // Look up the directory for the file.  When looking up something like
227   // sys/foo.h we'll discover all of the search directories that have a 'sys'
228   // subdirectory.  This will let us avoid having to waste time on known-to-fail
229   // searches when we go to find sys/bar.h, because all the search directories
230   // without a 'sys' subdir will get a cached failure result.
231   auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
232   if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
233     std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError());
234     if (CacheFailure)
235       NamedFileEnt->second = Err;
236     else
237       SeenFileEntries.erase(Filename);
238 
239     return llvm::errorCodeToError(Err);
240   }
241   DirectoryEntryRef DirInfo = *DirInfoOrErr;
242 
243   // FIXME: Use the directory info to prune this, before doing the stat syscall.
244   // FIXME: This will reduce the # syscalls.
245 
246   // Check to see if the file exists.
247   std::unique_ptr<llvm::vfs::File> F;
248   llvm::vfs::Status Status;
249   auto statError = getStatValue(InterndFileName, Status, true,
250                                 openFile ? &F : nullptr);
251   if (statError) {
252     // There's no real file at the given path.
253     if (CacheFailure)
254       NamedFileEnt->second = statError;
255     else
256       SeenFileEntries.erase(Filename);
257 
258     return llvm::errorCodeToError(statError);
259   }
260 
261   assert((openFile || !F) && "undesired open file");
262 
263   // It exists.  See if we have already opened a file with the same inode.
264   // This occurs when one dir is symlinked to another, for example.
265   FileEntry *&UFE = UniqueRealFiles[Status.getUniqueID()];
266   bool ReusingEntry = UFE != nullptr;
267   if (!UFE)
268     UFE = new (FilesAlloc.Allocate()) FileEntry();
269 
270   if (!Status.ExposesExternalVFSPath || Status.getName() == Filename) {
271     // Use the requested name. Set the FileEntry.
272     NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
273   } else {
274     // Name mismatch. We need a redirect. First grab the actual entry we want
275     // to return.
276     //
277     // This redirection logic intentionally leaks the external name of a
278     // redirected file that uses 'use-external-name' in \a
279     // vfs::RedirectionFileSystem. This allows clang to report the external
280     // name to users (in diagnostics) and to tools that don't have access to
281     // the VFS (in debug info and dependency '.d' files).
282     //
283     // FIXME: This is pretty complex and has some very complicated interactions
284     // with the rest of clang. It's also inconsistent with how "real"
285     // filesystems behave and confuses parts of clang expect to see the
286     // name-as-accessed on the \a FileEntryRef.
287     //
288     // A potential plan to remove this is as follows -
289     //   - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
290     //     to explicitly use the `getNameAsRequested()` rather than just using
291     //     `getName()`.
292     //   - Add a `FileManager::getExternalPath` API for explicitly getting the
293     //     remapped external filename when there is one available. Adopt it in
294     //     callers like diagnostics/deps reporting instead of calling
295     //     `getName()` directly.
296     //   - Switch the meaning of `FileEntryRef::getName()` to get the requested
297     //     name, not the external name. Once that sticks, revert callers that
298     //     want the requested name back to calling `getName()`.
299     //   - Update the VFS to always return the requested name. This could also
300     //     return the external name, or just have an API to request it
301     //     lazily. The latter has the benefit of making accesses of the
302     //     external path easily tracked, but may also require extra work than
303     //     just returning up front.
304     //   - (Optionally) Add an API to VFS to get the external filename lazily
305     //     and update `FileManager::getExternalPath()` to use it instead. This
306     //     has the benefit of making such accesses easily tracked, though isn't
307     //     necessarily required (and could cause extra work than just adding to
308     //     eg. `vfs::Status` up front).
309     auto &Redirection =
310         *SeenFileEntries
311              .insert({Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
312              .first;
313     assert(Redirection.second->V.is<FileEntry *>() &&
314            "filename redirected to a non-canonical filename?");
315     assert(Redirection.second->V.get<FileEntry *>() == UFE &&
316            "filename from getStatValue() refers to wrong file");
317 
318     // Cache the redirection in the previously-inserted entry, still available
319     // in the tentative return value.
320     NamedFileEnt->second = FileEntryRef::MapValue(Redirection, DirInfo);
321   }
322 
323   FileEntryRef ReturnedRef(*NamedFileEnt);
324   if (ReusingEntry) { // Already have an entry with this inode, return it.
325     return ReturnedRef;
326   }
327 
328   // Otherwise, we don't have this file yet, add it.
329   UFE->Size = Status.getSize();
330   UFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
331   UFE->Dir = &DirInfo.getDirEntry();
332   UFE->UID = NextFileUID++;
333   UFE->UniqueID = Status.getUniqueID();
334   UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
335   UFE->File = std::move(F);
336 
337   if (UFE->File) {
338     if (auto PathName = UFE->File->getName())
339       fillRealPathName(UFE, *PathName);
340   } else if (!openFile) {
341     // We should still fill the path even if we aren't opening the file.
342     fillRealPathName(UFE, InterndFileName);
343   }
344   return ReturnedRef;
345 }
346 
347 llvm::Expected<FileEntryRef> FileManager::getSTDIN() {
348   // Only read stdin once.
349   if (STDIN)
350     return *STDIN;
351 
352   std::unique_ptr<llvm::MemoryBuffer> Content;
353   if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN())
354     Content = std::move(*ContentOrError);
355   else
356     return llvm::errorCodeToError(ContentOrError.getError());
357 
358   STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
359                             Content->getBufferSize(), 0);
360   FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
361   FE.Content = std::move(Content);
362   FE.IsNamedPipe = true;
363   return *STDIN;
364 }
365 
366 const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size,
367                                              time_t ModificationTime) {
368   return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry();
369 }
370 
371 FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
372                                             time_t ModificationTime) {
373   ++NumFileLookups;
374 
375   // See if there is already an entry in the map for an existing file.
376   auto &NamedFileEnt = *SeenFileEntries.insert(
377       {Filename, std::errc::no_such_file_or_directory}).first;
378   if (NamedFileEnt.second) {
379     FileEntryRef::MapValue Value = *NamedFileEnt.second;
380     if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
381       return FileEntryRef(NamedFileEnt);
382     return FileEntryRef(*Value.V.get<const FileEntryRef::MapEntry *>());
383   }
384 
385   // We've not seen this before, or the file is cached as non-existent.
386   ++NumFileCacheMisses;
387   addAncestorsAsVirtualDirs(Filename);
388   FileEntry *UFE = nullptr;
389 
390   // Now that all ancestors of Filename are in the cache, the
391   // following call is guaranteed to find the DirectoryEntry from the
392   // cache. A virtual file can also have an empty filename, that could come
393   // from a source location preprocessor directive with an empty filename as
394   // an example, so we need to pretend it has a name to ensure a valid directory
395   // entry can be returned.
396   auto DirInfo = expectedToOptional(getDirectoryFromFile(
397       *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
398   assert(DirInfo &&
399          "The directory of a virtual file should already be in the cache.");
400 
401   // Check to see if the file exists. If so, drop the virtual file
402   llvm::vfs::Status Status;
403   const char *InterndFileName = NamedFileEnt.first().data();
404   if (!getStatValue(InterndFileName, Status, true, nullptr)) {
405     Status = llvm::vfs::Status(
406       Status.getName(), Status.getUniqueID(),
407       llvm::sys::toTimePoint(ModificationTime),
408       Status.getUser(), Status.getGroup(), Size,
409       Status.getType(), Status.getPermissions());
410 
411     auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
412     if (RealFE) {
413       // If we had already opened this file, close it now so we don't
414       // leak the descriptor. We're not going to use the file
415       // descriptor anyway, since this is a virtual file.
416       if (RealFE->File)
417         RealFE->closeFile();
418       // If we already have an entry with this inode, return it.
419       //
420       // FIXME: Surely this should add a reference by the new name, and return
421       // it instead...
422       NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
423       return FileEntryRef(NamedFileEnt);
424     }
425     // File exists, but no entry - create it.
426     RealFE = new (FilesAlloc.Allocate()) FileEntry();
427     RealFE->UniqueID = Status.getUniqueID();
428     RealFE->IsNamedPipe =
429         Status.getType() == llvm::sys::fs::file_type::fifo_file;
430     fillRealPathName(RealFE, Status.getName());
431 
432     UFE = RealFE;
433   } else {
434     // File does not exist, create a virtual entry.
435     UFE = new (FilesAlloc.Allocate()) FileEntry();
436     VirtualFileEntries.push_back(UFE);
437   }
438 
439   NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
440   UFE->Size    = Size;
441   UFE->ModTime = ModificationTime;
442   UFE->Dir     = &DirInfo->getDirEntry();
443   UFE->UID     = NextFileUID++;
444   UFE->File.reset();
445   return FileEntryRef(NamedFileEnt);
446 }
447 
448 OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {
449   // Stat of the file and return nullptr if it doesn't exist.
450   llvm::vfs::Status Status;
451   if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
452     return std::nullopt;
453 
454   if (!SeenBypassFileEntries)
455     SeenBypassFileEntries = std::make_unique<
456         llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
457 
458   // If we've already bypassed just use the existing one.
459   auto Insertion = SeenBypassFileEntries->insert(
460       {VF.getName(), std::errc::no_such_file_or_directory});
461   if (!Insertion.second)
462     return FileEntryRef(*Insertion.first);
463 
464   // Fill in the new entry from the stat.
465   FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
466   BypassFileEntries.push_back(BFE);
467   Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
468   BFE->Size = Status.getSize();
469   BFE->Dir = VF.getFileEntry().Dir;
470   BFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
471   BFE->UID = NextFileUID++;
472 
473   // Save the entry in the bypass table and return.
474   return FileEntryRef(*Insertion.first);
475 }
476 
477 bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
478   StringRef pathRef(path.data(), path.size());
479 
480   if (FileSystemOpts.WorkingDir.empty()
481       || llvm::sys::path::is_absolute(pathRef))
482     return false;
483 
484   SmallString<128> NewPath(FileSystemOpts.WorkingDir);
485   llvm::sys::path::append(NewPath, pathRef);
486   path = NewPath;
487   return true;
488 }
489 
490 bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
491   bool Changed = FixupRelativePath(Path);
492 
493   if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
494     FS->makeAbsolute(Path);
495     Changed = true;
496   }
497 
498   return Changed;
499 }
500 
501 void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
502   llvm::SmallString<128> AbsPath(FileName);
503   // This is not the same as `VFS::getRealPath()`, which resolves symlinks
504   // but can be very expensive on real file systems.
505   // FIXME: the semantic of RealPathName is unclear, and the name might be
506   // misleading. We need to clean up the interface here.
507   makeAbsolutePath(AbsPath);
508   llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
509   UFE->RealPathName = std::string(AbsPath);
510 }
511 
512 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
513 FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile,
514                               bool RequiresNullTerminator) {
515   const FileEntry *Entry = &FE.getFileEntry();
516   // If the content is living on the file entry, return a reference to it.
517   if (Entry->Content)
518     return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
519 
520   uint64_t FileSize = Entry->getSize();
521   // If there's a high enough chance that the file have changed since we
522   // got its size, force a stat before opening it.
523   if (isVolatile || Entry->isNamedPipe())
524     FileSize = -1;
525 
526   StringRef Filename = FE.getName();
527   // If the file is already open, use the open file descriptor.
528   if (Entry->File) {
529     auto Result = Entry->File->getBuffer(Filename, FileSize,
530                                          RequiresNullTerminator, isVolatile);
531     Entry->closeFile();
532     return Result;
533   }
534 
535   // Otherwise, open the file.
536   return getBufferForFileImpl(Filename, FileSize, isVolatile,
537                               RequiresNullTerminator);
538 }
539 
540 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
541 FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
542                                   bool isVolatile,
543                                   bool RequiresNullTerminator) {
544   if (FileSystemOpts.WorkingDir.empty())
545     return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
546                                 isVolatile);
547 
548   SmallString<128> FilePath(Filename);
549   FixupRelativePath(FilePath);
550   return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
551                               isVolatile);
552 }
553 
554 /// getStatValue - Get the 'stat' information for the specified path,
555 /// using the cache to accelerate it if possible.  This returns true
556 /// if the path points to a virtual file or does not exist, or returns
557 /// false if it's an existent real file.  If FileDescriptor is NULL,
558 /// do directory look-up instead of file look-up.
559 std::error_code
560 FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
561                           bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
562   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
563   // absolute!
564   if (FileSystemOpts.WorkingDir.empty())
565     return FileSystemStatCache::get(Path, Status, isFile, F,
566                                     StatCache.get(), *FS);
567 
568   SmallString<128> FilePath(Path);
569   FixupRelativePath(FilePath);
570 
571   return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
572                                   StatCache.get(), *FS);
573 }
574 
575 std::error_code
576 FileManager::getNoncachedStatValue(StringRef Path,
577                                    llvm::vfs::Status &Result) {
578   SmallString<128> FilePath(Path);
579   FixupRelativePath(FilePath);
580 
581   llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
582   if (!S)
583     return S.getError();
584   Result = *S;
585   return std::error_code();
586 }
587 
588 void FileManager::GetUniqueIDMapping(
589     SmallVectorImpl<OptionalFileEntryRef> &UIDToFiles) const {
590   UIDToFiles.clear();
591   UIDToFiles.resize(NextFileUID);
592 
593   for (const auto &Entry : SeenFileEntries) {
594     // Only return files that exist and are not redirected.
595     if (!Entry.getValue() || !Entry.getValue()->V.is<FileEntry *>())
596       continue;
597     FileEntryRef FE(Entry);
598     // Add this file if it's the first one with the UID, or if its name is
599     // better than the existing one.
600     OptionalFileEntryRef &ExistingFE = UIDToFiles[FE.getUID()];
601     if (!ExistingFE || FE.getName() < ExistingFE->getName())
602       ExistingFE = FE;
603   }
604 }
605 
606 StringRef FileManager::getCanonicalName(DirectoryEntryRef Dir) {
607   return getCanonicalName(Dir, Dir.getName());
608 }
609 
610 StringRef FileManager::getCanonicalName(FileEntryRef File) {
611   return getCanonicalName(File, File.getName());
612 }
613 
614 StringRef FileManager::getCanonicalName(const void *Entry, StringRef Name) {
615   llvm::DenseMap<const void *, llvm::StringRef>::iterator Known =
616       CanonicalNames.find(Entry);
617   if (Known != CanonicalNames.end())
618     return Known->second;
619 
620   // Name comes from FileEntry/DirectoryEntry::getName(), so it is safe to
621   // store it in the DenseMap below.
622   StringRef CanonicalName(Name);
623 
624   SmallString<256> AbsPathBuf;
625   SmallString<256> RealPathBuf;
626   if (!FS->getRealPath(Name, RealPathBuf)) {
627     if (is_style_windows(llvm::sys::path::Style::native)) {
628       // For Windows paths, only use the real path if it doesn't resolve
629       // a substitute drive, as those are used to avoid MAX_PATH issues.
630       AbsPathBuf = Name;
631       if (!FS->makeAbsolute(AbsPathBuf)) {
632         if (llvm::sys::path::root_name(RealPathBuf) ==
633             llvm::sys::path::root_name(AbsPathBuf)) {
634           CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
635         } else {
636           // Fallback to using the absolute path.
637           // Simplifying /../ is semantically valid on Windows even in the
638           // presence of symbolic links.
639           llvm::sys::path::remove_dots(AbsPathBuf, /*remove_dot_dot=*/true);
640           CanonicalName = AbsPathBuf.str().copy(CanonicalNameStorage);
641         }
642       }
643     } else {
644       CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
645     }
646   }
647 
648   CanonicalNames.insert({Entry, CanonicalName});
649   return CanonicalName;
650 }
651 
652 void FileManager::PrintStats() const {
653   llvm::errs() << "\n*** File Manager Stats:\n";
654   llvm::errs() << UniqueRealFiles.size() << " real files found, "
655                << UniqueRealDirs.size() << " real dirs found.\n";
656   llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
657                << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
658   llvm::errs() << NumDirLookups << " dir lookups, "
659                << NumDirCacheMisses << " dir cache misses.\n";
660   llvm::errs() << NumFileLookups << " file lookups, "
661                << NumFileCacheMisses << " file cache misses.\n";
662 
663   //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
664 }
665