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