[clang] da920c3 - [clang][deps] NFC: Move entry initialization into member functions

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 15 07:39:46 PST 2021


Author: Jan Svoboda
Date: 2021-12-15T16:39:29+01:00
New Revision: da920c3bcc794ee86c48f0e77eab4db068f17b5d

URL: https://github.com/llvm/llvm-project/commit/da920c3bcc794ee86c48f0e77eab4db068f17b5d
DIFF: https://github.com/llvm/llvm-project/commit/da920c3bcc794ee86c48f0e77eab4db068f17b5d.diff

LOG: [clang][deps] NFC: Move entry initialization into member functions

This is a prep-patch for making `CachedFileSystemEntry` initialization more lazy.

Added: 
    

Modified: 
    clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
    clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Removed: 
    


################################################################################
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 @@ class CachedFileSystemEntry {
   /// 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<llvm::vfs::Status> &&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<llvm::vfs::Status>
+  initFile(StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize = true);
 
   /// \returns True if the entry is initialized.
   bool isInitialized() const {
@@ -203,11 +200,6 @@ class DependencyScanningWorkerFilesystem : public llvm::vfs::ProxyFileSystem {
   llvm::ErrorOr<const CachedFileSystemEntry *>
   getOrCreateFileSystemEntry(const StringRef Filename);
 
-  /// Create a cached file system entry based on the initial status result.
-  CachedFileSystemEntry
-  createFileSystemEntry(llvm::ErrorOr<llvm::vfs::Status> &&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<llvm::vfs::Status>
+CachedFileSystemEntry::initFile(StringRef Filename, llvm::vfs::FileSystem &FS,
+                                bool Minimize) {
   // Load the file and its content from the file system.
   llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> MaybeFile =
       FS.openFileForRead(Filename);
   if (!MaybeFile)
     return MaybeFile.getError();
+
   llvm::ErrorOr<llvm::vfs::Status> Stat = (*MaybeFile)->status();
   if (!Stat)
     return Stat.getError();
 
-  llvm::vfs::File &F = **MaybeFile;
   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 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<llvm::SmallVectorMemoryBuffer>(
+  Contents = std::make_unique<llvm::SmallVectorMemoryBuffer>(
       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<llvm::vfs::Status> &&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<llvm::vfs::Status> &&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<const CachedFileSystemEntry *>
@@ -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;


        


More information about the cfe-commits mailing list