diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h index 4b286df8cd8b..4e770bb2e789 100644 --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h @@ -36,20 +36,17 @@ public: /// Creates an uninitialized entry. CachedFileSystemEntry() : MaybeStat(llvm::vfs::Status()) {} - CachedFileSystemEntry(std::error_code Error) : MaybeStat(std::move(Error)) {} + /// Initialize the cached file system entry. + void init(llvm::ErrorOr &&MaybeStatus, StringRef Filename, + llvm::vfs::FileSystem &FS, bool ShouldMinimize = true); - /// Create an entry that represents an opened source file with minimized or - /// original contents. + /// Initialize the entry as file with minimized or original contents. /// /// The filesystem opens the file even for `stat` calls open to avoid the /// issues with stat + open of minimized files that might lead to a /// mismatching size of the file. - static CachedFileSystemEntry createFileEntry(StringRef Filename, - llvm::vfs::FileSystem &FS, - bool Minimize = true); - - /// Create an entry that represents a directory on the filesystem. - static CachedFileSystemEntry createDirectoryEntry(llvm::vfs::Status &&Stat); + llvm::ErrorOr + initFile(StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize = true); /// \returns True if the entry is initialized. bool isInitialized() const { @@ -203,11 +200,6 @@ private: 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 32e8acbcfc3d..08cf05de9e03 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp @@ -16,20 +16,21 @@ using namespace clang; using namespace tooling; using namespace dependencies; -CachedFileSystemEntry CachedFileSystemEntry::createFileEntry( - StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize) { +llvm::ErrorOr +CachedFileSystemEntry::initFile(StringRef Filename, llvm::vfs::FileSystem &FS, + bool Minimize) { // Load the file and its content from the file system. llvm::ErrorOr> MaybeFile = FS.openFileForRead(Filename); if (!MaybeFile) return MaybeFile.getError(); + llvm::ErrorOr Stat = (*MaybeFile)->status(); if (!Stat) return Stat.getError(); - llvm::vfs::File &F = **MaybeFile; llvm::ErrorOr> MaybeBuffer = - F.getBuffer(Stat->getName()); + (*MaybeFile)->getBuffer(Stat->getName()); if (!MaybeBuffer) return MaybeBuffer.getError(); @@ -42,22 +43,18 @@ CachedFileSystemEntry CachedFileSystemEntry::createFileEntry( // Use the original file unless requested otherwise, or // if the minimization failed. // FIXME: Propage the diagnostic if desired by the client. - CachedFileSystemEntry Result; - Result.MaybeStat = std::move(*Stat); - Result.Contents = std::move(*MaybeBuffer); - return Result; + Contents = std::move(*MaybeBuffer); + return Stat; } - CachedFileSystemEntry Result; - size_t Size = MinimizedFileContents.size(); - Result.MaybeStat = llvm::vfs::Status(Stat->getName(), Stat->getUniqueID(), - Stat->getLastModificationTime(), - Stat->getUser(), Stat->getGroup(), Size, - Stat->getType(), Stat->getPermissions()); + Stat = llvm::vfs::Status(Stat->getName(), Stat->getUniqueID(), + Stat->getLastModificationTime(), Stat->getUser(), + Stat->getGroup(), MinimizedFileContents.size(), + Stat->getType(), Stat->getPermissions()); // The contents produced by the minimizer must be null terminated. assert(MinimizedFileContents.data()[MinimizedFileContents.size()] == '\0' && "not null terminated contents"); - Result.Contents = std::make_unique( + Contents = std::make_unique( std::move(MinimizedFileContents)); // Compute the skipped PP ranges that speedup skipping over inactive @@ -76,17 +73,8 @@ CachedFileSystemEntry CachedFileSystemEntry::createFileEntry( } Mapping[Range.Offset] = Range.Length; } - Result.PPSkippedRangeMapping = std::move(Mapping); - - return Result; -} - -CachedFileSystemEntry -CachedFileSystemEntry::createDirectoryEntry(llvm::vfs::Status &&Stat) { - assert(Stat.isDirectory() && "not a directory!"); - auto Result = CachedFileSystemEntry(); - Result.MaybeStat = std::move(Stat); - return Result; + PPSkippedRangeMapping = std::move(Mapping); + return Stat; } DependencyScanningFilesystemSharedCache::SingleCache::SingleCache() { @@ -157,17 +145,13 @@ 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); +void CachedFileSystemEntry::init(llvm::ErrorOr &&MaybeStatus, + StringRef Filename, llvm::vfs::FileSystem &FS, + bool ShouldMinimize) { + if (!MaybeStatus || MaybeStatus->isDirectory()) + MaybeStat = std::move(MaybeStatus); + else + MaybeStat = initFile(Filename, FS, ShouldMinimize); } llvm::ErrorOr @@ -196,8 +180,8 @@ DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry( // 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); + CacheEntry.init(std::move(MaybeStatus), Filename, getUnderlyingFS(), + ShouldMinimize); } Result = &CacheEntry;