//===- HTMLDiagnostics.cpp - HTML Diagnostics for Paths -------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines the HTMLDiagnostics object. // //===----------------------------------------------------------------------===// #include "clang/Analysis/PathDiagnostic.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/Stmt.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/Token.h" #include "clang/Rewrite/Core/HTMLRewrite.h" #include "clang/Rewrite/Core/Rewriter.h" #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" #include "clang/StaticAnalyzer/Core/IssueHash.h" #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Errc.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include #include #include #include #include #include #include #include #include #include using namespace clang; using namespace ento; //===----------------------------------------------------------------------===// // Boilerplate. //===----------------------------------------------------------------------===// namespace { class HTMLDiagnostics : public PathDiagnosticConsumer { std::string Directory; bool createdDir = false; bool noDir = false; const Preprocessor &PP; AnalyzerOptions &AnalyzerOpts; const bool SupportsCrossFileDiagnostics; public: HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &OutputDir, const Preprocessor &pp, bool supportsMultipleFiles) : Directory(OutputDir), PP(pp), AnalyzerOpts(AnalyzerOpts), SupportsCrossFileDiagnostics(supportsMultipleFiles) {} ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); } void FlushDiagnosticsImpl(std::vector &Diags, FilesMade *filesMade) override; StringRef getName() const override { return "HTMLDiagnostics"; } bool supportsCrossFileDiagnostics() const override { return SupportsCrossFileDiagnostics; } unsigned ProcessMacroPiece(raw_ostream &os, const PathDiagnosticMacroPiece& P, unsigned num); void HandlePiece(Rewriter &R, FileID BugFileID, const PathDiagnosticPiece &P, const std::vector &PopUpRanges, unsigned num, unsigned max); void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, const char *HighlightStart = "", const char *HighlightEnd = ""); void ReportDiag(const PathDiagnostic& D, FilesMade *filesMade); // Generate the full HTML report std::string GenerateHTML(const PathDiagnostic& D, Rewriter &R, const SourceManager& SMgr, const PathPieces& path, const char *declName); // Add HTML header/footers to file specified by FID void FinalizeHTML(const PathDiagnostic& D, Rewriter &R, const SourceManager& SMgr, const PathPieces& path, FileID FID, const FileEntry *Entry, const char *declName); // Rewrite the file specified by FID with HTML formatting. void RewriteFile(Rewriter &R, const PathPieces& path, FileID FID); private: /// \return Javascript for displaying shortcuts help; StringRef showHelpJavascript(); /// \return Javascript for navigating the HTML report using j/k keys. StringRef generateKeyboardNavigationJavascript(); /// \return JavaScript for an option to only show relevant lines. std::string showRelevantLinesJavascript( const PathDiagnostic &D, const PathPieces &path); /// Write executed lines from \p D in JSON format into \p os. void dumpCoverageData(const PathDiagnostic &D, const PathPieces &path, llvm::raw_string_ostream &os); }; } // namespace void ento::createHTMLDiagnosticConsumer( AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, const std::string &OutputDir, const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU) { // FIXME: HTML is currently our default output type, but if the output // directory isn't specified, it acts like if it was in the minimal text // output mode. This doesn't make much sense, we should have the minimal text // as our default. In the case of backward compatibility concerns, this could // be preserved with -analyzer-config-compatibility-mode=true. createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputDir, PP, CTU); // TODO: Emit an error here. if (OutputDir.empty()) return; C.push_back(new HTMLDiagnostics(AnalyzerOpts, OutputDir, PP, true)); } void ento::createHTMLSingleFileDiagnosticConsumer( AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, const std::string &OutputDir, const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU) { // TODO: Emit an error here. if (OutputDir.empty()) return; C.push_back(new HTMLDiagnostics(AnalyzerOpts, OutputDir, PP, false)); createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputDir, PP, CTU); } void ento::createPlistHTMLDiagnosticConsumer( AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, const std::string &prefix, const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU) { createHTMLDiagnosticConsumer( AnalyzerOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP, CTU); createPlistMultiFileDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU); createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU); } //===----------------------------------------------------------------------===// // Report processing. //===----------------------------------------------------------------------===// void HTMLDiagnostics::FlushDiagnosticsImpl( std::vector &Diags, FilesMade *filesMade) { for (const auto Diag : Diags) ReportDiag(*Diag, filesMade); } void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D, FilesMade *filesMade) { // Create the HTML directory if it is missing. if (!createdDir) { createdDir = true; if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) { llvm::errs() << "warning: could not create directory '" << Directory << "': " << ec.message() << '\n'; noDir = true; return; } } if (noDir) return; // First flatten out the entire path to make it easier to use. PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false); // The path as already been prechecked that the path is non-empty. assert(!path.empty()); const SourceManager &SMgr = path.front()->getLocation().getManager(); // Create a new rewriter to generate HTML. Rewriter R(const_cast(SMgr), PP.getLangOpts()); // The file for the first path element is considered the main report file, it // will usually be equivalent to SMgr.getMainFileID(); however, it might be a // header when -analyzer-opt-analyze-headers is used. FileID ReportFile = path.front()->getLocation().asLocation().getExpansionLoc().getFileID(); // Get the function/method name SmallString<128> declName("unknown"); int offsetDecl = 0; if (const Decl *DeclWithIssue = D.getDeclWithIssue()) { if (const auto *ND = dyn_cast(DeclWithIssue)) declName = ND->getDeclName().getAsString(); if (const Stmt *Body = DeclWithIssue->getBody()) { // Retrieve the relative position of the declaration which will be used // for the file name FullSourceLoc L( SMgr.getExpansionLoc(path.back()->getLocation().asLocation()), SMgr); FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getBeginLoc()), SMgr); offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber(); } } std::string report = GenerateHTML(D, R, SMgr, path, declName.c_str()); if (report.empty()) { llvm::errs() << "warning: no diagnostics generated for main file.\n"; return; } // Create a path for the target HTML file. int FD; SmallString<128> Model, ResultPath; if (!AnalyzerOpts.ShouldWriteStableReportFilename) { llvm::sys::path::append(Model, Directory, "report-%%%%%%.html"); if (std::error_code EC = llvm::sys::fs::make_absolute(Model)) { llvm::errs() << "warning: could not make '" << Model << "' absolute: " << EC.message() << '\n'; return; } if (std::error_code EC = llvm::sys::fs::createUniqueFile(Model, FD, ResultPath)) { llvm::errs() << "warning: could not create file in '" << Directory << "': " << EC.message() << '\n'; return; } } else { int i = 1; std::error_code EC; do { // Find a filename which is not already used const FileEntry* Entry = SMgr.getFileEntryForID(ReportFile); std::stringstream filename; Model = ""; filename << "report-" << llvm::sys::path::filename(Entry->getName()).str() << "-" << declName.c_str() << "-" << offsetDecl << "-" << i << ".html"; llvm::sys::path::append(Model, Directory, filename.str()); EC = llvm::sys::fs::openFileForReadWrite( Model, FD, llvm::sys::fs::CD_CreateNew, llvm::sys::fs::OF_None); if (EC && EC != llvm::errc::file_exists) { llvm::errs() << "warning: could not create file '" << Model << "': " << EC.message() << '\n'; return; } i++; } while (EC); } llvm::raw_fd_ostream os(FD, true); if (filesMade) filesMade->addDiagnostic(D, getName(), llvm::sys::path::filename(ResultPath)); // Emit the HTML to disk. os << report; } std::string HTMLDiagnostics::GenerateHTML(const PathDiagnostic& D, Rewriter &R, const SourceManager& SMgr, const PathPieces& path, const char *declName) { // Rewrite source files as HTML for every new file the path crosses std::vector FileIDs; for (auto I : path) { FileID FID = I->getLocation().asLocation().getExpansionLoc().getFileID(); if (llvm::is_contained(FileIDs, FID)) continue; FileIDs.push_back(FID); RewriteFile(R, path, FID); } if (SupportsCrossFileDiagnostics && FileIDs.size() > 1) { // Prefix file names, anchor tags, and nav cursors to every file for (auto I = FileIDs.begin(), E = FileIDs.end(); I != E; I++) { std::string s; llvm::raw_string_ostream os(s); if (I != FileIDs.begin()) os << "
\n"; os << "
getHashValue() << ">\n"; // Left nav arrow if (I != FileIDs.begin()) os << ""; os << "

