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/SmallString.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/Support/Format.h" 18 #include "llvm/Support/Path.h" 19 #include <optional> 20 21 using namespace llvm; 22 23 namespace { 24 25 // Return a string with the special characters in \p Str escaped. 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. 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. 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 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 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 275 void emitEpilog(raw_ostream &OS) { 276 OS << "</body>" 277 << "</html>"; 278 } 279 280 } // anonymous namespace 281 282 Expected<CoveragePrinter::OwnedStream> 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 300 void CoveragePrinterHTML::closeViewFile(OwnedStream OS) { 301 emitEpilog(*OS.get()); 302 } 303 304 /// Emit column labels for the table in the index. 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 if (Opts.ShowBranchSummary) 317 Columns.emplace_back(tag("td", "Branch Coverage", "column-entry-bold")); 318 OS << tag("tr", join(Columns.begin(), Columns.end(), "")); 319 } 320 321 std::string 322 CoveragePrinterHTML::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_dot=*/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 333 /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is 334 /// false, link the summary to \p SF. 335 void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF, 336 const FileCoverageSummary &FCS, 337 bool IsTotals) const { 338 SmallVector<std::string, 8> Columns; 339 340 // Format a coverage triple and add the result to the list of columns. 341 auto AddCoverageTripleToColumn = 342 [&Columns, this](unsigned Hit, unsigned Total, float Pctg) { 343 std::string S; 344 { 345 raw_string_ostream RSO{S}; 346 if (Total) 347 RSO << format("%*.2f", 7, Pctg) << "% "; 348 else 349 RSO << "- "; 350 RSO << '(' << Hit << '/' << Total << ')'; 351 } 352 const char *CellClass = "column-entry-yellow"; 353 if (Pctg >= Opts.HighCovWatermark) 354 CellClass = "column-entry-green"; 355 else if (Pctg < Opts.LowCovWatermark) 356 CellClass = "column-entry-red"; 357 Columns.emplace_back(tag("td", tag("pre", S), CellClass)); 358 }; 359 360 // Simplify the display file path, and wrap it in a link if requested. 361 std::string Filename; 362 if (IsTotals) { 363 Filename = std::string(SF); 364 } else { 365 Filename = buildLinkToFile(SF, FCS); 366 } 367 368 Columns.emplace_back(tag("td", tag("pre", Filename))); 369 AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(), 370 FCS.FunctionCoverage.getNumFunctions(), 371 FCS.FunctionCoverage.getPercentCovered()); 372 if (Opts.ShowInstantiationSummary) 373 AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(), 374 FCS.InstantiationCoverage.getNumFunctions(), 375 FCS.InstantiationCoverage.getPercentCovered()); 376 AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(), 377 FCS.LineCoverage.getNumLines(), 378 FCS.LineCoverage.getPercentCovered()); 379 if (Opts.ShowRegionSummary) 380 AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(), 381 FCS.RegionCoverage.getNumRegions(), 382 FCS.RegionCoverage.getPercentCovered()); 383 if (Opts.ShowBranchSummary) 384 AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(), 385 FCS.BranchCoverage.getNumBranches(), 386 FCS.BranchCoverage.getPercentCovered()); 387 388 if (IsTotals) 389 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold"); 390 else 391 OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row"); 392 } 393 394 Error CoveragePrinterHTML::createIndexFile( 395 ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage, 396 const CoverageFiltersMatchAll &Filters) { 397 // Emit the default stylesheet. 398 auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true); 399 if (Error E = CSSOrErr.takeError()) 400 return E; 401 402 OwnedStream CSS = std::move(CSSOrErr.get()); 403 CSS->operator<<(CSSForCoverage); 404 405 // Emit a file index along with some coverage statistics. 406 auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true); 407 if (Error E = OSOrErr.takeError()) 408 return E; 409 auto OS = std::move(OSOrErr.get()); 410 raw_ostream &OSRef = *OS.get(); 411 412 assert(Opts.hasOutputDirectory() && "No output directory for index file"); 413 emitPrelude(OSRef, Opts, getPathToStyle("")); 414 415 // Emit some basic information about the coverage report. 416 if (Opts.hasProjectTitle()) 417 OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts)); 418 OSRef << tag(ReportTitleTag, "Coverage Report"); 419 if (Opts.hasCreatedTime()) 420 OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts)); 421 422 // Emit a link to some documentation. 423 OSRef << tag("p", "Click " + 424 a("http://clang.llvm.org/docs/" 425 "SourceBasedCodeCoverage.html#interpreting-reports", 426 "here") + 427 " for information about interpreting this report."); 428 429 // Emit a table containing links to reports for each file in the covmapping. 430 // Exclude files which don't contain any regions. 431 OSRef << BeginCenteredDiv << BeginTable; 432 emitColumnLabelsForIndex(OSRef, Opts); 433 FileCoverageSummary Totals("TOTALS"); 434 auto FileReports = CoverageReport::prepareFileReports( 435 Coverage, Totals, SourceFiles, Opts, Filters); 436 bool EmptyFiles = false; 437 for (unsigned I = 0, E = FileReports.size(); I < E; ++I) { 438 if (FileReports[I].FunctionCoverage.getNumFunctions()) 439 emitFileSummary(OSRef, SourceFiles[I], FileReports[I]); 440 else 441 EmptyFiles = true; 442 } 443 emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true); 444 OSRef << EndTable << EndCenteredDiv; 445 446 // Emit links to files which don't contain any functions. These are normally 447 // not very useful, but could be relevant for code which abuses the 448 // preprocessor. 449 if (EmptyFiles && Filters.empty()) { 450 OSRef << tag("p", "Files which contain no functions. (These " 451 "files contain code pulled into other files " 452 "by the preprocessor.)\n"); 453 OSRef << BeginCenteredDiv << BeginTable; 454 for (unsigned I = 0, E = FileReports.size(); I < E; ++I) 455 if (!FileReports[I].FunctionCoverage.getNumFunctions()) { 456 std::string Link = buildLinkToFile(SourceFiles[I], FileReports[I]); 457 OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n'; 458 } 459 OSRef << EndTable << EndCenteredDiv; 460 } 461 462 OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts)); 463 emitEpilog(OSRef); 464 465 return Error::success(); 466 } 467 468 void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) { 469 OS << BeginCenteredDiv << BeginTable; 470 } 471 472 void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) { 473 OS << EndTable << EndCenteredDiv; 474 } 475 476 void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) { 477 OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions())) 478 << EndSourceNameDiv; 479 } 480 481 void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) { 482 OS << "<tr>"; 483 } 484 485 void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) { 486 // If this view has sub-views, renderLine() cannot close the view's cell. 487 // Take care of it here, after all sub-views have been rendered. 488 if (hasSubViews()) 489 OS << EndCodeTD; 490 OS << "</tr>"; 491 } 492 493 void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) { 494 // The table-based output makes view dividers unnecessary. 495 } 496 497 void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L, 498 const LineCoverageStats &LCS, 499 unsigned ExpansionCol, unsigned) { 500 StringRef Line = L.Line; 501 unsigned LineNo = L.LineNo; 502 503 // Steps for handling text-escaping, highlighting, and tooltip creation: 504 // 505 // 1. Split the line into N+1 snippets, where N = |Segments|. The first 506 // snippet starts from Col=1 and ends at the start of the first segment. 507 // The last snippet starts at the last mapped column in the line and ends 508 // at the end of the line. Both are required but may be empty. 509 510 SmallVector<std::string, 8> Snippets; 511 CoverageSegmentArray Segments = LCS.getLineSegments(); 512 513 unsigned LCol = 1; 514 auto Snip = [&](unsigned Start, unsigned Len) { 515 Snippets.push_back(std::string(Line.substr(Start, Len))); 516 LCol += Len; 517 }; 518 519 Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1)); 520 521 for (unsigned I = 1, E = Segments.size(); I < E; ++I) 522 Snip(LCol - 1, Segments[I]->Col - LCol); 523 524 // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1. 525 Snip(LCol - 1, Line.size() + 1 - LCol); 526 527 // 2. Escape all of the snippets. 528 529 for (unsigned I = 0, E = Snippets.size(); I < E; ++I) 530 Snippets[I] = escape(Snippets[I], getOptions()); 531 532 // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment 533 // 1 to set the highlight for snippet 2, segment 2 to set the highlight for 534 // snippet 3, and so on. 535 536 std::optional<StringRef> Color; 537 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; 538 auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) { 539 if (getOptions().Debug) 540 HighlightedRanges.emplace_back(LC, RC); 541 return tag("span", Snippet, std::string(*Color)); 542 }; 543 544 auto CheckIfUncovered = [&](const CoverageSegment *S) { 545 return S && (!S->IsGapRegion || (Color && *Color == "red")) && 546 S->HasCount && S->Count == 0; 547 }; 548 549 if (CheckIfUncovered(LCS.getWrappedSegment())) { 550 Color = "red"; 551 if (!Snippets[0].empty()) 552 Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size()); 553 } 554 555 for (unsigned I = 0, E = Segments.size(); I < E; ++I) { 556 const auto *CurSeg = Segments[I]; 557 if (CheckIfUncovered(CurSeg)) 558 Color = "red"; 559 else if (CurSeg->Col == ExpansionCol) 560 Color = "cyan"; 561 else 562 Color = std::nullopt; 563 564 if (Color) 565 Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col, 566 CurSeg->Col + Snippets[I + 1].size()); 567 } 568 569 if (Color && Segments.empty()) 570 Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size()); 571 572 if (getOptions().Debug) { 573 for (const auto &Range : HighlightedRanges) { 574 errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> "; 575 if (Range.second == 0) 576 errs() << "?"; 577 else 578 errs() << Range.second; 579 errs() << "\n"; 580 } 581 } 582 583 // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate 584 // sub-line region count tooltips if needed. 585 586 if (shouldRenderRegionMarkers(LCS)) { 587 // Just consider the segments which start *and* end on this line. 588 for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) { 589 const auto *CurSeg = Segments[I]; 590 if (!CurSeg->IsRegionEntry) 591 continue; 592 if (CurSeg->Count == LCS.getExecutionCount()) 593 continue; 594 595 Snippets[I + 1] = 596 tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count), 597 "tooltip-content"), 598 "tooltip"); 599 600 if (getOptions().Debug) 601 errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = " 602 << formatCount(CurSeg->Count) << "\n"; 603 } 604 } 605 606 OS << BeginCodeTD; 607 OS << BeginPre; 608 for (const auto &Snippet : Snippets) 609 OS << Snippet; 610 OS << EndPre; 611 612 // If there are no sub-views left to attach to this cell, end the cell. 613 // Otherwise, end it after the sub-views are rendered (renderLineSuffix()). 614 if (!hasSubViews()) 615 OS << EndCodeTD; 616 } 617 618 void SourceCoverageViewHTML::renderLineCoverageColumn( 619 raw_ostream &OS, const LineCoverageStats &Line) { 620 std::string Count; 621 if (Line.isMapped()) 622 Count = tag("pre", formatCount(Line.getExecutionCount())); 623 std::string CoverageClass = 624 (Line.getExecutionCount() > 0) ? "covered-line" : "uncovered-line"; 625 OS << tag("td", Count, CoverageClass); 626 } 627 628 void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS, 629 unsigned LineNo) { 630 std::string LineNoStr = utostr(uint64_t(LineNo)); 631 std::string TargetName = "L" + LineNoStr; 632 OS << tag("td", a("#" + TargetName, tag("pre", LineNoStr), TargetName), 633 "line-number"); 634 } 635 636 void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &, 637 const LineCoverageStats &Line, 638 unsigned) { 639 // Region markers are rendered in-line using tooltips. 640 } 641 642 void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L, 643 const LineCoverageStats &LCS, 644 unsigned ExpansionCol, 645 unsigned ViewDepth) { 646 // Render the line containing the expansion site. No extra formatting needed. 647 renderLine(OS, L, LCS, ExpansionCol, ViewDepth); 648 } 649 650 void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS, 651 ExpansionView &ESV, 652 unsigned ViewDepth) { 653 OS << BeginExpansionDiv; 654 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, 655 /*ShowTitle=*/false, ViewDepth + 1); 656 OS << EndExpansionDiv; 657 } 658 659 void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV, 660 unsigned ViewDepth) { 661 // Render the child subview. 662 if (getOptions().Debug) 663 errs() << "Branch at line " << BRV.getLine() << '\n'; 664 665 OS << BeginExpansionDiv; 666 OS << BeginPre; 667 for (const auto &R : BRV.Regions) { 668 // Calculate TruePercent and False Percent. 669 double TruePercent = 0.0; 670 double FalsePercent = 0.0; 671 unsigned Total = R.ExecutionCount + R.FalseExecutionCount; 672 673 if (!getOptions().ShowBranchCounts && Total != 0) { 674 TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0; 675 FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0; 676 } 677 678 // Display Line + Column. 679 std::string LineNoStr = utostr(uint64_t(R.LineStart)); 680 std::string ColNoStr = utostr(uint64_t(R.ColumnStart)); 681 std::string TargetName = "L" + LineNoStr; 682 683 OS << " Branch ("; 684 OS << tag("span", 685 a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr), 686 TargetName), 687 "line-number") + 688 "): ["; 689 690 if (R.Folded) { 691 OS << "Folded - Ignored]\n"; 692 continue; 693 } 694 695 // Display TrueCount or TruePercent. 696 std::string TrueColor = R.ExecutionCount ? "None" : "red"; 697 std::string TrueCovClass = 698 (R.ExecutionCount > 0) ? "covered-line" : "uncovered-line"; 699 700 OS << tag("span", "True", TrueColor); 701 OS << ": "; 702 if (getOptions().ShowBranchCounts) 703 OS << tag("span", formatCount(R.ExecutionCount), TrueCovClass) << ", "; 704 else 705 OS << format("%0.2f", TruePercent) << "%, "; 706 707 // Display FalseCount or FalsePercent. 708 std::string FalseColor = R.FalseExecutionCount ? "None" : "red"; 709 std::string FalseCovClass = 710 (R.FalseExecutionCount > 0) ? "covered-line" : "uncovered-line"; 711 712 OS << tag("span", "False", FalseColor); 713 OS << ": "; 714 if (getOptions().ShowBranchCounts) 715 OS << tag("span", formatCount(R.FalseExecutionCount), FalseCovClass); 716 else 717 OS << format("%0.2f", FalsePercent) << "%"; 718 719 OS << "]\n"; 720 } 721 OS << EndPre; 722 OS << EndExpansionDiv; 723 } 724 725 void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS, 726 InstantiationView &ISV, 727 unsigned ViewDepth) { 728 OS << BeginExpansionDiv; 729 if (!ISV.View) 730 OS << BeginSourceNameDiv 731 << tag("pre", 732 escape("Unexecuted instantiation: " + ISV.FunctionName.str(), 733 getOptions())) 734 << EndSourceNameDiv; 735 else 736 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, 737 /*ShowTitle=*/false, ViewDepth); 738 OS << EndExpansionDiv; 739 } 740 741 void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) { 742 if (getOptions().hasProjectTitle()) 743 OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions())); 744 OS << tag(ReportTitleTag, escape(Title, getOptions())); 745 if (getOptions().hasCreatedTime()) 746 OS << tag(CreatedTimeTag, 747 escape(getOptions().CreatedTimeStr, getOptions())); 748 } 749 750 void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS, 751 unsigned FirstUncoveredLineNo, 752 unsigned ViewDepth) { 753 std::string SourceLabel; 754 if (FirstUncoveredLineNo == 0) { 755 SourceLabel = tag("td", tag("pre", "Source")); 756 } else { 757 std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo)); 758 SourceLabel = 759 tag("td", tag("pre", "Source (" + 760 a(LinkTarget, "jump to first uncovered line") + 761 ")")); 762 } 763 764 renderLinePrefix(OS, ViewDepth); 765 OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count")) 766 << SourceLabel; 767 renderLineSuffix(OS, ViewDepth); 768 } 769