[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Basic / FileManager.cpp
blob4ef25358ec82f4b70409adaa2e1b6da45f1fa204
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.
11 //===----------------------------------------------------------------------===//
13 // TODO: This should index all interesting directories with dirent calls.
14 // getdirentries ?
15 // opendir/readdir_r/closedir ?
17 //===----------------------------------------------------------------------===//
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 <string>
35 #include <utility>
37 using namespace clang;
39 #define DEBUG_TYPE "file-search"
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.");
47 //===----------------------------------------------------------------------===//
48 // Common logic.
49 //===----------------------------------------------------------------------===//
51 FileManager::FileManager(const FileSystemOptions &FSO,
52 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
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();
61 FileManager::~FileManager() = default;
63 void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
64 assert(statCache && "No stat cache provided?");
65 StatCache = std::move(statCache);
68 void FileManager::clearStatCache() { StatCache.reset(); }
70 /// Retrieve the directory that the given file name resides in.
71 /// Filename can point to either a real file or a virtual file.
72 static llvm::Expected<DirectoryEntryRef>
73 getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
74 bool CacheFailure) {
75 if (Filename.empty())
76 return llvm::errorCodeToError(
77 make_error_code(std::errc::no_such_file_or_directory));
79 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
80 return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory));
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 = ".";
87 return FileMgr.getDirectoryRef(DirName, CacheFailure);
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 = ".";
97 auto &NamedDirEnt = *SeenDirEntries.insert(
98 {DirName, std::errc::no_such_file_or_directory}).first;
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;
107 // Add the virtual directory to the cache.
108 auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
109 UDE->Name = NamedDirEnt.first();
110 NamedDirEnt.second = *UDE;
111 VirtualDirectoryEntries.push_back(UDE);
113 // Recursively add the other ancestors.
114 addAncestorsAsVirtualDirs(DirName);
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 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;
137 ++NumDirLookups;
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());
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");
154 // Get the null-terminated directory name as stored as the key of the
155 // SeenDirEntries map.
156 StringRef InterndDirName = NamedDirEnt.first();
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);
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()];
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 UDE->Name = InterndDirName;
183 NamedDirEnt.second = *UDE;
185 return DirectoryEntryRef(NamedDirEnt);
188 llvm::ErrorOr<const DirectoryEntry *>
189 FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
190 auto Result = getDirectoryRef(DirName, CacheFailure);
191 if (Result)
192 return &Result->getDirEntry();
193 return llvm::errorToErrorCode(Result.takeError());
196 llvm::ErrorOr<const FileEntry *>
197 FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
198 auto Result = getFileRef(Filename, openFile, CacheFailure);
199 if (Result)
200 return &Result->getFileEntry();
201 return llvm::errorToErrorCode(Result.takeError());
204 llvm::Expected<FileEntryRef>
205 FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
206 ++NumFileLookups;
208 // See if there is already an entry in the map.
209 auto SeenFileInsertResult =
210 SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
211 if (!SeenFileInsertResult.second) {
212 if (!SeenFileInsertResult.first->second)
213 return llvm::errorCodeToError(
214 SeenFileInsertResult.first->second.getError());
215 return FileEntryRef(*SeenFileInsertResult.first);
218 // We've not seen this before. Fill it in.
219 ++NumFileCacheMisses;
220 auto *NamedFileEnt = &*SeenFileInsertResult.first;
221 assert(!NamedFileEnt->second && "should be newly-created");
223 // Get the null-terminated file name as stored as the key of the
224 // SeenFileEntries map.
225 StringRef InterndFileName = NamedFileEnt->first();
227 // Look up the directory for the file. When looking up something like
228 // sys/foo.h we'll discover all of the search directories that have a 'sys'
229 // subdirectory. This will let us avoid having to waste time on known-to-fail
230 // searches when we go to find sys/bar.h, because all the search directories
231 // without a 'sys' subdir will get a cached failure result.
232 auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
233 if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
234 std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError());
235 if (CacheFailure)
236 NamedFileEnt->second = Err;
237 else
238 SeenFileEntries.erase(Filename);
240 return llvm::errorCodeToError(Err);
242 DirectoryEntryRef DirInfo = *DirInfoOrErr;
244 // FIXME: Use the directory info to prune this, before doing the stat syscall.
245 // FIXME: This will reduce the # syscalls.
247 // Check to see if the file exists.
248 std::unique_ptr<llvm::vfs::File> F;
249 llvm::vfs::Status Status;
250 auto statError = getStatValue(InterndFileName, Status, true,
251 openFile ? &F : nullptr);
252 if (statError) {
253 // There's no real file at the given path.
254 if (CacheFailure)
255 NamedFileEnt->second = statError;
256 else
257 SeenFileEntries.erase(Filename);
259 return llvm::errorCodeToError(statError);
262 assert((openFile || !F) && "undesired open file");
264 // It exists. See if we have already opened a file with the same inode.
265 // This occurs when one dir is symlinked to another, for example.
266 FileEntry *&UFE = UniqueRealFiles[Status.getUniqueID()];
267 bool ReusingEntry = UFE != nullptr;
268 if (!UFE)
269 UFE = new (FilesAlloc.Allocate()) FileEntry();
271 if (!Status.ExposesExternalVFSPath || Status.getName() == Filename) {
272 // Use the requested name. Set the FileEntry.
273 NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
274 } else {
275 // Name mismatch. We need a redirect. First grab the actual entry we want
276 // to return.
278 // This redirection logic intentionally leaks the external name of a
279 // redirected file that uses 'use-external-name' in \a
280 // vfs::RedirectionFileSystem. This allows clang to report the external
281 // name to users (in diagnostics) and to tools that don't have access to
282 // the VFS (in debug info and dependency '.d' files).
284 // FIXME: This is pretty complex and has some very complicated interactions
285 // with the rest of clang. It's also inconsistent with how "real"
286 // filesystems behave and confuses parts of clang expect to see the
287 // name-as-accessed on the \a FileEntryRef.
289 // A potential plan to remove this is as follows -
290 // - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
291 // to explicitly use the `getNameAsRequested()` rather than just using
292 // `getName()`.
293 // - Add a `FileManager::getExternalPath` API for explicitly getting the
294 // remapped external filename when there is one available. Adopt it in
295 // callers like diagnostics/deps reporting instead of calling
296 // `getName()` directly.
297 // - Switch the meaning of `FileEntryRef::getName()` to get the requested
298 // name, not the external name. Once that sticks, revert callers that
299 // want the requested name back to calling `getName()`.
300 // - Update the VFS to always return the requested name. This could also
301 // return the external name, or just have an API to request it
302 // lazily. The latter has the benefit of making accesses of the
303 // external path easily tracked, but may also require extra work than
304 // just returning up front.
305 // - (Optionally) Add an API to VFS to get the external filename lazily
306 // and update `FileManager::getExternalPath()` to use it instead. This
307 // has the benefit of making such accesses easily tracked, though isn't
308 // necessarily required (and could cause extra work than just adding to
309 // eg. `vfs::Status` up front).
310 auto &Redirection =
311 *SeenFileEntries
312 .insert({Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
313 .first;
314 assert(Redirection.second->V.is<FileEntry *>() &&
315 "filename redirected to a non-canonical filename?");
316 assert(Redirection.second->V.get<FileEntry *>() == UFE &&
317 "filename from getStatValue() refers to wrong file");
319 // Cache the redirection in the previously-inserted entry, still available
320 // in the tentative return value.
321 NamedFileEnt->second = FileEntryRef::MapValue(Redirection);
324 FileEntryRef ReturnedRef(*NamedFileEnt);
325 if (ReusingEntry) { // Already have an entry with this inode, return it.
327 // FIXME: This hack ensures that `getDir()` will use the path that was
328 // used to lookup this file, even if we found a file by different path
329 // first. This is required in order to find a module's structure when its
330 // headers/module map are mapped in the VFS.
332 // See above for how this will eventually be removed. `IsVFSMapped`
333 // *cannot* be narrowed to `ExposesExternalVFSPath` as crash reproducers
334 // also depend on this logic and they have `use-external-paths: false`.
335 if (&DirInfo.getDirEntry() != UFE->Dir && Status.IsVFSMapped)
336 UFE->Dir = &DirInfo.getDirEntry();
338 // Always update LastRef to the last name by which a file was accessed.
339 // FIXME: Neither this nor always using the first reference is correct; we
340 // want to switch towards a design where we return a FileName object that
341 // encapsulates both the name by which the file was accessed and the
342 // corresponding FileEntry.
343 // FIXME: LastRef should be removed from FileEntry once all clients adopt
344 // FileEntryRef.
345 UFE->LastRef = ReturnedRef;
347 return ReturnedRef;
350 // Otherwise, we don't have this file yet, add it.
351 UFE->LastRef = ReturnedRef;
352 UFE->Size = Status.getSize();
353 UFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
354 UFE->Dir = &DirInfo.getDirEntry();
355 UFE->UID = NextFileUID++;
356 UFE->UniqueID = Status.getUniqueID();
357 UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
358 UFE->File = std::move(F);
360 if (UFE->File) {
361 if (auto PathName = UFE->File->getName())
362 fillRealPathName(UFE, *PathName);
363 } else if (!openFile) {
364 // We should still fill the path even if we aren't opening the file.
365 fillRealPathName(UFE, InterndFileName);
367 return ReturnedRef;
370 llvm::Expected<FileEntryRef> FileManager::getSTDIN() {
371 // Only read stdin once.
372 if (STDIN)
373 return *STDIN;
375 std::unique_ptr<llvm::MemoryBuffer> Content;
376 if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN())
377 Content = std::move(*ContentOrError);
378 else
379 return llvm::errorCodeToError(ContentOrError.getError());
381 STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
382 Content->getBufferSize(), 0);
383 FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
384 FE.Content = std::move(Content);
385 FE.IsNamedPipe = true;
386 return *STDIN;
389 const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size,
390 time_t ModificationTime) {
391 return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry();
394 FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
395 time_t ModificationTime) {
396 ++NumFileLookups;
398 // See if there is already an entry in the map for an existing file.
399 auto &NamedFileEnt = *SeenFileEntries.insert(
400 {Filename, std::errc::no_such_file_or_directory}).first;
401 if (NamedFileEnt.second) {
402 FileEntryRef::MapValue Value = *NamedFileEnt.second;
403 if (LLVM_LIKELY(Value.V.is<FileEntry *>()))
404 return FileEntryRef(NamedFileEnt);
405 return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(
406 Value.V.get<const void *>()));
409 // We've not seen this before, or the file is cached as non-existent.
410 ++NumFileCacheMisses;
411 addAncestorsAsVirtualDirs(Filename);
412 FileEntry *UFE = nullptr;
414 // Now that all ancestors of Filename are in the cache, the
415 // following call is guaranteed to find the DirectoryEntry from the
416 // cache. A virtual file can also have an empty filename, that could come
417 // from a source location preprocessor directive with an empty filename as
418 // an example, so we need to pretend it has a name to ensure a valid directory
419 // entry can be returned.
420 auto DirInfo = expectedToOptional(getDirectoryFromFile(
421 *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true));
422 assert(DirInfo &&
423 "The directory of a virtual file should already be in the cache.");
425 // Check to see if the file exists. If so, drop the virtual file
426 llvm::vfs::Status Status;
427 const char *InterndFileName = NamedFileEnt.first().data();
428 if (!getStatValue(InterndFileName, Status, true, nullptr)) {
429 Status = llvm::vfs::Status(
430 Status.getName(), Status.getUniqueID(),
431 llvm::sys::toTimePoint(ModificationTime),
432 Status.getUser(), Status.getGroup(), Size,
433 Status.getType(), Status.getPermissions());
435 auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
436 if (RealFE) {
437 // If we had already opened this file, close it now so we don't
438 // leak the descriptor. We're not going to use the file
439 // descriptor anyway, since this is a virtual file.
440 if (RealFE->File)
441 RealFE->closeFile();
442 // If we already have an entry with this inode, return it.
444 // FIXME: Surely this should add a reference by the new name, and return
445 // it instead...
446 NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
447 return FileEntryRef(NamedFileEnt);
449 // File exists, but no entry - create it.
450 RealFE = new (FilesAlloc.Allocate()) FileEntry();
451 RealFE->UniqueID = Status.getUniqueID();
452 RealFE->IsNamedPipe =
453 Status.getType() == llvm::sys::fs::file_type::fifo_file;
454 fillRealPathName(RealFE, Status.getName());
456 UFE = RealFE;
457 } else {
458 // File does not exist, create a virtual entry.
459 UFE = new (FilesAlloc.Allocate()) FileEntry();
460 VirtualFileEntries.push_back(UFE);
463 NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
464 UFE->LastRef = FileEntryRef(NamedFileEnt);
465 UFE->Size = Size;
466 UFE->ModTime = ModificationTime;
467 UFE->Dir = &DirInfo->getDirEntry();
468 UFE->UID = NextFileUID++;
469 UFE->File.reset();
470 return FileEntryRef(NamedFileEnt);
473 llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) {
474 // Stat of the file and return nullptr if it doesn't exist.
475 llvm::vfs::Status Status;
476 if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
477 return None;
479 if (!SeenBypassFileEntries)
480 SeenBypassFileEntries = std::make_unique<
481 llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
483 // If we've already bypassed just use the existing one.
484 auto Insertion = SeenBypassFileEntries->insert(
485 {VF.getName(), std::errc::no_such_file_or_directory});
486 if (!Insertion.second)
487 return FileEntryRef(*Insertion.first);
489 // Fill in the new entry from the stat.
490 FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
491 BypassFileEntries.push_back(BFE);
492 Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
493 BFE->LastRef = FileEntryRef(*Insertion.first);
494 BFE->Size = Status.getSize();
495 BFE->Dir = VF.getFileEntry().Dir;
496 BFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
497 BFE->UID = NextFileUID++;
499 // Save the entry in the bypass table and return.
500 return FileEntryRef(*Insertion.first);
503 bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
504 StringRef pathRef(path.data(), path.size());
506 if (FileSystemOpts.WorkingDir.empty()
507 || llvm::sys::path::is_absolute(pathRef))
508 return false;
510 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
511 llvm::sys::path::append(NewPath, pathRef);
512 path = NewPath;
513 return true;
516 bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
517 bool Changed = FixupRelativePath(Path);
519 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
520 FS->makeAbsolute(Path);
521 Changed = true;
524 return Changed;
527 void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
528 llvm::SmallString<128> AbsPath(FileName);
529 // This is not the same as `VFS::getRealPath()`, which resolves symlinks
530 // but can be very expensive on real file systems.
531 // FIXME: the semantic of RealPathName is unclear, and the name might be
532 // misleading. We need to clean up the interface here.
533 makeAbsolutePath(AbsPath);
534 llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
535 UFE->RealPathName = std::string(AbsPath.str());
538 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
539 FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
540 bool RequiresNullTerminator) {
541 // If the content is living on the file entry, return a reference to it.
542 if (Entry->Content)
543 return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
545 uint64_t FileSize = Entry->getSize();
546 // If there's a high enough chance that the file have changed since we
547 // got its size, force a stat before opening it.
548 if (isVolatile || Entry->isNamedPipe())
549 FileSize = -1;
551 StringRef Filename = Entry->getName();
552 // If the file is already open, use the open file descriptor.
553 if (Entry->File) {
554 auto Result = Entry->File->getBuffer(Filename, FileSize,
555 RequiresNullTerminator, isVolatile);
556 Entry->closeFile();
557 return Result;
560 // Otherwise, open the file.
561 return getBufferForFileImpl(Filename, FileSize, isVolatile,
562 RequiresNullTerminator);
565 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
566 FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
567 bool isVolatile,
568 bool RequiresNullTerminator) {
569 if (FileSystemOpts.WorkingDir.empty())
570 return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
571 isVolatile);
573 SmallString<128> FilePath(Filename);
574 FixupRelativePath(FilePath);
575 return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
576 isVolatile);
579 /// getStatValue - Get the 'stat' information for the specified path,
580 /// using the cache to accelerate it if possible. This returns true
581 /// if the path points to a virtual file or does not exist, or returns
582 /// false if it's an existent real file. If FileDescriptor is NULL,
583 /// do directory look-up instead of file look-up.
584 std::error_code
585 FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
586 bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
587 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
588 // absolute!
589 if (FileSystemOpts.WorkingDir.empty())
590 return FileSystemStatCache::get(Path, Status, isFile, F,
591 StatCache.get(), *FS);
593 SmallString<128> FilePath(Path);
594 FixupRelativePath(FilePath);
596 return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
597 StatCache.get(), *FS);
600 std::error_code
601 FileManager::getNoncachedStatValue(StringRef Path,
602 llvm::vfs::Status &Result) {
603 SmallString<128> FilePath(Path);
604 FixupRelativePath(FilePath);
606 llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
607 if (!S)
608 return S.getError();
609 Result = *S;
610 return std::error_code();
613 void FileManager::GetUniqueIDMapping(
614 SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
615 UIDToFiles.clear();
616 UIDToFiles.resize(NextFileUID);
618 // Map file entries
619 for (llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>,
620 llvm::BumpPtrAllocator>::const_iterator
621 FE = SeenFileEntries.begin(),
622 FEEnd = SeenFileEntries.end();
623 FE != FEEnd; ++FE)
624 if (llvm::ErrorOr<FileEntryRef::MapValue> Entry = FE->getValue()) {
625 if (const auto *FE = Entry->V.dyn_cast<FileEntry *>())
626 UIDToFiles[FE->getUID()] = FE;
629 // Map virtual file entries
630 for (const auto &VFE : VirtualFileEntries)
631 UIDToFiles[VFE->getUID()] = VFE;
634 StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
635 llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
636 = CanonicalNames.find(Dir);
637 if (Known != CanonicalNames.end())
638 return Known->second;
640 StringRef CanonicalName(Dir->getName());
642 SmallString<4096> CanonicalNameBuf;
643 if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
644 CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
646 CanonicalNames.insert({Dir, CanonicalName});
647 return CanonicalName;
650 StringRef FileManager::getCanonicalName(const FileEntry *File) {
651 llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
652 = CanonicalNames.find(File);
653 if (Known != CanonicalNames.end())
654 return Known->second;
656 StringRef CanonicalName(File->getName());
658 SmallString<4096> CanonicalNameBuf;
659 if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
660 CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
662 CanonicalNames.insert({File, CanonicalName});
663 return CanonicalName;
666 void FileManager::PrintStats() const {
667 llvm::errs() << "\n*** File Manager Stats:\n";
668 llvm::errs() << UniqueRealFiles.size() << " real files found, "
669 << UniqueRealDirs.size() << " real dirs found.\n";
670 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
671 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
672 llvm::errs() << NumDirLookups << " dir lookups, "
673 << NumDirCacheMisses << " dir cache misses.\n";
674 llvm::errs() << NumFileLookups << " file lookups, "
675 << NumFileCacheMisses << " file cache misses.\n";
677 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;