diff options
-rw-r--r-- | llvm/docs/CommandGuide/llvm-cov.rst | 2 | ||||
-rw-r--r-- | llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp | 31 | ||||
-rw-r--r-- | llvm/test/tools/llvm-cov/showTemplateInstantiations.cpp | 45 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/CodeCoverage.cpp | 10 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/CoverageViewOptions.h | 3 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/SourceCoverageView.cpp | 6 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp | 436 | ||||
-rw-r--r-- | llvm/tools/llvm-cov/SourceCoverageViewHTML.h | 83 |
9 files changed, 615 insertions, 2 deletions
diff --git a/llvm/docs/CommandGuide/llvm-cov.rst b/llvm/docs/CommandGuide/llvm-cov.rst index 35a7a036456..cdb36e7b98f 100644 --- a/llvm/docs/CommandGuide/llvm-cov.rst +++ b/llvm/docs/CommandGuide/llvm-cov.rst @@ -238,7 +238,7 @@ OPTIONS .. option:: -format=<FORMAT> - Use the specified output format. The supported formats are: "text". + Use the specified output format. The supported formats are: "text", "html". .. option:: -output-dir=PATH diff --git a/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp b/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp index b8ccdd4aa3e..c624c147820 100644 --- a/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp +++ b/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp @@ -38,3 +38,34 @@ int main() { // TEXT: 161| [[@LINE]]|int main( // Test index creation. // RUN: FileCheck -check-prefix=INDEX -input-file %t.dir/index.txt %s // INDEX: showLineExecutionCounts.cpp.txt +// +// Test html output. +// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -format html -o %t.html.dir -instr-profile %t.profdata -filename-equivalence %s +// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -format html -o %t.html.dir -instr-profile %t.profdata -filename-equivalence -name=main %s +// RUN: FileCheck -check-prefixes=HTML,HTML-WHOLE-FILE -input-file %t.html.dir/coverage/tmp/showLineExecutionCounts.cpp.html %s +// RUN: FileCheck -check-prefixes=HTML,HTML-FILTER -input-file %t.html.dir/functions.html %s +// +// HTML-WHOLE-FILE: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>// before +// HTML-FILTER-NOT: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-45]]</pre></td><td class='code'><pre>// before +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>int main() { +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> int x = 0 +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre><span class='red'> if (x) { +// HTML: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre><span class='red'> }</span> +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> x = 1; +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> } +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML: <td class='covered-line'><pre>16.2k</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> for (int i = 0; i < 100; ++i) +// HTML: <td class='covered-line'><pre>16.1k</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> x = 1; +// HTML: <td class='covered-line'><pre>16.1k</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> } +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> x = x < 10 +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> x = x > 10 +// HTML: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre><span class='red'> x - 1: +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> x + 1; +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> return 0; +// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>} +// HTML-WHOLE-FILE: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>// after +// HTML-FILTER-NOT: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-45]]</pre></td><td class='code'><pre>// after diff --git a/llvm/test/tools/llvm-cov/showTemplateInstantiations.cpp b/llvm/test/tools/llvm-cov/showTemplateInstantiations.cpp index 77ecb473400..8fc45898a8b 100644 --- a/llvm/test/tools/llvm-cov/showTemplateInstantiations.cpp +++ b/llvm/test/tools/llvm-cov/showTemplateInstantiations.cpp @@ -38,3 +38,48 @@ int main() { // ALL: 1| [[@LINE]]|int main() { } // ALL-NEXT: 1| [[@LINE]]|} // after coverage // ALL-NEXT: | [[@LINE]]|// after // FILTER-NOT: | [[@LINE-1]]|// after + +// Test html output. +// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence %s -format html -o %t.html.dir +// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence -name=_Z4funcIbEiT_ %s -format html -o %t.html.dir +// RUN: FileCheck -check-prefixes=HTML-SHARED,HTML-ALL -input-file=%t.html.dir/coverage/tmp/showTemplateInstantiations.cpp.html %s +// RUN: FileCheck -check-prefixes=HTML-SHARED,HTML-FILTER -input-file=%t.html.dir/functions.html %s + +// HTML-ALL: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>// before +// HTML-FILTER-NOT: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-45]]</pre></td><td class='code'><pre>// before +// HTML-ALL: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>template<typename T> +// HTML-ALL: <td class='covered-line'><pre>2</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>int func(T x) { +// HTML-ALL: <td class='covered-line'><pre>2</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> if(x) +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> ret +// HTML-ALL: <td class='covered-line'><pre>2</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> else +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> ret +// HTML-ALL: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> +// HTML-ALL: <td class='covered-line'><pre>2</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>} + +// HTML-SHARED: <div class='source-name-title'><pre>_Z4funcIbEiT_</pre></div><table> +// HTML-SHARED: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre>int func(T x) { +// HTML-SHARED: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre> if(x) +// HTML-SHARED: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre> ret +// HTML-SHARED: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre> else +// HTML-SHARED: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre> +// HTML-SHARED: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre> +// HTML-SHARED: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-53]]</pre></td><td class='code'><pre>} + +// HTML-ALL: <div class='source-name-title'><pre>_Z4funcIiEiT_</pre></div><table> +// HTML-FILTER-NOT: <div class='source-name-title'><pre>_Z4funcIiEiT_</pre></div><table> +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre>int func(T x) { +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre> if(x) +// HTML-ALL: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre> +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre> else +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre> ret +// HTML-ALL: <td class='uncovered-line'><pre>0</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre> +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-63]]</pre></td><td class='code'><pre>} + +// HTML-ALL: td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>int main() { +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> func<int>(0); +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> func<bool>(true); +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre> return 0; +// HTML-ALL: <td class='covered-line'><pre>1</pre></td><td class='line-number'><pre>[[@LINE-44]]</pre></td><td class='code'><pre>} + +// HTML-ALL: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-45]]</pre></td><td class='code'><pre>// after +// HTML-FILTER-NOT: <td class='uncovered-line'></td><td class='line-number'><pre>[[@LINE-46]]</pre></td><td class='code'><pre>// after diff --git a/llvm/tools/llvm-cov/CMakeLists.txt b/llvm/tools/llvm-cov/CMakeLists.txt index c8d64c08512..e22828e11ef 100644 --- a/llvm/tools/llvm-cov/CMakeLists.txt +++ b/llvm/tools/llvm-cov/CMakeLists.txt @@ -8,6 +8,7 @@ add_llvm_tool(llvm-cov CoverageReport.cpp CoverageSummaryInfo.cpp SourceCoverageView.cpp + SourceCoverageViewHTML.cpp SourceCoverageViewText.cpp TestingSupport.cpp ) diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp index 4fdb895cca7..7b9edc0e731 100644 --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -272,6 +272,8 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { "format", cl::desc("Output format for line-based coverage reports"), cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text", "Text output"), + clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", + "HTML output"), clEnumValEnd), cl::init(CoverageViewOptions::OutputFormat::Text)); @@ -333,6 +335,11 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { ? sys::Process::StandardOutHasColors() : UseColor == cl::BOU_TRUE; break; + case CoverageViewOptions::OutputFormat::HTML: + if (UseColor == cl::BOU_FALSE) + error("Color output cannot be disabled when generating html."); + ViewOpts.Colors = true; + break; } // Create the function filters @@ -527,6 +534,9 @@ int CodeCoverageTool::report(int argc, const char **argv, if (Err) return Err; + if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) + error("HTML output for summary reports is not yet supported."); + auto Coverage = load(); if (!Coverage) return 1; diff --git a/llvm/tools/llvm-cov/CoverageViewOptions.h b/llvm/tools/llvm-cov/CoverageViewOptions.h index 19756e6edb8..873eae885a7 100644 --- a/llvm/tools/llvm-cov/CoverageViewOptions.h +++ b/llvm/tools/llvm-cov/CoverageViewOptions.h @@ -17,7 +17,8 @@ namespace llvm { /// \brief The options for displaying the code coverage information. struct CoverageViewOptions { enum class OutputFormat { - Text + Text, + HTML }; bool Debug; diff --git a/llvm/tools/llvm-cov/SourceCoverageView.cpp b/llvm/tools/llvm-cov/SourceCoverageView.cpp index 08eb545a29c..baf7c148bb8 100644 --- a/llvm/tools/llvm-cov/SourceCoverageView.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "SourceCoverageView.h" +#include "SourceCoverageViewHTML.h" #include "SourceCoverageViewText.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" @@ -74,6 +75,8 @@ CoveragePrinter::create(const CoverageViewOptions &Opts) { switch (Opts.Format) { case CoverageViewOptions::OutputFormat::Text: return llvm::make_unique<CoveragePrinterText>(Opts); + case CoverageViewOptions::OutputFormat::HTML: + return llvm::make_unique<CoveragePrinterHTML>(Opts); } llvm_unreachable("Unknown coverage output format!"); } @@ -111,6 +114,9 @@ SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File, case CoverageViewOptions::OutputFormat::Text: return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options, std::move(CoverageInfo)); + case CoverageViewOptions::OutputFormat::HTML: + return llvm::make_unique<SourceCoverageViewHTML>(SourceName, File, Options, + std::move(CoverageInfo)); } llvm_unreachable("Unknown coverage output format!"); } diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp new file mode 100644 index 00000000000..81963e5c544 --- /dev/null +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -0,0 +1,436 @@ +//===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file This file implements the html coverage renderer. +/// +//===----------------------------------------------------------------------===// + +#include "SourceCoverageViewHTML.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Path.h" + +using namespace llvm; + +namespace { + +const char *BeginHeader = + "<head>" + "<meta name='viewport' content='width=device-width,initial-scale=1'>" + "<meta charset='UTF-8'>"; + +const char *CSSForCoverage = + "<style>" +R"( + +.red { + background-color: #FFD0D0; +} +.cyan { + background-color: cyan; +} +.black { + background-color: black; + color: white; +} +.green { + background-color: #98FFA6; + color: white; +} +.magenta { + background-color: #F998FF; + color: white; +} +body { + font-family: -apple-system, sans-serif; +} +pre { + margin-top: 0px !important; + margin-bottom: 0px !important; +} +.source-name-title { + padding: 5px 10px; + border-bottom: 1px solid #dbdbdb; + background-color: #eee; +} +.centered { + display: table; + margin-left: auto; + margin-right: auto; + border: 1px solid #dbdbdb; + border-radius: 3px; +} +.expansion-view { + background-color: rgba(0, 0, 0, 0); + margin-left: 0px; + margin-top: 5px; + margin-right: 5px; + margin-bottom: 5px; + border: 1px solid #dbdbdb; + border-radius: 3px; +} +table { + border-collapse: collapse; +} +.line-number { + text-align: right; + color: #aaa; +} +.covered-line { + text-align: right; + color: #0080ff; +} +.uncovered-line { + text-align: right; + color: #ff3300; +} +.tooltip { + position: relative; + display: inline; + background-color: #b3e6ff; + text-decoration: none; +} +.tooltip span.tooltip-content { + position: absolute; + width: 100px; + margin-left: -50px; + color: #FFFFFF; + background: #000000; + height: 30px; + line-height: 30px; + text-align: center; + visibility: hidden; + border-radius: 6px; +} +.tooltip span.tooltip-content:after { + content: ''; + position: absolute; + top: 100%; + left: 50%; + margin-left: -8px; + width: 0; height: 0; + border-top: 8px solid #000000; + border-right: 8px solid transparent; + border-left: 8px solid transparent; +} +:hover.tooltip span.tooltip-content { + visibility: visible; + opacity: 0.8; + bottom: 30px; + left: 50%; + z-index: 999; +} +th, td { + vertical-align: top; + padding: 2px 5px; + border-collapse: collapse; + border-right: solid 1px #eee; + border-left: solid 1px #eee; +} +td:first-child { + border-left: none; +} +td:last-child { + border-right: none; +} + +)" + "</style>"; + +const char *EndHeader = "</head>"; + +const char *BeginCenteredDiv = "<div class='centered'>"; + +const char *EndCenteredDiv = "</div>"; + +const char *BeginSourceNameDiv = "<div class='source-name-title'>"; + +const char *EndSourceNameDiv = "</div>"; + +const char *BeginCodeTD = "<td class='code'>"; + +const char *EndCodeTD = "</td>"; + +const char *BeginPre = "<pre>"; + +const char *EndPre = "</pre>"; + +const char *BeginExpansionDiv = "<div class='expansion-view'>"; + +const char *EndExpansionDiv = "</div>"; + +const char *BeginTable = "<table>"; + +const char *EndTable = "</table>"; + +void emitPrelude(raw_ostream &OS) { + OS << "<!doctype html>" + "<html>" + << BeginHeader << CSSForCoverage << EndHeader << "<body>" + << BeginCenteredDiv; +} + +void emitEpilog(raw_ostream &OS) { + OS << EndCenteredDiv << "</body>" + "</html>"; +} + +// Return a string with the special characters in \p Str escaped. +std::string escape(StringRef Str) { + std::string Result; + for (char C : Str) { + if (C == '&') + Result += "&"; + else if (C == '<') + Result += "<"; + else if (C == '>') + Result += ">"; + else if (C == '\"') + Result += """; + else + Result += C; + } + return Result; +} + +// Create a \p Name tag around \p Str, and optionally set its \p ClassName. +std::string tag(const std::string &Name, const std::string &Str, + const std::string &ClassName = "") { + std::string Tag = "<" + Name; + if (ClassName != "") + Tag += " class='" + ClassName + "'"; + return Tag + ">" + Str + "</" + Name + ">"; +} + +// Create an anchor to \p Link with the label \p Str. +std::string a(const std::string &Link, const std::string &Str) { + return "<a href='" + Link + "'>" + Str + "</a>"; +} + +} // anonymous namespace + +Expected<CoveragePrinter::OwnedStream> +CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) { + auto OSOrErr = createOutputStream(Path, "html", InToplevel); + if (!OSOrErr) + return OSOrErr; + + OwnedStream OS = std::move(OSOrErr.get()); + emitPrelude(*OS.get()); + return std::move(OS); +} + +void CoveragePrinterHTML::closeViewFile(OwnedStream OS) { + emitEpilog(*OS.get()); +} + +Error CoveragePrinterHTML::createIndexFile(ArrayRef<StringRef> SourceFiles) { + auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true); + if (Error E = OSOrErr.takeError()) + return E; + auto OS = std::move(OSOrErr.get()); + raw_ostream &OSRef = *OS.get(); + + // Emit a table containing links to reports for each file in the covmapping. + emitPrelude(OSRef); + OSRef << BeginSourceNameDiv << "Index" << EndSourceNameDiv; + OSRef << BeginTable; + for (StringRef SF : SourceFiles) { + std::string LinkText = escape(sys::path::relative_path(SF)); + std::string LinkTarget = + escape(getOutputPath(SF, "html", /*InToplevel=*/false)); + OSRef << tag("tr", tag("td", tag("pre", a(LinkTarget, LinkText), "code"))); + } + OSRef << EndTable; + emitEpilog(OSRef); + + return Error::success(); +} + +void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) { + OS << BeginTable; +} + +void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) { + OS << EndTable; +} + +void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS) { + OS << BeginSourceNameDiv << tag("pre", escape(getSourceName())) + << EndSourceNameDiv; +} + +void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) { + OS << "<tr>"; +} + +void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) { + // If this view has sub-views, renderLine() cannot close the view's cell. + // Take care of it here, after all sub-views have been rendered. + if (hasSubViews()) + OS << EndCodeTD; + OS << "</tr>"; +} + +void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) { + // The table-based output makes view dividers unnecessary. +} + +void SourceCoverageViewHTML::renderLine( + raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment, + CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned) { + StringRef Line = L.Line; + + // Steps for handling text-escaping, highlighting, and tooltip creation: + // + // 1. Split the line into N+1 snippets, where N = |Segments|. The first + // snippet starts from Col=1 and ends at the start of the first segment. + // The last snippet starts at the last mapped column in the line and ends + // at the end of the line. Both are required but may be empty. + + SmallVector<std::string, 8> Snippets; + + unsigned LCol = 1; + auto Snip = [&](unsigned Start, unsigned Len) { + assert(Start + Len <= Line.size() && "Snippet extends past the EOL"); + Snippets.push_back(Line.substr(Start, Len)); + LCol += Len; + }; + + Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1)); + + for (unsigned I = 1, E = Segments.size(); I < E; ++I) { + assert(LCol == Segments[I - 1]->Col && "Snippet start position is wrong"); + Snip(LCol - 1, Segments[I]->Col - LCol); + } + + // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1. + Snip(LCol - 1, Line.size() + 1 - LCol); + assert(LCol == Line.size() + 1 && "Final snippet doesn't reach the EOL"); + + // 2. Escape all of the snippets. + + for (unsigned I = 0, E = Snippets.size(); I < E; ++I) + Snippets[I] = escape(Snippets[I]); + + // 3. Use \p WrappedSegment to set the highlight for snippets 0 and 1. Use + // segment 1 to set the highlight for snippet 2, segment 2 to set the + // highlight for snippet 3, and so on. + + Optional<std::string> Color; + auto Highlight = [&](const std::string &Snippet) { + return tag("span", Snippet, Color.getValue()); + }; + + auto CheckIfUncovered = [](const coverage::CoverageSegment *S) { + return S && S->HasCount && S->Count == 0; + }; + + if (CheckIfUncovered(WrappedSegment) || + CheckIfUncovered(Segments.empty() ? nullptr : Segments.front())) { + Color = "red"; + Snippets[0] = Highlight(Snippets[0]); + Snippets[1] = Highlight(Snippets[1]); + } + + for (unsigned I = 1, E = Segments.size(); I < E; ++I) { + const auto *CurSeg = Segments[I]; + if (CurSeg->Col == ExpansionCol) + Color = "cyan"; + else if (CheckIfUncovered(CurSeg)) + Color = "red"; + else + Color = None; + + if (Color.hasValue()) + Snippets[I + 1] = Highlight(Snippets[I + 1]); + } + + // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate + // sub-line region count tooltips if needed. + + bool HasMultipleRegions = [&] { + unsigned RegionCount = 0; + for (const auto *S : Segments) + if (S->HasCount && S->IsRegionEntry) + if (++RegionCount > 1) + return true; + return false; + }(); + + if (shouldRenderRegionMarkers(HasMultipleRegions)) { + for (unsigned I = 0, E = Segments.size(); I < E; ++I) { + const auto *CurSeg = Segments[I]; + if (!CurSeg->IsRegionEntry || !CurSeg->HasCount) + continue; + + Snippets[I + 1] = + tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count), + "tooltip-content"), + "tooltip"); + } + } + + OS << BeginCodeTD; + OS << BeginPre; + for (const auto &Snippet : Snippets) + OS << Snippet; + OS << EndPre; + + // If there are no sub-views left to attach to this cell, end the cell. + // Otherwise, end it after the sub-views are rendered (renderLineSuffix()). + if (!hasSubViews()) + OS << EndCodeTD; +} + +void SourceCoverageViewHTML::renderLineCoverageColumn( + raw_ostream &OS, const LineCoverageStats &Line) { + std::string Count = ""; + if (Line.isMapped()) + Count = tag("pre", formatCount(Line.ExecutionCount)); + std::string CoverageClass = + (Line.ExecutionCount > 0) ? "covered-line" : "uncovered-line"; + OS << tag("td", Count, CoverageClass); +} + +void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS, + unsigned LineNo) { + OS << tag("td", tag("pre", utostr(uint64_t(LineNo))), "line-number"); +} + +void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &, + CoverageSegmentArray, + unsigned) { + // Region markers are rendered in-line using tooltips. +} + +void SourceCoverageViewHTML::renderExpansionSite( + raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment, + CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { + // Render the line containing the expansion site. No extra formatting needed. + renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth); +} + +void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS, + ExpansionView &ESV, + unsigned ViewDepth) { + OS << BeginExpansionDiv; + ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, + ViewDepth + 1); + OS << EndExpansionDiv; +} + +void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS, + InstantiationView &ISV, + unsigned ViewDepth) { + OS << BeginExpansionDiv; + ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth); + OS << EndExpansionDiv; +} diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.h b/llvm/tools/llvm-cov/SourceCoverageViewHTML.h new file mode 100644 index 00000000000..50ecf2bf899 --- /dev/null +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.h @@ -0,0 +1,83 @@ +//===- SourceCoverageViewHTML.h - A html code coverage view ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file This file defines the interface to the html coverage renderer. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_COV_SOURCECOVERAGEVIEWHTML_H +#define LLVM_COV_SOURCECOVERAGEVIEWHTML_H + +#include "SourceCoverageView.h" + +namespace llvm { + +/// \brief A coverage printer for html output. +class CoveragePrinterHTML : public CoveragePrinter { +public: + Expected<OwnedStream> createViewFile(StringRef Path, + bool InToplevel) override; + + void closeViewFile(OwnedStream OS) override; + + Error createIndexFile(ArrayRef<StringRef> SourceFiles) override; + + CoveragePrinterHTML(const CoverageViewOptions &Opts) + : CoveragePrinter(Opts) {} +}; + +/// \brief A code coverage view which supports html-based rendering. +class SourceCoverageViewHTML : public SourceCoverageView { + void renderViewHeader(raw_ostream &OS) override; + + void renderViewFooter(raw_ostream &OS) override; + + void renderSourceName(raw_ostream &OS) override; + + void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) override; + + void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) override; + + void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) override; + + void renderLine(raw_ostream &OS, LineRef L, + const coverage::CoverageSegment *WrappedSegment, + CoverageSegmentArray Segments, unsigned ExpansionCol, + unsigned ViewDepth) override; + + void renderExpansionSite(raw_ostream &OS, LineRef L, + const coverage::CoverageSegment *WrappedSegment, + CoverageSegmentArray Segments, unsigned ExpansionCol, + unsigned ViewDepth) override; + + void renderExpansionView(raw_ostream &OS, ExpansionView &ESV, + unsigned ViewDepth) override; + + void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, + unsigned ViewDepth) override; + + void renderLineCoverageColumn(raw_ostream &OS, + const LineCoverageStats &Line) override; + + void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) override; + + void renderRegionMarkers(raw_ostream &OS, CoverageSegmentArray Segments, + unsigned ViewDepth) override; + +public: + SourceCoverageViewHTML(StringRef SourceName, const MemoryBuffer &File, + const CoverageViewOptions &Options, + coverage::CoverageData &&CoverageInfo) + : SourceCoverageView(SourceName, File, Options, std::move(CoverageInfo)) { + } +}; + +} // namespace llvm + +#endif // LLVM_COV_SOURCECOVERAGEVIEWHTML_H |