[llvm] r296029 - [llvm-cov] Strip redundant path components from filenames (fix PR31982)

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 14:20:33 PST 2017


Author: vedantk
Date: Thu Feb 23 16:20:32 2017
New Revision: 296029

URL: http://llvm.org/viewvc/llvm-project?rev=296029&view=rev
Log:
[llvm-cov] Strip redundant path components from filenames (fix PR31982)

Instead of stripping the longest common prefix off of the filenames in a
report, strip out the longest chain of redundant path components. This
fixes the case in PR31982, where there are two files with the same
prefix, and stripping out the LCP makes things less intelligible.

Added:
    llvm/trunk/test/tools/llvm-cov/Inputs/multiple-files2.covmapping
Modified:
    llvm/trunk/test/tools/llvm-cov/multiple-files.test
    llvm/trunk/tools/llvm-cov/CoverageReport.cpp

Added: llvm/trunk/test/tools/llvm-cov/Inputs/multiple-files2.covmapping
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/Inputs/multiple-files2.covmapping?rev=296029&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-cov/Inputs/multiple-files2.covmapping (added) and llvm/trunk/test/tools/llvm-cov/Inputs/multiple-files2.covmapping Thu Feb 23 16:20:32 2017 differ

Modified: llvm/trunk/test/tools/llvm-cov/multiple-files.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/multiple-files.test?rev=296029&r1=296028&r2=296029&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/multiple-files.test (original)
+++ llvm/trunk/test/tools/llvm-cov/multiple-files.test Thu Feb 23 16:20:32 2017
@@ -1,9 +1,15 @@
 // RUN: llvm-profdata merge %S/Inputs/multiple-files.proftext -o %t.profdata
-// RUN: llvm-cov report %S/Inputs/multiple-files.covmapping -instr-profile %t.profdata | FileCheck %s
+// RUN: llvm-cov report %S/Inputs/multiple-files.covmapping -instr-profile %t.profdata | FileCheck %s -check-prefix=MANY_COMPONENTS
+// RUN: llvm-cov report %S/Inputs/multiple-files2.covmapping -instr-profile %t.profdata | FileCheck %s -check-prefix=ONE_COMPONENT
 
-// CHECK: Filename
-// CHECK-NEXT: ---
-// CHECK-NEXT: {{^}}a{{[/\\]}}f2.c
-// CHECK-NEXT: {{^}}b{{[/\\]}}c{{[/\\]}}f4.c
-// CHECK-NEXT: {{^}}b{{[/\\]}}f3.c
-// CHECK-NEXT: {{^}}f1.c
+// MANY_COMPONENTS: Filename
+// MANY_COMPONENTS-NEXT: ---
+// MANY_COMPONENTS-NEXT: {{^}}a{{[/\\]}}f2.c
+// MANY_COMPONENTS-NEXT: {{^}}b{{[/\\]}}c{{[/\\]}}f4.c
+// MANY_COMPONENTS-NEXT: {{^}}b{{[/\\]}}f3.c
+// MANY_COMPONENTS-NEXT: {{^}}f1.c
+
+// ONE_COMPONENT: Filename
+// ONE_COMPONENT-NEXT: ---
+// ONE_COMPONENT-NEXT: {{^}}cov.c
+// ONE_COMPONENT-NEXT: {{^}}cov.h

Modified: llvm/trunk/tools/llvm-cov/CoverageReport.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/CoverageReport.cpp?rev=296029&r1=296028&r2=296029&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/CoverageReport.cpp (original)
+++ llvm/trunk/tools/llvm-cov/CoverageReport.cpp Thu Feb 23 16:20:32 2017
@@ -118,19 +118,51 @@ raw_ostream::Colors determineCoveragePer
                                           : raw_ostream::RED;
 }
 
-/// \brief Determine the length of the longest common prefix of the strings in
-/// \p Strings.
-unsigned getLongestCommonPrefixLen(ArrayRef<std::string> Strings) {
-  unsigned LCP = Strings[0].size();
-  for (unsigned I = 1, E = Strings.size(); LCP > 0 && I < E; ++I) {
-    unsigned Cursor;
-    StringRef S = Strings[I];
-    for (Cursor = 0; Cursor < LCP && Cursor < S.size(); ++Cursor)
-      if (Strings[0][Cursor] != S[Cursor])
+/// \brief Get the number of redundant path components in each path in \p Paths.
+unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
+  // To start, set the number of redundant path components to the maximum
+  // possible value.
+  SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
+                                                sys::path::end(Paths[0])};
+  unsigned NumRedundant = FirstPathComponents.size();
+
+  for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; ++I) {
+    StringRef Path = Paths[I];
+    for (const auto &Component :
+         enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
+      // Do not increase the number of redundant components: that would remove
+      // useful parts of already-visited paths.
+      if (Component.Index >= NumRedundant)
         break;
-    LCP = std::min(LCP, Cursor);
+
+      // Lower the number of redundant components when there's a mismatch
+      // between the first path, and the path under consideration.
+      if (FirstPathComponents[Component.Index] != Component.Value) {
+        NumRedundant = Component.Index;
+        break;
+      }
+    }
+  }
+
+  return NumRedundant;
+}
+
+/// \brief Determine the length of the longest redundant prefix of the paths in
+/// \p Paths.
+unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
+  // If there's at most one path, no path components are redundant.
+  if (Paths.size() <= 1)
+    return 0;
+
+  unsigned PrefixLen = 0;
+  unsigned NumRedundant = getNumRedundantPathComponents(Paths);
+  auto Component = sys::path::begin(Paths[0]);
+  for (unsigned I = 0; I < NumRedundant; ++I) {
+    auto LastComponent = Component;
+    ++Component;
+    PrefixLen += Component - LastComponent;
   }
-  return LCP;
+  return PrefixLen;
 }
 
 } // end anonymous namespace
@@ -280,9 +312,7 @@ CoverageReport::prepareFileReports(const
                                    FileCoverageSummary &Totals,
                                    ArrayRef<std::string> Files) {
   std::vector<FileCoverageSummary> FileReports;
-  unsigned LCP = 0;
-  if (Files.size() > 1)
-    LCP = getLongestCommonPrefixLen(Files);
+  unsigned LCP = getRedundantPrefixLen(Files);
 
   for (StringRef Filename : Files) {
     FileCoverageSummary Summary(Filename.drop_front(LCP));




More information about the llvm-commits mailing list