blob: 648ddf89f81f6cca2b52f2c6a6eee684f4a758d9 [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 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
Alex Lorenze82d89c2014-08-22 22:56:03 +0000136void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
Vedant Kumar5053b112016-09-06 22:46:00 +0000137 auto FileCoverageColor =
138 determineCoveragePercentageColor(File.RegionCoverage);
139 auto FuncCoverageColor =
140 determineCoveragePercentageColor(File.FunctionCoverage);
141 auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000142 OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
143 << format("%*u", FileReportColumns[1],
144 (unsigned)File.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000145 Options.colored_ostream(OS, FileCoverageColor) << format(
146 "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
147 Options.colored_ostream(OS, FileCoverageColor)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000148 << format("%*.2f", FileReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000149 File.RegionCoverage.getPercentCovered())
150 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000151 OS << format("%*u", FileReportColumns[4],
152 (unsigned)File.FunctionCoverage.NumFunctions);
Ying Yie59ee432016-07-22 12:46:13 +0000153 OS << format("%*u", FileReportColumns[5],
154 (unsigned)(File.FunctionCoverage.NumFunctions -
155 File.FunctionCoverage.Executed));
Vedant Kumar5053b112016-09-06 22:46:00 +0000156 Options.colored_ostream(OS, FuncCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000157 << format("%*.2f", FileReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000158 File.FunctionCoverage.getPercentCovered())
159 << '%';
Ying Yie59ee432016-07-22 12:46:13 +0000160 OS << format("%*u", FileReportColumns[7],
161 (unsigned)File.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000162 Options.colored_ostream(OS, LineCoverageColor) << format(
163 "%*u", FileReportColumns[8], (unsigned)File.LineCoverage.NotCovered);
164 Options.colored_ostream(OS, LineCoverageColor)
Ying Yie59ee432016-07-22 12:46:13 +0000165 << format("%*.2f", FileReportColumns[9] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000166 File.LineCoverage.getPercentCovered())
167 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000168 OS << "\n";
169}
170
171void CoverageReport::render(const FunctionCoverageSummary &Function,
172 raw_ostream &OS) {
Vedant Kumar5053b112016-09-06 22:46:00 +0000173 auto FuncCoverageColor =
174 determineCoveragePercentageColor(Function.RegionCoverage);
175 auto LineCoverageColor =
176 determineCoveragePercentageColor(Function.LineCoverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000177 OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000178 << format("%*u", FunctionReportColumns[1],
179 (unsigned)Function.RegionCoverage.NumRegions);
Vedant Kumar5053b112016-09-06 22:46:00 +0000180 Options.colored_ostream(OS, FuncCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000181 << format("%*u", FunctionReportColumns[2],
182 (unsigned)Function.RegionCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000183 Options.colored_ostream(
184 OS, determineCoveragePercentageColor(Function.RegionCoverage))
185 << format("%*.2f", FunctionReportColumns[3] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000186 Function.RegionCoverage.getPercentCovered())
187 << '%';
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000188 OS << format("%*u", FunctionReportColumns[4],
189 (unsigned)Function.LineCoverage.NumLines);
Vedant Kumar5053b112016-09-06 22:46:00 +0000190 Options.colored_ostream(OS, LineCoverageColor)
NAKAMURA Takumi46d2e0e2014-10-01 00:29:26 +0000191 << format("%*u", FunctionReportColumns[5],
192 (unsigned)Function.LineCoverage.NotCovered);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000193 Options.colored_ostream(
194 OS, determineCoveragePercentageColor(Function.LineCoverage))
195 << format("%*.2f", FunctionReportColumns[6] - 1,
Vedant Kumar5053b112016-09-06 22:46:00 +0000196 Function.LineCoverage.getPercentCovered())
197 << '%';
Alex Lorenze82d89c2014-08-22 22:56:03 +0000198 OS << "\n";
199}
200
Vedant Kumarcef440f2016-06-28 16:12:18 +0000201void CoverageReport::renderFunctionReports(ArrayRef<StringRef> Files,
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000202 raw_ostream &OS) {
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000203 adjustColumnWidths(Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000204 bool isFirst = true;
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000205 for (StringRef Filename : Files) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000206 if (isFirst)
207 isFirst = false;
208 else
209 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000210 OS << "File '" << Filename << "':\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000211 OS << column("Name", FunctionReportColumns[0])
212 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
213 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
214 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
215 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
216 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
217 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
218 OS << "\n";
219 renderDivider(FunctionReportColumns, OS);
220 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000221 FunctionCoverageSummary Totals("TOTAL");
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000222 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000223 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
224 ++Totals.ExecutionCount;
225 Totals.RegionCoverage += Function.RegionCoverage;
226 Totals.LineCoverage += Function.LineCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000227 render(Function, OS);
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000228 }
229 if (Totals.ExecutionCount) {
230 renderDivider(FunctionReportColumns, OS);
231 OS << "\n";
232 render(Totals, OS);
233 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000234 }
235}
236
237void CoverageReport::renderFileReports(raw_ostream &OS) {
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000238 adjustColumnWidths(Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000239 OS << column("Filename", FileReportColumns[0])
240 << column("Regions", FileReportColumns[1], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000241 << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000242 << column("Cover", FileReportColumns[3], Column::RightAlignment)
243 << column("Functions", FileReportColumns[4], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000244 << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
Ying Yie59ee432016-07-22 12:46:13 +0000245 << column("Executed", FileReportColumns[6], Column::RightAlignment)
246 << column("Lines", FileReportColumns[7], Column::RightAlignment)
247 << column("Missed Lines", FileReportColumns[8], Column::RightAlignment)
Vedant Kumar5053b112016-09-06 22:46:00 +0000248 << column("Cover", FileReportColumns[9], Column::RightAlignment) << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000249 renderDivider(FileReportColumns, OS);
250 OS << "\n";
Vedant Kumarc3c39e72015-09-14 23:26:36 +0000251
Vedant Kumarfa754372016-09-08 00:56:43 +0000252 std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
253 unsigned LCP = 0;
254 if (UniqueSourceFiles.size() > 1)
255 LCP = getLongestCommonPrefixLen(UniqueSourceFiles);
256
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000257 FileCoverageSummary Totals("TOTAL");
Vedant Kumarfa754372016-09-08 00:56:43 +0000258 for (StringRef Filename : UniqueSourceFiles) {
259 FileCoverageSummary Summary(Filename.drop_front(LCP));
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000260 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000261 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
262 Summary.addFunction(Function);
263 Totals.addFunction(Function);
264 }
265 render(Summary, OS);
266 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000267 renderDivider(FileReportColumns, OS);
268 OS << "\n";
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000269 render(Totals, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000270}
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000271
272} // end namespace llvm