From c636d7a9a35348c500c978b7211ef8efcfa92d38 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 21 Nov 2016 14:00:04 +0000 Subject: [PATCH] [llvm-cov] Avoid 0% when reporting something that's 0/0 This commit makes llvm-cov avoid showing 0% (0/0) coverage for things like file function coverage, etc. in reports and HTML output. This can happen for files like headers that have macros but no functions. This commit makes llvm-cov report - (0/0) instead. rdar://29246480 Differential Revision: https://reviews.llvm.org/D26615 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287539 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Inputs/zeroFunctionFile.covmapping | Bin 0 -> 192 bytes test/tools/llvm-cov/Inputs/zeroFunctionFile.h | 3 ++ .../llvm-cov/Inputs/zeroFunctionFile.proftext | 16 +++++++ test/tools/llvm-cov/zeroFunctionFile.c | 19 ++++++++ tools/llvm-cov/CoverageReport.cpp | 44 +++++++++++------- tools/llvm-cov/SourceCoverageViewHTML.cpp | 12 +++-- 6 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping create mode 100644 test/tools/llvm-cov/Inputs/zeroFunctionFile.h create mode 100644 test/tools/llvm-cov/Inputs/zeroFunctionFile.proftext create mode 100644 test/tools/llvm-cov/zeroFunctionFile.c diff --git a/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping b/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping new file mode 100644 index 0000000000000000000000000000000000000000..b2c198a8fe0e5f5629a760737157f35ef702bd76 GIT binary patch literal 192 zcmd1FDa%dHFUu`SEiOq(EJ@^gaJaESfP*0|Kc6u-F*A>efq}sShz)?45s269mALJ5 z=tqnqkj)FkAn@yI?{l-1skb>HTrkNbu3wT{pkI|*lEWoBfSU{GgeP-0MKVo+mH2Ree0k&TB5>Hq)$ literal 0 HcmV?d00001 diff --git a/test/tools/llvm-cov/Inputs/zeroFunctionFile.h b/test/tools/llvm-cov/Inputs/zeroFunctionFile.h new file mode 100644 index 00000000000..1703e8e47d6 --- /dev/null +++ b/test/tools/llvm-cov/Inputs/zeroFunctionFile.h @@ -0,0 +1,3 @@ +// This header has no functions + +#define NOFUNCTIONS(x) (x) > 0 ? 0 : 1 diff --git a/test/tools/llvm-cov/Inputs/zeroFunctionFile.proftext b/test/tools/llvm-cov/Inputs/zeroFunctionFile.proftext new file mode 100644 index 00000000000..55187c1a486 --- /dev/null +++ b/test/tools/llvm-cov/Inputs/zeroFunctionFile.proftext @@ -0,0 +1,16 @@ +foo +# Func Hash: +13 +# Num Counters: +2 +# Counter Values: +1 +1 + +main +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +1 diff --git a/test/tools/llvm-cov/zeroFunctionFile.c b/test/tools/llvm-cov/zeroFunctionFile.c new file mode 100644 index 00000000000..87b6ecd3abb --- /dev/null +++ b/test/tools/llvm-cov/zeroFunctionFile.c @@ -0,0 +1,19 @@ +#include "Inputs/zeroFunctionFile.h" + +int foo(int x) { + return NOFUNCTIONS(x); +} +int main() { + return foo(2); +} + +// RUN: llvm-profdata merge %S/Inputs/zeroFunctionFile.proftext -o %t.profdata + +// RUN: llvm-cov report %S/Inputs/zeroFunctionFile.covmapping -instr-profile %t.profdata 2>&1 | FileCheck --check-prefix=REPORT --strict-whitespace %s +// REPORT: 0 0 - 0 0 - 0 0 - 0 0 - +// REPORT-NO: 0% + +// RUN: llvm-cov show %S/Inputs/zeroFunctionFile.covmapping -format html -instr-profile %t.profdata -o %t.dir +// RUN: FileCheck %s -input-file=%t.dir/index.html -check-prefix=HTML +// HTML:
- (0/0)
+// HTML-NO: 0.00% (0/0)
diff --git a/tools/llvm-cov/CoverageReport.cpp b/tools/llvm-cov/CoverageReport.cpp
index 5a56579149a..e88cb186acd 100644
--- a/tools/llvm-cov/CoverageReport.cpp
+++ b/tools/llvm-cov/CoverageReport.cpp
@@ -154,36 +154,48 @@ void CoverageReport::render(const FileCoverageSummary &File,
                (unsigned)File.RegionCoverage.NumRegions);
   Options.colored_ostream(OS, FileCoverageColor) << format(
       "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
-  Options.colored_ostream(OS, FileCoverageColor)
-      << format("%*.2f", FileReportColumns[3] - 1,
-                File.RegionCoverage.getPercentCovered())
-      << '%';
+  if (File.RegionCoverage.NumRegions)
+    Options.colored_ostream(OS, FileCoverageColor)
+        << format("%*.2f", FileReportColumns[3] - 1,
+                  File.RegionCoverage.getPercentCovered())
+        << '%';
+  else
+    OS << column("-", FileReportColumns[3], Column::RightAlignment);
   OS << format("%*u", FileReportColumns[4],
                (unsigned)File.FunctionCoverage.NumFunctions);
   OS << format("%*u", FileReportColumns[5],
                (unsigned)(File.FunctionCoverage.NumFunctions -
                           File.FunctionCoverage.Executed));
-  Options.colored_ostream(OS, FuncCoverageColor)
-      << format("%*.2f", FileReportColumns[6] - 1,
-                File.FunctionCoverage.getPercentCovered())
-      << '%';
+  if (File.FunctionCoverage.NumFunctions)
+    Options.colored_ostream(OS, FuncCoverageColor)
+        << format("%*.2f", FileReportColumns[6] - 1,
+                  File.FunctionCoverage.getPercentCovered())
+        << '%';
+  else
+    OS << column("-", FileReportColumns[6], Column::RightAlignment);
   OS << format("%*u", FileReportColumns[7],
                (unsigned)File.InstantiationCoverage.NumFunctions);
   OS << format("%*u", FileReportColumns[8],
                (unsigned)(File.InstantiationCoverage.NumFunctions -
                           File.InstantiationCoverage.Executed));
-  Options.colored_ostream(OS, InstantiationCoverageColor)
-      << format("%*.2f", FileReportColumns[9] - 1,
-                File.InstantiationCoverage.getPercentCovered())
-      << '%';
+  if (File.InstantiationCoverage.NumFunctions)
+    Options.colored_ostream(OS, InstantiationCoverageColor)
+        << format("%*.2f", FileReportColumns[9] - 1,
+                  File.InstantiationCoverage.getPercentCovered())
+        << '%';
+  else
+    OS << column("-", FileReportColumns[9], Column::RightAlignment);
   OS << format("%*u", FileReportColumns[10],
                (unsigned)File.LineCoverage.NumLines);
   Options.colored_ostream(OS, LineCoverageColor) << format(
       "%*u", FileReportColumns[11], (unsigned)File.LineCoverage.NotCovered);
-  Options.colored_ostream(OS, LineCoverageColor)
-      << format("%*.2f", FileReportColumns[12] - 1,
-                File.LineCoverage.getPercentCovered())
-      << '%';
+  if (File.LineCoverage.NumLines)
+    Options.colored_ostream(OS, LineCoverageColor)
+        << format("%*.2f", FileReportColumns[12] - 1,
+                  File.LineCoverage.getPercentCovered())
+        << '%';
+  else
+    OS << column("-", FileReportColumns[12], Column::RightAlignment);
   OS << "\n";
 }
 
diff --git a/tools/llvm-cov/SourceCoverageViewHTML.cpp b/tools/llvm-cov/SourceCoverageViewHTML.cpp
index 8fc01ef0d88..64b888e89d7 100644
--- a/tools/llvm-cov/SourceCoverageViewHTML.cpp
+++ b/tools/llvm-cov/SourceCoverageViewHTML.cpp
@@ -307,13 +307,17 @@ void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
     std::string S;
     {
       raw_string_ostream RSO{S};
-      RSO << format("%*.2f", 7, Pctg) << "% (" << Hit << '/' << Total << ')';
+      if (Total)
+        RSO << format("%*.2f", 7, Pctg) << "% ";
+      else
+        RSO << "- ";
+      RSO << '(' << Hit << '/' << Total << ')';
     }
     const char *CellClass = "column-entry-yellow";
-    if (Pctg < 80.0)
-      CellClass = "column-entry-red";
-    else if (Hit == Total)
+    if (Hit == Total)
       CellClass = "column-entry-green";
+    else if (Pctg < 80.0)
+      CellClass = "column-entry-red";
     Columns.emplace_back(tag("td", tag("pre", S), CellClass));
   };