" << SMgr.getFileEntryForID(*I)->getName() << "

\n"; // Right nav arrow if (I + 1 != E) os << ""; os << "
\n"; R.InsertTextBefore(SMgr.getLocForStartOfFile(*I), os.str()); } // Append files to the main report file in the order they appear in the path for (auto I : llvm::make_range(FileIDs.begin() + 1, FileIDs.end())) { std::string s; llvm::raw_string_ostream os(s); const RewriteBuffer *Buf = R.getRewriteBufferFor(I); for (auto BI : *Buf) os << BI; R.InsertTextAfter(SMgr.getLocForEndOfFile(FileIDs[0]), os.str()); } } const RewriteBuffer *Buf = R.getRewriteBufferFor(FileIDs[0]); if (!Buf) return {}; // Add CSS, header, and footer. FileID FID = path.back()->getLocation().asLocation().getExpansionLoc().getFileID(); const FileEntry* Entry = SMgr.getFileEntryForID(FID); FinalizeHTML(D, R, SMgr, path, FileIDs[0], Entry, declName); std::string file; llvm::raw_string_ostream os(file); for (auto BI : *Buf) os << BI; return os.str(); } void HTMLDiagnostics::dumpCoverageData( const PathDiagnostic &D, const PathPieces &path, llvm::raw_string_ostream &os) { const FilesToLineNumsMap &ExecutedLines = D.getExecutedLines(); os << "var relevant_lines = {"; for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) { if (I != ExecutedLines.begin()) os << ", "; os << "\"" << I->first.getHashValue() << "\": {"; for (unsigned LineNo : I->second) { if (LineNo != *(I->second.begin())) os << ", "; os << "\"" << LineNo << "\": 1"; } os << "}"; } os << "};"; } std::string HTMLDiagnostics::showRelevantLinesJavascript( const PathDiagnostic &D, const PathPieces &path) { std::string s; llvm::raw_string_ostream os(s); os << "
)<<<"; return os.str(); } void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R, const SourceManager& SMgr, const PathPieces& path, FileID FID, const FileEntry *Entry, const char *declName) { // This is a cludge; basically we want to append either the full // working directory if we have no directory information. This is // a work in progress. llvm::SmallString<0> DirName; if (llvm::sys::path::is_relative(Entry->getName())) { llvm::sys::fs::current_path(DirName); DirName += '/'; } int LineNumber = path.back()->getLocation().asLocation().getExpansionLineNumber(); int ColumnNumber = path.back()->getLocation().asLocation().getExpansionColumnNumber(); R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), showHelpJavascript()); R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), generateKeyboardNavigationJavascript()); // Checkbox and javascript for filtering the output to the counterexample. R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), showRelevantLinesJavascript(D, path)); // Add the name of the file as an

