diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h index b3a869af610786..af02fa2e7e876e 100644 --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h @@ -207,6 +207,11 @@ class DependencyScanningWorkerFilesystem : public llvm::vfs::ProxyFileSystem { llvm::ErrorOr getOrCreateFileSystemEntry(const StringRef Filename); + /// Create a cached file system entry based on the initial status result. + CachedFileSystemEntry + createFileSystemEntry(llvm::ErrorOr &&MaybeStatus, + StringRef Filename, bool ShouldMinimize); + /// The global cache shared between worker threads. DependencyScanningFilesystemSharedCache &SharedCache; /// The local cache is used by the worker thread to cache file system queries diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp index 8973f2658ed69c..f7c711690d7eac 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp @@ -167,6 +167,19 @@ bool DependencyScanningWorkerFilesystem::shouldMinimize(StringRef RawFilename) { return !NotToBeMinimized.contains(Filename); } +CachedFileSystemEntry DependencyScanningWorkerFilesystem::createFileSystemEntry( + llvm::ErrorOr &&MaybeStatus, StringRef Filename, + bool ShouldMinimize) { + if (!MaybeStatus) + return CachedFileSystemEntry(MaybeStatus.getError()); + + if (MaybeStatus->isDirectory()) + return CachedFileSystemEntry::createDirectoryEntry(std::move(*MaybeStatus)); + + return CachedFileSystemEntry::createFileEntry(Filename, getUnderlyingFS(), + ShouldMinimize); +} + llvm::ErrorOr DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry( const StringRef Filename) { @@ -186,22 +199,15 @@ DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry( CachedFileSystemEntry &CacheEntry = SharedCacheEntry.Value; if (!CacheEntry.isValid()) { - llvm::vfs::FileSystem &FS = getUnderlyingFS(); - auto MaybeStatus = FS.status(Filename); - if (!MaybeStatus) { - if (!shouldCacheStatFailures(Filename)) - // HACK: We need to always restat non source files if the stat fails. - // This is because Clang first looks up the module cache and module - // files before building them, and then looks for them again. If we - // cache the stat failure, it won't see them the second time. - return MaybeStatus.getError(); - CacheEntry = CachedFileSystemEntry(MaybeStatus.getError()); - } else if (MaybeStatus->isDirectory()) - CacheEntry = CachedFileSystemEntry::createDirectoryEntry( - std::move(*MaybeStatus)); - else - CacheEntry = CachedFileSystemEntry::createFileEntry(Filename, FS, - ShouldMinimize); + auto MaybeStatus = getUnderlyingFS().status(Filename); + if (!MaybeStatus && !shouldCacheStatFailures(Filename)) + // HACK: We need to always restat non source files if the stat fails. + // This is because Clang first looks up the module cache and module + // files before building them, and then looks for them again. If we + // cache the stat failure, it won't see them the second time. + return MaybeStatus.getError(); + CacheEntry = createFileSystemEntry(std::move(MaybeStatus), Filename, + ShouldMinimize); } Result = &CacheEntry;