Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 1 | //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file This file implements the html coverage renderer. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 14 | #include "CoverageReport.h" |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 15 | #include "SourceCoverageViewHTML.h" |
| 16 | #include "llvm/ADT/Optional.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 27 | // Return a string with the special characters in \p Str escaped. |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 28 | std::string escape(StringRef Str, const CoverageViewOptions &Opts) { |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 29 | std::string Result; |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 30 | unsigned ColNum = 0; // Record the column number. |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 31 | for (char C : Str) { |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 32 | ++ColNum; |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 33 | if (C == '&') |
| 34 | Result += "&"; |
| 35 | else if (C == '<') |
| 36 | Result += "<"; |
| 37 | else if (C == '>') |
| 38 | Result += ">"; |
| 39 | else if (C == '\"') |
| 40 | Result += """; |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 41 | else if (C == '\n' || C == '\r') { |
| 42 | Result += C; |
| 43 | ColNum = 0; |
| 44 | } else if (C == '\t') { |
| 45 | // Replace '\t' with TabSize spaces. |
| 46 | unsigned NumSpaces = Opts.TabSize - (--ColNum % Opts.TabSize); |
| 47 | for (unsigned I = 0; I < NumSpaces; ++I) |
| 48 | Result += " "; |
| 49 | ColNum += NumSpaces; |
| 50 | } else |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 51 | Result += C; |
| 52 | } |
| 53 | return Result; |
| 54 | } |
| 55 | |
| 56 | // Create a \p Name tag around \p Str, and optionally set its \p ClassName. |
| 57 | std::string tag(const std::string &Name, const std::string &Str, |
| 58 | const std::string &ClassName = "") { |
| 59 | std::string Tag = "<" + Name; |
| 60 | if (ClassName != "") |
| 61 | Tag += " class='" + ClassName + "'"; |
| 62 | return Tag + ">" + Str + "</" + Name + ">"; |
| 63 | } |
| 64 | |
| 65 | // Create an anchor to \p Link with the label \p Str. |
| 66 | std::string a(const std::string &Link, const std::string &Str, |
| 67 | const std::string &TargetType = "href") { |
| 68 | return "<a " + TargetType + "='" + Link + "'>" + Str + "</a>"; |
| 69 | } |
| 70 | |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 71 | const char *BeginHeader = |
| 72 | "<head>" |
| 73 | "<meta name='viewport' content='width=device-width,initial-scale=1'>" |
| 74 | "<meta charset='UTF-8'>"; |
| 75 | |
| 76 | const char *CSSForCoverage = |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 77 | R"(.red { |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 78 | background-color: #ffd0d0; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 79 | } |
| 80 | .cyan { |
| 81 | background-color: cyan; |
| 82 | } |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 83 | body { |
| 84 | font-family: -apple-system, sans-serif; |
| 85 | } |
| 86 | pre { |
| 87 | margin-top: 0px !important; |
| 88 | margin-bottom: 0px !important; |
| 89 | } |
| 90 | .source-name-title { |
| 91 | padding: 5px 10px; |
| 92 | border-bottom: 1px solid #dbdbdb; |
| 93 | background-color: #eee; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 94 | line-height: 35px; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 95 | } |
| 96 | .centered { |
| 97 | display: table; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 98 | margin-left: left; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 99 | margin-right: auto; |
| 100 | border: 1px solid #dbdbdb; |
| 101 | border-radius: 3px; |
| 102 | } |
| 103 | .expansion-view { |
| 104 | background-color: rgba(0, 0, 0, 0); |
| 105 | margin-left: 0px; |
| 106 | margin-top: 5px; |
| 107 | margin-right: 5px; |
| 108 | margin-bottom: 5px; |
| 109 | border: 1px solid #dbdbdb; |
| 110 | border-radius: 3px; |
| 111 | } |
| 112 | table { |
| 113 | border-collapse: collapse; |
| 114 | } |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 115 | .light-row { |
| 116 | background: #ffffff; |
| 117 | border: 1px solid #dbdbdb; |
| 118 | } |
| 119 | .column-entry { |
| 120 | text-align: right; |
| 121 | } |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 122 | .column-entry-left { |
| 123 | text-align: left; |
| 124 | } |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 125 | .column-entry-yellow { |
| 126 | text-align: right; |
| 127 | background-color: #ffffd0; |
| 128 | } |
| 129 | .column-entry-red { |
| 130 | text-align: right; |
| 131 | background-color: #ffd0d0; |
| 132 | } |
| 133 | .column-entry-green { |
| 134 | text-align: right; |
| 135 | background-color: #d0ffd0; |
| 136 | } |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 137 | .line-number { |
| 138 | text-align: right; |
| 139 | color: #aaa; |
| 140 | } |
| 141 | .covered-line { |
| 142 | text-align: right; |
| 143 | color: #0080ff; |
| 144 | } |
| 145 | .uncovered-line { |
| 146 | text-align: right; |
| 147 | color: #ff3300; |
| 148 | } |
| 149 | .tooltip { |
| 150 | position: relative; |
| 151 | display: inline; |
| 152 | background-color: #b3e6ff; |
| 153 | text-decoration: none; |
| 154 | } |
| 155 | .tooltip span.tooltip-content { |
| 156 | position: absolute; |
| 157 | width: 100px; |
| 158 | margin-left: -50px; |
| 159 | color: #FFFFFF; |
| 160 | background: #000000; |
| 161 | height: 30px; |
| 162 | line-height: 30px; |
| 163 | text-align: center; |
| 164 | visibility: hidden; |
| 165 | border-radius: 6px; |
| 166 | } |
| 167 | .tooltip span.tooltip-content:after { |
| 168 | content: ''; |
| 169 | position: absolute; |
| 170 | top: 100%; |
| 171 | left: 50%; |
| 172 | margin-left: -8px; |
| 173 | width: 0; height: 0; |
| 174 | border-top: 8px solid #000000; |
| 175 | border-right: 8px solid transparent; |
| 176 | border-left: 8px solid transparent; |
| 177 | } |
| 178 | :hover.tooltip span.tooltip-content { |
| 179 | visibility: visible; |
| 180 | opacity: 0.8; |
| 181 | bottom: 30px; |
| 182 | left: 50%; |
| 183 | z-index: 999; |
| 184 | } |
| 185 | th, td { |
| 186 | vertical-align: top; |
| 187 | padding: 2px 5px; |
| 188 | border-collapse: collapse; |
| 189 | border-right: solid 1px #eee; |
| 190 | border-left: solid 1px #eee; |
| 191 | } |
| 192 | td:first-child { |
| 193 | border-left: none; |
| 194 | } |
| 195 | td:last-child { |
| 196 | border-right: none; |
| 197 | } |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 198 | )"; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 199 | |
| 200 | const char *EndHeader = "</head>"; |
| 201 | |
| 202 | const char *BeginCenteredDiv = "<div class='centered'>"; |
| 203 | |
| 204 | const char *EndCenteredDiv = "</div>"; |
| 205 | |
| 206 | const char *BeginSourceNameDiv = "<div class='source-name-title'>"; |
| 207 | |
| 208 | const char *EndSourceNameDiv = "</div>"; |
| 209 | |
| 210 | const char *BeginCodeTD = "<td class='code'>"; |
| 211 | |
| 212 | const char *EndCodeTD = "</td>"; |
| 213 | |
| 214 | const char *BeginPre = "<pre>"; |
| 215 | |
| 216 | const char *EndPre = "</pre>"; |
| 217 | |
| 218 | const char *BeginExpansionDiv = "<div class='expansion-view'>"; |
| 219 | |
| 220 | const char *EndExpansionDiv = "</div>"; |
| 221 | |
| 222 | const char *BeginTable = "<table>"; |
| 223 | |
| 224 | const char *EndTable = "</table>"; |
| 225 | |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 226 | const char *ProjectTitleTag = "h1"; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 227 | |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 228 | const char *ReportTitleTag = "h2"; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 229 | |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 230 | const char *CreatedTimeTag = "h4"; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 231 | |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 232 | std::string getPathToStyle(StringRef ViewPath) { |
| 233 | std::string PathToStyle = ""; |
| 234 | std::string PathSep = sys::path::get_separator(); |
| 235 | unsigned NumSeps = ViewPath.count(PathSep); |
| 236 | for (unsigned I = 0, E = NumSeps; I < E; ++I) |
| 237 | PathToStyle += ".." + PathSep; |
| 238 | return PathToStyle + "style.css"; |
| 239 | } |
| 240 | |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 241 | void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts, |
| 242 | const std::string &PathToStyle = "") { |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 243 | OS << "<!doctype html>" |
| 244 | "<html>" |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 245 | << BeginHeader; |
| 246 | |
| 247 | // Link to a stylesheet if one is available. Otherwise, use the default style. |
| 248 | if (PathToStyle.empty()) |
| 249 | OS << "<style>" << CSSForCoverage << "</style>"; |
| 250 | else |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 251 | OS << "<link rel='stylesheet' type='text/css' href='" |
| 252 | << escape(PathToStyle, Opts) << "'>"; |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 253 | |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 254 | OS << EndHeader << "<body>"; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void emitEpilog(raw_ostream &OS) { |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 258 | OS << "</body>" |
| 259 | << "</html>"; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 262 | } // anonymous namespace |
| 263 | |
| 264 | Expected<CoveragePrinter::OwnedStream> |
| 265 | CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) { |
| 266 | auto OSOrErr = createOutputStream(Path, "html", InToplevel); |
| 267 | if (!OSOrErr) |
| 268 | return OSOrErr; |
| 269 | |
| 270 | OwnedStream OS = std::move(OSOrErr.get()); |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 271 | |
| 272 | if (!Opts.hasOutputDirectory()) { |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 273 | emitPrelude(*OS.get(), Opts); |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 274 | } else { |
| 275 | std::string ViewPath = getOutputPath(Path, "html", InToplevel); |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 276 | emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath)); |
Vedant Kumar | c076c49 | 2016-07-21 23:26:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 279 | return std::move(OS); |
| 280 | } |
| 281 | |
| 282 | void CoveragePrinterHTML::closeViewFile(OwnedStream OS) { |
| 283 | emitEpilog(*OS.get()); |
| 284 | } |
| 285 | |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 286 | /// Emit column labels for the table in the index. |
| 287 | static void emitColumnLabelsForIndex(raw_ostream &OS) { |
| 288 | SmallVector<std::string, 4> Columns; |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 289 | Columns.emplace_back(tag("td", "Filename", "column-entry-left")); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 290 | for (const char *Label : |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 291 | {"Function Coverage", "Line Coverage", "Region Coverage"}) |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 292 | Columns.emplace_back(tag("td", Label, "column-entry")); |
| 293 | OS << tag("tr", join(Columns.begin(), Columns.end(), "")); |
| 294 | } |
| 295 | |
| 296 | /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is |
| 297 | /// false, link the summary to \p SF. |
| 298 | void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF, |
| 299 | const FileCoverageSummary &FCS, |
| 300 | bool IsTotals) const { |
| 301 | SmallVector<std::string, 4> Columns; |
| 302 | |
| 303 | // Format a coverage triple and add the result to the list of columns. |
| 304 | auto AddCoverageTripleToColumn = [&Columns](unsigned Hit, unsigned Total, |
| 305 | float Pctg) { |
| 306 | std::string S; |
| 307 | { |
| 308 | raw_string_ostream RSO{S}; |
| 309 | RSO << format("%*.2f", 7, Pctg) << "% (" << Hit << '/' << Total << ')'; |
| 310 | } |
| 311 | const char *CellClass = "column-entry-yellow"; |
| 312 | if (Pctg < 80.0) |
| 313 | CellClass = "column-entry-red"; |
| 314 | else if (Hit == Total) |
| 315 | CellClass = "column-entry-green"; |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 316 | Columns.emplace_back(tag("td", tag("pre", S), CellClass)); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | // Simplify the display file path, and wrap it in a link if requested. |
| 320 | std::string Filename; |
| 321 | SmallString<128> LinkTextStr(sys::path::relative_path(FCS.Name)); |
| 322 | sys::path::remove_dots(LinkTextStr, /*remove_dot_dots=*/true); |
| 323 | sys::path::native(LinkTextStr); |
| 324 | std::string LinkText = escape(LinkTextStr, Opts); |
| 325 | if (IsTotals) { |
| 326 | Filename = LinkText; |
| 327 | } else { |
| 328 | std::string LinkTarget = |
| 329 | escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts); |
| 330 | Filename = a(LinkTarget, LinkText); |
| 331 | } |
| 332 | |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 333 | Columns.emplace_back(tag("td", tag("pre", Filename))); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 334 | AddCoverageTripleToColumn(FCS.FunctionCoverage.Executed, |
| 335 | FCS.FunctionCoverage.NumFunctions, |
| 336 | FCS.FunctionCoverage.getPercentCovered()); |
| 337 | AddCoverageTripleToColumn( |
| 338 | FCS.LineCoverage.NumLines - FCS.LineCoverage.NotCovered, |
| 339 | FCS.LineCoverage.NumLines, FCS.LineCoverage.getPercentCovered()); |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 340 | AddCoverageTripleToColumn( |
| 341 | FCS.RegionCoverage.NumRegions - FCS.RegionCoverage.NotCovered, |
| 342 | FCS.RegionCoverage.NumRegions, FCS.RegionCoverage.getPercentCovered()); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 343 | |
| 344 | OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row"); |
| 345 | } |
| 346 | |
| 347 | Error CoveragePrinterHTML::createIndexFile( |
| 348 | ArrayRef<StringRef> SourceFiles, |
| 349 | const coverage::CoverageMapping &Coverage) { |
| 350 | // Emit the default stylesheet. |
| 351 | auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true); |
| 352 | if (Error E = CSSOrErr.takeError()) |
| 353 | return E; |
| 354 | |
| 355 | OwnedStream CSS = std::move(CSSOrErr.get()); |
| 356 | CSS->operator<<(CSSForCoverage); |
| 357 | |
| 358 | // Emit a file index along with some coverage statistics. |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 359 | auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true); |
| 360 | if (Error E = OSOrErr.takeError()) |
| 361 | return E; |
| 362 | auto OS = std::move(OSOrErr.get()); |
| 363 | raw_ostream &OSRef = *OS.get(); |
| 364 | |
Vedant Kumar | 127d050 | 2016-07-22 20:49:23 +0000 | [diff] [blame] | 365 | assert(Opts.hasOutputDirectory() && "No output directory for index file"); |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 366 | emitPrelude(OSRef, Opts, getPathToStyle("")); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 367 | |
| 368 | // Emit some basic information about the coverage report. |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 369 | if (Opts.hasProjectTitle()) |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 370 | OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts)); |
| 371 | OSRef << tag(ReportTitleTag, "Coverage Report"); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 372 | if (Opts.hasCreatedTime()) |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 373 | OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts)); |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 374 | |
| 375 | // Emit a table containing links to reports for each file in the covmapping. |
| 376 | CoverageReport Report(Opts, Coverage); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 377 | OSRef << BeginCenteredDiv << BeginTable; |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 378 | emitColumnLabelsForIndex(OSRef); |
| 379 | FileCoverageSummary Totals("TOTALS"); |
| 380 | auto FileReports = Report.prepareFileReports(Totals, SourceFiles); |
| 381 | for (unsigned I = 0, E = FileReports.size(); I < E; ++I) |
| 382 | emitFileSummary(OSRef, SourceFiles[I], FileReports[I]); |
| 383 | emitFileSummary(OSRef, "Totals", Totals, /*IsTotals=*/true); |
Ying Yi | 544b1df | 2016-09-13 11:28:31 +0000 | [diff] [blame] | 384 | OSRef << EndTable << EndCenteredDiv |
| 385 | << tag("h5", escape(Opts.getLLVMVersionString(), Opts)); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 386 | emitEpilog(OSRef); |
| 387 | |
| 388 | return Error::success(); |
| 389 | } |
| 390 | |
| 391 | void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) { |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 392 | OS << BeginCenteredDiv << BeginTable; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) { |
Vedant Kumar | 84a280a | 2016-09-13 23:00:13 +0000 | [diff] [blame] | 396 | OS << EndTable << EndCenteredDiv; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Vedant Kumar | b1c174a | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 399 | void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) { |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 400 | OS << BeginSourceNameDiv; |
Vedant Kumar | 0053c0b | 2016-09-08 00:56:48 +0000 | [diff] [blame] | 401 | std::string ViewInfo = escape( |
| 402 | WholeFile ? getVerboseSourceName() : getSourceName(), getOptions()); |
| 403 | OS << tag("pre", ViewInfo); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 404 | OS << EndSourceNameDiv; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) { |
| 408 | OS << "<tr>"; |
| 409 | } |
| 410 | |
| 411 | void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) { |
| 412 | // If this view has sub-views, renderLine() cannot close the view's cell. |
| 413 | // Take care of it here, after all sub-views have been rendered. |
| 414 | if (hasSubViews()) |
| 415 | OS << EndCodeTD; |
| 416 | OS << "</tr>"; |
| 417 | } |
| 418 | |
| 419 | void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) { |
| 420 | // The table-based output makes view dividers unnecessary. |
| 421 | } |
| 422 | |
| 423 | void SourceCoverageViewHTML::renderLine( |
| 424 | raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment, |
| 425 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned) { |
| 426 | StringRef Line = L.Line; |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 427 | unsigned LineNo = L.LineNo; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 428 | |
| 429 | // Steps for handling text-escaping, highlighting, and tooltip creation: |
| 430 | // |
| 431 | // 1. Split the line into N+1 snippets, where N = |Segments|. The first |
| 432 | // snippet starts from Col=1 and ends at the start of the first segment. |
| 433 | // The last snippet starts at the last mapped column in the line and ends |
| 434 | // at the end of the line. Both are required but may be empty. |
| 435 | |
| 436 | SmallVector<std::string, 8> Snippets; |
| 437 | |
| 438 | unsigned LCol = 1; |
| 439 | auto Snip = [&](unsigned Start, unsigned Len) { |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 440 | Snippets.push_back(Line.substr(Start, Len)); |
| 441 | LCol += Len; |
| 442 | }; |
| 443 | |
| 444 | Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1)); |
| 445 | |
Vedant Kumar | c236e5a | 2016-09-09 18:44:40 +0000 | [diff] [blame] | 446 | for (unsigned I = 1, E = Segments.size(); I < E; ++I) |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 447 | Snip(LCol - 1, Segments[I]->Col - LCol); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 448 | |
| 449 | // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1. |
| 450 | Snip(LCol - 1, Line.size() + 1 - LCol); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 451 | |
| 452 | // 2. Escape all of the snippets. |
| 453 | |
| 454 | for (unsigned I = 0, E = Snippets.size(); I < E; ++I) |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 455 | Snippets[I] = escape(Snippets[I], getOptions()); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 456 | |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 457 | // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment |
| 458 | // 1 to set the highlight for snippet 2, segment 2 to set the highlight for |
| 459 | // snippet 3, and so on. |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 460 | |
| 461 | Optional<std::string> Color; |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 462 | SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 463 | auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) { |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 464 | if (getOptions().Debug) |
| 465 | HighlightedRanges.emplace_back(LC, RC); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 466 | return tag("span", Snippet, Color.getValue()); |
| 467 | }; |
| 468 | |
| 469 | auto CheckIfUncovered = [](const coverage::CoverageSegment *S) { |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 470 | return S && S->HasCount && S->Count == 0; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 473 | if (CheckIfUncovered(WrappedSegment)) { |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 474 | Color = "red"; |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 475 | if (!Snippets[0].empty()) |
| 476 | Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size()); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 479 | for (unsigned I = 0, E = Segments.size(); I < E; ++I) { |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 480 | const auto *CurSeg = Segments[I]; |
| 481 | if (CurSeg->Col == ExpansionCol) |
| 482 | Color = "cyan"; |
| 483 | else if (CheckIfUncovered(CurSeg)) |
| 484 | Color = "red"; |
| 485 | else |
| 486 | Color = None; |
| 487 | |
| 488 | if (Color.hasValue()) |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 489 | Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col, |
| 490 | CurSeg->Col + Snippets[I + 1].size()); |
| 491 | } |
| 492 | |
| 493 | if (Color.hasValue() && Segments.empty()) |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 494 | Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size()); |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 495 | |
| 496 | if (getOptions().Debug) { |
| 497 | for (const auto &Range : HighlightedRanges) { |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 498 | errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> "; |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 499 | if (Range.second == 0) |
| 500 | errs() << "?"; |
| 501 | else |
Vedant Kumar | 0b33f2c | 2016-09-08 19:18:23 +0000 | [diff] [blame] | 502 | errs() << Range.second; |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 503 | errs() << "\n"; |
| 504 | } |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate |
| 508 | // sub-line region count tooltips if needed. |
| 509 | |
| 510 | bool HasMultipleRegions = [&] { |
| 511 | unsigned RegionCount = 0; |
| 512 | for (const auto *S : Segments) |
| 513 | if (S->HasCount && S->IsRegionEntry) |
| 514 | if (++RegionCount > 1) |
| 515 | return true; |
| 516 | return false; |
| 517 | }(); |
| 518 | |
| 519 | if (shouldRenderRegionMarkers(HasMultipleRegions)) { |
| 520 | for (unsigned I = 0, E = Segments.size(); I < E; ++I) { |
| 521 | const auto *CurSeg = Segments[I]; |
| 522 | if (!CurSeg->IsRegionEntry || !CurSeg->HasCount) |
| 523 | continue; |
| 524 | |
| 525 | Snippets[I + 1] = |
| 526 | tag("div", Snippets[I + 1] + tag("span", formatCount(CurSeg->Count), |
Vedant Kumar | fc07e8b | 2016-07-27 21:57:15 +0000 | [diff] [blame] | 527 | "tooltip-content"), |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 528 | "tooltip"); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | OS << BeginCodeTD; |
| 533 | OS << BeginPre; |
| 534 | for (const auto &Snippet : Snippets) |
| 535 | OS << Snippet; |
| 536 | OS << EndPre; |
| 537 | |
| 538 | // If there are no sub-views left to attach to this cell, end the cell. |
| 539 | // Otherwise, end it after the sub-views are rendered (renderLineSuffix()). |
| 540 | if (!hasSubViews()) |
| 541 | OS << EndCodeTD; |
| 542 | } |
| 543 | |
| 544 | void SourceCoverageViewHTML::renderLineCoverageColumn( |
| 545 | raw_ostream &OS, const LineCoverageStats &Line) { |
| 546 | std::string Count = ""; |
| 547 | if (Line.isMapped()) |
| 548 | Count = tag("pre", formatCount(Line.ExecutionCount)); |
| 549 | std::string CoverageClass = |
| 550 | (Line.ExecutionCount > 0) ? "covered-line" : "uncovered-line"; |
| 551 | OS << tag("td", Count, CoverageClass); |
| 552 | } |
| 553 | |
| 554 | void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS, |
| 555 | unsigned LineNo) { |
Vedant Kumar | 2e08936 | 2016-07-18 17:53:16 +0000 | [diff] [blame] | 556 | std::string LineNoStr = utostr(uint64_t(LineNo)); |
| 557 | OS << tag("td", a("L" + LineNoStr, tag("pre", LineNoStr), "name"), |
| 558 | "line-number"); |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &, |
| 562 | CoverageSegmentArray, |
| 563 | unsigned) { |
| 564 | // Region markers are rendered in-line using tooltips. |
| 565 | } |
| 566 | |
| 567 | void SourceCoverageViewHTML::renderExpansionSite( |
| 568 | raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment, |
| 569 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { |
| 570 | // Render the line containing the expansion site. No extra formatting needed. |
| 571 | renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth); |
| 572 | } |
| 573 | |
| 574 | void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS, |
| 575 | ExpansionView &ESV, |
| 576 | unsigned ViewDepth) { |
| 577 | OS << BeginExpansionDiv; |
| 578 | ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, |
| 579 | ViewDepth + 1); |
| 580 | OS << EndExpansionDiv; |
| 581 | } |
| 582 | |
| 583 | void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS, |
| 584 | InstantiationView &ISV, |
| 585 | unsigned ViewDepth) { |
| 586 | OS << BeginExpansionDiv; |
| 587 | ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth); |
| 588 | OS << EndExpansionDiv; |
| 589 | } |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 590 | |
Vedant Kumar | b2edd11 | 2016-09-15 04:45:59 +0000 | [diff] [blame^] | 591 | void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) { |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 592 | if (getOptions().hasProjectTitle()) |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 593 | OS << tag(ProjectTitleTag, escape(getOptions().ProjectTitle, getOptions())); |
Vedant Kumar | b2edd11 | 2016-09-15 04:45:59 +0000 | [diff] [blame^] | 594 | OS << tag(ReportTitleTag, escape(Title, getOptions())); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 595 | if (getOptions().hasCreatedTime()) |
Vedant Kumar | 7b9e9bb | 2016-09-10 19:37:20 +0000 | [diff] [blame] | 596 | OS << tag(CreatedTimeTag, |
| 597 | escape(getOptions().CreatedTimeStr, getOptions())); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void SourceCoverageViewHTML::renderTableHeader(raw_ostream &OS, |
Vedant Kumar | b1c174a | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 601 | unsigned FirstUncoveredLineNo, |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 602 | unsigned ViewDepth) { |
Vedant Kumar | b1c174a | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 603 | std::string SourceLabel; |
| 604 | if (FirstUncoveredLineNo == 0) { |
| 605 | SourceLabel = tag("td", tag("pre", "Source")); |
| 606 | } else { |
| 607 | std::string LinkTarget = "#L" + utostr(uint64_t(FirstUncoveredLineNo)); |
| 608 | SourceLabel = |
| 609 | tag("td", tag("pre", "Source (" + |
| 610 | a(LinkTarget, "jump to first uncovered line") + |
| 611 | ")")); |
| 612 | } |
| 613 | |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 614 | renderLinePrefix(OS, ViewDepth); |
Vedant Kumar | b1c174a | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 615 | OS << tag("td", tag("pre", "Line No.")) << tag("td", tag("pre", "Count")) |
| 616 | << SourceLabel; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 617 | renderLineSuffix(OS, ViewDepth); |
| 618 | } |