blob: e83918474cba3ae6c67dcf70d15c871b0fc9cacd [file] [log] [blame]
Vedant Kumar4c010922016-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 Kumara59334d2016-09-09 01:32:55 +000014#include "CoverageReport.h"
Vedant Kumar4c010922016-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"
Ying Yi84dc9712016-08-24 14:27:23 +000019#include "llvm/Support/FileSystem.h"
Vedant Kumara59334d2016-09-09 01:32:55 +000020#include "llvm/Support/Format.h"
Vedant Kumar4c010922016-07-06 21:44:05 +000021#include "llvm/Support/Path.h"
22
23using namespace llvm;
24
25namespace {
26
Vedant Kumarc076c492016-07-21 23:26:15 +000027// Return a string with the special characters in \p Str escaped.
Ying Yi0ef31b72016-08-04 10:39:43 +000028std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
Vedant Kumarc076c492016-07-21 23:26:15 +000029 std::string Result;
Ying Yi0ef31b72016-08-04 10:39:43 +000030 unsigned ColNum = 0; // Record the column number.
Vedant Kumarc076c492016-07-21 23:26:15 +000031 for (char C : Str) {
Ying Yi0ef31b72016-08-04 10:39:43 +000032 ++ColNum;
Vedant Kumarc076c492016-07-21 23:26:15 +000033 if (C == '&')
34 Result += "&";
35 else if (C == '<')
36 Result += "&lt;";
37 else if (C == '>')
38 Result += "&gt;";
39 else if (C == '\"')
40 Result += "&quot;";
Ying Yi0ef31b72016-08-04 10:39:43 +000041 else if (C == '\n' || C == '\r') {
42 Result += C;
43 ColNum = 0;
44 } else if (C == '\t') {
45 // Replace '\t' with TabSize spaces.
46 unsigned NumSpaces = Opts.TabSize - (--ColNum % Opts.TabSize);
47 for (unsigned I = 0; I < NumSpaces; ++I)
48 Result += "&nbsp;";
49 ColNum += NumSpaces;
50 } else
Vedant Kumarc076c492016-07-21 23:26:15 +000051 Result += C;
52 }
53 return Result;
54}
55
56// Create a \p Name tag around \p Str, and optionally set its \p ClassName.
57std::string tag(const std::string &Name, const std::string &Str,
58 const std::string &ClassName = "") {
59 std::string Tag = "<" + Name;
60 if (ClassName != "")
61 Tag += " class='" + ClassName + "'";
62 return Tag + ">" + Str + "</" + Name + ">";
63}
64
65// Create an anchor to \p Link with the label \p Str.
66std::string a(const std::string &Link, const std::string &Str,
Vedant Kumar5a0e92b2016-11-02 19:44:13 +000067 const std::string &TargetName = "") {
68 std::string Name = TargetName.empty() ? "" : ("name='" + TargetName + "' ");
69 return "<a " + Name + "href='" + Link + "'>" + Str + "</a>";
Vedant Kumarc076c492016-07-21 23:26:15 +000070}
71
Vedant Kumar4c010922016-07-06 21:44:05 +000072const char *BeginHeader =
73 "<head>"
74 "<meta name='viewport' content='width=device-width,initial-scale=1'>"
75 "<meta charset='UTF-8'>";
76
77const char *CSSForCoverage =
Vedant Kumarc076c492016-07-21 23:26:15 +000078 R"(.red {
Vedant Kumara59334d2016-09-09 01:32:55 +000079 background-color: #ffd0d0;
Vedant Kumar4c010922016-07-06 21:44:05 +000080}
81.cyan {
82 background-color: cyan;
83}
Vedant Kumar4c010922016-07-06 21:44:05 +000084body {
85 font-family: -apple-system, sans-serif;
86}
87pre {
88 margin-top: 0px !important;
89 margin-bottom: 0px !important;
90}
91.source-name-title {
92 padding: 5px 10px;
93 border-bottom: 1px solid #dbdbdb;
94 background-color: #eee;
Ying Yi84dc9712016-08-24 14:27:23 +000095 line-height: 35px;
Vedant Kumar4c010922016-07-06 21:44:05 +000096}
97.centered {
98 display: table;
Ying Yi84dc9712016-08-24 14:27:23 +000099 margin-left: left;
Vedant Kumar4c010922016-07-06 21:44:05 +0000100 margin-right: auto;
101 border: 1px solid #dbdbdb;
102 border-radius: 3px;
103}
104.expansion-view {
105 background-color: rgba(0, 0, 0, 0);
106 margin-left: 0px;
107 margin-top: 5px;
108 margin-right: 5px;
109 margin-bottom: 5px;
110 border: 1px solid #dbdbdb;
111 border-radius: 3px;
112}
113table {
114 border-collapse: collapse;
115}
Vedant Kumara59334d2016-09-09 01:32:55 +0000116.light-row {
117 background: #ffffff;
118 border: 1px solid #dbdbdb;
119}
120.column-entry {
121 text-align: right;
122}
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000123.column-entry-left {
124 text-align: left;
125}
Vedant Kumara59334d2016-09-09 01:32:55 +0000126.column-entry-yellow {
127 text-align: right;
128 background-color: #ffffd0;
129}
130.column-entry-red {
131 text-align: right;
132 background-color: #ffd0d0;
133}
134.column-entry-green {
135 text-align: right;
136 background-color: #d0ffd0;
137}
Vedant Kumar4c010922016-07-06 21:44:05 +0000138.line-number {
139 text-align: right;
140 color: #aaa;
141}
142.covered-line {
143 text-align: right;
144 color: #0080ff;
145}
146.uncovered-line {
147 text-align: right;
148 color: #ff3300;
149}
150.tooltip {
151 position: relative;
152 display: inline;
153 background-color: #b3e6ff;
154 text-decoration: none;
155}
156.tooltip span.tooltip-content {
157 position: absolute;
158 width: 100px;
159 margin-left: -50px;
160 color: #FFFFFF;
161 background: #000000;
162 height: 30px;
163 line-height: 30px;
164 text-align: center;
165 visibility: hidden;
166 border-radius: 6px;
167}
168.tooltip span.tooltip-content:after {
169 content: '';
170 position: absolute;
171 top: 100%;
172 left: 50%;
173 margin-left: -8px;
174 width: 0; height: 0;
175 border-top: 8px solid #000000;
176 border-right: 8px solid transparent;
177 border-left: 8px solid transparent;
178}
179:hover.tooltip span.tooltip-content {
180 visibility: visible;
181 opacity: 0.8;
182 bottom: 30px;
183 left: 50%;
184 z-index: 999;
185}
186th, td {
187 vertical-align: top;
188 padding: 2px 5px;
189 border-collapse: collapse;
190 border-right: solid 1px #eee;
191 border-left: solid 1px #eee;
192}
193td:first-child {
194 border-left: none;
195}
196td:last-child {
197 border-right: none;
198}
Vedant Kumarc076c492016-07-21 23:26:15 +0000199)";
Vedant Kumar4c010922016-07-06 21:44:05 +0000200
201const char *EndHeader = "</head>";
202
203const char *BeginCenteredDiv = "<div class='centered'>";
204
205const char *EndCenteredDiv = "</div>";
206
207const char *BeginSourceNameDiv = "<div class='source-name-title'>";
208
209const char *EndSourceNameDiv = "</div>";
210
211const char *BeginCodeTD = "<td class='code'>";
212
213const char *EndCodeTD = "</td>";
214
215const char *BeginPre = "<pre>";
216
217const char *EndPre = "</pre>";
218
219const char *BeginExpansionDiv = "<div class='expansion-view'>";
220
221const char *EndExpansionDiv = "</div>";
222
223const char *BeginTable = "<table>";
224
225const char *EndTable = "</table>";
226
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000227const char *ProjectTitleTag = "h1";
Ying Yi84dc9712016-08-24 14:27:23 +0000228
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000229const char *ReportTitleTag = "h2";
Ying Yi84dc9712016-08-24 14:27:23 +0000230
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000231const char *CreatedTimeTag = "h4";
Ying Yi84dc9712016-08-24 14:27:23 +0000232
Vedant Kumarc076c492016-07-21 23:26:15 +0000233std::string getPathToStyle(StringRef ViewPath) {
234 std::string PathToStyle = "";
235 std::string PathSep = sys::path::get_separator();
236 unsigned NumSeps = ViewPath.count(PathSep);
237 for (unsigned I = 0, E = NumSeps; I < E; ++I)
238 PathToStyle += ".." + PathSep;
239 return PathToStyle + "style.css";
240}
241
Ying Yi0ef31b72016-08-04 10:39:43 +0000242void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
243 const std::string &PathToStyle = "") {
Vedant Kumar4c010922016-07-06 21:44:05 +0000244 OS << "<!doctype html>"
245 "<html>"
Vedant Kumarc076c492016-07-21 23:26:15 +0000246 << BeginHeader;
247
248 // Link to a stylesheet if one is available. Otherwise, use the default style.
249 if (PathToStyle.empty())
250 OS << "<style>" << CSSForCoverage << "</style>";
251 else
Ying Yi0ef31b72016-08-04 10:39:43 +0000252 OS << "<link rel='stylesheet' type='text/css' href='"
253 << escape(PathToStyle, Opts) << "'>";
Vedant Kumarc076c492016-07-21 23:26:15 +0000254
Ying Yi84dc9712016-08-24 14:27:23 +0000255 OS << EndHeader << "<body>";
Vedant Kumar4c010922016-07-06 21:44:05 +0000256}
257
258void emitEpilog(raw_ostream &OS) {
Ying Yi84dc9712016-08-24 14:27:23 +0000259 OS << "</body>"
260 << "</html>";
Vedant Kumar4c010922016-07-06 21:44:05 +0000261}
262
Vedant Kumar4c010922016-07-06 21:44:05 +0000263} // anonymous namespace
264
265Expected<CoveragePrinter::OwnedStream>
266CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
267 auto OSOrErr = createOutputStream(Path, "html", InToplevel);
268 if (!OSOrErr)
269 return OSOrErr;
270
271 OwnedStream OS = std::move(OSOrErr.get());
Vedant Kumarc076c492016-07-21 23:26:15 +0000272
273 if (!Opts.hasOutputDirectory()) {
Ying Yi0ef31b72016-08-04 10:39:43 +0000274 emitPrelude(*OS.get(), Opts);
Vedant Kumarc076c492016-07-21 23:26:15 +0000275 } else {
276 std::string ViewPath = getOutputPath(Path, "html", InToplevel);
Ying Yi0ef31b72016-08-04 10:39:43 +0000277 emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
Vedant Kumarc076c492016-07-21 23:26:15 +0000278 }
279
Vedant Kumar4c010922016-07-06 21:44:05 +0000280 return std::move(OS);
281}
282
283void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
284 emitEpilog(*OS.get());
285}
286
Vedant Kumara59334d2016-09-09 01:32:55 +0000287/// Emit column labels for the table in the index.
Eli Friedman50479f62017-09-11 22:56:20 +0000288static void emitColumnLabelsForIndex(raw_ostream &OS,
289 const CoverageViewOptions &Opts) {
Vedant Kumara59334d2016-09-09 01:32:55 +0000290 SmallVector<std::string, 4> Columns;
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000291 Columns.emplace_back(tag("td", "Filename", "column-entry-left"));
Eli Friedman50479f62017-09-11 22:56:20 +0000292 Columns.emplace_back(tag("td", "Function Coverage", "column-entry"));
293 if (Opts.ShowInstantiationSummary)
294 Columns.emplace_back(tag("td", "Instantiation Coverage", "column-entry"));
295 Columns.emplace_back(tag("td", "Line Coverage", "column-entry"));
296 if (Opts.ShowRegionSummary)
297 Columns.emplace_back(tag("td", "Region Coverage", "column-entry"));
Vedant Kumara59334d2016-09-09 01:32:55 +0000298 OS << tag("tr", join(Columns.begin(), Columns.end(), ""));
299}
300
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000301std::string
302CoveragePrinterHTML::buildLinkToFile(StringRef SF,
303 const FileCoverageSummary &FCS) const {
304 SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name));
305 sys::path::remove_dots(LinkTextStr, /*remove_dot_dots=*/true);
306 sys::path::native(LinkTextStr);
307 std::string LinkText = escape(LinkTextStr, Opts);
308 std::string LinkTarget =
309 escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
310 return a(LinkTarget, LinkText);
311}
312
Vedant Kumara59334d2016-09-09 01:32:55 +0000313/// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is
314/// false, link the summary to \p SF.
315void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
316 const FileCoverageSummary &FCS,
317 bool IsTotals) const {
Vedant Kumar224ef8d2016-09-23 18:57:27 +0000318 SmallVector<std::string, 8> Columns;
Vedant Kumara59334d2016-09-09 01:32:55 +0000319
320 // Format a coverage triple and add the result to the list of columns.
321 auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total,
322 float Pctg) {
323 std::string S;
324 {
325 raw_string_ostream RSO{S};
Alex Lorenz35369c12016-11-21 14:00:04 +0000326 if (Total)
327 RSO << format("%*.2f", 7, Pctg) << "% ";
328 else
329 RSO << "- ";
330 RSO << '(' << Hit << '/' << Total << ')';
Vedant Kumara59334d2016-09-09 01:32:55 +0000331 }
332 const char *CellClass = "column-entry-yellow";
Alex Lorenz35369c12016-11-21 14:00:04 +0000333 if (Hit == Total)
Vedant Kumara59334d2016-09-09 01:32:55 +0000334 CellClass = "column-entry-green";
Alex Lorenz35369c12016-11-21 14:00:04 +0000335 else if (Pctg < 80.0)
336 CellClass = "column-entry-red";
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000337 Columns.emplace_back(tag("td", tag("pre", S), CellClass));
Vedant Kumara59334d2016-09-09 01:32:55 +0000338 };
339
340 // Simplify the display file path, and wrap it in a link if requested.
341 std::string Filename;
Vedant Kumara59334d2016-09-09 01:32:55 +0000342 if (IsTotals) {
Vedant Kumar224ef8d2016-09-23 18:57:27 +0000343 Filename = "TOTALS";
Vedant Kumara59334d2016-09-09 01:32:55 +0000344 } else {
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000345 Filename = buildLinkToFile(SF, FCS);
Vedant Kumara59334d2016-09-09 01:32:55 +0000346 }
347
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000348 Columns.emplace_back(tag("td", tag("pre", Filename)));
Vedant Kumarc445e652017-09-15 23:00:01 +0000349 AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(),
350 FCS.FunctionCoverage.getNumFunctions(),
Vedant Kumara59334d2016-09-09 01:32:55 +0000351 FCS.FunctionCoverage.getPercentCovered());
Eli Friedman50479f62017-09-11 22:56:20 +0000352 if (Opts.ShowInstantiationSummary)
Vedant Kumarc445e652017-09-15 23:00:01 +0000353 AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(),
354 FCS.InstantiationCoverage.getNumFunctions(),
Eli Friedman50479f62017-09-11 22:56:20 +0000355 FCS.InstantiationCoverage.getPercentCovered());
Vedant Kumarc445e652017-09-15 23:00:01 +0000356 AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(),
357 FCS.LineCoverage.getNumLines(),
Vedant Kumar673ad722016-09-19 00:38:18 +0000358 FCS.LineCoverage.getPercentCovered());
Eli Friedman50479f62017-09-11 22:56:20 +0000359 if (Opts.ShowRegionSummary)
Vedant Kumarc445e652017-09-15 23:00:01 +0000360 AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(),
361 FCS.RegionCoverage.getNumRegions(),
Eli Friedman50479f62017-09-11 22:56:20 +0000362 FCS.RegionCoverage.getPercentCovered());
Vedant Kumara59334d2016-09-09 01:32:55 +0000363
364 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row");
365}
366
367Error CoveragePrinterHTML::createIndexFile(
Vedant Kumare955f612017-10-18 23:58:27 +0000368 ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
Sean Evesond932b2d2017-10-03 11:05:28 +0000369 const CoverageFiltersMatchAll &Filters) {
Vedant Kumara59334d2016-09-09 01:32:55 +0000370 // Emit the default stylesheet.
371 auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
372 if (Error E = CSSOrErr.takeError())
373 return E;
374
375 OwnedStream CSS = std::move(CSSOrErr.get());
376 CSS->operator<<(CSSForCoverage);
377
378 // Emit a file index along with some coverage statistics.
Vedant Kumar4c010922016-07-06 21:44:05 +0000379 auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
380 if (Error E = OSOrErr.takeError())
381 return E;
382 auto OS = std::move(OSOrErr.get());
383 raw_ostream &OSRef = *OS.get();
384
Vedant Kumar127d0502016-07-22 20:49:23 +0000385 assert(Opts.hasOutputDirectory() && "No output directory for index file");
Ying Yi0ef31b72016-08-04 10:39:43 +0000386 emitPrelude(OSRef, Opts, getPathToStyle(""));
Vedant Kumara59334d2016-09-09 01:32:55 +0000387
388 // Emit some basic information about the coverage report.
Ying Yi84dc9712016-08-24 14:27:23 +0000389 if (Opts.hasProjectTitle())
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000390 OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts));
391 OSRef << tag(ReportTitleTag, "Coverage Report");
Ying Yi84dc9712016-08-24 14:27:23 +0000392 if (Opts.hasCreatedTime())
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000393 OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts));
Vedant Kumara59334d2016-09-09 01:32:55 +0000394
Vedant Kumar91743f282016-09-19 02:15:59 +0000395 // Emit a link to some documentation.
396 OSRef << tag("p", "Click " +
397 a("http://clang.llvm.org/docs/"
398 "SourceBasedCodeCoverage.html#interpreting-reports",
399 "here") +
400 " for information about interpreting this report.");
401
Vedant Kumara59334d2016-09-09 01:32:55 +0000402 // Emit a table containing links to reports for each file in the covmapping.
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000403 // Exclude files which don't contain any regions.
Ying Yi84dc9712016-08-24 14:27:23 +0000404 OSRef << BeginCenteredDiv << BeginTable;
Eli Friedman50479f62017-09-11 22:56:20 +0000405 emitColumnLabelsForIndex(OSRef, Opts);
Vedant Kumara59334d2016-09-09 01:32:55 +0000406 FileCoverageSummary Totals("TOTALS");
Sean Evesonfa8ef352017-09-28 10:07:30 +0000407 auto FileReports = CoverageReport::prepareFileReports(
408 Coverage, Totals, SourceFiles, Opts, Filters);
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000409 bool EmptyFiles = false;
410 for (unsigned I = 0, E = FileReports.size(); I < E; ++I) {
Vedant Kumarc445e652017-09-15 23:00:01 +0000411 if (FileReports[I].FunctionCoverage.getNumFunctions())
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000412 emitFileSummary(OSRef, SourceFiles[I], FileReports[I]);
413 else
414 EmptyFiles = true;
415 }
Vedant Kumara59334d2016-09-09 01:32:55 +0000416 emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true);
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000417 OSRef << EndTable << EndCenteredDiv;
418
419 // Emit links to files which don't contain any functions. These are normally
420 // not very useful, but could be relevant for code which abuses the
421 // preprocessor.
Sean Evesond932b2d2017-10-03 11:05:28 +0000422 if (EmptyFiles && Filters.empty()) {
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000423 OSRef << tag("p", "Files which contain no functions. (These "
424 "files contain code pulled into other files "
425 "by the preprocessor.)\n");
426 OSRef << BeginCenteredDiv << BeginTable;
427 for (unsigned I = 0, E = FileReports.size(); I < E; ++I)
Vedant Kumarc445e652017-09-15 23:00:01 +0000428 if (!FileReports[I].FunctionCoverage.getNumFunctions()) {
Eli Friedmanc0c182c2017-08-09 20:43:31 +0000429 std::string Link = buildLinkToFile(SourceFiles[I], FileReports[I]);
430 OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n';
431 }
432 OSRef << EndTable << EndCenteredDiv;
433 }
434
435 OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts));
Vedant Kumar4c010922016-07-06 21:44:05 +0000436 emitEpilog(OSRef);
437
438 return Error::success();
439}
440
441void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000442 OS << BeginCenteredDiv << BeginTable;
Vedant Kumar4c010922016-07-06 21:44:05 +0000443}
444
445void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
Vedant Kumar84a280a2016-09-13 23:00:13 +0000446 OS << EndTable << EndCenteredDiv;
Vedant Kumar4c010922016-07-06 21:44:05 +0000447}
448
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000449void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) {
Vedant Kumar5c61c702016-10-25 00:08:33 +0000450 OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions()))
451 << EndSourceNameDiv;
Vedant Kumar4c010922016-07-06 21:44:05 +0000452}
453
454void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
455 OS << "<tr>";
456}
457
458void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
459 // If this view has sub-views, renderLine() cannot close the view's cell.
460 // Take care of it here, after all sub-views have been rendered.
461 if (hasSubViews())
462 OS << EndCodeTD;
463 OS << "</tr>";
464}
465
466void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
467 // The table-based output makes view dividers unnecessary.
468}
469
Vedant Kumar08a0a312017-10-18 18:52:28 +0000470void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L,
471 const LineCoverageStats &LCS,
472 unsigned ExpansionCol, unsigned) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000473 StringRef Line = L.Line;
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000474 unsigned LineNo = L.LineNo;
Vedant Kumar4c010922016-07-06 21:44:05 +0000475
476 // Steps for handling text-escaping, highlighting, and tooltip creation:
477 //
478 // 1. Split the line into N+1 snippets, where N = |Segments|. The first
479 // snippet starts from Col=1 and ends at the start of the first segment.
480 // The last snippet starts at the last mapped column in the line and ends
481 // at the end of the line. Both are required but may be empty.
482
483 SmallVector<std::string, 8> Snippets;
Vedant Kumar08a0a312017-10-18 18:52:28 +0000484 CoverageSegmentArray Segments = LCS.getLineSegments();
Vedant Kumar4c010922016-07-06 21:44:05 +0000485
486 unsigned LCol = 1;
487 auto Snip = [&](unsigned Start, unsigned Len) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000488 Snippets.push_back(Line.substr(Start, Len));
489 LCol += Len;
490 };
491
492 Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
493
Vedant Kumarc236e5a2016-09-09 18:44:40 +0000494 for (unsigned I = 1, E = Segments.size(); I < E; ++I)
Vedant Kumar4c010922016-07-06 21:44:05 +0000495 Snip(LCol - 1, Segments[I]->Col - LCol);
Vedant Kumar4c010922016-07-06 21:44:05 +0000496
497 // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
498 Snip(LCol - 1, Line.size() + 1 - LCol);
Vedant Kumar4c010922016-07-06 21:44:05 +0000499
500 // 2. Escape all of the snippets.
501
502 for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
Ying Yi0ef31b72016-08-04 10:39:43 +0000503 Snippets[I] = escape(Snippets[I], getOptions());
Vedant Kumar4c010922016-07-06 21:44:05 +0000504
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000505 // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
506 // 1 to set the highlight for snippet 2, segment 2 to set the highlight for
507 // snippet 3, and so on.
Vedant Kumar4c010922016-07-06 21:44:05 +0000508
Vedant Kumar988faf82017-10-18 18:52:27 +0000509 Optional<StringRef> Color;
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000510 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000511 auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000512 if (getOptions().Debug)
513 HighlightedRanges.emplace_back(LC, RC);
Vedant Kumar4c010922016-07-06 21:44:05 +0000514 return tag("span", Snippet, Color.getValue());
515 };
516
Vedant Kumare955f612017-10-18 23:58:27 +0000517 auto CheckIfUncovered = [](const CoverageSegment *S) {
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000518 return S && S->HasCount && S->Count == 0;
Vedant Kumar4c010922016-07-06 21:44:05 +0000519 };
520
Vedant Kumar08a0a312017-10-18 18:52:28 +0000521 if (CheckIfUncovered(LCS.getWrappedSegment())) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000522 Color = "red";
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000523 if (!Snippets[0].empty())
524 Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
Vedant Kumar4c010922016-07-06 21:44:05 +0000525 }
526
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000527 for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000528 const auto *CurSeg = Segments[I];
529 if (CurSeg->Col == ExpansionCol)
530 Color = "cyan";
Vedant Kumar988faf82017-10-18 18:52:27 +0000531 else if ((!CurSeg->IsGapRegion || (Color && *Color == "red")) &&
532 CheckIfUncovered(CurSeg))
Vedant Kumar4c010922016-07-06 21:44:05 +0000533 Color = "red";
534 else
535 Color = None;
536
537 if (Color.hasValue())
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000538 Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col,
539 CurSeg->Col + Snippets[I + 1].size());
540 }
541
542 if (Color.hasValue() && Segments.empty())
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000543 Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000544
545 if (getOptions().Debug) {
546 for (const auto &Range : HighlightedRanges) {
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000547 errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000548 if (Range.second == 0)
549 errs() << "?";
550 else
Vedant Kumar0b33f2c2016-09-08 19:18:23 +0000551 errs() << Range.second;
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000552 errs() << "\n";
553 }
Vedant Kumar4c010922016-07-06 21:44:05 +0000554 }
555
556 // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
557 // sub-line region count tooltips if needed.
558
Vedant Kumar933b37f2017-09-08 18:44:46 +0000559 if (shouldRenderRegionMarkers(Segments)) {
560 // Just consider the segments which start *and* end on this line.
561 for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000562 const auto *CurSeg = Segments[I];
Vedant Kumar933b37f2017-09-08 18:44:46 +0000563 if (!CurSeg->IsRegionEntry)
Vedant Kumar4c010922016-07-06 21:44:05 +0000564 continue;
Vedant Kumar9cbd33f2017-10-18 18:52:29 +0000565 if (CurSeg->Count == LCS.getExecutionCount())
566 continue;
Vedant Kumar4c010922016-07-06 21:44:05 +0000567
568 Snippets[I + 1] =
569 tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
Vedant Kumarfc07e8b2016-07-27 21:57:15 +0000570 "tooltip-content"),
Vedant Kumar4c010922016-07-06 21:44:05 +0000571 "tooltip");
Vedant Kumar933b37f2017-09-08 18:44:46 +0000572
573 if (getOptions().Debug)
574 errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = "
575 << formatCount(CurSeg->Count) << "\n";
Vedant Kumar4c010922016-07-06 21:44:05 +0000576 }
577 }
578
579 OS << BeginCodeTD;
580 OS << BeginPre;
581 for (const auto &Snippet : Snippets)
582 OS << Snippet;
583 OS << EndPre;
584
585 // If there are no sub-views left to attach to this cell, end the cell.
586 // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
587 if (!hasSubViews())
588 OS << EndCodeTD;
589}
590
591void SourceCoverageViewHTML::renderLineCoverageColumn(
592 raw_ostream &OS, const LineCoverageStats &Line) {
593 std::string Count = "";
594 if (Line.isMapped())
Vedant Kumar1963f512017-10-14 02:27:29 +0000595 Count = tag("pre", formatCount(Line.getExecutionCount()));
Vedant Kumar4c010922016-07-06 21:44:05 +0000596 std::string CoverageClass =
Vedant Kumar1963f512017-10-14 02:27:29 +0000597 (Line.getExecutionCount() > 0) ? "covered-line" : "uncovered-line";
Vedant Kumar4c010922016-07-06 21:44:05 +0000598 OS << tag("td", Count, CoverageClass);
599}
600
601void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
602 unsigned LineNo) {
Vedant Kumar2e089362016-07-18 17:53:16 +0000603 std::string LineNoStr = utostr(uint64_t(LineNo));
Vedant Kumar5a0e92b2016-11-02 19:44:13 +0000604 std::string TargetName = "L" + LineNoStr;
605 OS << tag("td", a("#" + TargetName, tag("pre", LineNoStr), TargetName),
Vedant Kumar2e089362016-07-18 17:53:16 +0000606 "line-number");
Vedant Kumar4c010922016-07-06 21:44:05 +0000607}
608
609void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
Vedant Kumar08a0a312017-10-18 18:52:28 +0000610 const LineCoverageStats &Line,
Vedant Kumar4c010922016-07-06 21:44:05 +0000611 unsigned) {
612 // Region markers are rendered in-line using tooltips.
613}
614
Vedant Kumar08a0a312017-10-18 18:52:28 +0000615void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L,
616 const LineCoverageStats &LCS,
617 unsigned ExpansionCol,
618 unsigned ViewDepth) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000619 // Render the line containing the expansion site. No extra formatting needed.
Vedant Kumar08a0a312017-10-18 18:52:28 +0000620 renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
Vedant Kumar4c010922016-07-06 21:44:05 +0000621}
622
623void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
624 ExpansionView &ESV,
625 unsigned ViewDepth) {
626 OS << BeginExpansionDiv;
627 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
Sean Evesonfa8ef352017-09-28 10:07:30 +0000628 /*ShowTitle=*/false, ViewDepth + 1);
Vedant Kumar4c010922016-07-06 21:44:05 +0000629 OS << EndExpansionDiv;
630}
631
632void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
633 InstantiationView &ISV,
634 unsigned ViewDepth) {
635 OS << BeginExpansionDiv;
Vedant Kumara8c396d2016-09-15 06:44:51 +0000636 if (!ISV.View)
637 OS << BeginSourceNameDiv
638 << tag("pre",
639 escape("Unexecuted instantiation: " + ISV.FunctionName.str(),
640 getOptions()))
641 << EndSourceNameDiv;
642 else
643 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
Sean Evesonfa8ef352017-09-28 10:07:30 +0000644 /*ShowTitle=*/false, ViewDepth);
Vedant Kumar4c010922016-07-06 21:44:05 +0000645 OS << EndExpansionDiv;
646}
Ying Yi84dc9712016-08-24 14:27:23 +0000647
Vedant Kumarb2edd112016-09-15 04:45:59 +0000648void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) {
Ying Yi84dc9712016-08-24 14:27:23 +0000649 if (getOptions().hasProjectTitle())
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000650 OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions()));
Vedant Kumarb2edd112016-09-15 04:45:59 +0000651 OS << tag(ReportTitleTag, escape(Title, getOptions()));
Ying Yi84dc9712016-08-24 14:27:23 +0000652 if (getOptions().hasCreatedTime())
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000653 OS << tag(CreatedTimeTag,
654 escape(getOptions().CreatedTimeStr, getOptions()));
Ying Yi84dc9712016-08-24 14:27:23 +0000655}
656
657void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS,
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000658 unsigned FirstUncoveredLineNo,
Ying Yi84dc9712016-08-24 14:27:23 +0000659 unsigned ViewDepth) {
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000660 std::string SourceLabel;
Vedant Kumar408866c2016-09-15 06:49:13 +0000661 if (FirstUncoveredLineNo == 0) {
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000662 SourceLabel = tag("td", tag("pre", "Source"));
663 } else {
664 std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo));
665 SourceLabel =
666 tag("td", tag("pre", "Source (" +
667 a(LinkTarget, "jump to first uncovered line") +
668 ")"));
669 }
670
Ying Yi84dc9712016-08-24 14:27:23 +0000671 renderLinePrefix(OS, ViewDepth);
Vedant Kumar98ba34e2016-09-19 00:38:14 +0000672 OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count"))
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000673 << SourceLabel;
Ying Yi84dc9712016-08-24 14:27:23 +0000674 renderLineSuffix(OS, ViewDepth);
675}