kousikk updated this revision to Diff 222669.
kousikk added a comment.

Add missed const


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68193/new/

https://reviews.llvm.org/D68193

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

Index: clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
===================================================================
--- clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
+++ clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
@@ -15,22 +15,19 @@
 using namespace tooling;
 using namespace dependencies;
 
+
 CachedFileSystemEntry CachedFileSystemEntry::createFileEntry(
-    StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize) {
+    llvm::vfs::Status &&Stat, llvm::vfs::FileSystem &FS, bool Minimize) {
   // Load the file and its content from the file system.
+  StringRef Filename = Stat.getName();
   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();
-  if (Stat->isDirectory())
-    return std::make_error_code(std::errc::is_a_directory);
 
   llvm::vfs::File &F = **MaybeFile;
   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MaybeBuffer =
-      F.getBuffer(Stat->getName());
+      F.getBuffer(Filename);
   if (!MaybeBuffer)
     return MaybeBuffer.getError();
 
@@ -44,7 +41,7 @@
     // if the minimization failed.
     // FIXME: Propage the diagnostic if desired by the client.
     CachedFileSystemEntry Result;
-    Result.MaybeStat = std::move(*Stat);
+    Result.MaybeStat = Stat;
     Result.Contents.reserve(Buffer->getBufferSize() + 1);
     Result.Contents.append(Buffer->getBufferStart(), Buffer->getBufferEnd());
     // Implicitly null terminate the contents for Clang's lexer.
@@ -55,10 +52,10 @@
 
   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());
+  Result.MaybeStat = llvm::vfs::Status(Stat.getName(), Stat.getUniqueID(),
+                                       Stat.getLastModificationTime(),
+                                       Stat.getUser(), Stat.getGroup(), 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");
@@ -124,14 +121,11 @@
   return It.first->getValue();
 }
 
-llvm::ErrorOr<llvm::vfs::Status>
-DependencyScanningWorkerFilesystem::status(const Twine &Path) {
-  SmallString<256> OwnedFilename;
-  StringRef Filename = Path.toStringRef(OwnedFilename);
-
-  // Check the local cache first.
-  if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename))
-    return Entry->getStatus();
+llvm::ErrorOr<const CachedFileSystemEntry *>
+DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry(const StringRef Filename) {
+  if (const CachedFileSystemEntry* Entry = getCachedEntry(Filename)) {
+    return Entry;
+  }
 
   // FIXME: Handle PCM/PCH files.
   // FIXME: Handle module map files.
@@ -154,7 +148,7 @@
             std::move(*MaybeStatus));
       else
         CacheEntry = CachedFileSystemEntry::createFileEntry(
-            Filename, FS, !KeepOriginalSource);
+            std::move(*MaybeStatus), FS, !KeepOriginalSource);
     }
 
     Result = &CacheEntry;
@@ -162,7 +156,17 @@
 
   // Store the result in the local cache.
   setCachedEntry(Filename, Result);
-  return Result->getStatus();
+  return Result;
+}
+
+llvm::ErrorOr<llvm::vfs::Status>
+DependencyScanningWorkerFilesystem::status(const Twine &Path) {
+  SmallString<256> OwnedFilename;
+  StringRef Filename = Path.toStringRef(OwnedFilename);
+  const llvm::ErrorOr<const CachedFileSystemEntry *> Result = getOrCreateFileSystemEntry(Filename);
+  if (!Result)
+    return Result.getError();
+  return (*Result)->getStatus();
 }
 
 namespace {
@@ -219,36 +223,8 @@
   SmallString<256> OwnedFilename;
   StringRef Filename = Path.toStringRef(OwnedFilename);
 
-  // Check the local cache first.
-  if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename))
-    return createFile(Entry, PPSkipMappings);
-
-  // FIXME: Handle PCM/PCH files.
-  // FIXME: Handle module map files.
-
-  bool KeepOriginalSource = IgnoredFiles.count(Filename);
-  DependencyScanningFilesystemSharedCache::SharedFileSystemEntry
-      &SharedCacheEntry = SharedCache.get(Filename);
-  const CachedFileSystemEntry *Result;
-  {
-    std::unique_lock<std::mutex> LockGuard(SharedCacheEntry.ValueLock);
-    CachedFileSystemEntry &CacheEntry = SharedCacheEntry.Value;
-
-    if (!CacheEntry.isValid()) {
-      CacheEntry = CachedFileSystemEntry::createFileEntry(
-        Filename, getUnderlyingFS(), !KeepOriginalSource);
-      if (CacheEntry.getError() == std::errc::is_a_directory) {
-        // Reset the CacheEntry to avoid setting an error entry in the
-        // cache for the given directory path.
-        CacheEntry = CachedFileSystemEntry();
-        return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(std::errc::is_a_directory);
-      }
-    }
-
-    Result = &CacheEntry;
-  }
-
-  // Store the result in the local cache.
-  setCachedEntry(Filename, Result);
-  return createFile(Result, PPSkipMappings);
+  const llvm::ErrorOr<const CachedFileSystemEntry *> Result = getOrCreateFileSystemEntry(Filename);
+  if (!Result)
+    return Result.getError();
+  return createFile(Result.get(), PPSkipMappings);
 }
Index: clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
===================================================================
--- clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
@@ -46,7 +46,7 @@
   /// mismatching size of the file. If file is not minimized, the full file is
   /// read and copied into memory to ensure that it's not memory mapped to avoid
   /// running out of file descriptors.
-  static CachedFileSystemEntry createFileEntry(StringRef Filename,
+  static CachedFileSystemEntry createFileEntry(llvm::vfs::Status &&Stat,
                                                llvm::vfs::FileSystem &FS,
                                                bool Minimize = true);
 
@@ -173,6 +173,9 @@
     return It == Cache.end() ? nullptr : It->getValue();
   }
 
+  llvm::ErrorOr<const CachedFileSystemEntry *>
+  getOrCreateFileSystemEntry(const StringRef Filename);
+
   DependencyScanningFilesystemSharedCache &SharedCache;
   /// The local cache is used by the worker thread to cache file system queries
   /// locally instead of querying the global cache every time.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to