clang 23.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/SmallString.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/Config/llvm-config.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/IOSandbox.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/Path.h"
29#include "llvm/Support/raw_ostream.h"
30#include <cassert>
31#include <climits>
32#include <cstdint>
33#include <cstdlib>
34#include <optional>
35#include <string>
36#include <utility>
37
38using namespace clang;
39
40#define DEBUG_TYPE "file-search"
41
42static void normalizeCacheKey(StringRef &Path,
43 std::optional<std::string> &Storage) {
44 using namespace llvm::sys::path;
45
46 // Drop trailing separators for non-root paths so that cache keys and `stat`
47 // queries use a single spelling. Keep root paths (`/`, `[A-Z]:\`) unchanged.
48 if (Path.size() > 1 && root_path(Path) != Path && is_separator(Path.back()))
49 Path = Path.drop_back();
50
51 // A bare drive path like "[A-Z]:" is drive-relative (current directory on the
52 // drive). As `[A-Z]:` is not a path specification, we must canonicalise it
53 // to `[A-Z]:.`.
54 if (is_style_windows(Style::native)) {
55 if (Path.size() > 1 && Path.back() == ':' &&
56 Path.equals_insensitive(root_name(Path))) {
57 Storage = Path.str() + ".";
58 Path = *Storage;
59 }
60 }
61}
62
63//===----------------------------------------------------------------------===//
64// Common logic.
65//===----------------------------------------------------------------------===//
66
69 : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
70 SeenFileEntries(64), NextFileUID(0) {
71 // If the caller doesn't provide a virtual file system, just grab the real
72 // file system.
73 if (!this->FS)
74 this->FS = llvm::vfs::getRealFileSystem();
75}
76
78
79void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
80 assert(statCache && "No stat cache provided?");
81 StatCache = std::move(statCache);
82}
83
84void FileManager::clearStatCache() { StatCache.reset(); }
85
86llvm::ErrorOr<DirectoryEntryRef>
87FileManager::getDirectoryFromFile(StringRef Filename, bool CacheFailure) {
88 if (Filename.empty())
89 return make_error_code(std::errc::no_such_file_or_directory);
90
91 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
92 return make_error_code(std::errc::is_a_directory);
93
94 StringRef DirName = llvm::sys::path::parent_path(Filename);
95 // Use the current directory if file has no path component.
96 if (DirName.empty())
97 DirName = ".";
98
99 return getDirectoryRefImpl(DirName, CacheFailure);
100}
101
102DirectoryEntry *&FileManager::getRealDirEntry(const llvm::vfs::Status &Status) {
103 assert(Status.isDirectory() && "The directory should exist!");
104 // See if we have already opened a directory with the
105 // same inode (this occurs on Unix-like systems when one dir is
106 // symlinked to another, for example) or the same path (on
107 // Windows).
108 DirectoryEntry *&UDE = UniqueRealDirs[Status.getUniqueID()];
109
110 if (!UDE) {
111 // We don't have this directory yet, add it. We use the string
112 // key from the SeenDirEntries map as the string.
113 UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
114 }
115 return UDE;
116}
117
118/// Add all ancestors of the given path (pointing to either a file or
119/// a directory) as virtual directories.
120void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
121 StringRef DirName = llvm::sys::path::parent_path(Path);
122 if (DirName.empty())
123 DirName = ".";
124
125 // Normalize the key for cache lookup/insert, but keep the original DirName
126 // for recursive processing since normalization can create paths that don't
127 // work well with parent_path() (e.g., "C:" -> "C:.").
128 std::optional<std::string> Storage;
129 StringRef OriginalDirName = DirName;
130 normalizeCacheKey(DirName, Storage);
131
132 auto &NamedDirEnt = *SeenDirEntries.insert(
133 {DirName, std::errc::no_such_file_or_directory}).first;
134
135 // When caching a virtual directory, we always cache its ancestors
136 // at the same time. Therefore, if DirName is already in the cache,
137 // we don't need to recurse as its ancestors must also already be in
138 // the cache (or it's a known non-virtual directory).
139 if (NamedDirEnt.second)
140 return;
141
142 // Check to see if the directory exists.
143 llvm::vfs::Status Status;
144 auto statError =
145 getStatValue(DirName, Status, false, nullptr /*directory lookup*/);
146 if (statError) {
147 // There's no real directory at the given path.
148 // Add the virtual directory to the cache.
149 auto *UDE = new (DirsAlloc.Allocate()) DirectoryEntry();
150 NamedDirEnt.second = *UDE;
151 VirtualDirectoryEntries.push_back(UDE);
152 } else {
153 // There is the real directory
154 DirectoryEntry *&UDE = getRealDirEntry(Status);
155 NamedDirEnt.second = *UDE;
156 }
157
158 // Recursively add the other ancestors.
159 addAncestorsAsVirtualDirs(OriginalDirName);
160}
161
162llvm::ErrorOr<DirectoryEntryRef>
163FileManager::getDirectoryRefImpl(StringRef DirName, bool CacheFailure) {
164 std::optional<std::string> DirNameStr;
165 normalizeCacheKey(DirName, DirNameStr);
166
167 ++NumDirLookups;
168
169 // See if there was already an entry in the map. Note that the map
170 // contains both virtual and real directories.
171 auto SeenDirInsertResult =
172 SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
173 if (!SeenDirInsertResult.second) {
174 if (SeenDirInsertResult.first->second)
175 return DirectoryEntryRef(*SeenDirInsertResult.first);
176 return SeenDirInsertResult.first->second.getError();
177 }
178
179 // We've not seen this before. Fill it in.
180 ++NumDirCacheMisses;
181 auto &NamedDirEnt = *SeenDirInsertResult.first;
182 assert(!NamedDirEnt.second && "should be newly-created");
183
184 // Get the null-terminated directory name as stored as the key of the
185 // SeenDirEntries map.
186 StringRef InterndDirName = NamedDirEnt.first();
187
188 // Check to see if the directory exists.
189 llvm::vfs::Status Status;
190 auto statError = getStatValue(InterndDirName, Status, false,
191 nullptr /*directory lookup*/);
192 if (statError) {
193 // There's no real directory at the given path.
194 if (CacheFailure)
195 NamedDirEnt.second = statError;
196 else
197 SeenDirEntries.erase(DirName);
198 return statError;
199 }
200
201 // It exists.
202 DirectoryEntry *&UDE = getRealDirEntry(Status);
203 NamedDirEnt.second = *UDE;
204
205 return DirectoryEntryRef(NamedDirEnt);
206}
207
208llvm::ErrorOr<FileEntryRef> FileManager::getFileRefImpl(StringRef Filename,
209 bool openFile,
210 bool CacheFailure,
211 bool IsText) {
212 ++NumFileLookups;
213
214 // See if there is already an entry in the map.
215 auto SeenFileInsertResult =
216 SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
217 if (!SeenFileInsertResult.second) {
218 if (!SeenFileInsertResult.first->second)
219 return SeenFileInsertResult.first->second.getError();
220 return FileEntryRef(*SeenFileInsertResult.first);
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(Filename, CacheFailure);
238 if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
239 std::error_code Err = DirInfoOrErr.getError();
240 if (CacheFailure)
241 NamedFileEnt->second = Err;
242 else
243 SeenFileEntries.erase(Filename);
244
245 return 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, IsText);
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 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 bool ReusingEntry = UFE != nullptr;
273 if (!UFE)
274 UFE = new (FilesAlloc.Allocate()) FileEntry();
275
276 if (!Status.ExposesExternalVFSPath || Status.getName() == Filename) {
277 // Use the requested name. Set the FileEntry.
278 NamedFileEnt->second = FileEntryRef::MapValue(*UFE, DirInfo);
279 } else {
280 // Name mismatch. We need a redirect. First grab the actual entry we want
281 // to return.
282 //
283 // This redirection logic intentionally leaks the external name of a
284 // redirected file that uses 'use-external-name' in \a
285 // vfs::RedirectionFileSystem. This allows clang to report the external
286 // name to users (in diagnostics) and to tools that don't have access to
287 // the VFS (in debug info and dependency '.d' files).
288 //
289 // FIXME: This is pretty complex and has some very complicated interactions
290 // with the rest of clang. It's also inconsistent with how "real"
291 // filesystems behave and confuses parts of clang expect to see the
292 // name-as-accessed on the \a FileEntryRef.
293 //
294 // A potential plan to remove this is as follows -
295 // - Update callers such as `HeaderSearch::findUsableModuleForHeader()`
296 // to explicitly use the `getNameAsRequested()` rather than just using
297 // `getName()`.
298 // - Add a `FileManager::getExternalPath` API for explicitly getting the
299 // remapped external filename when there is one available. Adopt it in
300 // callers like diagnostics/deps reporting instead of calling
301 // `getName()` directly.
302 // - Switch the meaning of `FileEntryRef::getName()` to get the requested
303 // name, not the external name. Once that sticks, revert callers that
304 // want the requested name back to calling `getName()`.
305 // - Update the VFS to always return the requested name. This could also
306 // return the external name, or just have an API to request it
307 // lazily. The latter has the benefit of making accesses of the
308 // external path easily tracked, but may also require extra work than
309 // just returning up front.
310 // - (Optionally) Add an API to VFS to get the external filename lazily
311 // and update `FileManager::getExternalPath()` to use it instead. This
312 // has the benefit of making such accesses easily tracked, though isn't
313 // necessarily required (and could cause extra work than just adding to
314 // eg. `vfs::Status` up front).
315 auto &Redirection =
316 *SeenFileEntries
317 .insert({Status.getName(), FileEntryRef::MapValue(*UFE, DirInfo)})
318 .first;
319 assert(isa<FileEntry *>(Redirection.second->V) &&
320 "filename redirected to a non-canonical filename?");
321 assert(cast<FileEntry *>(Redirection.second->V) == UFE &&
322 "filename from getStatValue() refers to wrong file");
323
324 // Cache the redirection in the previously-inserted entry, still available
325 // in the tentative return value.
326 NamedFileEnt->second = FileEntryRef::MapValue(Redirection, DirInfo);
327 }
328
329 FileEntryRef ReturnedRef(*NamedFileEnt);
330 if (ReusingEntry) { // Already have an entry with this inode, return it.
331 return ReturnedRef;
332 }
333
334 // Otherwise, we don't have this file yet, add it.
335 UFE->Size = Status.getSize();
336 UFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
337 UFE->Dir = &DirInfo.getDirEntry();
338 UFE->UID = NextFileUID++;
339 UFE->UniqueID = Status.getUniqueID();
340 UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
341 UFE->IsDeviceFile =
342 Status.getType() == llvm::sys::fs::file_type::character_file;
343 UFE->File = std::move(F);
344
345 if (UFE->File) {
346 if (auto PathName = UFE->File->getName())
347 fillRealPathName(UFE, *PathName);
348 } else if (!openFile) {
349 // We should still fill the path even if we aren't opening the file.
350 fillRealPathName(UFE, InterndFileName);
351 }
352 return ReturnedRef;
353}
354
356 // Only read stdin once.
357 if (STDIN)
358 return *STDIN;
359
360 auto ContentOrError = [] {
361 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
362 return llvm::MemoryBuffer::getSTDIN();
363 }();
364
365 if (!ContentOrError)
366 return llvm::createFileError("-", ContentOrError.getError());
367
368 auto Content = std::move(*ContentOrError);
369 STDIN = getVirtualFileRef(Content->getBufferIdentifier(),
370 Content->getBufferSize(), 0);
371 FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry());
372 FE.Content = std::move(Content);
373 FE.IsNamedPipe = true;
374 return *STDIN;
375}
376
377void FileManager::trackVFSUsage(bool Active) {
378 FS->visit([Active](llvm::vfs::FileSystem &FileSys) {
379 if (auto *RFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(&FileSys))
380 RFS->setUsageTrackingActive(Active);
381 });
382}
383
384FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size,
385 time_t ModificationTime) {
386 ++NumFileLookups;
387
388 // See if there is already an entry in the map for an existing file.
389 auto &NamedFileEnt = *SeenFileEntries.insert(
390 {Filename, std::errc::no_such_file_or_directory}).first;
391 if (NamedFileEnt.second) {
392 FileEntryRef::MapValue Value = *NamedFileEnt.second;
393 if (LLVM_LIKELY(isa<FileEntry *>(Value.V)))
394 return FileEntryRef(NamedFileEnt);
396 }
397
398 // We've not seen this before, or the file is cached as non-existent.
399 ++NumFileCacheMisses;
400 addAncestorsAsVirtualDirs(Filename);
401 FileEntry *UFE = nullptr;
402
403 // Now that all ancestors of Filename are in the cache, the
404 // following call is guaranteed to find the DirectoryEntry from the
405 // cache. A virtual file can also have an empty filename, that could come
406 // from a source location preprocessor directive with an empty filename as
407 // an example, so we need to pretend it has a name to ensure a valid directory
408 // entry can be returned.
409 auto DirInfo = getDirectoryFromFile(Filename.empty() ? "." : Filename,
410 /*CacheFailure=*/true);
411 assert(DirInfo &&
412 "The directory of a virtual file should already be in the cache.");
413
414 // Check to see if the file exists. If so, drop the virtual file
415 llvm::vfs::Status Status;
416 const char *InterndFileName = NamedFileEnt.first().data();
417 if (!getStatValue(InterndFileName, Status, true, nullptr)) {
418 Status = llvm::vfs::Status(
419 Status.getName(), Status.getUniqueID(),
420 llvm::sys::toTimePoint(ModificationTime),
421 Status.getUser(), Status.getGroup(), Size,
422 Status.getType(), Status.getPermissions());
423
424 auto &RealFE = UniqueRealFiles[Status.getUniqueID()];
425 if (RealFE) {
426 // If we had already opened this file, close it now so we don't
427 // leak the descriptor. We're not going to use the file
428 // descriptor anyway, since this is a virtual file.
429 if (RealFE->File)
430 RealFE->closeFile();
431 // If we already have an entry with this inode, return it.
432 //
433 // FIXME: Surely this should add a reference by the new name, and return
434 // it instead...
435 NamedFileEnt.second = FileEntryRef::MapValue(*RealFE, *DirInfo);
436 return FileEntryRef(NamedFileEnt);
437 }
438 // File exists, but no entry - create it.
439 RealFE = new (FilesAlloc.Allocate()) FileEntry();
440 RealFE->UniqueID = Status.getUniqueID();
441 RealFE->IsNamedPipe =
442 Status.getType() == llvm::sys::fs::file_type::fifo_file;
443 fillRealPathName(RealFE, Status.getName());
444
445 UFE = RealFE;
446 } else {
447 // File does not exist, create a virtual entry.
448 UFE = new (FilesAlloc.Allocate()) FileEntry();
449 VirtualFileEntries.push_back(UFE);
450 }
451
452 NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo);
453 UFE->Size = Size;
454 UFE->ModTime = ModificationTime;
455 UFE->Dir = &DirInfo->getDirEntry();
456 UFE->UID = NextFileUID++;
457 UFE->File.reset();
458 return FileEntryRef(NamedFileEnt);
459}
460
462 // Stat of the file and return nullptr if it doesn't exist.
463 llvm::vfs::Status Status;
464 if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
465 return std::nullopt;
466
467 if (!SeenBypassFileEntries)
468 SeenBypassFileEntries = std::make_unique<
469 llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>();
470
471 // If we've already bypassed just use the existing one.
472 auto Insertion = SeenBypassFileEntries->insert(
473 {VF.getName(), std::errc::no_such_file_or_directory});
474 if (!Insertion.second)
475 return FileEntryRef(*Insertion.first);
476
477 // Fill in the new entry from the stat.
478 FileEntry *BFE = new (FilesAlloc.Allocate()) FileEntry();
479 BypassFileEntries.push_back(BFE);
480 Insertion.first->second = FileEntryRef::MapValue(*BFE, VF.getDir());
481 BFE->Size = Status.getSize();
482 BFE->Dir = VF.getFileEntry().Dir;
483 BFE->ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
484 BFE->UID = NextFileUID++;
485
486 // Save the entry in the bypass table and return.
487 return FileEntryRef(*Insertion.first);
488}
489
491 SmallVectorImpl<char> &Path) {
492 StringRef pathRef(Path.data(), Path.size());
493
494 if (FileSystemOpts.WorkingDir.empty()
495 || llvm::sys::path::is_absolute(pathRef))
496 return false;
497
498 SmallString<128> NewPath(FileSystemOpts.WorkingDir);
499 llvm::sys::path::append(NewPath, pathRef);
500 Path = std::move(NewPath);
501 return true;
502}
503
505 bool Canonicalize) const {
506 bool Changed = FixupRelativePath(Path);
507
508 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
509 FS->makeAbsolute(Path);
510 Changed = true;
511 }
512
513 if (Canonicalize)
514 Changed |= llvm::sys::path::remove_dots(Path);
515
516 return Changed;
517}
518
519void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
521 // This is not the same as `VFS::getRealPath()`, which resolves symlinks
522 // but can be very expensive on real file systems.
523 // FIXME: the semantic of RealPathName is unclear, and the name might be
524 // misleading. We need to clean up the interface here.
525 makeAbsolutePath(AbsPath);
526 llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
527 UFE->RealPathName = std::string(AbsPath);
528}
529
530llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
532 bool RequiresNullTerminator,
533 std::optional<int64_t> MaybeLimit, bool IsText) {
534 const FileEntry *Entry = &FE.getFileEntry();
535 // If the content is living on the file entry, return a reference to it.
536 if (Entry->Content)
537 return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef());
538
539 uint64_t FileSize = Entry->getSize();
540
541 if (MaybeLimit)
542 FileSize = *MaybeLimit;
543
544 // If there's a high enough chance that the file have changed since we
545 // got its size, force a stat before opening it.
546 if (isVolatile || Entry->isNamedPipe())
547 FileSize = -1;
548
549 StringRef Filename = FE.getName();
550 // If the file is already open, use the open file descriptor.
551 if (Entry->File) {
552 auto Result = Entry->File->getBuffer(Filename, FileSize,
553 RequiresNullTerminator, isVolatile);
554 Entry->closeFile();
555 return Result;
556 }
557
558 // Otherwise, open the file.
559 return getBufferForFileImpl(Filename, FileSize, isVolatile,
560 RequiresNullTerminator, IsText);
561}
562
563llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
564FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
565 bool isVolatile, bool RequiresNullTerminator,
566 bool IsText) const {
567 if (FileSystemOpts.WorkingDir.empty())
568 return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
569 isVolatile, IsText);
570
571 SmallString<128> FilePath(Filename);
572 FixupRelativePath(FilePath);
573 return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
574 isVolatile, IsText);
575}
576
577/// getStatValue - Get the 'stat' information for the specified path,
578/// using the cache to accelerate it if possible. This returns true
579/// if the path points to a virtual file or does not exist, or returns
580/// false if it's an existent real file. If FileDescriptor is NULL,
581/// do directory look-up instead of file look-up.
582std::error_code FileManager::getStatValue(StringRef Path,
583 llvm::vfs::Status &Status,
584 bool isFile,
585 std::unique_ptr<llvm::vfs::File> *F,
586 bool IsText) {
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, StatCache.get(),
591 *FS, IsText);
592
593 SmallString<128> FilePath(Path);
594 FixupRelativePath(FilePath);
595
596 return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
597 StatCache.get(), *FS, IsText);
598}
599
600std::error_code
602 llvm::vfs::Status &Result) {
603 SmallString<128> FilePath(Path);
604 FixupRelativePath(FilePath);
605
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();
611}
612
614 SmallVectorImpl<OptionalFileEntryRef> &UIDToFiles) const {
615 UIDToFiles.clear();
616 UIDToFiles.resize(NextFileUID);
617
618 for (const auto &Entry : SeenFileEntries) {
619 // Only return files that exist and are not redirected.
620 if (!Entry.getValue() || !isa<FileEntry *>(Entry.getValue()->V))
621 continue;
622 FileEntryRef FE(Entry);
623 // Add this file if it's the first one with the UID, or if its name is
624 // better than the existing one.
625 OptionalFileEntryRef &ExistingFE = UIDToFiles[FE.getUID()];
626 if (!ExistingFE || FE.getName() < ExistingFE->getName())
627 ExistingFE = FE;
628 }
629}
630
632 return getCanonicalName(Dir, Dir.getName());
633}
634
638
639StringRef FileManager::getCanonicalName(const void *Entry, StringRef Name) {
640 llvm::DenseMap<const void *, llvm::StringRef>::iterator Known =
641 CanonicalNames.find(Entry);
642 if (Known != CanonicalNames.end())
643 return Known->second;
644
645 // Name comes from FileEntry/DirectoryEntry::getName(), so it is safe to
646 // store it in the DenseMap below.
647 StringRef CanonicalName(Name);
648
649 SmallString<256> AbsPathBuf;
650 SmallString<256> RealPathBuf;
651 if (!FS->getRealPath(Name, RealPathBuf)) {
652 if (is_style_windows(llvm::sys::path::Style::native)) {
653 // For Windows paths, only use the real path if it doesn't resolve
654 // a substitute drive, as those are used to avoid MAX_PATH issues.
655 AbsPathBuf = Name;
656 if (!FS->makeAbsolute(AbsPathBuf)) {
657 if (llvm::sys::path::root_name(RealPathBuf) ==
658 llvm::sys::path::root_name(AbsPathBuf)) {
659 CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
660 } else {
661 // Fallback to using the absolute path.
662 // Simplifying /../ is semantically valid on Windows even in the
663 // presence of symbolic links.
664 llvm::sys::path::remove_dots(AbsPathBuf, /*remove_dot_dot=*/true);
665 CanonicalName = AbsPathBuf.str().copy(CanonicalNameStorage);
666 }
667 }
668 } else {
669 CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
670 }
671 }
672
673 CanonicalNames.insert({Entry, CanonicalName});
674 return CanonicalName;
675}
676
678 assert(&Other != this && "Collecting stats into the same FileManager");
679 NumDirLookups += Other.NumDirLookups;
680 NumFileLookups += Other.NumFileLookups;
681 NumDirCacheMisses += Other.NumDirCacheMisses;
682 NumFileCacheMisses += Other.NumFileCacheMisses;
683}
684
686 llvm::errs() << "\n*** File Manager Stats:\n";
687 llvm::errs() << UniqueRealFiles.size() << " real files found, "
688 << UniqueRealDirs.size() << " real dirs found.\n";
689 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
690 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
691 llvm::errs() << NumDirLookups << " dir lookups, "
692 << NumDirCacheMisses << " dir cache misses.\n";
693 llvm::errs() << NumFileLookups << " file lookups, "
694 << NumFileCacheMisses << " file cache misses.\n";
695
696 getVirtualFileSystem().visit([](llvm::vfs::FileSystem &VFS) {
697 if (auto *T = dyn_cast_or_null<llvm::vfs::TracingFileSystem>(&VFS))
698 llvm::errs() << "\n*** Virtual File System Stats:\n"
699 << T->NumStatusCalls << " status() calls\n"
700 << T->NumOpenFileForReadCalls << " openFileForRead() calls\n"
701 << T->NumDirBeginCalls << " dir_begin() calls\n"
702 << T->NumGetRealPathCalls << " getRealPath() calls\n"
703 << T->NumExistsCalls << " exists() calls\n"
704 << T->NumIsLocalCalls << " isLocal() calls\n";
705 });
706
707 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
708}
static void normalizeCacheKey(StringRef &Path, std::optional< std::string > &Storage)
Defines the clang::FileManager interface and associated types.
Defines the FileSystemStatCache interface.
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
StringRef getName() const
const DirectoryEntry & getDirEntry() const
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
const FileEntry & getFileEntry() const
Definition FileEntry.h:70
StringRef getName() const
The name of this FileEntry.
Definition FileEntry.h:61
DirectoryEntryRef getDir() const
Definition FileEntry.h:78
unsigned getUID() const
Definition FileEntry.h:348
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:302
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:340
void closeFile() const
Definition FileEntry.cpp:23
off_t getSize() const
Definition FileEntry.h:328
void AddStats(const FileManager &Other)
Import statistics from a child FileManager and add them to this current FileManager.
void trackVFSUsage(bool Active)
Enable or disable tracking of VFS usage.
void clearStatCache()
Removes the FileSystemStatCache object from the manager.
llvm::vfs::FileSystem & getVirtualFileSystem() const
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(FileEntryRef Entry, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt, bool IsText=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
std::error_code getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result)
Get the 'stat' information for the given Path.
FileManager(const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
Construct a file manager, optionally with a custom VFS.
llvm::Expected< FileEntryRef > getSTDIN()
Get the FileEntryRef for stdin, returning an error if stdin cannot be read.
StringRef getCanonicalName(DirectoryEntryRef Dir)
Retrieve the canonical name for a given directory.
void GetUniqueIDMapping(SmallVectorImpl< OptionalFileEntryRef > &UIDToFiles) const
Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntryRef.
void setStatCache(std::unique_ptr< FileSystemStatCache > statCache)
Installs the provided FileSystemStatCache object within the FileManager.
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...
bool FixupRelativePath(SmallVectorImpl< char > &Path) const
If path is not absolute and FileSystemOptions set the working directory, the path is modified to be r...
bool makeAbsolutePath(SmallVectorImpl< char > &Path, bool Canonicalize=false) const
Makes Path absolute taking into account FileSystemOptions and the working directory option,...
void PrintStats() const
OptionalFileEntryRef getBypassFile(FileEntryRef VFE)
Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry,...
static bool fixupRelativePath(const FileSystemOptions &FileSystemOpts, SmallVectorImpl< char > &Path)
Keeps track of options that affect how file operations are performed.
std::string WorkingDir
If set, paths are resolved as if the working directory was set to the value of WorkingDir.
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, bool IsText=true)
Get the 'stat' information for the specified path, using the cache to accelerate it if possible.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
std::error_code make_error_code(BuildPreambleError Error)
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Other
Other implicit parameter.
Definition Decl.h:1763
Type stored in the StringMap.
Definition FileEntry.h:121