2014-08-23 00:56:03 +02:00
|
|
|
//===- CoverageReport.cpp - Code coverage report -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements rendering of a code coverage report.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CoverageReport.h"
|
|
|
|
#include "RenderingSupport.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
/// \brief Helper struct which prints trimmed and aligned columns.
|
|
|
|
struct Column {
|
2015-09-15 01:26:36 +02:00
|
|
|
enum TrimKind { NoTrim, WidthTrim, LeftTrim, RightTrim };
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
enum AlignmentKind { LeftAlignment, RightAlignment };
|
|
|
|
|
|
|
|
StringRef Str;
|
|
|
|
unsigned Width;
|
|
|
|
TrimKind Trim;
|
|
|
|
AlignmentKind Alignment;
|
|
|
|
|
|
|
|
Column(StringRef Str, unsigned Width)
|
2015-09-15 01:26:36 +02:00
|
|
|
: Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
Column &set(TrimKind Value) {
|
|
|
|
Trim = Value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Column &set(AlignmentKind Value) {
|
|
|
|
Alignment = Value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void render(raw_ostream &OS) const;
|
|
|
|
};
|
2015-09-15 01:26:36 +02:00
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
|
|
|
|
Value.render(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Column::render(raw_ostream &OS) const {
|
|
|
|
if (Str.size() <= Width) {
|
|
|
|
if (Alignment == RightAlignment) {
|
|
|
|
OS.indent(Width - Str.size());
|
|
|
|
OS << Str;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << Str;
|
|
|
|
OS.indent(Width - Str.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Trim) {
|
|
|
|
case NoTrim:
|
2015-09-15 01:26:36 +02:00
|
|
|
OS << Str;
|
|
|
|
break;
|
|
|
|
case WidthTrim:
|
2014-08-23 00:56:03 +02:00
|
|
|
OS << Str.substr(0, Width);
|
|
|
|
break;
|
|
|
|
case LeftTrim:
|
|
|
|
OS << "..." << Str.substr(Str.size() - Width + 3);
|
|
|
|
break;
|
|
|
|
case RightTrim:
|
|
|
|
OS << Str.substr(0, Width - 3) << "...";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Column column(StringRef Str, unsigned Width) {
|
|
|
|
return Column(Str, Width);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static Column column(StringRef Str, unsigned Width, const T &Value) {
|
|
|
|
return Column(Str, Width).set(Value);
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:26:36 +02:00
|
|
|
static size_t FileReportColumns[] = {25, 10, 8, 8, 10, 10};
|
|
|
|
static size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2015-10-21 18:03:32 +02:00
|
|
|
/// \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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
/// \brief Prints a horizontal divider which spans across the given columns.
|
|
|
|
template <typename T, size_t N>
|
|
|
|
static void renderDivider(T (&Columns)[N], raw_ostream &OS) {
|
|
|
|
unsigned Length = 0;
|
|
|
|
for (unsigned I = 0; I < N; ++I)
|
|
|
|
Length += Columns[I];
|
|
|
|
for (unsigned I = 0; I < Length; ++I)
|
|
|
|
OS << '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return the color which correponds to the coverage
|
|
|
|
/// percentage of a certain metric.
|
|
|
|
template <typename T>
|
|
|
|
static raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
|
|
|
|
if (Info.isFullyCovered())
|
|
|
|
return raw_ostream::GREEN;
|
|
|
|
return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
|
|
|
|
: raw_ostream::RED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
|
2015-09-15 01:26:36 +02:00
|
|
|
OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
|
|
|
|
<< format("%*u", FileReportColumns[1],
|
|
|
|
(unsigned)File.RegionCoverage.NumRegions);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(OS, File.RegionCoverage.isFullyCovered()
|
|
|
|
? raw_ostream::GREEN
|
|
|
|
: raw_ostream::RED)
|
2014-10-01 02:29:26 +02:00
|
|
|
<< format("%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(OS,
|
|
|
|
determineCoveragePercentageColor(File.RegionCoverage))
|
|
|
|
<< format("%*.2f", FileReportColumns[3] - 1,
|
|
|
|
File.RegionCoverage.getPercentCovered()) << '%';
|
2014-10-01 02:29:26 +02:00
|
|
|
OS << format("%*u", FileReportColumns[4],
|
|
|
|
(unsigned)File.FunctionCoverage.NumFunctions);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(
|
|
|
|
OS, determineCoveragePercentageColor(File.FunctionCoverage))
|
|
|
|
<< format("%*.2f", FileReportColumns[5] - 1,
|
|
|
|
File.FunctionCoverage.getPercentCovered()) << '%';
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::render(const FunctionCoverageSummary &Function,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
|
2014-10-01 02:29:26 +02:00
|
|
|
<< format("%*u", FunctionReportColumns[1],
|
|
|
|
(unsigned)Function.RegionCoverage.NumRegions);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(OS, Function.RegionCoverage.isFullyCovered()
|
|
|
|
? raw_ostream::GREEN
|
|
|
|
: raw_ostream::RED)
|
2014-10-01 02:29:26 +02:00
|
|
|
<< format("%*u", FunctionReportColumns[2],
|
|
|
|
(unsigned)Function.RegionCoverage.NotCovered);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(
|
|
|
|
OS, determineCoveragePercentageColor(Function.RegionCoverage))
|
|
|
|
<< format("%*.2f", FunctionReportColumns[3] - 1,
|
|
|
|
Function.RegionCoverage.getPercentCovered()) << '%';
|
2014-10-01 02:29:26 +02:00
|
|
|
OS << format("%*u", FunctionReportColumns[4],
|
|
|
|
(unsigned)Function.LineCoverage.NumLines);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(OS, Function.LineCoverage.isFullyCovered()
|
|
|
|
? raw_ostream::GREEN
|
|
|
|
: raw_ostream::RED)
|
2014-10-01 02:29:26 +02:00
|
|
|
<< format("%*u", FunctionReportColumns[5],
|
|
|
|
(unsigned)Function.LineCoverage.NotCovered);
|
2014-08-23 00:56:03 +02:00
|
|
|
Options.colored_ostream(
|
|
|
|
OS, determineCoveragePercentageColor(Function.LineCoverage))
|
|
|
|
<< format("%*.2f", FunctionReportColumns[6] - 1,
|
|
|
|
Function.LineCoverage.getPercentCovered()) << '%';
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:05:05 +01:00
|
|
|
void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
|
|
|
|
raw_ostream &OS) {
|
2015-10-21 18:03:32 +02:00
|
|
|
adjustColumnWidths(Coverage.get());
|
2014-08-23 00:56:03 +02:00
|
|
|
bool isFirst = true;
|
2015-02-14 03:05:05 +01:00
|
|
|
for (StringRef Filename : Files) {
|
2014-08-23 00:56:03 +02:00
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
|
|
|
OS << "\n";
|
2015-02-14 03:01:24 +01:00
|
|
|
OS << "File '" << Filename << "':\n";
|
2014-08-23 00:56:03 +02:00
|
|
|
OS << column("Name", FunctionReportColumns[0])
|
|
|
|
<< column("Regions", FunctionReportColumns[1], Column::RightAlignment)
|
|
|
|
<< column("Miss", FunctionReportColumns[2], Column::RightAlignment)
|
|
|
|
<< column("Cover", FunctionReportColumns[3], Column::RightAlignment)
|
|
|
|
<< column("Lines", FunctionReportColumns[4], Column::RightAlignment)
|
|
|
|
<< column("Miss", FunctionReportColumns[5], Column::RightAlignment)
|
|
|
|
<< column("Cover", FunctionReportColumns[6], Column::RightAlignment);
|
|
|
|
OS << "\n";
|
|
|
|
renderDivider(FunctionReportColumns, OS);
|
|
|
|
OS << "\n";
|
2015-02-14 03:01:24 +01:00
|
|
|
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;
|
2014-08-23 00:56:03 +02:00
|
|
|
render(Function, OS);
|
2015-02-14 03:01:24 +01:00
|
|
|
}
|
|
|
|
if (Totals.ExecutionCount) {
|
|
|
|
renderDivider(FunctionReportColumns, OS);
|
|
|
|
OS << "\n";
|
|
|
|
render(Totals, OS);
|
|
|
|
}
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::renderFileReports(raw_ostream &OS) {
|
2015-10-21 18:03:32 +02:00
|
|
|
adjustColumnWidths(Coverage.get());
|
2014-08-23 00:56:03 +02:00
|
|
|
OS << column("Filename", FileReportColumns[0])
|
|
|
|
<< column("Regions", FileReportColumns[1], Column::RightAlignment)
|
|
|
|
<< column("Miss", FileReportColumns[2], Column::RightAlignment)
|
|
|
|
<< column("Cover", FileReportColumns[3], Column::RightAlignment)
|
|
|
|
<< column("Functions", FileReportColumns[4], Column::RightAlignment)
|
2014-09-30 14:45:13 +02:00
|
|
|
<< column("Executed", FileReportColumns[5], Column::RightAlignment)
|
|
|
|
<< "\n";
|
2014-08-23 00:56:03 +02:00
|
|
|
renderDivider(FileReportColumns, OS);
|
|
|
|
OS << "\n";
|
2015-09-15 01:26:36 +02:00
|
|
|
|
2015-02-14 03:01:24 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-08-23 00:56:03 +02:00
|
|
|
renderDivider(FileReportColumns, OS);
|
|
|
|
OS << "\n";
|
2015-02-14 03:01:24 +01:00
|
|
|
render(Totals, OS);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|