1*0a6a1f1dSLionel Sambuc //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc //  This file defines the HTMLRewriter class, which is used to translate the
11*0a6a1f1dSLionel Sambuc //  text of a source file into prettified HTML.
12*0a6a1f1dSLionel Sambuc //
13*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Core/HTMLRewrite.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Basic/SourceManager.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Lex/TokenConcatenation.h"
19*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallString.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/raw_ostream.h"
24*0a6a1f1dSLionel Sambuc #include <memory>
25*0a6a1f1dSLionel Sambuc using namespace clang;
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc /// HighlightRange - Highlight a range in the source code with the specified
29*0a6a1f1dSLionel Sambuc /// start/end tags.  B/E must be in the same file.  This ensures that
30*0a6a1f1dSLionel Sambuc /// start/end tags are placed at the start/end of each line if the range is
31*0a6a1f1dSLionel Sambuc /// multiline.
HighlightRange(Rewriter & R,SourceLocation B,SourceLocation E,const char * StartTag,const char * EndTag)32*0a6a1f1dSLionel Sambuc void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
33*0a6a1f1dSLionel Sambuc                           const char *StartTag, const char *EndTag) {
34*0a6a1f1dSLionel Sambuc   SourceManager &SM = R.getSourceMgr();
35*0a6a1f1dSLionel Sambuc   B = SM.getExpansionLoc(B);
36*0a6a1f1dSLionel Sambuc   E = SM.getExpansionLoc(E);
37*0a6a1f1dSLionel Sambuc   FileID FID = SM.getFileID(B);
38*0a6a1f1dSLionel Sambuc   assert(SM.getFileID(E) == FID && "B/E not in the same file!");
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc   unsigned BOffset = SM.getFileOffset(B);
41*0a6a1f1dSLionel Sambuc   unsigned EOffset = SM.getFileOffset(E);
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc   // Include the whole end token in the range.
44*0a6a1f1dSLionel Sambuc   EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc   bool Invalid = false;
47*0a6a1f1dSLionel Sambuc   const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
48*0a6a1f1dSLionel Sambuc   if (Invalid)
49*0a6a1f1dSLionel Sambuc     return;
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc   HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
52*0a6a1f1dSLionel Sambuc                  BufferStart, StartTag, EndTag);
53*0a6a1f1dSLionel Sambuc }
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc /// HighlightRange - This is the same as the above method, but takes
56*0a6a1f1dSLionel Sambuc /// decomposed file locations.
HighlightRange(RewriteBuffer & RB,unsigned B,unsigned E,const char * BufferStart,const char * StartTag,const char * EndTag)57*0a6a1f1dSLionel Sambuc void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
58*0a6a1f1dSLionel Sambuc                           const char *BufferStart,
59*0a6a1f1dSLionel Sambuc                           const char *StartTag, const char *EndTag) {
60*0a6a1f1dSLionel Sambuc   // Insert the tag at the absolute start/end of the range.
61*0a6a1f1dSLionel Sambuc   RB.InsertTextAfter(B, StartTag);
62*0a6a1f1dSLionel Sambuc   RB.InsertTextBefore(E, EndTag);
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc   // Scan the range to see if there is a \r or \n.  If so, and if the line is
65*0a6a1f1dSLionel Sambuc   // not blank, insert tags on that line as well.
66*0a6a1f1dSLionel Sambuc   bool HadOpenTag = true;
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc   unsigned LastNonWhiteSpace = B;
69*0a6a1f1dSLionel Sambuc   for (unsigned i = B; i != E; ++i) {
70*0a6a1f1dSLionel Sambuc     switch (BufferStart[i]) {
71*0a6a1f1dSLionel Sambuc     case '\r':
72*0a6a1f1dSLionel Sambuc     case '\n':
73*0a6a1f1dSLionel Sambuc       // Okay, we found a newline in the range.  If we have an open tag, we need
74*0a6a1f1dSLionel Sambuc       // to insert a close tag at the first non-whitespace before the newline.
75*0a6a1f1dSLionel Sambuc       if (HadOpenTag)
76*0a6a1f1dSLionel Sambuc         RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc       // Instead of inserting an open tag immediately after the newline, we
79*0a6a1f1dSLionel Sambuc       // wait until we see a non-whitespace character.  This prevents us from
80*0a6a1f1dSLionel Sambuc       // inserting tags around blank lines, and also allows the open tag to
81*0a6a1f1dSLionel Sambuc       // be put *after* whitespace on a non-blank line.
82*0a6a1f1dSLionel Sambuc       HadOpenTag = false;
83*0a6a1f1dSLionel Sambuc       break;
84*0a6a1f1dSLionel Sambuc     case '\0':
85*0a6a1f1dSLionel Sambuc     case ' ':
86*0a6a1f1dSLionel Sambuc     case '\t':
87*0a6a1f1dSLionel Sambuc     case '\f':
88*0a6a1f1dSLionel Sambuc     case '\v':
89*0a6a1f1dSLionel Sambuc       // Ignore whitespace.
90*0a6a1f1dSLionel Sambuc       break;
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc     default:
93*0a6a1f1dSLionel Sambuc       // If there is no tag open, do it now.
94*0a6a1f1dSLionel Sambuc       if (!HadOpenTag) {
95*0a6a1f1dSLionel Sambuc         RB.InsertTextAfter(i, StartTag);
96*0a6a1f1dSLionel Sambuc         HadOpenTag = true;
97*0a6a1f1dSLionel Sambuc       }
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc       // Remember this character.
100*0a6a1f1dSLionel Sambuc       LastNonWhiteSpace = i;
101*0a6a1f1dSLionel Sambuc       break;
102*0a6a1f1dSLionel Sambuc     }
103*0a6a1f1dSLionel Sambuc   }
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc 
EscapeText(Rewriter & R,FileID FID,bool EscapeSpaces,bool ReplaceTabs)106*0a6a1f1dSLionel Sambuc void html::EscapeText(Rewriter &R, FileID FID,
107*0a6a1f1dSLionel Sambuc                       bool EscapeSpaces, bool ReplaceTabs) {
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
110*0a6a1f1dSLionel Sambuc   const char* C = Buf->getBufferStart();
111*0a6a1f1dSLionel Sambuc   const char* FileEnd = Buf->getBufferEnd();
112*0a6a1f1dSLionel Sambuc 
113*0a6a1f1dSLionel Sambuc   assert (C <= FileEnd);
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc   RewriteBuffer &RB = R.getEditBuffer(FID);
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc   unsigned ColNo = 0;
118*0a6a1f1dSLionel Sambuc   for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
119*0a6a1f1dSLionel Sambuc     switch (*C) {
120*0a6a1f1dSLionel Sambuc     default: ++ColNo; break;
121*0a6a1f1dSLionel Sambuc     case '\n':
122*0a6a1f1dSLionel Sambuc     case '\r':
123*0a6a1f1dSLionel Sambuc       ColNo = 0;
124*0a6a1f1dSLionel Sambuc       break;
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc     case ' ':
127*0a6a1f1dSLionel Sambuc       if (EscapeSpaces)
128*0a6a1f1dSLionel Sambuc         RB.ReplaceText(FilePos, 1, "&nbsp;");
129*0a6a1f1dSLionel Sambuc       ++ColNo;
130*0a6a1f1dSLionel Sambuc       break;
131*0a6a1f1dSLionel Sambuc     case '\f':
132*0a6a1f1dSLionel Sambuc       RB.ReplaceText(FilePos, 1, "<hr>");
133*0a6a1f1dSLionel Sambuc       ColNo = 0;
134*0a6a1f1dSLionel Sambuc       break;
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc     case '\t': {
137*0a6a1f1dSLionel Sambuc       if (!ReplaceTabs)
138*0a6a1f1dSLionel Sambuc         break;
139*0a6a1f1dSLionel Sambuc       unsigned NumSpaces = 8-(ColNo&7);
140*0a6a1f1dSLionel Sambuc       if (EscapeSpaces)
141*0a6a1f1dSLionel Sambuc         RB.ReplaceText(FilePos, 1,
142*0a6a1f1dSLionel Sambuc                        StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
143*0a6a1f1dSLionel Sambuc                                        "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
144*0a6a1f1dSLionel Sambuc       else
145*0a6a1f1dSLionel Sambuc         RB.ReplaceText(FilePos, 1, StringRef("        ", NumSpaces));
146*0a6a1f1dSLionel Sambuc       ColNo += NumSpaces;
147*0a6a1f1dSLionel Sambuc       break;
148*0a6a1f1dSLionel Sambuc     }
149*0a6a1f1dSLionel Sambuc     case '<':
150*0a6a1f1dSLionel Sambuc       RB.ReplaceText(FilePos, 1, "&lt;");
151*0a6a1f1dSLionel Sambuc       ++ColNo;
152*0a6a1f1dSLionel Sambuc       break;
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc     case '>':
155*0a6a1f1dSLionel Sambuc       RB.ReplaceText(FilePos, 1, "&gt;");
156*0a6a1f1dSLionel Sambuc       ++ColNo;
157*0a6a1f1dSLionel Sambuc       break;
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc     case '&':
160*0a6a1f1dSLionel Sambuc       RB.ReplaceText(FilePos, 1, "&amp;");
161*0a6a1f1dSLionel Sambuc       ++ColNo;
162*0a6a1f1dSLionel Sambuc       break;
163*0a6a1f1dSLionel Sambuc     }
164*0a6a1f1dSLionel Sambuc   }
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc 
EscapeText(StringRef s,bool EscapeSpaces,bool ReplaceTabs)167*0a6a1f1dSLionel Sambuc std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
168*0a6a1f1dSLionel Sambuc 
169*0a6a1f1dSLionel Sambuc   unsigned len = s.size();
170*0a6a1f1dSLionel Sambuc   std::string Str;
171*0a6a1f1dSLionel Sambuc   llvm::raw_string_ostream os(Str);
172*0a6a1f1dSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc   for (unsigned i = 0 ; i < len; ++i) {
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc     char c = s[i];
176*0a6a1f1dSLionel Sambuc     switch (c) {
177*0a6a1f1dSLionel Sambuc     default:
178*0a6a1f1dSLionel Sambuc       os << c; break;
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc     case ' ':
181*0a6a1f1dSLionel Sambuc       if (EscapeSpaces) os << "&nbsp;";
182*0a6a1f1dSLionel Sambuc       else os << ' ';
183*0a6a1f1dSLionel Sambuc       break;
184*0a6a1f1dSLionel Sambuc 
185*0a6a1f1dSLionel Sambuc     case '\t':
186*0a6a1f1dSLionel Sambuc       if (ReplaceTabs) {
187*0a6a1f1dSLionel Sambuc         if (EscapeSpaces)
188*0a6a1f1dSLionel Sambuc           for (unsigned i = 0; i < 4; ++i)
189*0a6a1f1dSLionel Sambuc             os << "&nbsp;";
190*0a6a1f1dSLionel Sambuc         else
191*0a6a1f1dSLionel Sambuc           for (unsigned i = 0; i < 4; ++i)
192*0a6a1f1dSLionel Sambuc             os << " ";
193*0a6a1f1dSLionel Sambuc       }
194*0a6a1f1dSLionel Sambuc       else
195*0a6a1f1dSLionel Sambuc         os << c;
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc       break;
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc     case '<': os << "&lt;"; break;
200*0a6a1f1dSLionel Sambuc     case '>': os << "&gt;"; break;
201*0a6a1f1dSLionel Sambuc     case '&': os << "&amp;"; break;
202*0a6a1f1dSLionel Sambuc     }
203*0a6a1f1dSLionel Sambuc   }
204*0a6a1f1dSLionel Sambuc 
205*0a6a1f1dSLionel Sambuc   return os.str();
206*0a6a1f1dSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc 
AddLineNumber(RewriteBuffer & RB,unsigned LineNo,unsigned B,unsigned E)208*0a6a1f1dSLionel Sambuc static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
209*0a6a1f1dSLionel Sambuc                           unsigned B, unsigned E) {
210*0a6a1f1dSLionel Sambuc   SmallString<256> Str;
211*0a6a1f1dSLionel Sambuc   llvm::raw_svector_ostream OS(Str);
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc   OS << "<tr><td class=\"num\" id=\"LN"
214*0a6a1f1dSLionel Sambuc      << LineNo << "\">"
215*0a6a1f1dSLionel Sambuc      << LineNo << "</td><td class=\"line\">";
216*0a6a1f1dSLionel Sambuc 
217*0a6a1f1dSLionel Sambuc   if (B == E) { // Handle empty lines.
218*0a6a1f1dSLionel Sambuc     OS << " </td></tr>";
219*0a6a1f1dSLionel Sambuc     RB.InsertTextBefore(B, OS.str());
220*0a6a1f1dSLionel Sambuc   } else {
221*0a6a1f1dSLionel Sambuc     RB.InsertTextBefore(B, OS.str());
222*0a6a1f1dSLionel Sambuc     RB.InsertTextBefore(E, "</td></tr>");
223*0a6a1f1dSLionel Sambuc   }
224*0a6a1f1dSLionel Sambuc }
225*0a6a1f1dSLionel Sambuc 
AddLineNumbers(Rewriter & R,FileID FID)226*0a6a1f1dSLionel Sambuc void html::AddLineNumbers(Rewriter& R, FileID FID) {
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
229*0a6a1f1dSLionel Sambuc   const char* FileBeg = Buf->getBufferStart();
230*0a6a1f1dSLionel Sambuc   const char* FileEnd = Buf->getBufferEnd();
231*0a6a1f1dSLionel Sambuc   const char* C = FileBeg;
232*0a6a1f1dSLionel Sambuc   RewriteBuffer &RB = R.getEditBuffer(FID);
233*0a6a1f1dSLionel Sambuc 
234*0a6a1f1dSLionel Sambuc   assert (C <= FileEnd);
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc   unsigned LineNo = 0;
237*0a6a1f1dSLionel Sambuc   unsigned FilePos = 0;
238*0a6a1f1dSLionel Sambuc 
239*0a6a1f1dSLionel Sambuc   while (C != FileEnd) {
240*0a6a1f1dSLionel Sambuc 
241*0a6a1f1dSLionel Sambuc     ++LineNo;
242*0a6a1f1dSLionel Sambuc     unsigned LineStartPos = FilePos;
243*0a6a1f1dSLionel Sambuc     unsigned LineEndPos = FileEnd - FileBeg;
244*0a6a1f1dSLionel Sambuc 
245*0a6a1f1dSLionel Sambuc     assert (FilePos <= LineEndPos);
246*0a6a1f1dSLionel Sambuc     assert (C < FileEnd);
247*0a6a1f1dSLionel Sambuc 
248*0a6a1f1dSLionel Sambuc     // Scan until the newline (or end-of-file).
249*0a6a1f1dSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc     while (C != FileEnd) {
251*0a6a1f1dSLionel Sambuc       char c = *C;
252*0a6a1f1dSLionel Sambuc       ++C;
253*0a6a1f1dSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc       if (c == '\n') {
255*0a6a1f1dSLionel Sambuc         LineEndPos = FilePos++;
256*0a6a1f1dSLionel Sambuc         break;
257*0a6a1f1dSLionel Sambuc       }
258*0a6a1f1dSLionel Sambuc 
259*0a6a1f1dSLionel Sambuc       ++FilePos;
260*0a6a1f1dSLionel Sambuc     }
261*0a6a1f1dSLionel Sambuc 
262*0a6a1f1dSLionel Sambuc     AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
263*0a6a1f1dSLionel Sambuc   }
264*0a6a1f1dSLionel Sambuc 
265*0a6a1f1dSLionel Sambuc   // Add one big table tag that surrounds all of the code.
266*0a6a1f1dSLionel Sambuc   RB.InsertTextBefore(0, "<table class=\"code\">\n");
267*0a6a1f1dSLionel Sambuc   RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
268*0a6a1f1dSLionel Sambuc }
269*0a6a1f1dSLionel Sambuc 
AddHeaderFooterInternalBuiltinCSS(Rewriter & R,FileID FID,const char * title)270*0a6a1f1dSLionel Sambuc void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
271*0a6a1f1dSLionel Sambuc                                              const char *title) {
272*0a6a1f1dSLionel Sambuc 
273*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
274*0a6a1f1dSLionel Sambuc   const char* FileStart = Buf->getBufferStart();
275*0a6a1f1dSLionel Sambuc   const char* FileEnd = Buf->getBufferEnd();
276*0a6a1f1dSLionel Sambuc 
277*0a6a1f1dSLionel Sambuc   SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
278*0a6a1f1dSLionel Sambuc   SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
279*0a6a1f1dSLionel Sambuc 
280*0a6a1f1dSLionel Sambuc   std::string s;
281*0a6a1f1dSLionel Sambuc   llvm::raw_string_ostream os(s);
282*0a6a1f1dSLionel Sambuc   os << "<!doctype html>\n" // Use HTML 5 doctype
283*0a6a1f1dSLionel Sambuc         "<html>\n<head>\n";
284*0a6a1f1dSLionel Sambuc 
285*0a6a1f1dSLionel Sambuc   if (title)
286*0a6a1f1dSLionel Sambuc     os << "<title>" << html::EscapeText(title) << "</title>\n";
287*0a6a1f1dSLionel Sambuc 
288*0a6a1f1dSLionel Sambuc   os << "<style type=\"text/css\">\n"
289*0a6a1f1dSLionel Sambuc       " body { color:#000000; background-color:#ffffff }\n"
290*0a6a1f1dSLionel Sambuc       " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
291*0a6a1f1dSLionel Sambuc       " h1 { font-size:14pt }\n"
292*0a6a1f1dSLionel Sambuc       " .code { border-collapse:collapse; width:100%; }\n"
293*0a6a1f1dSLionel Sambuc       " .code { font-family: \"Monospace\", monospace; font-size:10pt }\n"
294*0a6a1f1dSLionel Sambuc       " .code { line-height: 1.2em }\n"
295*0a6a1f1dSLionel Sambuc       " .comment { color: green; font-style: oblique }\n"
296*0a6a1f1dSLionel Sambuc       " .keyword { color: blue }\n"
297*0a6a1f1dSLionel Sambuc       " .string_literal { color: red }\n"
298*0a6a1f1dSLionel Sambuc       " .directive { color: darkmagenta }\n"
299*0a6a1f1dSLionel Sambuc       // Macro expansions.
300*0a6a1f1dSLionel Sambuc       " .expansion { display: none; }\n"
301*0a6a1f1dSLionel Sambuc       " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
302*0a6a1f1dSLionel Sambuc           "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
303*0a6a1f1dSLionel Sambuc           "  -webkit-border-radius:5px;  -webkit-box-shadow:1px 1px 7px #000; "
304*0a6a1f1dSLionel Sambuc           "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
305*0a6a1f1dSLionel Sambuc       " .macro { color: darkmagenta; background-color:LemonChiffon;"
306*0a6a1f1dSLionel Sambuc              // Macros are position: relative to provide base for expansions.
307*0a6a1f1dSLionel Sambuc              " position: relative }\n"
308*0a6a1f1dSLionel Sambuc       " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
309*0a6a1f1dSLionel Sambuc       " .num { text-align:right; font-size:8pt }\n"
310*0a6a1f1dSLionel Sambuc       " .num { color:#444444 }\n"
311*0a6a1f1dSLionel Sambuc       " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
312*0a6a1f1dSLionel Sambuc       " .line { white-space: pre }\n"
313*0a6a1f1dSLionel Sambuc       " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
314*0a6a1f1dSLionel Sambuc       " .msg { -webkit-border-radius:5px }\n"
315*0a6a1f1dSLionel Sambuc       " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
316*0a6a1f1dSLionel Sambuc       " .msg { float:left }\n"
317*0a6a1f1dSLionel Sambuc       " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
318*0a6a1f1dSLionel Sambuc       " .msg { margin-top:10px; margin-bottom:10px }\n"
319*0a6a1f1dSLionel Sambuc       " .msg { font-weight:bold }\n"
320*0a6a1f1dSLionel Sambuc       " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
321*0a6a1f1dSLionel Sambuc       " .msgT { padding:0x; spacing:0x }\n"
322*0a6a1f1dSLionel Sambuc       " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
323*0a6a1f1dSLionel Sambuc       " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
324*0a6a1f1dSLionel Sambuc       " .mrange { background-color:#dfddf3 }\n"
325*0a6a1f1dSLionel Sambuc       " .mrange { border-bottom:1px solid #6F9DBE }\n"
326*0a6a1f1dSLionel Sambuc       " .PathIndex { font-weight: bold; padding:0px 5px; "
327*0a6a1f1dSLionel Sambuc         "margin-right:5px; }\n"
328*0a6a1f1dSLionel Sambuc       " .PathIndex { -webkit-border-radius:8px }\n"
329*0a6a1f1dSLionel Sambuc       " .PathIndexEvent { background-color:#bfba87 }\n"
330*0a6a1f1dSLionel Sambuc       " .PathIndexControl { background-color:#8c8c8c }\n"
331*0a6a1f1dSLionel Sambuc       " .PathNav a { text-decoration:none; font-size: larger }\n"
332*0a6a1f1dSLionel Sambuc       " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
333*0a6a1f1dSLionel Sambuc       " .CodeRemovalHint { background-color:#de1010 }\n"
334*0a6a1f1dSLionel Sambuc       " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
335*0a6a1f1dSLionel Sambuc       " table.simpletable {\n"
336*0a6a1f1dSLionel Sambuc       "   padding: 5px;\n"
337*0a6a1f1dSLionel Sambuc       "   font-size:12pt;\n"
338*0a6a1f1dSLionel Sambuc       "   margin:20px;\n"
339*0a6a1f1dSLionel Sambuc       "   border-collapse: collapse; border-spacing: 0px;\n"
340*0a6a1f1dSLionel Sambuc       " }\n"
341*0a6a1f1dSLionel Sambuc       " td.rowname {\n"
342*0a6a1f1dSLionel Sambuc       "   text-align:right; font-weight:bold; color:#444444;\n"
343*0a6a1f1dSLionel Sambuc       "   padding-right:2ex; }\n"
344*0a6a1f1dSLionel Sambuc       "</style>\n</head>\n<body>";
345*0a6a1f1dSLionel Sambuc 
346*0a6a1f1dSLionel Sambuc   // Generate header
347*0a6a1f1dSLionel Sambuc   R.InsertTextBefore(StartLoc, os.str());
348*0a6a1f1dSLionel Sambuc   // Generate footer
349*0a6a1f1dSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc   R.InsertTextAfter(EndLoc, "</body></html>\n");
351*0a6a1f1dSLionel Sambuc }
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
354*0a6a1f1dSLionel Sambuc /// information about keywords, macro expansions etc.  This uses the macro
355*0a6a1f1dSLionel Sambuc /// table state from the end of the file, so it won't be perfectly perfect,
356*0a6a1f1dSLionel Sambuc /// but it will be reasonably close.
SyntaxHighlight(Rewriter & R,FileID FID,const Preprocessor & PP)357*0a6a1f1dSLionel Sambuc void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
358*0a6a1f1dSLionel Sambuc   RewriteBuffer &RB = R.getEditBuffer(FID);
359*0a6a1f1dSLionel Sambuc 
360*0a6a1f1dSLionel Sambuc   const SourceManager &SM = PP.getSourceManager();
361*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
362*0a6a1f1dSLionel Sambuc   Lexer L(FID, FromFile, SM, PP.getLangOpts());
363*0a6a1f1dSLionel Sambuc   const char *BufferStart = L.getBuffer().data();
364*0a6a1f1dSLionel Sambuc 
365*0a6a1f1dSLionel Sambuc   // Inform the preprocessor that we want to retain comments as tokens, so we
366*0a6a1f1dSLionel Sambuc   // can highlight them.
367*0a6a1f1dSLionel Sambuc   L.SetCommentRetentionState(true);
368*0a6a1f1dSLionel Sambuc 
369*0a6a1f1dSLionel Sambuc   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
370*0a6a1f1dSLionel Sambuc   // macros.
371*0a6a1f1dSLionel Sambuc   Token Tok;
372*0a6a1f1dSLionel Sambuc   L.LexFromRawLexer(Tok);
373*0a6a1f1dSLionel Sambuc 
374*0a6a1f1dSLionel Sambuc   while (Tok.isNot(tok::eof)) {
375*0a6a1f1dSLionel Sambuc     // Since we are lexing unexpanded tokens, all tokens are from the main
376*0a6a1f1dSLionel Sambuc     // FileID.
377*0a6a1f1dSLionel Sambuc     unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
378*0a6a1f1dSLionel Sambuc     unsigned TokLen = Tok.getLength();
379*0a6a1f1dSLionel Sambuc     switch (Tok.getKind()) {
380*0a6a1f1dSLionel Sambuc     default: break;
381*0a6a1f1dSLionel Sambuc     case tok::identifier:
382*0a6a1f1dSLionel Sambuc       llvm_unreachable("tok::identifier in raw lexing mode!");
383*0a6a1f1dSLionel Sambuc     case tok::raw_identifier: {
384*0a6a1f1dSLionel Sambuc       // Fill in Result.IdentifierInfo and update the token kind,
385*0a6a1f1dSLionel Sambuc       // looking up the identifier in the identifier table.
386*0a6a1f1dSLionel Sambuc       PP.LookUpIdentifierInfo(Tok);
387*0a6a1f1dSLionel Sambuc 
388*0a6a1f1dSLionel Sambuc       // If this is a pp-identifier, for a keyword, highlight it as such.
389*0a6a1f1dSLionel Sambuc       if (Tok.isNot(tok::identifier))
390*0a6a1f1dSLionel Sambuc         HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
391*0a6a1f1dSLionel Sambuc                        "<span class='keyword'>", "</span>");
392*0a6a1f1dSLionel Sambuc       break;
393*0a6a1f1dSLionel Sambuc     }
394*0a6a1f1dSLionel Sambuc     case tok::comment:
395*0a6a1f1dSLionel Sambuc       HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
396*0a6a1f1dSLionel Sambuc                      "<span class='comment'>", "</span>");
397*0a6a1f1dSLionel Sambuc       break;
398*0a6a1f1dSLionel Sambuc     case tok::utf8_string_literal:
399*0a6a1f1dSLionel Sambuc       // Chop off the u part of u8 prefix
400*0a6a1f1dSLionel Sambuc       ++TokOffs;
401*0a6a1f1dSLionel Sambuc       --TokLen;
402*0a6a1f1dSLionel Sambuc       // FALL THROUGH to chop the 8
403*0a6a1f1dSLionel Sambuc     case tok::wide_string_literal:
404*0a6a1f1dSLionel Sambuc     case tok::utf16_string_literal:
405*0a6a1f1dSLionel Sambuc     case tok::utf32_string_literal:
406*0a6a1f1dSLionel Sambuc       // Chop off the L, u, U or 8 prefix
407*0a6a1f1dSLionel Sambuc       ++TokOffs;
408*0a6a1f1dSLionel Sambuc       --TokLen;
409*0a6a1f1dSLionel Sambuc       // FALL THROUGH.
410*0a6a1f1dSLionel Sambuc     case tok::string_literal:
411*0a6a1f1dSLionel Sambuc       // FIXME: Exclude the optional ud-suffix from the highlighted range.
412*0a6a1f1dSLionel Sambuc       HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
413*0a6a1f1dSLionel Sambuc                      "<span class='string_literal'>", "</span>");
414*0a6a1f1dSLionel Sambuc       break;
415*0a6a1f1dSLionel Sambuc     case tok::hash: {
416*0a6a1f1dSLionel Sambuc       // If this is a preprocessor directive, all tokens to end of line are too.
417*0a6a1f1dSLionel Sambuc       if (!Tok.isAtStartOfLine())
418*0a6a1f1dSLionel Sambuc         break;
419*0a6a1f1dSLionel Sambuc 
420*0a6a1f1dSLionel Sambuc       // Eat all of the tokens until we get to the next one at the start of
421*0a6a1f1dSLionel Sambuc       // line.
422*0a6a1f1dSLionel Sambuc       unsigned TokEnd = TokOffs+TokLen;
423*0a6a1f1dSLionel Sambuc       L.LexFromRawLexer(Tok);
424*0a6a1f1dSLionel Sambuc       while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
425*0a6a1f1dSLionel Sambuc         TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
426*0a6a1f1dSLionel Sambuc         L.LexFromRawLexer(Tok);
427*0a6a1f1dSLionel Sambuc       }
428*0a6a1f1dSLionel Sambuc 
429*0a6a1f1dSLionel Sambuc       // Find end of line.  This is a hack.
430*0a6a1f1dSLionel Sambuc       HighlightRange(RB, TokOffs, TokEnd, BufferStart,
431*0a6a1f1dSLionel Sambuc                      "<span class='directive'>", "</span>");
432*0a6a1f1dSLionel Sambuc 
433*0a6a1f1dSLionel Sambuc       // Don't skip the next token.
434*0a6a1f1dSLionel Sambuc       continue;
435*0a6a1f1dSLionel Sambuc     }
436*0a6a1f1dSLionel Sambuc     }
437*0a6a1f1dSLionel Sambuc 
438*0a6a1f1dSLionel Sambuc     L.LexFromRawLexer(Tok);
439*0a6a1f1dSLionel Sambuc   }
440*0a6a1f1dSLionel Sambuc }
441*0a6a1f1dSLionel Sambuc 
442*0a6a1f1dSLionel Sambuc /// HighlightMacros - This uses the macro table state from the end of the
443*0a6a1f1dSLionel Sambuc /// file, to re-expand macros and insert (into the HTML) information about the
444*0a6a1f1dSLionel Sambuc /// macro expansions.  This won't be perfectly perfect, but it will be
445*0a6a1f1dSLionel Sambuc /// reasonably close.
HighlightMacros(Rewriter & R,FileID FID,const Preprocessor & PP)446*0a6a1f1dSLionel Sambuc void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
447*0a6a1f1dSLionel Sambuc   // Re-lex the raw token stream into a token buffer.
448*0a6a1f1dSLionel Sambuc   const SourceManager &SM = PP.getSourceManager();
449*0a6a1f1dSLionel Sambuc   std::vector<Token> TokenStream;
450*0a6a1f1dSLionel Sambuc 
451*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
452*0a6a1f1dSLionel Sambuc   Lexer L(FID, FromFile, SM, PP.getLangOpts());
453*0a6a1f1dSLionel Sambuc 
454*0a6a1f1dSLionel Sambuc   // Lex all the tokens in raw mode, to avoid entering #includes or expanding
455*0a6a1f1dSLionel Sambuc   // macros.
456*0a6a1f1dSLionel Sambuc   while (1) {
457*0a6a1f1dSLionel Sambuc     Token Tok;
458*0a6a1f1dSLionel Sambuc     L.LexFromRawLexer(Tok);
459*0a6a1f1dSLionel Sambuc 
460*0a6a1f1dSLionel Sambuc     // If this is a # at the start of a line, discard it from the token stream.
461*0a6a1f1dSLionel Sambuc     // We don't want the re-preprocess step to see #defines, #includes or other
462*0a6a1f1dSLionel Sambuc     // preprocessor directives.
463*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
464*0a6a1f1dSLionel Sambuc       continue;
465*0a6a1f1dSLionel Sambuc 
466*0a6a1f1dSLionel Sambuc     // If this is a ## token, change its kind to unknown so that repreprocessing
467*0a6a1f1dSLionel Sambuc     // it will not produce an error.
468*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::hashhash))
469*0a6a1f1dSLionel Sambuc       Tok.setKind(tok::unknown);
470*0a6a1f1dSLionel Sambuc 
471*0a6a1f1dSLionel Sambuc     // If this raw token is an identifier, the raw lexer won't have looked up
472*0a6a1f1dSLionel Sambuc     // the corresponding identifier info for it.  Do this now so that it will be
473*0a6a1f1dSLionel Sambuc     // macro expanded when we re-preprocess it.
474*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::raw_identifier))
475*0a6a1f1dSLionel Sambuc       PP.LookUpIdentifierInfo(Tok);
476*0a6a1f1dSLionel Sambuc 
477*0a6a1f1dSLionel Sambuc     TokenStream.push_back(Tok);
478*0a6a1f1dSLionel Sambuc 
479*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::eof)) break;
480*0a6a1f1dSLionel Sambuc   }
481*0a6a1f1dSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc   // Temporarily change the diagnostics object so that we ignore any generated
483*0a6a1f1dSLionel Sambuc   // diagnostics from this pass.
484*0a6a1f1dSLionel Sambuc   DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
485*0a6a1f1dSLionel Sambuc                              &PP.getDiagnostics().getDiagnosticOptions(),
486*0a6a1f1dSLionel Sambuc                       new IgnoringDiagConsumer);
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc   // FIXME: This is a huge hack; we reuse the input preprocessor because we want
489*0a6a1f1dSLionel Sambuc   // its state, but we aren't actually changing it (we hope). This should really
490*0a6a1f1dSLionel Sambuc   // construct a copy of the preprocessor.
491*0a6a1f1dSLionel Sambuc   Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
492*0a6a1f1dSLionel Sambuc   DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
493*0a6a1f1dSLionel Sambuc   TmpPP.setDiagnostics(TmpDiags);
494*0a6a1f1dSLionel Sambuc 
495*0a6a1f1dSLionel Sambuc   // Inform the preprocessor that we don't want comments.
496*0a6a1f1dSLionel Sambuc   TmpPP.SetCommentRetentionState(false, false);
497*0a6a1f1dSLionel Sambuc 
498*0a6a1f1dSLionel Sambuc   // We don't want pragmas either. Although we filtered out #pragma, removing
499*0a6a1f1dSLionel Sambuc   // _Pragma and __pragma is much harder.
500*0a6a1f1dSLionel Sambuc   bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
501*0a6a1f1dSLionel Sambuc   TmpPP.setPragmasEnabled(false);
502*0a6a1f1dSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc   // Enter the tokens we just lexed.  This will cause them to be macro expanded
504*0a6a1f1dSLionel Sambuc   // but won't enter sub-files (because we removed #'s).
505*0a6a1f1dSLionel Sambuc   TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
506*0a6a1f1dSLionel Sambuc 
507*0a6a1f1dSLionel Sambuc   TokenConcatenation ConcatInfo(TmpPP);
508*0a6a1f1dSLionel Sambuc 
509*0a6a1f1dSLionel Sambuc   // Lex all the tokens.
510*0a6a1f1dSLionel Sambuc   Token Tok;
511*0a6a1f1dSLionel Sambuc   TmpPP.Lex(Tok);
512*0a6a1f1dSLionel Sambuc   while (Tok.isNot(tok::eof)) {
513*0a6a1f1dSLionel Sambuc     // Ignore non-macro tokens.
514*0a6a1f1dSLionel Sambuc     if (!Tok.getLocation().isMacroID()) {
515*0a6a1f1dSLionel Sambuc       TmpPP.Lex(Tok);
516*0a6a1f1dSLionel Sambuc       continue;
517*0a6a1f1dSLionel Sambuc     }
518*0a6a1f1dSLionel Sambuc 
519*0a6a1f1dSLionel Sambuc     // Okay, we have the first token of a macro expansion: highlight the
520*0a6a1f1dSLionel Sambuc     // expansion by inserting a start tag before the macro expansion and
521*0a6a1f1dSLionel Sambuc     // end tag after it.
522*0a6a1f1dSLionel Sambuc     std::pair<SourceLocation, SourceLocation> LLoc =
523*0a6a1f1dSLionel Sambuc       SM.getExpansionRange(Tok.getLocation());
524*0a6a1f1dSLionel Sambuc 
525*0a6a1f1dSLionel Sambuc     // Ignore tokens whose instantiation location was not the main file.
526*0a6a1f1dSLionel Sambuc     if (SM.getFileID(LLoc.first) != FID) {
527*0a6a1f1dSLionel Sambuc       TmpPP.Lex(Tok);
528*0a6a1f1dSLionel Sambuc       continue;
529*0a6a1f1dSLionel Sambuc     }
530*0a6a1f1dSLionel Sambuc 
531*0a6a1f1dSLionel Sambuc     assert(SM.getFileID(LLoc.second) == FID &&
532*0a6a1f1dSLionel Sambuc            "Start and end of expansion must be in the same ultimate file!");
533*0a6a1f1dSLionel Sambuc 
534*0a6a1f1dSLionel Sambuc     std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
535*0a6a1f1dSLionel Sambuc     unsigned LineLen = Expansion.size();
536*0a6a1f1dSLionel Sambuc 
537*0a6a1f1dSLionel Sambuc     Token PrevPrevTok;
538*0a6a1f1dSLionel Sambuc     Token PrevTok = Tok;
539*0a6a1f1dSLionel Sambuc     // Okay, eat this token, getting the next one.
540*0a6a1f1dSLionel Sambuc     TmpPP.Lex(Tok);
541*0a6a1f1dSLionel Sambuc 
542*0a6a1f1dSLionel Sambuc     // Skip all the rest of the tokens that are part of this macro
543*0a6a1f1dSLionel Sambuc     // instantiation.  It would be really nice to pop up a window with all the
544*0a6a1f1dSLionel Sambuc     // spelling of the tokens or something.
545*0a6a1f1dSLionel Sambuc     while (!Tok.is(tok::eof) &&
546*0a6a1f1dSLionel Sambuc            SM.getExpansionLoc(Tok.getLocation()) == LLoc.first) {
547*0a6a1f1dSLionel Sambuc       // Insert a newline if the macro expansion is getting large.
548*0a6a1f1dSLionel Sambuc       if (LineLen > 60) {
549*0a6a1f1dSLionel Sambuc         Expansion += "<br>";
550*0a6a1f1dSLionel Sambuc         LineLen = 0;
551*0a6a1f1dSLionel Sambuc       }
552*0a6a1f1dSLionel Sambuc 
553*0a6a1f1dSLionel Sambuc       LineLen -= Expansion.size();
554*0a6a1f1dSLionel Sambuc 
555*0a6a1f1dSLionel Sambuc       // If the tokens were already space separated, or if they must be to avoid
556*0a6a1f1dSLionel Sambuc       // them being implicitly pasted, add a space between them.
557*0a6a1f1dSLionel Sambuc       if (Tok.hasLeadingSpace() ||
558*0a6a1f1dSLionel Sambuc           ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
559*0a6a1f1dSLionel Sambuc         Expansion += ' ';
560*0a6a1f1dSLionel Sambuc 
561*0a6a1f1dSLionel Sambuc       // Escape any special characters in the token text.
562*0a6a1f1dSLionel Sambuc       Expansion += EscapeText(TmpPP.getSpelling(Tok));
563*0a6a1f1dSLionel Sambuc       LineLen += Expansion.size();
564*0a6a1f1dSLionel Sambuc 
565*0a6a1f1dSLionel Sambuc       PrevPrevTok = PrevTok;
566*0a6a1f1dSLionel Sambuc       PrevTok = Tok;
567*0a6a1f1dSLionel Sambuc       TmpPP.Lex(Tok);
568*0a6a1f1dSLionel Sambuc     }
569*0a6a1f1dSLionel Sambuc 
570*0a6a1f1dSLionel Sambuc 
571*0a6a1f1dSLionel Sambuc     // Insert the expansion as the end tag, so that multi-line macros all get
572*0a6a1f1dSLionel Sambuc     // highlighted.
573*0a6a1f1dSLionel Sambuc     Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
574*0a6a1f1dSLionel Sambuc 
575*0a6a1f1dSLionel Sambuc     HighlightRange(R, LLoc.first, LLoc.second,
576*0a6a1f1dSLionel Sambuc                    "<span class='macro'>", Expansion.c_str());
577*0a6a1f1dSLionel Sambuc   }
578*0a6a1f1dSLionel Sambuc 
579*0a6a1f1dSLionel Sambuc   // Restore the preprocessor's old state.
580*0a6a1f1dSLionel Sambuc   TmpPP.setDiagnostics(*OldDiags);
581*0a6a1f1dSLionel Sambuc   TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
582*0a6a1f1dSLionel Sambuc }
583