| 1 | //===- DependencyScanningFilesystem.cpp - Optimized Scanning FS -----------===// |
| 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 | #include "clang/DependencyScanning/DependencyScanningFilesystem.h" |
| 10 | #include "clang/DependencyScanning/DependencyScanningService.h" |
| 11 | #include "llvm/Support/MemoryBuffer.h" |
| 12 | #include "llvm/Support/Threading.h" |
| 13 | #include <optional> |
| 14 | |
| 15 | using namespace clang; |
| 16 | using namespace dependencies; |
| 17 | |
| 18 | llvm::ErrorOr<DependencyScanningWorkerFilesystem::TentativeEntry> |
| 19 | DependencyScanningWorkerFilesystem::readFile(StringRef Filename, |
| 20 | llvm::vfs::FileSystem &FS) { |
| 21 | // Load the file and its content from the file system. |
| 22 | auto MaybeFile = FS.openFileForRead(Path: Filename); |
| 23 | if (!MaybeFile) |
| 24 | return MaybeFile.getError(); |
| 25 | auto File = std::move(*MaybeFile); |
| 26 | |
| 27 | auto MaybeStat = File->status(); |
| 28 | if (!MaybeStat) |
| 29 | return MaybeStat.getError(); |
| 30 | auto Stat = std::move(*MaybeStat); |
| 31 | |
| 32 | auto MaybeBuffer = File->getBuffer(Name: Stat.getName()); |
| 33 | if (!MaybeBuffer) |
| 34 | return MaybeBuffer.getError(); |
| 35 | auto Buffer = std::move(*MaybeBuffer); |
| 36 | |
| 37 | // If the file size changed between read and stat, pretend it didn't. |
| 38 | if (Stat.getSize() != Buffer->getBufferSize()) |
| 39 | Stat = llvm::vfs::Status::copyWithNewSize(In: Stat, NewSize: Buffer->getBufferSize()); |
| 40 | |
| 41 | return TentativeEntry(Stat, std::move(Buffer)); |
| 42 | } |
| 43 | |
| 44 | bool DependencyScanningWorkerFilesystem::ensureDirectiveTokensArePopulated( |
| 45 | EntryRef Ref) { |
| 46 | auto &Entry = Ref.Entry; |
| 47 | |
| 48 | if (Entry.isError() || Entry.isDirectory()) |
| 49 | return false; |
| 50 | |
| 51 | CachedFileContents *Contents = Entry.getCachedContents(); |
| 52 | assert(Contents && "contents not initialized" ); |
| 53 | |
| 54 | // Double-checked locking. |
| 55 | if (Contents->DepDirectives.load()) |
| 56 | return true; |
| 57 | |
| 58 | std::lock_guard<std::mutex> GuardLock(Contents->ValueLock); |
| 59 | |
| 60 | // Double-checked locking. |
| 61 | if (Contents->DepDirectives.load()) |
| 62 | return true; |
| 63 | |
| 64 | SmallVector<dependency_directives_scan::Directive, 64> Directives; |
| 65 | // Scan the file for preprocessor directives that might affect the |
| 66 | // dependencies. |
| 67 | if (scanSourceForDependencyDirectives(Input: Contents->Original->getBuffer(), |
| 68 | Tokens&: Contents->DepDirectiveTokens, |
| 69 | Directives)) { |
| 70 | Contents->DepDirectiveTokens.clear(); |
| 71 | // FIXME: Propagate the diagnostic if desired by the client. |
| 72 | Contents->DepDirectives.store(p: new std::optional<DependencyDirectivesTy>()); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // This function performed double-checked locking using `DepDirectives`. |
| 77 | // Assigning it must be the last thing this function does, otherwise other |
| 78 | // threads may skip the critical section (`DepDirectives != nullptr`), leading |
| 79 | // to a data race. |
| 80 | Contents->DepDirectives.store( |
| 81 | p: new std::optional<DependencyDirectivesTy>(std::move(Directives))); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | DependencyScanningFilesystemSharedCache:: |
| 86 | DependencyScanningFilesystemSharedCache() { |
| 87 | // This heuristic was chosen using a empirical testing on a |
| 88 | // reasonably high core machine (iMacPro 18 cores / 36 threads). The cache |
| 89 | // sharding gives a performance edge by reducing the lock contention. |
| 90 | // FIXME: A better heuristic might also consider the OS to account for |
| 91 | // the different cost of lock contention on different OSes. |
| 92 | NumShards = |
| 93 | std::max(a: 2u, b: llvm::hardware_concurrency().compute_thread_count() / 4); |
| 94 | CacheShards = std::make_unique<CacheShard[]>(num: NumShards); |
| 95 | } |
| 96 | |
| 97 | DependencyScanningFilesystemSharedCache::CacheShard & |
| 98 | DependencyScanningFilesystemSharedCache::getShardForFilename( |
| 99 | StringRef Filename) const { |
| 100 | assert(llvm::sys::path::is_absolute_gnu(Filename)); |
| 101 | return CacheShards[llvm::hash_value(S: Filename) % NumShards]; |
| 102 | } |
| 103 | |
| 104 | DependencyScanningFilesystemSharedCache::CacheShard & |
| 105 | DependencyScanningFilesystemSharedCache::getShardForUID( |
| 106 | llvm::sys::fs::UniqueID UID) const { |
| 107 | auto Hash = llvm::hash_combine(args: UID.getDevice(), args: UID.getFile()); |
| 108 | return CacheShards[Hash % NumShards]; |
| 109 | } |
| 110 | |
| 111 | std::vector<DependencyScanningFilesystemSharedCache::OutOfDateEntry> |
| 112 | DependencyScanningFilesystemSharedCache::getOutOfDateEntries( |
| 113 | llvm::vfs::FileSystem &UnderlyingFS) const { |
| 114 | // Iterate through all shards and look for cached stat errors. |
| 115 | std::vector<OutOfDateEntry> InvalidDiagInfo; |
| 116 | for (unsigned i = 0; i < NumShards; i++) { |
| 117 | const CacheShard &Shard = CacheShards[i]; |
| 118 | std::lock_guard<std::mutex> LockGuard(Shard.CacheLock); |
| 119 | for (const auto &[Path, State] : Shard.CacheByFilename) { |
| 120 | const CachedFileSystemEntry *Entry = State.Entry; |
| 121 | // Skip slots without a resolved entry: real-path-only entries from |
| 122 | // getRealPath, or uncached negative stats. Runs post-scan, so no |
| 123 | // in-progress slots remain. |
| 124 | if (!Entry) |
| 125 | continue; |
| 126 | llvm::ErrorOr<llvm::vfs::Status> Status = UnderlyingFS.status(Path); |
| 127 | if (Status) { |
| 128 | if (Entry->getError()) { |
| 129 | // This is the case where we have cached the non-existence |
| 130 | // of the file at Path first, and a file at the path is created |
| 131 | // later. The cache entry is not invalidated (as we have no good |
| 132 | // way to do it now), which may lead to missing file build errors. |
| 133 | InvalidDiagInfo.emplace_back(args: Path.data()); |
| 134 | } else { |
| 135 | llvm::vfs::Status CachedStatus = Entry->getStatus(); |
| 136 | if (Status->getType() == llvm::sys::fs::file_type::regular_file && |
| 137 | Status->getType() == CachedStatus.getType()) { |
| 138 | // We only check regular files. Directory files sizes could change |
| 139 | // due to content changes, and reporting directory size changes can |
| 140 | // lead to false positives. |
| 141 | // TODO: At the moment, we do not detect symlinks to files whose |
| 142 | // size may change. We need to decide if we want to detect cached |
| 143 | // symlink size changes. We can also expand this to detect file |
| 144 | // type changes. |
| 145 | uint64_t CachedSize = CachedStatus.getSize(); |
| 146 | uint64_t ActualSize = Status->getSize(); |
| 147 | if (CachedSize != ActualSize) { |
| 148 | // This is the case where the cached file has a different size |
| 149 | // from the actual file that comes from the underlying FS. |
| 150 | InvalidDiagInfo.emplace_back(args: Path.data(), args&: CachedSize, args&: ActualSize); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | return InvalidDiagInfo; |
| 158 | } |
| 159 | |
| 160 | namespace { |
| 161 | using InProgressEntry = |
| 162 | DependencyScanningFilesystemSharedCache::InProgressEntry; |
| 163 | using SlotResolved = llvm::ErrorOr<const CachedFileSystemEntry *>; |
| 164 | using SlotProducer = std::shared_ptr<InProgressEntry>; |
| 165 | using SlotAcquisitionResult = std::variant<SlotResolved, SlotProducer>; |
| 166 | |
| 167 | /// Returns a resolved entry if one is already present or in-flight under |
| 168 | /// \p K; otherwise installs a fresh \c InProgressEntry and returns it as a |
| 169 | /// producer slot. |
| 170 | template <typename Map, typename Key> |
| 171 | SlotAcquisitionResult acquireSlot(std::mutex &CacheLock, Map &M, const Key &K) { |
| 172 | std::shared_ptr<InProgressEntry> Pending; |
| 173 | { |
| 174 | std::lock_guard<std::mutex> ShardLock(CacheLock); |
| 175 | auto &State = M[K]; |
| 176 | |
| 177 | // Cache hit. |
| 178 | if (State.Entry) |
| 179 | return SlotResolved{State.Entry}; |
| 180 | |
| 181 | if (!State.InProgress) { |
| 182 | State.InProgress = std::make_shared<InProgressEntry>(); |
| 183 | return SlotProducer{State.InProgress}; |
| 184 | } |
| 185 | |
| 186 | // Copy the shared_ptr so the slot survives our wait once the shard lock |
| 187 | // is released and the producer resets State.InProgress on publish. |
| 188 | Pending = State.InProgress; |
| 189 | } |
| 190 | |
| 191 | // Wait off the shard lock so unrelated keys in this shard aren't blocked. |
| 192 | std::unique_lock<std::mutex> EntryLock(Pending->Mutex); |
| 193 | Pending->CondVar.wait(EntryLock, [&] { return Pending->Done; }); |
| 194 | return SlotResolved{Pending->Result}; |
| 195 | } |
| 196 | } // namespace |
| 197 | |
| 198 | const CachedRealPath * |
| 199 | DependencyScanningFilesystemSharedCache::CacheShard::findRealPathByFilename( |
| 200 | StringRef Filename) const { |
| 201 | assert(llvm::sys::path::is_absolute_gnu(Filename)); |
| 202 | std::lock_guard<std::mutex> LockGuard(CacheLock); |
| 203 | auto It = CacheByFilename.find(Key: Filename); |
| 204 | return It == CacheByFilename.end() ? nullptr : It->getValue().RealPath; |
| 205 | } |
| 206 | |
| 207 | const CachedRealPath &DependencyScanningFilesystemSharedCache::CacheShard:: |
| 208 | getOrEmplaceRealPathForFilename(StringRef Filename, |
| 209 | llvm::ErrorOr<llvm::StringRef> RealPath) { |
| 210 | std::lock_guard<std::mutex> LockGuard(CacheLock); |
| 211 | |
| 212 | const CachedRealPath *&StoredRealPath = CacheByFilename[Filename].RealPath; |
| 213 | if (!StoredRealPath) { |
| 214 | auto OwnedRealPath = [&]() -> CachedRealPath { |
| 215 | if (!RealPath) |
| 216 | return RealPath.getError(); |
| 217 | return RealPath->str(); |
| 218 | }(); |
| 219 | |
| 220 | StoredRealPath = new (RealPathStorage.Allocate()) |
| 221 | CachedRealPath(std::move(OwnedRealPath)); |
| 222 | } |
| 223 | |
| 224 | return *StoredRealPath; |
| 225 | } |
| 226 | |
| 227 | static IntrusiveRefCntPtr<llvm::vfs::FileSystem> |
| 228 | combineGlobalAndLocalFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> GlobalFS, |
| 229 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> LocalFS) { |
| 230 | if (LocalFS) { |
| 231 | // LocalFS takes precedence over GlobalFS, like it does for the queries that |
| 232 | // don't go through this overlay. |
| 233 | auto OverlayVFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>( |
| 234 | A: std::move(GlobalFS)); |
| 235 | OverlayVFS->pushOverlay(FS: std::move(LocalFS)); |
| 236 | return OverlayVFS; |
| 237 | } |
| 238 | return GlobalFS; |
| 239 | } |
| 240 | |
| 241 | DependencyScanningWorkerFilesystem::DependencyScanningWorkerFilesystem( |
| 242 | DependencyScanningService &Service, |
| 243 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, |
| 244 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> LocalFS) |
| 245 | : llvm::RTTIExtends<DependencyScanningWorkerFilesystem, |
| 246 | llvm::vfs::ProxyFileSystem>( |
| 247 | combineGlobalAndLocalFS(GlobalFS: FS, LocalFS)), |
| 248 | Service(Service), WorkingDirForCacheLookup(llvm::errc::invalid_argument), |
| 249 | LocalFS(std::move(LocalFS)), BaseFS(FS) { |
| 250 | updateWorkingDirForCacheLookup(); |
| 251 | } |
| 252 | |
| 253 | const CachedFileSystemEntry *DependencyScanningWorkerFilesystem::createEntry( |
| 254 | llvm::ErrorOr<TentativeEntry> &TEntry, |
| 255 | llvm::SpecificBumpPtrAllocator<CachedFileContents> &ContentsStorage, |
| 256 | llvm::SpecificBumpPtrAllocator<CachedFileSystemEntry> &EntryStorage) { |
| 257 | if (TEntry) { |
| 258 | CachedFileContents *StoredContents = nullptr; |
| 259 | if (TEntry->Contents) |
| 260 | StoredContents = new (ContentsStorage.Allocate()) |
| 261 | CachedFileContents(std::move(TEntry->Contents)); |
| 262 | return new (EntryStorage.Allocate()) |
| 263 | CachedFileSystemEntry(std::move(TEntry->Status), StoredContents); |
| 264 | } else { |
| 265 | return new (EntryStorage.Allocate()) |
| 266 | CachedFileSystemEntry(TEntry.getError()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | const CachedFileSystemEntry * |
| 271 | DependencyScanningWorkerFilesystem::resolveUIDThroughSharedCache( |
| 272 | StringRef OriginalFilename, const llvm::vfs::Status &Stat) { |
| 273 | auto &UIDShard = Service.getSharedCache().getShardForUID(UID: Stat.getUniqueID()); |
| 274 | auto UIDSlot = acquireSlot(CacheLock&: UIDShard.CacheLock, M&: UIDShard.EntriesByUID, |
| 275 | K: Stat.getUniqueID()); |
| 276 | if (auto *Resolved = std::get_if<SlotResolved>(ptr: &UIDSlot)) { |
| 277 | assert(*Resolved && **Resolved && |
| 278 | "in-progress UID slot fulfilled without an entry" ); |
| 279 | return **Resolved; |
| 280 | } |
| 281 | auto UIDProducer = std::move(std::get<SlotProducer>(v&: UIDSlot)); |
| 282 | |
| 283 | auto TEntry = Stat.isDirectory() ? TentativeEntry(Stat) |
| 284 | : readFile(Filename: OriginalFilename, FS&: *BaseFS); |
| 285 | |
| 286 | // Allocate the entry and bind the UID slot under one shard-lock acquisition |
| 287 | // (BumpPtrAllocator isn't thread-safe). On read-failure, the entry wraps |
| 288 | // the open error so concurrent UID waiters surface it rather than racing |
| 289 | // to retry the open. |
| 290 | const CachedFileSystemEntry *SharedEntry; |
| 291 | { |
| 292 | std::lock_guard<std::mutex> ShardLock(UIDShard.CacheLock); |
| 293 | auto &State = UIDShard.EntriesByUID[Stat.getUniqueID()]; |
| 294 | assert(!State.Entry && "UID slot already published an entry" ); |
| 295 | SharedEntry = |
| 296 | createEntry(TEntry, ContentsStorage&: UIDShard.ContentsStorage, EntryStorage&: UIDShard.EntryStorage); |
| 297 | State.Entry = SharedEntry; |
| 298 | State.InProgress.reset(); |
| 299 | } |
| 300 | UIDProducer->publish(R: SharedEntry); |
| 301 | return SharedEntry; |
| 302 | } |
| 303 | |
| 304 | const CachedFileSystemEntry * |
| 305 | DependencyScanningWorkerFilesystem::createCachedFSEntryForLocal( |
| 306 | StringRef OriginalFilename, const llvm::vfs::Status &Stat) { |
| 307 | assert(LocalFS); |
| 308 | auto TEntry = Stat.isDirectory() ? TentativeEntry(Stat) |
| 309 | : readFile(Filename: OriginalFilename, FS&: *LocalFS); |
| 310 | return createEntry(TEntry, ContentsStorage&: LocalContentsStorage, EntryStorage&: LocalEntryStorage); |
| 311 | } |
| 312 | |
| 313 | llvm::ErrorOr<const CachedFileSystemEntry *> |
| 314 | DependencyScanningWorkerFilesystem::resolveFilenameThroughSharedCache( |
| 315 | StringRef OriginalFilename, StringRef FilenameForLookup) { |
| 316 | if (LocalFS) { |
| 317 | if (auto Stat = LocalFS->status(Path: OriginalFilename)) { |
| 318 | return createCachedFSEntryForLocal(OriginalFilename, Stat: *Stat); |
| 319 | } |
| 320 | } |
| 321 | assert(llvm::sys::path::is_absolute_gnu(FilenameForLookup)); |
| 322 | auto &FilenameShard = |
| 323 | Service.getSharedCache().getShardForFilename(Filename: FilenameForLookup); |
| 324 | auto FilenameSlot = |
| 325 | acquireSlot(CacheLock&: FilenameShard.CacheLock, M&: FilenameShard.CacheByFilename, |
| 326 | K: FilenameForLookup); |
| 327 | if (auto *Resolved = std::get_if<SlotResolved>(ptr: &FilenameSlot)) |
| 328 | return *Resolved; |
| 329 | auto FilenameProducer = std::move(std::get<SlotProducer>(v&: FilenameSlot)); |
| 330 | |
| 331 | // Compute the outcome. Three cases: |
| 332 | // - Stat succeeded: delegate to the UID resolver. |
| 333 | // - Stat failed, cacheable: defer error-entry allocation to the critical |
| 334 | // section below so allocate+bind+reset share one shard-lock acquisition. |
| 335 | // - Stat failed, not cacheable: publish the error to current waiters but |
| 336 | // don't persist; a later separate query re-runs the stat (so a file |
| 337 | // created mid-scan becomes visible). |
| 338 | auto Stat = BaseFS->status(Path: OriginalFilename); |
| 339 | const bool ShouldCacheNegativeStat = |
| 340 | !Stat && Service.getOpts().CacheNegativeStats && |
| 341 | shouldCacheNegativeStatsForPath(Path: OriginalFilename); |
| 342 | llvm::ErrorOr<const CachedFileSystemEntry *> Result = std::error_code{}; |
| 343 | if (Stat) |
| 344 | Result = resolveUIDThroughSharedCache(OriginalFilename, Stat: *Stat); |
| 345 | else if (!ShouldCacheNegativeStat) |
| 346 | Result = Stat.getError(); |
| 347 | |
| 348 | // Bind the result and reset the in-flight slot under a single critical |
| 349 | // section. The cached-negative case allocates here so allocate+bind+reset |
| 350 | // share one shard-lock acquisition. |
| 351 | { |
| 352 | std::lock_guard<std::mutex> ShardLock(FilenameShard.CacheLock); |
| 353 | auto &State = FilenameShard.CacheByFilename[FilenameForLookup]; |
| 354 | assert(!State.Entry && "filename slot already published an entry" ); |
| 355 | if (ShouldCacheNegativeStat) { |
| 356 | auto *Entry = new (FilenameShard.EntryStorage.Allocate()) |
| 357 | CachedFileSystemEntry(Stat.getError()); |
| 358 | State.Entry = Entry; |
| 359 | Result = Entry; |
| 360 | } else if (Result) { |
| 361 | State.Entry = *Result; |
| 362 | } |
| 363 | State.InProgress.reset(); |
| 364 | } |
| 365 | FilenameProducer->publish(R: Result); |
| 366 | return Result; |
| 367 | } |
| 368 | |
| 369 | llvm::ErrorOr<EntryRef> |
| 370 | DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry( |
| 371 | StringRef OriginalFilename) { |
| 372 | SmallString<256> PathBuf; |
| 373 | auto FilenameForLookup = tryGetFilenameForLookup(OriginalFilename, PathBuf); |
| 374 | if (!FilenameForLookup) |
| 375 | return FilenameForLookup.getError(); |
| 376 | |
| 377 | auto &Local = LocalCache[*FilenameForLookup]; |
| 378 | if (Local.File) |
| 379 | return EntryRef(OriginalFilename, *Local.File).unwrapError(); |
| 380 | |
| 381 | auto MaybeEntry = |
| 382 | resolveFilenameThroughSharedCache(OriginalFilename, FilenameForLookup: *FilenameForLookup); |
| 383 | if (!MaybeEntry) |
| 384 | return MaybeEntry.getError(); |
| 385 | Local.File = *MaybeEntry; |
| 386 | return EntryRef(OriginalFilename, **MaybeEntry).unwrapError(); |
| 387 | } |
| 388 | |
| 389 | llvm::ErrorOr<llvm::vfs::Status> |
| 390 | DependencyScanningWorkerFilesystem::status(const Twine &Path) { |
| 391 | SmallString<256> OwnedFilename; |
| 392 | StringRef Filename = Path.toStringRef(Out&: OwnedFilename); |
| 393 | |
| 394 | llvm::ErrorOr<EntryRef> Result = getOrCreateFileSystemEntry(OriginalFilename: Filename); |
| 395 | if (!Result) |
| 396 | return Result.getError(); |
| 397 | return Result->getStatus(); |
| 398 | } |
| 399 | |
| 400 | bool DependencyScanningWorkerFilesystem::exists(const Twine &Path) { |
| 401 | // While some VFS overlay filesystems may implement more-efficient |
| 402 | // mechanisms for `exists` queries, `DependencyScanningWorkerFilesystem` |
| 403 | // typically wraps `RealFileSystem` which does not specialize `exists`, |
| 404 | // so it is not likely to benefit from such optimizations. Instead, |
| 405 | // it is more-valuable to have this query go through the |
| 406 | // cached-`status` code-path of the `DependencyScanningWorkerFilesystem`. |
| 407 | llvm::ErrorOr<llvm::vfs::Status> Status = status(Path); |
| 408 | return Status && Status->exists(); |
| 409 | } |
| 410 | |
| 411 | namespace { |
| 412 | |
| 413 | /// The VFS that is used by clang consumes the \c CachedFileSystemEntry using |
| 414 | /// this subclass. |
| 415 | class DepScanFile final : public llvm::vfs::File { |
| 416 | public: |
| 417 | DepScanFile(std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 418 | llvm::vfs::Status Stat) |
| 419 | : Buffer(std::move(Buffer)), Stat(std::move(Stat)) {} |
| 420 | |
| 421 | static llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> create(EntryRef Entry); |
| 422 | |
| 423 | llvm::ErrorOr<llvm::vfs::Status> status() override { return Stat; } |
| 424 | |
| 425 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 426 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, |
| 427 | bool IsVolatile) override { |
| 428 | return llvm::MemoryBuffer::getMemBuffer(Ref: Buffer->getMemBufferRef(), |
| 429 | RequiresNullTerminator); |
| 430 | } |
| 431 | |
| 432 | std::error_code close() override { return {}; } |
| 433 | |
| 434 | private: |
| 435 | std::unique_ptr<llvm::MemoryBuffer> Buffer; |
| 436 | llvm::vfs::Status Stat; |
| 437 | }; |
| 438 | |
| 439 | } // end anonymous namespace |
| 440 | |
| 441 | llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> |
| 442 | DepScanFile::create(EntryRef Entry) { |
| 443 | assert(!Entry.isError() && "error" ); |
| 444 | |
| 445 | if (Entry.isDirectory()) |
| 446 | return std::make_error_code(e: std::errc::is_a_directory); |
| 447 | |
| 448 | auto Result = std::make_unique<DepScanFile>( |
| 449 | args: llvm::MemoryBuffer::getMemBuffer(InputData: Entry.getContents(), |
| 450 | BufferName: Entry.getStatus().getName(), |
| 451 | /*RequiresNullTerminator=*/false), |
| 452 | args: Entry.getStatus()); |
| 453 | |
| 454 | return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>( |
| 455 | std::unique_ptr<llvm::vfs::File>(std::move(Result))); |
| 456 | } |
| 457 | |
| 458 | llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> |
| 459 | DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path) { |
| 460 | SmallString<256> OwnedFilename; |
| 461 | StringRef Filename = Path.toStringRef(Out&: OwnedFilename); |
| 462 | |
| 463 | llvm::ErrorOr<EntryRef> Result = getOrCreateFileSystemEntry(OriginalFilename: Filename); |
| 464 | if (!Result) |
| 465 | return Result.getError(); |
| 466 | return DepScanFile::create(Entry: Result.get()); |
| 467 | } |
| 468 | |
| 469 | std::error_code |
| 470 | DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path, |
| 471 | SmallVectorImpl<char> &Output) { |
| 472 | SmallString<256> OwnedFilename; |
| 473 | StringRef OriginalFilename = Path.toStringRef(Out&: OwnedFilename); |
| 474 | |
| 475 | SmallString<256> PathBuf; |
| 476 | auto FilenameForLookup = tryGetFilenameForLookup(OriginalFilename, PathBuf); |
| 477 | if (!FilenameForLookup) |
| 478 | return FilenameForLookup.getError(); |
| 479 | |
| 480 | auto HandleCachedRealPath = |
| 481 | [&Output](const CachedRealPath &RealPath) -> std::error_code { |
| 482 | if (!RealPath) |
| 483 | return RealPath.getError(); |
| 484 | Output.assign(in_start: RealPath->begin(), in_end: RealPath->end()); |
| 485 | return {}; |
| 486 | }; |
| 487 | |
| 488 | // If we already have the result in local cache, no work required. |
| 489 | auto &Local = LocalCache[*FilenameForLookup]; |
| 490 | if (Local.RealPath) |
| 491 | return HandleCachedRealPath(*Local.RealPath); |
| 492 | |
| 493 | // Resolve through the local VFS without going through the shared cache. |
| 494 | if (LocalFS && LocalFS->exists(Path: OriginalFilename)) { |
| 495 | std::error_code EC = LocalFS->getRealPath(Path: OriginalFilename, Output); |
| 496 | CachedRealPath RealPath = EC; |
| 497 | if (!EC) |
| 498 | RealPath = std::string(Output.begin(), Output.end()); |
| 499 | Local.RealPath = new (LocalRealPathStorage.Allocate()) |
| 500 | CachedRealPath(std::move(RealPath)); |
| 501 | return HandleCachedRealPath(*Local.RealPath); |
| 502 | } |
| 503 | |
| 504 | // If we have the result in the shared cache, cache it locally. |
| 505 | auto &Shard = |
| 506 | Service.getSharedCache().getShardForFilename(Filename: *FilenameForLookup); |
| 507 | if (const auto *ShardRealPath = |
| 508 | Shard.findRealPathByFilename(Filename: *FilenameForLookup)) { |
| 509 | Local.RealPath = ShardRealPath; |
| 510 | return HandleCachedRealPath(*Local.RealPath); |
| 511 | } |
| 512 | |
| 513 | // If we don't know the real path, compute it... |
| 514 | std::error_code EC = BaseFS->getRealPath(Path: OriginalFilename, Output); |
| 515 | llvm::ErrorOr<llvm::StringRef> ComputedRealPath = EC; |
| 516 | if (!EC) |
| 517 | ComputedRealPath = StringRef{Output.data(), Output.size()}; |
| 518 | |
| 519 | // ...and try to write it into the shared cache. In case some other thread won |
| 520 | // this race and already wrote its own result there, just adopt it. Write |
| 521 | // whatever is in the shared cache into the local one. |
| 522 | const auto &RealPath = Shard.getOrEmplaceRealPathForFilename( |
| 523 | Filename: *FilenameForLookup, RealPath: ComputedRealPath); |
| 524 | Local.RealPath = &RealPath; |
| 525 | return HandleCachedRealPath(*Local.RealPath); |
| 526 | } |
| 527 | |
| 528 | std::error_code DependencyScanningWorkerFilesystem::setCurrentWorkingDirectory( |
| 529 | const Twine &Path) { |
| 530 | std::error_code EC = ProxyFileSystem::setCurrentWorkingDirectory(Path); |
| 531 | updateWorkingDirForCacheLookup(); |
| 532 | return EC; |
| 533 | } |
| 534 | |
| 535 | void DependencyScanningWorkerFilesystem::updateWorkingDirForCacheLookup() { |
| 536 | llvm::ErrorOr<std::string> CWD = |
| 537 | getUnderlyingFS().getCurrentWorkingDirectory(); |
| 538 | if (!CWD) { |
| 539 | WorkingDirForCacheLookup = CWD.getError(); |
| 540 | } else if (!llvm::sys::path::is_absolute_gnu(path: *CWD)) { |
| 541 | WorkingDirForCacheLookup = llvm::errc::invalid_argument; |
| 542 | } else { |
| 543 | WorkingDirForCacheLookup = *CWD; |
| 544 | } |
| 545 | assert(!WorkingDirForCacheLookup || |
| 546 | llvm::sys::path::is_absolute_gnu(*WorkingDirForCacheLookup)); |
| 547 | } |
| 548 | |
| 549 | llvm::ErrorOr<StringRef> |
| 550 | DependencyScanningWorkerFilesystem::tryGetFilenameForLookup( |
| 551 | StringRef OriginalFilename, llvm::SmallVectorImpl<char> &PathBuf) const { |
| 552 | StringRef FilenameForLookup; |
| 553 | if (llvm::sys::path::is_absolute_gnu(path: OriginalFilename)) { |
| 554 | FilenameForLookup = OriginalFilename; |
| 555 | } else if (!WorkingDirForCacheLookup) { |
| 556 | return WorkingDirForCacheLookup.getError(); |
| 557 | } else { |
| 558 | StringRef RelFilename = OriginalFilename; |
| 559 | RelFilename.consume_front(Prefix: "./" ); |
| 560 | PathBuf.assign(in_start: WorkingDirForCacheLookup->begin(), |
| 561 | in_end: WorkingDirForCacheLookup->end()); |
| 562 | llvm::sys::path::append(path&: PathBuf, a: RelFilename); |
| 563 | FilenameForLookup = StringRef{PathBuf.begin(), PathBuf.size()}; |
| 564 | } |
| 565 | assert(llvm::sys::path::is_absolute_gnu(FilenameForLookup)); |
| 566 | return FilenameForLookup; |
| 567 | } |
| 568 | |
| 569 | const char DependencyScanningWorkerFilesystem::ID = 0; |
| 570 | |