#include "RenderingSupport.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
+#include <numeric>
using namespace llvm;
+
namespace {
+
/// \brief Helper struct which prints trimmed and aligned columns.
struct Column {
- enum TrimKind { NoTrim, WidthTrim, LeftTrim, RightTrim };
+ enum TrimKind { NoTrim, WidthTrim, RightTrim };
enum AlignmentKind { LeftAlignment, RightAlignment };
return *this;
}
- void render(raw_ostream &OS) const;
-};
-
-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());
+ void 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;
}
- OS << Str;
- OS.indent(Width - Str.size());
- return;
- }
- switch (Trim) {
- case NoTrim:
- OS << Str;
- break;
- case WidthTrim:
- 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;
+ switch (Trim) {
+ case NoTrim:
+ OS << Str;
+ break;
+ case WidthTrim:
+ OS << Str.substr(0, Width);
+ break;
+ case RightTrim:
+ OS << Str.substr(0, Width - 3) << "...";
+ break;
+ }
}
-}
+};
-static Column column(StringRef Str, unsigned Width) {
- return Column(Str, Width);
+raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
+ Value.render(OS);
+ return OS;
}
+Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
+
template <typename T>
-static Column column(StringRef Str, unsigned Width, const T &Value) {
+Column column(StringRef Str, unsigned Width, const T &Value) {
return Column(Str, Width).set(Value);
}
// Specify the default column widths.
-static size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 12, 18, 10};
-static size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
+size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 12, 18, 10};
+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()) {
+/// \brief Adjust column widths to fit long file paths and function names.
+void adjustColumnWidths(const coverage::CoverageMapping &CM) {
+ for (StringRef Filename : CM.getUniqueSourceFiles()) {
FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
- for (const auto &F : CM->getCoveredFunctions(Filename)) {
+ 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>
-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)
+/// \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)
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) {
+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;
}
+} // end anonymous namespace
+
+namespace llvm {
+
void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
<< format("%*u", FileReportColumns[1],
void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
raw_ostream &OS) {
- adjustColumnWidths(Coverage.get());
+ adjustColumnWidths(Coverage);
bool isFirst = true;
for (StringRef Filename : Files) {
if (isFirst)
renderDivider(FunctionReportColumns, OS);
OS << "\n";
FunctionCoverageSummary Totals("TOTAL");
- for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
+ for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
++Totals.ExecutionCount;
Totals.RegionCoverage += Function.RegionCoverage;
}
void CoverageReport::renderFileReports(raw_ostream &OS) {
- adjustColumnWidths(Coverage.get());
+ adjustColumnWidths(Coverage);
OS << column("Filename", FileReportColumns[0])
<< column("Regions", FileReportColumns[1], Column::RightAlignment)
<< column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
OS << "\n";
FileCoverageSummary Totals("TOTAL");
- for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
+ for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
FileCoverageSummary Summary(Filename);
- for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
+ for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
Summary.addFunction(Function);
Totals.addFunction(Function);
OS << "\n";
render(Totals, OS);
}
+
+} // end namespace llvm