xref: /freebsd/contrib/llvm-project/clang/lib/Rewrite/HTMLRewrite.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 using namespace clang;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric /// HighlightRange - Highlight a range in the source code with the specified
280b57cec5SDimitry Andric /// start/end tags.  B/E must be in the same file.  This ensures that
290b57cec5SDimitry Andric /// start/end tags are placed at the start/end of each line if the range is
300b57cec5SDimitry Andric /// multiline.
310b57cec5SDimitry Andric void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
320b57cec5SDimitry Andric                           const char *StartTag, const char *EndTag,
330b57cec5SDimitry Andric                           bool IsTokenRange) {
340b57cec5SDimitry Andric   SourceManager &SM = R.getSourceMgr();
350b57cec5SDimitry Andric   B = SM.getExpansionLoc(B);
360b57cec5SDimitry Andric   E = SM.getExpansionLoc(E);
370b57cec5SDimitry Andric   FileID FID = SM.getFileID(B);
380b57cec5SDimitry Andric   assert(SM.getFileID(E) == FID && "B/E not in the same file!");
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   unsigned BOffset = SM.getFileOffset(B);
410b57cec5SDimitry Andric   unsigned EOffset = SM.getFileOffset(E);
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   // Include the whole end token in the range.
440b57cec5SDimitry Andric   if (IsTokenRange)
450b57cec5SDimitry Andric     EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   bool Invalid = false;
480b57cec5SDimitry Andric   const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
490b57cec5SDimitry Andric   if (Invalid)
500b57cec5SDimitry Andric     return;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
530b57cec5SDimitry Andric                  BufferStart, StartTag, EndTag);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric /// HighlightRange - This is the same as the above method, but takes
570b57cec5SDimitry Andric /// decomposed file locations.
580b57cec5SDimitry Andric void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
590b57cec5SDimitry Andric                           const char *BufferStart,
600b57cec5SDimitry Andric                           const char *StartTag, const char *EndTag) {
610b57cec5SDimitry Andric   // Insert the tag at the absolute start/end of the range.
620b57cec5SDimitry Andric   RB.InsertTextAfter(B, StartTag);
630b57cec5SDimitry Andric   RB.InsertTextBefore(E, EndTag);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // Scan the range to see if there is a \r or \n.  If so, and if the line is
660b57cec5SDimitry Andric   // not blank, insert tags on that line as well.
670b57cec5SDimitry Andric   bool HadOpenTag = true;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   unsigned LastNonWhiteSpace = B;
700b57cec5SDimitry Andric   for (unsigned i = B; i != E; ++i) {
710b57cec5SDimitry Andric     switch (BufferStart[i]) {
720b57cec5SDimitry Andric     case '\r':
730b57cec5SDimitry Andric     case '\n':
740b57cec5SDimitry Andric       // Okay, we found a newline in the range.  If we have an open tag, we need
750b57cec5SDimitry Andric       // to insert a close tag at the first non-whitespace before the newline.
760b57cec5SDimitry Andric       if (HadOpenTag)
770b57cec5SDimitry Andric         RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric       // Instead of inserting an open tag immediately after the newline, we
800b57cec5SDimitry Andric       // wait until we see a non-whitespace character.  This prevents us from
810b57cec5SDimitry Andric       // inserting tags around blank lines, and also allows the open tag to
820b57cec5SDimitry Andric       // be put *after* whitespace on a non-blank line.
830b57cec5SDimitry Andric       HadOpenTag = false;
840b57cec5SDimitry Andric       break;
850b57cec5SDimitry Andric     case '\0':
860b57cec5SDimitry Andric     case ' ':
870b57cec5SDimitry Andric     case '\t':
880b57cec5SDimitry Andric     case '\f':
890b57cec5SDimitry Andric     case '\v':
900b57cec5SDimitry Andric       // Ignore whitespace.
910b57cec5SDimitry Andric       break;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     default:
940b57cec5SDimitry Andric       // If there is no tag open, do it now.
950b57cec5SDimitry Andric       if (!HadOpenTag) {
960b57cec5SDimitry Andric         RB.InsertTextAfter(i, StartTag);
970b57cec5SDimitry Andric         HadOpenTag = true;
980b57cec5SDimitry Andric       }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric       // Remember this character.
1010b57cec5SDimitry Andric       LastNonWhiteSpace = i;
1020b57cec5SDimitry Andric       break;
1030b57cec5SDimitry Andric     }
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric void html::EscapeText(Rewriter &R, FileID FID,
1080b57cec5SDimitry Andric                       bool EscapeSpaces, bool ReplaceTabs) {
1090b57cec5SDimitry Andric 
110e8d8bef9SDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
111e8d8bef9SDimitry Andric   const char* C = Buf.getBufferStart();
112e8d8bef9SDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   assert (C <= FileEnd);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   RewriteBuffer &RB = R.getEditBuffer(FID);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   unsigned ColNo = 0;
1190b57cec5SDimitry Andric   for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
1200b57cec5SDimitry Andric     switch (*C) {
1210b57cec5SDimitry Andric     default: ++ColNo; break;
1220b57cec5SDimitry Andric     case '\n':
1230b57cec5SDimitry Andric     case '\r':
1240b57cec5SDimitry Andric       ColNo = 0;
1250b57cec5SDimitry Andric       break;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric     case ' ':
1280b57cec5SDimitry Andric       if (EscapeSpaces)
1290b57cec5SDimitry Andric         RB.ReplaceText(FilePos, 1, "&nbsp;");
1300b57cec5SDimitry Andric       ++ColNo;
1310b57cec5SDimitry Andric       break;
1320b57cec5SDimitry Andric     case '\f':
1330b57cec5SDimitry Andric       RB.ReplaceText(FilePos, 1, "<hr>");
1340b57cec5SDimitry Andric       ColNo = 0;
1350b57cec5SDimitry Andric       break;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric     case '\t': {
1380b57cec5SDimitry Andric       if (!ReplaceTabs)
1390b57cec5SDimitry Andric         break;
1400b57cec5SDimitry Andric       unsigned NumSpaces = 8-(ColNo&7);
1410b57cec5SDimitry Andric       if (EscapeSpaces)
1420b57cec5SDimitry Andric         RB.ReplaceText(FilePos, 1,
1430b57cec5SDimitry Andric                        StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
1440b57cec5SDimitry Andric                                        "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
1450b57cec5SDimitry Andric       else
1460b57cec5SDimitry Andric         RB.ReplaceText(FilePos, 1, StringRef("        ", NumSpaces));
1470b57cec5SDimitry Andric       ColNo += NumSpaces;
1480b57cec5SDimitry Andric       break;
1490b57cec5SDimitry Andric     }
1500b57cec5SDimitry Andric     case '<':
1510b57cec5SDimitry Andric       RB.ReplaceText(FilePos, 1, "&lt;");
1520b57cec5SDimitry Andric       ++ColNo;
1530b57cec5SDimitry Andric       break;
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     case '>':
1560b57cec5SDimitry Andric       RB.ReplaceText(FilePos, 1, "&gt;");
1570b57cec5SDimitry Andric       ++ColNo;
1580b57cec5SDimitry Andric       break;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric     case '&':
1610b57cec5SDimitry Andric       RB.ReplaceText(FilePos, 1, "&amp;");
1620b57cec5SDimitry Andric       ++ColNo;
1630b57cec5SDimitry Andric       break;
1640b57cec5SDimitry Andric     }
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   unsigned len = s.size();
1710b57cec5SDimitry Andric   std::string Str;
1720b57cec5SDimitry Andric   llvm::raw_string_ostream os(Str);
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   for (unsigned i = 0 ; i < len; ++i) {
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     char c = s[i];
1770b57cec5SDimitry Andric     switch (c) {
1780b57cec5SDimitry Andric     default:
1790b57cec5SDimitry Andric       os << c; break;
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric     case ' ':
1820b57cec5SDimitry Andric       if (EscapeSpaces) os << "&nbsp;";
1830b57cec5SDimitry Andric       else os << ' ';
1840b57cec5SDimitry Andric       break;
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric     case '\t':
1870b57cec5SDimitry Andric       if (ReplaceTabs) {
1880b57cec5SDimitry Andric         if (EscapeSpaces)
1890b57cec5SDimitry Andric           for (unsigned i = 0; i < 4; ++i)
1900b57cec5SDimitry Andric             os << "&nbsp;";
1910b57cec5SDimitry Andric         else
1920b57cec5SDimitry Andric           for (unsigned i = 0; i < 4; ++i)
1930b57cec5SDimitry Andric             os << " ";
1940b57cec5SDimitry Andric       }
1950b57cec5SDimitry Andric       else
1960b57cec5SDimitry Andric         os << c;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric       break;
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     case '<': os << "&lt;"; break;
2010b57cec5SDimitry Andric     case '>': os << "&gt;"; break;
2020b57cec5SDimitry Andric     case '&': os << "&amp;"; break;
2030b57cec5SDimitry Andric     }
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
2060eae32dcSDimitry Andric   return Str;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
2100b57cec5SDimitry Andric                           unsigned B, unsigned E) {
2110b57cec5SDimitry Andric   SmallString<256> Str;
2120b57cec5SDimitry Andric   llvm::raw_svector_ostream OS(Str);
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   OS << "<tr class=\"codeline\" data-linenumber=\"" << LineNo << "\">"
2150b57cec5SDimitry Andric      << "<td class=\"num\" id=\"LN" << LineNo << "\">" << LineNo
2160b57cec5SDimitry Andric      << "</td><td class=\"line\">";
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   if (B == E) { // Handle empty lines.
2190b57cec5SDimitry Andric     OS << " </td></tr>";
2200b57cec5SDimitry Andric     RB.InsertTextBefore(B, OS.str());
2210b57cec5SDimitry Andric   } else {
2220b57cec5SDimitry Andric     RB.InsertTextBefore(B, OS.str());
2230b57cec5SDimitry Andric     RB.InsertTextBefore(E, "</td></tr>");
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric void html::AddLineNumbers(Rewriter& R, FileID FID) {
2280b57cec5SDimitry Andric 
229e8d8bef9SDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
230e8d8bef9SDimitry Andric   const char* FileBeg = Buf.getBufferStart();
231e8d8bef9SDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
2320b57cec5SDimitry Andric   const char* C = FileBeg;
2330b57cec5SDimitry Andric   RewriteBuffer &RB = R.getEditBuffer(FID);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   assert (C <= FileEnd);
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   unsigned LineNo = 0;
2380b57cec5SDimitry Andric   unsigned FilePos = 0;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   while (C != FileEnd) {
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     ++LineNo;
2430b57cec5SDimitry Andric     unsigned LineStartPos = FilePos;
2440b57cec5SDimitry Andric     unsigned LineEndPos = FileEnd - FileBeg;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric     assert (FilePos <= LineEndPos);
2470b57cec5SDimitry Andric     assert (C < FileEnd);
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric     // Scan until the newline (or end-of-file).
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric     while (C != FileEnd) {
2520b57cec5SDimitry Andric       char c = *C;
2530b57cec5SDimitry Andric       ++C;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric       if (c == '\n') {
2560b57cec5SDimitry Andric         LineEndPos = FilePos++;
2570b57cec5SDimitry Andric         break;
2580b57cec5SDimitry Andric       }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric       ++FilePos;
2610b57cec5SDimitry Andric     }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric     AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
2640b57cec5SDimitry Andric   }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // Add one big table tag that surrounds all of the code.
2670b57cec5SDimitry Andric   std::string s;
2680b57cec5SDimitry Andric   llvm::raw_string_ostream os(s);
2690b57cec5SDimitry Andric   os << "<table class=\"code\" data-fileid=\"" << FID.getHashValue() << "\">\n";
2700b57cec5SDimitry Andric   RB.InsertTextBefore(0, os.str());
2710b57cec5SDimitry Andric   RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric void html::AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID,
2750b57cec5SDimitry Andric                                              StringRef title) {
2760b57cec5SDimitry Andric 
277e8d8bef9SDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
278e8d8bef9SDimitry Andric   const char* FileStart = Buf.getBufferStart();
279e8d8bef9SDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
2820b57cec5SDimitry Andric   SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   std::string s;
2850b57cec5SDimitry Andric   llvm::raw_string_ostream os(s);
2860b57cec5SDimitry Andric   os << "<!doctype html>\n" // Use HTML 5 doctype
2870b57cec5SDimitry Andric         "<html>\n<head>\n";
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   if (!title.empty())
2900b57cec5SDimitry Andric     os << "<title>" << html::EscapeText(title) << "</title>\n";
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   os << R"<<<(
2930b57cec5SDimitry Andric <style type="text/css">
2940b57cec5SDimitry Andric body { color:#000000; background-color:#ffffff }
2950b57cec5SDimitry Andric body { font-family:Helvetica, sans-serif; font-size:10pt }
2960b57cec5SDimitry Andric h1 { font-size:14pt }
2970b57cec5SDimitry Andric .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }
2980b57cec5SDimitry Andric .FileNav { margin-left: 5px; margin-right: 5px; display: inline; }
2990b57cec5SDimitry Andric .FileNav a { text-decoration:none; font-size: larger; }
3000b57cec5SDimitry Andric .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }
3010b57cec5SDimitry Andric .divider { background-color: gray; }
3020b57cec5SDimitry Andric .code { border-collapse:collapse; width:100%; }
3030b57cec5SDimitry Andric .code { font-family: "Monospace", monospace; font-size:10pt }
3040b57cec5SDimitry Andric .code { line-height: 1.2em }
3050b57cec5SDimitry Andric .comment { color: green; font-style: oblique }
3060b57cec5SDimitry Andric .keyword { color: blue }
3070b57cec5SDimitry Andric .string_literal { color: red }
3080b57cec5SDimitry Andric .directive { color: darkmagenta }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric /* Macros and variables could have pop-up notes hidden by default.
3110b57cec5SDimitry Andric   - Macro pop-up:    expansion of the macro
3120b57cec5SDimitry Andric   - Variable pop-up: value (table) of the variable */
3130b57cec5SDimitry Andric .macro_popup, .variable_popup { display: none; }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric /* Pop-up appears on mouse-hover event. */
3160b57cec5SDimitry Andric .macro:hover .macro_popup, .variable:hover .variable_popup {
3170b57cec5SDimitry Andric   display: block;
3180b57cec5SDimitry Andric   padding: 2px;
3190b57cec5SDimitry Andric   -webkit-border-radius:5px;
3200b57cec5SDimitry Andric   -webkit-box-shadow:1px 1px 7px #000;
3210b57cec5SDimitry Andric   border-radius:5px;
3220b57cec5SDimitry Andric   box-shadow:1px 1px 7px #000;
3230b57cec5SDimitry Andric   position: absolute;
3240b57cec5SDimitry Andric   top: -1em;
3250b57cec5SDimitry Andric   left:10em;
3260b57cec5SDimitry Andric   z-index: 1
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric .macro_popup {
3300b57cec5SDimitry Andric   border: 2px solid red;
3310b57cec5SDimitry Andric   background-color:#FFF0F0;
3320b57cec5SDimitry Andric   font-weight: normal;
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric .variable_popup {
3360b57cec5SDimitry Andric   border: 2px solid blue;
3370b57cec5SDimitry Andric   background-color:#F0F0FF;
3380b57cec5SDimitry Andric   font-weight: bold;
3390b57cec5SDimitry Andric   font-family: Helvetica, sans-serif;
3400b57cec5SDimitry Andric   font-size: 9pt;
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric /* Pop-up notes needs a relative position as a base where they pops up. */
3440b57cec5SDimitry Andric .macro, .variable {
3450b57cec5SDimitry Andric   background-color: PaleGoldenRod;
3460b57cec5SDimitry Andric   position: relative;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric .macro { color: DarkMagenta; }
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric #tooltiphint {
3510b57cec5SDimitry Andric   position: fixed;
3520b57cec5SDimitry Andric   width: 50em;
3530b57cec5SDimitry Andric   margin-left: -25em;
3540b57cec5SDimitry Andric   left: 50%;
3550b57cec5SDimitry Andric   padding: 10px;
3560b57cec5SDimitry Andric   border: 1px solid #b0b0b0;
3570b57cec5SDimitry Andric   border-radius: 2px;
3580b57cec5SDimitry Andric   box-shadow: 1px 1px 7px black;
3590b57cec5SDimitry Andric   background-color: #c0c0c0;
3600b57cec5SDimitry Andric   z-index: 2;
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
3640b57cec5SDimitry Andric .num { text-align:right; font-size:8pt }
3650b57cec5SDimitry Andric .num { color:#444444 }
3660b57cec5SDimitry Andric .line { padding-left: 1ex; border-left: 3px solid #ccc }
3670b57cec5SDimitry Andric .line { white-space: pre }
3680b57cec5SDimitry Andric .msg { -webkit-box-shadow:1px 1px 7px #000 }
3690b57cec5SDimitry Andric .msg { box-shadow:1px 1px 7px #000 }
3700b57cec5SDimitry Andric .msg { -webkit-border-radius:5px }
3710b57cec5SDimitry Andric .msg { border-radius:5px }
3720b57cec5SDimitry Andric .msg { font-family:Helvetica, sans-serif; font-size:8pt }
3730b57cec5SDimitry Andric .msg { float:left }
374349cc55cSDimitry Andric .msg { position:relative }
3750b57cec5SDimitry Andric .msg { padding:0.25em 1ex 0.25em 1ex }
3760b57cec5SDimitry Andric .msg { margin-top:10px; margin-bottom:10px }
3770b57cec5SDimitry Andric .msg { font-weight:bold }
3780b57cec5SDimitry Andric .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
3790b57cec5SDimitry Andric .msgT { padding:0x; spacing:0x }
3800b57cec5SDimitry Andric .msgEvent { background-color:#fff8b4; color:#000000 }
3810b57cec5SDimitry Andric .msgControl { background-color:#bbbbbb; color:#000000 }
3820b57cec5SDimitry Andric .msgNote { background-color:#ddeeff; color:#000000 }
3830b57cec5SDimitry Andric .mrange { background-color:#dfddf3 }
3840b57cec5SDimitry Andric .mrange { border-bottom:1px solid #6F9DBE }
3850b57cec5SDimitry Andric .PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
3860b57cec5SDimitry Andric .PathIndex { -webkit-border-radius:8px }
3870b57cec5SDimitry Andric .PathIndex { border-radius:8px }
3880b57cec5SDimitry Andric .PathIndexEvent { background-color:#bfba87 }
3890b57cec5SDimitry Andric .PathIndexControl { background-color:#8c8c8c }
3900b57cec5SDimitry Andric .PathIndexPopUp { background-color: #879abc; }
3910b57cec5SDimitry Andric .PathNav a { text-decoration:none; font-size: larger }
3920b57cec5SDimitry Andric .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
3930b57cec5SDimitry Andric .CodeRemovalHint { background-color:#de1010 }
3940b57cec5SDimitry Andric .CodeRemovalHint { border-bottom:1px solid #6F9DBE }
395349cc55cSDimitry Andric .msg.selected{ background-color:orange !important; }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric table.simpletable {
3980b57cec5SDimitry Andric   padding: 5px;
3990b57cec5SDimitry Andric   font-size:12pt;
4000b57cec5SDimitry Andric   margin:20px;
4010b57cec5SDimitry Andric   border-collapse: collapse; border-spacing: 0px;
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric td.rowname {
4040b57cec5SDimitry Andric   text-align: right;
4050b57cec5SDimitry Andric   vertical-align: top;
4060b57cec5SDimitry Andric   font-weight: bold;
4070b57cec5SDimitry Andric   color:#444444;
4080b57cec5SDimitry Andric   padding-right:2ex;
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric /* Hidden text. */
4120b57cec5SDimitry Andric input.spoilerhider + label {
4130b57cec5SDimitry Andric   cursor: pointer;
4140b57cec5SDimitry Andric   text-decoration: underline;
4150b57cec5SDimitry Andric   display: block;
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric input.spoilerhider {
4180b57cec5SDimitry Andric  display: none;
4190b57cec5SDimitry Andric }
4200b57cec5SDimitry Andric input.spoilerhider ~ .spoiler {
4210b57cec5SDimitry Andric   overflow: hidden;
4220b57cec5SDimitry Andric   margin: 10px auto 0;
4230b57cec5SDimitry Andric   height: 0;
4240b57cec5SDimitry Andric   opacity: 0;
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric input.spoilerhider:checked + label + .spoiler{
4270b57cec5SDimitry Andric   height: auto;
4280b57cec5SDimitry Andric   opacity: 1;
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric </style>
4310b57cec5SDimitry Andric </head>
4320b57cec5SDimitry Andric <body>)<<<";
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   // Generate header
4350b57cec5SDimitry Andric   R.InsertTextBefore(StartLoc, os.str());
4360b57cec5SDimitry Andric   // Generate footer
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   R.InsertTextAfter(EndLoc, "</body></html>\n");
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
4420b57cec5SDimitry Andric /// information about keywords, macro expansions etc.  This uses the macro
4430b57cec5SDimitry Andric /// table state from the end of the file, so it won't be perfectly perfect,
4440b57cec5SDimitry Andric /// but it will be reasonably close.
4450b57cec5SDimitry Andric void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
4460b57cec5SDimitry Andric   RewriteBuffer &RB = R.getEditBuffer(FID);
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   const SourceManager &SM = PP.getSourceManager();
449e8d8bef9SDimitry Andric   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);
4500b57cec5SDimitry Andric   Lexer L(FID, FromFile, SM, PP.getLangOpts());
4510b57cec5SDimitry Andric   const char *BufferStart = L.getBuffer().data();
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   // Inform the preprocessor that we want to retain comments as tokens, so we
4540b57cec5SDimitry Andric   // can highlight them.
4550b57cec5SDimitry Andric   L.SetCommentRetentionState(true);
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
4580b57cec5SDimitry Andric   // macros.
4590b57cec5SDimitry Andric   Token Tok;
4600b57cec5SDimitry Andric   L.LexFromRawLexer(Tok);
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric   while (Tok.isNot(tok::eof)) {
4630b57cec5SDimitry Andric     // Since we are lexing unexpanded tokens, all tokens are from the main
4640b57cec5SDimitry Andric     // FileID.
4650b57cec5SDimitry Andric     unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
4660b57cec5SDimitry Andric     unsigned TokLen = Tok.getLength();
4670b57cec5SDimitry Andric     switch (Tok.getKind()) {
4680b57cec5SDimitry Andric     default: break;
4690b57cec5SDimitry Andric     case tok::identifier:
4700b57cec5SDimitry Andric       llvm_unreachable("tok::identifier in raw lexing mode!");
4710b57cec5SDimitry Andric     case tok::raw_identifier: {
4720b57cec5SDimitry Andric       // Fill in Result.IdentifierInfo and update the token kind,
4730b57cec5SDimitry Andric       // looking up the identifier in the identifier table.
4740b57cec5SDimitry Andric       PP.LookUpIdentifierInfo(Tok);
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric       // If this is a pp-identifier, for a keyword, highlight it as such.
4770b57cec5SDimitry Andric       if (Tok.isNot(tok::identifier))
4780b57cec5SDimitry Andric         HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
4790b57cec5SDimitry Andric                        "<span class='keyword'>", "</span>");
4800b57cec5SDimitry Andric       break;
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric     case tok::comment:
4830b57cec5SDimitry Andric       HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
4840b57cec5SDimitry Andric                      "<span class='comment'>", "</span>");
4850b57cec5SDimitry Andric       break;
4860b57cec5SDimitry Andric     case tok::utf8_string_literal:
4870b57cec5SDimitry Andric       // Chop off the u part of u8 prefix
4880b57cec5SDimitry Andric       ++TokOffs;
4890b57cec5SDimitry Andric       --TokLen;
4900b57cec5SDimitry Andric       // FALL THROUGH to chop the 8
491*bdd1243dSDimitry Andric       [[fallthrough]];
4920b57cec5SDimitry Andric     case tok::wide_string_literal:
4930b57cec5SDimitry Andric     case tok::utf16_string_literal:
4940b57cec5SDimitry Andric     case tok::utf32_string_literal:
4950b57cec5SDimitry Andric       // Chop off the L, u, U or 8 prefix
4960b57cec5SDimitry Andric       ++TokOffs;
4970b57cec5SDimitry Andric       --TokLen;
498*bdd1243dSDimitry Andric       [[fallthrough]];
4990b57cec5SDimitry Andric     case tok::string_literal:
5000b57cec5SDimitry Andric       // FIXME: Exclude the optional ud-suffix from the highlighted range.
5010b57cec5SDimitry Andric       HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
5020b57cec5SDimitry Andric                      "<span class='string_literal'>", "</span>");
5030b57cec5SDimitry Andric       break;
5040b57cec5SDimitry Andric     case tok::hash: {
5050b57cec5SDimitry Andric       // If this is a preprocessor directive, all tokens to end of line are too.
5060b57cec5SDimitry Andric       if (!Tok.isAtStartOfLine())
5070b57cec5SDimitry Andric         break;
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric       // Eat all of the tokens until we get to the next one at the start of
5100b57cec5SDimitry Andric       // line.
5110b57cec5SDimitry Andric       unsigned TokEnd = TokOffs+TokLen;
5120b57cec5SDimitry Andric       L.LexFromRawLexer(Tok);
5130b57cec5SDimitry Andric       while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
5140b57cec5SDimitry Andric         TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
5150b57cec5SDimitry Andric         L.LexFromRawLexer(Tok);
5160b57cec5SDimitry Andric       }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric       // Find end of line.  This is a hack.
5190b57cec5SDimitry Andric       HighlightRange(RB, TokOffs, TokEnd, BufferStart,
5200b57cec5SDimitry Andric                      "<span class='directive'>", "</span>");
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric       // Don't skip the next token.
5230b57cec5SDimitry Andric       continue;
5240b57cec5SDimitry Andric     }
5250b57cec5SDimitry Andric     }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric     L.LexFromRawLexer(Tok);
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric /// HighlightMacros - This uses the macro table state from the end of the
5320b57cec5SDimitry Andric /// file, to re-expand macros and insert (into the HTML) information about the
5330b57cec5SDimitry Andric /// macro expansions.  This won't be perfectly perfect, but it will be
5340b57cec5SDimitry Andric /// reasonably close.
5350b57cec5SDimitry Andric void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
5360b57cec5SDimitry Andric   // Re-lex the raw token stream into a token buffer.
5370b57cec5SDimitry Andric   const SourceManager &SM = PP.getSourceManager();
5380b57cec5SDimitry Andric   std::vector<Token> TokenStream;
5390b57cec5SDimitry Andric 
540e8d8bef9SDimitry Andric   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);
5410b57cec5SDimitry Andric   Lexer L(FID, FromFile, SM, PP.getLangOpts());
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
5440b57cec5SDimitry Andric   // macros.
54504eeddc0SDimitry Andric   while (true) {
5460b57cec5SDimitry Andric     Token Tok;
5470b57cec5SDimitry Andric     L.LexFromRawLexer(Tok);
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric     // If this is a # at the start of a line, discard it from the token stream.
5500b57cec5SDimitry Andric     // We don't want the re-preprocess step to see #defines, #includes or other
5510b57cec5SDimitry Andric     // preprocessor directives.
5520b57cec5SDimitry Andric     if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
5530b57cec5SDimitry Andric       continue;
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric     // If this is a ## token, change its kind to unknown so that repreprocessing
5560b57cec5SDimitry Andric     // it will not produce an error.
5570b57cec5SDimitry Andric     if (Tok.is(tok::hashhash))
5580b57cec5SDimitry Andric       Tok.setKind(tok::unknown);
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric     // If this raw token is an identifier, the raw lexer won't have looked up
5610b57cec5SDimitry Andric     // the corresponding identifier info for it.  Do this now so that it will be
5620b57cec5SDimitry Andric     // macro expanded when we re-preprocess it.
5630b57cec5SDimitry Andric     if (Tok.is(tok::raw_identifier))
5640b57cec5SDimitry Andric       PP.LookUpIdentifierInfo(Tok);
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric     TokenStream.push_back(Tok);
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric     if (Tok.is(tok::eof)) break;
5690b57cec5SDimitry Andric   }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   // Temporarily change the diagnostics object so that we ignore any generated
5720b57cec5SDimitry Andric   // diagnostics from this pass.
5730b57cec5SDimitry Andric   DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
5740b57cec5SDimitry Andric                              &PP.getDiagnostics().getDiagnosticOptions(),
5750b57cec5SDimitry Andric                       new IgnoringDiagConsumer);
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   // FIXME: This is a huge hack; we reuse the input preprocessor because we want
5780b57cec5SDimitry Andric   // its state, but we aren't actually changing it (we hope). This should really
5790b57cec5SDimitry Andric   // construct a copy of the preprocessor.
5800b57cec5SDimitry Andric   Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
5810b57cec5SDimitry Andric   DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
5820b57cec5SDimitry Andric   TmpPP.setDiagnostics(TmpDiags);
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   // Inform the preprocessor that we don't want comments.
5850b57cec5SDimitry Andric   TmpPP.SetCommentRetentionState(false, false);
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   // We don't want pragmas either. Although we filtered out #pragma, removing
5880b57cec5SDimitry Andric   // _Pragma and __pragma is much harder.
5890b57cec5SDimitry Andric   bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
5900b57cec5SDimitry Andric   TmpPP.setPragmasEnabled(false);
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   // Enter the tokens we just lexed.  This will cause them to be macro expanded
5930b57cec5SDimitry Andric   // but won't enter sub-files (because we removed #'s).
5940b57cec5SDimitry Andric   TmpPP.EnterTokenStream(TokenStream, false, /*IsReinject=*/false);
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   TokenConcatenation ConcatInfo(TmpPP);
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   // Lex all the tokens.
5990b57cec5SDimitry Andric   Token Tok;
6000b57cec5SDimitry Andric   TmpPP.Lex(Tok);
6010b57cec5SDimitry Andric   while (Tok.isNot(tok::eof)) {
6020b57cec5SDimitry Andric     // Ignore non-macro tokens.
6030b57cec5SDimitry Andric     if (!Tok.getLocation().isMacroID()) {
6040b57cec5SDimitry Andric       TmpPP.Lex(Tok);
6050b57cec5SDimitry Andric       continue;
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     // Okay, we have the first token of a macro expansion: highlight the
6090b57cec5SDimitry Andric     // expansion by inserting a start tag before the macro expansion and
6100b57cec5SDimitry Andric     // end tag after it.
6110b57cec5SDimitry Andric     CharSourceRange LLoc = SM.getExpansionRange(Tok.getLocation());
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     // Ignore tokens whose instantiation location was not the main file.
6140b57cec5SDimitry Andric     if (SM.getFileID(LLoc.getBegin()) != FID) {
6150b57cec5SDimitry Andric       TmpPP.Lex(Tok);
6160b57cec5SDimitry Andric       continue;
6170b57cec5SDimitry Andric     }
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric     assert(SM.getFileID(LLoc.getEnd()) == FID &&
6200b57cec5SDimitry Andric            "Start and end of expansion must be in the same ultimate file!");
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric     std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
6230b57cec5SDimitry Andric     unsigned LineLen = Expansion.size();
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric     Token PrevPrevTok;
6260b57cec5SDimitry Andric     Token PrevTok = Tok;
6270b57cec5SDimitry Andric     // Okay, eat this token, getting the next one.
6280b57cec5SDimitry Andric     TmpPP.Lex(Tok);
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric     // Skip all the rest of the tokens that are part of this macro
6310b57cec5SDimitry Andric     // instantiation.  It would be really nice to pop up a window with all the
6320b57cec5SDimitry Andric     // spelling of the tokens or something.
6330b57cec5SDimitry Andric     while (!Tok.is(tok::eof) &&
6340b57cec5SDimitry Andric            SM.getExpansionLoc(Tok.getLocation()) == LLoc.getBegin()) {
6350b57cec5SDimitry Andric       // Insert a newline if the macro expansion is getting large.
6360b57cec5SDimitry Andric       if (LineLen > 60) {
6370b57cec5SDimitry Andric         Expansion += "<br>";
6380b57cec5SDimitry Andric         LineLen = 0;
6390b57cec5SDimitry Andric       }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric       LineLen -= Expansion.size();
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric       // If the tokens were already space separated, or if they must be to avoid
6440b57cec5SDimitry Andric       // them being implicitly pasted, add a space between them.
6450b57cec5SDimitry Andric       if (Tok.hasLeadingSpace() ||
6460b57cec5SDimitry Andric           ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
6470b57cec5SDimitry Andric         Expansion += ' ';
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric       // Escape any special characters in the token text.
6500b57cec5SDimitry Andric       Expansion += EscapeText(TmpPP.getSpelling(Tok));
6510b57cec5SDimitry Andric       LineLen += Expansion.size();
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric       PrevPrevTok = PrevTok;
6540b57cec5SDimitry Andric       PrevTok = Tok;
6550b57cec5SDimitry Andric       TmpPP.Lex(Tok);
6560b57cec5SDimitry Andric     }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric     // Insert the 'macro_popup' as the end tag, so that multi-line macros all
6590b57cec5SDimitry Andric     // get highlighted.
6600b57cec5SDimitry Andric     Expansion = "<span class='macro_popup'>" + Expansion + "</span></span>";
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric     HighlightRange(R, LLoc.getBegin(), LLoc.getEnd(), "<span class='macro'>",
6630b57cec5SDimitry Andric                    Expansion.c_str(), LLoc.isTokenRange());
6640b57cec5SDimitry Andric   }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   // Restore the preprocessor's old state.
6670b57cec5SDimitry Andric   TmpPP.setDiagnostics(*OldDiags);
6680b57cec5SDimitry Andric   TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
6690b57cec5SDimitry Andric }
670