1 //===- CoverageReport.cpp - Code coverage report -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements rendering of a code coverage report.
12 //===----------------------------------------------------------------------===//
14 #include "CoverageReport.h"
15 #include "RenderingSupport.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/Path.h"
26 /// \brief Helper struct which prints trimmed and aligned columns.
28 enum TrimKind { NoTrim, WidthTrim, RightTrim };
30 enum AlignmentKind { LeftAlignment, RightAlignment };
35 AlignmentKind Alignment;
37 Column(StringRef Str, unsigned Width)
38 : Str(Str), Width(Width), Trim(WidthTrim), Alignment(LeftAlignment) {}
40 Column &set(TrimKind Value) {
45 Column &set(AlignmentKind Value) {
50 void render(raw_ostream &OS) const {
51 if (Str.size() <= Width) {
52 if (Alignment == RightAlignment) {
53 OS.indent(Width - Str.size());
58 OS.indent(Width - Str.size());
67 OS << Str.substr(0, Width);
70 OS << Str.substr(0, Width - 3) << "...";
76 raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
81 Column column(StringRef Str, unsigned Width) { return Column(Str, Width); }
84 Column column(StringRef Str, unsigned Width, const T &Value) {
85 return Column(Str, Width).set(Value);
88 // Specify the default column widths.
89 size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10,
90 16, 16, 10, 12, 18, 10};
91 size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
93 /// \brief Adjust column widths to fit long file paths and function names.
94 void adjustColumnWidths(ArrayRef<StringRef> Files,
95 ArrayRef<StringRef> Functions) {
96 for (StringRef Filename : Files)
97 FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
98 for (StringRef Funcname : Functions)
99 FunctionReportColumns[0] =
100 std::max(FunctionReportColumns[0], Funcname.size());
103 /// \brief Prints a horizontal divider long enough to cover the given column
105 void 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)
111 /// \brief Return the color which correponds to the coverage percentage of a
113 template <typename T>
114 raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
115 if (Info.isFullyCovered())
116 return raw_ostream::GREEN;
117 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
121 /// \brief Get the number of redundant path components in each path in \p Paths.
122 unsigned getNumRedundantPathComponents(ArrayRef<std::string> Paths) {
123 // To start, set the number of redundant path components to the maximum
125 SmallVector<StringRef, 8> FirstPathComponents{sys::path::begin(Paths[0]),
126 sys::path::end(Paths[0])};
127 unsigned NumRedundant = FirstPathComponents.size();
129 for (unsigned I = 1, E = Paths.size(); NumRedundant > 0 && I < E; ++I) {
130 StringRef Path = Paths[I];
131 for (const auto &Component :
132 enumerate(make_range(sys::path::begin(Path), sys::path::end(Path)))) {
133 // Do not increase the number of redundant components: that would remove
134 // useful parts of already-visited paths.
135 if (Component.index() >= NumRedundant)
138 // Lower the number of redundant components when there's a mismatch
139 // between the first path, and the path under consideration.
140 if (FirstPathComponents[Component.index()] != Component.value()) {
141 NumRedundant = Component.index();
150 /// \brief Determine the length of the longest redundant prefix of the paths in
152 unsigned getRedundantPrefixLen(ArrayRef<std::string> Paths) {
153 // If there's at most one path, no path components are redundant.
154 if (Paths.size() <= 1)
157 unsigned PrefixLen = 0;
158 unsigned NumRedundant = getNumRedundantPathComponents(Paths);
159 auto Component = sys::path::begin(Paths[0]);
160 for (unsigned I = 0; I < NumRedundant; ++I) {
161 auto LastComponent = Component;
163 PrefixLen += Component - LastComponent;
168 } // end anonymous namespace
172 void CoverageReport::render(const FileCoverageSummary &File,
173 raw_ostream &OS) const {
174 auto FileCoverageColor =
175 determineCoveragePercentageColor(File.RegionCoverage);
176 auto FuncCoverageColor =
177 determineCoveragePercentageColor(File.FunctionCoverage);
178 auto InstantiationCoverageColor =
179 determineCoveragePercentageColor(File.InstantiationCoverage);
180 auto LineCoverageColor = determineCoveragePercentageColor(File.LineCoverage);
181 SmallString<256> FileName = File.Name;
182 sys::path::remove_dots(FileName, /*remove_dot_dots=*/true);
183 sys::path::native(FileName);
184 OS << column(FileName, FileReportColumns[0], Column::NoTrim)
185 << format("%*u", FileReportColumns[1],
186 (unsigned)File.RegionCoverage.NumRegions);
187 Options.colored_ostream(OS, FileCoverageColor) << format(
188 "%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
189 if (File.RegionCoverage.NumRegions)
190 Options.colored_ostream(OS, FileCoverageColor)
191 << format("%*.2f", FileReportColumns[3] - 1,
192 File.RegionCoverage.getPercentCovered())
195 OS << column("-", FileReportColumns[3], Column::RightAlignment);
196 OS << format("%*u", FileReportColumns[4],
197 (unsigned)File.FunctionCoverage.NumFunctions);
198 OS << format("%*u", FileReportColumns[5],
199 (unsigned)(File.FunctionCoverage.NumFunctions -
200 File.FunctionCoverage.Executed));
201 if (File.FunctionCoverage.NumFunctions)
202 Options.colored_ostream(OS, FuncCoverageColor)
203 << format("%*.2f", FileReportColumns[6] - 1,
204 File.FunctionCoverage.getPercentCovered())
207 OS << column("-", FileReportColumns[6], Column::RightAlignment);
208 OS << format("%*u", FileReportColumns[7],
209 (unsigned)File.InstantiationCoverage.NumFunctions);
210 OS << format("%*u", FileReportColumns[8],
211 (unsigned)(File.InstantiationCoverage.NumFunctions -
212 File.InstantiationCoverage.Executed));
213 if (File.InstantiationCoverage.NumFunctions)
214 Options.colored_ostream(OS, InstantiationCoverageColor)
215 << format("%*.2f", FileReportColumns[9] - 1,
216 File.InstantiationCoverage.getPercentCovered())
219 OS << column("-", FileReportColumns[9], Column::RightAlignment);
220 OS << format("%*u", FileReportColumns[10],
221 (unsigned)File.LineCoverage.NumLines);
222 Options.colored_ostream(OS, LineCoverageColor) << format(
223 "%*u", FileReportColumns[11], (unsigned)File.LineCoverage.NotCovered);
224 if (File.LineCoverage.NumLines)
225 Options.colored_ostream(OS, LineCoverageColor)
226 << format("%*.2f", FileReportColumns[12] - 1,
227 File.LineCoverage.getPercentCovered())
230 OS << column("-", FileReportColumns[12], Column::RightAlignment);
234 void CoverageReport::render(const FunctionCoverageSummary &Function,
235 const DemangleCache &DC,
236 raw_ostream &OS) const {
237 auto FuncCoverageColor =
238 determineCoveragePercentageColor(Function.RegionCoverage);
239 auto LineCoverageColor =
240 determineCoveragePercentageColor(Function.LineCoverage);
241 OS << column(DC.demangle(Function.Name), FunctionReportColumns[0],
243 << format("%*u", FunctionReportColumns[1],
244 (unsigned)Function.RegionCoverage.NumRegions);
245 Options.colored_ostream(OS, FuncCoverageColor)
246 << format("%*u", FunctionReportColumns[2],
247 (unsigned)Function.RegionCoverage.NotCovered);
248 Options.colored_ostream(
249 OS, determineCoveragePercentageColor(Function.RegionCoverage))
250 << format("%*.2f", FunctionReportColumns[3] - 1,
251 Function.RegionCoverage.getPercentCovered())
253 OS << format("%*u", FunctionReportColumns[4],
254 (unsigned)Function.LineCoverage.NumLines);
255 Options.colored_ostream(OS, LineCoverageColor)
256 << format("%*u", FunctionReportColumns[5],
257 (unsigned)Function.LineCoverage.NotCovered);
258 Options.colored_ostream(
259 OS, determineCoveragePercentageColor(Function.LineCoverage))
260 << format("%*.2f", FunctionReportColumns[6] - 1,
261 Function.LineCoverage.getPercentCovered())
266 void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
267 const DemangleCache &DC,
270 for (StringRef Filename : Files) {
271 auto Functions = Coverage.getCoveredFunctions(Filename);
278 std::vector<StringRef> Funcnames;
279 for (const auto &F : Functions)
280 Funcnames.emplace_back(DC.demangle(F.Name));
281 adjustColumnWidths({}, Funcnames);
283 OS << "File '" << Filename << "':\n";
284 OS << column("Name", FunctionReportColumns[0])
285 << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
286 << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
287 << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
288 << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
289 << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
290 << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
292 renderDivider(FunctionReportColumns, OS);
294 FunctionCoverageSummary Totals("TOTAL");
295 for (const auto &F : Functions) {
296 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
297 ++Totals.ExecutionCount;
298 Totals.RegionCoverage += Function.RegionCoverage;
299 Totals.LineCoverage += Function.LineCoverage;
300 render(Function, DC, OS);
302 if (Totals.ExecutionCount) {
303 renderDivider(FunctionReportColumns, OS);
305 render(Totals, DC, OS);
310 std::vector<FileCoverageSummary>
311 CoverageReport::prepareFileReports(const coverage::CoverageMapping &Coverage,
312 FileCoverageSummary &Totals,
313 ArrayRef<std::string> Files) {
314 std::vector<FileCoverageSummary> FileReports;
315 unsigned LCP = getRedundantPrefixLen(Files);
317 for (StringRef Filename : Files) {
318 FileCoverageSummary Summary(Filename.drop_front(LCP));
320 // Map source locations to aggregate function coverage summaries.
321 DenseMap<std::pair<unsigned, unsigned>, FunctionCoverageSummary> Summaries;
323 for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
324 FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
325 auto StartLoc = F.CountedRegions[0].startLoc();
327 auto UniquedSummary = Summaries.insert({StartLoc, Function});
328 if (!UniquedSummary.second)
329 UniquedSummary.first->second.update(Function);
331 Summary.addInstantiation(Function);
332 Totals.addInstantiation(Function);
335 for (const auto &UniquedSummary : Summaries) {
336 const FunctionCoverageSummary &FCS = UniquedSummary.second;
337 Summary.addFunction(FCS);
338 Totals.addFunction(FCS);
341 FileReports.push_back(Summary);
347 void CoverageReport::renderFileReports(raw_ostream &OS) const {
348 std::vector<std::string> UniqueSourceFiles;
349 for (StringRef SF : Coverage.getUniqueSourceFiles())
350 UniqueSourceFiles.emplace_back(SF.str());
351 renderFileReports(OS, UniqueSourceFiles);
354 void CoverageReport::renderFileReports(raw_ostream &OS,
355 ArrayRef<std::string> Files) const {
356 FileCoverageSummary Totals("TOTAL");
357 auto FileReports = prepareFileReports(Coverage, Totals, Files);
359 std::vector<StringRef> Filenames;
360 for (const FileCoverageSummary &FCS : FileReports)
361 Filenames.emplace_back(FCS.Name);
362 adjustColumnWidths(Filenames, {});
364 OS << column("Filename", FileReportColumns[0])
365 << column("Regions", FileReportColumns[1], Column::RightAlignment)
366 << column("Missed Regions", FileReportColumns[2], Column::RightAlignment)
367 << column("Cover", FileReportColumns[3], Column::RightAlignment)
368 << column("Functions", FileReportColumns[4], Column::RightAlignment)
369 << column("Missed Functions", FileReportColumns[5], Column::RightAlignment)
370 << column("Executed", FileReportColumns[6], Column::RightAlignment)
371 << column("Instantiations", FileReportColumns[7], Column::RightAlignment)
372 << column("Missed Insts.", FileReportColumns[8], Column::RightAlignment)
373 << column("Executed", FileReportColumns[9], Column::RightAlignment)
374 << column("Lines", FileReportColumns[10], Column::RightAlignment)
375 << column("Missed Lines", FileReportColumns[11], Column::RightAlignment)
376 << column("Cover", FileReportColumns[12], Column::RightAlignment) << "\n";
377 renderDivider(FileReportColumns, OS);
380 for (const FileCoverageSummary &FCS : FileReports)
383 renderDivider(FileReportColumns, OS);
388 } // end namespace llvm