10b57cec5SDimitry Andric //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines the HTMLRewriter class, which is used to translate the 100b57cec5SDimitry Andric // text of a source file into prettified HTML. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/Rewrite/Core/HTMLRewrite.h" 150b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 160b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 170b57cec5SDimitry Andric #include "clang/Lex/TokenConcatenation.h" 180b57cec5SDimitry Andric #include "clang/Rewrite/Core/Rewriter.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 200b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 210b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 220b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 230b57cec5SDimitry Andric #include <memory> 240b57cec5SDimitry Andric 25*0fca6ea1SDimitry Andric using namespace clang; 26*0fca6ea1SDimitry Andric using namespace llvm; 27*0fca6ea1SDimitry Andric using namespace html; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric /// HighlightRange - Highlight a range in the source code with the specified 300b57cec5SDimitry Andric /// start/end tags. B/E must be in the same file. This ensures that 310b57cec5SDimitry Andric /// start/end tags are placed at the start/end of each line if the range is 320b57cec5SDimitry Andric /// multiline. 330b57cec5SDimitry Andric void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, 340b57cec5SDimitry Andric const char *StartTag, const char *EndTag, 350b57cec5SDimitry Andric bool IsTokenRange) { 360b57cec5SDimitry Andric SourceManager &SM = R.getSourceMgr(); 370b57cec5SDimitry Andric B = SM.getExpansionLoc(B); 380b57cec5SDimitry Andric E = SM.getExpansionLoc(E); 390b57cec5SDimitry Andric FileID FID = SM.getFileID(B); 400b57cec5SDimitry Andric assert(SM.getFileID(E) == FID && "B/E not in the same file!"); 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric unsigned BOffset = SM.getFileOffset(B); 430b57cec5SDimitry Andric unsigned EOffset = SM.getFileOffset(E); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // Include the whole end token in the range. 460b57cec5SDimitry Andric if (IsTokenRange) 470b57cec5SDimitry Andric EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts()); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric bool Invalid = false; 500b57cec5SDimitry Andric const char *BufferStart = SM.getBufferData(FID, &Invalid).data(); 510b57cec5SDimitry Andric if (Invalid) 520b57cec5SDimitry Andric return; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric HighlightRange(R.getEditBuffer(FID), BOffset, EOffset, 550b57cec5SDimitry Andric BufferStart, StartTag, EndTag); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric /// HighlightRange - This is the same as the above method, but takes 590b57cec5SDimitry Andric /// decomposed file locations. 600b57cec5SDimitry Andric void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, 610b57cec5SDimitry Andric const char *BufferStart, 620b57cec5SDimitry Andric const char *StartTag, const char *EndTag) { 630b57cec5SDimitry Andric // Insert the tag at the absolute start/end of the range. 640b57cec5SDimitry Andric RB.InsertTextAfter(B, StartTag); 650b57cec5SDimitry Andric RB.InsertTextBefore(E, EndTag); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric // Scan the range to see if there is a \r or \n. If so, and if the line is 680b57cec5SDimitry Andric // not blank, insert tags on that line as well. 690b57cec5SDimitry Andric bool HadOpenTag = true; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric unsigned LastNonWhiteSpace = B; 720b57cec5SDimitry Andric for (unsigned i = B; i != E; ++i) { 730b57cec5SDimitry Andric switch (BufferStart[i]) { 740b57cec5SDimitry Andric case '\r': 750b57cec5SDimitry Andric case '\n': 760b57cec5SDimitry Andric // Okay, we found a newline in the range. If we have an open tag, we need 770b57cec5SDimitry Andric // to insert a close tag at the first non-whitespace before the newline. 780b57cec5SDimitry Andric if (HadOpenTag) 790b57cec5SDimitry Andric RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag); 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // Instead of inserting an open tag immediately after the newline, we 820b57cec5SDimitry Andric // wait until we see a non-whitespace character. This prevents us from 830b57cec5SDimitry Andric // inserting tags around blank lines, and also allows the open tag to 840b57cec5SDimitry Andric // be put *after* whitespace on a non-blank line. 850b57cec5SDimitry Andric HadOpenTag = false; 860b57cec5SDimitry Andric break; 870b57cec5SDimitry Andric case '\0': 880b57cec5SDimitry Andric case ' ': 890b57cec5SDimitry Andric case '\t': 900b57cec5SDimitry Andric case '\f': 910b57cec5SDimitry Andric case '\v': 920b57cec5SDimitry Andric // Ignore whitespace. 930b57cec5SDimitry Andric break; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric default: 960b57cec5SDimitry Andric // If there is no tag open, do it now. 970b57cec5SDimitry Andric if (!HadOpenTag) { 980b57cec5SDimitry Andric RB.InsertTextAfter(i, StartTag); 990b57cec5SDimitry Andric HadOpenTag = true; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Remember this character. 1030b57cec5SDimitry Andric LastNonWhiteSpace = i; 1040b57cec5SDimitry Andric break; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 109*0fca6ea1SDimitry Andric namespace clang::html { 110*0fca6ea1SDimitry Andric struct RelexRewriteCache { 111*0fca6ea1SDimitry Andric // These structs mimic input arguments of HighlightRange(). 112*0fca6ea1SDimitry Andric struct Highlight { 113*0fca6ea1SDimitry Andric SourceLocation B, E; 114*0fca6ea1SDimitry Andric std::string StartTag, EndTag; 115*0fca6ea1SDimitry Andric bool IsTokenRange; 116*0fca6ea1SDimitry Andric }; 117*0fca6ea1SDimitry Andric struct RawHighlight { 118*0fca6ea1SDimitry Andric unsigned B, E; 119*0fca6ea1SDimitry Andric std::string StartTag, EndTag; 120*0fca6ea1SDimitry Andric }; 121*0fca6ea1SDimitry Andric 122*0fca6ea1SDimitry Andric // SmallVector isn't appropriate because these vectors are almost never small. 123*0fca6ea1SDimitry Andric using HighlightList = std::vector<Highlight>; 124*0fca6ea1SDimitry Andric using RawHighlightList = std::vector<RawHighlight>; 125*0fca6ea1SDimitry Andric 126*0fca6ea1SDimitry Andric DenseMap<FileID, RawHighlightList> SyntaxHighlights; 127*0fca6ea1SDimitry Andric DenseMap<FileID, HighlightList> MacroHighlights; 128*0fca6ea1SDimitry Andric }; 129*0fca6ea1SDimitry Andric } // namespace clang::html 130*0fca6ea1SDimitry Andric 131*0fca6ea1SDimitry Andric html::RelexRewriteCacheRef html::instantiateRelexRewriteCache() { 132*0fca6ea1SDimitry Andric return std::make_shared<RelexRewriteCache>(); 133*0fca6ea1SDimitry Andric } 134*0fca6ea1SDimitry Andric 1350b57cec5SDimitry Andric void html::EscapeText(Rewriter &R, FileID FID, 1360b57cec5SDimitry Andric bool EscapeSpaces, bool ReplaceTabs) { 1370b57cec5SDimitry Andric 138e8d8bef9SDimitry Andric llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID); 139e8d8bef9SDimitry Andric const char* C = Buf.getBufferStart(); 140e8d8bef9SDimitry Andric const char* FileEnd = Buf.getBufferEnd(); 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric assert (C <= FileEnd); 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric RewriteBuffer &RB = R.getEditBuffer(FID); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric unsigned ColNo = 0; 1470b57cec5SDimitry Andric for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { 1480b57cec5SDimitry Andric switch (*C) { 1490b57cec5SDimitry Andric default: ++ColNo; break; 1500b57cec5SDimitry Andric case '\n': 1510b57cec5SDimitry Andric case '\r': 1520b57cec5SDimitry Andric ColNo = 0; 1530b57cec5SDimitry Andric break; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric case ' ': 1560b57cec5SDimitry Andric if (EscapeSpaces) 1570b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, " "); 1580b57cec5SDimitry Andric ++ColNo; 1590b57cec5SDimitry Andric break; 1600b57cec5SDimitry Andric case '\f': 1610b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, "<hr>"); 1620b57cec5SDimitry Andric ColNo = 0; 1630b57cec5SDimitry Andric break; 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric case '\t': { 1660b57cec5SDimitry Andric if (!ReplaceTabs) 1670b57cec5SDimitry Andric break; 1680b57cec5SDimitry Andric unsigned NumSpaces = 8-(ColNo&7); 1690b57cec5SDimitry Andric if (EscapeSpaces) 1700b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, 1710b57cec5SDimitry Andric StringRef(" " 1720b57cec5SDimitry Andric " ", 6*NumSpaces)); 1730b57cec5SDimitry Andric else 1740b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces)); 1750b57cec5SDimitry Andric ColNo += NumSpaces; 1760b57cec5SDimitry Andric break; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric case '<': 1790b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, "<"); 1800b57cec5SDimitry Andric ++ColNo; 1810b57cec5SDimitry Andric break; 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric case '>': 1840b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, ">"); 1850b57cec5SDimitry Andric ++ColNo; 1860b57cec5SDimitry Andric break; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric case '&': 1890b57cec5SDimitry Andric RB.ReplaceText(FilePos, 1, "&"); 1900b57cec5SDimitry Andric ++ColNo; 1910b57cec5SDimitry Andric break; 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) { 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric unsigned len = s.size(); 1990b57cec5SDimitry Andric std::string Str; 2000b57cec5SDimitry Andric llvm::raw_string_ostream os(Str); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric for (unsigned i = 0 ; i < len; ++i) { 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric char c = s[i]; 2050b57cec5SDimitry Andric switch (c) { 2060b57cec5SDimitry Andric default: 2070b57cec5SDimitry Andric os << c; break; 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric case ' ': 2100b57cec5SDimitry Andric if (EscapeSpaces) os << " "; 2110b57cec5SDimitry Andric else os << ' '; 2120b57cec5SDimitry Andric break; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric case '\t': 2150b57cec5SDimitry Andric if (ReplaceTabs) { 2160b57cec5SDimitry Andric if (EscapeSpaces) 2170b57cec5SDimitry Andric for (unsigned i = 0; i < 4; ++i) 2180b57cec5SDimitry Andric os << " "; 2190b57cec5SDimitry Andric else 2200b57cec5SDimitry Andric for (unsigned i = 0; i < 4; ++i) 2210b57cec5SDimitry Andric os << " "; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric else 2240b57cec5SDimitry Andric os << c; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric break; 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric case '<': os << "<"; break; 2290b57cec5SDimitry Andric case '>': os << ">"; break; 2300b57cec5SDimitry Andric case '&': os << "&"; break; 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340eae32dcSDimitry Andric return Str; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, 2380b57cec5SDimitry Andric unsigned B, unsigned E) { 2390b57cec5SDimitry Andric SmallString<256> Str; 2400b57cec5SDimitry Andric llvm::raw_svector_ostream OS(Str); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric OS << "<tr class=\"codeline\" data-linenumber=\"" << LineNo << "\">" 2430b57cec5SDimitry Andric << "<td class=\"num\" id=\"LN" << LineNo << "\">" << LineNo 2440b57cec5SDimitry Andric << "</td><td class=\"line\">"; 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric if (B == E) { // Handle empty lines. 2470b57cec5SDimitry Andric OS << " </td></tr>"; 2480b57cec5SDimitry Andric RB.InsertTextBefore(B, OS.str()); 2490b57cec5SDimitry Andric } else { 2500b57cec5SDimitry Andric RB.InsertTextBefore(B, OS.str()); 2510b57cec5SDimitry Andric RB.InsertTextBefore(E, "</td></tr>"); 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric void html::AddLineNumbers(Rewriter& R, FileID FID) { 2560b57cec5SDimitry Andric 257e8d8bef9SDimitry Andric llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID); 258e8d8bef9SDimitry Andric const char* FileBeg = Buf.getBufferStart(); 259e8d8bef9SDimitry Andric const char* FileEnd = Buf.getBufferEnd(); 2600b57cec5SDimitry Andric const char* C = FileBeg; 2610b57cec5SDimitry Andric RewriteBuffer &RB = R.getEditBuffer(FID); 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric assert (C <= FileEnd); 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric unsigned LineNo = 0; 2660b57cec5SDimitry Andric unsigned FilePos = 0; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric while (C != FileEnd) { 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric ++LineNo; 2710b57cec5SDimitry Andric unsigned LineStartPos = FilePos; 2720b57cec5SDimitry Andric unsigned LineEndPos = FileEnd - FileBeg; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric assert (FilePos <= LineEndPos); 2750b57cec5SDimitry Andric assert (C < FileEnd); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric // Scan until the newline (or end-of-file). 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric while (C != FileEnd) { 2800b57cec5SDimitry Andric char c = *C; 2810b57cec5SDimitry Andric ++C; 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric if (c == '\n') { 2840b57cec5SDimitry Andric LineEndPos = FilePos++; 2850b57cec5SDimitry Andric break; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric ++FilePos; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Add one big table tag that surrounds all of the code. 2950b57cec5SDimitry Andric std::string s; 2960b57cec5SDimitry Andric llvm::raw_string_ostream os(s); 2970b57cec5SDimitry Andric os << "<table class=\"code\" data-fileid=\"" << FID.getHashValue() << "\">\n"; 2980b57cec5SDimitry Andric RB.InsertTextBefore(0, os.str()); 2990b57cec5SDimitry Andric RB.InsertTextAfter(FileEnd - FileBeg, "</table>"); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric void html::AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, 3030b57cec5SDimitry Andric StringRef title) { 3040b57cec5SDimitry Andric 305e8d8bef9SDimitry Andric llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID); 306e8d8bef9SDimitry Andric const char* FileStart = Buf.getBufferStart(); 307e8d8bef9SDimitry Andric const char* FileEnd = Buf.getBufferEnd(); 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID); 3100b57cec5SDimitry Andric SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart); 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric std::string s; 3130b57cec5SDimitry Andric llvm::raw_string_ostream os(s); 3140b57cec5SDimitry Andric os << "<!doctype html>\n" // Use HTML 5 doctype 3150b57cec5SDimitry Andric "<html>\n<head>\n"; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric if (!title.empty()) 3180b57cec5SDimitry Andric os << "<title>" << html::EscapeText(title) << "</title>\n"; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric os << R"<<<( 3210b57cec5SDimitry Andric <style type="text/css"> 3220b57cec5SDimitry Andric body { color:#000000; background-color:#ffffff } 3230b57cec5SDimitry Andric body { font-family:Helvetica, sans-serif; font-size:10pt } 3240b57cec5SDimitry Andric h1 { font-size:14pt } 3250b57cec5SDimitry Andric .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; } 3260b57cec5SDimitry Andric .FileNav { margin-left: 5px; margin-right: 5px; display: inline; } 3270b57cec5SDimitry Andric .FileNav a { text-decoration:none; font-size: larger; } 3280b57cec5SDimitry Andric .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; } 3290b57cec5SDimitry Andric .divider { background-color: gray; } 3300b57cec5SDimitry Andric .code { border-collapse:collapse; width:100%; } 3310b57cec5SDimitry Andric .code { font-family: "Monospace", monospace; font-size:10pt } 3320b57cec5SDimitry Andric .code { line-height: 1.2em } 3330b57cec5SDimitry Andric .comment { color: green; font-style: oblique } 3340b57cec5SDimitry Andric .keyword { color: blue } 3350b57cec5SDimitry Andric .string_literal { color: red } 3360b57cec5SDimitry Andric .directive { color: darkmagenta } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric /* Macros and variables could have pop-up notes hidden by default. 3390b57cec5SDimitry Andric - Macro pop-up: expansion of the macro 3400b57cec5SDimitry Andric - Variable pop-up: value (table) of the variable */ 3410b57cec5SDimitry Andric .macro_popup, .variable_popup { display: none; } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric /* Pop-up appears on mouse-hover event. */ 3440b57cec5SDimitry Andric .macro:hover .macro_popup, .variable:hover .variable_popup { 3450b57cec5SDimitry Andric display: block; 3460b57cec5SDimitry Andric padding: 2px; 3470b57cec5SDimitry Andric -webkit-border-radius:5px; 3480b57cec5SDimitry Andric -webkit-box-shadow:1px 1px 7px #000; 3490b57cec5SDimitry Andric border-radius:5px; 3500b57cec5SDimitry Andric box-shadow:1px 1px 7px #000; 3510b57cec5SDimitry Andric position: absolute; 3520b57cec5SDimitry Andric top: -1em; 3530b57cec5SDimitry Andric left:10em; 3540b57cec5SDimitry Andric z-index: 1 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric .macro_popup { 3580b57cec5SDimitry Andric border: 2px solid red; 3590b57cec5SDimitry Andric background-color:#FFF0F0; 3600b57cec5SDimitry Andric font-weight: normal; 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric .variable_popup { 3640b57cec5SDimitry Andric border: 2px solid blue; 3650b57cec5SDimitry Andric background-color:#F0F0FF; 3660b57cec5SDimitry Andric font-weight: bold; 3670b57cec5SDimitry Andric font-family: Helvetica, sans-serif; 3680b57cec5SDimitry Andric font-size: 9pt; 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric /* Pop-up notes needs a relative position as a base where they pops up. */ 3720b57cec5SDimitry Andric .macro, .variable { 3730b57cec5SDimitry Andric background-color: PaleGoldenRod; 3740b57cec5SDimitry Andric position: relative; 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric .macro { color: DarkMagenta; } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric #tooltiphint { 3790b57cec5SDimitry Andric position: fixed; 3800b57cec5SDimitry Andric width: 50em; 3810b57cec5SDimitry Andric margin-left: -25em; 3820b57cec5SDimitry Andric left: 50%; 3830b57cec5SDimitry Andric padding: 10px; 3840b57cec5SDimitry Andric border: 1px solid #b0b0b0; 3850b57cec5SDimitry Andric border-radius: 2px; 3860b57cec5SDimitry Andric box-shadow: 1px 1px 7px black; 3870b57cec5SDimitry Andric background-color: #c0c0c0; 3880b57cec5SDimitry Andric z-index: 2; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric .num { width:2.5em; padding-right:2ex; background-color:#eeeeee } 3920b57cec5SDimitry Andric .num { text-align:right; font-size:8pt } 3930b57cec5SDimitry Andric .num { color:#444444 } 3940b57cec5SDimitry Andric .line { padding-left: 1ex; border-left: 3px solid #ccc } 3950b57cec5SDimitry Andric .line { white-space: pre } 3960b57cec5SDimitry Andric .msg { -webkit-box-shadow:1px 1px 7px #000 } 3970b57cec5SDimitry Andric .msg { box-shadow:1px 1px 7px #000 } 3980b57cec5SDimitry Andric .msg { -webkit-border-radius:5px } 3990b57cec5SDimitry Andric .msg { border-radius:5px } 4000b57cec5SDimitry Andric .msg { font-family:Helvetica, sans-serif; font-size:8pt } 4010b57cec5SDimitry Andric .msg { float:left } 402349cc55cSDimitry Andric .msg { position:relative } 4030b57cec5SDimitry Andric .msg { padding:0.25em 1ex 0.25em 1ex } 4040b57cec5SDimitry Andric .msg { margin-top:10px; margin-bottom:10px } 4050b57cec5SDimitry Andric .msg { font-weight:bold } 4060b57cec5SDimitry Andric .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap } 4070b57cec5SDimitry Andric .msgT { padding:0x; spacing:0x } 4080b57cec5SDimitry Andric .msgEvent { background-color:#fff8b4; color:#000000 } 4090b57cec5SDimitry Andric .msgControl { background-color:#bbbbbb; color:#000000 } 4100b57cec5SDimitry Andric .msgNote { background-color:#ddeeff; color:#000000 } 4110b57cec5SDimitry Andric .mrange { background-color:#dfddf3 } 4120b57cec5SDimitry Andric .mrange { border-bottom:1px solid #6F9DBE } 4130b57cec5SDimitry Andric .PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; } 4140b57cec5SDimitry Andric .PathIndex { -webkit-border-radius:8px } 4150b57cec5SDimitry Andric .PathIndex { border-radius:8px } 4160b57cec5SDimitry Andric .PathIndexEvent { background-color:#bfba87 } 4170b57cec5SDimitry Andric .PathIndexControl { background-color:#8c8c8c } 4180b57cec5SDimitry Andric .PathIndexPopUp { background-color: #879abc; } 4190b57cec5SDimitry Andric .PathNav a { text-decoration:none; font-size: larger } 4200b57cec5SDimitry Andric .CodeInsertionHint { font-weight: bold; background-color: #10dd10 } 4210b57cec5SDimitry Andric .CodeRemovalHint { background-color:#de1010 } 4220b57cec5SDimitry Andric .CodeRemovalHint { border-bottom:1px solid #6F9DBE } 423349cc55cSDimitry Andric .msg.selected{ background-color:orange !important; } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric table.simpletable { 4260b57cec5SDimitry Andric padding: 5px; 4270b57cec5SDimitry Andric font-size:12pt; 4280b57cec5SDimitry Andric margin:20px; 4290b57cec5SDimitry Andric border-collapse: collapse; border-spacing: 0px; 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric td.rowname { 4320b57cec5SDimitry Andric text-align: right; 4330b57cec5SDimitry Andric vertical-align: top; 4340b57cec5SDimitry Andric font-weight: bold; 4350b57cec5SDimitry Andric color:#444444; 4360b57cec5SDimitry Andric padding-right:2ex; 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric /* Hidden text. */ 4400b57cec5SDimitry Andric input.spoilerhider + label { 4410b57cec5SDimitry Andric cursor: pointer; 4420b57cec5SDimitry Andric text-decoration: underline; 4430b57cec5SDimitry Andric display: block; 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric input.spoilerhider { 4460b57cec5SDimitry Andric display: none; 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric input.spoilerhider ~ .spoiler { 4490b57cec5SDimitry Andric overflow: hidden; 4500b57cec5SDimitry Andric margin: 10px auto 0; 4510b57cec5SDimitry Andric height: 0; 4520b57cec5SDimitry Andric opacity: 0; 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric input.spoilerhider:checked + label + .spoiler{ 4550b57cec5SDimitry Andric height: auto; 4560b57cec5SDimitry Andric opacity: 1; 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric </style> 4590b57cec5SDimitry Andric </head> 4600b57cec5SDimitry Andric <body>)<<<"; 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric // Generate header 4630b57cec5SDimitry Andric R.InsertTextBefore(StartLoc, os.str()); 4640b57cec5SDimitry Andric // Generate footer 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric R.InsertTextAfter(EndLoc, "</body></html>\n"); 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with 4700b57cec5SDimitry Andric /// information about keywords, macro expansions etc. This uses the macro 4710b57cec5SDimitry Andric /// table state from the end of the file, so it won't be perfectly perfect, 4720b57cec5SDimitry Andric /// but it will be reasonably close. 473*0fca6ea1SDimitry Andric static void SyntaxHighlightImpl( 474*0fca6ea1SDimitry Andric Rewriter &R, FileID FID, const Preprocessor &PP, 475*0fca6ea1SDimitry Andric llvm::function_ref<void(RewriteBuffer &, unsigned, unsigned, const char *, 476*0fca6ea1SDimitry Andric const char *, const char *)> 477*0fca6ea1SDimitry Andric HighlightRangeCallback) { 4780b57cec5SDimitry Andric 479*0fca6ea1SDimitry Andric RewriteBuffer &RB = R.getEditBuffer(FID); 4800b57cec5SDimitry Andric const SourceManager &SM = PP.getSourceManager(); 481e8d8bef9SDimitry Andric llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID); 482*0fca6ea1SDimitry Andric const char *BufferStart = FromFile.getBuffer().data(); 483*0fca6ea1SDimitry Andric 4840b57cec5SDimitry Andric Lexer L(FID, FromFile, SM, PP.getLangOpts()); 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // Inform the preprocessor that we want to retain comments as tokens, so we 4870b57cec5SDimitry Andric // can highlight them. 4880b57cec5SDimitry Andric L.SetCommentRetentionState(true); 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric // Lex all the tokens in raw mode, to avoid entering #includes or expanding 4910b57cec5SDimitry Andric // macros. 4920b57cec5SDimitry Andric Token Tok; 4930b57cec5SDimitry Andric L.LexFromRawLexer(Tok); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric while (Tok.isNot(tok::eof)) { 4960b57cec5SDimitry Andric // Since we are lexing unexpanded tokens, all tokens are from the main 4970b57cec5SDimitry Andric // FileID. 4980b57cec5SDimitry Andric unsigned TokOffs = SM.getFileOffset(Tok.getLocation()); 4990b57cec5SDimitry Andric unsigned TokLen = Tok.getLength(); 5000b57cec5SDimitry Andric switch (Tok.getKind()) { 5010b57cec5SDimitry Andric default: break; 5020b57cec5SDimitry Andric case tok::identifier: 5030b57cec5SDimitry Andric llvm_unreachable("tok::identifier in raw lexing mode!"); 5040b57cec5SDimitry Andric case tok::raw_identifier: { 5050b57cec5SDimitry Andric // Fill in Result.IdentifierInfo and update the token kind, 5060b57cec5SDimitry Andric // looking up the identifier in the identifier table. 5070b57cec5SDimitry Andric PP.LookUpIdentifierInfo(Tok); 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric // If this is a pp-identifier, for a keyword, highlight it as such. 5100b57cec5SDimitry Andric if (Tok.isNot(tok::identifier)) 511*0fca6ea1SDimitry Andric HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart, 5120b57cec5SDimitry Andric "<span class='keyword'>", "</span>"); 5130b57cec5SDimitry Andric break; 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric case tok::comment: 516*0fca6ea1SDimitry Andric HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart, 5170b57cec5SDimitry Andric "<span class='comment'>", "</span>"); 5180b57cec5SDimitry Andric break; 5190b57cec5SDimitry Andric case tok::utf8_string_literal: 5200b57cec5SDimitry Andric // Chop off the u part of u8 prefix 5210b57cec5SDimitry Andric ++TokOffs; 5220b57cec5SDimitry Andric --TokLen; 5230b57cec5SDimitry Andric // FALL THROUGH to chop the 8 524bdd1243dSDimitry Andric [[fallthrough]]; 5250b57cec5SDimitry Andric case tok::wide_string_literal: 5260b57cec5SDimitry Andric case tok::utf16_string_literal: 5270b57cec5SDimitry Andric case tok::utf32_string_literal: 5280b57cec5SDimitry Andric // Chop off the L, u, U or 8 prefix 5290b57cec5SDimitry Andric ++TokOffs; 5300b57cec5SDimitry Andric --TokLen; 531bdd1243dSDimitry Andric [[fallthrough]]; 5320b57cec5SDimitry Andric case tok::string_literal: 5330b57cec5SDimitry Andric // FIXME: Exclude the optional ud-suffix from the highlighted range. 534*0fca6ea1SDimitry Andric HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart, 5350b57cec5SDimitry Andric "<span class='string_literal'>", "</span>"); 5360b57cec5SDimitry Andric break; 5370b57cec5SDimitry Andric case tok::hash: { 5380b57cec5SDimitry Andric // If this is a preprocessor directive, all tokens to end of line are too. 5390b57cec5SDimitry Andric if (!Tok.isAtStartOfLine()) 5400b57cec5SDimitry Andric break; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Eat all of the tokens until we get to the next one at the start of 5430b57cec5SDimitry Andric // line. 5440b57cec5SDimitry Andric unsigned TokEnd = TokOffs+TokLen; 5450b57cec5SDimitry Andric L.LexFromRawLexer(Tok); 5460b57cec5SDimitry Andric while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) { 5470b57cec5SDimitry Andric TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength(); 5480b57cec5SDimitry Andric L.LexFromRawLexer(Tok); 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric // Find end of line. This is a hack. 552*0fca6ea1SDimitry Andric HighlightRangeCallback(RB, TokOffs, TokEnd, BufferStart, 5530b57cec5SDimitry Andric "<span class='directive'>", "</span>"); 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Don't skip the next token. 5560b57cec5SDimitry Andric continue; 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric L.LexFromRawLexer(Tok); 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric } 563*0fca6ea1SDimitry Andric void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP, 564*0fca6ea1SDimitry Andric RelexRewriteCacheRef Cache) { 565*0fca6ea1SDimitry Andric RewriteBuffer &RB = R.getEditBuffer(FID); 566*0fca6ea1SDimitry Andric const SourceManager &SM = PP.getSourceManager(); 567*0fca6ea1SDimitry Andric llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID); 568*0fca6ea1SDimitry Andric const char *BufferStart = FromFile.getBuffer().data(); 5690b57cec5SDimitry Andric 570*0fca6ea1SDimitry Andric if (Cache) { 571*0fca6ea1SDimitry Andric auto CacheIt = Cache->SyntaxHighlights.find(FID); 572*0fca6ea1SDimitry Andric if (CacheIt != Cache->SyntaxHighlights.end()) { 573*0fca6ea1SDimitry Andric for (const RelexRewriteCache::RawHighlight &H : CacheIt->second) { 574*0fca6ea1SDimitry Andric HighlightRange(RB, H.B, H.E, BufferStart, H.StartTag.data(), 575*0fca6ea1SDimitry Andric H.EndTag.data()); 576*0fca6ea1SDimitry Andric } 577*0fca6ea1SDimitry Andric return; 578*0fca6ea1SDimitry Andric } 579*0fca6ea1SDimitry Andric } 580*0fca6ea1SDimitry Andric 581*0fca6ea1SDimitry Andric // "Every time you would call HighlightRange, cache the inputs as well." 582*0fca6ea1SDimitry Andric auto HighlightRangeCallback = [&](RewriteBuffer &RB, unsigned B, unsigned E, 583*0fca6ea1SDimitry Andric const char *BufferStart, 584*0fca6ea1SDimitry Andric const char *StartTag, const char *EndTag) { 585*0fca6ea1SDimitry Andric HighlightRange(RB, B, E, BufferStart, StartTag, EndTag); 586*0fca6ea1SDimitry Andric 587*0fca6ea1SDimitry Andric if (Cache) 588*0fca6ea1SDimitry Andric Cache->SyntaxHighlights[FID].push_back({B, E, StartTag, EndTag}); 589*0fca6ea1SDimitry Andric }; 590*0fca6ea1SDimitry Andric 591*0fca6ea1SDimitry Andric SyntaxHighlightImpl(R, FID, PP, HighlightRangeCallback); 592*0fca6ea1SDimitry Andric } 593*0fca6ea1SDimitry Andric 594*0fca6ea1SDimitry Andric static void HighlightMacrosImpl( 595*0fca6ea1SDimitry Andric Rewriter &R, FileID FID, const Preprocessor &PP, 596*0fca6ea1SDimitry Andric llvm::function_ref<void(Rewriter &, SourceLocation, SourceLocation, 597*0fca6ea1SDimitry Andric const char *, const char *, bool)> 598*0fca6ea1SDimitry Andric HighlightRangeCallback) { 599*0fca6ea1SDimitry Andric 6000b57cec5SDimitry Andric // Re-lex the raw token stream into a token buffer. 6010b57cec5SDimitry Andric const SourceManager &SM = PP.getSourceManager(); 6020b57cec5SDimitry Andric std::vector<Token> TokenStream; 6030b57cec5SDimitry Andric 604e8d8bef9SDimitry Andric llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID); 6050b57cec5SDimitry Andric Lexer L(FID, FromFile, SM, PP.getLangOpts()); 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric // Lex all the tokens in raw mode, to avoid entering #includes or expanding 6080b57cec5SDimitry Andric // macros. 60904eeddc0SDimitry Andric while (true) { 6100b57cec5SDimitry Andric Token Tok; 6110b57cec5SDimitry Andric L.LexFromRawLexer(Tok); 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric // If this is a # at the start of a line, discard it from the token stream. 6140b57cec5SDimitry Andric // We don't want the re-preprocess step to see #defines, #includes or other 6150b57cec5SDimitry Andric // preprocessor directives. 6160b57cec5SDimitry Andric if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) 6170b57cec5SDimitry Andric continue; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric // If this is a ## token, change its kind to unknown so that repreprocessing 6200b57cec5SDimitry Andric // it will not produce an error. 6210b57cec5SDimitry Andric if (Tok.is(tok::hashhash)) 6220b57cec5SDimitry Andric Tok.setKind(tok::unknown); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric // If this raw token is an identifier, the raw lexer won't have looked up 6250b57cec5SDimitry Andric // the corresponding identifier info for it. Do this now so that it will be 6260b57cec5SDimitry Andric // macro expanded when we re-preprocess it. 6270b57cec5SDimitry Andric if (Tok.is(tok::raw_identifier)) 6280b57cec5SDimitry Andric PP.LookUpIdentifierInfo(Tok); 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric TokenStream.push_back(Tok); 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric if (Tok.is(tok::eof)) break; 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // Temporarily change the diagnostics object so that we ignore any generated 6360b57cec5SDimitry Andric // diagnostics from this pass. 6370b57cec5SDimitry Andric DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(), 6380b57cec5SDimitry Andric &PP.getDiagnostics().getDiagnosticOptions(), 6390b57cec5SDimitry Andric new IgnoringDiagConsumer); 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric // FIXME: This is a huge hack; we reuse the input preprocessor because we want 6420b57cec5SDimitry Andric // its state, but we aren't actually changing it (we hope). This should really 6430b57cec5SDimitry Andric // construct a copy of the preprocessor. 6440b57cec5SDimitry Andric Preprocessor &TmpPP = const_cast<Preprocessor&>(PP); 6450b57cec5SDimitry Andric DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics(); 6460b57cec5SDimitry Andric TmpPP.setDiagnostics(TmpDiags); 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // Inform the preprocessor that we don't want comments. 6490b57cec5SDimitry Andric TmpPP.SetCommentRetentionState(false, false); 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric // We don't want pragmas either. Although we filtered out #pragma, removing 6520b57cec5SDimitry Andric // _Pragma and __pragma is much harder. 6530b57cec5SDimitry Andric bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled(); 6540b57cec5SDimitry Andric TmpPP.setPragmasEnabled(false); 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric // Enter the tokens we just lexed. This will cause them to be macro expanded 6570b57cec5SDimitry Andric // but won't enter sub-files (because we removed #'s). 6580b57cec5SDimitry Andric TmpPP.EnterTokenStream(TokenStream, false, /*IsReinject=*/false); 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric TokenConcatenation ConcatInfo(TmpPP); 6610b57cec5SDimitry Andric 6620b57cec5SDimitry Andric // Lex all the tokens. 6630b57cec5SDimitry Andric Token Tok; 6640b57cec5SDimitry Andric TmpPP.Lex(Tok); 6650b57cec5SDimitry Andric while (Tok.isNot(tok::eof)) { 6660b57cec5SDimitry Andric // Ignore non-macro tokens. 6670b57cec5SDimitry Andric if (!Tok.getLocation().isMacroID()) { 6680b57cec5SDimitry Andric TmpPP.Lex(Tok); 6690b57cec5SDimitry Andric continue; 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric // Okay, we have the first token of a macro expansion: highlight the 6730b57cec5SDimitry Andric // expansion by inserting a start tag before the macro expansion and 6740b57cec5SDimitry Andric // end tag after it. 6750b57cec5SDimitry Andric CharSourceRange LLoc = SM.getExpansionRange(Tok.getLocation()); 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric // Ignore tokens whose instantiation location was not the main file. 6780b57cec5SDimitry Andric if (SM.getFileID(LLoc.getBegin()) != FID) { 6790b57cec5SDimitry Andric TmpPP.Lex(Tok); 6800b57cec5SDimitry Andric continue; 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric assert(SM.getFileID(LLoc.getEnd()) == FID && 6840b57cec5SDimitry Andric "Start and end of expansion must be in the same ultimate file!"); 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric std::string Expansion = EscapeText(TmpPP.getSpelling(Tok)); 6870b57cec5SDimitry Andric unsigned LineLen = Expansion.size(); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric Token PrevPrevTok; 6900b57cec5SDimitry Andric Token PrevTok = Tok; 6910b57cec5SDimitry Andric // Okay, eat this token, getting the next one. 6920b57cec5SDimitry Andric TmpPP.Lex(Tok); 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric // Skip all the rest of the tokens that are part of this macro 6950b57cec5SDimitry Andric // instantiation. It would be really nice to pop up a window with all the 6960b57cec5SDimitry Andric // spelling of the tokens or something. 6970b57cec5SDimitry Andric while (!Tok.is(tok::eof) && 6980b57cec5SDimitry Andric SM.getExpansionLoc(Tok.getLocation()) == LLoc.getBegin()) { 6990b57cec5SDimitry Andric // Insert a newline if the macro expansion is getting large. 7000b57cec5SDimitry Andric if (LineLen > 60) { 7010b57cec5SDimitry Andric Expansion += "<br>"; 7020b57cec5SDimitry Andric LineLen = 0; 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric LineLen -= Expansion.size(); 7060b57cec5SDimitry Andric 7070b57cec5SDimitry Andric // If the tokens were already space separated, or if they must be to avoid 7080b57cec5SDimitry Andric // them being implicitly pasted, add a space between them. 7090b57cec5SDimitry Andric if (Tok.hasLeadingSpace() || 7100b57cec5SDimitry Andric ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok)) 7110b57cec5SDimitry Andric Expansion += ' '; 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric // Escape any special characters in the token text. 7140b57cec5SDimitry Andric Expansion += EscapeText(TmpPP.getSpelling(Tok)); 7150b57cec5SDimitry Andric LineLen += Expansion.size(); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric PrevPrevTok = PrevTok; 7180b57cec5SDimitry Andric PrevTok = Tok; 7190b57cec5SDimitry Andric TmpPP.Lex(Tok); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric // Insert the 'macro_popup' as the end tag, so that multi-line macros all 7230b57cec5SDimitry Andric // get highlighted. 7240b57cec5SDimitry Andric Expansion = "<span class='macro_popup'>" + Expansion + "</span></span>"; 7250b57cec5SDimitry Andric 726*0fca6ea1SDimitry Andric HighlightRangeCallback(R, LLoc.getBegin(), LLoc.getEnd(), 727*0fca6ea1SDimitry Andric "<span class='macro'>", Expansion.c_str(), 728*0fca6ea1SDimitry Andric LLoc.isTokenRange()); 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric // Restore the preprocessor's old state. 7320b57cec5SDimitry Andric TmpPP.setDiagnostics(*OldDiags); 7330b57cec5SDimitry Andric TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled); 7340b57cec5SDimitry Andric } 735*0fca6ea1SDimitry Andric 736*0fca6ea1SDimitry Andric /// HighlightMacros - This uses the macro table state from the end of the 737*0fca6ea1SDimitry Andric /// file, to re-expand macros and insert (into the HTML) information about the 738*0fca6ea1SDimitry Andric /// macro expansions. This won't be perfectly perfect, but it will be 739*0fca6ea1SDimitry Andric /// reasonably close. 740*0fca6ea1SDimitry Andric void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP, 741*0fca6ea1SDimitry Andric RelexRewriteCacheRef Cache) { 742*0fca6ea1SDimitry Andric if (Cache) { 743*0fca6ea1SDimitry Andric auto CacheIt = Cache->MacroHighlights.find(FID); 744*0fca6ea1SDimitry Andric if (CacheIt != Cache->MacroHighlights.end()) { 745*0fca6ea1SDimitry Andric for (const RelexRewriteCache::Highlight &H : CacheIt->second) { 746*0fca6ea1SDimitry Andric HighlightRange(R, H.B, H.E, H.StartTag.data(), H.EndTag.data(), 747*0fca6ea1SDimitry Andric H.IsTokenRange); 748*0fca6ea1SDimitry Andric } 749*0fca6ea1SDimitry Andric return; 750*0fca6ea1SDimitry Andric } 751*0fca6ea1SDimitry Andric } 752*0fca6ea1SDimitry Andric 753*0fca6ea1SDimitry Andric // "Every time you would call HighlightRange, cache the inputs as well." 754*0fca6ea1SDimitry Andric auto HighlightRangeCallback = [&](Rewriter &R, SourceLocation B, 755*0fca6ea1SDimitry Andric SourceLocation E, const char *StartTag, 756*0fca6ea1SDimitry Andric const char *EndTag, bool isTokenRange) { 757*0fca6ea1SDimitry Andric HighlightRange(R, B, E, StartTag, EndTag, isTokenRange); 758*0fca6ea1SDimitry Andric 759*0fca6ea1SDimitry Andric if (Cache) { 760*0fca6ea1SDimitry Andric Cache->MacroHighlights[FID].push_back( 761*0fca6ea1SDimitry Andric {B, E, StartTag, EndTag, isTokenRange}); 762*0fca6ea1SDimitry Andric } 763*0fca6ea1SDimitry Andric }; 764*0fca6ea1SDimitry Andric 765*0fca6ea1SDimitry Andric HighlightMacrosImpl(R, FID, PP, HighlightRangeCallback); 766*0fca6ea1SDimitry Andric } 767