2014-08-23 06:56:03 +08: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 19:23:27 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2016-09-07 06:45:57 +08:00
|
|
|
#include <numeric>
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-09-07 06:45:57 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
namespace {
|
2016-09-07 06:45:57 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
/// \brief Helper struct which prints trimmed and aligned columns.
|
|
|
|
struct Column {
|
2016-09-07 06:45:57 +08:00
|
|
|
enum TrimKind { NoTrim, WidthTrim, RightTrim };
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
enum AlignmentKind { LeftAlignment, RightAlignment };
|
|
|
|
|
|
|
|
StringRef Str;
|
|
|
|
unsigned Width;
|
|
|
|
TrimKind Trim;
|
|
|
|
AlignmentKind Alignment;
|
|
|
|
|
|
|
|
Column(StringRef Str, unsigned Width)
|
2015-09-15 07:26:36 +08:00
|
|
|
: Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
Column &set(TrimKind Value) {
|
|
|
|
Trim = Value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Column &set(AlignmentKind Value) {
|
|
|
|
Alignment = Value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
void render(raw_ostream &OS) const {
|
|
|
|
if (Str.size() <= Width) {
|
|
|
|
if (Alignment == RightAlignment) {
|
|
|
|
OS.indent(Width - Str.size());
|
|
|
|
OS << Str;
|
|
|
|
return;
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << Str;
|
2016-09-07 06:45:57 +08:00
|
|
|
OS.indent(Width - Str.size());
|
2014-08-23 06:56:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
switch (Trim) {
|
|
|
|
case NoTrim:
|
|
|
|
OS << Str;
|
|
|
|
break;
|
|
|
|
case WidthTrim:
|
|
|
|
OS << Str.substr(0, Width);
|
|
|
|
break;
|
|
|
|
case RightTrim:
|
|
|
|
OS << Str.substr(0, Width - 3) << "...";
|
|
|
|
break;
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
2016-09-07 06:45:57 +08:00
|
|
|
};
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
|
|
|
|
Value.render(OS);
|
|
|
|
return OS;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
template <typename T>
|
2016-09-07 06:45:57 +08:00
|
|
|
Column column(StringRef Str, unsigned Width, const T &Value) {
|
2014-08-23 06:56:03 +08:00
|
|
|
return Column(Str, Width).set(Value);
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:46:13 +08:00
|
|
|
// Specify the default column widths.
|
2016-09-07 06:45:57 +08:00
|
|
|
size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 12, 18, 10};
|
|
|
|
size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
/// \brief Adjust column widths to fit long file paths and function names.
|
|
|
|
void adjustColumnWidths(const coverage::CoverageMapping &CM) {
|
|
|
|
for (StringRef Filename : CM.getUniqueSourceFiles()) {
|
2015-10-22 00:03:32 +08:00
|
|
|
FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
|
2016-09-07 06:45:57 +08:00
|
|
|
for (const auto &F : CM.getCoveredFunctions(Filename)) {
|
2015-10-22 00:03:32 +08:00
|
|
|
FunctionReportColumns[0] =
|
|
|
|
std::max(FunctionReportColumns[0], F.Name.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
/// \brief Prints a horizontal divider long enough to cover the given column
|
|
|
|
/// widths.
|
|
|
|
void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
|
|
|
|
size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
|
|
|
|
for (size_t I = 0; I < Length; ++I)
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << '-';
|
|
|
|
}
|
|
|
|
|
2016-09-07 06:46:00 +08:00
|
|
|
/// \brief Return the color which correponds to the coverage percentage of a
|
|
|
|
/// certain metric.
|
2014-08-23 06:56:03 +08:00
|
|
|
template <typename T>
|
2016-09-07 06:45:57 +08:00
|
|
|
raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
|
2014-08-23 06:56:03 +08:00
|
|
|
if (Info.isFullyCovered())
|
|
|
|
return raw_ostream::GREEN;
|
|
|
|
return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
|
|
|
|
: raw_ostream::RED;
|
|
|
|
}
|
|
|
|
|
2016-09-08 08:56:43 +08:00
|
|
|
/// \brief Determine the length of the longest common prefix of the strings in
|
|
|
|
/// \p Strings.
|
|
|
|
unsigned getLongestCommonPrefixLen(ArrayRef<StringRef> Strings) {
|
|
|
|
unsigned LCP = Strings[0].size();
|
|
|
|
for (unsigned I = 1, E = Strings.size(); LCP > 0 && I < E; ++I) {
|
|
|
|
auto Mismatch =
|
|
|
|
std::mismatch(Strings[0].begin(), Strings[0].end(), Strings[I].begin())
|
|
|
|
.first;
|
|
|
|
LCP = std::min(LCP, (unsigned)std::distance(Strings[0].begin(), Mismatch));
|
|
|
|
}
|
|
|
|
return LCP;
|
|
|
|
}
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2016-09-09 09:32:49 +08:00
|
|
|
void CoverageReport::render(const FileCoverageSummary &File,
|
|
|
|
raw_ostream &OS) const {
|
2016-09-07 06:46:00 +08:00
|
|
|
auto FileCoverageColor =
|
|
|
|
determineCoveragePercentageColor(File.RegionCoverage);
|
|
|
|
auto FuncCoverageColor =
|
|
|
|
determineCoveragePercentageColor(File.FunctionCoverage);
|
|
|
|
auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
|
2015-09-15 07:26:36 +08:00
|
|
|
OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
|
|
|
|
<< format("%*u", FileReportColumns[1],
|
|
|
|
(unsigned)File.RegionCoverage.NumRegions);
|
2016-09-07 06:46:00 +08:00
|
|
|
Options.colored_ostream(OS, FileCoverageColor) << format(
|
|
|
|
"%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
|
|
|
|
Options.colored_ostream(OS, FileCoverageColor)
|
2014-08-23 06:56:03 +08:00
|
|
|
<< format("%*.2f", FileReportColumns[3] - 1,
|
2016-09-07 06:46:00 +08:00
|
|
|
File.RegionCoverage.getPercentCovered())
|
|
|
|
<< '%';
|
2014-10-01 08:29:26 +08:00
|
|
|
OS << format("%*u", FileReportColumns[4],
|
|
|
|
(unsigned)File.FunctionCoverage.NumFunctions);
|
2016-07-22 20:46:13 +08:00
|
|
|
OS << format("%*u", FileReportColumns[5],
|
|
|
|
(unsigned)(File.FunctionCoverage.NumFunctions -
|
|
|
|
File.FunctionCoverage.Executed));
|
2016-09-07 06:46:00 +08:00
|
|
|
Options.colored_ostream(OS, FuncCoverageColor)
|
2016-07-22 20:46:13 +08:00
|
|
|
<< format("%*.2f", FileReportColumns[6] - 1,
|
2016-09-07 06:46:00 +08:00
|
|
|
File.FunctionCoverage.getPercentCovered())
|
|
|
|
<< '%';
|
2016-07-22 20:46:13 +08:00
|
|
|
OS << format("%*u", FileReportColumns[7],
|
|
|
|
(unsigned)File.LineCoverage.NumLines);
|
2016-09-07 06:46:00 +08:00
|
|
|
Options.colored_ostream(OS, LineCoverageColor) << format(
|
|
|
|
"%*u", FileReportColumns[8], (unsigned)File.LineCoverage.NotCovered);
|
|
|
|
Options.colored_ostream(OS, LineCoverageColor)
|
2016-07-22 20:46:13 +08:00
|
|
|
<< format("%*.2f", FileReportColumns[9] - 1,
|
2016-09-07 06:46:00 +08:00
|
|
|
File.LineCoverage.getPercentCovered())
|
|
|
|
<< '%';
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::render(const FunctionCoverageSummary &Function,
|
2016-09-09 09:32:49 +08:00
|
|
|
raw_ostream &OS) const {
|
2016-09-07 06:46:00 +08:00
|
|
|
auto FuncCoverageColor =
|
|
|
|
determineCoveragePercentageColor(Function.RegionCoverage);
|
|
|
|
auto LineCoverageColor =
|
|
|
|
determineCoveragePercentageColor(Function.LineCoverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
|
2014-10-01 08:29:26 +08:00
|
|
|
<< format("%*u", FunctionReportColumns[1],
|
|
|
|
(unsigned)Function.RegionCoverage.NumRegions);
|
2016-09-07 06:46:00 +08:00
|
|
|
Options.colored_ostream(OS, FuncCoverageColor)
|
2014-10-01 08:29:26 +08:00
|
|
|
<< format("%*u", FunctionReportColumns[2],
|
|
|
|
(unsigned)Function.RegionCoverage.NotCovered);
|
2014-08-23 06:56:03 +08:00
|
|
|
Options.colored_ostream(
|
|
|
|
OS, determineCoveragePercentageColor(Function.RegionCoverage))
|
|
|
|
<< format("%*.2f", FunctionReportColumns[3] - 1,
|
2016-09-07 06:46:00 +08:00
|
|
|
Function.RegionCoverage.getPercentCovered())
|
|
|
|
<< '%';
|
2014-10-01 08:29:26 +08:00
|
|
|
OS << format("%*u", FunctionReportColumns[4],
|
|
|
|
(unsigned)Function.LineCoverage.NumLines);
|
2016-09-07 06:46:00 +08:00
|
|
|
Options.colored_ostream(OS, LineCoverageColor)
|
2014-10-01 08:29:26 +08:00
|
|
|
<< format("%*u", FunctionReportColumns[5],
|
|
|
|
(unsigned)Function.LineCoverage.NotCovered);
|
2014-08-23 06:56:03 +08:00
|
|
|
Options.colored_ostream(
|
|
|
|
OS, determineCoveragePercentageColor(Function.LineCoverage))
|
|
|
|
<< format("%*.2f", FunctionReportColumns[6] - 1,
|
2016-09-07 06:46:00 +08:00
|
|
|
Function.LineCoverage.getPercentCovered())
|
|
|
|
<< '%';
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2016-06-29 00:12:18 +08:00
|
|
|
void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
|
2015-02-14 10:05:05 +08:00
|
|
|
raw_ostream &OS) {
|
2016-09-07 06:45:57 +08:00
|
|
|
adjustColumnWidths(Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
bool isFirst = true;
|
2015-02-14 10:05:05 +08:00
|
|
|
for (StringRef Filename : Files) {
|
2014-08-23 06:56:03 +08:00
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
|
|
|
OS << "\n";
|
2015-02-14 10:01:24 +08:00
|
|
|
OS << "File '" << Filename << "':\n";
|
2014-08-23 06:56:03 +08: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 10:01:24 +08:00
|
|
|
FunctionCoverageSummary Totals("TOTAL");
|
2016-09-07 06:45:57 +08:00
|
|
|
for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
|
2015-02-14 10:01:24 +08:00
|
|
|
FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
|
|
|
|
++Totals.ExecutionCount;
|
|
|
|
Totals.RegionCoverage += Function.RegionCoverage;
|
|
|
|
Totals.LineCoverage += Function.LineCoverage;
|
2014-08-23 06:56:03 +08:00
|
|
|
render(Function, OS);
|
2015-02-14 10:01:24 +08:00
|
|
|
}
|
|
|
|
if (Totals.ExecutionCount) {
|
|
|
|
renderDivider(FunctionReportColumns, OS);
|
|
|
|
OS << "\n";
|
|
|
|
render(Totals, OS);
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:32:49 +08:00
|
|
|
std::vector<FileCoverageSummary>
|
|
|
|
CoverageReport::prepareFileReports(FileCoverageSummary &Totals,
|
|
|
|
ArrayRef<StringRef> Files) const {
|
|
|
|
std::vector<FileCoverageSummary> FileReports;
|
|
|
|
unsigned LCP = 0;
|
|
|
|
if (Files.size() > 1)
|
|
|
|
LCP = getLongestCommonPrefixLen(Files);
|
|
|
|
|
|
|
|
for (StringRef Filename : Files) {
|
|
|
|
FileCoverageSummary Summary(Filename.drop_front(LCP));
|
|
|
|
for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
|
|
|
|
FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
|
|
|
|
Summary.addFunction(Function);
|
|
|
|
Totals.addFunction(Function);
|
|
|
|
}
|
|
|
|
FileReports.push_back(Summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileReports;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::renderFileReports(raw_ostream &OS) const {
|
|
|
|
std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
|
|
|
|
renderFileReports(OS, UniqueSourceFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageReport::renderFileReports(raw_ostream &OS,
|
|
|
|
ArrayRef<StringRef> Files) const {
|
2016-09-07 06:45:57 +08:00
|
|
|
adjustColumnWidths(Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << column("Filename", FileReportColumns[0])
|
|
|
|
<< column("Regions", FileReportColumns[1], Column::RightAlignment)
|
2016-07-22 20:46:13 +08:00
|
|
|
<< column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
|
2014-08-23 06:56:03 +08:00
|
|
|
<< column("Cover", FileReportColumns[3], Column::RightAlignment)
|
|
|
|
<< column("Functions", FileReportColumns[4], Column::RightAlignment)
|
2016-09-07 06:46:00 +08:00
|
|
|
<< column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
|
2016-07-22 20:46:13 +08:00
|
|
|
<< column("Executed", FileReportColumns[6], Column::RightAlignment)
|
|
|
|
<< column("Lines", FileReportColumns[7], Column::RightAlignment)
|
|
|
|
<< column("Missed Lines", FileReportColumns[8], Column::RightAlignment)
|
2016-09-07 06:46:00 +08:00
|
|
|
<< column("Cover", FileReportColumns[9], Column::RightAlignment) << "\n";
|
2014-08-23 06:56:03 +08:00
|
|
|
renderDivider(FileReportColumns, OS);
|
|
|
|
OS << "\n";
|
2015-09-15 07:26:36 +08:00
|
|
|
|
2015-02-14 10:01:24 +08:00
|
|
|
FileCoverageSummary Totals("TOTAL");
|
2016-09-09 09:32:49 +08:00
|
|
|
auto FileReports = prepareFileReports(Totals, Files);
|
|
|
|
for (const FileCoverageSummary &FCS : FileReports)
|
|
|
|
render(FCS, OS);
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
renderDivider(FileReportColumns, OS);
|
|
|
|
OS << "\n";
|
2015-02-14 10:01:24 +08:00
|
|
|
render(Totals, OS);
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
2016-09-07 06:45:57 +08:00
|
|
|
|
|
|
|
} // end namespace llvm
|