[mips][mips16] Re-work the inline assembly stubs to work with IAS. NFC.
[oota-llvm.git] / tools / llvm-cov / CoverageReport.cpp
1 //===- 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"
15 #include "RenderingSupport.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Format.h"
18
19 using namespace llvm;
20 namespace {
21 /// \brief Helper struct which prints trimmed and aligned columns.
22 struct Column {
23   enum TrimKind { NoTrim, WidthTrim, 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(WidthTrim), 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 };
47
48 raw_ostream &operator<<(raw_ostream &OS, const Column &Value) {
49   Value.render(OS);
50   return OS;
51 }
52 }
53
54 void Column::render(raw_ostream &OS) const {
55   if (Str.size() <= Width) {
56     if (Alignment == RightAlignment) {
57       OS.indent(Width - Str.size());
58       OS << Str;
59       return;
60     }
61     OS << Str;
62     OS.indent(Width - Str.size());
63     return;
64   }
65
66   switch (Trim) {
67   case NoTrim:
68     OS << Str;
69     break;
70   case WidthTrim:
71     OS << Str.substr(0, Width);
72     break;
73   case LeftTrim:
74     OS << "..." << Str.substr(Str.size() - Width + 3);
75     break;
76   case RightTrim:
77     OS << Str.substr(0, Width - 3) << "...";
78     break;
79   }
80 }
81
82 static Column column(StringRef Str, unsigned Width) {
83   return Column(Str, Width);
84 }
85
86 template <typename T>
87 static Column column(StringRef Str, unsigned Width, const T &Value) {
88   return Column(Str, Width).set(Value);
89 }
90
91 static size_t FileReportColumns[] = {25, 10, 8, 8, 10, 10};
92 static size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8};
93
94 /// \brief Prints a horizontal divider which spans across the given columns.
95 template <typename T, size_t N>
96 static void renderDivider(T (&Columns)[N], raw_ostream &OS) {
97   unsigned Length = 0;
98   for (unsigned I = 0; I < N; ++I)
99     Length += Columns[I];
100   for (unsigned I = 0; I < Length; ++I)
101     OS << '-';
102 }
103
104 /// \brief Return the color which correponds to the coverage
105 /// percentage of a certain metric.
106 template <typename T>
107 static raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
108   if (Info.isFullyCovered())
109     return raw_ostream::GREEN;
110   return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
111                                           : raw_ostream::RED;
112 }
113
114 void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
115   OS << column(File.Name, FileReportColumns[0], Column::NoTrim)
116      << format("%*u", FileReportColumns[1],
117                (unsigned)File.RegionCoverage.NumRegions);
118   Options.colored_ostream(OS, File.RegionCoverage.isFullyCovered()
119                                   ? raw_ostream::GREEN
120                                   : raw_ostream::RED)
121       << format("%*u", FileReportColumns[2], (unsigned)File.RegionCoverage.NotCovered);
122   Options.colored_ostream(OS,
123                           determineCoveragePercentageColor(File.RegionCoverage))
124       << format("%*.2f", FileReportColumns[3] - 1,
125                 File.RegionCoverage.getPercentCovered()) << '%';
126   OS << format("%*u", FileReportColumns[4],
127                (unsigned)File.FunctionCoverage.NumFunctions);
128   Options.colored_ostream(
129       OS, determineCoveragePercentageColor(File.FunctionCoverage))
130       << format("%*.2f", FileReportColumns[5] - 1,
131                 File.FunctionCoverage.getPercentCovered()) << '%';
132   OS << "\n";
133 }
134
135 void CoverageReport::render(const FunctionCoverageSummary &Function,
136                             raw_ostream &OS) {
137   OS << column(Function.Name, FunctionReportColumns[0], Column::RightTrim)
138      << format("%*u", FunctionReportColumns[1],
139                (unsigned)Function.RegionCoverage.NumRegions);
140   Options.colored_ostream(OS, Function.RegionCoverage.isFullyCovered()
141                                   ? raw_ostream::GREEN
142                                   : raw_ostream::RED)
143       << format("%*u", FunctionReportColumns[2],
144                 (unsigned)Function.RegionCoverage.NotCovered);
145   Options.colored_ostream(
146       OS, determineCoveragePercentageColor(Function.RegionCoverage))
147       << format("%*.2f", FunctionReportColumns[3] - 1,
148                 Function.RegionCoverage.getPercentCovered()) << '%';
149   OS << format("%*u", FunctionReportColumns[4],
150                (unsigned)Function.LineCoverage.NumLines);
151   Options.colored_ostream(OS, Function.LineCoverage.isFullyCovered()
152                                   ? raw_ostream::GREEN
153                                   : raw_ostream::RED)
154       << format("%*u", FunctionReportColumns[5],
155                 (unsigned)Function.LineCoverage.NotCovered);
156   Options.colored_ostream(
157       OS, determineCoveragePercentageColor(Function.LineCoverage))
158       << format("%*.2f", FunctionReportColumns[6] - 1,
159                 Function.LineCoverage.getPercentCovered()) << '%';
160   OS << "\n";
161 }
162
163 void CoverageReport::renderFunctionReports(ArrayRef<std::string> Files,
164                                            raw_ostream &OS) {
165   bool isFirst = true;
166   for (StringRef Filename : Files) {
167     if (isFirst)
168       isFirst = false;
169     else
170       OS << "\n";
171     OS << "File '" << Filename << "':\n";
172     OS << column("Name", FunctionReportColumns[0])
173        << column("Regions", FunctionReportColumns[1], Column::RightAlignment)
174        << column("Miss", FunctionReportColumns[2], Column::RightAlignment)
175        << column("Cover", FunctionReportColumns[3], Column::RightAlignment)
176        << column("Lines", FunctionReportColumns[4], Column::RightAlignment)
177        << column("Miss", FunctionReportColumns[5], Column::RightAlignment)
178        << column("Cover", FunctionReportColumns[6], Column::RightAlignment);
179     OS << "\n";
180     renderDivider(FunctionReportColumns, OS);
181     OS << "\n";
182     FunctionCoverageSummary Totals("TOTAL");
183     for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
184       FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
185       ++Totals.ExecutionCount;
186       Totals.RegionCoverage += Function.RegionCoverage;
187       Totals.LineCoverage += Function.LineCoverage;
188       render(Function, OS);
189     }
190     if (Totals.ExecutionCount) {
191       renderDivider(FunctionReportColumns, OS);
192       OS << "\n";
193       render(Totals, OS);
194     }
195   }
196 }
197
198 void CoverageReport::renderFileReports(raw_ostream &OS) {
199   // Adjust column widths to accomodate long paths and names.
200   for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
201     FileReportColumns[0] = std::max(FileReportColumns[0], Filename.size());
202     for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
203       FunctionReportColumns[0] =
204           std::max(FunctionReportColumns[0], F.Name.size());
205     }
206   }
207
208   OS << column("Filename", FileReportColumns[0])
209      << column("Regions", FileReportColumns[1], Column::RightAlignment)
210      << column("Miss", FileReportColumns[2], Column::RightAlignment)
211      << column("Cover", FileReportColumns[3], Column::RightAlignment)
212      << column("Functions", FileReportColumns[4], Column::RightAlignment)
213      << column("Executed", FileReportColumns[5], Column::RightAlignment)
214      << "\n";
215   renderDivider(FileReportColumns, OS);
216   OS << "\n";
217
218   FileCoverageSummary Totals("TOTAL");
219   for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
220     FileCoverageSummary Summary(Filename);
221     for (const auto &F : Coverage->getCoveredFunctions(Filename)) {
222       FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
223       Summary.addFunction(Function);
224       Totals.addFunction(Function);
225     }
226     render(Summary, OS);
227   }
228   renderDivider(FileReportColumns, OS);
229   OS << "\n";
230   render(Totals, OS);
231 }