blob: 4ce642c0fb069f984b33ddab8b83cf60cb5cd791 [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 Kumar702bb9d2016-09-06 22:45:57 +000018#include <numeric>
Alex Lorenze82d89c2014-08-22 22:56:03 +000019
20using namespace llvm;
Vedant Kumar702bb9d2016-09-06 22:45:57 +000021
Alex Lorenze82d89c2014-08-22 22:56:03 +000022namespace {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000023
Alex Lorenze82d89c2014-08-22 22:56:03 +000024/// \brief Helper struct which prints trimmed and aligned columns.
25struct Column {
Vedant Kumar702bb9d2016-09-06 22:45:57 +000026 enum TrimKind { NoTrim, WidthTrim, RightTrim };
Alex Lorenze82d89c2014-08-22 22:56:03 +000027
28 enum AlignmentKind { LeftAlignment, RightAlignment };
29
30 StringRef Str;
31 unsigned Width;
32 TrimKind Trim;
33 AlignmentKind Alignment;
34
35 Column(StringRef Str, unsigned Width)
Vedant Kumarc3c39e72015-09-14 23:26:36 +000036 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +000037
38 Column &set(TrimKind Value) {
39 Trim = Value;
40 return *this;
41 }
42
43 Column &set(AlignmentKind Value) {
44 Alignment = Value;
45 return *this;
46 }
47
Vedant Kumar702bb9d2016-09-06 22:45:57 +000048 void render(raw_ostream &OS) const {
49 if (Str.size() <= Width) {
50 if (Alignment == RightAlignment) {
51 OS.indent(Width - Str.size());
52 OS << Str;
53 return;
54 }
55 OS << Str;
56 OS.indent(Width - Str.size());
57 return;
58 }
59
60 switch (Trim) {
61 case NoTrim:
62 OS << Str;
63 break;
64 case WidthTrim:
65 OS << Str.substr(0, Width);
66 break;
67 case RightTrim:
68 OS << Str.substr(0, Width - 3) << "...";
69 break;
70 }
71 }
Alex Lorenze82d89c2014-08-22 22:56:03 +000072};
Vedant Kumarc3c39e72015-09-14 23:26:36 +000073
Alex Lorenze82d89c2014-08-22 22:56:03 +000074raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
75 Value.render(OS);
76 return OS;
77}
Alex Lorenze82d89c2014-08-22 22:56:03 +000078
Vedant Kumar702bb9d2016-09-06 22:45:57 +000079Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
Alex Lorenze82d89c2014-08-22 22:56:03 +000080
81template <typename T>
Vedant Kumar702bb9d2016-09-06 22:45:57 +000082Column column(StringRef Str, unsigned Width, const T &Value) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000083 return Column(Str, Width).set(Value);
84}
85
Ying Yie59ee432016-07-22 12:46:13 +000086// Specify the default column widths.
Vedant Kumar702bb9d2016-09-06 22:45:57 +000087size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 12, 18, 10};
88size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
Alex Lorenze82d89c2014-08-22 22:56:03 +000089
Vedant Kumar702bb9d2016-09-06 22:45:57 +000090/// \brief Adjust column widths to fit long file paths and function names.
91void adjustColumnWidths(const coverage::CoverageMapping &CM) {
92 for (StringRef Filename : CM.getUniqueSourceFiles()) {
Vedant Kumaraaead332015-10-21 16:03:32 +000093 FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
Vedant Kumar702bb9d2016-09-06 22:45:57 +000094 for (const auto &F : CM.getCoveredFunctions(Filename)) {
Vedant Kumaraaead332015-10-21 16:03:32 +000095 FunctionReportColumns[0] =
96 std::max(FunctionReportColumns[0], F.Name.size());
97 }
98 }
99}
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 Kumar702bb9d2016-09-06 22:45:57 +0000119} // end anonymous namespace
120
121namespace llvm {
122
Alex Lorenze82d89c2014-08-22 22:56:03 +0000123void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
Vedant Kumar5053b112016-09-06 22:46:00 +0000124 auto FileCoverageColor =
125 determineCoveragePercentageColor(File.RegionCoverage);
126 auto FuncCoverageColor =
127 determineCoveragePercentageColor(File.FunctionCoverage);
128 auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000129 OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
130 << format("%*u", FileReportColumns[1],
131 (unsigned)File.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000132 Options.colored_ostream(OS, FileCoverageColor) << format(
133 "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
134 Options.colored_ostream(OS, FileCoverageColor)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000135 << format("%*.2f", FileReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000136 File.RegionCoverage.getPercentCovered())
137 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000138 OS << format("%*u", FileReportColumns[4],
139 (unsigned)File.FunctionCoverage.NumFunctions);
Ying Yie59ee432016-07-22 12:46:13 +0000140 OS << format("%*u", FileReportColumns[5],
141 (unsigned)(File.FunctionCoverage.NumFunctions -
142 File.FunctionCoverage.Executed));
Vedant Kumar5053b112016-09-06 22:46:00 +0000143 Options.colored_ostream(OS, FuncCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000144 << format("%*.2f", FileReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000145 File.FunctionCoverage.getPercentCovered())
146 << '%';
Ying Yie59ee432016-07-22 12:46:13 +0000147 OS << format("%*u", FileReportColumns[7],
148 (unsigned)File.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000149 Options.colored_ostream(OS, LineCoverageColor) << format(
150 "%*u", FileReportColumns[8], (unsigned)File.LineCoverage.NotCovered);
151 Options.colored_ostream(OS, LineCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000152 << format("%*.2f", FileReportColumns[9] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000153 File.LineCoverage.getPercentCovered())
154 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000155 OS << "\n";
156}
157
158void CoverageReport::render(const FunctionCoverageSummary &Function,
159 raw_ostream &OS) {
Vedant Kumar5053b112016-09-06 22:46:00 +0000160 auto FuncCoverageColor =
161 determineCoveragePercentageColor(Function.RegionCoverage);
162 auto LineCoverageColor =
163 determineCoveragePercentageColor(Function.LineCoverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000164 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000165 << format("%*u", FunctionReportColumns[1],
166 (unsigned)Function.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000167 Options.colored_ostream(OS, FuncCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000168 << format("%*u", FunctionReportColumns[2],
169 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000170 Options.colored_ostream(
171 OS, determineCoveragePercentageColor(Function.RegionCoverage))
172 << format("%*.2f", FunctionReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000173 Function.RegionCoverage.getPercentCovered())
174 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000175 OS << format("%*u", FunctionReportColumns[4],
176 (unsigned)Function.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000177 Options.colored_ostream(OS, LineCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000178 << format("%*u", FunctionReportColumns[5],
179 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000180 Options.colored_ostream(
181 OS, determineCoveragePercentageColor(Function.LineCoverage))
182 << format("%*.2f", FunctionReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000183 Function.LineCoverage.getPercentCovered())
184 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000185 OS << "\n";
186}
187
Vedant Kumarcef440f2016-06-28 16:12:18 +0000188void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000189 raw_ostream &OS) {
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000190 adjustColumnWidths(Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000191 bool isFirst = true;
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000192 for (StringRef Filename : Files) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000193 if (isFirst)
194 isFirst = false;
195 else
196 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000197 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000198 OS << column("Name", FunctionReportColumns[0])
199 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
200 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
201 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
202 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
203 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
204 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
205 OS << "\n";
206 renderDivider(FunctionReportColumns, OS);
207 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000208 FunctionCoverageSummary Totals("TOTAL");
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000209 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000210 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
211 ++Totals.ExecutionCount;
212 Totals.RegionCoverage += Function.RegionCoverage;
213 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000214 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000215 }
216 if (Totals.ExecutionCount) {
217 renderDivider(FunctionReportColumns, OS);
218 OS << "\n";
219 render(Totals, OS);
220 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000221 }
222}
223
224void CoverageReport::renderFileReports(raw_ostream &OS) {
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000225 adjustColumnWidths(Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000226 OS << column("Filename", FileReportColumns[0])
227 << column("Regions", FileReportColumns[1], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000228 << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000229 << column("Cover", FileReportColumns[3], Column::RightAlignment)
230 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000231 << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000232 << column("Executed", FileReportColumns[6], Column::RightAlignment)
233 << column("Lines", FileReportColumns[7], Column::RightAlignment)
234 << column("Missed Lines", FileReportColumns[8], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000235 << column("Cover", FileReportColumns[9], Column::RightAlignment) << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000236 renderDivider(FileReportColumns, OS);
237 OS << "\n";
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000238
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000239 FileCoverageSummary Totals("TOTAL");
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000240 for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000241 FileCoverageSummary Summary(Filename);
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000242 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000243 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
244 Summary.addFunction(Function);
245 Totals.addFunction(Function);
246 }
247 render(Summary, OS);
248 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000249 renderDivider(FileReportColumns, OS);
250 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000251 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000252}
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000253
254} // end namespace llvm