1 //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This file implements the html coverage renderer.
12 //===----------------------------------------------------------------------===//
14 #include "SourceCoverageViewHTML.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Path.h"
24 const char *BeginHeader =
26 "<meta name='viewport' content='width=device-width,initial-scale=1'>"
27 "<meta charset='UTF-8'>";
29 const char *CSSForCoverage =
34 background-color: #FFD0D0;
37 background-color: cyan;
40 background-color: black;
44 background-color: #98FFA6;
48 background-color: #F998FF;
52 font-family: -apple-system, sans-serif;
55 margin-top: 0px !important;
56 margin-bottom: 0px !important;
60 border-bottom: 1px solid #dbdbdb;
61 background-color: #eee;
67 border: 1px solid #dbdbdb;
71 background-color: rgba(0, 0, 0, 0);
76 border: 1px solid #dbdbdb;
80 border-collapse: collapse;
97 background-color: #b3e6ff;
98 text-decoration: none;
100 .tooltip span.tooltip-content {
112 .tooltip span.tooltip-content:after {
119 border-top: 8px solid #000000;
120 border-right: 8px solid transparent;
121 border-left: 8px solid transparent;
123 :hover.tooltip span.tooltip-content {
133 border-collapse: collapse;
134 border-right: solid 1px #eee;
135 border-left: solid 1px #eee;
147 const char *EndHeader = "</head>";
149 const char *BeginCenteredDiv = "<div class='centered'>";
151 const char *EndCenteredDiv = "</div>";
153 const char *BeginSourceNameDiv = "<div class='source-name-title'>";
155 const char *EndSourceNameDiv = "</div>";
157 const char *BeginCodeTD = "<td class='code'>";
159 const char *EndCodeTD = "</td>";
161 const char *BeginPre = "<pre>";
163 const char *EndPre = "</pre>";
165 const char *BeginExpansionDiv = "<div class='expansion-view'>";
167 const char *EndExpansionDiv = "</div>";
169 const char *BeginTable = "<table>";
171 const char *EndTable = "</table>";
173 void emitPrelude(raw_ostream &OS) {
174 OS << "<!doctype html>"
176 << BeginHeader << CSSForCoverage << EndHeader << "<body>"
180 void emitEpilog(raw_ostream &OS) {
181 OS << EndCenteredDiv << "</body>"
185 // Return a string with the special characters in \p Str escaped.
186 std::string escape(StringRef Str) {
203 // Create a \p Name tag around \p Str, and optionally set its \p ClassName.
204 std::string tag(const std::string &Name, const std::string &Str,
205 const std::string &ClassName = "") {
206 std::string Tag = "<" + Name;
208 Tag += " class='" + ClassName + "'";
209 return Tag + ">" + Str + "</" + Name + ">";
212 // Create an anchor to \p Link with the label \p Str.
213 std::string a(const std::string &Link, const std::string &Str) {
214 return "<a href='" + Link + "'>" + Str + "</a>";
217 } // anonymous namespace
219 Expected<CoveragePrinter::OwnedStream>
220 CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
221 auto OSOrErr = createOutputStream(Path, "html", InToplevel);
225 OwnedStream OS = std::move(OSOrErr.get());
226 emitPrelude(*OS.get());
227 return std::move(OS);
230 void CoveragePrinterHTML::closeViewFile(OwnedStream OS) {
231 emitEpilog(*OS.get());
234 Error CoveragePrinterHTML::createIndexFile(ArrayRef<StringRef> SourceFiles) {
235 auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true);
236 if (Error E = OSOrErr.takeError())
238 auto OS = std::move(OSOrErr.get());
239 raw_ostream &OSRef = *OS.get();
241 // Emit a table containing links to reports for each file in the covmapping.
243 OSRef << BeginSourceNameDiv << "Index" << EndSourceNameDiv;
245 for (StringRef SF : SourceFiles) {
246 std::string LinkText = escape(sys::path::relative_path(SF));
247 std::string LinkTarget =
248 escape(getOutputPath(SF, "html", /*InToplevel=*/false));
249 OSRef << tag("tr", tag("td", tag("pre", a(LinkTarget, LinkText), "code")));
254 return Error::success();
257 void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) {
261 void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
265 void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS) {
266 OS << BeginSourceNameDiv << tag("pre", escape(getSourceName()))
270 void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) {
274 void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) {
275 // If this view has sub-views, renderLine() cannot close the view's cell.
276 // Take care of it here, after all sub-views have been rendered.
282 void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) {
283 // The table-based output makes view dividers unnecessary.
286 void SourceCoverageViewHTML::renderLine(
287 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
288 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned) {
289 StringRef Line = L.Line;
291 // Steps for handling text-escaping, highlighting, and tooltip creation:
293 // 1. Split the line into N+1 snippets, where N = |Segments|. The first
294 // snippet starts from Col=1 and ends at the start of the first segment.
295 // The last snippet starts at the last mapped column in the line and ends
296 // at the end of the line. Both are required but may be empty.
298 SmallVector<std::string, 8> Snippets;
301 auto Snip = [&](unsigned Start, unsigned Len) {
302 assert(Start + Len <= Line.size() && "Snippet extends past the EOL");
303 Snippets.push_back(Line.substr(Start, Len));
307 Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1));
309 for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
310 assert(LCol == Segments[I - 1]->Col && "Snippet start position is wrong");
311 Snip(LCol - 1, Segments[I]->Col - LCol);
314 // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1.
315 Snip(LCol - 1, Line.size() + 1 - LCol);
316 assert(LCol == Line.size() + 1 && "Final snippet doesn't reach the EOL");
318 // 2. Escape all of the snippets.
320 for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
321 Snippets[I] = escape(Snippets[I]);
323 // 3. Use \p WrappedSegment to set the highlight for snippets 0 and 1. Use
324 // segment 1 to set the highlight for snippet 2, segment 2 to set the
325 // highlight for snippet 3, and so on.
327 Optional<std::string> Color;
328 auto Highlight = [&](const std::string &Snippet) {
329 return tag("span", Snippet, Color.getValue());
332 auto CheckIfUncovered = [](const coverage::CoverageSegment *S) {
333 return S && S->HasCount && S->Count == 0;
336 if (CheckIfUncovered(WrappedSegment) ||
337 CheckIfUncovered(Segments.empty() ? nullptr : Segments.front())) {
339 Snippets[0] = Highlight(Snippets[0]);
340 Snippets[1] = Highlight(Snippets[1]);
343 for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
344 const auto *CurSeg = Segments[I];
345 if (CurSeg->Col == ExpansionCol)
347 else if (CheckIfUncovered(CurSeg))
352 if (Color.hasValue())
353 Snippets[I + 1] = Highlight(Snippets[I + 1]);
356 // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
357 // sub-line region count tooltips if needed.
359 bool HasMultipleRegions = [&] {
360 unsigned RegionCount = 0;
361 for (const auto *S : Segments)
362 if (S->HasCount && S->IsRegionEntry)
363 if (++RegionCount > 1)
368 if (shouldRenderRegionMarkers(HasMultipleRegions)) {
369 for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
370 const auto *CurSeg = Segments[I];
371 if (!CurSeg->IsRegionEntry || !CurSeg->HasCount)
375 tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count),
383 for (const auto &Snippet : Snippets)
387 // If there are no sub-views left to attach to this cell, end the cell.
388 // Otherwise, end it after the sub-views are rendered (renderLineSuffix()).
393 void SourceCoverageViewHTML::renderLineCoverageColumn(
394 raw_ostream &OS, const LineCoverageStats &Line) {
395 std::string Count = "";
397 Count = tag("pre", formatCount(Line.ExecutionCount));
398 std::string CoverageClass =
399 (Line.ExecutionCount > 0) ? "covered-line" : "uncovered-line";
400 OS << tag("td", Count, CoverageClass);
403 void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS,
405 OS << tag("td", tag("pre", utostr(uint64_t(LineNo))), "line-number");
408 void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &,
409 CoverageSegmentArray,
411 // Region markers are rendered in-line using tooltips.
414 void SourceCoverageViewHTML::renderExpansionSite(
415 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
416 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
417 // Render the line containing the expansion site. No extra formatting needed.
418 renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth);
421 void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS,
423 unsigned ViewDepth) {
424 OS << BeginExpansionDiv;
425 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
427 OS << EndExpansionDiv;
430 void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS,
431 InstantiationView &ISV,
432 unsigned ViewDepth) {
433 OS << BeginExpansionDiv;
434 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth);
435 OS << EndExpansionDiv;