10b57cec5SDimitry Andric //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file This file implements the html coverage renderer.
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "SourceCoverageViewHTML.h"
14c9157d92SDimitry Andric #include "CoverageReport.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
170b57cec5SDimitry Andric #include "llvm/Support/Format.h"
180b57cec5SDimitry Andric #include "llvm/Support/Path.h"
19c9157d92SDimitry Andric #include "llvm/Support/ThreadPool.h"
20bdd1243dSDimitry Andric #include <optional>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace {
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric // Return a string with the special characters in \p Str escaped.
escape(StringRef Str,const CoverageViewOptions & Opts)270b57cec5SDimitry Andric std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
280b57cec5SDimitry Andric   std::string TabExpandedResult;
290b57cec5SDimitry Andric   unsigned ColNum = 0; // Record the column number.
300b57cec5SDimitry Andric   for (char C : Str) {
310b57cec5SDimitry Andric     if (C == '\t') {
320b57cec5SDimitry Andric       // Replace '\t' with up to TabSize spaces.
330b57cec5SDimitry Andric       unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize);
345ffd83dbSDimitry Andric       TabExpandedResult.append(NumSpaces, ' ');
350b57cec5SDimitry Andric       ColNum += NumSpaces;
360b57cec5SDimitry Andric     } else {
370b57cec5SDimitry Andric       TabExpandedResult += C;
380b57cec5SDimitry Andric       if (C == '\n' || C == '\r')
390b57cec5SDimitry Andric         ColNum = 0;
400b57cec5SDimitry Andric       else
410b57cec5SDimitry Andric         ++ColNum;
420b57cec5SDimitry Andric     }
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric   std::string EscapedHTML;
450b57cec5SDimitry Andric   {
460b57cec5SDimitry Andric     raw_string_ostream OS{EscapedHTML};
470b57cec5SDimitry Andric     printHTMLEscaped(TabExpandedResult, OS);
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric   return EscapedHTML;
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric // Create a \p Name tag around \p Str, and optionally set its \p ClassName.
tag(StringRef Name,StringRef Str,StringRef ClassName="")53c9157d92SDimitry Andric std::string tag(StringRef Name, StringRef Str, StringRef ClassName = "") {
54c9157d92SDimitry Andric   std::string Tag = "<";
55c9157d92SDimitry Andric   Tag += Name;
56c9157d92SDimitry Andric   if (!ClassName.empty()) {
57c9157d92SDimitry Andric     Tag += " class='";
58c9157d92SDimitry Andric     Tag += ClassName;
59c9157d92SDimitry Andric     Tag += "'";
60c9157d92SDimitry Andric   }
61c9157d92SDimitry Andric   Tag += ">";
62c9157d92SDimitry Andric   Tag += Str;
63c9157d92SDimitry Andric   Tag += "</";
64c9157d92SDimitry Andric   Tag += Name;
65c9157d92SDimitry Andric   Tag += ">";
66c9157d92SDimitry Andric   return Tag;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric // Create an anchor to \p Link with the label \p Str.
a(StringRef Link,StringRef Str,StringRef TargetName="")70c9157d92SDimitry Andric std::string a(StringRef Link, StringRef Str, StringRef TargetName = "") {
71c9157d92SDimitry Andric   std::string Tag;
72c9157d92SDimitry Andric   Tag += "<a ";
73c9157d92SDimitry Andric   if (!TargetName.empty()) {
74c9157d92SDimitry Andric     Tag += "name='";
75c9157d92SDimitry Andric     Tag += TargetName;
76c9157d92SDimitry Andric     Tag += "' ";
77c9157d92SDimitry Andric   }
78c9157d92SDimitry Andric   Tag += "href='";
79c9157d92SDimitry Andric   Tag += Link;
80c9157d92SDimitry Andric   Tag += "'>";
81c9157d92SDimitry Andric   Tag += Str;
82c9157d92SDimitry Andric   Tag += "</a>";
83c9157d92SDimitry Andric   return Tag;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric const char *BeginHeader =
870b57cec5SDimitry Andric     "<head>"
880b57cec5SDimitry Andric     "<meta name='viewport' content='width=device-width,initial-scale=1'>"
890b57cec5SDimitry Andric     "<meta charset='UTF-8'>";
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric const char *CSSForCoverage =
920b57cec5SDimitry Andric     R"(.red {
930b57cec5SDimitry Andric   background-color: #ffd0d0;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric .cyan {
960b57cec5SDimitry Andric   background-color: cyan;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric body {
990b57cec5SDimitry Andric   font-family: -apple-system, sans-serif;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric pre {
1020b57cec5SDimitry Andric   margin-top: 0px !important;
1030b57cec5SDimitry Andric   margin-bottom: 0px !important;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric .source-name-title {
1060b57cec5SDimitry Andric   padding: 5px 10px;
1070b57cec5SDimitry Andric   border-bottom: 1px solid #dbdbdb;
1080b57cec5SDimitry Andric   background-color: #eee;
1090b57cec5SDimitry Andric   line-height: 35px;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric .centered {
1120b57cec5SDimitry Andric   display: table;
1130b57cec5SDimitry Andric   margin-left: left;
1140b57cec5SDimitry Andric   margin-right: auto;
1150b57cec5SDimitry Andric   border: 1px solid #dbdbdb;
1160b57cec5SDimitry Andric   border-radius: 3px;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric .expansion-view {
1190b57cec5SDimitry Andric   background-color: rgba(0, 0, 0, 0);
1200b57cec5SDimitry Andric   margin-left: 0px;
1210b57cec5SDimitry Andric   margin-top: 5px;
1220b57cec5SDimitry Andric   margin-right: 5px;
1230b57cec5SDimitry Andric   margin-bottom: 5px;
1240b57cec5SDimitry Andric   border: 1px solid #dbdbdb;
1250b57cec5SDimitry Andric   border-radius: 3px;
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric table {
1280b57cec5SDimitry Andric   border-collapse: collapse;
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric .light-row {
1310b57cec5SDimitry Andric   background: #ffffff;
1320b57cec5SDimitry Andric   border: 1px solid #dbdbdb;
133e710425bSDimitry Andric   border-left: none;
134e710425bSDimitry Andric   border-right: none;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric .light-row-bold {
1370b57cec5SDimitry Andric   background: #ffffff;
1380b57cec5SDimitry Andric   border: 1px solid #dbdbdb;
139e710425bSDimitry Andric   border-left: none;
140e710425bSDimitry Andric   border-right: none;
1410b57cec5SDimitry Andric   font-weight: bold;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric .column-entry {
1440b57cec5SDimitry Andric   text-align: left;
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric .column-entry-bold {
1470b57cec5SDimitry Andric   font-weight: bold;
1480b57cec5SDimitry Andric   text-align: left;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric .column-entry-yellow {
1510b57cec5SDimitry Andric   text-align: left;
1520b57cec5SDimitry Andric   background-color: #ffffd0;
1530b57cec5SDimitry Andric }
154e710425bSDimitry Andric .column-entry-yellow:hover, tr:hover .column-entry-yellow {
1550b57cec5SDimitry Andric   background-color: #fffff0;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric .column-entry-red {
1580b57cec5SDimitry Andric   text-align: left;
1590b57cec5SDimitry Andric   background-color: #ffd0d0;
1600b57cec5SDimitry Andric }
161e710425bSDimitry Andric .column-entry-red:hover, tr:hover .column-entry-red {
1620b57cec5SDimitry Andric   background-color: #fff0f0;
1630b57cec5SDimitry Andric }
164e710425bSDimitry Andric .column-entry-gray {
165e710425bSDimitry Andric   text-align: left;
166e710425bSDimitry Andric   background-color: #fbfbfb;
167e710425bSDimitry Andric }
168e710425bSDimitry Andric .column-entry-gray:hover, tr:hover .column-entry-gray {
169e710425bSDimitry Andric   background-color: #f0f0f0;
170e710425bSDimitry Andric }
1710b57cec5SDimitry Andric .column-entry-green {
1720b57cec5SDimitry Andric   text-align: left;
1730b57cec5SDimitry Andric   background-color: #d0ffd0;
1740b57cec5SDimitry Andric }
175e710425bSDimitry Andric .column-entry-green:hover, tr:hover .column-entry-green {
1760b57cec5SDimitry Andric   background-color: #f0fff0;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric .line-number {
1790b57cec5SDimitry Andric   text-align: right;
1800b57cec5SDimitry Andric   color: #aaa;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric .covered-line {
1830b57cec5SDimitry Andric   text-align: right;
1840b57cec5SDimitry Andric   color: #0080ff;
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric .uncovered-line {
1870b57cec5SDimitry Andric   text-align: right;
1880b57cec5SDimitry Andric   color: #ff3300;
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric .tooltip {
1910b57cec5SDimitry Andric   position: relative;
1920b57cec5SDimitry Andric   display: inline;
1930b57cec5SDimitry Andric   background-color: #b3e6ff;
1940b57cec5SDimitry Andric   text-decoration: none;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric .tooltip span.tooltip-content {
1970b57cec5SDimitry Andric   position: absolute;
1980b57cec5SDimitry Andric   width: 100px;
1990b57cec5SDimitry Andric   margin-left: -50px;
2000b57cec5SDimitry Andric   color: #FFFFFF;
2010b57cec5SDimitry Andric   background: #000000;
2020b57cec5SDimitry Andric   height: 30px;
2030b57cec5SDimitry Andric   line-height: 30px;
2040b57cec5SDimitry Andric   text-align: center;
2050b57cec5SDimitry Andric   visibility: hidden;
2060b57cec5SDimitry Andric   border-radius: 6px;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric .tooltip span.tooltip-content:after {
2090b57cec5SDimitry Andric   content: '';
2100b57cec5SDimitry Andric   position: absolute;
2110b57cec5SDimitry Andric   top: 100%;
2120b57cec5SDimitry Andric   left: 50%;
2130b57cec5SDimitry Andric   margin-left: -8px;
2140b57cec5SDimitry Andric   width: 0; height: 0;
2150b57cec5SDimitry Andric   border-top: 8px solid #000000;
2160b57cec5SDimitry Andric   border-right: 8px solid transparent;
2170b57cec5SDimitry Andric   border-left: 8px solid transparent;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric :hover.tooltip span.tooltip-content {
2200b57cec5SDimitry Andric   visibility: visible;
2210b57cec5SDimitry Andric   opacity: 0.8;
2220b57cec5SDimitry Andric   bottom: 30px;
2230b57cec5SDimitry Andric   left: 50%;
2240b57cec5SDimitry Andric   z-index: 999;
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric th, td {
2270b57cec5SDimitry Andric   vertical-align: top;
2280b57cec5SDimitry Andric   padding: 2px 8px;
2290b57cec5SDimitry Andric   border-collapse: collapse;
2300b57cec5SDimitry Andric   border-right: solid 1px #eee;
2310b57cec5SDimitry Andric   border-left: solid 1px #eee;
2320b57cec5SDimitry Andric   text-align: left;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric td pre {
2350b57cec5SDimitry Andric   display: inline-block;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric td:first-child {
2380b57cec5SDimitry Andric   border-left: none;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric td:last-child {
2410b57cec5SDimitry Andric   border-right: none;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric tr:hover {
2440b57cec5SDimitry Andric   background-color: #f0f0f0;
2450b57cec5SDimitry Andric }
246e710425bSDimitry Andric tr:last-child {
247e710425bSDimitry Andric   border-bottom: none;
248e710425bSDimitry Andric }
249*471f3ba4SDimitry Andric tr:has(> td >a:target) > td.code > pre {
250*471f3ba4SDimitry Andric   background-color: #ffa;
251*471f3ba4SDimitry Andric }
2520b57cec5SDimitry Andric )";
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric const char *EndHeader = "</head>";
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric const char *BeginCenteredDiv = "<div class='centered'>";
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric const char *EndCenteredDiv = "</div>";
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric const char *BeginSourceNameDiv = "<div class='source-name-title'>";
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric const char *EndSourceNameDiv = "</div>";
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric const char *BeginCodeTD = "<td class='code'>";
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric const char *EndCodeTD = "</td>";
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric const char *BeginPre = "<pre>";
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric const char *EndPre = "</pre>";
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric const char *BeginExpansionDiv = "<div class='expansion-view'>";
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric const char *EndExpansionDiv = "</div>";
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric const char *BeginTable = "<table>";
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric const char *EndTable = "</table>";
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric const char *ProjectTitleTag = "h1";
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric const char *ReportTitleTag = "h2";
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric const char *CreatedTimeTag = "h4";
2850b57cec5SDimitry Andric 
getPathToStyle(StringRef ViewPath)2860b57cec5SDimitry Andric std::string getPathToStyle(StringRef ViewPath) {
287e8d8bef9SDimitry Andric   std::string PathToStyle;
2885ffd83dbSDimitry Andric   std::string PathSep = std::string(sys::path::get_separator());
2890b57cec5SDimitry Andric   unsigned NumSeps = ViewPath.count(PathSep);
2900b57cec5SDimitry Andric   for (unsigned I = 0, E = NumSeps; I < E; ++I)
2910b57cec5SDimitry Andric     PathToStyle += ".." + PathSep;
2920b57cec5SDimitry Andric   return PathToStyle + "style.css";
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric 
emitPrelude(raw_ostream & OS,const CoverageViewOptions & Opts,const std::string & PathToStyle="")2950b57cec5SDimitry Andric void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
2960b57cec5SDimitry Andric                  const std::string &PathToStyle = "") {
2970b57cec5SDimitry Andric   OS << "<!doctype html>"
2980b57cec5SDimitry Andric         "<html>"
2990b57cec5SDimitry Andric      << BeginHeader;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // Link to a stylesheet if one is available. Otherwise, use the default style.
3020b57cec5SDimitry Andric   if (PathToStyle.empty())
3030b57cec5SDimitry Andric     OS << "<style>" << CSSForCoverage << "</style>";
3040b57cec5SDimitry Andric   else
3050b57cec5SDimitry Andric     OS << "<link rel='stylesheet' type='text/css' href='"
3060b57cec5SDimitry Andric        << escape(PathToStyle, Opts) << "'>";
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   OS << EndHeader << "<body>";
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
emitTableRow(raw_ostream & OS,const CoverageViewOptions & Opts,const std::string & FirstCol,const FileCoverageSummary & FCS,bool IsTotals)311c9157d92SDimitry Andric void emitTableRow(raw_ostream &OS, const CoverageViewOptions &Opts,
312c9157d92SDimitry Andric                   const std::string &FirstCol, const FileCoverageSummary &FCS,
313c9157d92SDimitry Andric                   bool IsTotals) {
314c9157d92SDimitry Andric   SmallVector<std::string, 8> Columns;
315c9157d92SDimitry Andric 
316c9157d92SDimitry Andric   // Format a coverage triple and add the result to the list of columns.
317c9157d92SDimitry Andric   auto AddCoverageTripleToColumn =
318c9157d92SDimitry Andric       [&Columns, &Opts](unsigned Hit, unsigned Total, float Pctg) {
319c9157d92SDimitry Andric         std::string S;
320c9157d92SDimitry Andric         {
321c9157d92SDimitry Andric           raw_string_ostream RSO{S};
322c9157d92SDimitry Andric           if (Total)
323c9157d92SDimitry Andric             RSO << format("%*.2f", 7, Pctg) << "% ";
324c9157d92SDimitry Andric           else
325c9157d92SDimitry Andric             RSO << "- ";
326c9157d92SDimitry Andric           RSO << '(' << Hit << '/' << Total << ')';
327c9157d92SDimitry Andric         }
328c9157d92SDimitry Andric         const char *CellClass = "column-entry-yellow";
329e710425bSDimitry Andric         if (!Total)
330e710425bSDimitry Andric           CellClass = "column-entry-gray";
331e710425bSDimitry Andric         else if (Pctg >= Opts.HighCovWatermark)
332c9157d92SDimitry Andric           CellClass = "column-entry-green";
333c9157d92SDimitry Andric         else if (Pctg < Opts.LowCovWatermark)
334c9157d92SDimitry Andric           CellClass = "column-entry-red";
335c9157d92SDimitry Andric         Columns.emplace_back(tag("td", tag("pre", S), CellClass));
336c9157d92SDimitry Andric       };
337c9157d92SDimitry Andric 
338c9157d92SDimitry Andric   Columns.emplace_back(tag("td", tag("pre", FirstCol)));
339c9157d92SDimitry Andric   AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(),
340c9157d92SDimitry Andric                             FCS.FunctionCoverage.getNumFunctions(),
341c9157d92SDimitry Andric                             FCS.FunctionCoverage.getPercentCovered());
342c9157d92SDimitry Andric   if (Opts.ShowInstantiationSummary)
343c9157d92SDimitry Andric     AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(),
344c9157d92SDimitry Andric                               FCS.InstantiationCoverage.getNumFunctions(),
345c9157d92SDimitry Andric                               FCS.InstantiationCoverage.getPercentCovered());
346c9157d92SDimitry Andric   AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(),
347c9157d92SDimitry Andric                             FCS.LineCoverage.getNumLines(),
348c9157d92SDimitry Andric                             FCS.LineCoverage.getPercentCovered());
349c9157d92SDimitry Andric   if (Opts.ShowRegionSummary)
350c9157d92SDimitry Andric     AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(),
351c9157d92SDimitry Andric                               FCS.RegionCoverage.getNumRegions(),
352c9157d92SDimitry Andric                               FCS.RegionCoverage.getPercentCovered());
353c9157d92SDimitry Andric   if (Opts.ShowBranchSummary)
354c9157d92SDimitry Andric     AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(),
355c9157d92SDimitry Andric                               FCS.BranchCoverage.getNumBranches(),
356c9157d92SDimitry Andric                               FCS.BranchCoverage.getPercentCovered());
357c9157d92SDimitry Andric   if (Opts.ShowMCDCSummary)
358c9157d92SDimitry Andric     AddCoverageTripleToColumn(FCS.MCDCCoverage.getCoveredPairs(),
359c9157d92SDimitry Andric                               FCS.MCDCCoverage.getNumPairs(),
360c9157d92SDimitry Andric                               FCS.MCDCCoverage.getPercentCovered());
361c9157d92SDimitry Andric 
362c9157d92SDimitry Andric   if (IsTotals)
363c9157d92SDimitry Andric     OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold");
364c9157d92SDimitry Andric   else
365c9157d92SDimitry Andric     OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row");
366c9157d92SDimitry Andric }
367c9157d92SDimitry Andric 
emitEpilog(raw_ostream & OS)3680b57cec5SDimitry Andric void emitEpilog(raw_ostream &OS) {
3690b57cec5SDimitry Andric   OS << "</body>"
3700b57cec5SDimitry Andric      << "</html>";
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric } // anonymous namespace
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric Expected<CoveragePrinter::OwnedStream>
createViewFile(StringRef Path,bool InToplevel)3760b57cec5SDimitry Andric CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
3770b57cec5SDimitry Andric   auto OSOrErr = createOutputStream(Path, "html", InToplevel);
3780b57cec5SDimitry Andric   if (!OSOrErr)
3790b57cec5SDimitry Andric     return OSOrErr;
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   OwnedStream OS = std::move(OSOrErr.get());
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   if (!Opts.hasOutputDirectory()) {
3840b57cec5SDimitry Andric     emitPrelude(*OS.get(), Opts);
3850b57cec5SDimitry Andric   } else {
3860b57cec5SDimitry Andric     std::string ViewPath = getOutputPath(Path, "html", InToplevel);
3870b57cec5SDimitry Andric     emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
3880b57cec5SDimitry Andric   }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   return std::move(OS);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric 
closeViewFile(OwnedStream OS)3930b57cec5SDimitry Andric void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
3940b57cec5SDimitry Andric   emitEpilog(*OS.get());
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric /// Emit column labels for the table in the index.
emitColumnLabelsForIndex(raw_ostream & OS,const CoverageViewOptions & Opts)3980b57cec5SDimitry Andric static void emitColumnLabelsForIndex(raw_ostream &OS,
3990b57cec5SDimitry Andric                                      const CoverageViewOptions &Opts) {
4000b57cec5SDimitry Andric   SmallVector<std::string, 4> Columns;
4010b57cec5SDimitry Andric   Columns.emplace_back(tag("td", "Filename", "column-entry-bold"));
4020b57cec5SDimitry Andric   Columns.emplace_back(tag("td", "Function Coverage", "column-entry-bold"));
4030b57cec5SDimitry Andric   if (Opts.ShowInstantiationSummary)
4040b57cec5SDimitry Andric     Columns.emplace_back(
4050b57cec5SDimitry Andric         tag("td", "Instantiation Coverage", "column-entry-bold"));
4060b57cec5SDimitry Andric   Columns.emplace_back(tag("td", "Line Coverage", "column-entry-bold"));
4070b57cec5SDimitry Andric   if (Opts.ShowRegionSummary)
4080b57cec5SDimitry Andric     Columns.emplace_back(tag("td", "Region Coverage", "column-entry-bold"));
409e8d8bef9SDimitry Andric   if (Opts.ShowBranchSummary)
410e8d8bef9SDimitry Andric     Columns.emplace_back(tag("td", "Branch Coverage", "column-entry-bold"));
411c9157d92SDimitry Andric   if (Opts.ShowMCDCSummary)
412c9157d92SDimitry Andric     Columns.emplace_back(tag("td", "MC/DC", "column-entry-bold"));
4130b57cec5SDimitry Andric   OS << tag("tr", join(Columns.begin(), Columns.end(), ""));
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric std::string
buildLinkToFile(StringRef SF,const FileCoverageSummary & FCS) const4170b57cec5SDimitry Andric CoveragePrinterHTML::buildLinkToFile(StringRef SF,
4180b57cec5SDimitry Andric                                      const FileCoverageSummary &FCS) const {
4190b57cec5SDimitry Andric   SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name));
42004eeddc0SDimitry Andric   sys::path::remove_dots(LinkTextStr, /*remove_dot_dot=*/true);
4210b57cec5SDimitry Andric   sys::path::native(LinkTextStr);
4220b57cec5SDimitry Andric   std::string LinkText = escape(LinkTextStr, Opts);
4230b57cec5SDimitry Andric   std::string LinkTarget =
4240b57cec5SDimitry Andric       escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
4250b57cec5SDimitry Andric   return a(LinkTarget, LinkText);
4260b57cec5SDimitry Andric }
4270b57cec5SDimitry Andric 
emitStyleSheet()428c9157d92SDimitry Andric Error CoveragePrinterHTML::emitStyleSheet() {
4290b57cec5SDimitry Andric   auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
4300b57cec5SDimitry Andric   if (Error E = CSSOrErr.takeError())
4310b57cec5SDimitry Andric     return E;
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   OwnedStream CSS = std::move(CSSOrErr.get());
4340b57cec5SDimitry Andric   CSS->operator<<(CSSForCoverage);
4350b57cec5SDimitry Andric 
436c9157d92SDimitry Andric   return Error::success();
437c9157d92SDimitry Andric }
4380b57cec5SDimitry Andric 
emitReportHeader(raw_ostream & OSRef,const std::string & Title)439c9157d92SDimitry Andric void CoveragePrinterHTML::emitReportHeader(raw_ostream &OSRef,
440c9157d92SDimitry Andric                                            const std::string &Title) {
4410b57cec5SDimitry Andric   // Emit some basic information about the coverage report.
4420b57cec5SDimitry Andric   if (Opts.hasProjectTitle())
4430b57cec5SDimitry Andric     OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts));
444c9157d92SDimitry Andric   OSRef << tag(ReportTitleTag, Title);
4450b57cec5SDimitry Andric   if (Opts.hasCreatedTime())
4460b57cec5SDimitry Andric     OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts));
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   // Emit a link to some documentation.
4490b57cec5SDimitry Andric   OSRef << tag("p", "Click " +
4500b57cec5SDimitry Andric                         a("http://clang.llvm.org/docs/"
4510b57cec5SDimitry Andric                           "SourceBasedCodeCoverage.html#interpreting-reports",
4520b57cec5SDimitry Andric                           "here") +
4530b57cec5SDimitry Andric                         " for information about interpreting this report.");
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   // Emit a table containing links to reports for each file in the covmapping.
4560b57cec5SDimitry Andric   // Exclude files which don't contain any regions.
4570b57cec5SDimitry Andric   OSRef << BeginCenteredDiv << BeginTable;
4580b57cec5SDimitry Andric   emitColumnLabelsForIndex(OSRef, Opts);
459c9157d92SDimitry Andric }
460c9157d92SDimitry Andric 
461c9157d92SDimitry Andric /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is
462c9157d92SDimitry Andric /// false, link the summary to \p SF.
emitFileSummary(raw_ostream & OS,StringRef SF,const FileCoverageSummary & FCS,bool IsTotals) const463c9157d92SDimitry Andric void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
464c9157d92SDimitry Andric                                           const FileCoverageSummary &FCS,
465c9157d92SDimitry Andric                                           bool IsTotals) const {
466c9157d92SDimitry Andric   // Simplify the display file path, and wrap it in a link if requested.
467c9157d92SDimitry Andric   std::string Filename;
468c9157d92SDimitry Andric   if (IsTotals) {
469c9157d92SDimitry Andric     Filename = std::string(SF);
470c9157d92SDimitry Andric   } else {
471c9157d92SDimitry Andric     Filename = buildLinkToFile(SF, FCS);
472c9157d92SDimitry Andric   }
473c9157d92SDimitry Andric 
474c9157d92SDimitry Andric   emitTableRow(OS, Opts, Filename, FCS, IsTotals);
475c9157d92SDimitry Andric }
476c9157d92SDimitry Andric 
createIndexFile(ArrayRef<std::string> SourceFiles,const CoverageMapping & Coverage,const CoverageFiltersMatchAll & Filters)477c9157d92SDimitry Andric Error CoveragePrinterHTML::createIndexFile(
478c9157d92SDimitry Andric     ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
479c9157d92SDimitry Andric     const CoverageFiltersMatchAll &Filters) {
480c9157d92SDimitry Andric   // Emit the default stylesheet.
481c9157d92SDimitry Andric   if (Error E = emitStyleSheet())
482c9157d92SDimitry Andric     return E;
483c9157d92SDimitry Andric 
484c9157d92SDimitry Andric   // Emit a file index along with some coverage statistics.
485c9157d92SDimitry Andric   auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
486c9157d92SDimitry Andric   if (Error E = OSOrErr.takeError())
487c9157d92SDimitry Andric     return E;
488c9157d92SDimitry Andric   auto OS = std::move(OSOrErr.get());
489c9157d92SDimitry Andric   raw_ostream &OSRef = *OS.get();
490c9157d92SDimitry Andric 
491c9157d92SDimitry Andric   assert(Opts.hasOutputDirectory() && "No output directory for index file");
492c9157d92SDimitry Andric   emitPrelude(OSRef, Opts, getPathToStyle(""));
493c9157d92SDimitry Andric 
494c9157d92SDimitry Andric   emitReportHeader(OSRef, "Coverage Report");
495c9157d92SDimitry Andric 
4960b57cec5SDimitry Andric   FileCoverageSummary Totals("TOTALS");
4970b57cec5SDimitry Andric   auto FileReports = CoverageReport::prepareFileReports(
4980b57cec5SDimitry Andric       Coverage, Totals, SourceFiles, Opts, Filters);
4990b57cec5SDimitry Andric   bool EmptyFiles = false;
5000b57cec5SDimitry Andric   for (unsigned I = 0, E = FileReports.size(); I < E; ++I) {
5010b57cec5SDimitry Andric     if (FileReports[I].FunctionCoverage.getNumFunctions())
5020b57cec5SDimitry Andric       emitFileSummary(OSRef, SourceFiles[I], FileReports[I]);
5030b57cec5SDimitry Andric     else
5040b57cec5SDimitry Andric       EmptyFiles = true;
5050b57cec5SDimitry Andric   }
5060b57cec5SDimitry Andric   emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true);
5070b57cec5SDimitry Andric   OSRef << EndTable << EndCenteredDiv;
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   // Emit links to files which don't contain any functions. These are normally
5100b57cec5SDimitry Andric   // not very useful, but could be relevant for code which abuses the
5110b57cec5SDimitry Andric   // preprocessor.
5120b57cec5SDimitry Andric   if (EmptyFiles && Filters.empty()) {
5130b57cec5SDimitry Andric     OSRef << tag("p", "Files which contain no functions. (These "
5140b57cec5SDimitry Andric                       "files contain code pulled into other files "
5150b57cec5SDimitry Andric                       "by the preprocessor.)\n");
5160b57cec5SDimitry Andric     OSRef << BeginCenteredDiv << BeginTable;
5170b57cec5SDimitry Andric     for (unsigned I = 0, E = FileReports.size(); I < E; ++I)
5180b57cec5SDimitry Andric       if (!FileReports[I].FunctionCoverage.getNumFunctions()) {
5190b57cec5SDimitry Andric         std::string Link = buildLinkToFile(SourceFiles[I], FileReports[I]);
5200b57cec5SDimitry Andric         OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n';
5210b57cec5SDimitry Andric       }
5220b57cec5SDimitry Andric     OSRef << EndTable << EndCenteredDiv;
5230b57cec5SDimitry Andric   }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts));
5260b57cec5SDimitry Andric   emitEpilog(OSRef);
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric   return Error::success();
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
531c9157d92SDimitry Andric struct CoveragePrinterHTMLDirectory::Reporter : public DirectoryCoverageReport {
532c9157d92SDimitry Andric   CoveragePrinterHTMLDirectory &Printer;
533c9157d92SDimitry Andric 
ReporterCoveragePrinterHTMLDirectory::Reporter534c9157d92SDimitry Andric   Reporter(CoveragePrinterHTMLDirectory &Printer,
535c9157d92SDimitry Andric            const coverage::CoverageMapping &Coverage,
536c9157d92SDimitry Andric            const CoverageFiltersMatchAll &Filters)
537c9157d92SDimitry Andric       : DirectoryCoverageReport(Printer.Opts, Coverage, Filters),
538c9157d92SDimitry Andric         Printer(Printer) {}
539c9157d92SDimitry Andric 
generateSubDirectoryReportCoveragePrinterHTMLDirectory::Reporter540c9157d92SDimitry Andric   Error generateSubDirectoryReport(SubFileReports &&SubFiles,
541c9157d92SDimitry Andric                                    SubDirReports &&SubDirs,
542c9157d92SDimitry Andric                                    FileCoverageSummary &&SubTotals) override {
543c9157d92SDimitry Andric     auto &LCPath = SubTotals.Name;
544c9157d92SDimitry Andric     assert(Options.hasOutputDirectory() &&
545c9157d92SDimitry Andric            "No output directory for index file");
546c9157d92SDimitry Andric 
547c9157d92SDimitry Andric     SmallString<128> OSPath = LCPath;
548c9157d92SDimitry Andric     sys::path::append(OSPath, "index");
549c9157d92SDimitry Andric     auto OSOrErr = Printer.createOutputStream(OSPath, "html",
550c9157d92SDimitry Andric                                               /*InToplevel=*/false);
551c9157d92SDimitry Andric     if (auto E = OSOrErr.takeError())
552c9157d92SDimitry Andric       return E;
553c9157d92SDimitry Andric     auto OS = std::move(OSOrErr.get());
554c9157d92SDimitry Andric     raw_ostream &OSRef = *OS.get();
555c9157d92SDimitry Andric 
556c9157d92SDimitry Andric     auto IndexHtmlPath = Printer.getOutputPath((LCPath + "index").str(), "html",
557c9157d92SDimitry Andric                                                /*InToplevel=*/false);
558c9157d92SDimitry Andric     emitPrelude(OSRef, Options, getPathToStyle(IndexHtmlPath));
559c9157d92SDimitry Andric 
560c9157d92SDimitry Andric     auto NavLink = buildTitleLinks(LCPath);
561c9157d92SDimitry Andric     Printer.emitReportHeader(OSRef, "Coverage Report (" + NavLink + ")");
562c9157d92SDimitry Andric 
563c9157d92SDimitry Andric     std::vector<const FileCoverageSummary *> EmptyFiles;
564c9157d92SDimitry Andric 
565c9157d92SDimitry Andric     // Make directories at the top of the table.
566c9157d92SDimitry Andric     for (auto &&SubDir : SubDirs) {
567c9157d92SDimitry Andric       auto &Report = SubDir.second.first;
568c9157d92SDimitry Andric       if (!Report.FunctionCoverage.getNumFunctions())
569c9157d92SDimitry Andric         EmptyFiles.push_back(&Report);
570c9157d92SDimitry Andric       else
571c9157d92SDimitry Andric         emitTableRow(OSRef, Options, buildRelLinkToFile(Report.Name), Report,
572c9157d92SDimitry Andric                      /*IsTotals=*/false);
573c9157d92SDimitry Andric     }
574c9157d92SDimitry Andric 
575c9157d92SDimitry Andric     for (auto &&SubFile : SubFiles) {
576c9157d92SDimitry Andric       auto &Report = SubFile.second;
577c9157d92SDimitry Andric       if (!Report.FunctionCoverage.getNumFunctions())
578c9157d92SDimitry Andric         EmptyFiles.push_back(&Report);
579c9157d92SDimitry Andric       else
580c9157d92SDimitry Andric         emitTableRow(OSRef, Options, buildRelLinkToFile(Report.Name), Report,
581c9157d92SDimitry Andric                      /*IsTotals=*/false);
582c9157d92SDimitry Andric     }
583c9157d92SDimitry Andric 
584c9157d92SDimitry Andric     // Emit the totals row.
585c9157d92SDimitry Andric     emitTableRow(OSRef, Options, "Totals", SubTotals, /*IsTotals=*/false);
586c9157d92SDimitry Andric     OSRef << EndTable << EndCenteredDiv;
587c9157d92SDimitry Andric 
588c9157d92SDimitry Andric     // Emit links to files which don't contain any functions. These are normally
589c9157d92SDimitry Andric     // not very useful, but could be relevant for code which abuses the
590c9157d92SDimitry Andric     // preprocessor.
591c9157d92SDimitry Andric     if (!EmptyFiles.empty()) {
592c9157d92SDimitry Andric       OSRef << tag("p", "Files which contain no functions. (These "
593c9157d92SDimitry Andric                         "files contain code pulled into other files "
594c9157d92SDimitry Andric                         "by the preprocessor.)\n");
595c9157d92SDimitry Andric       OSRef << BeginCenteredDiv << BeginTable;
596c9157d92SDimitry Andric       for (auto FCS : EmptyFiles) {
597c9157d92SDimitry Andric         auto Link = buildRelLinkToFile(FCS->Name);
598c9157d92SDimitry Andric         OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n';
599c9157d92SDimitry Andric       }
600c9157d92SDimitry Andric       OSRef << EndTable << EndCenteredDiv;
601c9157d92SDimitry Andric     }
602c9157d92SDimitry Andric 
603c9157d92SDimitry Andric     // Emit epilog.
604c9157d92SDimitry Andric     OSRef << tag("h5", escape(Options.getLLVMVersionString(), Options));
605c9157d92SDimitry Andric     emitEpilog(OSRef);
606c9157d92SDimitry Andric 
607c9157d92SDimitry Andric     return Error::success();
608c9157d92SDimitry Andric   }
609c9157d92SDimitry Andric 
610c9157d92SDimitry Andric   /// Make a title with hyperlinks to the index.html files of each hierarchy
611c9157d92SDimitry Andric   /// of the report.
buildTitleLinksCoveragePrinterHTMLDirectory::Reporter612c9157d92SDimitry Andric   std::string buildTitleLinks(StringRef LCPath) const {
613c9157d92SDimitry Andric     // For each report level in LCPStack, extract the path component and
614c9157d92SDimitry Andric     // calculate the number of "../" relative to current LCPath.
615c9157d92SDimitry Andric     SmallVector<std::pair<SmallString<128>, unsigned>, 16> Components;
616c9157d92SDimitry Andric 
617c9157d92SDimitry Andric     auto Iter = LCPStack.begin(), IterE = LCPStack.end();
618c9157d92SDimitry Andric     SmallString<128> RootPath;
619c9157d92SDimitry Andric     if (*Iter == 0) {
620c9157d92SDimitry Andric       // If llvm-cov works on relative coverage mapping data, the LCP of
621c9157d92SDimitry Andric       // all source file paths can be 0, which makes the title path empty.
622c9157d92SDimitry Andric       // As we like adding a slash at the back of the path to indicate a
623c9157d92SDimitry Andric       // directory, in this case, we use "." as the root path to make it
624c9157d92SDimitry Andric       // not be confused with the root path "/".
625c9157d92SDimitry Andric       RootPath = ".";
626c9157d92SDimitry Andric     } else {
627c9157d92SDimitry Andric       RootPath = LCPath.substr(0, *Iter);
628c9157d92SDimitry Andric       sys::path::native(RootPath);
629c9157d92SDimitry Andric       sys::path::remove_dots(RootPath, /*remove_dot_dot=*/true);
630c9157d92SDimitry Andric     }
631c9157d92SDimitry Andric     Components.emplace_back(std::move(RootPath), 0);
632c9157d92SDimitry Andric 
633c9157d92SDimitry Andric     for (auto Last = *Iter; ++Iter != IterE; Last = *Iter) {
634c9157d92SDimitry Andric       SmallString<128> SubPath = LCPath.substr(Last, *Iter - Last);
635c9157d92SDimitry Andric       sys::path::native(SubPath);
636c9157d92SDimitry Andric       sys::path::remove_dots(SubPath, /*remove_dot_dot=*/true);
637c9157d92SDimitry Andric       auto Level = unsigned(SubPath.count(sys::path::get_separator())) + 1;
638c9157d92SDimitry Andric       Components.back().second += Level;
639c9157d92SDimitry Andric       Components.emplace_back(std::move(SubPath), Level);
640c9157d92SDimitry Andric     }
641c9157d92SDimitry Andric 
642c9157d92SDimitry Andric     // Then we make the title accroding to Components.
643c9157d92SDimitry Andric     std::string S;
644c9157d92SDimitry Andric     for (auto I = Components.begin(), E = Components.end();;) {
645c9157d92SDimitry Andric       auto &Name = I->first;
646c9157d92SDimitry Andric       if (++I == E) {
647c9157d92SDimitry Andric         S += a("./index.html", Name);
648c9157d92SDimitry Andric         S += sys::path::get_separator();
649c9157d92SDimitry Andric         break;
650c9157d92SDimitry Andric       }
651c9157d92SDimitry Andric 
652c9157d92SDimitry Andric       SmallString<128> Link;
653c9157d92SDimitry Andric       for (unsigned J = I->second; J > 0; --J)
654c9157d92SDimitry Andric         Link += "../";
655c9157d92SDimitry Andric       Link += "index.html";
656c9157d92SDimitry Andric       S += a(Link, Name);
657c9157d92SDimitry Andric       S += sys::path::get_separator();
658c9157d92SDimitry Andric     }
659c9157d92SDimitry Andric     return S;
660c9157d92SDimitry Andric   }
661c9157d92SDimitry Andric 
buildRelLinkToFileCoveragePrinterHTMLDirectory::Reporter662c9157d92SDimitry Andric   std::string buildRelLinkToFile(StringRef RelPath) const {
663c9157d92SDimitry Andric     SmallString<128> LinkTextStr(RelPath);
664c9157d92SDimitry Andric     sys::path::native(LinkTextStr);
665c9157d92SDimitry Andric 
666c9157d92SDimitry Andric     // remove_dots will remove trailing slash, so we need to check before it.
667c9157d92SDimitry Andric     auto IsDir = LinkTextStr.ends_with(sys::path::get_separator());
668c9157d92SDimitry Andric     sys::path::remove_dots(LinkTextStr, /*remove_dot_dot=*/true);
669c9157d92SDimitry Andric 
670c9157d92SDimitry Andric     SmallString<128> LinkTargetStr(LinkTextStr);
671c9157d92SDimitry Andric     if (IsDir) {
672c9157d92SDimitry Andric       LinkTextStr += sys::path::get_separator();
673c9157d92SDimitry Andric       sys::path::append(LinkTargetStr, "index.html");
674c9157d92SDimitry Andric     } else {
675c9157d92SDimitry Andric       LinkTargetStr += ".html";
676c9157d92SDimitry Andric     }
677c9157d92SDimitry Andric 
678c9157d92SDimitry Andric     auto LinkText = escape(LinkTextStr, Options);
679c9157d92SDimitry Andric     auto LinkTarget = escape(LinkTargetStr, Options);
680c9157d92SDimitry Andric     return a(LinkTarget, LinkText);
681c9157d92SDimitry Andric   }
682c9157d92SDimitry Andric };
683c9157d92SDimitry Andric 
createIndexFile(ArrayRef<std::string> SourceFiles,const CoverageMapping & Coverage,const CoverageFiltersMatchAll & Filters)684c9157d92SDimitry Andric Error CoveragePrinterHTMLDirectory::createIndexFile(
685c9157d92SDimitry Andric     ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
686c9157d92SDimitry Andric     const CoverageFiltersMatchAll &Filters) {
687c9157d92SDimitry Andric   // The createSubIndexFile function only works when SourceFiles is
688c9157d92SDimitry Andric   // more than one. So we fallback to CoveragePrinterHTML when it is.
689c9157d92SDimitry Andric   if (SourceFiles.size() <= 1)
690c9157d92SDimitry Andric     return CoveragePrinterHTML::createIndexFile(SourceFiles, Coverage, Filters);
691c9157d92SDimitry Andric 
692c9157d92SDimitry Andric   // Emit the default stylesheet.
693c9157d92SDimitry Andric   if (Error E = emitStyleSheet())
694c9157d92SDimitry Andric     return E;
695c9157d92SDimitry Andric 
696c9157d92SDimitry Andric   // Emit index files in every subdirectory.
697c9157d92SDimitry Andric   Reporter Report(*this, Coverage, Filters);
698c9157d92SDimitry Andric   auto TotalsOrErr = Report.prepareDirectoryReports(SourceFiles);
699c9157d92SDimitry Andric   if (auto E = TotalsOrErr.takeError())
700c9157d92SDimitry Andric     return E;
701c9157d92SDimitry Andric   auto &LCPath = TotalsOrErr->Name;
702c9157d92SDimitry Andric 
703c9157d92SDimitry Andric   // Emit the top level index file. Top level index file is just a redirection
704c9157d92SDimitry Andric   // to the index file in the LCP directory.
705c9157d92SDimitry Andric   auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
706c9157d92SDimitry Andric   if (auto E = OSOrErr.takeError())
707c9157d92SDimitry Andric     return E;
708c9157d92SDimitry Andric   auto OS = std::move(OSOrErr.get());
709c9157d92SDimitry Andric   auto LCPIndexFilePath =
710c9157d92SDimitry Andric       getOutputPath((LCPath + "index").str(), "html", /*InToplevel=*/false);
711c9157d92SDimitry Andric   *OS.get() << R"(<!DOCTYPE html>
712c9157d92SDimitry Andric   <html>
713c9157d92SDimitry Andric     <head>
714c9157d92SDimitry Andric       <meta http-equiv="Refresh" content="0; url=')"
715c9157d92SDimitry Andric             << LCPIndexFilePath << R"('" />
716c9157d92SDimitry Andric     </head>
717c9157d92SDimitry Andric     <body></body>
718c9157d92SDimitry Andric   </html>
719c9157d92SDimitry Andric   )";
720c9157d92SDimitry Andric 
721c9157d92SDimitry Andric   return Error::success();
722c9157d92SDimitry Andric }
723c9157d92SDimitry Andric 
renderViewHeader(raw_ostream & OS)7240b57cec5SDimitry Andric void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
7250b57cec5SDimitry Andric   OS << BeginCenteredDiv << BeginTable;
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric 
renderViewFooter(raw_ostream & OS)7280b57cec5SDimitry Andric void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
7290b57cec5SDimitry Andric   OS << EndTable << EndCenteredDiv;
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
renderSourceName(raw_ostream & OS,bool WholeFile)7320b57cec5SDimitry Andric void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) {
7330b57cec5SDimitry Andric   OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions()))
7340b57cec5SDimitry Andric      << EndSourceNameDiv;
7350b57cec5SDimitry Andric }
7360b57cec5SDimitry Andric 
renderLinePrefix(raw_ostream & OS,unsigned)7370b57cec5SDimitry Andric void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
7380b57cec5SDimitry Andric   OS << "<tr>";
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric 
renderLineSuffix(raw_ostream & OS,unsigned)7410b57cec5SDimitry Andric void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
7420b57cec5SDimitry Andric   // If this view has sub-views, renderLine() cannot close the view's cell.
7430b57cec5SDimitry Andric   // Take care of it here, after all sub-views have been rendered.
7440b57cec5SDimitry Andric   if (hasSubViews())
7450b57cec5SDimitry Andric     OS << EndCodeTD;
7460b57cec5SDimitry Andric   OS << "</tr>";
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric 
renderViewDivider(raw_ostream &,unsigned)7490b57cec5SDimitry Andric void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
7500b57cec5SDimitry Andric   // The table-based output makes view dividers unnecessary.
7510b57cec5SDimitry Andric }
7520b57cec5SDimitry Andric 
renderLine(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned)7530b57cec5SDimitry Andric void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L,
7540b57cec5SDimitry Andric                                         const LineCoverageStats &LCS,
7550b57cec5SDimitry Andric                                         unsigned ExpansionCol, unsigned) {
7560b57cec5SDimitry Andric   StringRef Line = L.Line;
7570b57cec5SDimitry Andric   unsigned LineNo = L.LineNo;
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   // Steps for handling text-escaping, highlighting, and tooltip creation:
7600b57cec5SDimitry Andric   //
7610b57cec5SDimitry Andric   // 1. Split the line into N+1 snippets, where N = |Segments|. The first
7620b57cec5SDimitry Andric   //    snippet starts from Col=1 and ends at the start of the first segment.
7630b57cec5SDimitry Andric   //    The last snippet starts at the last mapped column in the line and ends
7640b57cec5SDimitry Andric   //    at the end of the line. Both are required but may be empty.
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric   SmallVector<std::string, 8> Snippets;
7670b57cec5SDimitry Andric   CoverageSegmentArray Segments = LCS.getLineSegments();
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric   unsigned LCol = 1;
7700b57cec5SDimitry Andric   auto Snip = [&](unsigned Start, unsigned Len) {
7715ffd83dbSDimitry Andric     Snippets.push_back(std::string(Line.substr(Start, Len)));
7720b57cec5SDimitry Andric     LCol += Len;
7730b57cec5SDimitry Andric   };
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
7760b57cec5SDimitry Andric 
7770b57cec5SDimitry Andric   for (unsigned I = 1, E = Segments.size(); I < E; ++I)
7780b57cec5SDimitry Andric     Snip(LCol - 1, Segments[I]->Col - LCol);
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric   // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
7810b57cec5SDimitry Andric   Snip(LCol - 1, Line.size() + 1 - LCol);
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   // 2. Escape all of the snippets.
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric   for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
7860b57cec5SDimitry Andric     Snippets[I] = escape(Snippets[I], getOptions());
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric   // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
7890b57cec5SDimitry Andric   //    1 to set the highlight for snippet 2, segment 2 to set the highlight for
7900b57cec5SDimitry Andric   //    snippet 3, and so on.
7910b57cec5SDimitry Andric 
792bdd1243dSDimitry Andric   std::optional<StringRef> Color;
7930b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
7940b57cec5SDimitry Andric   auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
7950b57cec5SDimitry Andric     if (getOptions().Debug)
7960b57cec5SDimitry Andric       HighlightedRanges.emplace_back(LC, RC);
79781ad6265SDimitry Andric     return tag("span", Snippet, std::string(*Color));
7980b57cec5SDimitry Andric   };
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric   auto CheckIfUncovered = [&](const CoverageSegment *S) {
8010b57cec5SDimitry Andric     return S && (!S->IsGapRegion || (Color && *Color == "red")) &&
8020b57cec5SDimitry Andric            S->HasCount && S->Count == 0;
8030b57cec5SDimitry Andric   };
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   if (CheckIfUncovered(LCS.getWrappedSegment())) {
8060b57cec5SDimitry Andric     Color = "red";
8070b57cec5SDimitry Andric     if (!Snippets[0].empty())
8080b57cec5SDimitry Andric       Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
8090b57cec5SDimitry Andric   }
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
8120b57cec5SDimitry Andric     const auto *CurSeg = Segments[I];
8130b57cec5SDimitry Andric     if (CheckIfUncovered(CurSeg))
8140b57cec5SDimitry Andric       Color = "red";
8150b57cec5SDimitry Andric     else if (CurSeg->Col == ExpansionCol)
8160b57cec5SDimitry Andric       Color = "cyan";
8170b57cec5SDimitry Andric     else
818bdd1243dSDimitry Andric       Color = std::nullopt;
8190b57cec5SDimitry Andric 
82081ad6265SDimitry Andric     if (Color)
8210b57cec5SDimitry Andric       Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col,
8220b57cec5SDimitry Andric                                   CurSeg->Col + Snippets[I + 1].size());
8230b57cec5SDimitry Andric   }
8240b57cec5SDimitry Andric 
82581ad6265SDimitry Andric   if (Color && Segments.empty())
8260b57cec5SDimitry Andric     Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric   if (getOptions().Debug) {
8290b57cec5SDimitry Andric     for (const auto &Range : HighlightedRanges) {
8300b57cec5SDimitry Andric       errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
8310b57cec5SDimitry Andric       if (Range.second == 0)
8320b57cec5SDimitry Andric         errs() << "?";
8330b57cec5SDimitry Andric       else
8340b57cec5SDimitry Andric         errs() << Range.second;
8350b57cec5SDimitry Andric       errs() << "\n";
8360b57cec5SDimitry Andric     }
8370b57cec5SDimitry Andric   }
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric   // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
8400b57cec5SDimitry Andric   //    sub-line region count tooltips if needed.
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric   if (shouldRenderRegionMarkers(LCS)) {
8430b57cec5SDimitry Andric     // Just consider the segments which start *and* end on this line.
8440b57cec5SDimitry Andric     for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
8450b57cec5SDimitry Andric       const auto *CurSeg = Segments[I];
8460b57cec5SDimitry Andric       if (!CurSeg->IsRegionEntry)
8470b57cec5SDimitry Andric         continue;
8480b57cec5SDimitry Andric       if (CurSeg->Count == LCS.getExecutionCount())
8490b57cec5SDimitry Andric         continue;
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric       Snippets[I + 1] =
8520b57cec5SDimitry Andric           tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
8530b57cec5SDimitry Andric                                            "tooltip-content"),
8540b57cec5SDimitry Andric               "tooltip");
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric       if (getOptions().Debug)
8570b57cec5SDimitry Andric         errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = "
8580b57cec5SDimitry Andric                << formatCount(CurSeg->Count) << "\n";
8590b57cec5SDimitry Andric     }
8600b57cec5SDimitry Andric   }
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   OS << BeginCodeTD;
8630b57cec5SDimitry Andric   OS << BeginPre;
8640b57cec5SDimitry Andric   for (const auto &Snippet : Snippets)
8650b57cec5SDimitry Andric     OS << Snippet;
8660b57cec5SDimitry Andric   OS << EndPre;
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric   // If there are no sub-views left to attach to this cell, end the cell.
8690b57cec5SDimitry Andric   // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
8700b57cec5SDimitry Andric   if (!hasSubViews())
8710b57cec5SDimitry Andric     OS << EndCodeTD;
8720b57cec5SDimitry Andric }
8730b57cec5SDimitry Andric 
renderLineCoverageColumn(raw_ostream & OS,const LineCoverageStats & Line)8740b57cec5SDimitry Andric void SourceCoverageViewHTML::renderLineCoverageColumn(
8750b57cec5SDimitry Andric     raw_ostream &OS, const LineCoverageStats &Line) {
876e8d8bef9SDimitry Andric   std::string Count;
8770b57cec5SDimitry Andric   if (Line.isMapped())
8780b57cec5SDimitry Andric     Count = tag("pre", formatCount(Line.getExecutionCount()));
8790b57cec5SDimitry Andric   std::string CoverageClass =
8800b57cec5SDimitry Andric       (Line.getExecutionCount() > 0) ? "covered-line" : "uncovered-line";
8810b57cec5SDimitry Andric   OS << tag("td", Count, CoverageClass);
8820b57cec5SDimitry Andric }
8830b57cec5SDimitry Andric 
renderLineNumberColumn(raw_ostream & OS,unsigned LineNo)8840b57cec5SDimitry Andric void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
8850b57cec5SDimitry Andric                                                     unsigned LineNo) {
8860b57cec5SDimitry Andric   std::string LineNoStr = utostr(uint64_t(LineNo));
8870b57cec5SDimitry Andric   std::string TargetName = "L" + LineNoStr;
8880b57cec5SDimitry Andric   OS << tag("td", a("#" + TargetName, tag("pre", LineNoStr), TargetName),
8890b57cec5SDimitry Andric             "line-number");
8900b57cec5SDimitry Andric }
8910b57cec5SDimitry Andric 
renderRegionMarkers(raw_ostream &,const LineCoverageStats & Line,unsigned)8920b57cec5SDimitry Andric void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
8930b57cec5SDimitry Andric                                                  const LineCoverageStats &Line,
8940b57cec5SDimitry Andric                                                  unsigned) {
8950b57cec5SDimitry Andric   // Region markers are rendered in-line using tooltips.
8960b57cec5SDimitry Andric }
8970b57cec5SDimitry Andric 
renderExpansionSite(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned ViewDepth)8980b57cec5SDimitry Andric void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L,
8990b57cec5SDimitry Andric                                                  const LineCoverageStats &LCS,
9000b57cec5SDimitry Andric                                                  unsigned ExpansionCol,
9010b57cec5SDimitry Andric                                                  unsigned ViewDepth) {
9020b57cec5SDimitry Andric   // Render the line containing the expansion site. No extra formatting needed.
9030b57cec5SDimitry Andric   renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
9040b57cec5SDimitry Andric }
9050b57cec5SDimitry Andric 
renderExpansionView(raw_ostream & OS,ExpansionView & ESV,unsigned ViewDepth)9060b57cec5SDimitry Andric void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
9070b57cec5SDimitry Andric                                                  ExpansionView &ESV,
9080b57cec5SDimitry Andric                                                  unsigned ViewDepth) {
9090b57cec5SDimitry Andric   OS << BeginExpansionDiv;
9100b57cec5SDimitry Andric   ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
9110b57cec5SDimitry Andric                   /*ShowTitle=*/false, ViewDepth + 1);
9120b57cec5SDimitry Andric   OS << EndExpansionDiv;
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
renderBranchView(raw_ostream & OS,BranchView & BRV,unsigned ViewDepth)915e8d8bef9SDimitry Andric void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV,
916e8d8bef9SDimitry Andric                                               unsigned ViewDepth) {
917e8d8bef9SDimitry Andric   // Render the child subview.
918e8d8bef9SDimitry Andric   if (getOptions().Debug)
919e8d8bef9SDimitry Andric     errs() << "Branch at line " << BRV.getLine() << '\n';
920e8d8bef9SDimitry Andric 
921e8d8bef9SDimitry Andric   OS << BeginExpansionDiv;
922e8d8bef9SDimitry Andric   OS << BeginPre;
923e8d8bef9SDimitry Andric   for (const auto &R : BRV.Regions) {
924e8d8bef9SDimitry Andric     // Calculate TruePercent and False Percent.
925e8d8bef9SDimitry Andric     double TruePercent = 0.0;
926e8d8bef9SDimitry Andric     double FalsePercent = 0.0;
927c9157d92SDimitry Andric     // FIXME: It may overflow when the data is too large, but I have not
928c9157d92SDimitry Andric     // encountered it in actual use, and not sure whether to use __uint128_t.
929c9157d92SDimitry Andric     uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
930e8d8bef9SDimitry Andric 
931e8d8bef9SDimitry Andric     if (!getOptions().ShowBranchCounts && Total != 0) {
932e8d8bef9SDimitry Andric       TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
933e8d8bef9SDimitry Andric       FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
934e8d8bef9SDimitry Andric     }
935e8d8bef9SDimitry Andric 
936e8d8bef9SDimitry Andric     // Display Line + Column.
937e8d8bef9SDimitry Andric     std::string LineNoStr = utostr(uint64_t(R.LineStart));
938e8d8bef9SDimitry Andric     std::string ColNoStr = utostr(uint64_t(R.ColumnStart));
939e8d8bef9SDimitry Andric     std::string TargetName = "L" + LineNoStr;
940e8d8bef9SDimitry Andric 
941e8d8bef9SDimitry Andric     OS << "  Branch (";
942e8d8bef9SDimitry Andric     OS << tag("span",
943e8d8bef9SDimitry Andric               a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr),
944e8d8bef9SDimitry Andric                 TargetName),
945e8d8bef9SDimitry Andric               "line-number") +
946e8d8bef9SDimitry Andric               "): [";
947e8d8bef9SDimitry Andric 
948e8d8bef9SDimitry Andric     if (R.Folded) {
949e8d8bef9SDimitry Andric       OS << "Folded - Ignored]\n";
950e8d8bef9SDimitry Andric       continue;
951e8d8bef9SDimitry Andric     }
952e8d8bef9SDimitry Andric 
953e8d8bef9SDimitry Andric     // Display TrueCount or TruePercent.
954e8d8bef9SDimitry Andric     std::string TrueColor = R.ExecutionCount ? "None" : "red";
955e8d8bef9SDimitry Andric     std::string TrueCovClass =
956e8d8bef9SDimitry Andric         (R.ExecutionCount > 0) ? "covered-line" : "uncovered-line";
957e8d8bef9SDimitry Andric 
958e8d8bef9SDimitry Andric     OS << tag("span", "True", TrueColor);
959e8d8bef9SDimitry Andric     OS << ": ";
960e8d8bef9SDimitry Andric     if (getOptions().ShowBranchCounts)
961e8d8bef9SDimitry Andric       OS << tag("span", formatCount(R.ExecutionCount), TrueCovClass) << ", ";
962e8d8bef9SDimitry Andric     else
963e8d8bef9SDimitry Andric       OS << format("%0.2f", TruePercent) << "%, ";
964e8d8bef9SDimitry Andric 
965e8d8bef9SDimitry Andric     // Display FalseCount or FalsePercent.
966e8d8bef9SDimitry Andric     std::string FalseColor = R.FalseExecutionCount ? "None" : "red";
967e8d8bef9SDimitry Andric     std::string FalseCovClass =
968e8d8bef9SDimitry Andric         (R.FalseExecutionCount > 0) ? "covered-line" : "uncovered-line";
969e8d8bef9SDimitry Andric 
970e8d8bef9SDimitry Andric     OS << tag("span", "False", FalseColor);
971e8d8bef9SDimitry Andric     OS << ": ";
972e8d8bef9SDimitry Andric     if (getOptions().ShowBranchCounts)
973e8d8bef9SDimitry Andric       OS << tag("span", formatCount(R.FalseExecutionCount), FalseCovClass);
974e8d8bef9SDimitry Andric     else
975e8d8bef9SDimitry Andric       OS << format("%0.2f", FalsePercent) << "%";
976e8d8bef9SDimitry Andric 
977e8d8bef9SDimitry Andric     OS << "]\n";
978e8d8bef9SDimitry Andric   }
979e8d8bef9SDimitry Andric   OS << EndPre;
980e8d8bef9SDimitry Andric   OS << EndExpansionDiv;
981e8d8bef9SDimitry Andric }
982e8d8bef9SDimitry Andric 
renderMCDCView(raw_ostream & OS,MCDCView & MRV,unsigned ViewDepth)983c9157d92SDimitry Andric void SourceCoverageViewHTML::renderMCDCView(raw_ostream &OS, MCDCView &MRV,
984c9157d92SDimitry Andric                                             unsigned ViewDepth) {
985c9157d92SDimitry Andric   for (auto &Record : MRV.Records) {
986c9157d92SDimitry Andric     OS << BeginExpansionDiv;
987c9157d92SDimitry Andric     OS << BeginPre;
988c9157d92SDimitry Andric     OS << "  MC/DC Decision Region (";
989c9157d92SDimitry Andric 
990c9157d92SDimitry Andric     // Display Line + Column information.
991c9157d92SDimitry Andric     const CounterMappingRegion &DecisionRegion = Record.getDecisionRegion();
992c9157d92SDimitry Andric     std::string LineNoStr = Twine(DecisionRegion.LineStart).str();
993c9157d92SDimitry Andric     std::string ColNoStr = Twine(DecisionRegion.ColumnStart).str();
994c9157d92SDimitry Andric     std::string TargetName = "L" + LineNoStr;
995c9157d92SDimitry Andric     OS << tag("span",
996*471f3ba4SDimitry Andric               a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr)),
997c9157d92SDimitry Andric               "line-number") +
998c9157d92SDimitry Andric               ") to (";
999c9157d92SDimitry Andric     LineNoStr = utostr(uint64_t(DecisionRegion.LineEnd));
1000c9157d92SDimitry Andric     ColNoStr = utostr(uint64_t(DecisionRegion.ColumnEnd));
1001c9157d92SDimitry Andric     OS << tag("span",
1002*471f3ba4SDimitry Andric               a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr)),
1003c9157d92SDimitry Andric               "line-number") +
1004c9157d92SDimitry Andric               ")\n\n";
1005c9157d92SDimitry Andric 
1006c9157d92SDimitry Andric     // Display MC/DC Information.
1007c9157d92SDimitry Andric     OS << "  Number of Conditions: " << Record.getNumConditions() << "\n";
1008c9157d92SDimitry Andric     for (unsigned i = 0; i < Record.getNumConditions(); i++) {
1009c9157d92SDimitry Andric       OS << "     " << Record.getConditionHeaderString(i);
1010c9157d92SDimitry Andric     }
1011c9157d92SDimitry Andric     OS << "\n";
1012c9157d92SDimitry Andric     OS << "  Executed MC/DC Test Vectors:\n\n     ";
1013c9157d92SDimitry Andric     OS << Record.getTestVectorHeaderString();
1014c9157d92SDimitry Andric     for (unsigned i = 0; i < Record.getNumTestVectors(); i++)
1015c9157d92SDimitry Andric       OS << Record.getTestVectorString(i);
1016c9157d92SDimitry Andric     OS << "\n";
1017c9157d92SDimitry Andric     for (unsigned i = 0; i < Record.getNumConditions(); i++)
1018c9157d92SDimitry Andric       OS << Record.getConditionCoverageString(i);
1019c9157d92SDimitry Andric     OS << "  MC/DC Coverage for Expression: ";
1020c9157d92SDimitry Andric     OS << format("%0.2f", Record.getPercentCovered()) << "%\n";
1021c9157d92SDimitry Andric     OS << EndPre;
1022c9157d92SDimitry Andric     OS << EndExpansionDiv;
1023c9157d92SDimitry Andric   }
1024c9157d92SDimitry Andric   return;
1025c9157d92SDimitry Andric }
1026c9157d92SDimitry Andric 
renderInstantiationView(raw_ostream & OS,InstantiationView & ISV,unsigned ViewDepth)10270b57cec5SDimitry Andric void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
10280b57cec5SDimitry Andric                                                      InstantiationView &ISV,
10290b57cec5SDimitry Andric                                                      unsigned ViewDepth) {
10300b57cec5SDimitry Andric   OS << BeginExpansionDiv;
10310b57cec5SDimitry Andric   if (!ISV.View)
10320b57cec5SDimitry Andric     OS << BeginSourceNameDiv
10330b57cec5SDimitry Andric        << tag("pre",
10340b57cec5SDimitry Andric               escape("Unexecuted instantiation: " + ISV.FunctionName.str(),
10350b57cec5SDimitry Andric                      getOptions()))
10360b57cec5SDimitry Andric        << EndSourceNameDiv;
10370b57cec5SDimitry Andric   else
10380b57cec5SDimitry Andric     ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
10390b57cec5SDimitry Andric                     /*ShowTitle=*/false, ViewDepth);
10400b57cec5SDimitry Andric   OS << EndExpansionDiv;
10410b57cec5SDimitry Andric }
10420b57cec5SDimitry Andric 
renderTitle(raw_ostream & OS,StringRef Title)10430b57cec5SDimitry Andric void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) {
10440b57cec5SDimitry Andric   if (getOptions().hasProjectTitle())
10450b57cec5SDimitry Andric     OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions()));
10460b57cec5SDimitry Andric   OS << tag(ReportTitleTag, escape(Title, getOptions()));
10470b57cec5SDimitry Andric   if (getOptions().hasCreatedTime())
10480b57cec5SDimitry Andric     OS << tag(CreatedTimeTag,
10490b57cec5SDimitry Andric               escape(getOptions().CreatedTimeStr, getOptions()));
10500b57cec5SDimitry Andric }
10510b57cec5SDimitry Andric 
renderTableHeader(raw_ostream & OS,unsigned FirstUncoveredLineNo,unsigned ViewDepth)10520b57cec5SDimitry Andric void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS,
10530b57cec5SDimitry Andric                                                unsigned FirstUncoveredLineNo,
10540b57cec5SDimitry Andric                                                unsigned ViewDepth) {
10550b57cec5SDimitry Andric   std::string SourceLabel;
10560b57cec5SDimitry Andric   if (FirstUncoveredLineNo == 0) {
10570b57cec5SDimitry Andric     SourceLabel = tag("td", tag("pre", "Source"));
10580b57cec5SDimitry Andric   } else {
10590b57cec5SDimitry Andric     std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo));
10600b57cec5SDimitry Andric     SourceLabel =
10610b57cec5SDimitry Andric         tag("td", tag("pre", "Source (" +
10620b57cec5SDimitry Andric                                  a(LinkTarget, "jump to first uncovered line") +
10630b57cec5SDimitry Andric                                  ")"));
10640b57cec5SDimitry Andric   }
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric   renderLinePrefix(OS, ViewDepth);
10670b57cec5SDimitry Andric   OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count"))
10680b57cec5SDimitry Andric      << SourceLabel;
10690b57cec5SDimitry Andric   renderLineSuffix(OS, ViewDepth);
10700b57cec5SDimitry Andric }
1071