Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===--- FileManager.cpp - File System Probing and Caching ----------------===// |
| 2 | // |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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 |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Config/llvm-config.h" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 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 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Common logic. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 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) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 72 | static llvm::Expected<DirectoryEntryRef> |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 73 | getDirectoryFromFile(FileManager &FileMgr, StringRef Filename, |
| 74 | bool CacheFailure) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 75 | if (Filename.empty()) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 76 | return llvm::errorCodeToError( |
| 77 | make_error_code(std::errc::no_such_file_or_directory)); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 78 | |
| 79 | if (llvm::sys::path::is_separator(Filename[Filename.size() - 1])) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 80 | return llvm::errorCodeToError(make_error_code(std::errc::is_a_directory)); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 87 | return FileMgr.getDirectoryRef(DirName, CacheFailure); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 97 | auto &NamedDirEnt = *SeenDirEntries.insert( |
| 98 | {DirName, std::errc::no_such_file_or_directory}).first; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 103 | // the cache (or it's a known non-virtual directory). |
| 104 | if (NamedDirEnt.second) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 105 | return; |
| 106 | |
| 107 | // Add the virtual directory to the cache. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 108 | auto UDE = std::make_unique<DirectoryEntry>(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 109 | UDE->Name = NamedDirEnt.first(); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 110 | NamedDirEnt.second = *UDE.get(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 111 | VirtualDirectoryEntries.push_back(std::move(UDE)); |
| 112 | |
| 113 | // Recursively add the other ancestors. |
| 114 | addAncestorsAsVirtualDirs(DirName); |
| 115 | } |
| 116 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 117 | llvm::Expected<DirectoryEntryRef> |
| 118 | FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | #ifdef _WIN32 |
| 127 | // Fixing a problem with "clang C:test.c" on Windows. |
| 128 | // Stat("C:") does not recognize "C:" as a valid directory |
| 129 | std::string DirNameStr; |
| 130 | if (DirName.size() > 1 && DirName.back() == ':' && |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 131 | DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 132 | DirNameStr = DirName.str() + '.'; |
| 133 | DirName = DirNameStr; |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | ++NumDirLookups; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 138 | |
| 139 | // See if there was already an entry in the map. Note that the map |
| 140 | // contains both virtual and real directories. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 141 | auto SeenDirInsertResult = |
| 142 | SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory}); |
| 143 | if (!SeenDirInsertResult.second) { |
| 144 | if (SeenDirInsertResult.first->second) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 145 | return DirectoryEntryRef(*SeenDirInsertResult.first); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 146 | return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError()); |
| 147 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 148 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 149 | // We've not seen this before. Fill it in. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 150 | ++NumDirCacheMisses; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 151 | auto &NamedDirEnt = *SeenDirInsertResult.first; |
| 152 | assert(!NamedDirEnt.second && "should be newly-created"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 159 | llvm::vfs::Status Status; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 160 | auto statError = getStatValue(InterndDirName, Status, false, |
| 161 | nullptr /*directory lookup*/); |
| 162 | if (statError) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 163 | // There's no real directory at the given path. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 164 | if (CacheFailure) |
| 165 | NamedDirEnt.second = statError; |
| 166 | else |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 167 | SeenDirEntries.erase(DirName); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 168 | return llvm::errorCodeToError(statError); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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). |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 175 | DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()]; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 176 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 177 | NamedDirEnt.second = UDE; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 184 | return DirectoryEntryRef(NamedDirEnt); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 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) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 205 | ++NumFileLookups; |
| 206 | |
| 207 | // See if there is already an entry in the map. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 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. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 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 *>())); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 221 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 222 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 223 | // We've not seen this before. Fill it in. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 224 | ++NumFileCacheMisses; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 225 | auto *NamedFileEnt = &*SeenFileInsertResult.first; |
| 226 | assert(!NamedFileEnt->second && "should be newly-created"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 227 | |
| 228 | // Get the null-terminated file name as stored as the key of the |
| 229 | // SeenFileEntries map. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 230 | StringRef InterndFileName = NamedFileEnt->first(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 237 | auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure); |
| 238 | if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 239 | std::error_code Err = errorToErrorCode(DirInfoOrErr.takeError()); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 240 | if (CacheFailure) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 241 | NamedFileEnt->second = Err; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 242 | else |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 243 | SeenFileEntries.erase(Filename); |
| 244 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 245 | return llvm::errorCodeToError(Err); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 246 | } |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 247 | DirectoryEntryRef DirInfo = *DirInfoOrErr; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 248 | |
| 249 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 250 | // FIXME: This will reduce the # syscalls. |
| 251 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 252 | // Check to see if the file exists. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 253 | std::unique_ptr<llvm::vfs::File> F; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 254 | llvm::vfs::Status Status; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 255 | auto statError = getStatValue(InterndFileName, Status, true, |
| 256 | openFile ? &F : nullptr); |
| 257 | if (statError) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 258 | // There's no real file at the given path. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 259 | if (CacheFailure) |
| 260 | NamedFileEnt->second = statError; |
| 261 | else |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 262 | SeenFileEntries.erase(Filename); |
| 263 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 264 | return llvm::errorCodeToError(statError); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 271 | FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()]; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 272 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 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 | auto &Redirection = |
| 280 | *SeenFileEntries |
| 281 | .insert({Status.getName(), FileEntryRef::MapValue(UFE, DirInfo)}) |
| 282 | .first; |
| 283 | assert(Redirection.second->V.is<FileEntry *>() && |
| 284 | "filename redirected to a non-canonical filename?"); |
| 285 | assert(Redirection.second->V.get<FileEntry *>() == &UFE && |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 286 | "filename from getStatValue() refers to wrong file"); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 287 | |
| 288 | // Cache the redirection in the previously-inserted entry, still available |
| 289 | // in the tentative return value. |
| 290 | NamedFileEnt->second = FileEntryRef::MapValue(Redirection); |
| 291 | |
| 292 | // Fix the tentative return value. |
| 293 | NamedFileEnt = &Redirection; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 296 | FileEntryRef ReturnedRef(*NamedFileEnt); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 297 | if (UFE.isValid()) { // Already have an entry with this inode, return it. |
| 298 | |
| 299 | // FIXME: this hack ensures that if we look up a file by a virtual path in |
| 300 | // the VFS that the getDir() will have the virtual path, even if we found |
| 301 | // the file by a 'real' path first. This is required in order to find a |
| 302 | // module's structure when its headers/module map are mapped in the VFS. |
| 303 | // We should remove this as soon as we can properly support a file having |
| 304 | // multiple names. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 305 | if (&DirInfo.getDirEntry() != UFE.Dir && Status.IsVFSMapped) |
| 306 | UFE.Dir = &DirInfo.getDirEntry(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 307 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 308 | // Always update LastRef to the last name by which a file was accessed. |
| 309 | // FIXME: Neither this nor always using the first reference is correct; we |
| 310 | // want to switch towards a design where we return a FileName object that |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 311 | // encapsulates both the name by which the file was accessed and the |
| 312 | // corresponding FileEntry. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 313 | // FIXME: LastRef should be removed from FileEntry once all clients adopt |
| 314 | // FileEntryRef. |
| 315 | UFE.LastRef = ReturnedRef; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 316 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 317 | return ReturnedRef; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Otherwise, we don't have this file yet, add it. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 321 | UFE.LastRef = ReturnedRef; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 322 | UFE.Size = Status.getSize(); |
| 323 | UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 324 | UFE.Dir = &DirInfo.getDirEntry(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 325 | UFE.UID = NextFileUID++; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 326 | UFE.UniqueID = Status.getUniqueID(); |
| 327 | UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 328 | UFE.File = std::move(F); |
| 329 | UFE.IsValid = true; |
| 330 | |
| 331 | if (UFE.File) { |
| 332 | if (auto PathName = UFE.File->getName()) |
| 333 | fillRealPathName(&UFE, *PathName); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 334 | } else if (!openFile) { |
| 335 | // We should still fill the path even if we aren't opening the file. |
| 336 | fillRealPathName(&UFE, InterndFileName); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 337 | } |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 338 | return ReturnedRef; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 341 | llvm::Expected<FileEntryRef> FileManager::getSTDIN() { |
| 342 | // Only read stdin once. |
| 343 | if (STDIN) |
| 344 | return *STDIN; |
| 345 | |
| 346 | std::unique_ptr<llvm::MemoryBuffer> Content; |
| 347 | if (auto ContentOrError = llvm::MemoryBuffer::getSTDIN()) |
| 348 | Content = std::move(*ContentOrError); |
| 349 | else |
| 350 | return llvm::errorCodeToError(ContentOrError.getError()); |
| 351 | |
| 352 | STDIN = getVirtualFileRef(Content->getBufferIdentifier(), |
| 353 | Content->getBufferSize(), 0); |
| 354 | FileEntry &FE = const_cast<FileEntry &>(STDIN->getFileEntry()); |
| 355 | FE.Content = std::move(Content); |
| 356 | FE.IsNamedPipe = true; |
| 357 | return *STDIN; |
| 358 | } |
| 359 | |
| 360 | const FileEntry *FileManager::getVirtualFile(StringRef Filename, off_t Size, |
| 361 | time_t ModificationTime) { |
| 362 | return &getVirtualFileRef(Filename, Size, ModificationTime).getFileEntry(); |
| 363 | } |
| 364 | |
| 365 | FileEntryRef FileManager::getVirtualFileRef(StringRef Filename, off_t Size, |
| 366 | time_t ModificationTime) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 367 | ++NumFileLookups; |
| 368 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 369 | // See if there is already an entry in the map for an existing file. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 370 | auto &NamedFileEnt = *SeenFileEntries.insert( |
| 371 | {Filename, std::errc::no_such_file_or_directory}).first; |
| 372 | if (NamedFileEnt.second) { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 373 | FileEntryRef::MapValue Value = *NamedFileEnt.second; |
| 374 | if (LLVM_LIKELY(Value.V.is<FileEntry *>())) |
| 375 | return FileEntryRef(NamedFileEnt); |
| 376 | return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>( |
| 377 | Value.V.get<const void *>())); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 378 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 379 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 380 | // We've not seen this before, or the file is cached as non-existent. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 381 | ++NumFileCacheMisses; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 382 | addAncestorsAsVirtualDirs(Filename); |
| 383 | FileEntry *UFE = nullptr; |
| 384 | |
| 385 | // Now that all ancestors of Filename are in the cache, the |
| 386 | // following call is guaranteed to find the DirectoryEntry from the |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 387 | // cache. A virtual file can also have an empty filename, that could come |
| 388 | // from a source location preprocessor directive with an empty filename as |
| 389 | // an example, so we need to pretend it has a name to ensure a valid directory |
| 390 | // entry can be returned. |
| 391 | auto DirInfo = expectedToOptional(getDirectoryFromFile( |
| 392 | *this, Filename.empty() ? "." : Filename, /*CacheFailure=*/true)); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 393 | assert(DirInfo && |
| 394 | "The directory of a virtual file should already be in the cache."); |
| 395 | |
| 396 | // Check to see if the file exists. If so, drop the virtual file |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 397 | llvm::vfs::Status Status; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 398 | const char *InterndFileName = NamedFileEnt.first().data(); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 399 | if (!getStatValue(InterndFileName, Status, true, nullptr)) { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 400 | UFE = &UniqueRealFiles[Status.getUniqueID()]; |
| 401 | Status = llvm::vfs::Status( |
| 402 | Status.getName(), Status.getUniqueID(), |
| 403 | llvm::sys::toTimePoint(ModificationTime), |
| 404 | Status.getUser(), Status.getGroup(), Size, |
| 405 | Status.getType(), Status.getPermissions()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 406 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 407 | NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 408 | |
| 409 | // If we had already opened this file, close it now so we don't |
| 410 | // leak the descriptor. We're not going to use the file |
| 411 | // descriptor anyway, since this is a virtual file. |
| 412 | if (UFE->File) |
| 413 | UFE->closeFile(); |
| 414 | |
| 415 | // If we already have an entry with this inode, return it. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 416 | // |
| 417 | // FIXME: Surely this should add a reference by the new name, and return |
| 418 | // it instead... |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 419 | if (UFE->isValid()) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 420 | return FileEntryRef(NamedFileEnt); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 421 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 422 | UFE->UniqueID = Status.getUniqueID(); |
| 423 | UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; |
| 424 | fillRealPathName(UFE, Status.getName()); |
| 425 | } else { |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 426 | VirtualFileEntries.push_back(std::make_unique<FileEntry>()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 427 | UFE = VirtualFileEntries.back().get(); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 428 | NamedFileEnt.second = FileEntryRef::MapValue(*UFE, *DirInfo); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 431 | UFE->LastRef = FileEntryRef(NamedFileEnt); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 432 | UFE->Size = Size; |
| 433 | UFE->ModTime = ModificationTime; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 434 | UFE->Dir = &DirInfo->getDirEntry(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 435 | UFE->UID = NextFileUID++; |
| 436 | UFE->IsValid = true; |
| 437 | UFE->File.reset(); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 438 | return FileEntryRef(NamedFileEnt); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 441 | llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) { |
| 442 | // Stat of the file and return nullptr if it doesn't exist. |
| 443 | llvm::vfs::Status Status; |
| 444 | if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr)) |
| 445 | return None; |
| 446 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 447 | if (!SeenBypassFileEntries) |
| 448 | SeenBypassFileEntries = std::make_unique< |
| 449 | llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>(); |
| 450 | |
| 451 | // If we've already bypassed just use the existing one. |
| 452 | auto Insertion = SeenBypassFileEntries->insert( |
| 453 | {VF.getName(), std::errc::no_such_file_or_directory}); |
| 454 | if (!Insertion.second) |
| 455 | return FileEntryRef(*Insertion.first); |
| 456 | |
| 457 | // Fill in the new entry from the stat. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 458 | BypassFileEntries.push_back(std::make_unique<FileEntry>()); |
| 459 | const FileEntry &VFE = VF.getFileEntry(); |
| 460 | FileEntry &BFE = *BypassFileEntries.back(); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 461 | Insertion.first->second = FileEntryRef::MapValue(BFE, VF.getDir()); |
| 462 | BFE.LastRef = FileEntryRef(*Insertion.first); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 463 | BFE.Size = Status.getSize(); |
| 464 | BFE.Dir = VFE.Dir; |
| 465 | BFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime()); |
| 466 | BFE.UID = NextFileUID++; |
| 467 | BFE.IsValid = true; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 468 | |
| 469 | // Save the entry in the bypass table and return. |
| 470 | return FileEntryRef(*Insertion.first); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 473 | bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { |
| 474 | StringRef pathRef(path.data(), path.size()); |
| 475 | |
| 476 | if (FileSystemOpts.WorkingDir.empty() |
| 477 | || llvm::sys::path::is_absolute(pathRef)) |
| 478 | return false; |
| 479 | |
| 480 | SmallString<128> NewPath(FileSystemOpts.WorkingDir); |
| 481 | llvm::sys::path::append(NewPath, pathRef); |
| 482 | path = NewPath; |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const { |
| 487 | bool Changed = FixupRelativePath(Path); |
| 488 | |
| 489 | if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) { |
| 490 | FS->makeAbsolute(Path); |
| 491 | Changed = true; |
| 492 | } |
| 493 | |
| 494 | return Changed; |
| 495 | } |
| 496 | |
| 497 | void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) { |
| 498 | llvm::SmallString<128> AbsPath(FileName); |
| 499 | // This is not the same as `VFS::getRealPath()`, which resolves symlinks |
| 500 | // but can be very expensive on real file systems. |
| 501 | // FIXME: the semantic of RealPathName is unclear, and the name might be |
| 502 | // misleading. We need to clean up the interface here. |
| 503 | makeAbsolutePath(AbsPath); |
| 504 | llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true); |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 505 | UFE->RealPathName = std::string(AbsPath.str()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 509 | FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile, |
| 510 | bool RequiresNullTerminator) { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 511 | // If the content is living on the file entry, return a reference to it. |
| 512 | if (Entry->Content) |
| 513 | return llvm::MemoryBuffer::getMemBuffer(Entry->Content->getMemBufferRef()); |
| 514 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 515 | uint64_t FileSize = Entry->getSize(); |
| 516 | // If there's a high enough chance that the file have changed since we |
| 517 | // got its size, force a stat before opening it. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 518 | if (isVolatile || Entry->isNamedPipe()) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 519 | FileSize = -1; |
| 520 | |
| 521 | StringRef Filename = Entry->getName(); |
| 522 | // If the file is already open, use the open file descriptor. |
| 523 | if (Entry->File) { |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 524 | auto Result = Entry->File->getBuffer(Filename, FileSize, |
| 525 | RequiresNullTerminator, isVolatile); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 526 | Entry->closeFile(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 527 | return Result; |
| 528 | } |
| 529 | |
| 530 | // Otherwise, open the file. |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 531 | return getBufferForFileImpl(Filename, FileSize, isVolatile, |
| 532 | RequiresNullTerminator); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 533 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 534 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 535 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 536 | FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize, |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 537 | bool isVolatile, |
| 538 | bool RequiresNullTerminator) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 539 | if (FileSystemOpts.WorkingDir.empty()) |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 540 | return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator, |
| 541 | isVolatile); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 542 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 543 | SmallString<128> FilePath(Filename); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 544 | FixupRelativePath(FilePath); |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 545 | return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator, |
| 546 | isVolatile); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 549 | /// getStatValue - Get the 'stat' information for the specified path, |
| 550 | /// using the cache to accelerate it if possible. This returns true |
| 551 | /// if the path points to a virtual file or does not exist, or returns |
| 552 | /// false if it's an existent real file. If FileDescriptor is NULL, |
| 553 | /// do directory look-up instead of file look-up. |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 554 | std::error_code |
| 555 | FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status, |
| 556 | bool isFile, std::unique_ptr<llvm::vfs::File> *F) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 557 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 558 | // absolute! |
| 559 | if (FileSystemOpts.WorkingDir.empty()) |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 560 | return FileSystemStatCache::get(Path, Status, isFile, F, |
| 561 | StatCache.get(), *FS); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 562 | |
| 563 | SmallString<128> FilePath(Path); |
| 564 | FixupRelativePath(FilePath); |
| 565 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 566 | return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F, |
| 567 | StatCache.get(), *FS); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 568 | } |
| 569 | |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 570 | std::error_code |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 571 | FileManager::getNoncachedStatValue(StringRef Path, |
| 572 | llvm::vfs::Status &Result) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 573 | SmallString<128> FilePath(Path); |
| 574 | FixupRelativePath(FilePath); |
| 575 | |
| 576 | llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str()); |
| 577 | if (!S) |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 578 | return S.getError(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 579 | Result = *S; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 580 | return std::error_code(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void FileManager::GetUniqueIDMapping( |
| 584 | SmallVectorImpl<const FileEntry *> &UIDToFiles) const { |
| 585 | UIDToFiles.clear(); |
| 586 | UIDToFiles.resize(NextFileUID); |
| 587 | |
| 588 | // Map file entries |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 589 | for (llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>, |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 590 | llvm::BumpPtrAllocator>::const_iterator |
| 591 | FE = SeenFileEntries.begin(), |
| 592 | FEEnd = SeenFileEntries.end(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 593 | FE != FEEnd; ++FE) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 594 | if (llvm::ErrorOr<FileEntryRef::MapValue> Entry = FE->getValue()) { |
| 595 | if (const auto *FE = Entry->V.dyn_cast<FileEntry *>()) |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 596 | UIDToFiles[FE->getUID()] = FE; |
| 597 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 598 | |
| 599 | // Map virtual file entries |
| 600 | for (const auto &VFE : VirtualFileEntries) |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 601 | UIDToFiles[VFE->getUID()] = VFE.get(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 604 | StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) { |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 605 | llvm::DenseMap<const void *, llvm::StringRef>::iterator Known |
| 606 | = CanonicalNames.find(Dir); |
| 607 | if (Known != CanonicalNames.end()) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 608 | return Known->second; |
| 609 | |
| 610 | StringRef CanonicalName(Dir->getName()); |
| 611 | |
| 612 | SmallString<4096> CanonicalNameBuf; |
| 613 | if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf)) |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 614 | CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 615 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 616 | CanonicalNames.insert({Dir, CanonicalName}); |
| 617 | return CanonicalName; |
| 618 | } |
| 619 | |
| 620 | StringRef FileManager::getCanonicalName(const FileEntry *File) { |
| 621 | llvm::DenseMap<const void *, llvm::StringRef>::iterator Known |
| 622 | = CanonicalNames.find(File); |
| 623 | if (Known != CanonicalNames.end()) |
| 624 | return Known->second; |
| 625 | |
| 626 | StringRef CanonicalName(File->getName()); |
| 627 | |
| 628 | SmallString<4096> CanonicalNameBuf; |
| 629 | if (!FS->getRealPath(File->getName(), CanonicalNameBuf)) |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 630 | CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 631 | |
| 632 | CanonicalNames.insert({File, CanonicalName}); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 633 | return CanonicalName; |
| 634 | } |
| 635 | |
| 636 | void FileManager::PrintStats() const { |
| 637 | llvm::errs() << "\n*** File Manager Stats:\n"; |
| 638 | llvm::errs() << UniqueRealFiles.size() << " real files found, " |
| 639 | << UniqueRealDirs.size() << " real dirs found.\n"; |
| 640 | llvm::errs() << VirtualFileEntries.size() << " virtual files found, " |
| 641 | << VirtualDirectoryEntries.size() << " virtual dirs found.\n"; |
| 642 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 643 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 644 | llvm::errs() << NumFileLookups << " file lookups, " |
| 645 | << NumFileCacheMisses << " file cache misses.\n"; |
| 646 | |
| 647 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
| 648 | } |