blob: 3f730bb7bc822c0592a7f4c530800e7ffd05ce94 [file] [log] [blame]
Vedant Kumar55c8c002016-07-06 21:44:05 +00001//===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===//
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/// \file This file implements the html coverage renderer.
11///
12//===----------------------------------------------------------------------===//
13
Vedant Kumar84dc7512016-09-09 01:32:55 +000014#include "CoverageReport.h"
Vedant Kumar55c8c002016-07-06 21:44:05 +000015#include "SourceCoverageViewHTML.h"
16#include "llvm/ADT/Optional.h"
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringExtras.h"
Vedant Kumar84dc7512016-09-09 01:32:55 +000019#include "llvm/Support/Format.h"
Vedant Kumar55c8c002016-07-06 21:44:05 +000020#include "llvm/Support/Path.h"
21
22using namespace llvm;
23
24namespace {
25
Vedant Kumar6aa68be2016-07-21 23:26:15 +000026// Return a string with the special characters in \p Str escaped.
Ying Yi65787e22016-08-04 10:39:43 +000027std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000028 std::string TabExpandedResult;
Ying Yi65787e22016-08-04 10:39:43 +000029 unsigned ColNum = 0; // Record the column number.
Vedant Kumar6aa68be2016-07-21 23:26:15 +000030 for (char C : Str) {
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000031 if (C == '\t') {
32 // Replace '\t' with up to TabSize spaces.
33 unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize);
Ying Yi65787e22016-08-04 10:39:43 +000034 for (unsigned I = 0; I < NumSpaces; ++I)
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000035 TabExpandedResult += ' ';
Ying Yi65787e22016-08-04 10:39:43 +000036 ColNum += NumSpaces;
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000037 } else {
38 TabExpandedResult += C;
39 if (C == '\n' || C == '\r')
40 ColNum = 0;
41 else
42 ++ColNum;
43 }
Vedant Kumar6aa68be2016-07-21 23:26:15 +000044 }
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000045 std::string EscapedHTML;
46 {
47 raw_string_ostream OS{EscapedHTML};
Jonas Devlieghere7eeba252018-05-31 17:01:42 +000048 printHTMLEscaped(TabExpandedResult, OS);
Vedant Kumardb0f0ac2018-05-30 23:35:14 +000049 }
50 return EscapedHTML;
Vedant Kumar6aa68be2016-07-21 23:26:15 +000051}
52
53// Create a \p Name tag around \p Str, and optionally set its \p ClassName.
54std::string tag(const std::string &Name, const std::string &Str,
55 const std::string &ClassName = "") {
56 std::string Tag = "<" + Name;
Jordan Rupprechtf6ea1292018-12-20 00:57:06 +000057 if (!ClassName.empty())
Vedant Kumar6aa68be2016-07-21 23:26:15 +000058 Tag += " class='" + ClassName + "'";
59 return Tag + ">" + Str + "</" + Name + ">";
60}
61
62// Create an anchor to \p Link with the label \p Str.
63std::string a(const std::string &Link, const std::string &Str,
Vedant Kumarf13ebed2016-11-02 19:44:13 +000064 const std::string &TargetName = "") {
65 std::string Name = TargetName.empty() ? "" : ("name='" + TargetName + "' ");
66 return "<a " + Name + "href='" + Link + "'>" + Str + "</a>";
Vedant Kumar6aa68be2016-07-21 23:26:15 +000067}
68
Vedant Kumar55c8c002016-07-06 21:44:05 +000069const char *BeginHeader =
70 "<head>"
71 "<meta name='viewport' content='width=device-width,initial-scale=1'>"
72 "<meta charset='UTF-8'>";
73
74const char *CSSForCoverage =
Vedant Kumar6aa68be2016-07-21 23:26:15 +000075 R"(.red {
Vedant Kumar84dc7512016-09-09 01:32:55 +000076 background-color: #ffd0d0;
Vedant Kumar55c8c002016-07-06 21:44:05 +000077}
78.cyan {
79 background-color: cyan;
80}
Vedant Kumar55c8c002016-07-06 21:44:05 +000081body {
82 font-family: -apple-system, sans-serif;
83}
84pre {
85 margin-top: 0px !important;
86 margin-bottom: 0px !important;
87}
88.source-name-title {
89 padding: 5px 10px;
90 border-bottom: 1px solid #dbdbdb;
91 background-color: #eee;
Ying Yi1461e992016-08-24 14:27:23 +000092 line-height: 35px;
Vedant Kumar55c8c002016-07-06 21:44:05 +000093}
94.centered {
95 display: table;
Ying Yi1461e992016-08-24 14:27:23 +000096 margin-left: left;
Vedant Kumar55c8c002016-07-06 21:44:05 +000097 margin-right: auto;
98 border: 1px solid #dbdbdb;
99 border-radius: 3px;
100}
101.expansion-view {
102 background-color: rgba(0, 0, 0, 0);
103 margin-left: 0px;
104 margin-top: 5px;
105 margin-right: 5px;
106 margin-bottom: 5px;
107 border: 1px solid #dbdbdb;
108 border-radius: 3px;
109}
110table {
111 border-collapse: collapse;
112}
Vedant Kumar84dc7512016-09-09 01:32:55 +0000113.light-row {
114 background: #ffffff;
115 border: 1px solid #dbdbdb;
116}
Max Moroz430f9f72018-01-31 17:37:21 +0000117.light-row-bold {
118 background: #ffffff;
119 border: 1px solid #dbdbdb;
120 font-weight: bold;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000121}
Max Moroz430f9f72018-01-31 17:37:21 +0000122.column-entry {
123 text-align: left;
124}
125.column-entry-bold {
126 font-weight: bold;
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000127 text-align: left;
128}
Vedant Kumar84dc7512016-09-09 01:32:55 +0000129.column-entry-yellow {
Max Moroz430f9f72018-01-31 17:37:21 +0000130 text-align: left;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000131 background-color: #ffffd0;
132}
Max Moroz430f9f72018-01-31 17:37:21 +0000133.column-entry-yellow:hover {
134 background-color: #fffff0;
135}
Vedant Kumar84dc7512016-09-09 01:32:55 +0000136.column-entry-red {
Max Moroz430f9f72018-01-31 17:37:21 +0000137 text-align: left;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000138 background-color: #ffd0d0;
139}
Max Moroz430f9f72018-01-31 17:37:21 +0000140.column-entry-red:hover {
141 background-color: #fff0f0;
142}
Vedant Kumar84dc7512016-09-09 01:32:55 +0000143.column-entry-green {
Max Moroz430f9f72018-01-31 17:37:21 +0000144 text-align: left;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000145 background-color: #d0ffd0;
146}
Max Moroz430f9f72018-01-31 17:37:21 +0000147.column-entry-green:hover {
148 background-color: #f0fff0;
149}
Vedant Kumar55c8c002016-07-06 21:44:05 +0000150.line-number {
151 text-align: right;
152 color: #aaa;
153}
154.covered-line {
155 text-align: right;
156 color: #0080ff;
157}
158.uncovered-line {
159 text-align: right;
160 color: #ff3300;
161}
162.tooltip {
163 position: relative;
164 display: inline;
165 background-color: #b3e6ff;
166 text-decoration: none;
167}
168.tooltip span.tooltip-content {
169 position: absolute;
170 width: 100px;
171 margin-left: -50px;
172 color: #FFFFFF;
173 background: #000000;
174 height: 30px;
175 line-height: 30px;
176 text-align: center;
177 visibility: hidden;
178 border-radius: 6px;
179}
180.tooltip span.tooltip-content:after {
181 content: '';
182 position: absolute;
183 top: 100%;
184 left: 50%;
185 margin-left: -8px;
186 width: 0; height: 0;
187 border-top: 8px solid #000000;
188 border-right: 8px solid transparent;
189 border-left: 8px solid transparent;
190}
191:hover.tooltip span.tooltip-content {
192 visibility: visible;
193 opacity: 0.8;
194 bottom: 30px;
195 left: 50%;
196 z-index: 999;
197}
198th, td {
199 vertical-align: top;
Max Moroz430f9f72018-01-31 17:37:21 +0000200 padding: 2px 8px;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000201 border-collapse: collapse;
202 border-right: solid 1px #eee;
203 border-left: solid 1px #eee;
Max Moroz430f9f72018-01-31 17:37:21 +0000204 text-align: left;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000205}
Max Morozf880cf02018-05-22 22:40:14 +0000206td pre {
207 display: inline-block;
208}
Vedant Kumar55c8c002016-07-06 21:44:05 +0000209td:first-child {
210 border-left: none;
211}
212td:last-child {
213 border-right: none;
214}
Max Moroz430f9f72018-01-31 17:37:21 +0000215tr:hover {
216 background-color: #f0f0f0;
217}
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000218)";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000219
220const char *EndHeader = "</head>";
221
222const char *BeginCenteredDiv = "<div class='centered'>";
223
224const char *EndCenteredDiv = "</div>";
225
226const char *BeginSourceNameDiv = "<div class='source-name-title'>";
227
228const char *EndSourceNameDiv = "</div>";
229
230const char *BeginCodeTD = "<td class='code'>";
231
232const char *EndCodeTD = "</td>";
233
234const char *BeginPre = "<pre>";
235
236const char *EndPre = "</pre>";
237
238const char *BeginExpansionDiv = "<div class='expansion-view'>";
239
240const char *EndExpansionDiv = "</div>";
241
242const char *BeginTable = "<table>";
243
244const char *EndTable = "</table>";
245
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000246const char *ProjectTitleTag = "h1";
Ying Yi1461e992016-08-24 14:27:23 +0000247
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000248const char *ReportTitleTag = "h2";
Ying Yi1461e992016-08-24 14:27:23 +0000249
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000250const char *CreatedTimeTag = "h4";
Ying Yi1461e992016-08-24 14:27:23 +0000251
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000252std::string getPathToStyle(StringRef ViewPath) {
253 std::string PathToStyle = "";
254 std::string PathSep = sys::path::get_separator();
255 unsigned NumSeps = ViewPath.count(PathSep);
256 for (unsigned I = 0, E = NumSeps; I < E; ++I)
257 PathToStyle += ".." + PathSep;
258 return PathToStyle + "style.css";
259}
260
Ying Yi65787e22016-08-04 10:39:43 +0000261void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
262 const std::string &PathToStyle = "") {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000263 OS << "<!doctype html>"
264 "<html>"
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000265 << BeginHeader;
266
267 // Link to a stylesheet if one is available. Otherwise, use the default style.
268 if (PathToStyle.empty())
269 OS << "<style>" << CSSForCoverage << "</style>";
270 else
Ying Yi65787e22016-08-04 10:39:43 +0000271 OS << "<link rel='stylesheet' type='text/css' href='"
272 << escape(PathToStyle, Opts) << "'>";
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000273
Ying Yi1461e992016-08-24 14:27:23 +0000274 OS << EndHeader << "<body>";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000275}
276
277void emitEpilog(raw_ostream &OS) {
Ying Yi1461e992016-08-24 14:27:23 +0000278 OS << "</body>"
279 << "</html>";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000280}
281
Vedant Kumar55c8c002016-07-06 21:44:05 +0000282} // anonymous namespace
283
284Expected<CoveragePrinter::OwnedStream>
285CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
286 auto OSOrErr = createOutputStream(Path, "html", InToplevel);
287 if (!OSOrErr)
288 return OSOrErr;
289
290 OwnedStream OS = std::move(OSOrErr.get());
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000291
292 if (!Opts.hasOutputDirectory()) {
Ying Yi65787e22016-08-04 10:39:43 +0000293 emitPrelude(*OS.get(), Opts);
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000294 } else {
295 std::string ViewPath = getOutputPath(Path, "html", InToplevel);
Ying Yi65787e22016-08-04 10:39:43 +0000296 emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
Vedant Kumar6aa68be2016-07-21 23:26:15 +0000297 }
298
Vedant Kumar55c8c002016-07-06 21:44:05 +0000299 return std::move(OS);
300}
301
302void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
303 emitEpilog(*OS.get());
304}
305
Vedant Kumar84dc7512016-09-09 01:32:55 +0000306/// Emit column labels for the table in the index.
Eli Friedmand3822e42017-09-11 22:56:20 +0000307static void emitColumnLabelsForIndex(raw_ostream &OS,
308 const CoverageViewOptions &Opts) {
Vedant Kumar84dc7512016-09-09 01:32:55 +0000309 SmallVector<std::string, 4> Columns;
Max Moroz430f9f72018-01-31 17:37:21 +0000310 Columns.emplace_back(tag("td", "Filename", "column-entry-bold"));
311 Columns.emplace_back(tag("td", "Function Coverage", "column-entry-bold"));
Eli Friedmand3822e42017-09-11 22:56:20 +0000312 if (Opts.ShowInstantiationSummary)
Max Moroz430f9f72018-01-31 17:37:21 +0000313 Columns.emplace_back(
314 tag("td", "Instantiation Coverage", "column-entry-bold"));
315 Columns.emplace_back(tag("td", "Line Coverage", "column-entry-bold"));
Eli Friedmand3822e42017-09-11 22:56:20 +0000316 if (Opts.ShowRegionSummary)
Max Moroz430f9f72018-01-31 17:37:21 +0000317 Columns.emplace_back(tag("td", "Region Coverage", "column-entry-bold"));
Vedant Kumar84dc7512016-09-09 01:32:55 +0000318 OS << tag("tr", join(Columns.begin(), Columns.end(), ""));
319}
320
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000321std::string
322CoveragePrinterHTML::buildLinkToFile(StringRef SF,
323 const FileCoverageSummary &FCS) const {
324 SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name));
325 sys::path::remove_dots(LinkTextStr, /*remove_dot_dots=*/true);
326 sys::path::native(LinkTextStr);
327 std::string LinkText = escape(LinkTextStr, Opts);
328 std::string LinkTarget =
329 escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
330 return a(LinkTarget, LinkText);
331}
332
Vedant Kumar84dc7512016-09-09 01:32:55 +0000333/// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is
334/// false, link the summary to \p SF.
335void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
336 const FileCoverageSummary &FCS,
337 bool IsTotals) const {
Vedant Kumarc36f3152016-09-23 18:57:27 +0000338 SmallVector<std::string, 8> Columns;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000339
340 // Format a coverage triple and add the result to the list of columns.
341 auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total,
342 float Pctg) {
343 std::string S;
344 {
345 raw_string_ostream RSO{S};
Alex Lorenzc636d7a2016-11-21 14:00:04 +0000346 if (Total)
Max Moroz48c8d1e2018-01-31 22:13:07 +0000347 RSO << format("%*.2f", 7, Pctg) << "% ";
Alex Lorenzc636d7a2016-11-21 14:00:04 +0000348 else
349 RSO << "- ";
350 RSO << '(' << Hit << '/' << Total << ')';
Vedant Kumar84dc7512016-09-09 01:32:55 +0000351 }
352 const char *CellClass = "column-entry-yellow";
Alex Lorenzc636d7a2016-11-21 14:00:04 +0000353 if (Hit == Total)
Vedant Kumar84dc7512016-09-09 01:32:55 +0000354 CellClass = "column-entry-green";
Alex Lorenzc636d7a2016-11-21 14:00:04 +0000355 else if (Pctg < 80.0)
356 CellClass = "column-entry-red";
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000357 Columns.emplace_back(tag("td", tag("pre", S), CellClass));
Vedant Kumar84dc7512016-09-09 01:32:55 +0000358 };
359
360 // Simplify the display file path, and wrap it in a link if requested.
361 std::string Filename;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000362 if (IsTotals) {
Max Moroz430f9f72018-01-31 17:37:21 +0000363 Filename = SF;
Vedant Kumar84dc7512016-09-09 01:32:55 +0000364 } else {
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000365 Filename = buildLinkToFile(SF, FCS);
Vedant Kumar84dc7512016-09-09 01:32:55 +0000366 }
367
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000368 Columns.emplace_back(tag("td", tag("pre", Filename)));
Vedant Kumarc94f3922017-09-15 23:00:01 +0000369 AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(),
370 FCS.FunctionCoverage.getNumFunctions(),
Vedant Kumar84dc7512016-09-09 01:32:55 +0000371 FCS.FunctionCoverage.getPercentCovered());
Eli Friedmand3822e42017-09-11 22:56:20 +0000372 if (Opts.ShowInstantiationSummary)
Vedant Kumarc94f3922017-09-15 23:00:01 +0000373 AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(),
374 FCS.InstantiationCoverage.getNumFunctions(),
Eli Friedmand3822e42017-09-11 22:56:20 +0000375 FCS.InstantiationCoverage.getPercentCovered());
Vedant Kumarc94f3922017-09-15 23:00:01 +0000376 AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(),
377 FCS.LineCoverage.getNumLines(),
Vedant Kumar40817d82016-09-19 00:38:18 +0000378 FCS.LineCoverage.getPercentCovered());
Eli Friedmand3822e42017-09-11 22:56:20 +0000379 if (Opts.ShowRegionSummary)
Vedant Kumarc94f3922017-09-15 23:00:01 +0000380 AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(),
381 FCS.RegionCoverage.getNumRegions(),
Eli Friedmand3822e42017-09-11 22:56:20 +0000382 FCS.RegionCoverage.getPercentCovered());
Vedant Kumar84dc7512016-09-09 01:32:55 +0000383
Max Moroz430f9f72018-01-31 17:37:21 +0000384 if (IsTotals)
385 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold");
386 else
387 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row");
Vedant Kumar84dc7512016-09-09 01:32:55 +0000388}
389
390Error CoveragePrinterHTML::createIndexFile(
Vedant Kumar34ea0de2017-10-18 23:58:27 +0000391 ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
Sean Evesonb863c402017-10-03 11:05:28 +0000392 const CoverageFiltersMatchAll &Filters) {
Vedant Kumar84dc7512016-09-09 01:32:55 +0000393 // Emit the default stylesheet.
394 auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
395 if (Error E = CSSOrErr.takeError())
396 return E;
397
398 OwnedStream CSS = std::move(CSSOrErr.get());
399 CSS->operator<<(CSSForCoverage);
400
401 // Emit a file index along with some coverage statistics.
Vedant Kumar55c8c002016-07-06 21:44:05 +0000402 auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
403 if (Error E = OSOrErr.takeError())
404 return E;
405 auto OS = std::move(OSOrErr.get());
406 raw_ostream &OSRef = *OS.get();
407
Vedant Kumard12f3722016-07-22 20:49:23 +0000408 assert(Opts.hasOutputDirectory() && "No output directory for index file");
Ying Yi65787e22016-08-04 10:39:43 +0000409 emitPrelude(OSRef, Opts, getPathToStyle(""));
Vedant Kumar84dc7512016-09-09 01:32:55 +0000410
411 // Emit some basic information about the coverage report.
Ying Yi1461e992016-08-24 14:27:23 +0000412 if (Opts.hasProjectTitle())
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000413 OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts));
414 OSRef << tag(ReportTitleTag, "Coverage Report");
Ying Yi1461e992016-08-24 14:27:23 +0000415 if (Opts.hasCreatedTime())
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000416 OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts));
Vedant Kumar84dc7512016-09-09 01:32:55 +0000417
Vedant Kumar764028e2016-09-19 02:15:59 +0000418 // Emit a link to some documentation.
419 OSRef << tag("p", "Click " +
420 a("http://clang.llvm.org/docs/"
421 "SourceBasedCodeCoverage.html#interpreting-reports",
422 "here") +
423 " for information about interpreting this report.");
424
Vedant Kumar84dc7512016-09-09 01:32:55 +0000425 // Emit a table containing links to reports for each file in the covmapping.
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000426 // Exclude files which don't contain any regions.
Ying Yi1461e992016-08-24 14:27:23 +0000427 OSRef << BeginCenteredDiv << BeginTable;
Eli Friedmand3822e42017-09-11 22:56:20 +0000428 emitColumnLabelsForIndex(OSRef, Opts);
Vedant Kumar84dc7512016-09-09 01:32:55 +0000429 FileCoverageSummary Totals("TOTALS");
Sean Eveson078c2c32017-09-28 10:07:30 +0000430 auto FileReports = CoverageReport::prepareFileReports(
431 Coverage, Totals, SourceFiles, Opts, Filters);
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000432 bool EmptyFiles = false;
433 for (unsigned I = 0, E = FileReports.size(); I < E; ++I) {
Vedant Kumarc94f3922017-09-15 23:00:01 +0000434 if (FileReports[I].FunctionCoverage.getNumFunctions())
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000435 emitFileSummary(OSRef, SourceFiles[I], FileReports[I]);
436 else
437 EmptyFiles = true;
438 }
Vedant Kumar84dc7512016-09-09 01:32:55 +0000439 emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true);
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000440 OSRef << EndTable << EndCenteredDiv;
441
442 // Emit links to files which don't contain any functions. These are normally
443 // not very useful, but could be relevant for code which abuses the
444 // preprocessor.
Sean Evesonb863c402017-10-03 11:05:28 +0000445 if (EmptyFiles && Filters.empty()) {
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000446 OSRef << tag("p", "Files which contain no functions. (These "
447 "files contain code pulled into other files "
448 "by the preprocessor.)\n");
449 OSRef << BeginCenteredDiv << BeginTable;
450 for (unsigned I = 0, E = FileReports.size(); I < E; ++I)
Vedant Kumarc94f3922017-09-15 23:00:01 +0000451 if (!FileReports[I].FunctionCoverage.getNumFunctions()) {
Eli Friedman9bcc2d42017-08-09 20:43:31 +0000452 std::string Link = buildLinkToFile(SourceFiles[I], FileReports[I]);
453 OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n';
454 }
455 OSRef << EndTable << EndCenteredDiv;
456 }
457
458 OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts));
Vedant Kumar55c8c002016-07-06 21:44:05 +0000459 emitEpilog(OSRef);
460
461 return Error::success();
462}
463
464void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000465 OS << BeginCenteredDiv << BeginTable;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000466}
467
468void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
Vedant Kumare7f7e182016-09-13 23:00:13 +0000469 OS << EndTable << EndCenteredDiv;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000470}
471
Vedant Kumara8edd762016-09-10 19:37:26 +0000472void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) {
Vedant Kumar5b5c5e72016-10-25 00:08:33 +0000473 OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions()))
474 << EndSourceNameDiv;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000475}
476
477void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
478 OS << "<tr>";
479}
480
481void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
482 // If this view has sub-views, renderLine() cannot close the view's cell.
483 // Take care of it here, after all sub-views have been rendered.
484 if (hasSubViews())
485 OS << EndCodeTD;
486 OS << "</tr>";
487}
488
489void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
490 // The table-based output makes view dividers unnecessary.
491}
492
Vedant Kumard35bb382017-10-18 18:52:28 +0000493void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L,
494 const LineCoverageStats &LCS,
495 unsigned ExpansionCol, unsigned) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000496 StringRef Line = L.Line;
Vedant Kumare2017752016-07-27 21:57:15 +0000497 unsigned LineNo = L.LineNo;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000498
499 // Steps for handling text-escaping, highlighting, and tooltip creation:
500 //
501 // 1. Split the line into N+1 snippets, where N = |Segments|. The first
502 // snippet starts from Col=1 and ends at the start of the first segment.
503 // The last snippet starts at the last mapped column in the line and ends
504 // at the end of the line. Both are required but may be empty.
505
506 SmallVector<std::string, 8> Snippets;
Vedant Kumard35bb382017-10-18 18:52:28 +0000507 CoverageSegmentArray Segments = LCS.getLineSegments();
Vedant Kumar55c8c002016-07-06 21:44:05 +0000508
509 unsigned LCol = 1;
510 auto Snip = [&](unsigned Start, unsigned Len) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000511 Snippets.push_back(Line.substr(Start, Len));
512 LCol += Len;
513 };
514
515 Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
516
Vedant Kumar07e40722016-09-09 18:44:40 +0000517 for (unsigned I = 1, E = Segments.size(); I < E; ++I)
Vedant Kumar55c8c002016-07-06 21:44:05 +0000518 Snip(LCol - 1, Segments[I]->Col - LCol);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000519
520 // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
521 Snip(LCol - 1, Line.size() + 1 - LCol);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000522
523 // 2. Escape all of the snippets.
524
525 for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
Ying Yi65787e22016-08-04 10:39:43 +0000526 Snippets[I] = escape(Snippets[I], getOptions());
Vedant Kumar55c8c002016-07-06 21:44:05 +0000527
Vedant Kumare2017752016-07-27 21:57:15 +0000528 // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
529 // 1 to set the highlight for snippet 2, segment 2 to set the highlight for
530 // snippet 3, and so on.
Vedant Kumar55c8c002016-07-06 21:44:05 +0000531
Vedant Kumar55fff372017-10-18 18:52:27 +0000532 Optional<StringRef> Color;
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000533 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
Vedant Kumare2017752016-07-27 21:57:15 +0000534 auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000535 if (getOptions().Debug)
536 HighlightedRanges.emplace_back(LC, RC);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000537 return tag("span", Snippet, Color.getValue());
538 };
539
Vedant Kumarf3617562017-11-09 02:33:43 +0000540 auto CheckIfUncovered = [&](const CoverageSegment *S) {
541 return S && (!S->IsGapRegion || (Color && *Color == "red")) &&
542 S->HasCount && S->Count == 0;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000543 };
544
Vedant Kumard35bb382017-10-18 18:52:28 +0000545 if (CheckIfUncovered(LCS.getWrappedSegment())) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000546 Color = "red";
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000547 if (!Snippets[0].empty())
548 Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
Vedant Kumar55c8c002016-07-06 21:44:05 +0000549 }
550
Vedant Kumare2017752016-07-27 21:57:15 +0000551 for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000552 const auto *CurSeg = Segments[I];
Vedant Kumarf3617562017-11-09 02:33:43 +0000553 if (CheckIfUncovered(CurSeg))
Vedant Kumar55c8c002016-07-06 21:44:05 +0000554 Color = "red";
Vedant Kumarf3617562017-11-09 02:33:43 +0000555 else if (CurSeg->Col == ExpansionCol)
556 Color = "cyan";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000557 else
558 Color = None;
559
560 if (Color.hasValue())
Vedant Kumare2017752016-07-27 21:57:15 +0000561 Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col,
562 CurSeg->Col + Snippets[I + 1].size());
563 }
564
565 if (Color.hasValue() && Segments.empty())
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000566 Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
Vedant Kumare2017752016-07-27 21:57:15 +0000567
568 if (getOptions().Debug) {
569 for (const auto &Range : HighlightedRanges) {
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000570 errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
Vedant Kumare2017752016-07-27 21:57:15 +0000571 if (Range.second == 0)
572 errs() << "?";
573 else
Vedant Kumar111f4fb2016-09-08 19:18:23 +0000574 errs() << Range.second;
Vedant Kumare2017752016-07-27 21:57:15 +0000575 errs() << "\n";
576 }
Vedant Kumar55c8c002016-07-06 21:44:05 +0000577 }
578
579 // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
580 // sub-line region count tooltips if needed.
581
Vedant Kumar5a74fe72017-11-09 02:33:44 +0000582 if (shouldRenderRegionMarkers(LCS)) {
Vedant Kumar2cefdfa2017-09-08 18:44:46 +0000583 // Just consider the segments which start *and* end on this line.
584 for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000585 const auto *CurSeg = Segments[I];
Vedant Kumar2cefdfa2017-09-08 18:44:46 +0000586 if (!CurSeg->IsRegionEntry)
Vedant Kumar55c8c002016-07-06 21:44:05 +0000587 continue;
Vedant Kumar4a5c81f2017-10-18 18:52:29 +0000588 if (CurSeg->Count == LCS.getExecutionCount())
589 continue;
Vedant Kumar55c8c002016-07-06 21:44:05 +0000590
591 Snippets[I + 1] =
592 tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
Vedant Kumare2017752016-07-27 21:57:15 +0000593 "tooltip-content"),
Vedant Kumar55c8c002016-07-06 21:44:05 +0000594 "tooltip");
Vedant Kumar2cefdfa2017-09-08 18:44:46 +0000595
596 if (getOptions().Debug)
597 errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = "
598 << formatCount(CurSeg->Count) << "\n";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000599 }
600 }
601
602 OS << BeginCodeTD;
603 OS << BeginPre;
604 for (const auto &Snippet : Snippets)
605 OS << Snippet;
606 OS << EndPre;
607
608 // If there are no sub-views left to attach to this cell, end the cell.
609 // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
610 if (!hasSubViews())
611 OS << EndCodeTD;
612}
613
614void SourceCoverageViewHTML::renderLineCoverageColumn(
615 raw_ostream &OS, const LineCoverageStats &Line) {
616 std::string Count = "";
617 if (Line.isMapped())
Vedant Kumar53719452017-10-14 02:27:29 +0000618 Count = tag("pre", formatCount(Line.getExecutionCount()));
Vedant Kumar55c8c002016-07-06 21:44:05 +0000619 std::string CoverageClass =
Vedant Kumar53719452017-10-14 02:27:29 +0000620 (Line.getExecutionCount() > 0) ? "covered-line" : "uncovered-line";
Vedant Kumar55c8c002016-07-06 21:44:05 +0000621 OS << tag("td", Count, CoverageClass);
622}
623
624void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
625 unsigned LineNo) {
Vedant Kumar98c7b9a2016-07-18 17:53:16 +0000626 std::string LineNoStr = utostr(uint64_t(LineNo));
Vedant Kumarf13ebed2016-11-02 19:44:13 +0000627 std::string TargetName = "L" + LineNoStr;
628 OS << tag("td", a("#" + TargetName, tag("pre", LineNoStr), TargetName),
Vedant Kumar98c7b9a2016-07-18 17:53:16 +0000629 "line-number");
Vedant Kumar55c8c002016-07-06 21:44:05 +0000630}
631
632void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
Vedant Kumard35bb382017-10-18 18:52:28 +0000633 const LineCoverageStats &Line,
Vedant Kumar55c8c002016-07-06 21:44:05 +0000634 unsigned) {
635 // Region markers are rendered in-line using tooltips.
636}
637
Vedant Kumard35bb382017-10-18 18:52:28 +0000638void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L,
639 const LineCoverageStats &LCS,
640 unsigned ExpansionCol,
641 unsigned ViewDepth) {
Vedant Kumar55c8c002016-07-06 21:44:05 +0000642 // Render the line containing the expansion site. No extra formatting needed.
Vedant Kumard35bb382017-10-18 18:52:28 +0000643 renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000644}
645
646void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
647 ExpansionView &ESV,
648 unsigned ViewDepth) {
649 OS << BeginExpansionDiv;
650 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
Sean Eveson078c2c32017-09-28 10:07:30 +0000651 /*ShowTitle=*/false, ViewDepth + 1);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000652 OS << EndExpansionDiv;
653}
654
655void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
656 InstantiationView &ISV,
657 unsigned ViewDepth) {
658 OS << BeginExpansionDiv;
Vedant Kumar61f56942016-09-15 06:44:51 +0000659 if (!ISV.View)
660 OS << BeginSourceNameDiv
661 << tag("pre",
662 escape("Unexecuted instantiation: " + ISV.FunctionName.str(),
663 getOptions()))
664 << EndSourceNameDiv;
665 else
666 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
Sean Eveson078c2c32017-09-28 10:07:30 +0000667 /*ShowTitle=*/false, ViewDepth);
Vedant Kumar55c8c002016-07-06 21:44:05 +0000668 OS << EndExpansionDiv;
669}
Ying Yi1461e992016-08-24 14:27:23 +0000670
Vedant Kumar7551d482016-09-15 04:45:59 +0000671void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) {
Ying Yi1461e992016-08-24 14:27:23 +0000672 if (getOptions().hasProjectTitle())
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000673 OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions()));
Vedant Kumar7551d482016-09-15 04:45:59 +0000674 OS << tag(ReportTitleTag, escape(Title, getOptions()));
Ying Yi1461e992016-08-24 14:27:23 +0000675 if (getOptions().hasCreatedTime())
Vedant Kumarce19c1c2016-09-10 19:37:20 +0000676 OS << tag(CreatedTimeTag,
677 escape(getOptions().CreatedTimeStr, getOptions()));
Ying Yi1461e992016-08-24 14:27:23 +0000678}
679
680void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS,
Vedant Kumara8edd762016-09-10 19:37:26 +0000681 unsigned FirstUncoveredLineNo,
Ying Yi1461e992016-08-24 14:27:23 +0000682 unsigned ViewDepth) {
Vedant Kumara8edd762016-09-10 19:37:26 +0000683 std::string SourceLabel;
Vedant Kumar4cb35092016-09-15 06:49:13 +0000684 if (FirstUncoveredLineNo == 0) {
Vedant Kumara8edd762016-09-10 19:37:26 +0000685 SourceLabel = tag("td", tag("pre", "Source"));
686 } else {
687 std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo));
688 SourceLabel =
689 tag("td", tag("pre", "Source (" +
690 a(LinkTarget, "jump to first uncovered line") +
691 ")"));
692 }
693
Ying Yi1461e992016-08-24 14:27:23 +0000694 renderLinePrefix(OS, ViewDepth);
Vedant Kumarc063c432016-09-19 00:38:14 +0000695 OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count"))
Vedant Kumara8edd762016-09-10 19:37:26 +0000696 << SourceLabel;
Ying Yi1461e992016-08-24 14:27:23 +0000697 renderLineSuffix(OS, ViewDepth);
698}