/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- HTMLDiagnostics.cpp - HTML Diagnostics for Paths -------------------===// |
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 | | // This file defines the HTMLDiagnostics object. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/Decl.h" |
14 | | #include "clang/AST/DeclBase.h" |
15 | | #include "clang/AST/Stmt.h" |
16 | | #include "clang/Analysis/IssueHash.h" |
17 | | #include "clang/Analysis/MacroExpansionContext.h" |
18 | | #include "clang/Analysis/PathDiagnostic.h" |
19 | | #include "clang/Basic/FileManager.h" |
20 | | #include "clang/Basic/LLVM.h" |
21 | | #include "clang/Basic/SourceLocation.h" |
22 | | #include "clang/Basic/SourceManager.h" |
23 | | #include "clang/Lex/Lexer.h" |
24 | | #include "clang/Lex/Preprocessor.h" |
25 | | #include "clang/Lex/Token.h" |
26 | | #include "clang/Rewrite/Core/HTMLRewrite.h" |
27 | | #include "clang/Rewrite/Core/Rewriter.h" |
28 | | #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" |
29 | | #include "llvm/ADT/ArrayRef.h" |
30 | | #include "llvm/ADT/SmallString.h" |
31 | | #include "llvm/ADT/StringRef.h" |
32 | | #include "llvm/ADT/iterator_range.h" |
33 | | #include "llvm/Support/Casting.h" |
34 | | #include "llvm/Support/Errc.h" |
35 | | #include "llvm/Support/ErrorHandling.h" |
36 | | #include "llvm/Support/FileSystem.h" |
37 | | #include "llvm/Support/MemoryBuffer.h" |
38 | | #include "llvm/Support/Path.h" |
39 | | #include "llvm/Support/raw_ostream.h" |
40 | | #include <algorithm> |
41 | | #include <cassert> |
42 | | #include <map> |
43 | | #include <memory> |
44 | | #include <set> |
45 | | #include <sstream> |
46 | | #include <string> |
47 | | #include <system_error> |
48 | | #include <utility> |
49 | | #include <vector> |
50 | | |
51 | | using namespace clang; |
52 | | using namespace ento; |
53 | | |
54 | | //===----------------------------------------------------------------------===// |
55 | | // Boilerplate. |
56 | | //===----------------------------------------------------------------------===// |
57 | | |
58 | | namespace { |
59 | | |
60 | | class HTMLDiagnostics : public PathDiagnosticConsumer { |
61 | | PathDiagnosticConsumerOptions DiagOpts; |
62 | | std::string Directory; |
63 | | bool createdDir = false; |
64 | | bool noDir = false; |
65 | | const Preprocessor &PP; |
66 | | const bool SupportsCrossFileDiagnostics; |
67 | | |
68 | | public: |
69 | | HTMLDiagnostics(PathDiagnosticConsumerOptions DiagOpts, |
70 | | const std::string &OutputDir, const Preprocessor &pp, |
71 | | bool supportsMultipleFiles) |
72 | | : DiagOpts(std::move(DiagOpts)), Directory(OutputDir), PP(pp), |
73 | 45 | SupportsCrossFileDiagnostics(supportsMultipleFiles) {} |
74 | | |
75 | 45 | ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); } |
76 | | |
77 | | void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, |
78 | | FilesMade *filesMade) override; |
79 | | |
80 | 94 | StringRef getName() const override { |
81 | 94 | return "HTMLDiagnostics"; |
82 | 94 | } |
83 | | |
84 | 101 | bool supportsCrossFileDiagnostics() const override { |
85 | 101 | return SupportsCrossFileDiagnostics; |
86 | 101 | } |
87 | | |
88 | | unsigned ProcessMacroPiece(raw_ostream &os, |
89 | | const PathDiagnosticMacroPiece& P, |
90 | | unsigned num); |
91 | | |
92 | | void HandlePiece(Rewriter &R, FileID BugFileID, const PathDiagnosticPiece &P, |
93 | | const std::vector<SourceRange> &PopUpRanges, unsigned num, |
94 | | unsigned max); |
95 | | |
96 | | void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, |
97 | | const char *HighlightStart = "<span class=\"mrange\">", |
98 | | const char *HighlightEnd = "</span>"); |
99 | | |
100 | | void ReportDiag(const PathDiagnostic& D, |
101 | | FilesMade *filesMade); |
102 | | |
103 | | // Generate the full HTML report |
104 | | std::string GenerateHTML(const PathDiagnostic& D, Rewriter &R, |
105 | | const SourceManager& SMgr, const PathPieces& path, |
106 | | const char *declName); |
107 | | |
108 | | // Add HTML header/footers to file specified by FID |
109 | | void FinalizeHTML(const PathDiagnostic& D, Rewriter &R, |
110 | | const SourceManager& SMgr, const PathPieces& path, |
111 | | FileID FID, const FileEntry *Entry, const char *declName); |
112 | | |
113 | | // Rewrite the file specified by FID with HTML formatting. |
114 | | void RewriteFile(Rewriter &R, const PathPieces& path, FileID FID); |
115 | | |
116 | | |
117 | | private: |
118 | | /// \return Javascript for displaying shortcuts help; |
119 | | StringRef showHelpJavascript(); |
120 | | |
121 | | /// \return Javascript for navigating the HTML report using j/k keys. |
122 | | StringRef generateKeyboardNavigationJavascript(); |
123 | | |
124 | | /// \return JavaScript for an option to only show relevant lines. |
125 | | std::string showRelevantLinesJavascript( |
126 | | const PathDiagnostic &D, const PathPieces &path); |
127 | | |
128 | | /// Write executed lines from \p D in JSON format into \p os. |
129 | | void dumpCoverageData(const PathDiagnostic &D, |
130 | | const PathPieces &path, |
131 | | llvm::raw_string_ostream &os); |
132 | | }; |
133 | | |
134 | | } // namespace |
135 | | |
136 | | void ento::createHTMLDiagnosticConsumer( |
137 | | PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, |
138 | | const std::string &OutputDir, const Preprocessor &PP, |
139 | | const cross_tu::CrossTranslationUnitContext &CTU, |
140 | 1.15k | const MacroExpansionContext &MacroExpansions) { |
141 | | |
142 | | // FIXME: HTML is currently our default output type, but if the output |
143 | | // directory isn't specified, it acts like if it was in the minimal text |
144 | | // output mode. This doesn't make much sense, we should have the minimal text |
145 | | // as our default. In the case of backward compatibility concerns, this could |
146 | | // be preserved with -analyzer-config-compatibility-mode=true. |
147 | 1.15k | createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU, |
148 | 1.15k | MacroExpansions); |
149 | | |
150 | | // TODO: Emit an error here. |
151 | 1.15k | if (OutputDir.empty()) |
152 | 1.11k | return; |
153 | | |
154 | 42 | C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, true)); |
155 | 42 | } |
156 | | |
157 | | void ento::createHTMLSingleFileDiagnosticConsumer( |
158 | | PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, |
159 | | const std::string &OutputDir, const Preprocessor &PP, |
160 | | const cross_tu::CrossTranslationUnitContext &CTU, |
161 | 3 | const clang::MacroExpansionContext &MacroExpansions) { |
162 | 3 | createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU, |
163 | 3 | MacroExpansions); |
164 | | |
165 | | // TODO: Emit an error here. |
166 | 3 | if (OutputDir.empty()) |
167 | 0 | return; |
168 | | |
169 | 3 | C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, false)); |
170 | 3 | } |
171 | | |
172 | | void ento::createPlistHTMLDiagnosticConsumer( |
173 | | PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, |
174 | | const std::string &prefix, const Preprocessor &PP, |
175 | | const cross_tu::CrossTranslationUnitContext &CTU, |
176 | 2 | const MacroExpansionContext &MacroExpansions) { |
177 | 2 | createHTMLDiagnosticConsumer( |
178 | 2 | DiagOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP, CTU, |
179 | 2 | MacroExpansions); |
180 | 2 | createPlistMultiFileDiagnosticConsumer(DiagOpts, C, prefix, PP, CTU, |
181 | 2 | MacroExpansions); |
182 | 2 | createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, prefix, PP, |
183 | 2 | CTU, MacroExpansions); |
184 | 2 | } |
185 | | |
186 | | void ento::createSarifHTMLDiagnosticConsumer( |
187 | | PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, |
188 | | const std::string &sarif_file, const Preprocessor &PP, |
189 | | const cross_tu::CrossTranslationUnitContext &CTU, |
190 | 0 | const MacroExpansionContext &MacroExpansions) { |
191 | 0 | createHTMLDiagnosticConsumer( |
192 | 0 | DiagOpts, C, std::string(llvm::sys::path::parent_path(sarif_file)), PP, |
193 | 0 | CTU, MacroExpansions); |
194 | 0 | createSarifDiagnosticConsumer(DiagOpts, C, sarif_file, PP, CTU, |
195 | 0 | MacroExpansions); |
196 | 0 | createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, sarif_file, |
197 | 0 | PP, CTU, MacroExpansions); |
198 | 0 | } |
199 | | |
200 | | //===----------------------------------------------------------------------===// |
201 | | // Report processing. |
202 | | //===----------------------------------------------------------------------===// |
203 | | |
204 | | void HTMLDiagnostics::FlushDiagnosticsImpl( |
205 | | std::vector<const PathDiagnostic *> &Diags, |
206 | 45 | FilesMade *filesMade) { |
207 | 45 | for (const auto Diag : Diags) |
208 | 97 | ReportDiag(*Diag, filesMade); |
209 | 45 | } |
210 | | |
211 | | void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D, |
212 | 97 | FilesMade *filesMade) { |
213 | | // Create the HTML directory if it is missing. |
214 | 97 | if (!createdDir) { |
215 | 41 | createdDir = true; |
216 | 41 | if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) { |
217 | 0 | llvm::errs() << "warning: could not create directory '" |
218 | 0 | << Directory << "': " << ec.message() << '\n'; |
219 | 0 | noDir = true; |
220 | 0 | return; |
221 | 0 | } |
222 | 41 | } |
223 | | |
224 | 97 | if (noDir) |
225 | 0 | return; |
226 | | |
227 | | // First flatten out the entire path to make it easier to use. |
228 | 97 | PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false); |
229 | | |
230 | | // The path as already been prechecked that the path is non-empty. |
231 | 97 | assert(!path.empty()); |
232 | 0 | const SourceManager &SMgr = path.front()->getLocation().getManager(); |
233 | | |
234 | | // Create a new rewriter to generate HTML. |
235 | 97 | Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts()); |
236 | | |
237 | | // The file for the first path element is considered the main report file, it |
238 | | // will usually be equivalent to SMgr.getMainFileID(); however, it might be a |
239 | | // header when -analyzer-opt-analyze-headers is used. |
240 | 97 | FileID ReportFile = path.front()->getLocation().asLocation().getExpansionLoc().getFileID(); |
241 | | |
242 | | // Get the function/method name |
243 | 97 | SmallString<128> declName("unknown"); |
244 | 97 | int offsetDecl = 0; |
245 | 97 | if (const Decl *DeclWithIssue = D.getDeclWithIssue()) { |
246 | 97 | if (const auto *ND = dyn_cast<NamedDecl>(DeclWithIssue)) |
247 | 95 | declName = ND->getDeclName().getAsString(); |
248 | | |
249 | 97 | if (const Stmt *Body = DeclWithIssue->getBody()) { |
250 | | // Retrieve the relative position of the declaration which will be used |
251 | | // for the file name |
252 | 97 | FullSourceLoc L( |
253 | 97 | SMgr.getExpansionLoc(path.back()->getLocation().asLocation()), |
254 | 97 | SMgr); |
255 | 97 | FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getBeginLoc()), SMgr); |
256 | 97 | offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber(); |
257 | 97 | } |
258 | 97 | } |
259 | | |
260 | 97 | std::string report = GenerateHTML(D, R, SMgr, path, declName.c_str()); |
261 | 97 | if (report.empty()) { |
262 | 0 | llvm::errs() << "warning: no diagnostics generated for main file.\n"; |
263 | 0 | return; |
264 | 0 | } |
265 | | |
266 | | // Create a path for the target HTML file. |
267 | 97 | int FD; |
268 | 97 | SmallString<128> Model, ResultPath; |
269 | | |
270 | 97 | if (!DiagOpts.ShouldWriteStableReportFilename) { |
271 | 97 | llvm::sys::path::append(Model, Directory, "report-%%%%%%.html"); |
272 | 97 | if (std::error_code EC = |
273 | 97 | llvm::sys::fs::make_absolute(Model)) { |
274 | 0 | llvm::errs() << "warning: could not make '" << Model |
275 | 0 | << "' absolute: " << EC.message() << '\n'; |
276 | 0 | return; |
277 | 0 | } |
278 | 97 | if (std::error_code EC = llvm::sys::fs::createUniqueFile( |
279 | 97 | Model, FD, ResultPath, llvm::sys::fs::OF_Text)) { |
280 | 3 | llvm::errs() << "warning: could not create file in '" << Directory |
281 | 3 | << "': " << EC.message() << '\n'; |
282 | 3 | return; |
283 | 3 | } |
284 | 97 | } else { |
285 | 0 | int i = 1; |
286 | 0 | std::error_code EC; |
287 | 0 | do { |
288 | | // Find a filename which is not already used |
289 | 0 | const FileEntry* Entry = SMgr.getFileEntryForID(ReportFile); |
290 | 0 | std::stringstream filename; |
291 | 0 | Model = ""; |
292 | 0 | filename << "report-" |
293 | 0 | << llvm::sys::path::filename(Entry->getName()).str() |
294 | 0 | << "-" << declName.c_str() |
295 | 0 | << "-" << offsetDecl |
296 | 0 | << "-" << i << ".html"; |
297 | 0 | llvm::sys::path::append(Model, Directory, |
298 | 0 | filename.str()); |
299 | 0 | EC = llvm::sys::fs::openFileForReadWrite( |
300 | 0 | Model, FD, llvm::sys::fs::CD_CreateNew, llvm::sys::fs::OF_None); |
301 | 0 | if (EC && EC != llvm::errc::file_exists) { |
302 | 0 | llvm::errs() << "warning: could not create file '" << Model |
303 | 0 | << "': " << EC.message() << '\n'; |
304 | 0 | return; |
305 | 0 | } |
306 | 0 | i++; |
307 | 0 | } while (EC); |
308 | 0 | } |
309 | | |
310 | 94 | llvm::raw_fd_ostream os(FD, true); |
311 | | |
312 | 94 | if (filesMade) |
313 | 94 | filesMade->addDiagnostic(D, getName(), |
314 | 94 | llvm::sys::path::filename(ResultPath)); |
315 | | |
316 | | // Emit the HTML to disk. |
317 | 94 | os << report; |
318 | 94 | } |
319 | | |
320 | | std::string HTMLDiagnostics::GenerateHTML(const PathDiagnostic& D, Rewriter &R, |
321 | 97 | const SourceManager& SMgr, const PathPieces& path, const char *declName) { |
322 | | // Rewrite source files as HTML for every new file the path crosses |
323 | 97 | std::vector<FileID> FileIDs; |
324 | 251 | for (auto I : path) { |
325 | 251 | FileID FID = I->getLocation().asLocation().getExpansionLoc().getFileID(); |
326 | 251 | if (llvm::is_contained(FileIDs, FID)) |
327 | 150 | continue; |
328 | | |
329 | 101 | FileIDs.push_back(FID); |
330 | 101 | RewriteFile(R, path, FID); |
331 | 101 | } |
332 | | |
333 | 97 | if (SupportsCrossFileDiagnostics && FileIDs.size() > 195 ) { |
334 | | // Prefix file names, anchor tags, and nav cursors to every file |
335 | 12 | for (auto I = FileIDs.begin(), E = FileIDs.end(); I != E; I++8 ) { |
336 | 8 | std::string s; |
337 | 8 | llvm::raw_string_ostream os(s); |
338 | | |
339 | 8 | if (I != FileIDs.begin()) |
340 | 4 | os << "<hr class=divider>\n"; |
341 | | |
342 | 8 | os << "<div id=File" << I->getHashValue() << ">\n"; |
343 | | |
344 | | // Left nav arrow |
345 | 8 | if (I != FileIDs.begin()) |
346 | 4 | os << "<div class=FileNav><a href=\"#File" << (I - 1)->getHashValue() |
347 | 4 | << "\">←</a></div>"; |
348 | | |
349 | 8 | os << "<h4 class=FileName>" << SMgr.getFileEntryForID(*I)->getName() |
350 | 8 | << "</h4>\n"; |
351 | | |
352 | | // Right nav arrow |
353 | 8 | if (I + 1 != E) |
354 | 4 | os << "<div class=FileNav><a href=\"#File" << (I + 1)->getHashValue() |
355 | 4 | << "\">→</a></div>"; |
356 | | |
357 | 8 | os << "</div>\n"; |
358 | | |
359 | 8 | R.InsertTextBefore(SMgr.getLocForStartOfFile(*I), os.str()); |
360 | 8 | } |
361 | | |
362 | | // Append files to the main report file in the order they appear in the path |
363 | 4 | for (auto I : llvm::make_range(FileIDs.begin() + 1, FileIDs.end())) { |
364 | 4 | std::string s; |
365 | 4 | llvm::raw_string_ostream os(s); |
366 | | |
367 | 4 | const RewriteBuffer *Buf = R.getRewriteBufferFor(I); |
368 | 4 | for (auto BI : *Buf) |
369 | 7.12k | os << BI; |
370 | | |
371 | 4 | R.InsertTextAfter(SMgr.getLocForEndOfFile(FileIDs[0]), os.str()); |
372 | 4 | } |
373 | 4 | } |
374 | | |
375 | 97 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileIDs[0]); |
376 | 97 | if (!Buf) |
377 | 0 | return {}; |
378 | | |
379 | | // Add CSS, header, and footer. |
380 | 97 | FileID FID = |
381 | 97 | path.back()->getLocation().asLocation().getExpansionLoc().getFileID(); |
382 | 97 | const FileEntry* Entry = SMgr.getFileEntryForID(FID); |
383 | 97 | FinalizeHTML(D, R, SMgr, path, FileIDs[0], Entry, declName); |
384 | | |
385 | 97 | std::string file; |
386 | 97 | llvm::raw_string_ostream os(file); |
387 | 97 | for (auto BI : *Buf) |
388 | 4.47M | os << BI; |
389 | | |
390 | 97 | return os.str(); |
391 | 97 | } |
392 | | |
393 | | void HTMLDiagnostics::dumpCoverageData( |
394 | | const PathDiagnostic &D, |
395 | | const PathPieces &path, |
396 | 97 | llvm::raw_string_ostream &os) { |
397 | | |
398 | 97 | const FilesToLineNumsMap &ExecutedLines = D.getExecutedLines(); |
399 | | |
400 | 97 | os << "var relevant_lines = {"; |
401 | 97 | for (auto I = ExecutedLines.begin(), |
402 | 198 | E = ExecutedLines.end(); I != E; ++I101 ) { |
403 | 101 | if (I != ExecutedLines.begin()) |
404 | 4 | os << ", "; |
405 | | |
406 | 101 | os << "\"" << I->first.getHashValue() << "\": {"; |
407 | 608 | for (unsigned LineNo : I->second) { |
408 | 608 | if (LineNo != *(I->second.begin())) |
409 | 507 | os << ", "; |
410 | | |
411 | 608 | os << "\"" << LineNo << "\": 1"; |
412 | 608 | } |
413 | 101 | os << "}"; |
414 | 101 | } |
415 | | |
416 | 97 | os << "};"; |
417 | 97 | } |
418 | | |
419 | | std::string HTMLDiagnostics::showRelevantLinesJavascript( |
420 | 97 | const PathDiagnostic &D, const PathPieces &path) { |
421 | 97 | std::string s; |
422 | 97 | llvm::raw_string_ostream os(s); |
423 | 97 | os << "<script type='text/javascript'>\n"; |
424 | 97 | dumpCoverageData(D, path, os); |
425 | 97 | os << R"<<<( |
426 | 97 | |
427 | 97 | var filterCounterexample = function (hide) { |
428 | 97 | var tables = document.getElementsByClassName("code"); |
429 | 97 | for (var t=0; t<tables.length; t++) { |
430 | 97 | var table = tables[t]; |
431 | 97 | var file_id = table.getAttribute("data-fileid"); |
432 | 97 | var lines_in_fid = relevant_lines[file_id]; |
433 | 97 | if (!lines_in_fid) { |
434 | 97 | lines_in_fid = {}; |
435 | 97 | } |
436 | 97 | var lines = table.getElementsByClassName("codeline"); |
437 | 97 | for (var i=0; i<lines.length; i++) { |
438 | 97 | var el = lines[i]; |
439 | 97 | var lineNo = el.getAttribute("data-linenumber"); |
440 | 97 | if (!lines_in_fid[lineNo]) { |
441 | 97 | if (hide) { |
442 | 97 | el.setAttribute("hidden", ""); |
443 | 97 | } else { |
444 | 97 | el.removeAttribute("hidden"); |
445 | 97 | } |
446 | 97 | } |
447 | 97 | } |
448 | 97 | } |
449 | 97 | } |
450 | 97 | |
451 | 97 | window.addEventListener("keydown", function (event) { |
452 | 97 | if (event.defaultPrevented) { |
453 | 97 | return; |
454 | 97 | } |
455 | 97 | if (event.key == "S") { |
456 | 97 | var checked = document.getElementsByName("showCounterexample")[0].checked; |
457 | 97 | filterCounterexample(!checked); |
458 | 97 | document.getElementsByName("showCounterexample")[0].checked = !checked; |
459 | 97 | } else { |
460 | 97 | return; |
461 | 97 | } |
462 | 97 | event.preventDefault(); |
463 | 97 | }, true); |
464 | 97 | |
465 | 97 | document.addEventListener("DOMContentLoaded", function() { |
466 | 97 | document.querySelector('input[name="showCounterexample"]').onchange= |
467 | 97 | function (event) { |
468 | 97 | filterCounterexample(this.checked); |
469 | 97 | }; |
470 | 97 | }); |
471 | 97 | </script> |
472 | 97 | |
473 | 97 | <form> |
474 | 97 | <input type="checkbox" name="showCounterexample" id="showCounterexample" /> |
475 | 97 | <label for="showCounterexample"> |
476 | 97 | Show only relevant lines |
477 | 97 | </label> |
478 | 97 | </form> |
479 | 97 | )<<<"; |
480 | | |
481 | 97 | return os.str(); |
482 | 97 | } |
483 | | |
484 | | void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R, |
485 | | const SourceManager& SMgr, const PathPieces& path, FileID FID, |
486 | 97 | const FileEntry *Entry, const char *declName) { |
487 | | // This is a cludge; basically we want to append either the full |
488 | | // working directory if we have no directory information. This is |
489 | | // a work in progress. |
490 | | |
491 | 97 | llvm::SmallString<0> DirName; |
492 | | |
493 | 97 | if (llvm::sys::path::is_relative(Entry->getName())) { |
494 | 0 | llvm::sys::fs::current_path(DirName); |
495 | 0 | DirName += '/'; |
496 | 0 | } |
497 | | |
498 | 97 | int LineNumber = path.back()->getLocation().asLocation().getExpansionLineNumber(); |
499 | 97 | int ColumnNumber = path.back()->getLocation().asLocation().getExpansionColumnNumber(); |
500 | | |
501 | 97 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), showHelpJavascript()); |
502 | | |
503 | 97 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), |
504 | 97 | generateKeyboardNavigationJavascript()); |
505 | | |
506 | | // Checkbox and javascript for filtering the output to the counterexample. |
507 | 97 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), |
508 | 97 | showRelevantLinesJavascript(D, path)); |
509 | | |
510 | | // Add the name of the file as an <h1> tag. |
511 | 97 | { |
512 | 97 | std::string s; |
513 | 97 | llvm::raw_string_ostream os(s); |
514 | | |
515 | 97 | os << "<!-- REPORTHEADER -->\n" |
516 | 97 | << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
517 | 97 | "<tr><td class=\"rowname\">File:</td><td>" |
518 | 97 | << html::EscapeText(DirName) |
519 | 97 | << html::EscapeText(Entry->getName()) |
520 | 97 | << "</td></tr>\n<tr><td class=\"rowname\">Warning:</td><td>" |
521 | 97 | "<a href=\"#EndPath\">line " |
522 | 97 | << LineNumber |
523 | 97 | << ", column " |
524 | 97 | << ColumnNumber |
525 | 97 | << "</a><br />" |
526 | 97 | << D.getVerboseDescription() << "</td></tr>\n"; |
527 | | |
528 | | // The navigation across the extra notes pieces. |
529 | 97 | unsigned NumExtraPieces = 0; |
530 | 251 | for (const auto &Piece : path) { |
531 | 251 | if (const auto *P = dyn_cast<PathDiagnosticNotePiece>(Piece.get())) { |
532 | 0 | int LineNumber = |
533 | 0 | P->getLocation().asLocation().getExpansionLineNumber(); |
534 | 0 | int ColumnNumber = |
535 | 0 | P->getLocation().asLocation().getExpansionColumnNumber(); |
536 | 0 | os << "<tr><td class=\"rowname\">Note:</td><td>" |
537 | 0 | << "<a href=\"#Note" << NumExtraPieces << "\">line " |
538 | 0 | << LineNumber << ", column " << ColumnNumber << "</a><br />" |
539 | 0 | << P->getString() << "</td></tr>"; |
540 | 0 | ++NumExtraPieces; |
541 | 0 | } |
542 | 251 | } |
543 | | |
544 | | // Output any other meta data. |
545 | | |
546 | 97 | for (PathDiagnostic::meta_iterator I = D.meta_begin(), E = D.meta_end(); |
547 | 97 | I != E; ++I0 ) { |
548 | 0 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
549 | 0 | } |
550 | | |
551 | 97 | os << R"<<<( |
552 | 97 | </table> |
553 | 97 | <!-- REPORTSUMMARYEXTRA --> |
554 | 97 | <h3>Annotated Source Code</h3> |
555 | 97 | <p>Press <a href="#" onclick="toggleHelp(); return false;">'?'</a> |
556 | 97 | to see keyboard shortcuts</p> |
557 | 97 | <input type="checkbox" class="spoilerhider" id="showinvocation" /> |
558 | 97 | <label for="showinvocation" >Show analyzer invocation</label> |
559 | 97 | <div class="spoiler">clang -cc1 )<<<"; |
560 | 97 | os << html::EscapeText(DiagOpts.ToolInvocation); |
561 | 97 | os << R"<<<( |
562 | 97 | </div> |
563 | 97 | <div id='tooltiphint' hidden="true"> |
564 | 97 | <p>Keyboard shortcuts: </p> |
565 | 97 | <ul> |
566 | 97 | <li>Use 'j/k' keys for keyboard navigation</li> |
567 | 97 | <li>Use 'Shift+S' to show/hide relevant lines</li> |
568 | 97 | <li>Use '?' to toggle this window</li> |
569 | 97 | </ul> |
570 | 97 | <a href="#" onclick="toggleHelp(); return false;">Close</a> |
571 | 97 | </div> |
572 | 97 | )<<<"; |
573 | 97 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
574 | 97 | } |
575 | | |
576 | | // Embed meta-data tags. |
577 | 97 | { |
578 | 97 | std::string s; |
579 | 97 | llvm::raw_string_ostream os(s); |
580 | | |
581 | 97 | StringRef BugDesc = D.getVerboseDescription(); |
582 | 97 | if (!BugDesc.empty()) |
583 | 97 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
584 | | |
585 | 97 | StringRef BugType = D.getBugType(); |
586 | 97 | if (!BugType.empty()) |
587 | 97 | os << "\n<!-- BUGTYPE " << BugType << " -->\n"; |
588 | | |
589 | 97 | PathDiagnosticLocation UPDLoc = D.getUniqueingLoc(); |
590 | 97 | FullSourceLoc L(SMgr.getExpansionLoc(UPDLoc.isValid() |
591 | 97 | ? UPDLoc.asLocation()1 |
592 | 97 | : D.getLocation().asLocation()96 ), |
593 | 97 | SMgr); |
594 | 97 | const Decl *DeclWithIssue = D.getDeclWithIssue(); |
595 | | |
596 | 97 | StringRef BugCategory = D.getCategory(); |
597 | 97 | if (!BugCategory.empty()) |
598 | 97 | os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; |
599 | | |
600 | 97 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
601 | | |
602 | 97 | os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n"; |
603 | | |
604 | 97 | os << "\n<!-- FUNCTIONNAME " << declName << " -->\n"; |
605 | | |
606 | 97 | os << "\n<!-- ISSUEHASHCONTENTOFLINEINCONTEXT " |
607 | 97 | << getIssueHash(L, D.getCheckerName(), D.getBugType(), DeclWithIssue, |
608 | 97 | PP.getLangOpts()) |
609 | 97 | << " -->\n"; |
610 | | |
611 | 97 | os << "\n<!-- BUGLINE " |
612 | 97 | << LineNumber |
613 | 97 | << " -->\n"; |
614 | | |
615 | 97 | os << "\n<!-- BUGCOLUMN " |
616 | 97 | << ColumnNumber |
617 | 97 | << " -->\n"; |
618 | | |
619 | 97 | os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n"; |
620 | | |
621 | | // Mark the end of the tags. |
622 | 97 | os << "\n<!-- BUGMETAEND -->\n"; |
623 | | |
624 | | // Insert the text. |
625 | 97 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
626 | 97 | } |
627 | | |
628 | 97 | html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); |
629 | 97 | } |
630 | | |
631 | 97 | StringRef HTMLDiagnostics::showHelpJavascript() { |
632 | 97 | return R"<<<( |
633 | 97 | <script type='text/javascript'> |
634 | 97 | |
635 | 97 | var toggleHelp = function() { |
636 | 97 | var hint = document.querySelector("#tooltiphint"); |
637 | 97 | var attributeName = "hidden"; |
638 | 97 | if (hint.hasAttribute(attributeName)) { |
639 | 97 | hint.removeAttribute(attributeName); |
640 | 97 | } else { |
641 | 97 | hint.setAttribute("hidden", "true"); |
642 | 97 | } |
643 | 97 | }; |
644 | 97 | window.addEventListener("keydown", function (event) { |
645 | 97 | if (event.defaultPrevented) { |
646 | 97 | return; |
647 | 97 | } |
648 | 97 | if (event.key == "?") { |
649 | 97 | toggleHelp(); |
650 | 97 | } else { |
651 | 97 | return; |
652 | 97 | } |
653 | 97 | event.preventDefault(); |
654 | 97 | }); |
655 | 97 | </script> |
656 | 97 | )<<<"; |
657 | 97 | } |
658 | | |
659 | 10 | static bool shouldDisplayPopUpRange(const SourceRange &Range) { |
660 | 10 | return !(Range.getBegin().isMacroID() || Range.getEnd().isMacroID()9 ); |
661 | 10 | } |
662 | | |
663 | | static void |
664 | | HandlePopUpPieceStartTag(Rewriter &R, |
665 | 101 | const std::vector<SourceRange> &PopUpRanges) { |
666 | 101 | for (const auto &Range : PopUpRanges) { |
667 | 4 | if (!shouldDisplayPopUpRange(Range)) |
668 | 0 | continue; |
669 | | |
670 | 4 | html::HighlightRange(R, Range.getBegin(), Range.getEnd(), "", |
671 | 4 | "<table class='variable_popup'><tbody>", |
672 | 4 | /*IsTokenRange=*/true); |
673 | 4 | } |
674 | 101 | } |
675 | | |
676 | | static void HandlePopUpPieceEndTag(Rewriter &R, |
677 | | const PathDiagnosticPopUpPiece &Piece, |
678 | | std::vector<SourceRange> &PopUpRanges, |
679 | | unsigned int LastReportedPieceIndex, |
680 | 6 | unsigned int PopUpPieceIndex) { |
681 | 6 | SmallString<256> Buf; |
682 | 6 | llvm::raw_svector_ostream Out(Buf); |
683 | | |
684 | 6 | SourceRange Range(Piece.getLocation().asRange()); |
685 | 6 | if (!shouldDisplayPopUpRange(Range)) |
686 | 1 | return; |
687 | | |
688 | | // Write out the path indices with a right arrow and the message as a row. |
689 | 5 | Out << "<tr><td valign='top'><div class='PathIndex PathIndexPopUp'>" |
690 | 5 | << LastReportedPieceIndex; |
691 | | |
692 | | // Also annotate the state transition with extra indices. |
693 | 5 | Out << '.' << PopUpPieceIndex; |
694 | | |
695 | 5 | Out << "</div></td><td>" << Piece.getString() << "</td></tr>"; |
696 | | |
697 | | // If no report made at this range mark the variable and add the end tags. |
698 | 5 | if (std::find(PopUpRanges.begin(), PopUpRanges.end(), Range) == |
699 | 5 | PopUpRanges.end()) { |
700 | | // Store that we create a report at this range. |
701 | 4 | PopUpRanges.push_back(Range); |
702 | | |
703 | 4 | Out << "</tbody></table></span>"; |
704 | 4 | html::HighlightRange(R, Range.getBegin(), Range.getEnd(), |
705 | 4 | "<span class='variable'>", Buf.c_str(), |
706 | 4 | /*IsTokenRange=*/true); |
707 | 4 | } else { |
708 | | // Otherwise inject just the new row at the end of the range. |
709 | 1 | html::HighlightRange(R, Range.getBegin(), Range.getEnd(), "", Buf.c_str(), |
710 | 1 | /*IsTokenRange=*/true); |
711 | 1 | } |
712 | 5 | } |
713 | | |
714 | | void HTMLDiagnostics::RewriteFile(Rewriter &R, |
715 | 101 | const PathPieces& path, FileID FID) { |
716 | | // Process the path. |
717 | | // Maintain the counts of extra note pieces separately. |
718 | 101 | unsigned TotalPieces = path.size(); |
719 | 101 | unsigned TotalNotePieces = std::count_if( |
720 | 268 | path.begin(), path.end(), [](const PathDiagnosticPieceRef &p) { |
721 | 268 | return isa<PathDiagnosticNotePiece>(*p); |
722 | 268 | }); |
723 | 101 | unsigned PopUpPieceCount = std::count_if( |
724 | 268 | path.begin(), path.end(), [](const PathDiagnosticPieceRef &p) { |
725 | 268 | return isa<PathDiagnosticPopUpPiece>(*p); |
726 | 268 | }); |
727 | | |
728 | 101 | unsigned TotalRegularPieces = TotalPieces - TotalNotePieces - PopUpPieceCount; |
729 | 101 | unsigned NumRegularPieces = TotalRegularPieces; |
730 | 101 | unsigned NumNotePieces = TotalNotePieces; |
731 | | // Stores the count of the regular piece indices. |
732 | 101 | std::map<int, int> IndexMap; |
733 | | |
734 | | // Stores the different ranges where we have reported something. |
735 | 101 | std::vector<SourceRange> PopUpRanges; |
736 | 369 | for (auto I = path.rbegin(), E = path.rend(); I != E; ++I268 ) { |
737 | 268 | const auto &Piece = *I->get(); |
738 | | |
739 | 268 | if (isa<PathDiagnosticPopUpPiece>(Piece)) { |
740 | 6 | ++IndexMap[NumRegularPieces]; |
741 | 262 | } else if (isa<PathDiagnosticNotePiece>(Piece)) { |
742 | | // This adds diagnostic bubbles, but not navigation. |
743 | | // Navigation through note pieces would be added later, |
744 | | // as a separate pass through the piece list. |
745 | 0 | HandlePiece(R, FID, Piece, PopUpRanges, NumNotePieces, TotalNotePieces); |
746 | 0 | --NumNotePieces; |
747 | 262 | } else { |
748 | 262 | HandlePiece(R, FID, Piece, PopUpRanges, NumRegularPieces, |
749 | 262 | TotalRegularPieces); |
750 | 262 | --NumRegularPieces; |
751 | 262 | } |
752 | 268 | } |
753 | | |
754 | | // Secondary indexing if we are having multiple pop-ups between two notes. |
755 | | // (e.g. [(13) 'a' is 'true']; [(13.1) 'b' is 'false']; [(13.2) 'c' is...) |
756 | 101 | NumRegularPieces = TotalRegularPieces; |
757 | 369 | for (auto I = path.rbegin(), E = path.rend(); I != E; ++I268 ) { |
758 | 268 | const auto &Piece = *I->get(); |
759 | | |
760 | 268 | if (const auto *PopUpP = dyn_cast<PathDiagnosticPopUpPiece>(&Piece)) { |
761 | 6 | int PopUpPieceIndex = IndexMap[NumRegularPieces]; |
762 | | |
763 | | // Pop-up pieces needs the index of the last reported piece and its count |
764 | | // how many times we report to handle multiple reports on the same range. |
765 | | // This marks the variable, adds the </table> end tag and the message |
766 | | // (list element) as a row. The <table> start tag will be added after the |
767 | | // rows has been written out. Note: It stores every different range. |
768 | 6 | HandlePopUpPieceEndTag(R, *PopUpP, PopUpRanges, NumRegularPieces, |
769 | 6 | PopUpPieceIndex); |
770 | | |
771 | 6 | if (PopUpPieceIndex > 0) |
772 | 6 | --IndexMap[NumRegularPieces]; |
773 | | |
774 | 262 | } else if (!isa<PathDiagnosticNotePiece>(Piece)) { |
775 | 262 | --NumRegularPieces; |
776 | 262 | } |
777 | 268 | } |
778 | | |
779 | | // Add the <table> start tag of pop-up pieces based on the stored ranges. |
780 | 101 | HandlePopUpPieceStartTag(R, PopUpRanges); |
781 | | |
782 | | // Add line numbers, header, footer, etc. |
783 | 101 | html::EscapeText(R, FID); |
784 | 101 | html::AddLineNumbers(R, FID); |
785 | | |
786 | | // If we have a preprocessor, relex the file and syntax highlight. |
787 | | // We might not have a preprocessor if we come from a deserialized AST file, |
788 | | // for example. |
789 | 101 | html::SyntaxHighlight(R, FID, PP); |
790 | 101 | html::HighlightMacros(R, FID, PP); |
791 | 101 | } |
792 | | |
793 | | void HTMLDiagnostics::HandlePiece(Rewriter &R, FileID BugFileID, |
794 | | const PathDiagnosticPiece &P, |
795 | | const std::vector<SourceRange> &PopUpRanges, |
796 | 262 | unsigned num, unsigned max) { |
797 | | // For now, just draw a box above the line in question, and emit the |
798 | | // warning. |
799 | 262 | FullSourceLoc Pos = P.getLocation().asLocation(); |
800 | | |
801 | 262 | if (!Pos.isValid()) |
802 | 0 | return; |
803 | | |
804 | 262 | SourceManager &SM = R.getSourceMgr(); |
805 | 262 | assert(&Pos.getManager() == &SM && "SourceManagers are different!"); |
806 | 0 | std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos); |
807 | | |
808 | 262 | if (LPosInfo.first != BugFileID) |
809 | 16 | return; |
810 | | |
811 | 246 | llvm::MemoryBufferRef Buf = SM.getBufferOrFake(LPosInfo.first); |
812 | 246 | const char *FileStart = Buf.getBufferStart(); |
813 | | |
814 | | // Compute the column number. Rewind from the current position to the start |
815 | | // of the line. |
816 | 246 | unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); |
817 | 246 | const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData(); |
818 | 246 | const char *LineStart = TokInstantiationPtr-ColNo; |
819 | | |
820 | | // Compute LineEnd. |
821 | 246 | const char *LineEnd = TokInstantiationPtr; |
822 | 246 | const char *FileEnd = Buf.getBufferEnd(); |
823 | 8.31k | while (*LineEnd != '\n' && LineEnd != FileEnd8.06k ) |
824 | 8.06k | ++LineEnd; |
825 | | |
826 | | // Compute the margin offset by counting tabs and non-tabs. |
827 | 246 | unsigned PosNo = 0; |
828 | 1.72k | for (const char* c = LineStart; c != TokInstantiationPtr; ++c1.47k ) |
829 | 1.47k | PosNo += *c == '\t' ? 85 : 11.47k ; |
830 | | |
831 | | // Create the html for the message. |
832 | | |
833 | 246 | const char *Kind = nullptr; |
834 | 246 | bool IsNote = false; |
835 | 246 | bool SuppressIndex = (max == 1); |
836 | 246 | switch (P.getKind()) { |
837 | 209 | case PathDiagnosticPiece::Event: Kind = "Event"; break; |
838 | 37 | case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break; |
839 | | // Setting Kind to "Control" is intentional. |
840 | 0 | case PathDiagnosticPiece::Macro: Kind = "Control"; break; |
841 | 0 | case PathDiagnosticPiece::Note: |
842 | 0 | Kind = "Note"; |
843 | 0 | IsNote = true; |
844 | 0 | SuppressIndex = true; |
845 | 0 | break; |
846 | 0 | case PathDiagnosticPiece::Call: |
847 | 0 | case PathDiagnosticPiece::PopUp: |
848 | 0 | llvm_unreachable("Calls and extra notes should already be handled"); |
849 | 246 | } |
850 | | |
851 | 246 | std::string sbuf; |
852 | 246 | llvm::raw_string_ostream os(sbuf); |
853 | | |
854 | 246 | os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; |
855 | | |
856 | 246 | if (IsNote) |
857 | 0 | os << "Note" << num; |
858 | 246 | else if (num == max) |
859 | 97 | os << "EndPath"; |
860 | 149 | else |
861 | 149 | os << "Path" << num; |
862 | | |
863 | 246 | os << "\" class=\"msg"; |
864 | 246 | if (Kind) |
865 | 246 | os << " msg" << Kind; |
866 | 246 | os << "\" style=\"margin-left:" << PosNo << "ex"; |
867 | | |
868 | | // Output a maximum size. |
869 | 246 | if (!isa<PathDiagnosticMacroPiece>(P)) { |
870 | | // Get the string and determining its maximum substring. |
871 | 246 | const auto &Msg = P.getString(); |
872 | 246 | unsigned max_token = 0; |
873 | 246 | unsigned cnt = 0; |
874 | 246 | unsigned len = Msg.size(); |
875 | | |
876 | 246 | for (char C : Msg) |
877 | 9.42k | switch (C) { |
878 | 8.31k | default: |
879 | 8.31k | ++cnt; |
880 | 8.31k | continue; |
881 | 1.10k | case ' ': |
882 | 1.10k | case '\t': |
883 | 1.10k | case '\n': |
884 | 1.10k | if (cnt > max_token) max_token = cnt319 ; |
885 | 1.10k | cnt = 0; |
886 | 9.42k | } |
887 | | |
888 | 246 | if (cnt > max_token) |
889 | 56 | max_token = cnt; |
890 | | |
891 | | // Determine the approximate size of the message bubble in em. |
892 | 246 | unsigned em; |
893 | 246 | const unsigned max_line = 120; |
894 | | |
895 | 246 | if (max_token >= max_line) |
896 | 0 | em = max_token / 2; |
897 | 246 | else { |
898 | 246 | unsigned characters = max_line; |
899 | 246 | unsigned lines = len / max_line; |
900 | | |
901 | 246 | if (lines > 0) { |
902 | 298 | for (; characters > max_token; --characters283 ) |
903 | 286 | if (len / characters > lines) { |
904 | 3 | ++characters; |
905 | 3 | break; |
906 | 3 | } |
907 | 15 | } |
908 | | |
909 | 246 | em = characters / 2; |
910 | 246 | } |
911 | | |
912 | 246 | if (em < max_line/2) |
913 | 15 | os << "; max-width:" << em << "em"; |
914 | 246 | } |
915 | 0 | else |
916 | 0 | os << "; max-width:100em"; |
917 | | |
918 | 246 | os << "\">"; |
919 | | |
920 | 246 | if (!SuppressIndex) { |
921 | 201 | os << "<table class=\"msgT\"><tr><td valign=\"top\">"; |
922 | 201 | os << "<div class=\"PathIndex"; |
923 | 201 | if (Kind) os << " PathIndex" << Kind; |
924 | 201 | os << "\">" << num << "</div>"; |
925 | | |
926 | 201 | if (num > 1) { |
927 | 149 | os << "</td><td><div class=\"PathNav\"><a href=\"#Path" |
928 | 149 | << (num - 1) |
929 | 149 | << "\" title=\"Previous event (" |
930 | 149 | << (num - 1) |
931 | 149 | << ")\">←</a></div>"; |
932 | 149 | } |
933 | | |
934 | 201 | os << "</td><td>"; |
935 | 201 | } |
936 | | |
937 | 246 | if (const auto *MP = dyn_cast<PathDiagnosticMacroPiece>(&P)) { |
938 | 0 | os << "Within the expansion of the macro '"; |
939 | | |
940 | | // Get the name of the macro by relexing it. |
941 | 0 | { |
942 | 0 | FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc(); |
943 | 0 | assert(L.isFileID()); |
944 | 0 | StringRef BufferInfo = L.getBufferData(); |
945 | 0 | std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc(); |
946 | 0 | const char* MacroName = LocInfo.second + BufferInfo.data(); |
947 | 0 | Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(), |
948 | 0 | BufferInfo.begin(), MacroName, BufferInfo.end()); |
949 | |
|
950 | 0 | Token TheTok; |
951 | 0 | rawLexer.LexFromRawLexer(TheTok); |
952 | 0 | for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i) |
953 | 0 | os << MacroName[i]; |
954 | 0 | } |
955 | | |
956 | 0 | os << "':\n"; |
957 | |
|
958 | 0 | if (!SuppressIndex) { |
959 | 0 | os << "</td>"; |
960 | 0 | if (num < max) { |
961 | 0 | os << "<td><div class=\"PathNav\"><a href=\"#"; |
962 | 0 | if (num == max - 1) |
963 | 0 | os << "EndPath"; |
964 | 0 | else |
965 | 0 | os << "Path" << (num + 1); |
966 | 0 | os << "\" title=\"Next event (" |
967 | 0 | << (num + 1) |
968 | 0 | << ")\">→</a></div></td>"; |
969 | 0 | } |
970 | |
|
971 | 0 | os << "</tr></table>"; |
972 | 0 | } |
973 | | |
974 | | // Within a macro piece. Write out each event. |
975 | 0 | ProcessMacroPiece(os, *MP, 0); |
976 | 0 | } |
977 | 246 | else { |
978 | 246 | os << html::EscapeText(P.getString()); |
979 | | |
980 | 246 | if (!SuppressIndex) { |
981 | 201 | os << "</td>"; |
982 | 201 | if (num < max) { |
983 | 149 | os << "<td><div class=\"PathNav\"><a href=\"#"; |
984 | 149 | if (num == max - 1) |
985 | 52 | os << "EndPath"; |
986 | 97 | else |
987 | 97 | os << "Path" << (num + 1); |
988 | 149 | os << "\" title=\"Next event (" |
989 | 149 | << (num + 1) |
990 | 149 | << ")\">→</a></div></td>"; |
991 | 149 | } |
992 | | |
993 | 201 | os << "</tr></table>"; |
994 | 201 | } |
995 | 246 | } |
996 | | |
997 | 0 | os << "</div></td></tr>"; |
998 | | |
999 | | // Insert the new html. |
1000 | 246 | unsigned DisplayPos = LineEnd - FileStart; |
1001 | 246 | SourceLocation Loc = |
1002 | 246 | SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos); |
1003 | | |
1004 | 246 | R.InsertTextBefore(Loc, os.str()); |
1005 | | |
1006 | | // Now highlight the ranges. |
1007 | 246 | ArrayRef<SourceRange> Ranges = P.getRanges(); |
1008 | 246 | for (const auto &Range : Ranges) { |
1009 | | // If we have already highlighted the range as a pop-up there is no work. |
1010 | 199 | if (std::find(PopUpRanges.begin(), PopUpRanges.end(), Range) != |
1011 | 199 | PopUpRanges.end()) |
1012 | 0 | continue; |
1013 | | |
1014 | 199 | HighlightRange(R, LPosInfo.first, Range); |
1015 | 199 | } |
1016 | 246 | } |
1017 | | |
1018 | 0 | static void EmitAlphaCounter(raw_ostream &os, unsigned n) { |
1019 | 0 | unsigned x = n % ('z' - 'a'); |
1020 | 0 | n /= 'z' - 'a'; |
1021 | |
|
1022 | 0 | if (n > 0) |
1023 | 0 | EmitAlphaCounter(os, n); |
1024 | |
|
1025 | 0 | os << char('a' + x); |
1026 | 0 | } |
1027 | | |
1028 | | unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os, |
1029 | | const PathDiagnosticMacroPiece& P, |
1030 | 0 | unsigned num) { |
1031 | 0 | for (const auto &subPiece : P.subPieces) { |
1032 | 0 | if (const auto *MP = dyn_cast<PathDiagnosticMacroPiece>(subPiece.get())) { |
1033 | 0 | num = ProcessMacroPiece(os, *MP, num); |
1034 | 0 | continue; |
1035 | 0 | } |
1036 | | |
1037 | 0 | if (const auto *EP = dyn_cast<PathDiagnosticEventPiece>(subPiece.get())) { |
1038 | 0 | os << "<div class=\"msg msgEvent\" style=\"width:94%; " |
1039 | 0 | "margin-left:5px\">" |
1040 | 0 | "<table class=\"msgT\"><tr>" |
1041 | 0 | "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">"; |
1042 | 0 | EmitAlphaCounter(os, num++); |
1043 | 0 | os << "</div></td><td valign=\"top\">" |
1044 | 0 | << html::EscapeText(EP->getString()) |
1045 | 0 | << "</td></tr></table></div>\n"; |
1046 | 0 | } |
1047 | 0 | } |
1048 | |
|
1049 | 0 | return num; |
1050 | 0 | } |
1051 | | |
1052 | | void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, |
1053 | | SourceRange Range, |
1054 | | const char *HighlightStart, |
1055 | 199 | const char *HighlightEnd) { |
1056 | 199 | SourceManager &SM = R.getSourceMgr(); |
1057 | 199 | const LangOptions &LangOpts = R.getLangOpts(); |
1058 | | |
1059 | 199 | SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin()); |
1060 | 199 | unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart); |
1061 | | |
1062 | 199 | SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd()); |
1063 | 199 | unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd); |
1064 | | |
1065 | 199 | if (EndLineNo < StartLineNo) |
1066 | 0 | return; |
1067 | | |
1068 | 199 | if (SM.getFileID(InstantiationStart) != BugFileID || |
1069 | 199 | SM.getFileID(InstantiationEnd) != BugFileID) |
1070 | 0 | return; |
1071 | | |
1072 | | // Compute the column number of the end. |
1073 | 199 | unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd); |
1074 | 199 | unsigned OldEndColNo = EndColNo; |
1075 | | |
1076 | 199 | if (EndColNo) { |
1077 | | // Add in the length of the token, so that we cover multi-char tokens. |
1078 | 199 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; |
1079 | 199 | } |
1080 | | |
1081 | | // Highlight the range. Make the span tag the outermost tag for the |
1082 | | // selected range. |
1083 | | |
1084 | 199 | SourceLocation E = |
1085 | 199 | InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo); |
1086 | | |
1087 | 199 | html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); |
1088 | 199 | } |
1089 | | |
1090 | 97 | StringRef HTMLDiagnostics::generateKeyboardNavigationJavascript() { |
1091 | 97 | return R"<<<( |
1092 | 97 | <script type='text/javascript'> |
1093 | 97 | var digitMatcher = new RegExp("[0-9]+"); |
1094 | 97 | |
1095 | 97 | var querySelectorAllArray = function(selector) { |
1096 | 97 | return Array.prototype.slice.call( |
1097 | 97 | document.querySelectorAll(selector)); |
1098 | 97 | } |
1099 | 97 | |
1100 | 97 | document.addEventListener("DOMContentLoaded", function() { |
1101 | 97 | querySelectorAllArray(".PathNav > a").forEach( |
1102 | 97 | function(currentValue, currentIndex) { |
1103 | 97 | var hrefValue = currentValue.getAttribute("href"); |
1104 | 97 | currentValue.onclick = function() { |
1105 | 97 | scrollTo(document.querySelector(hrefValue)); |
1106 | 97 | return false; |
1107 | 97 | }; |
1108 | 97 | }); |
1109 | 97 | }); |
1110 | 97 | |
1111 | 97 | var findNum = function() { |
1112 | 97 | var s = document.querySelector(".selected"); |
1113 | 97 | if (!s || s.id == "EndPath") { |
1114 | 97 | return 0; |
1115 | 97 | } |
1116 | 97 | var out = parseInt(digitMatcher.exec(s.id)[0]); |
1117 | 97 | return out; |
1118 | 97 | }; |
1119 | 97 | |
1120 | 97 | var scrollTo = function(el) { |
1121 | 97 | querySelectorAllArray(".selected").forEach(function(s) { |
1122 | 97 | s.classList.remove("selected"); |
1123 | 97 | }); |
1124 | 97 | el.classList.add("selected"); |
1125 | 97 | window.scrollBy(0, el.getBoundingClientRect().top - |
1126 | 97 | (window.innerHeight / 2)); |
1127 | 97 | } |
1128 | 97 | |
1129 | 97 | var move = function(num, up, numItems) { |
1130 | 97 | if (num == 1 && up || num == numItems - 1 && !up) { |
1131 | 97 | return 0; |
1132 | 97 | } else if (num == 0 && up) { |
1133 | 97 | return numItems - 1; |
1134 | 97 | } else if (num == 0 && !up) { |
1135 | 97 | return 1 % numItems; |
1136 | 97 | } |
1137 | 97 | return up ? num - 1 : num + 1; |
1138 | 97 | } |
1139 | 97 | |
1140 | 97 | var numToId = function(num) { |
1141 | 97 | if (num == 0) { |
1142 | 97 | return document.getElementById("EndPath") |
1143 | 97 | } |
1144 | 97 | return document.getElementById("Path" + num); |
1145 | 97 | }; |
1146 | 97 | |
1147 | 97 | var navigateTo = function(up) { |
1148 | 97 | var numItems = document.querySelectorAll( |
1149 | 97 | ".line > .msgEvent, .line > .msgControl").length; |
1150 | 97 | var currentSelected = findNum(); |
1151 | 97 | var newSelected = move(currentSelected, up, numItems); |
1152 | 97 | var newEl = numToId(newSelected, numItems); |
1153 | 97 | |
1154 | 97 | // Scroll element into center. |
1155 | 97 | scrollTo(newEl); |
1156 | 97 | }; |
1157 | 97 | |
1158 | 97 | window.addEventListener("keydown", function (event) { |
1159 | 97 | if (event.defaultPrevented) { |
1160 | 97 | return; |
1161 | 97 | } |
1162 | 97 | if (event.key == "j") { |
1163 | 97 | navigateTo(/*up=*/false); |
1164 | 97 | } else if (event.key == "k") { |
1165 | 97 | navigateTo(/*up=*/true); |
1166 | 97 | } else { |
1167 | 97 | return; |
1168 | 97 | } |
1169 | 97 | event.preventDefault(); |
1170 | 97 | }, true); |
1171 | 97 | </script> |
1172 | 97 | )<<<"; |
1173 | 97 | } |