[llvm-cov] Adjust column widths for function and file reports
[oota-llvm.git] / tools / llvm-cov / CoverageReport.cpp
index 94071b7..ed01a2e 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "CoverageReport.h"
-#include "CoverageSummary.h"
 #include "RenderingSupport.h"
-#include "llvm/Support/Format.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Format.h"
 
 using namespace llvm;
 namespace {
 /// \brief Helper struct which prints trimmed and aligned columns.
 struct Column {
-  enum TrimKind { NoTrim, LeftTrim, RightTrim };
+  enum TrimKind { NoTrim, WidthTrim, LeftTrim, RightTrim };
 
   enum AlignmentKind { LeftAlignment, RightAlignment };
 
@@ -31,7 +30,7 @@ struct Column {
   AlignmentKind Alignment;
 
   Column(StringRef Str, unsigned Width)
-      : Str(Str), Width(Width), Trim(NoTrim), Alignment(LeftAlignment) {}
+      : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
 
   Column &set(TrimKind Value) {
     Trim = Value;
@@ -45,6 +44,7 @@ struct Column {
 
   void render(raw_ostream &OS) const;
 };
+
 raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
   Value.render(OS);
   return OS;
@@ -65,6 +65,9 @@ void Column::render(raw_ostream &OS) const {
 
   switch (Trim) {
   case NoTrim:
+    OS << Str;
+    break;
+  case WidthTrim:
     OS << Str.substr(0, Width);
     break;
   case LeftTrim:
@@ -85,8 +88,19 @@ static Column column(StringRef Str, unsigned Width, const T &Value) {
   return Column(Str, Width).set(Value);
 }
 
-static const unsigned FileReportColumns[] = {25, 10, 8, 8, 10, 10};
-static const unsigned FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
+static size_t FileReportColumns[] = {25, 10, 8, 8, 10, 10};
+static size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
+
+/// \brief  Adjust column widths to fit long file paths and function names.
+static void adjustColumnWidths(coverage::CoverageMapping *CM) {
+  for (StringRef Filename : CM->getUniqueSourceFiles()) {
+    FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
+    for (const auto &F : CM->getCoveredFunctions(Filename)) {
+      FunctionReportColumns[0] =
+          std::max(FunctionReportColumns[0], F.Name.size());
+    }
+  }
+}
 
 /// \brief Prints a horizontal divider which spans across the given columns.
 template <typename T, size_t N>
@@ -109,18 +123,19 @@ static raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
 }
 
 void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
-  OS << column(File.Name, FileReportColumns[0], Column::LeftTrim)
-     << format("%*zd", FileReportColumns[1], File.RegionCoverage.NumRegions);
+  OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
+     << format("%*u", FileReportColumns[1],
+               (unsigned)File.RegionCoverage.NumRegions);
   Options.colored_ostream(OS, File.RegionCoverage.isFullyCovered()
                                   ? raw_ostream::GREEN
                                   : raw_ostream::RED)
-      << format("%*zd", FileReportColumns[2], File.RegionCoverage.NotCovered);
+      << format("%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
   Options.colored_ostream(OS,
                           determineCoveragePercentageColor(File.RegionCoverage))
       << format("%*.2f", FileReportColumns[3] - 1,
                 File.RegionCoverage.getPercentCovered()) << '%';
-  OS << format("%*zd", FileReportColumns[4],
-               File.FunctionCoverage.NumFunctions);
+  OS << format("%*u", FileReportColumns[4],
+               (unsigned)File.FunctionCoverage.NumFunctions);
   Options.colored_ostream(
       OS, determineCoveragePercentageColor(File.FunctionCoverage))
       << format("%*.2f", FileReportColumns[5] - 1,
@@ -131,24 +146,24 @@ void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
 void CoverageReport::render(const FunctionCoverageSummary &Function,
                             raw_ostream &OS) {
   OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
-     << format("%*zd", FunctionReportColumns[1],
-               Function.RegionCoverage.NumRegions);
+     << format("%*u", FunctionReportColumns[1],
+               (unsigned)Function.RegionCoverage.NumRegions);
   Options.colored_ostream(OS, Function.RegionCoverage.isFullyCovered()
                                   ? raw_ostream::GREEN
                                   : raw_ostream::RED)
-      << format("%*zd", FunctionReportColumns[2],
-                Function.RegionCoverage.NotCovered);
+      << format("%*u", FunctionReportColumns[2],
+                (unsigned)Function.RegionCoverage.NotCovered);
   Options.colored_ostream(
       OS, determineCoveragePercentageColor(Function.RegionCoverage))
       << format("%*.2f", FunctionReportColumns[3] - 1,
                 Function.RegionCoverage.getPercentCovered()) << '%';
-  OS << format("%*zd", FunctionReportColumns[4],
-               Function.LineCoverage.NumLines);
+  OS << format("%*u", FunctionReportColumns[4],
+               (unsigned)Function.LineCoverage.NumLines);
   Options.colored_ostream(OS, Function.LineCoverage.isFullyCovered()
                                   ? raw_ostream::GREEN
                                   : raw_ostream::RED)
-      << format("%*zd", FunctionReportColumns[5],
-                Function.LineCoverage.NotCovered);
+      << format("%*u", FunctionReportColumns[5],
+                (unsigned)Function.LineCoverage.NotCovered);
   Options.colored_ostream(
       OS, determineCoveragePercentageColor(Function.LineCoverage))
       << format("%*.2f", FunctionReportColumns[6] - 1,
@@ -156,14 +171,16 @@ void CoverageReport::render(const FunctionCoverageSummary &Function,
   OS << "\n";
 }
 
-void CoverageReport::renderFunctionReports(raw_ostream &OS) {
+void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
+                                           raw_ostream &OS) {
+  adjustColumnWidths(Coverage.get());
   bool isFirst = true;
-  for (const auto &File : Summary.getFileSummaries()) {
+  for (StringRef Filename : Files) {
     if (isFirst)
       isFirst = false;
     else
       OS << "\n";
-    OS << "File '" << File.Name << "':\n";
+    OS << "File '" << Filename << "':\n";
     OS << column("Name", FunctionReportColumns[0])
        << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
        << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
@@ -174,17 +191,24 @@ void CoverageReport::renderFunctionReports(raw_ostream &OS) {
     OS << "\n";
     renderDivider(FunctionReportColumns, OS);
     OS << "\n";
-    for (const auto &Function : File.FunctionSummaries)
+    FunctionCoverageSummary Totals("TOTAL");
+    for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
+      FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
+      ++Totals.ExecutionCount;
+      Totals.RegionCoverage += Function.RegionCoverage;
+      Totals.LineCoverage += Function.LineCoverage;
       render(Function, OS);
-    renderDivider(FunctionReportColumns, OS);
-    OS << "\n";
-    render(FunctionCoverageSummary("TOTAL", /*ExecutionCount=*/0,
-                                   File.RegionCoverage, File.LineCoverage),
-           OS);
+    }
+    if (Totals.ExecutionCount) {
+      renderDivider(FunctionReportColumns, OS);
+      OS << "\n";
+      render(Totals, OS);
+    }
   }
 }
 
 void CoverageReport::renderFileReports(raw_ostream &OS) {
+  adjustColumnWidths(Coverage.get());
   OS << column("Filename", FileReportColumns[0])
      << column("Regions", FileReportColumns[1], Column::RightAlignment)
      << column("Miss", FileReportColumns[2], Column::RightAlignment)
@@ -194,9 +218,18 @@ void CoverageReport::renderFileReports(raw_ostream &OS) {
      << "\n";
   renderDivider(FileReportColumns, OS);
   OS << "\n";
-  for (const auto &File : Summary.getFileSummaries())
-    render(File, OS);
+
+  FileCoverageSummary Totals("TOTAL");
+  for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
+    FileCoverageSummary Summary(Filename);
+    for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
+      FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
+      Summary.addFunction(Function);
+      Totals.addFunction(Function);
+    }
+    render(Summary, OS);
+  }
   renderDivider(FileReportColumns, OS);
   OS << "\n";
-  render(Summary.getCombinedFileSummaries(), OS);
+  render(Totals, OS);
 }