[PATCH] D97061: [llvm-cov] Compare path only to find the same file

Choongwoo Han via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 2 11:31:55 PST 2021


tunz updated this revision to Diff 327533.
tunz added a comment.

Do not update test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D97061

Files:
  llvm/tools/llvm-cov/CodeCoverage.cpp


Index: llvm/tools/llvm-cov/CodeCoverage.cpp
===================================================================
--- llvm/tools/llvm-cov/CodeCoverage.cpp
+++ llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -42,6 +42,7 @@
 #include <functional>
 #include <map>
 #include <system_error>
+#include <unordered_map>
 
 using namespace llvm;
 using namespace coverage;
@@ -80,6 +81,13 @@
   /// directory, recursively collect all of the paths within the directory.
   void collectPaths(const std::string &Path);
 
+  /// Check if the two given two files are the same file.
+  bool isEquivalentFile(StringRef FilePath1, StringRef FilePath2);
+
+  /// Retrieve a file status with a cache.
+  Optional<sys::fs::file_status>
+  CodeCoverageTool::getFileStatus(StringRef FilePath);
+
   /// Return a memory buffer for the given source file.
   ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
 
@@ -153,6 +161,10 @@
   /// remapped to, when using -path-equivalence.
   Optional<std::pair<std::string, std::string>> PathRemapping;
 
+  // File status cache used when finding the same file.
+  std::unordered_map<std::string, Optional<sys::fs::file_status>>
+      FileStatusCache;
+
   /// The architecture the coverage mapping data targets.
   std::vector<StringRef> CoverageArches;
 
@@ -239,6 +251,29 @@
   }
 }
 
+Optional<sys::fs::file_status>
+CodeCoverageTool::getFileStatus(StringRef FilePath) {
+  auto It = FileStatusCache.find(FilePath.str());
+  if (It != FileStatusCache.end())
+    return It->second;
+
+  Optional<sys::fs::file_status> result;
+  sys::fs::file_status status;
+  if (!sys::fs::status(FilePath, status))
+    result = status;
+
+  FileStatusCache.emplace(std::string(FilePath), result);
+  return result;
+}
+
+bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1,
+                                        StringRef FilePath2) {
+  auto file_status1 = getFileStatus(FilePath1);
+  auto file_status2 = getFileStatus(FilePath1);
+  return file_status1.hasValue() && file_status2.hasValue() &&
+         sys::fs::equivalent(file_status1.getValue(), file_status2.getValue());
+}
+
 ErrorOr<const MemoryBuffer &>
 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
   // If we've remapped filenames, look up the real location for this file.
@@ -248,9 +283,11 @@
     if (Loc != RemappedFilenames.end())
       SourceFile = Loc->second;
   }
+
   for (const auto &Files : LoadedSourceFiles)
-    if (sys::fs::equivalent(SourceFile, Files.first))
+    if (isEquivalentFile(SourceFile, Files.first))
       return *Files.second;
+
   auto Buffer = MemoryBuffer::getFile(SourceFile);
   if (auto EC = Buffer.getError()) {
     error(EC.message(), SourceFile);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97061.327533.patch
Type: text/x-patch
Size: 2690 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210302/2f770182/attachment.bin>


More information about the llvm-commits mailing list