14c01092aSVedant Kumar //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===//
24c01092aSVedant Kumar //
34c01092aSVedant Kumar //                     The LLVM Compiler Infrastructure
44c01092aSVedant Kumar //
54c01092aSVedant Kumar // This file is distributed under the University of Illinois Open Source
64c01092aSVedant Kumar // License. See LICENSE.TXT for details.
74c01092aSVedant Kumar //
84c01092aSVedant Kumar //===----------------------------------------------------------------------===//
94c01092aSVedant Kumar ///
104c01092aSVedant Kumar /// \file This file implements the html coverage renderer.
114c01092aSVedant Kumar ///
124c01092aSVedant Kumar //===----------------------------------------------------------------------===//
134c01092aSVedant Kumar 
14a59334daSVedant Kumar #include "CoverageReport.h"
154c01092aSVedant Kumar #include "SourceCoverageViewHTML.h"
164c01092aSVedant Kumar #include "llvm/ADT/Optional.h"
174c01092aSVedant Kumar #include "llvm/ADT/SmallString.h"
184c01092aSVedant Kumar #include "llvm/ADT/StringExtras.h"
1984dc971eSYing Yi #include "llvm/Support/FileSystem.h"
20a59334daSVedant Kumar #include "llvm/Support/Format.h"
214c01092aSVedant Kumar #include "llvm/Support/Path.h"
224c01092aSVedant Kumar 
234c01092aSVedant Kumar using namespace llvm;
244c01092aSVedant Kumar 
254c01092aSVedant Kumar namespace {
264c01092aSVedant Kumar 
27c076c490SVedant Kumar // Return a string with the special characters in \p Str escaped.
280ef31b79SYing Yi std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
29c076c490SVedant Kumar   std::string Result;
300ef31b79SYing Yi   unsigned ColNum = 0; // Record the column number.
31c076c490SVedant Kumar   for (char C : Str) {
320ef31b79SYing Yi     ++ColNum;
33c076c490SVedant Kumar     if (C == '&')
34c076c490SVedant Kumar       Result += "&";
35c076c490SVedant Kumar     else if (C == '<')
36c076c490SVedant Kumar       Result += "&lt;";
37c076c490SVedant Kumar     else if (C == '>')
38c076c490SVedant Kumar       Result += "&gt;";
39c076c490SVedant Kumar     else if (C == '\"')
40c076c490SVedant Kumar       Result += "&quot;";
410ef31b79SYing Yi     else if (C == '\n' || C == '\r') {
420ef31b79SYing Yi       Result += C;
430ef31b79SYing Yi       ColNum = 0;
440ef31b79SYing Yi     } else if (C == '\t') {
450ef31b79SYing Yi       // Replace '\t' with TabSize spaces.
460ef31b79SYing Yi       unsigned NumSpaces = Opts.TabSize - (--ColNum % Opts.TabSize);
470ef31b79SYing Yi       for (unsigned I = 0; I < NumSpaces; ++I)
480ef31b79SYing Yi         Result += "&nbsp;";
490ef31b79SYing Yi       ColNum += NumSpaces;
500ef31b79SYing Yi     } else
51c076c490SVedant Kumar       Result += C;
52c076c490SVedant Kumar   }
53c076c490SVedant Kumar   return Result;
54c076c490SVedant Kumar }
55c076c490SVedant Kumar 
56c076c490SVedant Kumar // Create a \p Name tag around \p Str, and optionally set its \p ClassName.
57c076c490SVedant Kumar std::string tag(const std::string &Name, const std::string &Str,
58c076c490SVedant Kumar                 const std::string &ClassName = "") {
59c076c490SVedant Kumar   std::string Tag = "<" + Name;
60c076c490SVedant Kumar   if (ClassName != "")
61c076c490SVedant Kumar     Tag += " class='" + ClassName + "'";
62c076c490SVedant Kumar   return Tag + ">" + Str + "</" + Name + ">";
63c076c490SVedant Kumar }
64c076c490SVedant Kumar 
65c076c490SVedant Kumar // Create an anchor to \p Link with the label \p Str.
66c076c490SVedant Kumar std::string a(const std::string &Link, const std::string &Str,
67c076c490SVedant Kumar               const std::string &TargetType = "href") {
68c076c490SVedant Kumar   return "<a " + TargetType + "='" + Link + "'>" + Str + "</a>";
69c076c490SVedant Kumar }
70c076c490SVedant Kumar 
714c01092aSVedant Kumar const char *BeginHeader =
724c01092aSVedant Kumar   "<head>"
734c01092aSVedant Kumar     "<meta name='viewport' content='width=device-width,initial-scale=1'>"
744c01092aSVedant Kumar     "<meta charset='UTF-8'>";
754c01092aSVedant Kumar 
764c01092aSVedant Kumar const char *CSSForCoverage =
77c076c490SVedant Kumar     R"(.red {
78a59334daSVedant Kumar   background-color: #ffd0d0;
794c01092aSVedant Kumar }
804c01092aSVedant Kumar .cyan {
814c01092aSVedant Kumar   background-color: cyan;
824c01092aSVedant Kumar }
834c01092aSVedant Kumar body {
844c01092aSVedant Kumar   font-family: -apple-system, sans-serif;
854c01092aSVedant Kumar }
864c01092aSVedant Kumar pre {
874c01092aSVedant Kumar   margin-top: 0px !important;
884c01092aSVedant Kumar   margin-bottom: 0px !important;
894c01092aSVedant Kumar }
904c01092aSVedant Kumar .source-name-title {
914c01092aSVedant Kumar   padding: 5px 10px;
924c01092aSVedant Kumar   border-bottom: 1px solid #dbdbdb;
934c01092aSVedant Kumar   background-color: #eee;
9484dc971eSYing Yi   line-height: 35px;
954c01092aSVedant Kumar }
964c01092aSVedant Kumar .centered {
974c01092aSVedant Kumar   display: table;
9884dc971eSYing Yi   margin-left: left;
994c01092aSVedant Kumar   margin-right: auto;
1004c01092aSVedant Kumar   border: 1px solid #dbdbdb;
1014c01092aSVedant Kumar   border-radius: 3px;
1024c01092aSVedant Kumar }
1034c01092aSVedant Kumar .expansion-view {
1044c01092aSVedant Kumar   background-color: rgba(0, 0, 0, 0);
1054c01092aSVedant Kumar   margin-left: 0px;
1064c01092aSVedant Kumar   margin-top: 5px;
1074c01092aSVedant Kumar   margin-right: 5px;
1084c01092aSVedant Kumar   margin-bottom: 5px;
1094c01092aSVedant Kumar   border: 1px solid #dbdbdb;
1104c01092aSVedant Kumar   border-radius: 3px;
1114c01092aSVedant Kumar }
1124c01092aSVedant Kumar table {
1134c01092aSVedant Kumar   border-collapse: collapse;
1144c01092aSVedant Kumar }
115a59334daSVedant Kumar .light-row {
116a59334daSVedant Kumar   background: #ffffff;
117a59334daSVedant Kumar   border: 1px solid #dbdbdb;
118a59334daSVedant Kumar }
119a59334daSVedant Kumar .column-entry {
120a59334daSVedant Kumar   text-align: right;
121a59334daSVedant Kumar }
1227b9e9bb4SVedant Kumar .column-entry-left {
1237b9e9bb4SVedant Kumar   text-align: left;
1247b9e9bb4SVedant Kumar }
125a59334daSVedant Kumar .column-entry-yellow {
126a59334daSVedant Kumar   text-align: right;
127a59334daSVedant Kumar   background-color: #ffffd0;
128a59334daSVedant Kumar }
129a59334daSVedant Kumar .column-entry-red {
130a59334daSVedant Kumar   text-align: right;
131a59334daSVedant Kumar   background-color: #ffd0d0;
132a59334daSVedant Kumar }
133a59334daSVedant Kumar .column-entry-green {
134a59334daSVedant Kumar   text-align: right;
135a59334daSVedant Kumar   background-color: #d0ffd0;
136a59334daSVedant Kumar }
1374c01092aSVedant Kumar .line-number {
1384c01092aSVedant Kumar   text-align: right;
1394c01092aSVedant Kumar   color: #aaa;
1404c01092aSVedant Kumar }
1414c01092aSVedant Kumar .covered-line {
1424c01092aSVedant Kumar   text-align: right;
1434c01092aSVedant Kumar   color: #0080ff;
1444c01092aSVedant Kumar }
1454c01092aSVedant Kumar .uncovered-line {
1464c01092aSVedant Kumar   text-align: right;
1474c01092aSVedant Kumar   color: #ff3300;
1484c01092aSVedant Kumar }
1494c01092aSVedant Kumar .tooltip {
1504c01092aSVedant Kumar   position: relative;
1514c01092aSVedant Kumar   display: inline;
1524c01092aSVedant Kumar   background-color: #b3e6ff;
1534c01092aSVedant Kumar   text-decoration: none;
1544c01092aSVedant Kumar }
1554c01092aSVedant Kumar .tooltip span.tooltip-content {
1564c01092aSVedant Kumar   position: absolute;
1574c01092aSVedant Kumar   width: 100px;
1584c01092aSVedant Kumar   margin-left: -50px;
1594c01092aSVedant Kumar   color: #FFFFFF;
1604c01092aSVedant Kumar   background: #000000;
1614c01092aSVedant Kumar   height: 30px;
1624c01092aSVedant Kumar   line-height: 30px;
1634c01092aSVedant Kumar   text-align: center;
1644c01092aSVedant Kumar   visibility: hidden;
1654c01092aSVedant Kumar   border-radius: 6px;
1664c01092aSVedant Kumar }
1674c01092aSVedant Kumar .tooltip span.tooltip-content:after {
1684c01092aSVedant Kumar   content: '';
1694c01092aSVedant Kumar   position: absolute;
1704c01092aSVedant Kumar   top: 100%;
1714c01092aSVedant Kumar   left: 50%;
1724c01092aSVedant Kumar   margin-left: -8px;
1734c01092aSVedant Kumar   width: 0; height: 0;
1744c01092aSVedant Kumar   border-top: 8px solid #000000;
1754c01092aSVedant Kumar   border-right: 8px solid transparent;
1764c01092aSVedant Kumar   border-left: 8px solid transparent;
1774c01092aSVedant Kumar }
1784c01092aSVedant Kumar :hover.tooltip span.tooltip-content {
1794c01092aSVedant Kumar   visibility: visible;
1804c01092aSVedant Kumar   opacity: 0.8;
1814c01092aSVedant Kumar   bottom: 30px;
1824c01092aSVedant Kumar   left: 50%;
1834c01092aSVedant Kumar   z-index: 999;
1844c01092aSVedant Kumar }
1854c01092aSVedant Kumar th, td {
1864c01092aSVedant Kumar   vertical-align: top;
1874c01092aSVedant Kumar   padding: 2px 5px;
1884c01092aSVedant Kumar   border-collapse: collapse;
1894c01092aSVedant Kumar   border-right: solid 1px #eee;
1904c01092aSVedant Kumar   border-left: solid 1px #eee;
1914c01092aSVedant Kumar }
1924c01092aSVedant Kumar td:first-child {
1934c01092aSVedant Kumar   border-left: none;
1944c01092aSVedant Kumar }
1954c01092aSVedant Kumar td:last-child {
1964c01092aSVedant Kumar   border-right: none;
1974c01092aSVedant Kumar }
198c076c490SVedant Kumar )";
1994c01092aSVedant Kumar 
2004c01092aSVedant Kumar const char *EndHeader = "</head>";
2014c01092aSVedant Kumar 
2024c01092aSVedant Kumar const char *BeginCenteredDiv = "<div class='centered'>";
2034c01092aSVedant Kumar 
2044c01092aSVedant Kumar const char *EndCenteredDiv = "</div>";
2054c01092aSVedant Kumar 
2064c01092aSVedant Kumar const char *BeginSourceNameDiv = "<div class='source-name-title'>";
2074c01092aSVedant Kumar 
2084c01092aSVedant Kumar const char *EndSourceNameDiv = "</div>";
2094c01092aSVedant Kumar 
2104c01092aSVedant Kumar const char *BeginCodeTD = "<td class='code'>";
2114c01092aSVedant Kumar 
2124c01092aSVedant Kumar const char *EndCodeTD = "</td>";
2134c01092aSVedant Kumar 
2144c01092aSVedant Kumar const char *BeginPre = "<pre>";
2154c01092aSVedant Kumar 
2164c01092aSVedant Kumar const char *EndPre = "</pre>";
2174c01092aSVedant Kumar 
2184c01092aSVedant Kumar const char *BeginExpansionDiv = "<div class='expansion-view'>";
2194c01092aSVedant Kumar 
2204c01092aSVedant Kumar const char *EndExpansionDiv = "</div>";
2214c01092aSVedant Kumar 
2224c01092aSVedant Kumar const char *BeginTable = "<table>";
2234c01092aSVedant Kumar 
2244c01092aSVedant Kumar const char *EndTable = "</table>";
2254c01092aSVedant Kumar 
2267b9e9bb4SVedant Kumar const char *ProjectTitleTag = "h1";
22784dc971eSYing Yi 
2287b9e9bb4SVedant Kumar const char *ReportTitleTag = "h2";
22984dc971eSYing Yi 
2307b9e9bb4SVedant Kumar const char *CreatedTimeTag = "h4";
23184dc971eSYing Yi 
232c076c490SVedant Kumar std::string getPathToStyle(StringRef ViewPath) {
233c076c490SVedant Kumar   std::string PathToStyle = "";
234c076c490SVedant Kumar   std::string PathSep = sys::path::get_separator();
235c076c490SVedant Kumar   unsigned NumSeps = ViewPath.count(PathSep);
236c076c490SVedant Kumar   for (unsigned I = 0, E = NumSeps; I < E; ++I)
237c076c490SVedant Kumar     PathToStyle += ".." + PathSep;
238c076c490SVedant Kumar   return PathToStyle + "style.css";
239c076c490SVedant Kumar }
240c076c490SVedant Kumar 
2410ef31b79SYing Yi void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
2420ef31b79SYing Yi                  const std::string &PathToStyle = "") {
2434c01092aSVedant Kumar   OS << "<!doctype html>"
2444c01092aSVedant Kumar         "<html>"
245c076c490SVedant Kumar      << BeginHeader;
246c076c490SVedant Kumar 
247c076c490SVedant Kumar   // Link to a stylesheet if one is available. Otherwise, use the default style.
248c076c490SVedant Kumar   if (PathToStyle.empty())
249c076c490SVedant Kumar     OS << "<style>" << CSSForCoverage << "</style>";
250c076c490SVedant Kumar   else
2510ef31b79SYing Yi     OS << "<link rel='stylesheet' type='text/css' href='"
2520ef31b79SYing Yi        << escape(PathToStyle, Opts) << "'>";
253c076c490SVedant Kumar 
25484dc971eSYing Yi   OS << EndHeader << "<body>";
2554c01092aSVedant Kumar }
2564c01092aSVedant Kumar 
2574c01092aSVedant Kumar void emitEpilog(raw_ostream &OS) {
25884dc971eSYing Yi   OS << "</body>"
25984dc971eSYing Yi      << "</html>";
2604c01092aSVedant Kumar }
2614c01092aSVedant Kumar 
2624c01092aSVedant Kumar } // anonymous namespace
2634c01092aSVedant Kumar 
2644c01092aSVedant Kumar Expected<CoveragePrinter::OwnedStream>
2654c01092aSVedant Kumar CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
2664c01092aSVedant Kumar   auto OSOrErr = createOutputStream(Path, "html", InToplevel);
2674c01092aSVedant Kumar   if (!OSOrErr)
2684c01092aSVedant Kumar     return OSOrErr;
2694c01092aSVedant Kumar 
2704c01092aSVedant Kumar   OwnedStream OS = std::move(OSOrErr.get());
271c076c490SVedant Kumar 
272c076c490SVedant Kumar   if (!Opts.hasOutputDirectory()) {
2730ef31b79SYing Yi     emitPrelude(*OS.get(), Opts);
274c076c490SVedant Kumar   } else {
275c076c490SVedant Kumar     std::string ViewPath = getOutputPath(Path, "html", InToplevel);
2760ef31b79SYing Yi     emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
277c076c490SVedant Kumar   }
278c076c490SVedant Kumar 
2794c01092aSVedant Kumar   return std::move(OS);
2804c01092aSVedant Kumar }
2814c01092aSVedant Kumar 
2824c01092aSVedant Kumar void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
2834c01092aSVedant Kumar   emitEpilog(*OS.get());
2844c01092aSVedant Kumar }
2854c01092aSVedant Kumar 
286a59334daSVedant Kumar /// Emit column labels for the table in the index.
287a59334daSVedant Kumar static void emitColumnLabelsForIndex(raw_ostream &OS) {
288a59334daSVedant Kumar   SmallVector<std::string, 4> Columns;
2897b9e9bb4SVedant Kumar   Columns.emplace_back(tag("td", "Filename", "column-entry-left"));
290016111f7SVedant Kumar   for (const char *Label : {"Function Coverage", "Instantiation Coverage",
291016111f7SVedant Kumar                             "Line Coverage", "Region Coverage"})
292a59334daSVedant Kumar     Columns.emplace_back(tag("td", Label, "column-entry"));
293a59334daSVedant Kumar   OS << tag("tr", join(Columns.begin(), Columns.end(), ""));
294a59334daSVedant Kumar }
295a59334daSVedant Kumar 
296a59334daSVedant Kumar /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is
297a59334daSVedant Kumar /// false, link the summary to \p SF.
298a59334daSVedant Kumar void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF,
299a59334daSVedant Kumar                                           const FileCoverageSummary &FCS,
300a59334daSVedant Kumar                                           bool IsTotals) const {
301a59334daSVedant Kumar   SmallVector<std::string, 4> Columns;
302a59334daSVedant Kumar 
303a59334daSVedant Kumar   // Format a coverage triple and add the result to the list of columns.
304a59334daSVedant Kumar   auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total,
305a59334daSVedant Kumar                                               float Pctg) {
306a59334daSVedant Kumar     std::string S;
307a59334daSVedant Kumar     {
308a59334daSVedant Kumar       raw_string_ostream RSO{S};
309a59334daSVedant Kumar       RSO << format("%*.2f", 7, Pctg) << "% (" << Hit << '/' << Total << ')';
310a59334daSVedant Kumar     }
311a59334daSVedant Kumar     const char *CellClass = "column-entry-yellow";
312a59334daSVedant Kumar     if (Pctg < 80.0)
313a59334daSVedant Kumar       CellClass = "column-entry-red";
314a59334daSVedant Kumar     else if (Hit == Total)
315a59334daSVedant Kumar       CellClass = "column-entry-green";
3167b9e9bb4SVedant Kumar     Columns.emplace_back(tag("td", tag("pre", S), CellClass));
317a59334daSVedant Kumar   };
318a59334daSVedant Kumar 
319a59334daSVedant Kumar   // Simplify the display file path, and wrap it in a link if requested.
320a59334daSVedant Kumar   std::string Filename;
321a59334daSVedant Kumar   SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name));
322a59334daSVedant Kumar   sys::path::remove_dots(LinkTextStr, /*remove_dot_dots=*/true);
323a59334daSVedant Kumar   sys::path::native(LinkTextStr);
324a59334daSVedant Kumar   std::string LinkText = escape(LinkTextStr, Opts);
325a59334daSVedant Kumar   if (IsTotals) {
326a59334daSVedant Kumar     Filename = LinkText;
327a59334daSVedant Kumar   } else {
328a59334daSVedant Kumar     std::string LinkTarget =
329a59334daSVedant Kumar         escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
330a59334daSVedant Kumar     Filename = a(LinkTarget, LinkText);
331a59334daSVedant Kumar   }
332a59334daSVedant Kumar 
3337b9e9bb4SVedant Kumar   Columns.emplace_back(tag("td", tag("pre", Filename)));
334a59334daSVedant Kumar   AddCoverageTripleToColumn(FCS.FunctionCoverage.Executed,
335a59334daSVedant Kumar                             FCS.FunctionCoverage.NumFunctions,
336a59334daSVedant Kumar                             FCS.FunctionCoverage.getPercentCovered());
337016111f7SVedant Kumar   AddCoverageTripleToColumn(FCS.InstantiationCoverage.Executed,
338016111f7SVedant Kumar                             FCS.InstantiationCoverage.NumFunctions,
339016111f7SVedant Kumar                             FCS.InstantiationCoverage.getPercentCovered());
340673ad727SVedant Kumar   AddCoverageTripleToColumn(FCS.LineCoverage.Covered, FCS.LineCoverage.NumLines,
341673ad727SVedant Kumar                             FCS.LineCoverage.getPercentCovered());
342673ad727SVedant Kumar   AddCoverageTripleToColumn(FCS.RegionCoverage.Covered,
343673ad727SVedant Kumar                             FCS.RegionCoverage.NumRegions,
344673ad727SVedant Kumar                             FCS.RegionCoverage.getPercentCovered());
345a59334daSVedant Kumar 
346a59334daSVedant Kumar   OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row");
347a59334daSVedant Kumar }
348a59334daSVedant Kumar 
349a59334daSVedant Kumar Error CoveragePrinterHTML::createIndexFile(
350a59334daSVedant Kumar     ArrayRef<StringRef> SourceFiles,
351a59334daSVedant Kumar     const coverage::CoverageMapping &Coverage) {
352a59334daSVedant Kumar   // Emit the default stylesheet.
353a59334daSVedant Kumar   auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true);
354a59334daSVedant Kumar   if (Error E = CSSOrErr.takeError())
355a59334daSVedant Kumar     return E;
356a59334daSVedant Kumar 
357a59334daSVedant Kumar   OwnedStream CSS = std::move(CSSOrErr.get());
358a59334daSVedant Kumar   CSS->operator<<(CSSForCoverage);
359a59334daSVedant Kumar 
360a59334daSVedant Kumar   // Emit a file index along with some coverage statistics.
3614c01092aSVedant Kumar   auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
3624c01092aSVedant Kumar   if (Error E = OSOrErr.takeError())
3634c01092aSVedant Kumar     return E;
3644c01092aSVedant Kumar   auto OS = std::move(OSOrErr.get());
3654c01092aSVedant Kumar   raw_ostream &OSRef = *OS.get();
3664c01092aSVedant Kumar 
367127d0502SVedant Kumar   assert(Opts.hasOutputDirectory() && "No output directory for index file");
3680ef31b79SYing Yi   emitPrelude(OSRef, Opts, getPathToStyle(""));
369a59334daSVedant Kumar 
370a59334daSVedant Kumar   // Emit some basic information about the coverage report.
37184dc971eSYing Yi   if (Opts.hasProjectTitle())
3727b9e9bb4SVedant Kumar     OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts));
3737b9e9bb4SVedant Kumar   OSRef << tag(ReportTitleTag, "Coverage Report");
37484dc971eSYing Yi   if (Opts.hasCreatedTime())
3757b9e9bb4SVedant Kumar     OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts));
376a59334daSVedant Kumar 
377a59334daSVedant Kumar   // Emit a table containing links to reports for each file in the covmapping.
37884dc971eSYing Yi   OSRef << BeginCenteredDiv << BeginTable;
379a59334daSVedant Kumar   emitColumnLabelsForIndex(OSRef);
380a59334daSVedant Kumar   FileCoverageSummary Totals("TOTALS");
381*9cbf80afSVedant Kumar   auto FileReports =
382*9cbf80afSVedant Kumar       CoverageReport::prepareFileReports(Coverage, Totals, SourceFiles);
383a59334daSVedant Kumar   for (unsigned I = 0, E = FileReports.size(); I < E; ++I)
384a59334daSVedant Kumar     emitFileSummary(OSRef, SourceFiles[I], FileReports[I]);
385a59334daSVedant Kumar   emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true);
386544b1df6SYing Yi   OSRef << EndTable << EndCenteredDiv
387544b1df6SYing Yi         << tag("h5", escape(Opts.getLLVMVersionString(), Opts));
3884c01092aSVedant Kumar   emitEpilog(OSRef);
3894c01092aSVedant Kumar 
3904c01092aSVedant Kumar   return Error::success();
3914c01092aSVedant Kumar }
3924c01092aSVedant Kumar 
3934c01092aSVedant Kumar void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
3947b9e9bb4SVedant Kumar   OS << BeginCenteredDiv << BeginTable;
3954c01092aSVedant Kumar }
3964c01092aSVedant Kumar 
3974c01092aSVedant Kumar void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
39884a280adSVedant Kumar   OS << EndTable << EndCenteredDiv;
3994c01092aSVedant Kumar }
4004c01092aSVedant Kumar 
401b1c174aaSVedant Kumar void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) {
40284dc971eSYing Yi   OS << BeginSourceNameDiv;
4030053c0b6SVedant Kumar   std::string ViewInfo = escape(
4040053c0b6SVedant Kumar       WholeFile ? getVerboseSourceName() : getSourceName(), getOptions());
4050053c0b6SVedant Kumar   OS << tag("pre", ViewInfo);
40684dc971eSYing Yi   OS << EndSourceNameDiv;
4074c01092aSVedant Kumar }
4084c01092aSVedant Kumar 
4094c01092aSVedant Kumar void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
4104c01092aSVedant Kumar   OS << "<tr>";
4114c01092aSVedant Kumar }
4124c01092aSVedant Kumar 
4134c01092aSVedant Kumar void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
4144c01092aSVedant Kumar   // If this view has sub-views, renderLine() cannot close the view's cell.
4154c01092aSVedant Kumar   // Take care of it here, after all sub-views have been rendered.
4164c01092aSVedant Kumar   if (hasSubViews())
4174c01092aSVedant Kumar     OS << EndCodeTD;
4184c01092aSVedant Kumar   OS << "</tr>";
4194c01092aSVedant Kumar }
4204c01092aSVedant Kumar 
4214c01092aSVedant Kumar void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
4224c01092aSVedant Kumar   // The table-based output makes view dividers unnecessary.
4234c01092aSVedant Kumar }
4244c01092aSVedant Kumar 
4254c01092aSVedant Kumar void SourceCoverageViewHTML::renderLine(
4264c01092aSVedant Kumar     raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
4274c01092aSVedant Kumar     CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned) {
4284c01092aSVedant Kumar   StringRef Line = L.Line;
429fc07e8b4SVedant Kumar   unsigned LineNo = L.LineNo;
4304c01092aSVedant Kumar 
4314c01092aSVedant Kumar   // Steps for handling text-escaping, highlighting, and tooltip creation:
4324c01092aSVedant Kumar   //
4334c01092aSVedant Kumar   // 1. Split the line into N+1 snippets, where N = |Segments|. The first
4344c01092aSVedant Kumar   //    snippet starts from Col=1 and ends at the start of the first segment.
4354c01092aSVedant Kumar   //    The last snippet starts at the last mapped column in the line and ends
4364c01092aSVedant Kumar   //    at the end of the line. Both are required but may be empty.
4374c01092aSVedant Kumar 
4384c01092aSVedant Kumar   SmallVector<std::string, 8> Snippets;
4394c01092aSVedant Kumar 
4404c01092aSVedant Kumar   unsigned LCol = 1;
4414c01092aSVedant Kumar   auto Snip = [&](unsigned Start, unsigned Len) {
4424c01092aSVedant Kumar     Snippets.push_back(Line.substr(Start, Len));
4434c01092aSVedant Kumar     LCol += Len;
4444c01092aSVedant Kumar   };
4454c01092aSVedant Kumar 
4464c01092aSVedant Kumar   Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
4474c01092aSVedant Kumar 
448c236e5a0SVedant Kumar   for (unsigned I = 1, E = Segments.size(); I < E; ++I)
4494c01092aSVedant Kumar     Snip(LCol - 1, Segments[I]->Col - LCol);
4504c01092aSVedant Kumar 
4514c01092aSVedant Kumar   // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
4524c01092aSVedant Kumar   Snip(LCol - 1, Line.size() + 1 - LCol);
4534c01092aSVedant Kumar 
4544c01092aSVedant Kumar   // 2. Escape all of the snippets.
4554c01092aSVedant Kumar 
4564c01092aSVedant Kumar   for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
4570ef31b79SYing Yi     Snippets[I] = escape(Snippets[I], getOptions());
4584c01092aSVedant Kumar 
459fc07e8b4SVedant Kumar   // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
460fc07e8b4SVedant Kumar   //    1 to set the highlight for snippet 2, segment 2 to set the highlight for
461fc07e8b4SVedant Kumar   //    snippet 3, and so on.
4624c01092aSVedant Kumar 
4634c01092aSVedant Kumar   Optional<std::string> Color;
4640b33f2c0SVedant Kumar   SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
465fc07e8b4SVedant Kumar   auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
4660b33f2c0SVedant Kumar     if (getOptions().Debug)
467fc07e8b4SVedant Kumar       HighlightedRanges.emplace_back(LC, RC);
4684c01092aSVedant Kumar     return tag("span", Snippet, Color.getValue());
4694c01092aSVedant Kumar   };
4704c01092aSVedant Kumar 
4714c01092aSVedant Kumar   auto CheckIfUncovered = [](const coverage::CoverageSegment *S) {
4720b33f2c0SVedant Kumar     return S && S->HasCount && S->Count == 0;
4734c01092aSVedant Kumar   };
4744c01092aSVedant Kumar 
4750b33f2c0SVedant Kumar   if (CheckIfUncovered(WrappedSegment)) {
4764c01092aSVedant Kumar     Color = "red";
4770b33f2c0SVedant Kumar     if (!Snippets[0].empty())
4780b33f2c0SVedant Kumar       Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
4794c01092aSVedant Kumar   }
4804c01092aSVedant Kumar 
481fc07e8b4SVedant Kumar   for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
4824c01092aSVedant Kumar     const auto *CurSeg = Segments[I];
4834c01092aSVedant Kumar     if (CurSeg->Col == ExpansionCol)
4844c01092aSVedant Kumar       Color = "cyan";
4854c01092aSVedant Kumar     else if (CheckIfUncovered(CurSeg))
4864c01092aSVedant Kumar       Color = "red";
4874c01092aSVedant Kumar     else
4884c01092aSVedant Kumar       Color = None;
4894c01092aSVedant Kumar 
4904c01092aSVedant Kumar     if (Color.hasValue())
491fc07e8b4SVedant Kumar       Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col,
492fc07e8b4SVedant Kumar                                   CurSeg->Col + Snippets[I + 1].size());
493fc07e8b4SVedant Kumar   }
494fc07e8b4SVedant Kumar 
495fc07e8b4SVedant Kumar   if (Color.hasValue() && Segments.empty())
4960b33f2c0SVedant Kumar     Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
497fc07e8b4SVedant Kumar 
498fc07e8b4SVedant Kumar   if (getOptions().Debug) {
499fc07e8b4SVedant Kumar     for (const auto &Range : HighlightedRanges) {
5000b33f2c0SVedant Kumar       errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
501fc07e8b4SVedant Kumar       if (Range.second == 0)
502fc07e8b4SVedant Kumar         errs() << "?";
503fc07e8b4SVedant Kumar       else
5040b33f2c0SVedant Kumar         errs() << Range.second;
505fc07e8b4SVedant Kumar       errs() << "\n";
506fc07e8b4SVedant Kumar     }
5074c01092aSVedant Kumar   }
5084c01092aSVedant Kumar 
5094c01092aSVedant Kumar   // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
5104c01092aSVedant Kumar   //    sub-line region count tooltips if needed.
5114c01092aSVedant Kumar 
5124c01092aSVedant Kumar   bool HasMultipleRegions = [&] {
5134c01092aSVedant Kumar     unsigned RegionCount = 0;
5144c01092aSVedant Kumar     for (const auto *S : Segments)
5154c01092aSVedant Kumar       if (S->HasCount && S->IsRegionEntry)
5164c01092aSVedant Kumar         if (++RegionCount > 1)
5174c01092aSVedant Kumar           return true;
5184c01092aSVedant Kumar     return false;
5194c01092aSVedant Kumar   }();
5204c01092aSVedant Kumar 
5214c01092aSVedant Kumar   if (shouldRenderRegionMarkers(HasMultipleRegions)) {
5224c01092aSVedant Kumar     for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
5234c01092aSVedant Kumar       const auto *CurSeg = Segments[I];
5244c01092aSVedant Kumar       if (!CurSeg->IsRegionEntry || !CurSeg->HasCount)
5254c01092aSVedant Kumar         continue;
5264c01092aSVedant Kumar 
5274c01092aSVedant Kumar       Snippets[I + 1] =
5284c01092aSVedant Kumar           tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
5294c01092aSVedant Kumar                                            "tooltip-content"),
5304c01092aSVedant Kumar               "tooltip");
5314c01092aSVedant Kumar     }
5324c01092aSVedant Kumar   }
5334c01092aSVedant Kumar 
5344c01092aSVedant Kumar   OS << BeginCodeTD;
5354c01092aSVedant Kumar   OS << BeginPre;
5364c01092aSVedant Kumar   for (const auto &Snippet : Snippets)
5374c01092aSVedant Kumar     OS << Snippet;
5384c01092aSVedant Kumar   OS << EndPre;
5394c01092aSVedant Kumar 
5404c01092aSVedant Kumar   // If there are no sub-views left to attach to this cell, end the cell.
5414c01092aSVedant Kumar   // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
5424c01092aSVedant Kumar   if (!hasSubViews())
5434c01092aSVedant Kumar     OS << EndCodeTD;
5444c01092aSVedant Kumar }
5454c01092aSVedant Kumar 
5464c01092aSVedant Kumar void SourceCoverageViewHTML::renderLineCoverageColumn(
5474c01092aSVedant Kumar     raw_ostream &OS, const LineCoverageStats &Line) {
5484c01092aSVedant Kumar   std::string Count = "";
5494c01092aSVedant Kumar   if (Line.isMapped())
5504c01092aSVedant Kumar     Count = tag("pre", formatCount(Line.ExecutionCount));
5514c01092aSVedant Kumar   std::string CoverageClass =
5524c01092aSVedant Kumar       (Line.ExecutionCount > 0) ? "covered-line" : "uncovered-line";
5534c01092aSVedant Kumar   OS << tag("td", Count, CoverageClass);
5544c01092aSVedant Kumar }
5554c01092aSVedant Kumar 
5564c01092aSVedant Kumar void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
5574c01092aSVedant Kumar                                                     unsigned LineNo) {
5582e089362SVedant Kumar   std::string LineNoStr = utostr(uint64_t(LineNo));
5592e089362SVedant Kumar   OS << tag("td", a("L" + LineNoStr, tag("pre", LineNoStr), "name"),
5602e089362SVedant Kumar             "line-number");
5614c01092aSVedant Kumar }
5624c01092aSVedant Kumar 
5634c01092aSVedant Kumar void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
5644c01092aSVedant Kumar                                                  CoverageSegmentArray,
5654c01092aSVedant Kumar                                                  unsigned) {
5664c01092aSVedant Kumar   // Region markers are rendered in-line using tooltips.
5674c01092aSVedant Kumar }
5684c01092aSVedant Kumar 
5694c01092aSVedant Kumar void SourceCoverageViewHTML::renderExpansionSite(
5704c01092aSVedant Kumar     raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
5714c01092aSVedant Kumar     CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
5724c01092aSVedant Kumar   // Render the line containing the expansion site. No extra formatting needed.
5734c01092aSVedant Kumar   renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth);
5744c01092aSVedant Kumar }
5754c01092aSVedant Kumar 
5764c01092aSVedant Kumar void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
5774c01092aSVedant Kumar                                                  ExpansionView &ESV,
5784c01092aSVedant Kumar                                                  unsigned ViewDepth) {
5794c01092aSVedant Kumar   OS << BeginExpansionDiv;
5804c01092aSVedant Kumar   ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
5814c01092aSVedant Kumar                   ViewDepth + 1);
5824c01092aSVedant Kumar   OS << EndExpansionDiv;
5834c01092aSVedant Kumar }
5844c01092aSVedant Kumar 
5854c01092aSVedant Kumar void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
5864c01092aSVedant Kumar                                                      InstantiationView &ISV,
5874c01092aSVedant Kumar                                                      unsigned ViewDepth) {
5884c01092aSVedant Kumar   OS << BeginExpansionDiv;
589a8c396d9SVedant Kumar   if (!ISV.View)
590a8c396d9SVedant Kumar     OS << BeginSourceNameDiv
591a8c396d9SVedant Kumar        << tag("pre",
592a8c396d9SVedant Kumar               escape("Unexecuted instantiation: " + ISV.FunctionName.str(),
593a8c396d9SVedant Kumar                      getOptions()))
594a8c396d9SVedant Kumar        << EndSourceNameDiv;
595a8c396d9SVedant Kumar   else
596a8c396d9SVedant Kumar     ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
597a8c396d9SVedant Kumar                     ViewDepth);
5984c01092aSVedant Kumar   OS << EndExpansionDiv;
5994c01092aSVedant Kumar }
60084dc971eSYing Yi 
601b2edd11fSVedant Kumar void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) {
60284dc971eSYing Yi   if (getOptions().hasProjectTitle())
6037b9e9bb4SVedant Kumar     OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions()));
604b2edd11fSVedant Kumar   OS << tag(ReportTitleTag, escape(Title, getOptions()));
60584dc971eSYing Yi   if (getOptions().hasCreatedTime())
6067b9e9bb4SVedant Kumar     OS << tag(CreatedTimeTag,
6077b9e9bb4SVedant Kumar               escape(getOptions().CreatedTimeStr, getOptions()));
60884dc971eSYing Yi }
60984dc971eSYing Yi 
61084dc971eSYing Yi void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS,
611b1c174aaSVedant Kumar                                                unsigned FirstUncoveredLineNo,
61284dc971eSYing Yi                                                unsigned ViewDepth) {
613b1c174aaSVedant Kumar   std::string SourceLabel;
614408866caSVedant Kumar   if (FirstUncoveredLineNo == 0) {
615b1c174aaSVedant Kumar     SourceLabel = tag("td", tag("pre", "Source"));
616b1c174aaSVedant Kumar   } else {
617b1c174aaSVedant Kumar     std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo));
618b1c174aaSVedant Kumar     SourceLabel =
619b1c174aaSVedant Kumar         tag("td", tag("pre", "Source (" +
620b1c174aaSVedant Kumar                                  a(LinkTarget, "jump to first uncovered line") +
621b1c174aaSVedant Kumar                                  ")"));
622b1c174aaSVedant Kumar   }
623b1c174aaSVedant Kumar 
62484dc971eSYing Yi   renderLinePrefix(OS, ViewDepth);
62598ba34e5SVedant Kumar   OS << tag("td", tag("pre", "Line")) << tag("td", tag("pre", "Count"))
626b1c174aaSVedant Kumar      << SourceLabel;
62784dc971eSYing Yi   renderLineSuffix(OS, ViewDepth);
62884dc971eSYing Yi }
629