tag. { std::string s; llvm::raw_string_ostream os(s); os << "\n" << "

Bug Summary

\n\n" "\n\n"; // The navigation across the extra notes pieces. unsigned NumExtraPieces = 0; for (const auto &Piece : path) { if (const auto *P = dyn_cast(Piece.get())) { int LineNumber = P->getLocation().asLocation().getExpansionLineNumber(); int ColumnNumber = P->getLocation().asLocation().getExpansionColumnNumber(); os << ""; ++NumExtraPieces; } } // Output any other meta data. for (PathDiagnostic::meta_iterator I = D.meta_begin(), E = D.meta_end(); I != E; ++I) { os << "\n"; } os << R"<<<(
File:" << html::EscapeText(DirName) << html::EscapeText(Entry->getName()) << "
Warning:" "line " << LineNumber << ", column " << ColumnNumber << "
" << D.getVerboseDescription() << "
Note:" << "line " << LineNumber << ", column " << ColumnNumber << "
" << P->getString() << "
" << html::EscapeText(*I) << "

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 )<<<"; os << html::EscapeText(AnalyzerOpts.FullCompilerInvocation); os << R"<<<(
)<<<"; R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); } // Embed meta-data tags. { std::string s; llvm::raw_string_ostream os(s); StringRef BugDesc = D.getVerboseDescription(); if (!BugDesc.empty()) os << "\n\n"; StringRef BugType = D.getBugType(); if (!BugType.empty()) os << "\n\n"; PathDiagnosticLocation UPDLoc = D.getUniqueingLoc(); FullSourceLoc L(SMgr.getExpansionLoc(UPDLoc.isValid() ? UPDLoc.asLocation() : D.getLocation().asLocation()), SMgr); const Decl *DeclWithIssue = D.getDeclWithIssue(); StringRef BugCategory = D.getCategory(); if (!BugCategory.empty()) os << "\n\n"; os << "\n\n"; os << "\n\n"; os << "\n\n"; os << "\n\n"; os << "\n\n"; os << "\n\n"; os << "\n\n"; // Mark the end of the tags. os << "\n\n"; // Insert the text. R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); } html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); } StringRef HTMLDiagnostics::showHelpJavascript() { return R"<<<( )<<<"; } static bool shouldDisplayPopUpRange(const SourceRange &Range) { return !(Range.getBegin().isMacroID() || Range.getEnd().isMacroID()); } static void HandlePopUpPieceStartTag(Rewriter &R, const std::vector &PopUpRanges) { for (const auto &Range : PopUpRanges) { if (!shouldDisplayPopUpRange(Range)) continue; html::HighlightRange(R, Range.getBegin(), Range.getEnd(), "", "", /*IsTokenRange=*/true); } } static void HandlePopUpPieceEndTag(Rewriter &R, const PathDiagnosticPopUpPiece &Piece, std::vector &PopUpRanges, unsigned int LastReportedPieceIndex, unsigned int PopUpPieceIndex) { SmallString<256> Buf; llvm::raw_svector_ostream Out(Buf); SourceRange Range(Piece.getLocation().asRange()); if (!shouldDisplayPopUpRange(Range)) return; // Write out the path indices with a right arrow and the message as a row. Out << ""; // If no report made at this range mark the variable and add the end tags. if (std::find(PopUpRanges.begin(), PopUpRanges.end(), Range) == PopUpRanges.end()) { // Store that we create a report at this range. PopUpRanges.push_back(Range); Out << "
" << LastReportedPieceIndex; // Also annotate the state transition with extra indices. Out << '.' << PopUpPieceIndex; Out << "
" << Piece.getString() << "
"; html::HighlightRange(R, Range.getBegin(), Range.getEnd(), "", Buf.c_str(), /*IsTokenRange=*/true); } else { // Otherwise inject just the new row at the end of the range. html::HighlightRange(R, Range.getBegin(), Range.getEnd(), "", Buf.c_str(), /*IsTokenRange=*/true); } } void HTMLDiagnostics::RewriteFile(Rewriter &R, const PathPieces& path, FileID FID) { // Process the path. // Maintain the counts of extra note pieces separately. unsigned TotalPieces = path.size(); unsigned TotalNotePieces = std::count_if( path.begin(), path.end(), [](const PathDiagnosticPieceRef &p) { return isa(*p); }); unsigned PopUpPieceCount = std::count_if( path.begin(), path.end(), [](const PathDiagnosticPieceRef &p) { return isa(*p); }); unsigned TotalRegularPieces = TotalPieces - TotalNotePieces - PopUpPieceCount; unsigned NumRegularPieces = TotalRegularPieces; unsigned NumNotePieces = TotalNotePieces; // Stores the count of the regular piece indices. std::map IndexMap; // Stores the different ranges where we have reported something. std::vector PopUpRanges; for (auto I = path.rbegin(), E = path.rend(); I != E; ++I) { const auto &Piece = *I->get(); if (isa(Piece)) { ++IndexMap[NumRegularPieces]; } else if (isa(Piece)) { // This adds diagnostic bubbles, but not navigation. // Navigation through note pieces would be added later, // as a separate pass through the piece list. HandlePiece(R, FID, Piece, PopUpRanges, NumNotePieces, TotalNotePieces); --NumNotePieces; } else { HandlePiece(R, FID, Piece, PopUpRanges, NumRegularPieces, TotalRegularPieces); --NumRegularPieces; } } // Secondary indexing if we are having multiple pop-ups between two notes. // (e.g. [(13) 'a' is 'true']; [(13.1) 'b' is 'false']; [(13.2) 'c' is...) NumRegularPieces = TotalRegularPieces; for (auto I = path.rbegin(), E = path.rend(); I != E; ++I) { const auto &Piece = *I->get(); if (const auto *PopUpP = dyn_cast(&Piece)) { int PopUpPieceIndex = IndexMap[NumRegularPieces]; // Pop-up pieces needs the index of the last reported piece and its count // how many times we report to handle multiple reports on the same range. // This marks the variable, adds the end tag and the message // (list element) as a row. The start tag will be added after the // rows has been written out. Note: It stores every different range. HandlePopUpPieceEndTag(R, *PopUpP, PopUpRanges, NumRegularPieces, PopUpPieceIndex); if (PopUpPieceIndex > 0) --IndexMap[NumRegularPieces]; } else if (!isa(Piece)) { --NumRegularPieces; } } // Add the
start tag of pop-up pieces based on the stored ranges. HandlePopUpPieceStartTag(R, PopUpRanges); // Add line numbers, header, footer, etc. html::EscapeText(R, FID); html::AddLineNumbers(R, FID); // If we have a preprocessor, relex the file and syntax highlight. // We might not have a preprocessor if we come from a deserialized AST file, // for example. html::SyntaxHighlight(R, FID, PP); html::HighlightMacros(R, FID, PP); } void HTMLDiagnostics::HandlePiece(Rewriter &R, FileID BugFileID, const PathDiagnosticPiece &P, const std::vector &PopUpRanges, unsigned num, unsigned max) { // For now, just draw a box above the line in question, and emit the // warning. FullSourceLoc Pos = P.getLocation().asLocation(); if (!Pos.isValid()) return; SourceManager &SM = R.getSourceMgr(); assert(&Pos.getManager() == &SM && "SourceManagers are different!"); std::pair LPosInfo = SM.getDecomposedExpansionLoc(Pos); if (LPosInfo.first != BugFileID) return; const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first); const char* FileStart = Buf->getBufferStart(); // Compute the column number. Rewind from the current position to the start // of the line. unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData(); const char *LineStart = TokInstantiationPtr-ColNo; // Compute LineEnd. const char *LineEnd = TokInstantiationPtr; const char* FileEnd = Buf->getBufferEnd(); while (*LineEnd != '\n' && LineEnd != FileEnd) ++LineEnd; // Compute the margin offset by counting tabs and non-tabs. unsigned PosNo = 0; for (const char* c = LineStart; c != TokInstantiationPtr; ++c) PosNo += *c == '\t' ? 8 : 1; // Create the html for the message. const char *Kind = nullptr; bool IsNote = false; bool SuppressIndex = (max == 1); switch (P.getKind()) { case PathDiagnosticPiece::Event: Kind = "Event"; break; case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break; // Setting Kind to "Control" is intentional. case PathDiagnosticPiece::Macro: Kind = "Control"; break; case PathDiagnosticPiece::Note: Kind = "Note"; IsNote = true; SuppressIndex = true; break; case PathDiagnosticPiece::Call: case PathDiagnosticPiece::PopUp: llvm_unreachable("Calls and extra notes should already be handled"); } std::string sbuf; llvm::raw_string_ostream os(sbuf); os << "\n"; if (num < max) { os << ""; } os << "
(P)) { // Get the string and determining its maximum substring. const auto &Msg = P.getString(); unsigned max_token = 0; unsigned cnt = 0; unsigned len = Msg.size(); for (char C : Msg) switch (C) { default: ++cnt; continue; case ' ': case '\t': case '\n': if (cnt > max_token) max_token = cnt; cnt = 0; } if (cnt > max_token) max_token = cnt; // Determine the approximate size of the message bubble in em. unsigned em; const unsigned max_line = 120; if (max_token >= max_line) em = max_token / 2; else { unsigned characters = max_line; unsigned lines = len / max_line; if (lines > 0) { for (; characters > max_token; --characters) if (len / characters > lines) { ++characters; break; } } em = characters / 2; } if (em < max_line/2) os << "; max-width:" << em << "em"; } else os << "; max-width:100em"; os << "\">"; if (!SuppressIndex) { os << ""; if (num < max) { os << ""; } os << "
"; os << "
" << num << "
"; if (num > 1) { os << "
"; } os << ""; } if (const auto *MP = dyn_cast(&P)) { os << "Within the expansion of the macro '"; // Get the name of the macro by relexing it. { FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc(); assert(L.isFileID()); StringRef BufferInfo = L.getBufferData(); std::pair LocInfo = L.getDecomposedLoc(); const char* MacroName = LocInfo.second + BufferInfo.data(); Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(), BufferInfo.begin(), MacroName, BufferInfo.end()); Token TheTok; rawLexer.LexFromRawLexer(TheTok); for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i) os << MacroName[i]; } os << "':\n"; if (!SuppressIndex) { os << "
"; } // Within a macro piece. Write out each event. ProcessMacroPiece(os, *MP, 0); } else { os << html::EscapeText(P.getString()); if (!SuppressIndex) { os << "
"; } } os << ""; // Insert the new html. unsigned DisplayPos = LineEnd - FileStart; SourceLocation Loc = SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos); R.InsertTextBefore(Loc, os.str()); // Now highlight the ranges. ArrayRef Ranges = P.getRanges(); for (const auto &Range : Ranges) { // If we have already highlighted the range as a pop-up there is no work. if (std::find(PopUpRanges.begin(), PopUpRanges.end(), Range) != PopUpRanges.end()) continue; HighlightRange(R, LPosInfo.first, Range); } } static void EmitAlphaCounter(raw_ostream &os, unsigned n) { unsigned x = n % ('z' - 'a'); n /= 'z' - 'a'; if (n > 0) EmitAlphaCounter(os, n); os << char('a' + x); } unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os, const PathDiagnosticMacroPiece& P, unsigned num) { for (const auto &subPiece : P.subPieces) { if (const auto *MP = dyn_cast(subPiece.get())) { num = ProcessMacroPiece(os, *MP, num); continue; } if (const auto *EP = dyn_cast(subPiece.get())) { os << "
" "" "
"; EmitAlphaCounter(os, num++); os << "
" << html::EscapeText(EP->getString()) << "
\n"; } } return num; } void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, const char *HighlightStart, const char *HighlightEnd) { SourceManager &SM = R.getSourceMgr(); const LangOptions &LangOpts = R.getLangOpts(); SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin()); unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart); SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd()); unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd); if (EndLineNo < StartLineNo) return; if (SM.getFileID(InstantiationStart) != BugFileID || SM.getFileID(InstantiationEnd) != BugFileID) return; // Compute the column number of the end. unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd); unsigned OldEndColNo = EndColNo; if (EndColNo) { // Add in the length of the token, so that we cover multi-char tokens. EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; } // Highlight the range. Make the span tag the outermost tag for the // selected range. SourceLocation E = InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo); html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); } StringRef HTMLDiagnostics::generateKeyboardNavigationJavascript() { return R"<<<( )<<<"; }