blob: e70cce40a98767d88af2f4905f4d2d72b00e4630 [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"
Vedant Kumar016111f2016-09-19 00:38:23 +000016#include "llvm/ADT/DenseMap.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include "llvm/Support/FileSystem.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/Support/Format.h"
Vedant Kumard938dfb2016-09-09 17:37:11 +000019#include "llvm/Support/Path.h"
Vedant Kumar702bb9d2016-09-06 22:45:57 +000020#include <numeric>
Alex Lorenze82d89c2014-08-22 22:56:03 +000021
22using namespace llvm;
Vedant Kumar702bb9d2016-09-06 22:45:57 +000023
Alex Lorenze82d89c2014-08-22 22:56:03 +000024namespace {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000025
Alex Lorenze82d89c2014-08-22 22:56:03 +000026/// \brief Helper struct which prints trimmed and aligned columns.
27struct Column {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000028 enum TrimKind { NoTrim, WidthTrim, RightTrim };
Alex Lorenze82d89c2014-08-22 22:56:03 +000029
30 enum AlignmentKind { LeftAlignment, RightAlignment };
31
32 StringRef Str;
33 unsigned Width;
34 TrimKind Trim;
35 AlignmentKind Alignment;
36
37 Column(StringRef Str, unsigned Width)
Vedant Kumarc3c39e72015-09-14 23:26:36 +000038 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +000039
40 Column &set(TrimKind Value) {
41 Trim = Value;
42 return *this;
43 }
44
45 Column &set(AlignmentKind Value) {
46 Alignment = Value;
47 return *this;
48 }
49
Vedant Kumar702bb9d2016-09-06 22:45:57 +000050 void render(raw_ostream &OS) const {
51 if (Str.size() <= Width) {
52 if (Alignment == RightAlignment) {
53 OS.indent(Width - Str.size());
54 OS << Str;
55 return;
56 }
57 OS << Str;
58 OS.indent(Width - Str.size());
59 return;
60 }
61
62 switch (Trim) {
63 case NoTrim:
64 OS << Str;
65 break;
66 case WidthTrim:
67 OS << Str.substr(0, Width);
68 break;
69 case RightTrim:
70 OS << Str.substr(0, Width - 3) << "...";
71 break;
72 }
73 }
Alex Lorenze82d89c2014-08-22 22:56:03 +000074};
Vedant Kumarc3c39e72015-09-14 23:26:36 +000075
Alex Lorenze82d89c2014-08-22 22:56:03 +000076raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
77 Value.render(OS);
78 return OS;
79}
Alex Lorenze82d89c2014-08-22 22:56:03 +000080
Vedant Kumar702bb9d2016-09-06 22:45:57 +000081Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
Alex Lorenze82d89c2014-08-22 22:56:03 +000082
83template <typename T>
Vedant Kumar702bb9d2016-09-06 22:45:57 +000084Column column(StringRef Str, unsigned Width, const T &Value) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000085 return Column(Str, Width).set(Value);
86}
87
Ying Yie59ee432016-07-22 12:46:13 +000088// Specify the default column widths.
Vedant Kumar016111f2016-09-19 00:38:23 +000089size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10,
90 16, 16, 10, 12, 18, 10};
Vedant Kumar702bb9d2016-09-06 22:45:57 +000091size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
Alex Lorenze82d89c2014-08-22 22:56:03 +000092
Vedant Kumar702bb9d2016-09-06 22:45:57 +000093/// \brief Adjust column widths to fit long file paths and function names.
Vedant Kumardab0ec12016-09-19 00:38:16 +000094void adjustColumnWidths(ArrayRef<StringRef> Files,
95 ArrayRef<StringRef> Functions) {
96 for (StringRef Filename : Files)
Vedant Kumaraaead332015-10-21 16:03:32 +000097 FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
Vedant Kumardab0ec12016-09-19 00:38:16 +000098 for (StringRef Funcname : Functions)
99 FunctionReportColumns[0] =
100 std::max(FunctionReportColumns[0], Funcname.size());
Vedant Kumaraaead332015-10-21 16:03:32 +0000101}
102
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000103/// \brief Prints a horizontal divider long enough to cover the given column
104/// widths.
105void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
106 size_t Length = std::accumulate(ColumnWidths.begin(), ColumnWidths.end(), 0);
107 for (size_t I = 0; I < Length; ++I)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000108 OS << '-';
109}
110
Vedant Kumar5053b112016-09-06 22:46:00 +0000111/// \brief Return the color which correponds to the coverage percentage of a
112/// certain metric.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113template <typename T>
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000114raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000115 if (Info.isFullyCovered())
116 return raw_ostream::GREEN;
117 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
118 : raw_ostream::RED;
119}
120
Vedant Kumarfa754372016-09-08 00:56:43 +0000121/// \brief Determine the length of the longest common prefix of the strings in
122/// \p Strings.
123unsigned getLongestCommonPrefixLen(ArrayRef<StringRef> Strings) {
124 unsigned LCP = Strings[0].size();
125 for (unsigned I = 1, E = Strings.size(); LCP > 0 && I < E; ++I) {
126 auto Mismatch =
127 std::mismatch(Strings[0].begin(), Strings[0].end(), Strings[I].begin())
128 .first;
129 LCP = std::min(LCP, (unsigned)std::distance(Strings[0].begin(), Mismatch));
130 }
131 return LCP;
132}
133
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000134} // end anonymous namespace
135
136namespace llvm {
137
Vedant Kumar627887b62016-09-09 01:32:49 +0000138void CoverageReport::render(const FileCoverageSummary &File,
139 raw_ostream &OS) const {
Vedant Kumar5053b112016-09-06 22:46:00 +0000140 auto FileCoverageColor =
141 determineCoveragePercentageColor(File.RegionCoverage);
142 auto FuncCoverageColor =
143 determineCoveragePercentageColor(File.FunctionCoverage);
Vedant Kumar016111f2016-09-19 00:38:23 +0000144 auto InstantiationCoverageColor =
145 determineCoveragePercentageColor(File.InstantiationCoverage);
Vedant Kumar5053b112016-09-06 22:46:00 +0000146 auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
Vedant Kumard938dfb2016-09-09 17:37:11 +0000147 SmallString<256> FileName = File.Name;
148 sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
149 sys::path::native(FileName);
150 OS << column(FileName, FileReportColumns[0], Column::NoTrim)
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000151 << format("%*u", FileReportColumns[1],
152 (unsigned)File.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000153 Options.colored_ostream(OS, FileCoverageColor) << format(
154 "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
155 Options.colored_ostream(OS, FileCoverageColor)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000156 << format("%*.2f", FileReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000157 File.RegionCoverage.getPercentCovered())
158 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000159 OS << format("%*u", FileReportColumns[4],
160 (unsigned)File.FunctionCoverage.NumFunctions);
Ying Yie59ee432016-07-22 12:46:13 +0000161 OS << format("%*u", FileReportColumns[5],
162 (unsigned)(File.FunctionCoverage.NumFunctions -
163 File.FunctionCoverage.Executed));
Vedant Kumar5053b112016-09-06 22:46:00 +0000164 Options.colored_ostream(OS, FuncCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000165 << format("%*.2f", FileReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000166 File.FunctionCoverage.getPercentCovered())
167 << '%';
Ying Yie59ee432016-07-22 12:46:13 +0000168 OS << format("%*u", FileReportColumns[7],
Vedant Kumar016111f2016-09-19 00:38:23 +0000169 (unsigned)File.InstantiationCoverage.NumFunctions);
170 OS << format("%*u", FileReportColumns[8],
171 (unsigned)(File.InstantiationCoverage.NumFunctions -
172 File.InstantiationCoverage.Executed));
173 Options.colored_ostream(OS, InstantiationCoverageColor)
174 << format("%*.2f", FileReportColumns[9] - 1,
175 File.InstantiationCoverage.getPercentCovered())
176 << '%';
177 OS << format("%*u", FileReportColumns[10],
Ying Yie59ee432016-07-22 12:46:13 +0000178 (unsigned)File.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000179 Options.colored_ostream(OS, LineCoverageColor) << format(
Vedant Kumar016111f2016-09-19 00:38:23 +0000180 "%*u", FileReportColumns[11], (unsigned)File.LineCoverage.NotCovered);
Vedant Kumar5053b112016-09-06 22:46:00 +0000181 Options.colored_ostream(OS, LineCoverageColor)
Vedant Kumar016111f2016-09-19 00:38:23 +0000182 << format("%*.2f", FileReportColumns[12] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000183 File.LineCoverage.getPercentCovered())
184 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000185 OS << "\n";
186}
187
188void CoverageReport::render(const FunctionCoverageSummary &Function,
Vedant Kumar627887b62016-09-09 01:32:49 +0000189 raw_ostream &OS) const {
Vedant Kumar5053b112016-09-06 22:46:00 +0000190 auto FuncCoverageColor =
191 determineCoveragePercentageColor(Function.RegionCoverage);
192 auto LineCoverageColor =
193 determineCoveragePercentageColor(Function.LineCoverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000194 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000195 << format("%*u", FunctionReportColumns[1],
196 (unsigned)Function.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000197 Options.colored_ostream(OS, FuncCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000198 << format("%*u", FunctionReportColumns[2],
199 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000200 Options.colored_ostream(
201 OS, determineCoveragePercentageColor(Function.RegionCoverage))
202 << format("%*.2f", FunctionReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000203 Function.RegionCoverage.getPercentCovered())
204 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000205 OS << format("%*u", FunctionReportColumns[4],
206 (unsigned)Function.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000207 Options.colored_ostream(OS, LineCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000208 << format("%*u", FunctionReportColumns[5],
209 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000210 Options.colored_ostream(
211 OS, determineCoveragePercentageColor(Function.LineCoverage))
212 << format("%*.2f", FunctionReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000213 Function.LineCoverage.getPercentCovered())
214 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000215 OS << "\n";
216}
217
Vedant Kumarcef440f2016-06-28 16:12:18 +0000218void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000219 raw_ostream &OS) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000220 bool isFirst = true;
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000221 for (StringRef Filename : Files) {
Vedant Kumardab0ec12016-09-19 00:38:16 +0000222 auto Functions = Coverage.getCoveredFunctions(Filename);
223
Alex Lorenze82d89c2014-08-22 22:56:03 +0000224 if (isFirst)
225 isFirst = false;
226 else
227 OS << "\n";
Vedant Kumardab0ec12016-09-19 00:38:16 +0000228
229 std::vector<StringRef> Funcnames;
230 for (const auto &F : Functions)
231 Funcnames.emplace_back(F.Name);
232 adjustColumnWidths({}, Funcnames);
233
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000234 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000235 OS << column("Name", FunctionReportColumns[0])
236 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
237 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
238 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
239 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
240 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
241 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
242 OS << "\n";
243 renderDivider(FunctionReportColumns, OS);
244 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000245 FunctionCoverageSummary Totals("TOTAL");
Vedant Kumardab0ec12016-09-19 00:38:16 +0000246 for (const auto &F : Functions) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000247 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
248 ++Totals.ExecutionCount;
249 Totals.RegionCoverage += Function.RegionCoverage;
250 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000251 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000252 }
253 if (Totals.ExecutionCount) {
254 renderDivider(FunctionReportColumns, OS);
255 OS << "\n";
256 render(Totals, OS);
257 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000258 }
259}
260
Vedant Kumar627887b62016-09-09 01:32:49 +0000261std::vector<FileCoverageSummary>
262CoverageReport::prepareFileReports(FileCoverageSummary &Totals,
263 ArrayRef<StringRef> Files) const {
264 std::vector<FileCoverageSummary> FileReports;
265 unsigned LCP = 0;
266 if (Files.size() > 1)
267 LCP = getLongestCommonPrefixLen(Files);
268
269 for (StringRef Filename : Files) {
270 FileCoverageSummary Summary(Filename.drop_front(LCP));
Vedant Kumar016111f2016-09-19 00:38:23 +0000271
272 // Map source locations to aggregate function coverage summaries.
273 DenseMap<std::pair<unsigned, unsigned>, FunctionCoverageSummary> Summaries;
274
Vedant Kumar627887b62016-09-09 01:32:49 +0000275 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
276 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
Vedant Kumar016111f2016-09-19 00:38:23 +0000277 auto StartLoc = F.CountedRegions[0].startLoc();
278
279 auto UniquedSummary = Summaries.insert({StartLoc, Function});
280 if (!UniquedSummary.second)
281 UniquedSummary.first->second.update(Function);
282
283 Summary.addInstantiation(Function);
284 Totals.addInstantiation(Function);
Vedant Kumar627887b62016-09-09 01:32:49 +0000285 }
Vedant Kumar016111f2016-09-19 00:38:23 +0000286
287 for (const auto &UniquedSummary : Summaries) {
288 const FunctionCoverageSummary &FCS = UniquedSummary.second;
289 Summary.addFunction(FCS);
290 Totals.addFunction(FCS);
291 }
292
Vedant Kumar627887b62016-09-09 01:32:49 +0000293 FileReports.push_back(Summary);
294 }
295
296 return FileReports;
297}
298
299void CoverageReport::renderFileReports(raw_ostream &OS) const {
300 std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
301 renderFileReports(OS, UniqueSourceFiles);
302}
303
304void CoverageReport::renderFileReports(raw_ostream &OS,
305 ArrayRef<StringRef> Files) const {
Vedant Kumardab0ec12016-09-19 00:38:16 +0000306 FileCoverageSummary Totals("TOTAL");
307 auto FileReports = prepareFileReports(Totals, Files);
308
309 std::vector<StringRef> Filenames;
310 for (const FileCoverageSummary &FCS : FileReports)
311 Filenames.emplace_back(FCS.Name);
312 adjustColumnWidths(Filenames, {});
313
Alex Lorenze82d89c2014-08-22 22:56:03 +0000314 OS << column("Filename", FileReportColumns[0])
315 << column("Regions", FileReportColumns[1], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000316 << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000317 << column("Cover", FileReportColumns[3], Column::RightAlignment)
318 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000319 << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000320 << column("Executed", FileReportColumns[6], Column::RightAlignment)
Vedant Kumar016111f2016-09-19 00:38:23 +0000321 << column("Instantiations", FileReportColumns[7], Column::RightAlignment)
322 << column("Missed Insts.", FileReportColumns[8], Column::RightAlignment)
323 << column("Executed", FileReportColumns[9], Column::RightAlignment)
324 << column("Lines", FileReportColumns[10], Column::RightAlignment)
325 << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
326 << column("Cover", FileReportColumns[12], Column::RightAlignment) << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000327 renderDivider(FileReportColumns, OS);
328 OS << "\n";
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000329
Vedant Kumar627887b62016-09-09 01:32:49 +0000330 for (const FileCoverageSummary &FCS : FileReports)
331 render(FCS, OS);
332
Alex Lorenze82d89c2014-08-22 22:56:03 +0000333 renderDivider(FileReportColumns, OS);
334 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000335 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000336}
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000337
338} // end namespace llvm