xref: /src/contrib/llvm-project/clang/lib/Rewrite/HTMLRewrite.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1ec2b103cSEd Schouten //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
2ec2b103cSEd Schouten //
322989816SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422989816SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
522989816SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ec2b103cSEd Schouten //
7ec2b103cSEd Schouten //===----------------------------------------------------------------------===//
8ec2b103cSEd Schouten //
99f4dbff6SDimitry Andric //  This file defines the HTMLRewriter class, which is used to translate the
10ec2b103cSEd Schouten //  text of a source file into prettified HTML.
11ec2b103cSEd Schouten //
12ec2b103cSEd Schouten //===----------------------------------------------------------------------===//
13ec2b103cSEd Schouten 
1413cc256eSDimitry Andric #include "clang/Rewrite/Core/HTMLRewrite.h"
15ec2b103cSEd Schouten #include "clang/Basic/SourceManager.h"
16809500fcSDimitry Andric #include "clang/Lex/Preprocessor.h"
17809500fcSDimitry Andric #include "clang/Lex/TokenConcatenation.h"
18809500fcSDimitry Andric #include "clang/Rewrite/Core/Rewriter.h"
19809500fcSDimitry Andric #include "llvm/ADT/SmallString.h"
20bca07a45SDimitry Andric #include "llvm/Support/ErrorHandling.h"
21ec2b103cSEd Schouten #include "llvm/Support/MemoryBuffer.h"
22ec2b103cSEd Schouten #include "llvm/Support/raw_ostream.h"
239f4dbff6SDimitry Andric #include <memory>
24ec2b103cSEd Schouten 
25ac9a064cSDimitry Andric using namespace clang;
26ac9a064cSDimitry Andric using namespace llvm;
27ac9a064cSDimitry Andric using namespace html;
28ec2b103cSEd Schouten 
29ec2b103cSEd Schouten /// HighlightRange - Highlight a range in the source code with the specified
30ec2b103cSEd Schouten /// start/end tags.  B/E must be in the same file.  This ensures that
31ec2b103cSEd Schouten /// start/end tags are placed at the start/end of each line if the range is
32ec2b103cSEd Schouten /// multiline.
HighlightRange(Rewriter & R,SourceLocation B,SourceLocation E,const char * StartTag,const char * EndTag,bool IsTokenRange)33ec2b103cSEd Schouten void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
3448675466SDimitry Andric                           const char *StartTag, const char *EndTag,
3548675466SDimitry Andric                           bool IsTokenRange) {
36ec2b103cSEd Schouten   SourceManager &SM = R.getSourceMgr();
3736981b17SDimitry Andric   B = SM.getExpansionLoc(B);
3836981b17SDimitry Andric   E = SM.getExpansionLoc(E);
39ec2b103cSEd Schouten   FileID FID = SM.getFileID(B);
40ec2b103cSEd Schouten   assert(SM.getFileID(E) == FID && "B/E not in the same file!");
41ec2b103cSEd Schouten 
42ec2b103cSEd Schouten   unsigned BOffset = SM.getFileOffset(B);
43ec2b103cSEd Schouten   unsigned EOffset = SM.getFileOffset(E);
44ec2b103cSEd Schouten 
45ec2b103cSEd Schouten   // Include the whole end token in the range.
4648675466SDimitry Andric   if (IsTokenRange)
47ec2b103cSEd Schouten     EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
48ec2b103cSEd Schouten 
494a37f65fSRoman Divacky   bool Invalid = false;
504a37f65fSRoman Divacky   const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
514a37f65fSRoman Divacky   if (Invalid)
524a37f65fSRoman Divacky     return;
534a37f65fSRoman Divacky 
54ec2b103cSEd Schouten   HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
554a37f65fSRoman Divacky                  BufferStart, StartTag, EndTag);
56ec2b103cSEd Schouten }
57ec2b103cSEd Schouten 
58ec2b103cSEd Schouten /// HighlightRange - This is the same as the above method, but takes
59ec2b103cSEd Schouten /// decomposed file locations.
HighlightRange(RewriteBuffer & RB,unsigned B,unsigned E,const char * BufferStart,const char * StartTag,const char * EndTag)60ec2b103cSEd Schouten void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
61ec2b103cSEd Schouten                           const char *BufferStart,
62ec2b103cSEd Schouten                           const char *StartTag, const char *EndTag) {
63ec2b103cSEd Schouten   // Insert the tag at the absolute start/end of the range.
644c8b2481SRoman Divacky   RB.InsertTextAfter(B, StartTag);
654c8b2481SRoman Divacky   RB.InsertTextBefore(E, EndTag);
66ec2b103cSEd Schouten 
67ec2b103cSEd Schouten   // Scan the range to see if there is a \r or \n.  If so, and if the line is
68ec2b103cSEd Schouten   // not blank, insert tags on that line as well.
69ec2b103cSEd Schouten   bool HadOpenTag = true;
70ec2b103cSEd Schouten 
71ec2b103cSEd Schouten   unsigned LastNonWhiteSpace = B;
72ec2b103cSEd Schouten   for (unsigned i = B; i != E; ++i) {
73ec2b103cSEd Schouten     switch (BufferStart[i]) {
74ec2b103cSEd Schouten     case '\r':
75ec2b103cSEd Schouten     case '\n':
76ec2b103cSEd Schouten       // Okay, we found a newline in the range.  If we have an open tag, we need
77ec2b103cSEd Schouten       // to insert a close tag at the first non-whitespace before the newline.
78ec2b103cSEd Schouten       if (HadOpenTag)
794c8b2481SRoman Divacky         RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
80ec2b103cSEd Schouten 
81ec2b103cSEd Schouten       // Instead of inserting an open tag immediately after the newline, we
82ec2b103cSEd Schouten       // wait until we see a non-whitespace character.  This prevents us from
83ec2b103cSEd Schouten       // inserting tags around blank lines, and also allows the open tag to
84ec2b103cSEd Schouten       // be put *after* whitespace on a non-blank line.
85ec2b103cSEd Schouten       HadOpenTag = false;
86ec2b103cSEd Schouten       break;
87ec2b103cSEd Schouten     case '\0':
88ec2b103cSEd Schouten     case ' ':
89ec2b103cSEd Schouten     case '\t':
90ec2b103cSEd Schouten     case '\f':
91ec2b103cSEd Schouten     case '\v':
92ec2b103cSEd Schouten       // Ignore whitespace.
93ec2b103cSEd Schouten       break;
94ec2b103cSEd Schouten 
95ec2b103cSEd Schouten     default:
96ec2b103cSEd Schouten       // If there is no tag open, do it now.
97ec2b103cSEd Schouten       if (!HadOpenTag) {
984c8b2481SRoman Divacky         RB.InsertTextAfter(i, StartTag);
99ec2b103cSEd Schouten         HadOpenTag = true;
100ec2b103cSEd Schouten       }
101ec2b103cSEd Schouten 
102ec2b103cSEd Schouten       // Remember this character.
103ec2b103cSEd Schouten       LastNonWhiteSpace = i;
104ec2b103cSEd Schouten       break;
105ec2b103cSEd Schouten     }
106ec2b103cSEd Schouten   }
107ec2b103cSEd Schouten }
108ec2b103cSEd Schouten 
109ac9a064cSDimitry Andric namespace clang::html {
110ac9a064cSDimitry Andric struct RelexRewriteCache {
111ac9a064cSDimitry Andric   // These structs mimic input arguments of HighlightRange().
112ac9a064cSDimitry Andric   struct Highlight {
113ac9a064cSDimitry Andric     SourceLocation B, E;
114ac9a064cSDimitry Andric     std::string StartTag, EndTag;
115ac9a064cSDimitry Andric     bool IsTokenRange;
116ac9a064cSDimitry Andric   };
117ac9a064cSDimitry Andric   struct RawHighlight {
118ac9a064cSDimitry Andric     unsigned B, E;
119ac9a064cSDimitry Andric     std::string StartTag, EndTag;
120ac9a064cSDimitry Andric   };
121ac9a064cSDimitry Andric 
122ac9a064cSDimitry Andric   // SmallVector isn't appropriate because these vectors are almost never small.
123ac9a064cSDimitry Andric   using HighlightList = std::vector<Highlight>;
124ac9a064cSDimitry Andric   using RawHighlightList = std::vector<RawHighlight>;
125ac9a064cSDimitry Andric 
126ac9a064cSDimitry Andric   DenseMap<FileID, RawHighlightList> SyntaxHighlights;
127ac9a064cSDimitry Andric   DenseMap<FileID, HighlightList> MacroHighlights;
128ac9a064cSDimitry Andric };
129ac9a064cSDimitry Andric } // namespace clang::html
130ac9a064cSDimitry Andric 
instantiateRelexRewriteCache()131ac9a064cSDimitry Andric html::RelexRewriteCacheRef html::instantiateRelexRewriteCache() {
132ac9a064cSDimitry Andric   return std::make_shared<RelexRewriteCache>();
133ac9a064cSDimitry Andric }
134ac9a064cSDimitry Andric 
EscapeText(Rewriter & R,FileID FID,bool EscapeSpaces,bool ReplaceTabs)135ec2b103cSEd Schouten void html::EscapeText(Rewriter &R, FileID FID,
136ec2b103cSEd Schouten                       bool EscapeSpaces, bool ReplaceTabs) {
137ec2b103cSEd Schouten 
138b60736ecSDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
139b60736ecSDimitry Andric   const char* C = Buf.getBufferStart();
140b60736ecSDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
141ec2b103cSEd Schouten 
142ec2b103cSEd Schouten   assert (C <= FileEnd);
143ec2b103cSEd Schouten 
144ec2b103cSEd Schouten   RewriteBuffer &RB = R.getEditBuffer(FID);
145ec2b103cSEd Schouten 
146ec2b103cSEd Schouten   unsigned ColNo = 0;
147ec2b103cSEd Schouten   for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
148ec2b103cSEd Schouten     switch (*C) {
149ec2b103cSEd Schouten     default: ++ColNo; break;
150ec2b103cSEd Schouten     case '\n':
151ec2b103cSEd Schouten     case '\r':
152ec2b103cSEd Schouten       ColNo = 0;
153ec2b103cSEd Schouten       break;
154ec2b103cSEd Schouten 
155ec2b103cSEd Schouten     case ' ':
156ec2b103cSEd Schouten       if (EscapeSpaces)
1574c8b2481SRoman Divacky         RB.ReplaceText(FilePos, 1, "&nbsp;");
158ec2b103cSEd Schouten       ++ColNo;
159ec2b103cSEd Schouten       break;
160ec2b103cSEd Schouten     case '\f':
1614c8b2481SRoman Divacky       RB.ReplaceText(FilePos, 1, "<hr>");
162ec2b103cSEd Schouten       ColNo = 0;
163ec2b103cSEd Schouten       break;
164ec2b103cSEd Schouten 
165ec2b103cSEd Schouten     case '\t': {
166ec2b103cSEd Schouten       if (!ReplaceTabs)
167ec2b103cSEd Schouten         break;
168ec2b103cSEd Schouten       unsigned NumSpaces = 8-(ColNo&7);
169ec2b103cSEd Schouten       if (EscapeSpaces)
1704c8b2481SRoman Divacky         RB.ReplaceText(FilePos, 1,
17136981b17SDimitry Andric                        StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
1724c8b2481SRoman Divacky                                        "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
173ec2b103cSEd Schouten       else
17436981b17SDimitry Andric         RB.ReplaceText(FilePos, 1, StringRef("        ", NumSpaces));
175ec2b103cSEd Schouten       ColNo += NumSpaces;
176ec2b103cSEd Schouten       break;
177ec2b103cSEd Schouten     }
178ec2b103cSEd Schouten     case '<':
1794c8b2481SRoman Divacky       RB.ReplaceText(FilePos, 1, "&lt;");
180ec2b103cSEd Schouten       ++ColNo;
181ec2b103cSEd Schouten       break;
182ec2b103cSEd Schouten 
183ec2b103cSEd Schouten     case '>':
1844c8b2481SRoman Divacky       RB.ReplaceText(FilePos, 1, "&gt;");
185ec2b103cSEd Schouten       ++ColNo;
186ec2b103cSEd Schouten       break;
187ec2b103cSEd Schouten 
188ec2b103cSEd Schouten     case '&':
1894c8b2481SRoman Divacky       RB.ReplaceText(FilePos, 1, "&amp;");
190ec2b103cSEd Schouten       ++ColNo;
191ec2b103cSEd Schouten       break;
192ec2b103cSEd Schouten     }
193ec2b103cSEd Schouten   }
194ec2b103cSEd Schouten }
195ec2b103cSEd Schouten 
EscapeText(StringRef s,bool EscapeSpaces,bool ReplaceTabs)196bfef3995SDimitry Andric std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
197ec2b103cSEd Schouten 
198ec2b103cSEd Schouten   unsigned len = s.size();
199ec2b103cSEd Schouten   std::string Str;
200ec2b103cSEd Schouten   llvm::raw_string_ostream os(Str);
201ec2b103cSEd Schouten 
202ec2b103cSEd Schouten   for (unsigned i = 0 ; i < len; ++i) {
203ec2b103cSEd Schouten 
204ec2b103cSEd Schouten     char c = s[i];
205ec2b103cSEd Schouten     switch (c) {
206ec2b103cSEd Schouten     default:
207ec2b103cSEd Schouten       os << c; break;
208ec2b103cSEd Schouten 
209ec2b103cSEd Schouten     case ' ':
210ec2b103cSEd Schouten       if (EscapeSpaces) os << "&nbsp;";
211ec2b103cSEd Schouten       else os << ' ';
212ec2b103cSEd Schouten       break;
213ec2b103cSEd Schouten 
214ec2b103cSEd Schouten     case '\t':
215ec2b103cSEd Schouten       if (ReplaceTabs) {
216ec2b103cSEd Schouten         if (EscapeSpaces)
217ec2b103cSEd Schouten           for (unsigned i = 0; i < 4; ++i)
218ec2b103cSEd Schouten             os << "&nbsp;";
219ec2b103cSEd Schouten         else
220ec2b103cSEd Schouten           for (unsigned i = 0; i < 4; ++i)
221ec2b103cSEd Schouten             os << " ";
222ec2b103cSEd Schouten       }
223ec2b103cSEd Schouten       else
224ec2b103cSEd Schouten         os << c;
225ec2b103cSEd Schouten 
226ec2b103cSEd Schouten       break;
227ec2b103cSEd Schouten 
228ec2b103cSEd Schouten     case '<': os << "&lt;"; break;
229ec2b103cSEd Schouten     case '>': os << "&gt;"; break;
230ec2b103cSEd Schouten     case '&': os << "&amp;"; break;
231ec2b103cSEd Schouten     }
232ec2b103cSEd Schouten   }
233ec2b103cSEd Schouten 
23477fc4c14SDimitry Andric   return Str;
235ec2b103cSEd Schouten }
236ec2b103cSEd Schouten 
AddLineNumber(RewriteBuffer & RB,unsigned LineNo,unsigned B,unsigned E)237ec2b103cSEd Schouten static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
238ec2b103cSEd Schouten                           unsigned B, unsigned E) {
239dbe13110SDimitry Andric   SmallString<256> Str;
2404c8b2481SRoman Divacky   llvm::raw_svector_ostream OS(Str);
2414c8b2481SRoman Divacky 
24248675466SDimitry Andric   OS << "<tr class=\"codeline\" data-linenumber=\"" << LineNo << "\">"
24348675466SDimitry Andric      << "<td class=\"num\" id=\"LN" << LineNo << "\">" << LineNo
24448675466SDimitry Andric      << "</td><td class=\"line\">";
245ec2b103cSEd Schouten 
246ec2b103cSEd Schouten   if (B == E) { // Handle empty lines.
2474c8b2481SRoman Divacky     OS << " </td></tr>";
2484c8b2481SRoman Divacky     RB.InsertTextBefore(B, OS.str());
249ec2b103cSEd Schouten   } else {
2504c8b2481SRoman Divacky     RB.InsertTextBefore(B, OS.str());
2514c8b2481SRoman Divacky     RB.InsertTextBefore(E, "</td></tr>");
252ec2b103cSEd Schouten   }
253ec2b103cSEd Schouten }
254ec2b103cSEd Schouten 
AddLineNumbers(Rewriter & R,FileID FID)255ec2b103cSEd Schouten void html::AddLineNumbers(Rewriter& R, FileID FID) {
256ec2b103cSEd Schouten 
257b60736ecSDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
258b60736ecSDimitry Andric   const char* FileBeg = Buf.getBufferStart();
259b60736ecSDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
260ec2b103cSEd Schouten   const char* C = FileBeg;
261ec2b103cSEd Schouten   RewriteBuffer &RB = R.getEditBuffer(FID);
262ec2b103cSEd Schouten 
263ec2b103cSEd Schouten   assert (C <= FileEnd);
264ec2b103cSEd Schouten 
265ec2b103cSEd Schouten   unsigned LineNo = 0;
266ec2b103cSEd Schouten   unsigned FilePos = 0;
267ec2b103cSEd Schouten 
268ec2b103cSEd Schouten   while (C != FileEnd) {
269ec2b103cSEd Schouten 
270ec2b103cSEd Schouten     ++LineNo;
271ec2b103cSEd Schouten     unsigned LineStartPos = FilePos;
272ec2b103cSEd Schouten     unsigned LineEndPos = FileEnd - FileBeg;
273ec2b103cSEd Schouten 
274ec2b103cSEd Schouten     assert (FilePos <= LineEndPos);
275ec2b103cSEd Schouten     assert (C < FileEnd);
276ec2b103cSEd Schouten 
277ec2b103cSEd Schouten     // Scan until the newline (or end-of-file).
278ec2b103cSEd Schouten 
279ec2b103cSEd Schouten     while (C != FileEnd) {
280ec2b103cSEd Schouten       char c = *C;
281ec2b103cSEd Schouten       ++C;
282ec2b103cSEd Schouten 
283ec2b103cSEd Schouten       if (c == '\n') {
284ec2b103cSEd Schouten         LineEndPos = FilePos++;
285ec2b103cSEd Schouten         break;
286ec2b103cSEd Schouten       }
287ec2b103cSEd Schouten 
288ec2b103cSEd Schouten       ++FilePos;
289ec2b103cSEd Schouten     }
290ec2b103cSEd Schouten 
291ec2b103cSEd Schouten     AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
292ec2b103cSEd Schouten   }
293ec2b103cSEd Schouten 
294ec2b103cSEd Schouten   // Add one big table tag that surrounds all of the code.
29548675466SDimitry Andric   std::string s;
29648675466SDimitry Andric   llvm::raw_string_ostream os(s);
29748675466SDimitry Andric   os << "<table class=\"code\" data-fileid=\"" << FID.getHashValue() << "\">\n";
29848675466SDimitry Andric   RB.InsertTextBefore(0, os.str());
2994c8b2481SRoman Divacky   RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
300ec2b103cSEd Schouten }
301ec2b103cSEd Schouten 
AddHeaderFooterInternalBuiltinCSS(Rewriter & R,FileID FID,StringRef title)302ec2b103cSEd Schouten void html::AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID,
303bab175ecSDimitry Andric                                              StringRef title) {
304ec2b103cSEd Schouten 
305b60736ecSDimitry Andric   llvm::MemoryBufferRef Buf = R.getSourceMgr().getBufferOrFake(FID);
306b60736ecSDimitry Andric   const char* FileStart = Buf.getBufferStart();
307b60736ecSDimitry Andric   const char* FileEnd = Buf.getBufferEnd();
308ec2b103cSEd Schouten 
309ec2b103cSEd Schouten   SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
31036981b17SDimitry Andric   SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
311ec2b103cSEd Schouten 
312ec2b103cSEd Schouten   std::string s;
313ec2b103cSEd Schouten   llvm::raw_string_ostream os(s);
314ec2b103cSEd Schouten   os << "<!doctype html>\n" // Use HTML 5 doctype
315ec2b103cSEd Schouten         "<html>\n<head>\n";
316ec2b103cSEd Schouten 
317bab175ecSDimitry Andric   if (!title.empty())
318ec2b103cSEd Schouten     os << "<title>" << html::EscapeText(title) << "</title>\n";
319ec2b103cSEd Schouten 
32048675466SDimitry Andric   os << R"<<<(
32148675466SDimitry Andric <style type="text/css">
32248675466SDimitry Andric body { color:#000000; background-color:#ffffff }
32348675466SDimitry Andric body { font-family:Helvetica, sans-serif; font-size:10pt }
32448675466SDimitry Andric h1 { font-size:14pt }
32548675466SDimitry Andric .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }
32648675466SDimitry Andric .FileNav { margin-left: 5px; margin-right: 5px; display: inline; }
32748675466SDimitry Andric .FileNav a { text-decoration:none; font-size: larger; }
32848675466SDimitry Andric .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }
32948675466SDimitry Andric .divider { background-color: gray; }
33048675466SDimitry Andric .code { border-collapse:collapse; width:100%; }
33148675466SDimitry Andric .code { font-family: "Monospace", monospace; font-size:10pt }
33248675466SDimitry Andric .code { line-height: 1.2em }
33348675466SDimitry Andric .comment { color: green; font-style: oblique }
33448675466SDimitry Andric .keyword { color: blue }
33548675466SDimitry Andric .string_literal { color: red }
33648675466SDimitry Andric .directive { color: darkmagenta }
33722989816SDimitry Andric 
33822989816SDimitry Andric /* Macros and variables could have pop-up notes hidden by default.
33922989816SDimitry Andric   - Macro pop-up:    expansion of the macro
34022989816SDimitry Andric   - Variable pop-up: value (table) of the variable */
34122989816SDimitry Andric .macro_popup, .variable_popup { display: none; }
34222989816SDimitry Andric 
34322989816SDimitry Andric /* Pop-up appears on mouse-hover event. */
34422989816SDimitry Andric .macro:hover .macro_popup, .variable:hover .variable_popup {
34548675466SDimitry Andric   display: block;
34648675466SDimitry Andric   padding: 2px;
34748675466SDimitry Andric   -webkit-border-radius:5px;
34848675466SDimitry Andric   -webkit-box-shadow:1px 1px 7px #000;
34948675466SDimitry Andric   border-radius:5px;
35048675466SDimitry Andric   box-shadow:1px 1px 7px #000;
35148675466SDimitry Andric   position: absolute;
35248675466SDimitry Andric   top: -1em;
35348675466SDimitry Andric   left:10em;
35448675466SDimitry Andric   z-index: 1
35548675466SDimitry Andric }
35648675466SDimitry Andric 
35722989816SDimitry Andric .macro_popup {
35822989816SDimitry Andric   border: 2px solid red;
35922989816SDimitry Andric   background-color:#FFF0F0;
36022989816SDimitry Andric   font-weight: normal;
36122989816SDimitry Andric }
36222989816SDimitry Andric 
36322989816SDimitry Andric .variable_popup {
36422989816SDimitry Andric   border: 2px solid blue;
36522989816SDimitry Andric   background-color:#F0F0FF;
36622989816SDimitry Andric   font-weight: bold;
36722989816SDimitry Andric   font-family: Helvetica, sans-serif;
36822989816SDimitry Andric   font-size: 9pt;
36922989816SDimitry Andric }
37022989816SDimitry Andric 
37122989816SDimitry Andric /* Pop-up notes needs a relative position as a base where they pops up. */
37222989816SDimitry Andric .macro, .variable {
37322989816SDimitry Andric   background-color: PaleGoldenRod;
37422989816SDimitry Andric   position: relative;
37522989816SDimitry Andric }
37622989816SDimitry Andric .macro { color: DarkMagenta; }
37722989816SDimitry Andric 
37848675466SDimitry Andric #tooltiphint {
37948675466SDimitry Andric   position: fixed;
38048675466SDimitry Andric   width: 50em;
38148675466SDimitry Andric   margin-left: -25em;
38248675466SDimitry Andric   left: 50%;
38348675466SDimitry Andric   padding: 10px;
38448675466SDimitry Andric   border: 1px solid #b0b0b0;
38548675466SDimitry Andric   border-radius: 2px;
38648675466SDimitry Andric   box-shadow: 1px 1px 7px black;
38748675466SDimitry Andric   background-color: #c0c0c0;
38848675466SDimitry Andric   z-index: 2;
38948675466SDimitry Andric }
39048675466SDimitry Andric 
39148675466SDimitry Andric .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
39248675466SDimitry Andric .num { text-align:right; font-size:8pt }
39348675466SDimitry Andric .num { color:#444444 }
39448675466SDimitry Andric .line { padding-left: 1ex; border-left: 3px solid #ccc }
39548675466SDimitry Andric .line { white-space: pre }
39648675466SDimitry Andric .msg { -webkit-box-shadow:1px 1px 7px #000 }
39748675466SDimitry Andric .msg { box-shadow:1px 1px 7px #000 }
39848675466SDimitry Andric .msg { -webkit-border-radius:5px }
39948675466SDimitry Andric .msg { border-radius:5px }
40048675466SDimitry Andric .msg { font-family:Helvetica, sans-serif; font-size:8pt }
40148675466SDimitry Andric .msg { float:left }
402c0981da4SDimitry Andric .msg { position:relative }
40348675466SDimitry Andric .msg { padding:0.25em 1ex 0.25em 1ex }
40448675466SDimitry Andric .msg { margin-top:10px; margin-bottom:10px }
40548675466SDimitry Andric .msg { font-weight:bold }
40648675466SDimitry Andric .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
40748675466SDimitry Andric .msgT { padding:0x; spacing:0x }
40848675466SDimitry Andric .msgEvent { background-color:#fff8b4; color:#000000 }
40948675466SDimitry Andric .msgControl { background-color:#bbbbbb; color:#000000 }
41048675466SDimitry Andric .msgNote { background-color:#ddeeff; color:#000000 }
41148675466SDimitry Andric .mrange { background-color:#dfddf3 }
41248675466SDimitry Andric .mrange { border-bottom:1px solid #6F9DBE }
41348675466SDimitry Andric .PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
41448675466SDimitry Andric .PathIndex { -webkit-border-radius:8px }
41548675466SDimitry Andric .PathIndex { border-radius:8px }
41648675466SDimitry Andric .PathIndexEvent { background-color:#bfba87 }
41748675466SDimitry Andric .PathIndexControl { background-color:#8c8c8c }
41822989816SDimitry Andric .PathIndexPopUp { background-color: #879abc; }
41948675466SDimitry Andric .PathNav a { text-decoration:none; font-size: larger }
42048675466SDimitry Andric .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
42148675466SDimitry Andric .CodeRemovalHint { background-color:#de1010 }
42248675466SDimitry Andric .CodeRemovalHint { border-bottom:1px solid #6F9DBE }
423c0981da4SDimitry Andric .msg.selected{ background-color:orange !important; }
42448675466SDimitry Andric 
42548675466SDimitry Andric table.simpletable {
42648675466SDimitry Andric   padding: 5px;
42748675466SDimitry Andric   font-size:12pt;
42848675466SDimitry Andric   margin:20px;
42948675466SDimitry Andric   border-collapse: collapse; border-spacing: 0px;
43048675466SDimitry Andric }
43148675466SDimitry Andric td.rowname {
43248675466SDimitry Andric   text-align: right;
43348675466SDimitry Andric   vertical-align: top;
43448675466SDimitry Andric   font-weight: bold;
43548675466SDimitry Andric   color:#444444;
43648675466SDimitry Andric   padding-right:2ex;
43748675466SDimitry Andric }
43848675466SDimitry Andric 
43948675466SDimitry Andric /* Hidden text. */
44048675466SDimitry Andric input.spoilerhider + label {
44148675466SDimitry Andric   cursor: pointer;
44248675466SDimitry Andric   text-decoration: underline;
44348675466SDimitry Andric   display: block;
44448675466SDimitry Andric }
44548675466SDimitry Andric input.spoilerhider {
44648675466SDimitry Andric  display: none;
44748675466SDimitry Andric }
44848675466SDimitry Andric input.spoilerhider ~ .spoiler {
44948675466SDimitry Andric   overflow: hidden;
45048675466SDimitry Andric   margin: 10px auto 0;
45148675466SDimitry Andric   height: 0;
45248675466SDimitry Andric   opacity: 0;
45348675466SDimitry Andric }
45448675466SDimitry Andric input.spoilerhider:checked + label + .spoiler{
45548675466SDimitry Andric   height: auto;
45648675466SDimitry Andric   opacity: 1;
45748675466SDimitry Andric }
45848675466SDimitry Andric </style>
45948675466SDimitry Andric </head>
46048675466SDimitry Andric <body>)<<<";
461ec2b103cSEd Schouten 
462ec2b103cSEd Schouten   // Generate header
4634c8b2481SRoman Divacky   R.InsertTextBefore(StartLoc, os.str());
464ec2b103cSEd Schouten   // Generate footer
465ec2b103cSEd Schouten 
4664c8b2481SRoman Divacky   R.InsertTextAfter(EndLoc, "</body></html>\n");
467ec2b103cSEd Schouten }
468ec2b103cSEd Schouten 
469ec2b103cSEd Schouten /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
470ec2b103cSEd Schouten /// information about keywords, macro expansions etc.  This uses the macro
471ec2b103cSEd Schouten /// table state from the end of the file, so it won't be perfectly perfect,
472ec2b103cSEd Schouten /// but it will be reasonably close.
SyntaxHighlightImpl(Rewriter & R,FileID FID,const Preprocessor & PP,llvm::function_ref<void (RewriteBuffer &,unsigned,unsigned,const char *,const char *,const char *)> HighlightRangeCallback)473ac9a064cSDimitry Andric static void SyntaxHighlightImpl(
474ac9a064cSDimitry Andric     Rewriter &R, FileID FID, const Preprocessor &PP,
475ac9a064cSDimitry Andric     llvm::function_ref<void(RewriteBuffer &, unsigned, unsigned, const char *,
476ac9a064cSDimitry Andric                             const char *, const char *)>
477ac9a064cSDimitry Andric         HighlightRangeCallback) {
478ec2b103cSEd Schouten 
479ac9a064cSDimitry Andric   RewriteBuffer &RB = R.getEditBuffer(FID);
480ec2b103cSEd Schouten   const SourceManager &SM = PP.getSourceManager();
481b60736ecSDimitry Andric   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);
482ac9a064cSDimitry Andric   const char *BufferStart = FromFile.getBuffer().data();
483ac9a064cSDimitry Andric 
484dbe13110SDimitry Andric   Lexer L(FID, FromFile, SM, PP.getLangOpts());
485ec2b103cSEd Schouten 
486ec2b103cSEd Schouten   // Inform the preprocessor that we want to retain comments as tokens, so we
487ec2b103cSEd Schouten   // can highlight them.
488ec2b103cSEd Schouten   L.SetCommentRetentionState(true);
489ec2b103cSEd Schouten 
490ec2b103cSEd Schouten   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
491ec2b103cSEd Schouten   // macros.
492ec2b103cSEd Schouten   Token Tok;
493ec2b103cSEd Schouten   L.LexFromRawLexer(Tok);
494ec2b103cSEd Schouten 
495ec2b103cSEd Schouten   while (Tok.isNot(tok::eof)) {
496ec2b103cSEd Schouten     // Since we are lexing unexpanded tokens, all tokens are from the main
497ec2b103cSEd Schouten     // FileID.
498ec2b103cSEd Schouten     unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
499ec2b103cSEd Schouten     unsigned TokLen = Tok.getLength();
500ec2b103cSEd Schouten     switch (Tok.getKind()) {
501ec2b103cSEd Schouten     default: break;
502bca07a45SDimitry Andric     case tok::identifier:
503bca07a45SDimitry Andric       llvm_unreachable("tok::identifier in raw lexing mode!");
504bca07a45SDimitry Andric     case tok::raw_identifier: {
505bca07a45SDimitry Andric       // Fill in Result.IdentifierInfo and update the token kind,
506bca07a45SDimitry Andric       // looking up the identifier in the identifier table.
507bca07a45SDimitry Andric       PP.LookUpIdentifierInfo(Tok);
508ec2b103cSEd Schouten 
509ec2b103cSEd Schouten       // If this is a pp-identifier, for a keyword, highlight it as such.
510bca07a45SDimitry Andric       if (Tok.isNot(tok::identifier))
511ac9a064cSDimitry Andric         HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
512ec2b103cSEd Schouten                                "<span class='keyword'>", "</span>");
513ec2b103cSEd Schouten       break;
514ec2b103cSEd Schouten     }
515ec2b103cSEd Schouten     case tok::comment:
516ac9a064cSDimitry Andric       HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
517ec2b103cSEd Schouten                              "<span class='comment'>", "</span>");
518ec2b103cSEd Schouten       break;
51936981b17SDimitry Andric     case tok::utf8_string_literal:
52036981b17SDimitry Andric       // Chop off the u part of u8 prefix
52136981b17SDimitry Andric       ++TokOffs;
52236981b17SDimitry Andric       --TokLen;
52336981b17SDimitry Andric       // FALL THROUGH to chop the 8
524e3b55780SDimitry Andric       [[fallthrough]];
525ec2b103cSEd Schouten     case tok::wide_string_literal:
52636981b17SDimitry Andric     case tok::utf16_string_literal:
52736981b17SDimitry Andric     case tok::utf32_string_literal:
52836981b17SDimitry Andric       // Chop off the L, u, U or 8 prefix
529ec2b103cSEd Schouten       ++TokOffs;
530ec2b103cSEd Schouten       --TokLen;
531e3b55780SDimitry Andric       [[fallthrough]];
532ec2b103cSEd Schouten     case tok::string_literal:
533dbe13110SDimitry Andric       // FIXME: Exclude the optional ud-suffix from the highlighted range.
534ac9a064cSDimitry Andric       HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
535ec2b103cSEd Schouten                              "<span class='string_literal'>", "</span>");
536ec2b103cSEd Schouten       break;
537ec2b103cSEd Schouten     case tok::hash: {
538ec2b103cSEd Schouten       // If this is a preprocessor directive, all tokens to end of line are too.
539ec2b103cSEd Schouten       if (!Tok.isAtStartOfLine())
540ec2b103cSEd Schouten         break;
541ec2b103cSEd Schouten 
542ec2b103cSEd Schouten       // Eat all of the tokens until we get to the next one at the start of
543ec2b103cSEd Schouten       // line.
544ec2b103cSEd Schouten       unsigned TokEnd = TokOffs+TokLen;
545ec2b103cSEd Schouten       L.LexFromRawLexer(Tok);
546ec2b103cSEd Schouten       while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
547ec2b103cSEd Schouten         TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
548ec2b103cSEd Schouten         L.LexFromRawLexer(Tok);
549ec2b103cSEd Schouten       }
550ec2b103cSEd Schouten 
551ec2b103cSEd Schouten       // Find end of line.  This is a hack.
552ac9a064cSDimitry Andric       HighlightRangeCallback(RB, TokOffs, TokEnd, BufferStart,
553ec2b103cSEd Schouten                              "<span class='directive'>", "</span>");
554ec2b103cSEd Schouten 
555ec2b103cSEd Schouten       // Don't skip the next token.
556ec2b103cSEd Schouten       continue;
557ec2b103cSEd Schouten     }
558ec2b103cSEd Schouten     }
559ec2b103cSEd Schouten 
560ec2b103cSEd Schouten     L.LexFromRawLexer(Tok);
561ec2b103cSEd Schouten   }
562ec2b103cSEd Schouten }
SyntaxHighlight(Rewriter & R,FileID FID,const Preprocessor & PP,RelexRewriteCacheRef Cache)563ac9a064cSDimitry Andric void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP,
564ac9a064cSDimitry Andric                            RelexRewriteCacheRef Cache) {
565ac9a064cSDimitry Andric   RewriteBuffer &RB = R.getEditBuffer(FID);
566ac9a064cSDimitry Andric   const SourceManager &SM = PP.getSourceManager();
567ac9a064cSDimitry Andric   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);
568ac9a064cSDimitry Andric   const char *BufferStart = FromFile.getBuffer().data();
569ec2b103cSEd Schouten 
570ac9a064cSDimitry Andric   if (Cache) {
571ac9a064cSDimitry Andric     auto CacheIt = Cache->SyntaxHighlights.find(FID);
572ac9a064cSDimitry Andric     if (CacheIt != Cache->SyntaxHighlights.end()) {
573ac9a064cSDimitry Andric       for (const RelexRewriteCache::RawHighlight &H : CacheIt->second) {
574ac9a064cSDimitry Andric         HighlightRange(RB, H.B, H.E, BufferStart, H.StartTag.data(),
575ac9a064cSDimitry Andric                        H.EndTag.data());
576ac9a064cSDimitry Andric       }
577ac9a064cSDimitry Andric       return;
578ac9a064cSDimitry Andric     }
579ac9a064cSDimitry Andric   }
580ac9a064cSDimitry Andric 
581ac9a064cSDimitry Andric   // "Every time you would call HighlightRange, cache the inputs as well."
582ac9a064cSDimitry Andric   auto HighlightRangeCallback = [&](RewriteBuffer &RB, unsigned B, unsigned E,
583ac9a064cSDimitry Andric                                     const char *BufferStart,
584ac9a064cSDimitry Andric                                     const char *StartTag, const char *EndTag) {
585ac9a064cSDimitry Andric     HighlightRange(RB, B, E, BufferStart, StartTag, EndTag);
586ac9a064cSDimitry Andric 
587ac9a064cSDimitry Andric     if (Cache)
588ac9a064cSDimitry Andric       Cache->SyntaxHighlights[FID].push_back({B, E, StartTag, EndTag});
589ac9a064cSDimitry Andric   };
590ac9a064cSDimitry Andric 
591ac9a064cSDimitry Andric   SyntaxHighlightImpl(R, FID, PP, HighlightRangeCallback);
592ac9a064cSDimitry Andric }
593ac9a064cSDimitry Andric 
HighlightMacrosImpl(Rewriter & R,FileID FID,const Preprocessor & PP,llvm::function_ref<void (Rewriter &,SourceLocation,SourceLocation,const char *,const char *,bool)> HighlightRangeCallback)594ac9a064cSDimitry Andric static void HighlightMacrosImpl(
595ac9a064cSDimitry Andric     Rewriter &R, FileID FID, const Preprocessor &PP,
596ac9a064cSDimitry Andric     llvm::function_ref<void(Rewriter &, SourceLocation, SourceLocation,
597ac9a064cSDimitry Andric                             const char *, const char *, bool)>
598ac9a064cSDimitry Andric         HighlightRangeCallback) {
599ac9a064cSDimitry Andric 
600ec2b103cSEd Schouten   // Re-lex the raw token stream into a token buffer.
601ec2b103cSEd Schouten   const SourceManager &SM = PP.getSourceManager();
602ec2b103cSEd Schouten   std::vector<Token> TokenStream;
603ec2b103cSEd Schouten 
604b60736ecSDimitry Andric   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID);
605dbe13110SDimitry Andric   Lexer L(FID, FromFile, SM, PP.getLangOpts());
606ec2b103cSEd Schouten 
607ec2b103cSEd Schouten   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
608ec2b103cSEd Schouten   // macros.
6096f8fc217SDimitry Andric   while (true) {
610ec2b103cSEd Schouten     Token Tok;
611ec2b103cSEd Schouten     L.LexFromRawLexer(Tok);
612ec2b103cSEd Schouten 
613ec2b103cSEd Schouten     // If this is a # at the start of a line, discard it from the token stream.
614ec2b103cSEd Schouten     // We don't want the re-preprocess step to see #defines, #includes or other
615ec2b103cSEd Schouten     // preprocessor directives.
616ec2b103cSEd Schouten     if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
617ec2b103cSEd Schouten       continue;
618ec2b103cSEd Schouten 
619ec2b103cSEd Schouten     // If this is a ## token, change its kind to unknown so that repreprocessing
620ec2b103cSEd Schouten     // it will not produce an error.
621ec2b103cSEd Schouten     if (Tok.is(tok::hashhash))
622ec2b103cSEd Schouten       Tok.setKind(tok::unknown);
623ec2b103cSEd Schouten 
624ec2b103cSEd Schouten     // If this raw token is an identifier, the raw lexer won't have looked up
625ec2b103cSEd Schouten     // the corresponding identifier info for it.  Do this now so that it will be
626ec2b103cSEd Schouten     // macro expanded when we re-preprocess it.
627bca07a45SDimitry Andric     if (Tok.is(tok::raw_identifier))
628bca07a45SDimitry Andric       PP.LookUpIdentifierInfo(Tok);
629ec2b103cSEd Schouten 
630ec2b103cSEd Schouten     TokenStream.push_back(Tok);
631ec2b103cSEd Schouten 
632ec2b103cSEd Schouten     if (Tok.is(tok::eof)) break;
633ec2b103cSEd Schouten   }
634ec2b103cSEd Schouten 
635ec2b103cSEd Schouten   // Temporarily change the diagnostics object so that we ignore any generated
636ec2b103cSEd Schouten   // diagnostics from this pass.
63736981b17SDimitry Andric   DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
63813cc256eSDimitry Andric                              &PP.getDiagnostics().getDiagnosticOptions(),
63936981b17SDimitry Andric                       new IgnoringDiagConsumer);
640ec2b103cSEd Schouten 
6418f57cb03SRoman Divacky   // FIXME: This is a huge hack; we reuse the input preprocessor because we want
6428f57cb03SRoman Divacky   // its state, but we aren't actually changing it (we hope). This should really
6438f57cb03SRoman Divacky   // construct a copy of the preprocessor.
6448f57cb03SRoman Divacky   Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
64536981b17SDimitry Andric   DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
6468f57cb03SRoman Divacky   TmpPP.setDiagnostics(TmpDiags);
647ec2b103cSEd Schouten 
648ec2b103cSEd Schouten   // Inform the preprocessor that we don't want comments.
6498f57cb03SRoman Divacky   TmpPP.SetCommentRetentionState(false, false);
650ec2b103cSEd Schouten 
65156d91b49SDimitry Andric   // We don't want pragmas either. Although we filtered out #pragma, removing
65256d91b49SDimitry Andric   // _Pragma and __pragma is much harder.
65356d91b49SDimitry Andric   bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
65456d91b49SDimitry Andric   TmpPP.setPragmasEnabled(false);
65556d91b49SDimitry Andric 
656ec2b103cSEd Schouten   // Enter the tokens we just lexed.  This will cause them to be macro expanded
657ec2b103cSEd Schouten   // but won't enter sub-files (because we removed #'s).
65822989816SDimitry Andric   TmpPP.EnterTokenStream(TokenStream, false, /*IsReinject=*/false);
659ec2b103cSEd Schouten 
6608f57cb03SRoman Divacky   TokenConcatenation ConcatInfo(TmpPP);
661ec2b103cSEd Schouten 
662ec2b103cSEd Schouten   // Lex all the tokens.
663ec2b103cSEd Schouten   Token Tok;
6648f57cb03SRoman Divacky   TmpPP.Lex(Tok);
665ec2b103cSEd Schouten   while (Tok.isNot(tok::eof)) {
666ec2b103cSEd Schouten     // Ignore non-macro tokens.
667ec2b103cSEd Schouten     if (!Tok.getLocation().isMacroID()) {
6688f57cb03SRoman Divacky       TmpPP.Lex(Tok);
669ec2b103cSEd Schouten       continue;
670ec2b103cSEd Schouten     }
671ec2b103cSEd Schouten 
672ec2b103cSEd Schouten     // Okay, we have the first token of a macro expansion: highlight the
673180abc3dSDimitry Andric     // expansion by inserting a start tag before the macro expansion and
674ec2b103cSEd Schouten     // end tag after it.
67548675466SDimitry Andric     CharSourceRange LLoc = SM.getExpansionRange(Tok.getLocation());
676ec2b103cSEd Schouten 
677ec2b103cSEd Schouten     // Ignore tokens whose instantiation location was not the main file.
67848675466SDimitry Andric     if (SM.getFileID(LLoc.getBegin()) != FID) {
6798f57cb03SRoman Divacky       TmpPP.Lex(Tok);
680ec2b103cSEd Schouten       continue;
681ec2b103cSEd Schouten     }
682ec2b103cSEd Schouten 
68348675466SDimitry Andric     assert(SM.getFileID(LLoc.getEnd()) == FID &&
684ec2b103cSEd Schouten            "Start and end of expansion must be in the same ultimate file!");
685ec2b103cSEd Schouten 
6868f57cb03SRoman Divacky     std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
687ec2b103cSEd Schouten     unsigned LineLen = Expansion.size();
688ec2b103cSEd Schouten 
6890883ccd9SRoman Divacky     Token PrevPrevTok;
690ec2b103cSEd Schouten     Token PrevTok = Tok;
691ec2b103cSEd Schouten     // Okay, eat this token, getting the next one.
6928f57cb03SRoman Divacky     TmpPP.Lex(Tok);
693ec2b103cSEd Schouten 
694ec2b103cSEd Schouten     // Skip all the rest of the tokens that are part of this macro
695ec2b103cSEd Schouten     // instantiation.  It would be really nice to pop up a window with all the
696ec2b103cSEd Schouten     // spelling of the tokens or something.
697ec2b103cSEd Schouten     while (!Tok.is(tok::eof) &&
69848675466SDimitry Andric            SM.getExpansionLoc(Tok.getLocation()) == LLoc.getBegin()) {
699ec2b103cSEd Schouten       // Insert a newline if the macro expansion is getting large.
700ec2b103cSEd Schouten       if (LineLen > 60) {
701ec2b103cSEd Schouten         Expansion += "<br>";
702ec2b103cSEd Schouten         LineLen = 0;
703ec2b103cSEd Schouten       }
704ec2b103cSEd Schouten 
705ec2b103cSEd Schouten       LineLen -= Expansion.size();
706ec2b103cSEd Schouten 
707ec2b103cSEd Schouten       // If the tokens were already space separated, or if they must be to avoid
708ec2b103cSEd Schouten       // them being implicitly pasted, add a space between them.
709ec2b103cSEd Schouten       if (Tok.hasLeadingSpace() ||
7100883ccd9SRoman Divacky           ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
711ec2b103cSEd Schouten         Expansion += ' ';
712ec2b103cSEd Schouten 
713ec2b103cSEd Schouten       // Escape any special characters in the token text.
7148f57cb03SRoman Divacky       Expansion += EscapeText(TmpPP.getSpelling(Tok));
715ec2b103cSEd Schouten       LineLen += Expansion.size();
716ec2b103cSEd Schouten 
7170883ccd9SRoman Divacky       PrevPrevTok = PrevTok;
718ec2b103cSEd Schouten       PrevTok = Tok;
7198f57cb03SRoman Divacky       TmpPP.Lex(Tok);
720ec2b103cSEd Schouten     }
721ec2b103cSEd Schouten 
72222989816SDimitry Andric     // Insert the 'macro_popup' as the end tag, so that multi-line macros all
72322989816SDimitry Andric     // get highlighted.
72422989816SDimitry Andric     Expansion = "<span class='macro_popup'>" + Expansion + "</span></span>";
725ec2b103cSEd Schouten 
726ac9a064cSDimitry Andric     HighlightRangeCallback(R, LLoc.getBegin(), LLoc.getEnd(),
727ac9a064cSDimitry Andric                            "<span class='macro'>", Expansion.c_str(),
728ac9a064cSDimitry Andric                            LLoc.isTokenRange());
729ec2b103cSEd Schouten   }
730ec2b103cSEd Schouten 
73156d91b49SDimitry Andric   // Restore the preprocessor's old state.
7328f57cb03SRoman Divacky   TmpPP.setDiagnostics(*OldDiags);
73356d91b49SDimitry Andric   TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
734ec2b103cSEd Schouten }
735ac9a064cSDimitry Andric 
736ac9a064cSDimitry Andric /// HighlightMacros - This uses the macro table state from the end of the
737ac9a064cSDimitry Andric /// file, to re-expand macros and insert (into the HTML) information about the
738ac9a064cSDimitry Andric /// macro expansions.  This won't be perfectly perfect, but it will be
739ac9a064cSDimitry Andric /// reasonably close.
HighlightMacros(Rewriter & R,FileID FID,const Preprocessor & PP,RelexRewriteCacheRef Cache)740ac9a064cSDimitry Andric void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP,
741ac9a064cSDimitry Andric                            RelexRewriteCacheRef Cache) {
742ac9a064cSDimitry Andric   if (Cache) {
743ac9a064cSDimitry Andric     auto CacheIt = Cache->MacroHighlights.find(FID);
744ac9a064cSDimitry Andric     if (CacheIt != Cache->MacroHighlights.end()) {
745ac9a064cSDimitry Andric       for (const RelexRewriteCache::Highlight &H : CacheIt->second) {
746ac9a064cSDimitry Andric         HighlightRange(R, H.B, H.E, H.StartTag.data(), H.EndTag.data(),
747ac9a064cSDimitry Andric                        H.IsTokenRange);
748ac9a064cSDimitry Andric       }
749ac9a064cSDimitry Andric       return;
750ac9a064cSDimitry Andric     }
751ac9a064cSDimitry Andric   }
752ac9a064cSDimitry Andric 
753ac9a064cSDimitry Andric   // "Every time you would call HighlightRange, cache the inputs as well."
754ac9a064cSDimitry Andric   auto HighlightRangeCallback = [&](Rewriter &R, SourceLocation B,
755ac9a064cSDimitry Andric                                     SourceLocation E, const char *StartTag,
756ac9a064cSDimitry Andric                                     const char *EndTag, bool isTokenRange) {
757ac9a064cSDimitry Andric     HighlightRange(R, B, E, StartTag, EndTag, isTokenRange);
758ac9a064cSDimitry Andric 
759ac9a064cSDimitry Andric     if (Cache) {
760ac9a064cSDimitry Andric       Cache->MacroHighlights[FID].push_back(
761ac9a064cSDimitry Andric           {B, E, StartTag, EndTag, isTokenRange});
762ac9a064cSDimitry Andric     }
763ac9a064cSDimitry Andric   };
764ac9a064cSDimitry Andric 
765ac9a064cSDimitry Andric   HighlightMacrosImpl(R, FID, PP, HighlightRangeCallback);
766ac9a064cSDimitry Andric }
767