1 //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file This file implements the html coverage renderer.
10 ///
11 //===----------------------------------------------------------------------===//
12
13 #include "CoverageReport.h"
14 #include "SourceCoverageViewHTML.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/Path.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 // Return a string with the special characters in \p Str escaped.
escape(StringRef Str,const CoverageViewOptions & Opts)26 std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
27 std::string TabExpandedResult;
28 unsigned ColNum = 0; // Record the column number.
29 for (char C : Str) {
30 if (C == '\t') {
31 // Replace '\t' with up to TabSize spaces.
32 unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize);
33 TabExpandedResult.append(NumSpaces, ' ');
34 ColNum += NumSpaces;
35 } else {
36 TabExpandedResult += C;
37 if (C == '\n' || C == '\r')
38 ColNum = 0;
39 else
40 ++ColNum;
41 }
42 }
43 std::string EscapedHTML;
44 {
45 raw_string_ostream OS{EscapedHTML};
46 printHTMLEscaped(TabExpandedResult, OS);
47 }
48 return EscapedHTML;
49 }
50
51 // Create a \p Name tag around \p Str, and optionally set its \p ClassName.
tag(const std::string & Name,const std::string & Str,const std::string & ClassName="")52 std::string tag(const std::string &Name, const std::string &Str,
53 const std::string &ClassName = "") {
54 std::string Tag = "<" + Name;
55 if (!ClassName.empty())
56 Tag += " class='" + ClassName + "'";
57 return Tag + ">" + Str + "</" + Name + ">";
58 }
59
60 // Create an anchor to \p Link with the label \p Str.
a(const std::string & Link,const std::string & Str,const std::string & TargetName="")61 std::string a(const std::string &Link, const std::string &Str,
62 const std::string &TargetName = "") {
63 std::string Name = TargetName.empty() ? "" : ("name='" + TargetName + "' ");
64 return "<a " + Name + "href='" + Link + "'>" + Str + "</a>";
65 }
66
67 const char *BeginHeader =
68 "<head>"
69 "<meta name='viewport' content='width=device-width,initial-scale=1'>"
70 "<meta charset='UTF-8'>";
71
72 const char *CSSForCoverage =
73 R"(.red {
74 background-color: #ffd0d0;
75 }
76 .cyan {
77 background-color: cyan;
78 }
79 body {
80 font-family: -apple-system, sans-serif;
81 }
82 pre {
83 margin-top: 0px !important;
84 margin-bottom: 0px !important;
85 }
86 .source-name-title {
87 padding: 5px 10px;
88 border-bottom: 1px solid #dbdbdb;
89 background-color: #eee;
90 line-height: 35px;
91 }
92 .centered {
93 display: table;
94 margin-left: left;
95 margin-right: auto;
96 border: 1px solid #dbdbdb;
97 border-radius: 3px;
98 }
99 .expansion-view {
100 background-color: rgba(0, 0, 0, 0);
101 margin-left: 0px;
102 margin-top: 5px;
103 margin-right: 5px;
104 margin-bottom: 5px;
105 border: 1px solid #dbdbdb;
106 border-radius: 3px;
107 }
108 table {
109 border-collapse: collapse;
110 }
111 .light-row {
112 background: #ffffff;
113 border: 1px solid #dbdbdb;
114 }
115 .light-row-bold {
116 background: #ffffff;
117 border: 1px solid #dbdbdb;
118 font-weight: bold;
119 }
120 .column-entry {
121 text-align: left;
122 }
123 .column-entry-bold {
124 font-weight: bold;
125 text-align: left;
126 }
127 .column-entry-yellow {
128 text-align: left;
129 background-color: #ffffd0;
130 }
131 .column-entry-yellow:hover {
132 background-color: #fffff0;
133 }
134 .column-entry-red {
135 text-align: left;
136 background-color: #ffd0d0;
137 }
138 .column-entry-red:hover {
139 background-color: #fff0f0;
140 }
141 .column-entry-green {
142 text-align: left;
143 background-color: #d0ffd0;
144 }
145 .column-entry-green:hover {
146 background-color: #f0fff0;
147 }
148 .line-number {
149 text-align: right;
150 color: #aaa;
151 }
152 .covered-line {
153 text-align: right;
154 color: #0080ff;
155 }
156 .uncovered-line {
157 text-align: right;
158 color: #ff3300;
159 }
160 .tooltip {
161 position: relative;
162 display: inline;
163 background-color: #b3e6ff;
164 text-decoration: none;
165 }
166 .tooltip span.tooltip-content {
167 position: absolute;
168 width: 100px;
169 margin-left: -50px;
170 color: #FFFFFF;
171 background: #000000;
172 height: 30px;
173 line-height: 30px;
174 text-align: center;
175 visibility: hidden;
176 border-radius: 6px;
177 }
178 .tooltip span.tooltip-content:after {
179 content: '';
180 position: absolute;
181 top: 100%;
182 left: 50%;
183 margin-left: -8px;
184 width: 0; height: 0;
185 border-top: 8px solid #000000;
186 border-right: 8px solid transparent;
187 border-left: 8px solid transparent;
188 }
189 :hover.tooltip span.tooltip-content {
190 visibility: visible;
191 opacity: 0.8;
192 bottom: 30px;
193 left: 50%;
194 z-index: 999;
195 }
196 th, td {
197 vertical-align: top;
198 padding: 2px 8px;
199 border-collapse: collapse;
200 border-right: solid 1px #eee;
201 border-left: solid 1px #eee;
202 text-align: left;
203 }
204 td pre {
205 display: inline-block;
206 }
207 td:first-child {
208 border-left: none;
209 }
210 td:last-child {
211 border-right: none;
212 }
213 tr:hover {
214 background-color: #f0f0f0;
215 }
216 )";
217
218 const char *EndHeader = "</head>";
219
220 const char *BeginCenteredDiv = "<div class='centered'>";
221
222 const char *EndCenteredDiv = "</div>";
223
224 const char *BeginSourceNameDiv = "<div class='source-name-title'>";
225
226 const char *EndSourceNameDiv = "</div>";
227
228 const char *BeginCodeTD = "<td class='code'>";
229
230 const char *EndCodeTD = "</td>";
231
232 const char *BeginPre = "<pre>";
233
234 const char *EndPre = "</pre>";
235
236 const char *BeginExpansionDiv = "<div class='expansion-view'>";
237
238 const char *EndExpansionDiv = "</div>";
239
240 const char *BeginTable = "<table>";
241
242 const char *EndTable = "</table>";
243
244 const char *ProjectTitleTag = "h1";
245
246 const char *ReportTitleTag = "h2";
247
248 const char *CreatedTimeTag = "h4";
249
getPathToStyle(StringRef ViewPath)250 std::string getPathToStyle(StringRef ViewPath) {
251 std::string PathToStyle = "";
252 std::string PathSep = std::string(sys::path::get_separator());
253 unsigned NumSeps = ViewPath.count(PathSep);
254 for (unsigned I = 0, E = NumSeps; I < E; ++I)
255 PathToStyle += ".." + PathSep;
256 return PathToStyle + "style.css";
257 }
258
emitPrelude(raw_ostream & OS,const CoverageViewOptions & Opts,const std::string & PathToStyle="")259 void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
260 const std::string &PathToStyle = "") {
261 OS << "<!doctype html>"
262 "<html>"
263 << BeginHeader;
264
265 // Link to a stylesheet if one is available. Otherwise, use the default style.
266 if (PathToStyle.empty())
267 OS << "<style>" << CSSForCoverage << "</style>";
268 else
269 OS << "<link rel='stylesheet' type='text/css' href='"
270 << escape(PathToStyle, Opts) << "'>";
271
272 OS << EndHeader << "<body>";
273 }
274
emitEpilog(raw_ostream & OS)275 void emitEpilog(raw_ostream &OS) {
276 OS << "</body>"
277 << "</html>";
278 }
279
280 } // anonymous namespace
281
282 Expected<CoveragePrinter::OwnedStream>
createViewFile(StringRef Path,bool InToplevel)283 CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
284 auto OSOrErr = createOutputStream(Path, "html", InToplevel);
285 if (!OSOrErr)
286 return OSOrErr;
287
288 OwnedStream OS = std::move(OSOrErr.get());
289
290 if (!Opts.hasOutputDirectory()) {
291 emitPrelude(*OS.get(), Opts);
292 } else {
293 std::string ViewPath = getOutputPath(Path, "html", InToplevel);
294 emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
295 }
296
297 return std::move(OS);
298 }
299
closeViewFile(OwnedStream OS)300 void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
301 emitEpilog(*OS.get());
302 }
303
304 /// Emit column labels for the table in the index.
emitColumnLabelsForIndex(raw_ostream & OS,const CoverageViewOptions & Opts)305 static void emitColumnLabelsForIndex(raw_ostream &OS,
306 const CoverageViewOptions &Opts) {
307 SmallVector<std::string, 4> Columns;
308 Columns.emplace_back(tag("td", "Filename", "column-entry-bold"));
309 Columns.emplace_back(tag("td", "Function Coverage", "column-entry-bold"));
310 if (Opts.ShowInstantiationSummary)
311 Columns.emplace_back(
312 tag("td", "Instantiation Coverage", "column-entry-bold"));
313 Columns.emplace_back(tag("td", "Line Coverage", "column-entry-bold"));
314 if (Opts.ShowRegionSummary)
315 Columns.emplace_back(tag("td", "Region Coverage", "column-entry-bold"));
316 OS << tag("tr", join(Columns.begin(), Columns.end(), ""));
317 }
318
319 std::string
buildLinkToFile(StringRef SF,const FileCoverageSummary & FCS) const320 CoveragePrinterHTML::buildLinkToFile(StringRef SF,
321 const FileCoverageSummary &FCS) const {
322 SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name));
323 sys::path::remove_dots(LinkTextStr, /*remove_dot_dots=*/true);
324 sys::path::native(LinkTextStr);
325 std::string LinkText = escape(LinkTextStr, Opts);
326 std::string LinkTarget =
327 escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
328 return a(LinkTarget, LinkText);
329 }
330
331 /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is
332 /// false, link the summary to \p SF.
emitFileSummary(raw_ostream & OS,StringRef SF,const FileCoverageSummary & FCS,bool IsTotals) const333 void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
334 const FileCoverageSummary &FCS,
335 bool IsTotals) const {
336 SmallVector<std::string, 8> Columns;
337
338 // Format a coverage triple and add the result to the list of columns.
339 auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total,
340 float Pctg) {
341 std::string S;
342 {
343 raw_string_ostream RSO{S};
344 if (Total)
345 RSO << format("%*.2f", 7, Pctg) << "% ";
346 else
347 RSO << "- ";
348 RSO << '(' << Hit << '/' << Total << ')';
349 }
350 const char *CellClass = "column-entry-yellow";
351 if (Hit == Total)
352 CellClass = "column-entry-green";
353 else if (Pctg < 80.0)
354 CellClass = "column-entry-red";
355 Columns.emplace_back(tag("td", tag("pre", S), CellClass));
356 };
357
358 // Simplify the display file path, and wrap it in a link if requested.
359 std::string Filename;
360 if (IsTotals) {
361 Filename = std::string(SF);
362 } else {
363 Filename = buildLinkToFile(SF, FCS);
364 }
365
366 Columns.emplace_back(tag("td", tag("pre", Filename)));
367 AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(),
368 FCS.FunctionCoverage.getNumFunctions(),
369 FCS.FunctionCoverage.getPercentCovered());
370 if (Opts.ShowInstantiationSummary)
371 AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(),
372 FCS.InstantiationCoverage.getNumFunctions(),
373 FCS.InstantiationCoverage.getPercentCovered());
374 AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(),
375 FCS.LineCoverage.getNumLines(),
376 FCS.LineCoverage.getPercentCovered());
377 if (Opts.ShowRegionSummary)
378 AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(),
379 FCS.RegionCoverage.getNumRegions(),
380 FCS.RegionCoverage.getPercentCovered());
381
382 if (IsTotals)
383 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold");
384 else
385 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row");
386 }
387
createIndexFile(ArrayRef<std::string> SourceFiles,const CoverageMapping & Coverage,const CoverageFiltersMatchAll & Filters)388 Error CoveragePrinterHTML::createIndexFile(
389 ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
390 const CoverageFiltersMatchAll &Filters) {
391 // Emit the default stylesheet.
392 auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
393 if (Error E = CSSOrErr.takeError())
394 return E;
395
396 OwnedStream CSS = std::move(CSSOrErr.get());
397 CSS->operator<<(CSSForCoverage);
398
399 // Emit a file index along with some coverage statistics.
400 auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
401 if (Error E = OSOrErr.takeError())
402 return E;
403 auto OS = std::move(OSOrErr.get());
404 raw_ostream &OSRef = *OS.get();
405
406 assert(Opts.hasOutputDirectory() && "No output directory for index file");
407 emitPrelude(OSRef, Opts, getPathToStyle(""));
408
409 // Emit some basic information about the coverage report.
410 if (Opts.hasProjectTitle())
411 OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts));
412 OSRef << tag(ReportTitleTag, "Coverage Report");
413 if (Opts.hasCreatedTime())
414 OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts));
415
416 // Emit a link to some documentation.
417 OSRef << tag("p", "Click " +
418 a("http://clang.llvm.org/docs/"
419 "SourceBasedCodeCoverage.html#interpreting-reports",
420 "here") +
421 " for information about interpreting this report.");
422
423 // Emit a table containing links to reports for each file in the covmapping.
424 // Exclude files which don't contain any regions.
425 OSRef << BeginCenteredDiv << BeginTable;
426 emitColumnLabelsForIndex(OSRef, Opts);
427 FileCoverageSummary Totals("TOTALS");
428 auto FileReports = CoverageReport::prepareFileReports(
429 Coverage, Totals, SourceFiles, Opts, Filters);
430 bool EmptyFiles = false;
431 for (unsigned I = 0, E = FileReports.size(); I < E; ++I) {
432 if (FileReports[I].FunctionCoverage.getNumFunctions())
433 emitFileSummary(OSRef, SourceFiles[I], FileReports[I]);
434 else
435 EmptyFiles = true;
436 }
437 emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true);
438 OSRef << EndTable << EndCenteredDiv;
439
440 // Emit links to files which don't contain any functions. These are normally
441 // not very useful, but could be relevant for code which abuses the
442 // preprocessor.
443 if (EmptyFiles && Filters.empty()) {
444 OSRef << tag("p", "Files which contain no functions. (These "
445 "files contain code pulled into other files "
446 "by the preprocessor.)\n");
447 OSRef << BeginCenteredDiv << BeginTable;
448 for (unsigned I = 0, E = FileReports.size(); I < E; ++I)
449 if (!FileReports[I].FunctionCoverage.getNumFunctions()) {
450 std::string Link = buildLinkToFile(SourceFiles[I], FileReports[I]);
451 OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n';
452 }
453 OSRef << EndTable << EndCenteredDiv;
454 }
455
456 OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts));
457 emitEpilog(OSRef);
458
459 return Error::success();
460 }
461
renderViewHeader(raw_ostream & OS)462 void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
463 OS << BeginCenteredDiv << BeginTable;
464 }
465
renderViewFooter(raw_ostream & OS)466 void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
467 OS << EndTable << EndCenteredDiv;
468 }
469
renderSourceName(raw_ostream & OS,bool WholeFile)470 void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) {
471 OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions()))
472 << EndSourceNameDiv;
473 }
474
renderLinePrefix(raw_ostream & OS,unsigned)475 void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
476 OS << "<tr>";
477 }
478
renderLineSuffix(raw_ostream & OS,unsigned)479 void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
480 // If this view has sub-views, renderLine() cannot close the view's cell.
481 // Take care of it here, after all sub-views have been rendered.
482 if (hasSubViews())
483 OS << EndCodeTD;
484 OS << "</tr>";
485 }
486
renderViewDivider(raw_ostream &,unsigned)487 void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
488 // The table-based output makes view dividers unnecessary.
489 }
490
renderLine(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned)491 void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L,
492 const LineCoverageStats &LCS,
493 unsigned ExpansionCol, unsigned) {
494 StringRef Line = L.Line;
495 unsigned LineNo = L.LineNo;
496
497 // Steps for handling text-escaping, highlighting, and tooltip creation:
498 //
499 // 1. Split the line into N+1 snippets, where N = |Segments|. The first
500 // snippet starts from Col=1 and ends at the start of the first segment.
501 // The last snippet starts at the last mapped column in the line and ends
502 // at the end of the line. Both are required but may be empty.
503
504 SmallVector<std::string, 8> Snippets;
505 CoverageSegmentArray Segments = LCS.getLineSegments();
506
507 unsigned LCol = 1;
508 auto Snip = [&](unsigned Start, unsigned Len) {
509 Snippets.push_back(std::string(Line.substr(Start, Len)));
510 LCol += Len;
511 };
512
513 Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
514
515 for (unsigned I = 1, E = Segments.size(); I < E; ++I)
516 Snip(LCol - 1, Segments[I]->Col - LCol);
517
518 // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
519 Snip(LCol - 1, Line.size() + 1 - LCol);
520
521 // 2. Escape all of the snippets.
522
523 for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
524 Snippets[I] = escape(Snippets[I], getOptions());
525
526 // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
527 // 1 to set the highlight for snippet 2, segment 2 to set the highlight for
528 // snippet 3, and so on.
529
530 Optional<StringRef> Color;
531 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
532 auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
533 if (getOptions().Debug)
534 HighlightedRanges.emplace_back(LC, RC);
535 return tag("span", Snippet, std::string(Color.getValue()));
536 };
537
538 auto CheckIfUncovered = [&](const CoverageSegment *S) {
539 return S && (!S->IsGapRegion || (Color && *Color == "red")) &&
540 S->HasCount && S->Count == 0;
541 };
542
543 if (CheckIfUncovered(LCS.getWrappedSegment())) {
544 Color = "red";
545 if (!Snippets[0].empty())
546 Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
547 }
548
549 for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
550 const auto *CurSeg = Segments[I];
551 if (CheckIfUncovered(CurSeg))
552 Color = "red";
553 else if (CurSeg->Col == ExpansionCol)
554 Color = "cyan";
555 else
556 Color = None;
557
558 if (Color.hasValue())
559 Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col,
560 CurSeg->Col + Snippets[I + 1].size());
561 }
562
563 if (Color.hasValue() && Segments.empty())
564 Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
565
566 if (getOptions().Debug) {
567 for (const auto &Range : HighlightedRanges) {
568 errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
569 if (Range.second == 0)
570 errs() << "?";
571 else
572 errs() << Range.second;
573 errs() << "\n";
574 }
575 }
576
577 // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
578 // sub-line region count tooltips if needed.
579
580 if (shouldRenderRegionMarkers(LCS)) {
581 // Just consider the segments which start *and* end on this line.
582 for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
583 const auto *CurSeg = Segments[I];
584 if (!CurSeg->IsRegionEntry)
585 continue;
586 if (CurSeg->Count == LCS.getExecutionCount())
587 continue;
588
589 Snippets[I + 1] =
590 tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
591 "tooltip-content"),
592 "tooltip");
593
594 if (getOptions().Debug)
595 errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = "
596 << formatCount(CurSeg->Count) << "\n";
597 }
598 }
599
600 OS << BeginCodeTD;
601 OS << BeginPre;
602 for (const auto &Snippet : Snippets)
603 OS << Snippet;
604 OS << EndPre;
605
606 // If there are no sub-views left to attach to this cell, end the cell.
607 // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
608 if (!hasSubViews())
609 OS << EndCodeTD;
610 }
611
renderLineCoverageColumn(raw_ostream & OS,const LineCoverageStats & Line)612 void SourceCoverageViewHTML::renderLineCoverageColumn(
613 raw_ostream &OS, const LineCoverageStats &Line) {
614 std::string Count = "";
615 if (Line.isMapped())
616 Count = tag("pre", formatCount(Line.getExecutionCount()));
617 std::string CoverageClass =
618 (Line.getExecutionCount() > 0) ? "covered-line" : "uncovered-line";
619 OS << tag("td", Count, CoverageClass);
620 }
621
renderLineNumberColumn(raw_ostream & OS,unsigned LineNo)622 void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
623 unsigned LineNo) {
624 std::string LineNoStr = utostr(uint64_t(LineNo));
625 std::string TargetName = "L" + LineNoStr;
626 OS << tag("td", a("#" + TargetName, tag("pre", LineNoStr), TargetName),
627 "line-number");
628 }
629
renderRegionMarkers(raw_ostream &,const LineCoverageStats & Line,unsigned)630 void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
631 const LineCoverageStats &Line,
632 unsigned) {
633 // Region markers are rendered in-line using tooltips.
634 }
635
renderExpansionSite(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned ViewDepth)636 void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L,
637 const LineCoverageStats &LCS,
638 unsigned ExpansionCol,
639 unsigned ViewDepth) {
640 // Render the line containing the expansion site. No extra formatting needed.
641 renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
642 }
643
renderExpansionView(raw_ostream & OS,ExpansionView & ESV,unsigned ViewDepth)644 void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
645 ExpansionView &ESV,
646 unsigned ViewDepth) {
647 OS << BeginExpansionDiv;
648 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
649 /*ShowTitle=*/false, ViewDepth + 1);
650 OS << EndExpansionDiv;
651 }
652
renderInstantiationView(raw_ostream & OS,InstantiationView & ISV,unsigned ViewDepth)653 void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
654 InstantiationView &ISV,
655 unsigned ViewDepth) {
656 OS << BeginExpansionDiv;
657 if (!ISV.View)
658 OS << BeginSourceNameDiv
659 << tag("pre",
660 escape("Unexecuted instantiation: " + ISV.FunctionName.str(),
661 getOptions()))
662 << EndSourceNameDiv;
663 else
664 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
665 /*ShowTitle=*/false, ViewDepth);
666 OS << EndExpansionDiv;
667 }
668
renderTitle(raw_ostream & OS,StringRef Title)669 void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) {
670 if (getOptions().hasProjectTitle())
671 OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions()));
672 OS << tag(ReportTitleTag, escape(Title, getOptions()));
673 if (getOptions().hasCreatedTime())
674 OS << tag(CreatedTimeTag,
675 escape(getOptions().CreatedTimeStr, getOptions()));
676 }
677
renderTableHeader(raw_ostream & OS,unsigned FirstUncoveredLineNo,unsigned ViewDepth)678 void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS,
679 unsigned FirstUncoveredLineNo,
680 unsigned ViewDepth) {
681 std::string SourceLabel;
682 if (FirstUncoveredLineNo == 0) {
683 SourceLabel = tag("td", tag("pre", "Source"));
684 } else {
685 std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo));
686 SourceLabel =
687 tag("td", tag("pre", "Source (" +
688 a(LinkTarget, "jump to first uncovered line") +
689 ")"));
690 }
691
692 renderLinePrefix(OS, ViewDepth);
693 OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count"))
694 << SourceLabel;
695 renderLineSuffix(OS, ViewDepth);
696 }
697