blob: 332268a23437fd2675c8b5d91824e6a0a36ef8b5 [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"
Vedant Kumard938dfb2016-09-09 17:37:11 +000018#include "llvm/Support/Path.h"
Vedant Kumar702bb9d2016-09-06 22:45:57 +000019#include <numeric>
Alex Lorenze82d89c2014-08-22 22:56:03 +000020
21using namespace llvm;
Vedant Kumar702bb9d2016-09-06 22:45:57 +000022
Alex Lorenze82d89c2014-08-22 22:56:03 +000023namespace {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000024
Alex Lorenze82d89c2014-08-22 22:56:03 +000025/// \brief Helper struct which prints trimmed and aligned columns.
26struct Column {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000027 enum TrimKind { NoTrim, WidthTrim, RightTrim };
Alex Lorenze82d89c2014-08-22 22:56:03 +000028
29 enum AlignmentKind { LeftAlignment, RightAlignment };
30
31 StringRef Str;
32 unsigned Width;
33 TrimKind Trim;
34 AlignmentKind Alignment;
35
36 Column(StringRef Str, unsigned Width)
Vedant Kumarc3c39e72015-09-14 23:26:36 +000037 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +000038
39 Column &set(TrimKind Value) {
40 Trim = Value;
41 return *this;
42 }
43
44 Column &set(AlignmentKind Value) {
45 Alignment = Value;
46 return *this;
47 }
48
Vedant Kumar702bb9d2016-09-06 22:45:57 +000049 void render(raw_ostream &OS) const {
50 if (Str.size() <= Width) {
51 if (Alignment == RightAlignment) {
52 OS.indent(Width - Str.size());
53 OS << Str;
54 return;
55 }
56 OS << Str;
57 OS.indent(Width - Str.size());
58 return;
59 }
60
61 switch (Trim) {
62 case NoTrim:
63 OS << Str;
64 break;
65 case WidthTrim:
66 OS << Str.substr(0, Width);
67 break;
68 case RightTrim:
69 OS << Str.substr(0, Width - 3) << "...";
70 break;
71 }
72 }
Alex Lorenze82d89c2014-08-22 22:56:03 +000073};
Vedant Kumarc3c39e72015-09-14 23:26:36 +000074
Alex Lorenze82d89c2014-08-22 22:56:03 +000075raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
76 Value.render(OS);
77 return OS;
78}
Alex Lorenze82d89c2014-08-22 22:56:03 +000079
Vedant Kumar702bb9d2016-09-06 22:45:57 +000080Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
Alex Lorenze82d89c2014-08-22 22:56:03 +000081
82template <typename T>
Vedant Kumar702bb9d2016-09-06 22:45:57 +000083Column column(StringRef Str, unsigned Width, const T &Value) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000084 return Column(Str, Width).set(Value);
85}
86
Ying Yie59ee432016-07-22 12:46:13 +000087// Specify the default column widths.
Vedant Kumar702bb9d2016-09-06 22:45:57 +000088size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 12, 18, 10};
89size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
Alex Lorenze82d89c2014-08-22 22:56:03 +000090
Vedant Kumar702bb9d2016-09-06 22:45:57 +000091/// \brief Adjust column widths to fit long file paths and function names.
Vedant Kumardab0ec12016-09-19 00:38:16 +000092void adjustColumnWidths(ArrayRef<StringRef> Files,
93 ArrayRef<StringRef> Functions) {
94 for (StringRef Filename : Files)
Vedant Kumaraaead332015-10-21 16:03:32 +000095 FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
Vedant Kumardab0ec12016-09-19 00:38:16 +000096 for (StringRef Funcname : Functions)
97 FunctionReportColumns[0] =
98 std::max(FunctionReportColumns[0], Funcname.size());
Vedant Kumaraaead332015-10-21 16:03:32 +000099}
100
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000101/// \brief Prints a horizontal divider long enough to cover the given column
102/// widths.
103void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
104 size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
105 for (size_t I = 0; I < Length; ++I)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000106 OS << '-';
107}
108
Vedant Kumar5053b112016-09-06 22:46:00 +0000109/// \brief Return the color which correponds to the coverage percentage of a
110/// certain metric.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000111template <typename T>
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000112raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113 if (Info.isFullyCovered())
114 return raw_ostream::GREEN;
115 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
116 : raw_ostream::RED;
117}
118
Vedant Kumarfa754372016-09-08 00:56:43 +0000119/// \brief Determine the length of the longest common prefix of the strings in
120/// \p Strings.
121unsigned getLongestCommonPrefixLen(ArrayRef<StringRef> Strings) {
122 unsigned LCP = Strings[0].size();
123 for (unsigned I = 1, E = Strings.size(); LCP > 0 && I < E; ++I) {
124 auto Mismatch =
125 std::mismatch(Strings[0].begin(), Strings[0].end(), Strings[I].begin())
126 .first;
127 LCP = std::min(LCP, (unsigned)std::distance(Strings[0].begin(), Mismatch));
128 }
129 return LCP;
130}
131
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000132} // end anonymous namespace
133
134namespace llvm {
135
Vedant Kumar627887b62016-09-09 01:32:49 +0000136void CoverageReport::render(const FileCoverageSummary &File,
137 raw_ostream &OS) const {
Vedant Kumar5053b112016-09-06 22:46:00 +0000138 auto FileCoverageColor =
139 determineCoveragePercentageColor(File.RegionCoverage);
140 auto FuncCoverageColor =
141 determineCoveragePercentageColor(File.FunctionCoverage);
142 auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
Vedant Kumard938dfb2016-09-09 17:37:11 +0000143 SmallString<256> FileName = File.Name;
144 sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
145 sys::path::native(FileName);
146 OS << column(FileName, FileReportColumns[0], Column::NoTrim)
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000147 << format("%*u", FileReportColumns[1],
148 (unsigned)File.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000149 Options.colored_ostream(OS, FileCoverageColor) << format(
150 "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
151 Options.colored_ostream(OS, FileCoverageColor)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000152 << format("%*.2f", FileReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000153 File.RegionCoverage.getPercentCovered())
154 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000155 OS << format("%*u", FileReportColumns[4],
156 (unsigned)File.FunctionCoverage.NumFunctions);
Ying Yie59ee432016-07-22 12:46:13 +0000157 OS << format("%*u", FileReportColumns[5],
158 (unsigned)(File.FunctionCoverage.NumFunctions -
159 File.FunctionCoverage.Executed));
Vedant Kumar5053b112016-09-06 22:46:00 +0000160 Options.colored_ostream(OS, FuncCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000161 << format("%*.2f", FileReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000162 File.FunctionCoverage.getPercentCovered())
163 << '%';
Ying Yie59ee432016-07-22 12:46:13 +0000164 OS << format("%*u", FileReportColumns[7],
165 (unsigned)File.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000166 Options.colored_ostream(OS, LineCoverageColor) << format(
167 "%*u", FileReportColumns[8], (unsigned)File.LineCoverage.NotCovered);
168 Options.colored_ostream(OS, LineCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000169 << format("%*.2f", FileReportColumns[9] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000170 File.LineCoverage.getPercentCovered())
171 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000172 OS << "\n";
173}
174
175void CoverageReport::render(const FunctionCoverageSummary &Function,
Vedant Kumar627887b62016-09-09 01:32:49 +0000176 raw_ostream &OS) const {
Vedant Kumar5053b112016-09-06 22:46:00 +0000177 auto FuncCoverageColor =
178 determineCoveragePercentageColor(Function.RegionCoverage);
179 auto LineCoverageColor =
180 determineCoveragePercentageColor(Function.LineCoverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000181 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000182 << format("%*u", FunctionReportColumns[1],
183 (unsigned)Function.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000184 Options.colored_ostream(OS, FuncCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000185 << format("%*u", FunctionReportColumns[2],
186 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000187 Options.colored_ostream(
188 OS, determineCoveragePercentageColor(Function.RegionCoverage))
189 << format("%*.2f", FunctionReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000190 Function.RegionCoverage.getPercentCovered())
191 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000192 OS << format("%*u", FunctionReportColumns[4],
193 (unsigned)Function.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000194 Options.colored_ostream(OS, LineCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000195 << format("%*u", FunctionReportColumns[5],
196 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000197 Options.colored_ostream(
198 OS, determineCoveragePercentageColor(Function.LineCoverage))
199 << format("%*.2f", FunctionReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000200 Function.LineCoverage.getPercentCovered())
201 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000202 OS << "\n";
203}
204
Vedant Kumarcef440f2016-06-28 16:12:18 +0000205void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000206 raw_ostream &OS) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000207 bool isFirst = true;
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000208 for (StringRef Filename : Files) {
Vedant Kumardab0ec12016-09-19 00:38:16 +0000209 auto Functions = Coverage.getCoveredFunctions(Filename);
210
Alex Lorenze82d89c2014-08-22 22:56:03 +0000211 if (isFirst)
212 isFirst = false;
213 else
214 OS << "\n";
Vedant Kumardab0ec12016-09-19 00:38:16 +0000215
216 std::vector<StringRef> Funcnames;
217 for (const auto &F : Functions)
218 Funcnames.emplace_back(F.Name);
219 adjustColumnWidths({}, Funcnames);
220
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000221 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000222 OS << column("Name", FunctionReportColumns[0])
223 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
224 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
225 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
226 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
227 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
228 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
229 OS << "\n";
230 renderDivider(FunctionReportColumns, OS);
231 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000232 FunctionCoverageSummary Totals("TOTAL");
Vedant Kumardab0ec12016-09-19 00:38:16 +0000233 for (const auto &F : Functions) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000234 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
235 ++Totals.ExecutionCount;
236 Totals.RegionCoverage += Function.RegionCoverage;
237 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000238 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000239 }
240 if (Totals.ExecutionCount) {
241 renderDivider(FunctionReportColumns, OS);
242 OS << "\n";
243 render(Totals, OS);
244 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000245 }
246}
247
Vedant Kumar627887b62016-09-09 01:32:49 +0000248std::vector<FileCoverageSummary>
249CoverageReport::prepareFileReports(FileCoverageSummary &Totals,
250 ArrayRef<StringRef> Files) const {
251 std::vector<FileCoverageSummary> FileReports;
252 unsigned LCP = 0;
253 if (Files.size() > 1)
254 LCP = getLongestCommonPrefixLen(Files);
255
256 for (StringRef Filename : Files) {
257 FileCoverageSummary Summary(Filename.drop_front(LCP));
258 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
259 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
260 Summary.addFunction(Function);
261 Totals.addFunction(Function);
262 }
263 FileReports.push_back(Summary);
264 }
265
266 return FileReports;
267}
268
269void CoverageReport::renderFileReports(raw_ostream &OS) const {
270 std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
271 renderFileReports(OS, UniqueSourceFiles);
272}
273
274void CoverageReport::renderFileReports(raw_ostream &OS,
275 ArrayRef<StringRef> Files) const {
Vedant Kumardab0ec12016-09-19 00:38:16 +0000276 FileCoverageSummary Totals("TOTAL");
277 auto FileReports = prepareFileReports(Totals, Files);
278
279 std::vector<StringRef> Filenames;
280 for (const FileCoverageSummary &FCS : FileReports)
281 Filenames.emplace_back(FCS.Name);
282 adjustColumnWidths(Filenames, {});
283
Alex Lorenze82d89c2014-08-22 22:56:03 +0000284 OS << column("Filename", FileReportColumns[0])
285 << column("Regions", FileReportColumns[1], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000286 << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000287 << column("Cover", FileReportColumns[3], Column::RightAlignment)
288 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000289 << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000290 << column("Executed", FileReportColumns[6], Column::RightAlignment)
291 << column("Lines", FileReportColumns[7], Column::RightAlignment)
292 << column("Missed Lines", FileReportColumns[8], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000293 << column("Cover", FileReportColumns[9], Column::RightAlignment) << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000294 renderDivider(FileReportColumns, OS);
295 OS << "\n";
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000296
Vedant Kumar627887b62016-09-09 01:32:49 +0000297 for (const FileCoverageSummary &FCS : FileReports)
298 render(FCS, OS);
299
Alex Lorenze82d89c2014-08-22 22:56:03 +0000300 renderDivider(FileReportColumns, OS);
301 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000302 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000303}
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000304
305} // end namespace llvm