| 1 | //===- SourceCoverageViewHTML.cpp - A html code coverage view -------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file This file implements the html coverage renderer. |
| 10 | /// |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "SourceCoverageViewHTML.h" |
| 14 | #include "CoverageReport.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/Format.h" |
| 18 | #include "llvm/Support/Path.h" |
| 19 | #include <optional> |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Return a string with the special characters in \p Str escaped. |
| 26 | std::string escape(StringRef Str, const CoverageViewOptions &Opts) { |
| 27 | std::string TabExpandedResult; |
| 28 | unsigned ColNum = 0; // Record the column number. |
| 29 | for (char C : Str) { |
| 30 | if (C == '\t') { |
| 31 | // Replace '\t' with up to TabSize spaces. |
| 32 | unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize); |
| 33 | TabExpandedResult.append(n: NumSpaces, c: ' '); |
| 34 | ColNum += NumSpaces; |
| 35 | } else { |
| 36 | TabExpandedResult += C; |
| 37 | if (C == '\n' || C == '\r') |
| 38 | ColNum = 0; |
| 39 | else |
| 40 | ++ColNum; |
| 41 | } |
| 42 | } |
| 43 | std::string EscapedHTML; |
| 44 | { |
| 45 | raw_string_ostream OS{EscapedHTML}; |
| 46 | printHTMLEscaped(String: TabExpandedResult, Out&: OS); |
| 47 | } |
| 48 | return EscapedHTML; |
| 49 | } |
| 50 | |
| 51 | // Create a \p Name tag around \p Str, and optionally set its \p ClassName. |
| 52 | std::string tag(StringRef Name, StringRef Str, StringRef ClassName = "" ) { |
| 53 | std::string Tag = "<" ; |
| 54 | Tag += Name; |
| 55 | if (!ClassName.empty()) { |
| 56 | Tag += " class='" ; |
| 57 | Tag += ClassName; |
| 58 | Tag += "'" ; |
| 59 | } |
| 60 | Tag += ">" ; |
| 61 | Tag += Str; |
| 62 | Tag += "</" ; |
| 63 | Tag += Name; |
| 64 | Tag += ">" ; |
| 65 | return Tag; |
| 66 | } |
| 67 | |
| 68 | // Create an anchor to \p Link with the label \p Str. |
| 69 | std::string a(StringRef Link, StringRef Str, StringRef TargetName = "" ) { |
| 70 | std::string Tag; |
| 71 | Tag += "<a " ; |
| 72 | if (!TargetName.empty()) { |
| 73 | Tag += "name='" ; |
| 74 | Tag += TargetName; |
| 75 | Tag += "' " ; |
| 76 | } |
| 77 | Tag += "href='" ; |
| 78 | Tag += Link; |
| 79 | Tag += "'>" ; |
| 80 | Tag += Str; |
| 81 | Tag += "</a>" ; |
| 82 | return Tag; |
| 83 | } |
| 84 | |
| 85 | const char * = |
| 86 | "<head>" |
| 87 | "<meta name='viewport' content='width=device-width,initial-scale=1'>" |
| 88 | "<meta charset='UTF-8'>" ; |
| 89 | |
| 90 | const char *JSForCoverage = |
| 91 | R"javascript( |
| 92 | function next_uncovered(selector, reverse, scroll_selector) { |
| 93 | function visit_element(element) { |
| 94 | element.classList.add("seen"); |
| 95 | element.classList.add("selected"); |
| 96 | |
| 97 | if (!scroll_selector) { |
| 98 | scroll_selector = "tr:has(.selected) td.line-number" |
| 99 | } |
| 100 | |
| 101 | const scroll_to = document.querySelector(scroll_selector); |
| 102 | if (scroll_to) { |
| 103 | scroll_to.scrollIntoView({behavior: "smooth", block: "center", inline: "end"}); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | function select_one() { |
| 108 | if (!reverse) { |
| 109 | const previously_selected = document.querySelector(".selected"); |
| 110 | |
| 111 | if (previously_selected) { |
| 112 | previously_selected.classList.remove("selected"); |
| 113 | } |
| 114 | |
| 115 | return document.querySelector(selector + ":not(.seen)"); |
| 116 | } else { |
| 117 | const previously_selected = document.querySelector(".selected"); |
| 118 | |
| 119 | if (previously_selected) { |
| 120 | previously_selected.classList.remove("selected"); |
| 121 | previously_selected.classList.remove("seen"); |
| 122 | } |
| 123 | |
| 124 | const nodes = document.querySelectorAll(selector + ".seen"); |
| 125 | if (nodes) { |
| 126 | const last = nodes[nodes.length - 1]; // last |
| 127 | return last; |
| 128 | } else { |
| 129 | return undefined; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | function reset_all() { |
| 135 | if (!reverse) { |
| 136 | const all_seen = document.querySelectorAll(selector + ".seen"); |
| 137 | |
| 138 | if (all_seen) { |
| 139 | all_seen.forEach(e => e.classList.remove("seen")); |
| 140 | } |
| 141 | } else { |
| 142 | const all_seen = document.querySelectorAll(selector + ":not(.seen)"); |
| 143 | |
| 144 | if (all_seen) { |
| 145 | all_seen.forEach(e => e.classList.add("seen")); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | const uncovered = select_one(); |
| 152 | |
| 153 | if (uncovered) { |
| 154 | visit_element(uncovered); |
| 155 | } else { |
| 156 | reset_all(); |
| 157 | |
| 158 | const uncovered = select_one(); |
| 159 | |
| 160 | if (uncovered) { |
| 161 | visit_element(uncovered); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | function next_line(reverse) { |
| 167 | next_uncovered("td.uncovered-line", reverse) |
| 168 | } |
| 169 | |
| 170 | function next_region(reverse) { |
| 171 | next_uncovered("span.red.region", reverse); |
| 172 | } |
| 173 | |
| 174 | function next_branch(reverse) { |
| 175 | next_uncovered("span.red.branch", reverse); |
| 176 | } |
| 177 | |
| 178 | document.addEventListener("keypress", function(event) { |
| 179 | const reverse = event.shiftKey; |
| 180 | if (event.code == "KeyL") { |
| 181 | next_line(reverse); |
| 182 | } |
| 183 | if (event.code == "KeyB") { |
| 184 | next_branch(reverse); |
| 185 | } |
| 186 | if (event.code == "KeyR") { |
| 187 | next_region(reverse); |
| 188 | } |
| 189 | }); |
| 190 | )javascript" ; |
| 191 | |
| 192 | const char *CSSForCoverage = |
| 193 | R"(.red { |
| 194 | background-color: #f004; |
| 195 | } |
| 196 | .cyan { |
| 197 | background-color: cyan; |
| 198 | } |
| 199 | html { |
| 200 | scroll-behavior: smooth; |
| 201 | } |
| 202 | body { |
| 203 | font-family: -apple-system, sans-serif; |
| 204 | } |
| 205 | pre { |
| 206 | margin-top: 0px !important; |
| 207 | margin-bottom: 0px !important; |
| 208 | } |
| 209 | .source-name-title { |
| 210 | padding: 5px 10px; |
| 211 | border-bottom: 1px solid #8888; |
| 212 | background-color: #0002; |
| 213 | line-height: 35px; |
| 214 | } |
| 215 | .centered { |
| 216 | display: table; |
| 217 | margin-left: left; |
| 218 | margin-right: auto; |
| 219 | border: 1px solid #8888; |
| 220 | border-radius: 3px; |
| 221 | } |
| 222 | .expansion-view { |
| 223 | margin-left: 0px; |
| 224 | margin-top: 5px; |
| 225 | margin-right: 5px; |
| 226 | margin-bottom: 5px; |
| 227 | border: 1px solid #8888; |
| 228 | border-radius: 3px; |
| 229 | } |
| 230 | table { |
| 231 | border-collapse: collapse; |
| 232 | } |
| 233 | .light-row { |
| 234 | border: 1px solid #8888; |
| 235 | border-left: none; |
| 236 | border-right: none; |
| 237 | } |
| 238 | .light-row-bold { |
| 239 | border: 1px solid #8888; |
| 240 | border-left: none; |
| 241 | border-right: none; |
| 242 | font-weight: bold; |
| 243 | } |
| 244 | .column-entry { |
| 245 | text-align: left; |
| 246 | } |
| 247 | .column-entry-bold { |
| 248 | font-weight: bold; |
| 249 | text-align: left; |
| 250 | } |
| 251 | .column-entry-yellow { |
| 252 | text-align: left; |
| 253 | background-color: #ff06; |
| 254 | } |
| 255 | .column-entry-red { |
| 256 | text-align: left; |
| 257 | background-color: #f004; |
| 258 | } |
| 259 | .column-entry-gray { |
| 260 | text-align: left; |
| 261 | background-color: #fff4; |
| 262 | } |
| 263 | .column-entry-green { |
| 264 | text-align: left; |
| 265 | background-color: #0f04; |
| 266 | } |
| 267 | .line-number { |
| 268 | text-align: right; |
| 269 | } |
| 270 | .covered-line { |
| 271 | text-align: right; |
| 272 | color: #06d; |
| 273 | } |
| 274 | .uncovered-line { |
| 275 | text-align: right; |
| 276 | color: #d00; |
| 277 | } |
| 278 | .uncovered-line.selected { |
| 279 | color: #f00; |
| 280 | font-weight: bold; |
| 281 | } |
| 282 | .region.red.selected { |
| 283 | background-color: #f008; |
| 284 | font-weight: bold; |
| 285 | } |
| 286 | .branch.red.selected { |
| 287 | background-color: #f008; |
| 288 | font-weight: bold; |
| 289 | } |
| 290 | .tooltip { |
| 291 | position: relative; |
| 292 | display: inline; |
| 293 | background-color: #bef; |
| 294 | text-decoration: none; |
| 295 | } |
| 296 | .tooltip span.tooltip-content { |
| 297 | position: absolute; |
| 298 | width: 100px; |
| 299 | margin-left: -50px; |
| 300 | color: #FFFFFF; |
| 301 | background: #000000; |
| 302 | height: 30px; |
| 303 | line-height: 30px; |
| 304 | text-align: center; |
| 305 | visibility: hidden; |
| 306 | border-radius: 6px; |
| 307 | } |
| 308 | .tooltip span.tooltip-content:after { |
| 309 | content: ''; |
| 310 | position: absolute; |
| 311 | top: 100%; |
| 312 | left: 50%; |
| 313 | margin-left: -8px; |
| 314 | width: 0; height: 0; |
| 315 | border-top: 8px solid #000000; |
| 316 | border-right: 8px solid transparent; |
| 317 | border-left: 8px solid transparent; |
| 318 | } |
| 319 | :hover.tooltip span.tooltip-content { |
| 320 | visibility: visible; |
| 321 | opacity: 0.8; |
| 322 | bottom: 30px; |
| 323 | left: 50%; |
| 324 | z-index: 999; |
| 325 | } |
| 326 | th, td { |
| 327 | vertical-align: top; |
| 328 | padding: 2px 8px; |
| 329 | border-collapse: collapse; |
| 330 | border-right: 1px solid #8888; |
| 331 | border-left: 1px solid #8888; |
| 332 | text-align: left; |
| 333 | } |
| 334 | td pre { |
| 335 | display: inline-block; |
| 336 | text-decoration: inherit; |
| 337 | } |
| 338 | td:first-child { |
| 339 | border-left: none; |
| 340 | } |
| 341 | td:last-child { |
| 342 | border-right: none; |
| 343 | } |
| 344 | tr:hover { |
| 345 | background-color: #eee; |
| 346 | } |
| 347 | tr:last-child { |
| 348 | border-bottom: none; |
| 349 | } |
| 350 | tr:has(> td >a:target), tr:has(> td.uncovered-line.selected) { |
| 351 | background-color: #8884; |
| 352 | } |
| 353 | a { |
| 354 | color: inherit; |
| 355 | } |
| 356 | .control { |
| 357 | position: fixed; |
| 358 | top: 0em; |
| 359 | right: 0em; |
| 360 | padding: 1em; |
| 361 | background: #FFF8; |
| 362 | } |
| 363 | @media (prefers-color-scheme: dark) { |
| 364 | body { |
| 365 | background-color: #222; |
| 366 | color: whitesmoke; |
| 367 | } |
| 368 | tr:hover { |
| 369 | background-color: #111; |
| 370 | } |
| 371 | .covered-line { |
| 372 | color: #39f; |
| 373 | } |
| 374 | .uncovered-line { |
| 375 | color: #f55; |
| 376 | } |
| 377 | .tooltip { |
| 378 | background-color: #068; |
| 379 | } |
| 380 | .control { |
| 381 | background: #2228; |
| 382 | } |
| 383 | tr:has(> td >a:target), tr:has(> td.uncovered-line.selected) { |
| 384 | background-color: #8884; |
| 385 | } |
| 386 | } |
| 387 | )" ; |
| 388 | |
| 389 | const char * = "</head>" ; |
| 390 | |
| 391 | const char *BeginCenteredDiv = "<div class='centered'>" ; |
| 392 | |
| 393 | const char *EndCenteredDiv = "</div>" ; |
| 394 | |
| 395 | const char *BeginSourceNameDiv = "<div class='source-name-title'>" ; |
| 396 | |
| 397 | const char *EndSourceNameDiv = "</div>" ; |
| 398 | |
| 399 | const char *BeginCodeTD = "<td class='code'>" ; |
| 400 | |
| 401 | const char *EndCodeTD = "</td>" ; |
| 402 | |
| 403 | const char *BeginPre = "<pre>" ; |
| 404 | |
| 405 | const char *EndPre = "</pre>" ; |
| 406 | |
| 407 | const char *BeginExpansionDiv = "<div class='expansion-view'>" ; |
| 408 | |
| 409 | const char *EndExpansionDiv = "</div>" ; |
| 410 | |
| 411 | const char *BeginTable = "<table>" ; |
| 412 | |
| 413 | const char *EndTable = "</table>" ; |
| 414 | |
| 415 | const char *ProjectTitleTag = "h1" ; |
| 416 | |
| 417 | const char *ReportTitleTag = "h2" ; |
| 418 | |
| 419 | const char *CreatedTimeTag = "h4" ; |
| 420 | |
| 421 | std::string getPathToStyle(StringRef ViewPath) { |
| 422 | std::string PathToStyle; |
| 423 | std::string PathSep = std::string(sys::path::get_separator()); |
| 424 | unsigned NumSeps = ViewPath.count(Str: PathSep); |
| 425 | for (unsigned I = 0, E = NumSeps; I < E; ++I) |
| 426 | PathToStyle += ".." + PathSep; |
| 427 | return PathToStyle + "style.css" ; |
| 428 | } |
| 429 | |
| 430 | std::string getPathToJavaScript(StringRef ViewPath) { |
| 431 | std::string PathToJavaScript; |
| 432 | std::string PathSep = std::string(sys::path::get_separator()); |
| 433 | unsigned NumSeps = ViewPath.count(Str: PathSep); |
| 434 | for (unsigned I = 0, E = NumSeps; I < E; ++I) |
| 435 | PathToJavaScript += ".." + PathSep; |
| 436 | return PathToJavaScript + "control.js" ; |
| 437 | } |
| 438 | |
| 439 | void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts, |
| 440 | const std::string &PathToStyle = "" , |
| 441 | const std::string &PathToJavaScript = "" ) { |
| 442 | OS << "<!doctype html>" |
| 443 | "<html>" |
| 444 | << BeginHeader; |
| 445 | |
| 446 | // Link to a stylesheet if one is available. Otherwise, use the default style. |
| 447 | if (PathToStyle.empty()) |
| 448 | OS << "<style>" << CSSForCoverage << "</style>" ; |
| 449 | else |
| 450 | OS << "<link rel='stylesheet' type='text/css' href='" |
| 451 | << escape(Str: PathToStyle, Opts) << "'>" ; |
| 452 | |
| 453 | // Link to a JavaScript if one is available |
| 454 | if (PathToJavaScript.empty()) |
| 455 | OS << "<script>" << JSForCoverage << "</script>" ; |
| 456 | else |
| 457 | OS << "<script src='" << escape(Str: PathToJavaScript, Opts) << "'></script>" ; |
| 458 | |
| 459 | OS << EndHeader << "<body>" ; |
| 460 | } |
| 461 | |
| 462 | void emitTableRow(raw_ostream &OS, const CoverageViewOptions &Opts, |
| 463 | const std::string &FirstCol, const FileCoverageSummary &FCS, |
| 464 | bool IsTotals) { |
| 465 | SmallVector<std::string, 8> Columns; |
| 466 | |
| 467 | // Format a coverage triple and add the result to the list of columns. |
| 468 | auto AddCoverageTripleToColumn = |
| 469 | [&Columns, &Opts](unsigned Hit, unsigned Total, float Pctg) { |
| 470 | std::string S; |
| 471 | { |
| 472 | raw_string_ostream RSO{S}; |
| 473 | if (Total) |
| 474 | RSO << format(Fmt: "%*.2f" , Vals: 7, Vals: Pctg) << "% " ; |
| 475 | else |
| 476 | RSO << "- " ; |
| 477 | RSO << '(' << Hit << '/' << Total << ')'; |
| 478 | } |
| 479 | const char *CellClass = "column-entry-yellow" ; |
| 480 | if (!Total) |
| 481 | CellClass = "column-entry-gray" ; |
| 482 | else if (Pctg >= Opts.HighCovWatermark) |
| 483 | CellClass = "column-entry-green" ; |
| 484 | else if (Pctg < Opts.LowCovWatermark) |
| 485 | CellClass = "column-entry-red" ; |
| 486 | Columns.emplace_back(Args: tag(Name: "td" , Str: tag(Name: "pre" , Str: S), ClassName: CellClass)); |
| 487 | }; |
| 488 | |
| 489 | Columns.emplace_back(Args: tag(Name: "td" , Str: tag(Name: "pre" , Str: FirstCol))); |
| 490 | AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(), |
| 491 | FCS.FunctionCoverage.getNumFunctions(), |
| 492 | FCS.FunctionCoverage.getPercentCovered()); |
| 493 | if (Opts.ShowInstantiationSummary) |
| 494 | AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(), |
| 495 | FCS.InstantiationCoverage.getNumFunctions(), |
| 496 | FCS.InstantiationCoverage.getPercentCovered()); |
| 497 | AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(), |
| 498 | FCS.LineCoverage.getNumLines(), |
| 499 | FCS.LineCoverage.getPercentCovered()); |
| 500 | if (Opts.ShowRegionSummary) |
| 501 | AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(), |
| 502 | FCS.RegionCoverage.getNumRegions(), |
| 503 | FCS.RegionCoverage.getPercentCovered()); |
| 504 | if (Opts.ShowBranchSummary) |
| 505 | AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(), |
| 506 | FCS.BranchCoverage.getNumBranches(), |
| 507 | FCS.BranchCoverage.getPercentCovered()); |
| 508 | if (Opts.ShowMCDCSummary) |
| 509 | AddCoverageTripleToColumn(FCS.MCDCCoverage.getCoveredPairs(), |
| 510 | FCS.MCDCCoverage.getNumPairs(), |
| 511 | FCS.MCDCCoverage.getPercentCovered()); |
| 512 | |
| 513 | if (IsTotals) |
| 514 | OS << tag(Name: "tr" , Str: join(Begin: Columns.begin(), End: Columns.end(), Separator: "" ), ClassName: "light-row-bold" ); |
| 515 | else |
| 516 | OS << tag(Name: "tr" , Str: join(Begin: Columns.begin(), End: Columns.end(), Separator: "" ), ClassName: "light-row" ); |
| 517 | } |
| 518 | |
| 519 | void emitEpilog(raw_ostream &OS) { |
| 520 | OS << "</body>" |
| 521 | << "</html>" ; |
| 522 | } |
| 523 | |
| 524 | } // anonymous namespace |
| 525 | |
| 526 | Expected<CoveragePrinter::OwnedStream> |
| 527 | CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) { |
| 528 | auto OSOrErr = createOutputStream(Path, Extension: "html" , InToplevel); |
| 529 | if (!OSOrErr) |
| 530 | return OSOrErr; |
| 531 | |
| 532 | OwnedStream OS = std::move(OSOrErr.get()); |
| 533 | |
| 534 | if (!Opts.hasOutputDirectory()) { |
| 535 | emitPrelude(OS&: *OS.get(), Opts); |
| 536 | } else { |
| 537 | std::string ViewPath = getOutputPath(Path, Extension: "html" , InToplevel); |
| 538 | emitPrelude(OS&: *OS.get(), Opts, PathToStyle: getPathToStyle(ViewPath), |
| 539 | PathToJavaScript: getPathToJavaScript(ViewPath)); |
| 540 | } |
| 541 | |
| 542 | return std::move(OS); |
| 543 | } |
| 544 | |
| 545 | void CoveragePrinterHTML::closeViewFile(OwnedStream OS) { |
| 546 | emitEpilog(OS&: *OS.get()); |
| 547 | } |
| 548 | |
| 549 | /// Emit column labels for the table in the index. |
| 550 | static void emitColumnLabelsForIndex(raw_ostream &OS, |
| 551 | const CoverageViewOptions &Opts) { |
| 552 | SmallVector<std::string, 4> Columns; |
| 553 | Columns.emplace_back(Args: tag(Name: "td" , Str: "Filename" , ClassName: "column-entry-bold" )); |
| 554 | Columns.emplace_back(Args: tag(Name: "td" , Str: "Function Coverage" , ClassName: "column-entry-bold" )); |
| 555 | if (Opts.ShowInstantiationSummary) |
| 556 | Columns.emplace_back( |
| 557 | Args: tag(Name: "td" , Str: "Instantiation Coverage" , ClassName: "column-entry-bold" )); |
| 558 | Columns.emplace_back(Args: tag(Name: "td" , Str: "Line Coverage" , ClassName: "column-entry-bold" )); |
| 559 | if (Opts.ShowRegionSummary) |
| 560 | Columns.emplace_back(Args: tag(Name: "td" , Str: "Region Coverage" , ClassName: "column-entry-bold" )); |
| 561 | if (Opts.ShowBranchSummary) |
| 562 | Columns.emplace_back(Args: tag(Name: "td" , Str: "Branch Coverage" , ClassName: "column-entry-bold" )); |
| 563 | if (Opts.ShowMCDCSummary) |
| 564 | Columns.emplace_back(Args: tag(Name: "td" , Str: "MC/DC" , ClassName: "column-entry-bold" )); |
| 565 | OS << tag(Name: "tr" , Str: join(Begin: Columns.begin(), End: Columns.end(), Separator: "" )); |
| 566 | } |
| 567 | |
| 568 | std::string |
| 569 | CoveragePrinterHTML::buildLinkToFile(StringRef SF, |
| 570 | const FileCoverageSummary &FCS) const { |
| 571 | SmallString<128> LinkTextStr(sys::path::relative_path(path: FCS.Name)); |
| 572 | sys::path::remove_dots(path&: LinkTextStr, /*remove_dot_dot=*/true); |
| 573 | sys::path::native(path&: LinkTextStr); |
| 574 | std::string LinkText = escape(Str: LinkTextStr, Opts); |
| 575 | std::string LinkTarget = |
| 576 | escape(Str: getOutputPath(Path: SF, Extension: "html" , /*InToplevel=*/false), Opts); |
| 577 | return a(Link: LinkTarget, Str: LinkText); |
| 578 | } |
| 579 | |
| 580 | Error CoveragePrinterHTML::emitStyleSheet() { |
| 581 | auto CSSOrErr = createOutputStream(Path: "style" , Extension: "css" , /*InToplevel=*/true); |
| 582 | if (Error E = CSSOrErr.takeError()) |
| 583 | return E; |
| 584 | |
| 585 | OwnedStream CSS = std::move(CSSOrErr.get()); |
| 586 | CSS->operator<<(Str: CSSForCoverage); |
| 587 | |
| 588 | return Error::success(); |
| 589 | } |
| 590 | |
| 591 | Error CoveragePrinterHTML::emitJavaScript() { |
| 592 | auto JSOrErr = createOutputStream(Path: "control" , Extension: "js" , /*InToplevel=*/true); |
| 593 | if (Error E = JSOrErr.takeError()) |
| 594 | return E; |
| 595 | |
| 596 | OwnedStream JS = std::move(JSOrErr.get()); |
| 597 | JS->operator<<(Str: JSForCoverage); |
| 598 | |
| 599 | return Error::success(); |
| 600 | } |
| 601 | |
| 602 | void CoveragePrinterHTML::(raw_ostream &OSRef, |
| 603 | const std::string &Title) { |
| 604 | // Emit some basic information about the coverage report. |
| 605 | if (Opts.hasProjectTitle()) |
| 606 | OSRef << tag(Name: ProjectTitleTag, Str: escape(Str: Opts.ProjectTitle, Opts)); |
| 607 | OSRef << tag(Name: ReportTitleTag, Str: Title); |
| 608 | if (Opts.hasCreatedTime()) |
| 609 | OSRef << tag(Name: CreatedTimeTag, Str: escape(Str: Opts.CreatedTimeStr, Opts)); |
| 610 | |
| 611 | // Emit a link to some documentation. |
| 612 | OSRef << tag(Name: "p" , Str: "Click " + |
| 613 | a(Link: "http://clang.llvm.org/docs/" |
| 614 | "SourceBasedCodeCoverage.html#interpreting-reports" , |
| 615 | Str: "here" ) + |
| 616 | " for information about interpreting this report." ); |
| 617 | |
| 618 | // Emit a table containing links to reports for each file in the covmapping. |
| 619 | // Exclude files which don't contain any regions. |
| 620 | OSRef << BeginCenteredDiv << BeginTable; |
| 621 | emitColumnLabelsForIndex(OS&: OSRef, Opts); |
| 622 | } |
| 623 | |
| 624 | /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is |
| 625 | /// false, link the summary to \p SF. |
| 626 | void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF, |
| 627 | const FileCoverageSummary &FCS, |
| 628 | bool IsTotals) const { |
| 629 | // Simplify the display file path, and wrap it in a link if requested. |
| 630 | std::string Filename; |
| 631 | if (IsTotals) { |
| 632 | Filename = std::string(SF); |
| 633 | } else { |
| 634 | Filename = buildLinkToFile(SF, FCS); |
| 635 | } |
| 636 | |
| 637 | emitTableRow(OS, Opts, FirstCol: Filename, FCS, IsTotals); |
| 638 | } |
| 639 | |
| 640 | Error CoveragePrinterHTML::createIndexFile( |
| 641 | ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage, |
| 642 | const CoverageFiltersMatchAll &Filters) { |
| 643 | // Emit the default stylesheet. |
| 644 | if (Error E = emitStyleSheet()) |
| 645 | return E; |
| 646 | |
| 647 | // Emit the JavaScript UI implementation |
| 648 | if (Error E = emitJavaScript()) |
| 649 | return E; |
| 650 | |
| 651 | // Emit a file index along with some coverage statistics. |
| 652 | auto OSOrErr = createOutputStream(Path: "index" , Extension: "html" , /*InToplevel=*/true); |
| 653 | if (Error E = OSOrErr.takeError()) |
| 654 | return E; |
| 655 | auto OS = std::move(OSOrErr.get()); |
| 656 | raw_ostream &OSRef = *OS.get(); |
| 657 | |
| 658 | assert(Opts.hasOutputDirectory() && "No output directory for index file" ); |
| 659 | emitPrelude(OS&: OSRef, Opts, PathToStyle: getPathToStyle(ViewPath: "" ), PathToJavaScript: getPathToJavaScript(ViewPath: "" )); |
| 660 | |
| 661 | emitReportHeader(OSRef, Title: "Coverage Report" ); |
| 662 | |
| 663 | FileCoverageSummary Totals("TOTALS" ); |
| 664 | auto FileReports = CoverageReport::prepareFileReports( |
| 665 | Coverage, Totals, Files: SourceFiles, Options: Opts, Filters); |
| 666 | bool EmptyFiles = false; |
| 667 | for (unsigned I = 0, E = FileReports.size(); I < E; ++I) { |
| 668 | if (FileReports[I].FunctionCoverage.getNumFunctions()) |
| 669 | emitFileSummary(OS&: OSRef, SF: SourceFiles[I], FCS: FileReports[I]); |
| 670 | else |
| 671 | EmptyFiles = true; |
| 672 | } |
| 673 | emitFileSummary(OS&: OSRef, SF: "Totals" , FCS: Totals, /*IsTotals=*/true); |
| 674 | OSRef << EndTable << EndCenteredDiv; |
| 675 | |
| 676 | // Emit links to files which don't contain any functions. These are normally |
| 677 | // not very useful, but could be relevant for code which abuses the |
| 678 | // preprocessor. |
| 679 | if (EmptyFiles && Filters.empty()) { |
| 680 | OSRef << tag(Name: "p" , Str: "Files which contain no functions. (These " |
| 681 | "files contain code pulled into other files " |
| 682 | "by the preprocessor.)\n" ); |
| 683 | OSRef << BeginCenteredDiv << BeginTable; |
| 684 | for (unsigned I = 0, E = FileReports.size(); I < E; ++I) |
| 685 | if (!FileReports[I].FunctionCoverage.getNumFunctions()) { |
| 686 | std::string Link = buildLinkToFile(SF: SourceFiles[I], FCS: FileReports[I]); |
| 687 | OSRef << tag(Name: "tr" , Str: tag(Name: "td" , Str: tag(Name: "pre" , Str: Link)), ClassName: "light-row" ) << '\n'; |
| 688 | } |
| 689 | OSRef << EndTable << EndCenteredDiv; |
| 690 | } |
| 691 | |
| 692 | OSRef << tag(Name: "h5" , Str: escape(Str: Opts.getLLVMVersionString(), Opts)); |
| 693 | emitEpilog(OS&: OSRef); |
| 694 | |
| 695 | return Error::success(); |
| 696 | } |
| 697 | |
| 698 | struct CoveragePrinterHTMLDirectory::Reporter : public DirectoryCoverageReport { |
| 699 | CoveragePrinterHTMLDirectory &Printer; |
| 700 | |
| 701 | Reporter(CoveragePrinterHTMLDirectory &Printer, |
| 702 | const coverage::CoverageMapping &Coverage, |
| 703 | const CoverageFiltersMatchAll &Filters) |
| 704 | : DirectoryCoverageReport(Printer.Opts, Coverage, Filters), |
| 705 | Printer(Printer) {} |
| 706 | |
| 707 | Error generateSubDirectoryReport(SubFileReports &&SubFiles, |
| 708 | SubDirReports &&SubDirs, |
| 709 | FileCoverageSummary &&SubTotals) override { |
| 710 | auto &LCPath = SubTotals.Name; |
| 711 | assert(Options.hasOutputDirectory() && |
| 712 | "No output directory for index file" ); |
| 713 | |
| 714 | SmallString<128> OSPath = LCPath; |
| 715 | sys::path::append(path&: OSPath, a: "index" ); |
| 716 | auto OSOrErr = Printer.createOutputStream(Path: OSPath, Extension: "html" , |
| 717 | /*InToplevel=*/false); |
| 718 | if (auto E = OSOrErr.takeError()) |
| 719 | return E; |
| 720 | auto OS = std::move(OSOrErr.get()); |
| 721 | raw_ostream &OSRef = *OS.get(); |
| 722 | |
| 723 | auto IndexHtmlPath = Printer.getOutputPath(Path: (LCPath + "index" ).str(), Extension: "html" , |
| 724 | /*InToplevel=*/false); |
| 725 | emitPrelude(OS&: OSRef, Opts: Options, PathToStyle: getPathToStyle(ViewPath: IndexHtmlPath), |
| 726 | PathToJavaScript: getPathToJavaScript(ViewPath: IndexHtmlPath)); |
| 727 | |
| 728 | auto NavLink = buildTitleLinks(LCPath); |
| 729 | Printer.emitReportHeader(OSRef, Title: "Coverage Report (" + NavLink + ")" ); |
| 730 | |
| 731 | std::vector<const FileCoverageSummary *> EmptyFiles; |
| 732 | |
| 733 | // Make directories at the top of the table. |
| 734 | for (auto &&SubDir : SubDirs) { |
| 735 | auto &Report = SubDir.second.first; |
| 736 | if (!Report.FunctionCoverage.getNumFunctions()) |
| 737 | EmptyFiles.push_back(x: &Report); |
| 738 | else |
| 739 | emitTableRow(OS&: OSRef, Opts: Options, FirstCol: buildRelLinkToFile(RelPath: Report.Name), FCS: Report, |
| 740 | /*IsTotals=*/false); |
| 741 | } |
| 742 | |
| 743 | for (auto &&SubFile : SubFiles) { |
| 744 | auto &Report = SubFile.second; |
| 745 | if (!Report.FunctionCoverage.getNumFunctions()) |
| 746 | EmptyFiles.push_back(x: &Report); |
| 747 | else |
| 748 | emitTableRow(OS&: OSRef, Opts: Options, FirstCol: buildRelLinkToFile(RelPath: Report.Name), FCS: Report, |
| 749 | /*IsTotals=*/false); |
| 750 | } |
| 751 | |
| 752 | // Emit the totals row. |
| 753 | emitTableRow(OS&: OSRef, Opts: Options, FirstCol: "Totals" , FCS: SubTotals, /*IsTotals=*/false); |
| 754 | OSRef << EndTable << EndCenteredDiv; |
| 755 | |
| 756 | // Emit links to files which don't contain any functions. These are normally |
| 757 | // not very useful, but could be relevant for code which abuses the |
| 758 | // preprocessor. |
| 759 | if (!EmptyFiles.empty()) { |
| 760 | OSRef << tag(Name: "p" , Str: "Files which contain no functions. (These " |
| 761 | "files contain code pulled into other files " |
| 762 | "by the preprocessor.)\n" ); |
| 763 | OSRef << BeginCenteredDiv << BeginTable; |
| 764 | for (auto FCS : EmptyFiles) { |
| 765 | auto Link = buildRelLinkToFile(RelPath: FCS->Name); |
| 766 | OSRef << tag(Name: "tr" , Str: tag(Name: "td" , Str: tag(Name: "pre" , Str: Link)), ClassName: "light-row" ) << '\n'; |
| 767 | } |
| 768 | OSRef << EndTable << EndCenteredDiv; |
| 769 | } |
| 770 | |
| 771 | // Emit epilog. |
| 772 | OSRef << tag(Name: "h5" , Str: escape(Str: Options.getLLVMVersionString(), Opts: Options)); |
| 773 | emitEpilog(OS&: OSRef); |
| 774 | |
| 775 | return Error::success(); |
| 776 | } |
| 777 | |
| 778 | /// Make a title with hyperlinks to the index.html files of each hierarchy |
| 779 | /// of the report. |
| 780 | std::string buildTitleLinks(StringRef LCPath) const { |
| 781 | // For each report level in LCPStack, extract the path component and |
| 782 | // calculate the number of "../" relative to current LCPath. |
| 783 | SmallVector<std::pair<SmallString<128>, unsigned>, 16> Components; |
| 784 | |
| 785 | auto Iter = LCPStack.begin(), IterE = LCPStack.end(); |
| 786 | SmallString<128> RootPath; |
| 787 | if (*Iter == 0) { |
| 788 | // If llvm-cov works on relative coverage mapping data, the LCP of |
| 789 | // all source file paths can be 0, which makes the title path empty. |
| 790 | // As we like adding a slash at the back of the path to indicate a |
| 791 | // directory, in this case, we use "." as the root path to make it |
| 792 | // not be confused with the root path "/". |
| 793 | RootPath = "." ; |
| 794 | } else { |
| 795 | RootPath = LCPath.substr(Start: 0, N: *Iter); |
| 796 | sys::path::native(path&: RootPath); |
| 797 | sys::path::remove_dots(path&: RootPath, /*remove_dot_dot=*/true); |
| 798 | } |
| 799 | Components.emplace_back(Args: std::move(RootPath), Args: 0); |
| 800 | |
| 801 | for (auto Last = *Iter; ++Iter != IterE; Last = *Iter) { |
| 802 | SmallString<128> SubPath = LCPath.substr(Start: Last, N: *Iter - Last); |
| 803 | sys::path::native(path&: SubPath); |
| 804 | sys::path::remove_dots(path&: SubPath, /*remove_dot_dot=*/true); |
| 805 | auto Level = unsigned(SubPath.count(Str: sys::path::get_separator())) + 1; |
| 806 | Components.back().second += Level; |
| 807 | Components.emplace_back(Args: std::move(SubPath), Args&: Level); |
| 808 | } |
| 809 | |
| 810 | // Then we make the title accroding to Components. |
| 811 | std::string S; |
| 812 | for (auto I = Components.begin(), E = Components.end();;) { |
| 813 | auto &Name = I->first; |
| 814 | if (++I == E) { |
| 815 | S += a(Link: "./index.html" , Str: Name); |
| 816 | S += sys::path::get_separator(); |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | SmallString<128> Link; |
| 821 | for (unsigned J = I->second; J > 0; --J) |
| 822 | Link += "../" ; |
| 823 | Link += "index.html" ; |
| 824 | S += a(Link, Str: Name); |
| 825 | S += sys::path::get_separator(); |
| 826 | } |
| 827 | return S; |
| 828 | } |
| 829 | |
| 830 | std::string buildRelLinkToFile(StringRef RelPath) const { |
| 831 | SmallString<128> LinkTextStr(RelPath); |
| 832 | sys::path::native(path&: LinkTextStr); |
| 833 | |
| 834 | // remove_dots will remove trailing slash, so we need to check before it. |
| 835 | auto IsDir = LinkTextStr.ends_with(Suffix: sys::path::get_separator()); |
| 836 | sys::path::remove_dots(path&: LinkTextStr, /*remove_dot_dot=*/true); |
| 837 | |
| 838 | SmallString<128> LinkTargetStr(LinkTextStr); |
| 839 | if (IsDir) { |
| 840 | LinkTextStr += sys::path::get_separator(); |
| 841 | sys::path::append(path&: LinkTargetStr, a: "index.html" ); |
| 842 | } else { |
| 843 | LinkTargetStr += ".html" ; |
| 844 | } |
| 845 | |
| 846 | auto LinkText = escape(Str: LinkTextStr, Opts: Options); |
| 847 | auto LinkTarget = escape(Str: LinkTargetStr, Opts: Options); |
| 848 | return a(Link: LinkTarget, Str: LinkText); |
| 849 | } |
| 850 | }; |
| 851 | |
| 852 | Error CoveragePrinterHTMLDirectory::createIndexFile( |
| 853 | ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage, |
| 854 | const CoverageFiltersMatchAll &Filters) { |
| 855 | // The createSubIndexFile function only works when SourceFiles is |
| 856 | // more than one. So we fallback to CoveragePrinterHTML when it is. |
| 857 | if (SourceFiles.size() <= 1) |
| 858 | return CoveragePrinterHTML::createIndexFile(SourceFiles, Coverage, Filters); |
| 859 | |
| 860 | // Emit the default stylesheet. |
| 861 | if (Error E = emitStyleSheet()) |
| 862 | return E; |
| 863 | |
| 864 | // Emit the JavaScript UI implementation |
| 865 | if (Error E = emitJavaScript()) |
| 866 | return E; |
| 867 | |
| 868 | // Emit index files in every subdirectory. |
| 869 | Reporter Report(*this, Coverage, Filters); |
| 870 | auto TotalsOrErr = Report.prepareDirectoryReports(SourceFiles); |
| 871 | if (auto E = TotalsOrErr.takeError()) |
| 872 | return E; |
| 873 | auto &LCPath = TotalsOrErr->Name; |
| 874 | |
| 875 | // Emit the top level index file. Top level index file is just a redirection |
| 876 | // to the index file in the LCP directory. |
| 877 | auto OSOrErr = createOutputStream(Path: "index" , Extension: "html" , /*InToplevel=*/true); |
| 878 | if (auto E = OSOrErr.takeError()) |
| 879 | return E; |
| 880 | auto OS = std::move(OSOrErr.get()); |
| 881 | auto LCPIndexFilePath = |
| 882 | getOutputPath(Path: (LCPath + "index" ).str(), Extension: "html" , /*InToplevel=*/false); |
| 883 | *OS.get() << R"(<!DOCTYPE html> |
| 884 | <html> |
| 885 | <head> |
| 886 | <meta http-equiv="Refresh" content="0; url=')" |
| 887 | << LCPIndexFilePath << R"('" /> |
| 888 | </head> |
| 889 | <body></body> |
| 890 | </html> |
| 891 | )" ; |
| 892 | |
| 893 | return Error::success(); |
| 894 | } |
| 895 | |
| 896 | void SourceCoverageViewHTML::(raw_ostream &OS) { |
| 897 | OS << BeginCenteredDiv << BeginTable; |
| 898 | } |
| 899 | |
| 900 | void SourceCoverageViewHTML::(raw_ostream &OS) { |
| 901 | OS << EndTable << EndCenteredDiv; |
| 902 | } |
| 903 | |
| 904 | void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS, bool WholeFile) { |
| 905 | OS << BeginSourceNameDiv << tag(Name: "pre" , Str: escape(Str: getSourceName(), Opts: getOptions())) |
| 906 | << EndSourceNameDiv; |
| 907 | } |
| 908 | |
| 909 | void SourceCoverageViewHTML::renderLinePrefix(raw_ostream &OS, unsigned) { |
| 910 | OS << "<tr>" ; |
| 911 | } |
| 912 | |
| 913 | void SourceCoverageViewHTML::renderLineSuffix(raw_ostream &OS, unsigned) { |
| 914 | // If this view has sub-views, renderLine() cannot close the view's cell. |
| 915 | // Take care of it here, after all sub-views have been rendered. |
| 916 | if (hasSubViews()) |
| 917 | OS << EndCodeTD; |
| 918 | OS << "</tr>" ; |
| 919 | } |
| 920 | |
| 921 | void SourceCoverageViewHTML::renderViewDivider(raw_ostream &, unsigned) { |
| 922 | // The table-based output makes view dividers unnecessary. |
| 923 | } |
| 924 | |
| 925 | void SourceCoverageViewHTML::renderLine(raw_ostream &OS, LineRef L, |
| 926 | const LineCoverageStats &LCS, |
| 927 | unsigned ExpansionCol, unsigned) { |
| 928 | StringRef Line = L.Line; |
| 929 | unsigned LineNo = L.LineNo; |
| 930 | |
| 931 | // Steps for handling text-escaping, highlighting, and tooltip creation: |
| 932 | // |
| 933 | // 1. Split the line into N+1 snippets, where N = |Segments|. The first |
| 934 | // snippet starts from Col=1 and ends at the start of the first segment. |
| 935 | // The last snippet starts at the last mapped column in the line and ends |
| 936 | // at the end of the line. Both are required but may be empty. |
| 937 | |
| 938 | SmallVector<std::string, 8> Snippets; |
| 939 | CoverageSegmentArray Segments = LCS.getLineSegments(); |
| 940 | |
| 941 | unsigned LCol = 1; |
| 942 | auto Snip = [&](unsigned Start, unsigned Len) { |
| 943 | Snippets.push_back(Elt: std::string(Line.substr(Start, N: Len))); |
| 944 | LCol += Len; |
| 945 | }; |
| 946 | |
| 947 | Snip(LCol - 1, Segments.empty() ? 0 : (Segments.front()->Col - 1)); |
| 948 | |
| 949 | for (unsigned I = 1, E = Segments.size(); I < E; ++I) |
| 950 | Snip(LCol - 1, Segments[I]->Col - LCol); |
| 951 | |
| 952 | // |Line| + 1 is needed to avoid underflow when, e.g |Line| = 0 and LCol = 1. |
| 953 | Snip(LCol - 1, Line.size() + 1 - LCol); |
| 954 | |
| 955 | // 2. Escape all of the snippets. |
| 956 | |
| 957 | for (std::string &Snippet : Snippets) |
| 958 | Snippet = escape(Str: Snippet, Opts: getOptions()); |
| 959 | |
| 960 | // 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment |
| 961 | // 1 to set the highlight for snippet 2, segment 2 to set the highlight for |
| 962 | // snippet 3, and so on. |
| 963 | |
| 964 | std::optional<StringRef> Color; |
| 965 | SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; |
| 966 | auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) { |
| 967 | if (getOptions().Debug) |
| 968 | HighlightedRanges.emplace_back(Args&: LC, Args&: RC); |
| 969 | if (Snippet.empty()) |
| 970 | return tag(Name: "span" , Str: Snippet, ClassName: std::string(*Color)); |
| 971 | else |
| 972 | return tag(Name: "span" , Str: Snippet, ClassName: "region " + std::string(*Color)); |
| 973 | }; |
| 974 | |
| 975 | auto CheckIfUncovered = [&](const CoverageSegment *S) { |
| 976 | return S && (!S->IsGapRegion || (Color && *Color == "red" )) && |
| 977 | S->HasCount && S->Count == 0; |
| 978 | }; |
| 979 | |
| 980 | if (CheckIfUncovered(LCS.getWrappedSegment())) { |
| 981 | Color = "red" ; |
| 982 | if (!Snippets[0].empty()) |
| 983 | Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size()); |
| 984 | } |
| 985 | |
| 986 | for (unsigned I = 0, E = Segments.size(); I < E; ++I) { |
| 987 | const auto *CurSeg = Segments[I]; |
| 988 | if (CheckIfUncovered(CurSeg)) |
| 989 | Color = "red" ; |
| 990 | else if (CurSeg->Col == ExpansionCol) |
| 991 | Color = "cyan" ; |
| 992 | else |
| 993 | Color = std::nullopt; |
| 994 | |
| 995 | if (Color) |
| 996 | Snippets[I + 1] = Highlight(Snippets[I + 1], CurSeg->Col, |
| 997 | CurSeg->Col + Snippets[I + 1].size()); |
| 998 | } |
| 999 | |
| 1000 | if (Color && Segments.empty()) |
| 1001 | Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size()); |
| 1002 | |
| 1003 | if (getOptions().Debug) { |
| 1004 | for (const auto &Range : HighlightedRanges) { |
| 1005 | errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> " ; |
| 1006 | if (Range.second == 0) |
| 1007 | errs() << "?" ; |
| 1008 | else |
| 1009 | errs() << Range.second; |
| 1010 | errs() << "\n" ; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate |
| 1015 | // sub-line region count tooltips if needed. |
| 1016 | |
| 1017 | if (shouldRenderRegionMarkers(LCS)) { |
| 1018 | // Just consider the segments which start *and* end on this line. |
| 1019 | for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) { |
| 1020 | const auto *CurSeg = Segments[I]; |
| 1021 | auto CurSegCount = BinaryCount(N: CurSeg->Count); |
| 1022 | auto LCSCount = BinaryCount(N: LCS.getExecutionCount()); |
| 1023 | if (!CurSeg->IsRegionEntry) |
| 1024 | continue; |
| 1025 | if (CurSegCount == LCSCount) |
| 1026 | continue; |
| 1027 | |
| 1028 | Snippets[I + 1] = |
| 1029 | tag(Name: "div" , |
| 1030 | Str: Snippets[I + 1] + |
| 1031 | tag(Name: "span" , Str: formatCount(N: CurSegCount), ClassName: "tooltip-content" ), |
| 1032 | ClassName: "tooltip" ); |
| 1033 | |
| 1034 | if (getOptions().Debug) |
| 1035 | errs() << "Marker at " << CurSeg->Line << ":" << CurSeg->Col << " = " |
| 1036 | << formatCount(N: CurSegCount) << "\n" ; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | OS << BeginCodeTD; |
| 1041 | OS << BeginPre; |
| 1042 | for (const auto &Snippet : Snippets) |
| 1043 | OS << Snippet; |
| 1044 | OS << EndPre; |
| 1045 | |
| 1046 | // If there are no sub-views left to attach to this cell, end the cell. |
| 1047 | // Otherwise, end it after the sub-views are rendered (renderLineSuffix()). |
| 1048 | if (!hasSubViews()) |
| 1049 | OS << EndCodeTD; |
| 1050 | } |
| 1051 | |
| 1052 | void SourceCoverageViewHTML::renderLineCoverageColumn( |
| 1053 | raw_ostream &OS, const LineCoverageStats &Line) { |
| 1054 | std::string Count; |
| 1055 | if (Line.isMapped()) |
| 1056 | Count = tag(Name: "pre" , Str: formatBinaryCount(N: Line.getExecutionCount())); |
| 1057 | std::string CoverageClass = |
| 1058 | (Line.getExecutionCount() > 0) |
| 1059 | ? "covered-line" |
| 1060 | : (Line.isMapped() ? "uncovered-line" : "skipped-line" ); |
| 1061 | OS << tag(Name: "td" , Str: Count, ClassName: CoverageClass); |
| 1062 | } |
| 1063 | |
| 1064 | void SourceCoverageViewHTML::renderLineNumberColumn(raw_ostream &OS, |
| 1065 | unsigned LineNo) { |
| 1066 | std::string LineNoStr = utostr(X: uint64_t(LineNo)); |
| 1067 | std::string TargetName = "L" + LineNoStr; |
| 1068 | OS << tag(Name: "td" , Str: a(Link: "#" + TargetName, Str: tag(Name: "pre" , Str: LineNoStr), TargetName), |
| 1069 | ClassName: "line-number" ); |
| 1070 | } |
| 1071 | |
| 1072 | void SourceCoverageViewHTML::renderRegionMarkers(raw_ostream &, |
| 1073 | const LineCoverageStats &Line, |
| 1074 | unsigned) { |
| 1075 | // Region markers are rendered in-line using tooltips. |
| 1076 | } |
| 1077 | |
| 1078 | void SourceCoverageViewHTML::renderExpansionSite(raw_ostream &OS, LineRef L, |
| 1079 | const LineCoverageStats &LCS, |
| 1080 | unsigned ExpansionCol, |
| 1081 | unsigned ViewDepth) { |
| 1082 | // Render the line containing the expansion site. No extra formatting needed. |
| 1083 | renderLine(OS, L, LCS, ExpansionCol, ViewDepth); |
| 1084 | } |
| 1085 | |
| 1086 | void SourceCoverageViewHTML::renderExpansionView(raw_ostream &OS, |
| 1087 | ExpansionView &ESV, |
| 1088 | unsigned ViewDepth) { |
| 1089 | OS << BeginExpansionDiv; |
| 1090 | ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, |
| 1091 | /*ShowTitle=*/false, ViewDepth: ViewDepth + 1); |
| 1092 | OS << EndExpansionDiv; |
| 1093 | } |
| 1094 | |
| 1095 | void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV, |
| 1096 | unsigned ViewDepth) { |
| 1097 | // Render the child subview. |
| 1098 | if (getOptions().Debug) |
| 1099 | errs() << "Branch at line " << BRV.getLine() << '\n'; |
| 1100 | |
| 1101 | auto BranchCount = [&](StringRef Label, uint64_t Count, bool Folded, |
| 1102 | double Total) { |
| 1103 | if (Folded) |
| 1104 | return std::string{"Folded" }; |
| 1105 | |
| 1106 | std::string Str; |
| 1107 | raw_string_ostream OS(Str); |
| 1108 | |
| 1109 | OS << tag(Name: "span" , Str: Label, ClassName: (Count ? "None" : "red branch" )) << ": " ; |
| 1110 | if (getOptions().ShowBranchCounts) |
| 1111 | OS << tag(Name: "span" , Str: formatBinaryCount(N: Count), |
| 1112 | ClassName: (Count ? "covered-line" : "uncovered-line" )); |
| 1113 | else |
| 1114 | OS << format(Fmt: "%0.2f" , Vals: (Total != 0 ? 100.0 * Count / Total : 0.0)) << "%" ; |
| 1115 | |
| 1116 | return Str; |
| 1117 | }; |
| 1118 | |
| 1119 | OS << BeginExpansionDiv; |
| 1120 | OS << BeginPre; |
| 1121 | for (const auto &R : BRV.Regions) { |
| 1122 | // This can be `double` since it is only used as a denominator. |
| 1123 | // FIXME: It is still inaccurate if Count is greater than (1LL << 53). |
| 1124 | double Total = |
| 1125 | static_cast<double>(R.ExecutionCount) + R.FalseExecutionCount; |
| 1126 | |
| 1127 | // Display Line + Column. |
| 1128 | std::string LineNoStr = utostr(X: uint64_t(R.LineStart)); |
| 1129 | std::string ColNoStr = utostr(X: uint64_t(R.ColumnStart)); |
| 1130 | std::string TargetName = "L" + LineNoStr; |
| 1131 | |
| 1132 | OS << " Branch (" ; |
| 1133 | OS << tag(Name: "span" , |
| 1134 | Str: a(Link: "#" + TargetName, Str: tag(Name: "span" , Str: LineNoStr + ":" + ColNoStr), |
| 1135 | TargetName), |
| 1136 | ClassName: "line-number" ) + |
| 1137 | "): [" ; |
| 1138 | |
| 1139 | if (R.TrueFolded && R.FalseFolded) { |
| 1140 | OS << "Folded - Ignored]\n" ; |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | OS << BranchCount("True" , R.ExecutionCount, R.TrueFolded, Total) << ", " |
| 1145 | << BranchCount("False" , R.FalseExecutionCount, R.FalseFolded, Total) |
| 1146 | << "]\n" ; |
| 1147 | } |
| 1148 | OS << EndPre; |
| 1149 | OS << EndExpansionDiv; |
| 1150 | } |
| 1151 | |
| 1152 | void SourceCoverageViewHTML::renderMCDCView(raw_ostream &OS, MCDCView &MRV, |
| 1153 | unsigned ViewDepth) { |
| 1154 | const bool ShowNonExecutedVectors = getOptions().ShowMCDCNonExecutedVectors; |
| 1155 | |
| 1156 | for (auto &Record : MRV.Records) { |
| 1157 | OS << BeginExpansionDiv; |
| 1158 | OS << BeginPre; |
| 1159 | OS << " MC/DC Decision Region (" ; |
| 1160 | |
| 1161 | // Display Line + Column information. |
| 1162 | const CounterMappingRegion &DecisionRegion = Record.getDecisionRegion(); |
| 1163 | std::string LineNoStr = Twine(DecisionRegion.LineStart).str(); |
| 1164 | std::string ColNoStr = Twine(DecisionRegion.ColumnStart).str(); |
| 1165 | std::string TargetName = "L" + LineNoStr; |
| 1166 | OS << tag(Name: "span" , |
| 1167 | Str: a(Link: "#" + TargetName, Str: tag(Name: "span" , Str: LineNoStr + ":" + ColNoStr)), |
| 1168 | ClassName: "line-number" ) + |
| 1169 | ") to (" ; |
| 1170 | LineNoStr = utostr(X: uint64_t(DecisionRegion.LineEnd)); |
| 1171 | ColNoStr = utostr(X: uint64_t(DecisionRegion.ColumnEnd)); |
| 1172 | OS << tag(Name: "span" , |
| 1173 | Str: a(Link: "#" + TargetName, Str: tag(Name: "span" , Str: LineNoStr + ":" + ColNoStr)), |
| 1174 | ClassName: "line-number" ) + |
| 1175 | ")\n\n" ; |
| 1176 | |
| 1177 | // Display MC/DC Information. |
| 1178 | OS << " Number of Conditions: " << Record.getNumConditions() << "\n" ; |
| 1179 | for (unsigned i = 0; i < Record.getNumConditions(); i++) { |
| 1180 | OS << " " << Record.getConditionHeaderString(Condition: i); |
| 1181 | } |
| 1182 | OS << "\n" ; |
| 1183 | OS << " MC/DC Test Vectors\n\n" ; |
| 1184 | |
| 1185 | const unsigned NumExecuted = Record.getNumTestVectors(); |
| 1186 | const unsigned NumNotExecuted = Record.getNumNotExecutedTestVectors(); |
| 1187 | |
| 1188 | const std::string = Record.getTestVectorHeaderString(); |
| 1189 | |
| 1190 | OS << " Executed:\n\n " ; |
| 1191 | if (NumExecuted == 0) { |
| 1192 | OS << "None.\n" ; |
| 1193 | } else { |
| 1194 | OS << HeaderStr; |
| 1195 | for (unsigned k = 0; k < NumExecuted; k++) |
| 1196 | OS << Record.getTestVectorString(TestVectorIndex: k); |
| 1197 | } |
| 1198 | |
| 1199 | if (ShowNonExecutedVectors) { |
| 1200 | OS << "\n Not executed:\n\n " ; |
| 1201 | if (NumNotExecuted == 0) { |
| 1202 | OS << "None.\n" ; |
| 1203 | } else { |
| 1204 | OS << HeaderStr; |
| 1205 | for (unsigned k = 0; k < NumNotExecuted; k++) |
| 1206 | OS << Record.getNotExecutedTestVectorString(NotExecutedIndex: k); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | OS << "\n" ; |
| 1211 | for (unsigned i = 0; i < Record.getNumConditions(); i++) |
| 1212 | OS << Record.getConditionCoverageString(Condition: i); |
| 1213 | OS << " MC/DC Coverage for Expression: " ; |
| 1214 | OS << format(Fmt: "%0.2f" , Vals: Record.getPercentCovered()) << "%\n" ; |
| 1215 | OS << EndPre; |
| 1216 | OS << EndExpansionDiv; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS, |
| 1221 | InstantiationView &ISV, |
| 1222 | unsigned ViewDepth) { |
| 1223 | OS << BeginExpansionDiv; |
| 1224 | if (!ISV.View) |
| 1225 | OS << BeginSourceNameDiv |
| 1226 | << tag(Name: "pre" , |
| 1227 | Str: escape(Str: "Unexecuted instantiation: " + ISV.FunctionName.str(), |
| 1228 | Opts: getOptions())) |
| 1229 | << EndSourceNameDiv; |
| 1230 | else |
| 1231 | ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, |
| 1232 | /*ShowTitle=*/false, ViewDepth); |
| 1233 | OS << EndExpansionDiv; |
| 1234 | } |
| 1235 | |
| 1236 | void SourceCoverageViewHTML::renderTitle(raw_ostream &OS, StringRef Title) { |
| 1237 | if (getOptions().hasProjectTitle()) |
| 1238 | OS << tag(Name: ProjectTitleTag, Str: escape(Str: getOptions().ProjectTitle, Opts: getOptions())); |
| 1239 | OS << tag(Name: ReportTitleTag, Str: escape(Str: Title, Opts: getOptions())); |
| 1240 | if (getOptions().hasCreatedTime()) |
| 1241 | OS << tag(Name: CreatedTimeTag, |
| 1242 | Str: escape(Str: getOptions().CreatedTimeStr, Opts: getOptions())); |
| 1243 | |
| 1244 | OS << tag(Name: "span" , |
| 1245 | Str: a(Link: "javascript:next_line()" , Str: "next uncovered line (L)" ) + ", " + |
| 1246 | a(Link: "javascript:next_region()" , Str: "next uncovered region (R)" ) + |
| 1247 | ", " + |
| 1248 | a(Link: "javascript:next_branch()" , Str: "next uncovered branch (B)" ), |
| 1249 | ClassName: "control" ); |
| 1250 | } |
| 1251 | |
| 1252 | void SourceCoverageViewHTML::(raw_ostream &OS, |
| 1253 | unsigned ViewDepth) { |
| 1254 | std::string Links; |
| 1255 | |
| 1256 | renderLinePrefix(OS, ViewDepth); |
| 1257 | OS << tag(Name: "td" , Str: tag(Name: "pre" , Str: "Line" )) << tag(Name: "td" , Str: tag(Name: "pre" , Str: "Count" )); |
| 1258 | OS << tag(Name: "td" , Str: tag(Name: "pre" , Str: "Source" + Links)); |
| 1259 | renderLineSuffix(OS, ViewDepth); |
| 1260 | } |
| 1261 | |