blob: db0754268c30b1fc360259023c17a1678256ddd5 [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 {
23 enum TrimKind { NoTrim, LeftTrim, RightTrim };
24
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)
33 : Str(Str), Width(Width), Trim(NoTrim), Alignment(LeftAlignment) {}
34
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};
47raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
48 Value.render(OS);
49 return OS;
50}
51}
52
53void Column::render(raw_ostream &OS) const {
54 if (Str.size() <= Width) {
55 if (Alignment == RightAlignment) {
56 OS.indent(Width - Str.size());
57 OS << Str;
58 return;
59 }
60 OS << Str;
61 OS.indent(Width - Str.size());
62 return;
63 }
64
65 switch (Trim) {
66 case NoTrim:
67 OS << Str.substr(0, Width);
68 break;
69 case LeftTrim:
70 OS << "..." << Str.substr(Str.size() - Width + 3);
71 break;
72 case RightTrim:
73 OS << Str.substr(0, Width - 3) << "...";
74 break;
75 }
76}
77
78static Column column(StringRef Str, unsigned Width) {
79 return Column(Str, Width);
80}
81
82template <typename T>
83static Column column(StringRef Str, unsigned Width, const T &Value) {
84 return Column(Str, Width).set(Value);
85}
86
Alex Lorenzcb1702d2014-09-30 12:45:13 +000087static const unsigned FileReportColumns[] = {25, 10, 8, 8, 10, 10};
Alex Lorenze82d89c2014-08-22 22:56:03 +000088static const unsigned FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
89
90/// \brief Prints a horizontal divider which spans across the given columns.
91template <typename T, size_t N>
92static void renderDivider(T (&Columns)[N], raw_ostream &OS) {
93 unsigned Length = 0;
94 for (unsigned I = 0; I < N; ++I)
95 Length += Columns[I];
96 for (unsigned I = 0; I < Length; ++I)
97 OS << '-';
98}
99
100/// \brief Return the color which correponds to the coverage
101/// percentage of a certain metric.
102template <typename T>
103static raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
104 if (Info.isFullyCovered())
105 return raw_ostream::GREEN;
106 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
107 : raw_ostream::RED;
108}
109
110void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
111 OS << column(File.Name, FileReportColumns[0], Column::LeftTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000112 << format("%*u", FileReportColumns[1], (unsigned)File.RegionCoverage.NumRegions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113 Options.colored_ostream(OS, File.RegionCoverage.isFullyCovered()
114 ? raw_ostream::GREEN
115 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000116 << format("%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000117 Options.colored_ostream(OS,
118 determineCoveragePercentageColor(File.RegionCoverage))
119 << format("%*.2f", FileReportColumns[3] - 1,
120 File.RegionCoverage.getPercentCovered()) << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000121 OS << format("%*u", FileReportColumns[4],
122 (unsigned)File.FunctionCoverage.NumFunctions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000123 Options.colored_ostream(
124 OS, determineCoveragePercentageColor(File.FunctionCoverage))
125 << format("%*.2f", FileReportColumns[5] - 1,
126 File.FunctionCoverage.getPercentCovered()) << '%';
127 OS << "\n";
128}
129
130void CoverageReport::render(const FunctionCoverageSummary &Function,
131 raw_ostream &OS) {
132 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000133 << format("%*u", FunctionReportColumns[1],
134 (unsigned)Function.RegionCoverage.NumRegions);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000135 Options.colored_ostream(OS, Function.RegionCoverage.isFullyCovered()
136 ? raw_ostream::GREEN
137 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000138 << format("%*u", FunctionReportColumns[2],
139 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000140 Options.colored_ostream(
141 OS, determineCoveragePercentageColor(Function.RegionCoverage))
142 << format("%*.2f", FunctionReportColumns[3] - 1,
143 Function.RegionCoverage.getPercentCovered()) << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000144 OS << format("%*u", FunctionReportColumns[4],
145 (unsigned)Function.LineCoverage.NumLines);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000146 Options.colored_ostream(OS, Function.LineCoverage.isFullyCovered()
147 ? raw_ostream::GREEN
148 : raw_ostream::RED)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000149 << format("%*u", FunctionReportColumns[5],
150 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000151 Options.colored_ostream(
152 OS, determineCoveragePercentageColor(Function.LineCoverage))
153 << format("%*.2f", FunctionReportColumns[6] - 1,
154 Function.LineCoverage.getPercentCovered()) << '%';
155 OS << "\n";
156}
157
158void CoverageReport::renderFunctionReports(raw_ostream &OS) {
159 bool isFirst = true;
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000160 for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000161 if (isFirst)
162 isFirst = false;
163 else
164 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000165 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000166 OS << column("Name", FunctionReportColumns[0])
167 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
168 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
169 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
170 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
171 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
172 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
173 OS << "\n";
174 renderDivider(FunctionReportColumns, OS);
175 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000176 FunctionCoverageSummary Totals("TOTAL");
177 for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
178 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
179 ++Totals.ExecutionCount;
180 Totals.RegionCoverage += Function.RegionCoverage;
181 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000182 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000183 }
184 if (Totals.ExecutionCount) {
185 renderDivider(FunctionReportColumns, OS);
186 OS << "\n";
187 render(Totals, OS);
188 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000189 }
190}
191
192void CoverageReport::renderFileReports(raw_ostream &OS) {
193 OS << column("Filename", FileReportColumns[0])
194 << column("Regions", FileReportColumns[1], Column::RightAlignment)
195 << column("Miss", FileReportColumns[2], Column::RightAlignment)
196 << column("Cover", FileReportColumns[3], Column::RightAlignment)
197 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000198 << column("Executed", FileReportColumns[5], Column::RightAlignment)
199 << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000200 renderDivider(FileReportColumns, OS);
201 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000202 FileCoverageSummary Totals("TOTAL");
203 for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
204 FileCoverageSummary Summary(Filename);
205 for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
206 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
207 Summary.addFunction(Function);
208 Totals.addFunction(Function);
209 }
210 render(Summary, OS);
211 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000212 renderDivider(FileReportColumns, OS);
213 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000214 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000215}