blob: ed01a2e154f1ba364f31b31a229b872b46323123 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- CoverageReport.cpp - Code coverage report -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements rendering of a code coverage report.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CoverageReport.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000015#include "RenderingSupport.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "llvm/Support/FileSystem.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/Support/Format.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018
19using namespace llvm;
20namespace {
21/// \brief Helper struct which prints trimmed and aligned columns.
22struct Column {
Vedant Kumarc3c39e72015-09-14 23:26:36 +000023 enum TrimKind { NoTrim, WidthTrim, LeftTrim, RightTrim };
Alex Lorenze82d89c2014-08-22 22:56:03 +000024
25 enum AlignmentKind { LeftAlignment, RightAlignment };
26
27 StringRef Str;
28 unsigned Width;
29 TrimKind Trim;
30 AlignmentKind Alignment;
31
32 Column(StringRef Str, unsigned Width)
Vedant Kumarc3c39e72015-09-14 23:26:36 +000033 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +000034
35 Column &set(TrimKind Value) {
36 Trim = Value;
37 return *this;
38 }
39
40 Column &set(AlignmentKind Value) {
41 Alignment = Value;
42 return *this;
43 }
44
45 void render(raw_ostream &OS) const;
46};
Vedant Kumarc3c39e72015-09-14 23:26:36 +000047
Alex Lorenze82d89c2014-08-22 22:56:03 +000048raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
49 Value.render(OS);
50 return OS;
51}
52}
53
54void Column::render(raw_ostream &OS) const {
55 if (Str.size() <= Width) {
56 if (Alignment == RightAlignment) {
57 OS.indent(Width - Str.size());
58 OS << Str;
59 return;
60 }
61 OS << Str;
62 OS.indent(Width - Str.size());
63 return;
64 }
65
66 switch (Trim) {
67 case NoTrim:
Vedant Kumarc3c39e72015-09-14 23:26:36 +000068 OS << Str;
69 break;
70 case WidthTrim:
Alex Lorenze82d89c2014-08-22 22:56:03 +000071 OS << Str.substr(0, Width);
72 break;
73 case LeftTrim:
74 OS << "..." << Str.substr(Str.size() - Width + 3);
75 break;
76 case RightTrim:
77 OS << Str.substr(0, Width - 3) << "...";
78 break;
79 }
80}
81
82static Column column(StringRef Str, unsigned Width) {
83 return Column(Str, Width);
84}
85
86template <typename T>
87static Column column(StringRef Str, unsigned Width, const T &Value) {
88 return Column(Str, Width).set(Value);
89}
90
Vedant Kumarc3c39e72015-09-14 23:26:36 +000091static size_t FileReportColumns[] = {25, 10, 8, 8, 10, 10};
92static size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
Alex Lorenze82d89c2014-08-22 22:56:03 +000093
Vedant Kumaraaead332015-10-21 16:03:32 +000094/// \brief Adjust column widths to fit long file paths and function names.
95static void adjustColumnWidths(coverage::CoverageMapping *CM) {
96 for (StringRef Filename : CM->getUniqueSourceFiles()) {
97 FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
98 for (const auto &F : CM->getCoveredFunctions(Filename)) {
99 FunctionReportColumns[0] =
100 std::max(FunctionReportColumns[0], F.Name.size());
101 }
102 }
103}
104
Alex Lorenze82d89c2014-08-22 22:56:03 +0000105/// \brief Prints a horizontal divider which spans across the given columns.
106template <typename T, size_t N>
107static void renderDivider(T (&Columns)[N], raw_ostream &OS) {
108 unsigned Length = 0;
109 for (unsigned I = 0; I < N; ++I)
110 Length += Columns[I];
111 for (unsigned I = 0; I < Length; ++I)
112 OS << '-';
113}
114
115/// \brief Return the color which correponds to the coverage
116/// percentage of a certain metric.
117template <typename T>
118static raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
119 if (Info.isFullyCovered())
120 return raw_ostream::GREEN;
121 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
122 : raw_ostream::RED;
123}
124
125void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000126 OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
127 << format("%*u", FileReportColumns[1],
128 (unsigned)File.RegionCoverage.NumRegions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000129 Options.colored_ostream(OS, File.RegionCoverage.isFullyCovered()
130 ? raw_ostream::GREEN
131 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000132 << format("%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000133 Options.colored_ostream(OS,
134 determineCoveragePercentageColor(File.RegionCoverage))
135 << format("%*.2f", FileReportColumns[3] - 1,
136 File.RegionCoverage.getPercentCovered()) << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000137 OS << format("%*u", FileReportColumns[4],
138 (unsigned)File.FunctionCoverage.NumFunctions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000139 Options.colored_ostream(
140 OS, determineCoveragePercentageColor(File.FunctionCoverage))
141 << format("%*.2f", FileReportColumns[5] - 1,
142 File.FunctionCoverage.getPercentCovered()) << '%';
143 OS << "\n";
144}
145
146void CoverageReport::render(const FunctionCoverageSummary &Function,
147 raw_ostream &OS) {
148 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000149 << format("%*u", FunctionReportColumns[1],
150 (unsigned)Function.RegionCoverage.NumRegions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000151 Options.colored_ostream(OS, Function.RegionCoverage.isFullyCovered()
152 ? raw_ostream::GREEN
153 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000154 << format("%*u", FunctionReportColumns[2],
155 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000156 Options.colored_ostream(
157 OS, determineCoveragePercentageColor(Function.RegionCoverage))
158 << format("%*.2f", FunctionReportColumns[3] - 1,
159 Function.RegionCoverage.getPercentCovered()) << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000160 OS << format("%*u", FunctionReportColumns[4],
161 (unsigned)Function.LineCoverage.NumLines);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000162 Options.colored_ostream(OS, Function.LineCoverage.isFullyCovered()
163 ? raw_ostream::GREEN
164 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000165 << format("%*u", FunctionReportColumns[5],
166 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000167 Options.colored_ostream(
168 OS, determineCoveragePercentageColor(Function.LineCoverage))
169 << format("%*.2f", FunctionReportColumns[6] - 1,
170 Function.LineCoverage.getPercentCovered()) << '%';
171 OS << "\n";
172}
173
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000174void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
175 raw_ostream &OS) {
Vedant Kumaraaead332015-10-21 16:03:32 +0000176 adjustColumnWidths(Coverage.get());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000177 bool isFirst = true;
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000178 for (StringRef Filename : Files) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000179 if (isFirst)
180 isFirst = false;
181 else
182 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000183 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000184 OS << column("Name", FunctionReportColumns[0])
185 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
186 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
187 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
188 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
189 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
190 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
191 OS << "\n";
192 renderDivider(FunctionReportColumns, OS);
193 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000194 FunctionCoverageSummary Totals("TOTAL");
195 for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
196 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
197 ++Totals.ExecutionCount;
198 Totals.RegionCoverage += Function.RegionCoverage;
199 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000200 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000201 }
202 if (Totals.ExecutionCount) {
203 renderDivider(FunctionReportColumns, OS);
204 OS << "\n";
205 render(Totals, OS);
206 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000207 }
208}
209
210void CoverageReport::renderFileReports(raw_ostream &OS) {
Vedant Kumaraaead332015-10-21 16:03:32 +0000211 adjustColumnWidths(Coverage.get());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000212 OS << column("Filename", FileReportColumns[0])
213 << column("Regions", FileReportColumns[1], Column::RightAlignment)
214 << column("Miss", FileReportColumns[2], Column::RightAlignment)
215 << column("Cover", FileReportColumns[3], Column::RightAlignment)
216 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000217 << column("Executed", FileReportColumns[5], Column::RightAlignment)
218 << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000219 renderDivider(FileReportColumns, OS);
220 OS << "\n";
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000221
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000222 FileCoverageSummary Totals("TOTAL");
223 for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
224 FileCoverageSummary Summary(Filename);
225 for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
226 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
227 Summary.addFunction(Function);
228 Totals.addFunction(Function);
229 }
230 render(Summary, OS);
231 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000232 renderDivider(FileReportColumns, OS);
233 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000234 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000235}