blob: 275fbd0ebca21c0490655b439827d3905f5db92d [file] [log] [blame]
Ted Kremenek7dcc8222008-03-18 21:19:49 +00001//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alp Tokerf6a24ce2013-12-05 16:25:25 +000010// This file defines the HTMLRewriter class, which is used to translate the
Ted Kremenek7dcc8222008-03-18 21:19:49 +000011// text of a source file into prettified HTML.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekcdf81492012-09-01 05:09:24 +000015#include "clang/Rewrite/Core/HTMLRewrite.h"
Ted Kremenek7dcc8222008-03-18 21:19:49 +000016#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/TokenConcatenation.h"
19#include "clang/Rewrite/Core/Rewriter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "llvm/ADT/SmallString.h"
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +000021#include "llvm/Support/ErrorHandling.h"
Ted Kremenek7dcc8222008-03-18 21:19:49 +000022#include "llvm/Support/MemoryBuffer.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000023#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000024#include <memory>
Ted Kremenek7dcc8222008-03-18 21:19:49 +000025using namespace clang;
26
Chris Lattner10dbbb02008-04-16 23:06:45 +000027
Chris Lattner8e3006a2008-04-16 22:45:51 +000028/// HighlightRange - Highlight a range in the source code with the specified
29/// start/end tags. B/E must be in the same file. This ensures that
30/// start/end tags are placed at the start/end of each line if the range is
31/// multiline.
32void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
33 const char *StartTag, const char *EndTag) {
34 SourceManager &SM = R.getSourceMgr();
Chandler Carruth35f53202011-07-25 16:49:02 +000035 B = SM.getExpansionLoc(B);
36 E = SM.getExpansionLoc(E);
Chris Lattnercbc35ecb2009-01-19 07:46:45 +000037 FileID FID = SM.getFileID(B);
38 assert(SM.getFileID(E) == FID && "B/E not in the same file!");
Chris Lattner8e3006a2008-04-16 22:45:51 +000039
Chris Lattnerc360bf22009-01-27 06:27:13 +000040 unsigned BOffset = SM.getFileOffset(B);
41 unsigned EOffset = SM.getFileOffset(E);
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattner8e3006a2008-04-16 22:45:51 +000043 // Include the whole end token in the range.
Chris Lattner184e65d2009-04-14 23:22:57 +000044 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
Mike Stump11289f42009-09-09 15:08:12 +000045
Douglas Gregore0fbb832010-03-16 00:06:06 +000046 bool Invalid = false;
Benjamin Kramereb92dc02010-03-16 14:14:31 +000047 const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
Douglas Gregore0fbb832010-03-16 00:06:06 +000048 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +000049 return;
Douglas Gregor802b7762010-03-15 22:54:52 +000050
Chris Lattnerd32480d2009-01-17 06:22:33 +000051 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
Douglas Gregor802b7762010-03-15 22:54:52 +000052 BufferStart, StartTag, EndTag);
Chris Lattner8e3006a2008-04-16 22:45:51 +000053}
54
55/// HighlightRange - This is the same as the above method, but takes
56/// decomposed file locations.
57void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
58 const char *BufferStart,
59 const char *StartTag, const char *EndTag) {
Chris Lattner10dbbb02008-04-16 23:06:45 +000060 // Insert the tag at the absolute start/end of the range.
Daniel Dunbardec484a2009-08-19 19:10:30 +000061 RB.InsertTextAfter(B, StartTag);
62 RB.InsertTextBefore(E, EndTag);
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattner10dbbb02008-04-16 23:06:45 +000064 // Scan the range to see if there is a \r or \n. If so, and if the line is
65 // not blank, insert tags on that line as well.
66 bool HadOpenTag = true;
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattner10dbbb02008-04-16 23:06:45 +000068 unsigned LastNonWhiteSpace = B;
69 for (unsigned i = B; i != E; ++i) {
70 switch (BufferStart[i]) {
71 case '\r':
72 case '\n':
73 // Okay, we found a newline in the range. If we have an open tag, we need
74 // to insert a close tag at the first non-whitespace before the newline.
75 if (HadOpenTag)
Daniel Dunbardec484a2009-08-19 19:10:30 +000076 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner10dbbb02008-04-16 23:06:45 +000078 // Instead of inserting an open tag immediately after the newline, we
79 // wait until we see a non-whitespace character. This prevents us from
80 // inserting tags around blank lines, and also allows the open tag to
81 // be put *after* whitespace on a non-blank line.
82 HadOpenTag = false;
83 break;
84 case '\0':
85 case ' ':
86 case '\t':
87 case '\f':
88 case '\v':
89 // Ignore whitespace.
90 break;
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattner10dbbb02008-04-16 23:06:45 +000092 default:
93 // If there is no tag open, do it now.
94 if (!HadOpenTag) {
Daniel Dunbardec484a2009-08-19 19:10:30 +000095 RB.InsertTextAfter(i, StartTag);
Chris Lattner10dbbb02008-04-16 23:06:45 +000096 HadOpenTag = true;
97 }
Mike Stump11289f42009-09-09 15:08:12 +000098
Chris Lattner10dbbb02008-04-16 23:06:45 +000099 // Remember this character.
100 LastNonWhiteSpace = i;
101 break;
102 }
103 }
Chris Lattner8e3006a2008-04-16 22:45:51 +0000104}
105
Chris Lattnerd32480d2009-01-17 06:22:33 +0000106void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenek5f7ece02008-04-08 22:37:58 +0000107 bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattnerd32480d2009-01-17 06:22:33 +0000109 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek7dcc8222008-03-18 21:19:49 +0000110 const char* C = Buf->getBufferStart();
111 const char* FileEnd = Buf->getBufferEnd();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek7dcc8222008-03-18 21:19:49 +0000113 assert (C <= FileEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattnerd32480d2009-01-17 06:22:33 +0000115 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner4bbd1642008-04-18 04:54:20 +0000116
117 unsigned ColNo = 0;
Ted Kremenek7dcc8222008-03-18 21:19:49 +0000118 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek7dcc8222008-03-18 21:19:49 +0000119 switch (*C) {
Chris Lattner4bbd1642008-04-18 04:54:20 +0000120 default: ++ColNo; break;
121 case '\n':
122 case '\r':
123 ColNo = 0;
124 break;
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattnere7d696e2008-04-16 04:33:23 +0000126 case ' ':
127 if (EscapeSpaces)
Daniel Dunbardec484a2009-08-19 19:10:30 +0000128 RB.ReplaceText(FilePos, 1, "&nbsp;");
Chris Lattner4bbd1642008-04-18 04:54:20 +0000129 ++ColNo;
Chris Lattnere7d696e2008-04-16 04:33:23 +0000130 break;
Chris Lattner1e517a72008-04-19 23:56:30 +0000131 case '\f':
Daniel Dunbardec484a2009-08-19 19:10:30 +0000132 RB.ReplaceText(FilePos, 1, "<hr>");
Chris Lattner1e517a72008-04-19 23:56:30 +0000133 ColNo = 0;
134 break;
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattner4bbd1642008-04-18 04:54:20 +0000136 case '\t': {
Chris Lattnere7d696e2008-04-16 04:33:23 +0000137 if (!ReplaceTabs)
Ted Kremenekc5991792008-04-03 07:12:29 +0000138 break;
Chris Lattner4bbd1642008-04-18 04:54:20 +0000139 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattnere7d696e2008-04-16 04:33:23 +0000140 if (EscapeSpaces)
Daniel Dunbardec484a2009-08-19 19:10:30 +0000141 RB.ReplaceText(FilePos, 1,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Daniel Dunbardec484a2009-08-19 19:10:30 +0000143 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
Chris Lattnere7d696e2008-04-16 04:33:23 +0000144 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000145 RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces));
Chris Lattner4bbd1642008-04-18 04:54:20 +0000146 ColNo += NumSpaces;
Chris Lattnere7d696e2008-04-16 04:33:23 +0000147 break;
Chris Lattner4bbd1642008-04-18 04:54:20 +0000148 }
Chris Lattnere7d696e2008-04-16 04:33:23 +0000149 case '<':
Daniel Dunbardec484a2009-08-19 19:10:30 +0000150 RB.ReplaceText(FilePos, 1, "&lt;");
Chris Lattner4bbd1642008-04-18 04:54:20 +0000151 ++ColNo;
Chris Lattnere7d696e2008-04-16 04:33:23 +0000152 break;
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattnere7d696e2008-04-16 04:33:23 +0000154 case '>':
Daniel Dunbardec484a2009-08-19 19:10:30 +0000155 RB.ReplaceText(FilePos, 1, "&gt;");
Chris Lattner4bbd1642008-04-18 04:54:20 +0000156 ++ColNo;
Chris Lattnere7d696e2008-04-16 04:33:23 +0000157 break;
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnere7d696e2008-04-16 04:33:23 +0000159 case '&':
Daniel Dunbardec484a2009-08-19 19:10:30 +0000160 RB.ReplaceText(FilePos, 1, "&amp;");
Chris Lattner4bbd1642008-04-18 04:54:20 +0000161 ++ColNo;
Chris Lattnere7d696e2008-04-16 04:33:23 +0000162 break;
Ted Kremenek7dcc8222008-03-18 21:19:49 +0000163 }
164 }
165}
166
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000167std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenek561dfe32008-03-27 17:15:29 +0000169 unsigned len = s.size();
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000170 std::string Str;
171 llvm::raw_string_ostream os(Str);
Mike Stump11289f42009-09-09 15:08:12 +0000172
Ted Kremenek561dfe32008-03-27 17:15:29 +0000173 for (unsigned i = 0 ; i < len; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000174
Ted Kremenek561dfe32008-03-27 17:15:29 +0000175 char c = s[i];
Ted Kremenek561dfe32008-03-27 17:15:29 +0000176 switch (c) {
Chris Lattnerc326f7e2008-04-16 04:37:29 +0000177 default:
178 os << c; break;
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattnerc326f7e2008-04-16 04:37:29 +0000180 case ' ':
181 if (EscapeSpaces) os << "&nbsp;";
182 else os << ' ';
183 break;
Mike Stump11289f42009-09-09 15:08:12 +0000184
Zhongxing Xub5624442009-08-17 14:13:14 +0000185 case '\t':
186 if (ReplaceTabs) {
187 if (EscapeSpaces)
188 for (unsigned i = 0; i < 4; ++i)
189 os << "&nbsp;";
190 else
191 for (unsigned i = 0; i < 4; ++i)
192 os << " ";
193 }
Mike Stump11289f42009-09-09 15:08:12 +0000194 else
Zhongxing Xub5624442009-08-17 14:13:14 +0000195 os << c;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Zhongxing Xub5624442009-08-17 14:13:14 +0000197 break;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Zhongxing Xub5624442009-08-17 14:13:14 +0000199 case '<': os << "&lt;"; break;
200 case '>': os << "&gt;"; break;
201 case '&': os << "&amp;"; break;
Ted Kremenek561dfe32008-03-27 17:15:29 +0000202 }
203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Ted Kremenek561dfe32008-03-27 17:15:29 +0000205 return os.str();
206}
207
Chris Lattnerc326f7e2008-04-16 04:37:29 +0000208static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
209 unsigned B, unsigned E) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000210 SmallString<256> Str;
Daniel Dunbar69c300d2009-08-19 18:30:37 +0000211 llvm::raw_svector_ostream OS(Str);
212
213 OS << "<tr><td class=\"num\" id=\"LN"
214 << LineNo << "\">"
215 << LineNo << "</td><td class=\"line\">";
Mike Stump11289f42009-09-09 15:08:12 +0000216
Ted Kremenekc5991792008-04-03 07:12:29 +0000217 if (B == E) { // Handle empty lines.
Daniel Dunbar69c300d2009-08-19 18:30:37 +0000218 OS << " </td></tr>";
Daniel Dunbardec484a2009-08-19 19:10:30 +0000219 RB.InsertTextBefore(B, OS.str());
Chris Lattner05e53102008-04-16 04:11:35 +0000220 } else {
Daniel Dunbardec484a2009-08-19 19:10:30 +0000221 RB.InsertTextBefore(B, OS.str());
222 RB.InsertTextBefore(E, "</td></tr>");
Ted Kremenekc5991792008-04-03 07:12:29 +0000223 }
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000224}
225
Chris Lattnerd32480d2009-01-17 06:22:33 +0000226void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000227
Chris Lattnerd32480d2009-01-17 06:22:33 +0000228 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000229 const char* FileBeg = Buf->getBufferStart();
230 const char* FileEnd = Buf->getBufferEnd();
231 const char* C = FileBeg;
Chris Lattnerd32480d2009-01-17 06:22:33 +0000232 RewriteBuffer &RB = R.getEditBuffer(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000234 assert (C <= FileEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000235
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000236 unsigned LineNo = 0;
237 unsigned FilePos = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000238
239 while (C != FileEnd) {
240
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000241 ++LineNo;
242 unsigned LineStartPos = FilePos;
243 unsigned LineEndPos = FileEnd - FileBeg;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000245 assert (FilePos <= LineEndPos);
246 assert (C < FileEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000248 // Scan until the newline (or end-of-file).
Mike Stump11289f42009-09-09 15:08:12 +0000249
Ted Kremenekc5991792008-04-03 07:12:29 +0000250 while (C != FileEnd) {
251 char c = *C;
252 ++C;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenekc5991792008-04-03 07:12:29 +0000254 if (c == '\n') {
255 LineEndPos = FilePos++;
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000256 break;
257 }
Mike Stump11289f42009-09-09 15:08:12 +0000258
Ted Kremenekc5991792008-04-03 07:12:29 +0000259 ++FilePos;
260 }
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattnerc326f7e2008-04-16 04:37:29 +0000262 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenek3f839262008-03-19 05:07:26 +0000263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Chris Lattnerc326f7e2008-04-16 04:37:29 +0000265 // Add one big table tag that surrounds all of the code.
Daniel Dunbardec484a2009-08-19 19:10:30 +0000266 RB.InsertTextBefore(0, "<table class=\"code\">\n");
267 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
Ted Kremenek5792ceb2008-03-18 23:08:51 +0000268}
Ted Kremenek966da4a2008-03-19 06:14:37 +0000269
Mike Stump11289f42009-09-09 15:08:12 +0000270void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000271 const char *title) {
Ted Kremenek966da4a2008-03-19 06:14:37 +0000272
Chris Lattnerd32480d2009-01-17 06:22:33 +0000273 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek966da4a2008-03-19 06:14:37 +0000274 const char* FileStart = Buf->getBufferStart();
275 const char* FileEnd = Buf->getBufferEnd();
276
Chris Lattnerd32480d2009-01-17 06:22:33 +0000277 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000278 SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
Ted Kremenek966da4a2008-03-19 06:14:37 +0000279
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000280 std::string s;
281 llvm::raw_string_ostream os(s);
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000282 os << "<!doctype html>\n" // Use HTML 5 doctype
283 "<html>\n<head>\n";
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000285 if (title)
286 os << "<title>" << html::EscapeText(title) << "</title>\n";
Mike Stump11289f42009-09-09 15:08:12 +0000287
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000288 os << "<style type=\"text/css\">\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000289 " body { color:#000000; background-color:#ffffff }\n"
290 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek7d6219f2008-04-15 21:25:08 +0000291 " h1 { font-size:14pt }\n"
Ted Kremeneka821e082008-04-18 02:12:39 +0000292 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek6b333562012-01-20 20:39:55 +0000293 " .code { font-family: \"Monospace\", monospace; font-size:10pt }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000294 " .code { line-height: 1.2em }\n"
Ted Kremeneka821e082008-04-18 02:12:39 +0000295 " .comment { color: green; font-style: oblique }\n"
296 " .keyword { color: blue }\n"
Ted Kremenekafe2a9f2008-08-31 16:37:56 +0000297 " .string_literal { color: red }\n"
Ted Kremeneka821e082008-04-18 02:12:39 +0000298 " .directive { color: darkmagenta }\n"
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000299 // Macro expansions.
Ted Kremenekc2c08ca2008-04-17 19:57:27 +0000300 " .expansion { display: none; }\n"
301 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattner6929bd82008-04-17 21:32:46 +0000302 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000303 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattnerf2191262008-04-17 21:28:41 +0000304 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremeneka821e082008-04-18 02:12:39 +0000305 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000306 // Macros are position: relative to provide base for expansions.
307 " position: relative }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000308 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremeneka039a622009-03-10 05:14:32 +0000309 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000310 " .num { color:#444444 }\n"
311 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
312 " .line { white-space: pre }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000313 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
314 " .msg { -webkit-border-radius:5px }\n"
Ted Kremeneka039a622009-03-10 05:14:32 +0000315 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000316 " .msg { float:left }\n"
Ted Kremenek6c6a0982009-03-03 03:00:21 +0000317 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000318 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremeneke7808232009-03-02 21:42:01 +0000319 " .msg { font-weight:bold }\n"
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000320 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
321 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremeneke7808232009-03-02 21:42:01 +0000322 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000323 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenekf17d5752008-04-09 15:40:40 +0000324 " .mrange { background-color:#dfddf3 }\n"
325 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Jordan Rose11790a42012-08-02 02:26:19 +0000326 " .PathIndex { font-weight: bold; padding:0px 5px; "
Ted Kremenek6c6a0982009-03-03 03:00:21 +0000327 "margin-right:5px; }\n"
Ted Kremenekccca5c32009-03-02 23:39:27 +0000328 " .PathIndex { -webkit-border-radius:8px }\n"
329 " .PathIndexEvent { background-color:#bfba87 }\n"
330 " .PathIndexControl { background-color:#8c8c8c }\n"
Jordan Rose11790a42012-08-02 02:26:19 +0000331 " .PathNav a { text-decoration:none; font-size: larger }\n"
Douglas Gregor87f95b02009-02-26 21:00:50 +0000332 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
333 " .CodeRemovalHint { background-color:#de1010 }\n"
334 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek7d6219f2008-04-15 21:25:08 +0000335 " table.simpletable {\n"
336 " padding: 5px;\n"
337 " font-size:12pt;\n"
338 " margin:20px;\n"
339 " border-collapse: collapse; border-spacing: 0px;\n"
340 " }\n"
341 " td.rowname {\n"
342 " text-align:right; font-weight:bold; color:#444444;\n"
343 " padding-right:2ex; }\n"
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000344 "</style>\n</head>\n<body>";
Ted Kremenekf17d5752008-04-09 15:40:40 +0000345
Ted Kremenek79ab0fa2008-07-07 18:31:05 +0000346 // Generate header
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000347 R.InsertTextBefore(StartLoc, os.str());
Ted Kremenek966da4a2008-03-19 06:14:37 +0000348 // Generate footer
Mike Stump11289f42009-09-09 15:08:12 +0000349
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000350 R.InsertTextAfter(EndLoc, "</body></html>\n");
Ted Kremenek966da4a2008-03-19 06:14:37 +0000351}
Chris Lattner060d8aa2008-04-16 06:11:58 +0000352
353/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
354/// information about keywords, macro expansions etc. This uses the macro
355/// table state from the end of the file, so it won't be perfectly perfect,
356/// but it will be reasonably close.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000357void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000358 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner060d8aa2008-04-16 06:11:58 +0000359
Chris Lattner9ef847b2009-02-13 19:33:24 +0000360 const SourceManager &SM = PP.getSourceManager();
Chris Lattner710bb872009-11-30 04:18:44 +0000361 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000362 Lexer L(FID, FromFile, SM, PP.getLangOpts());
Alexander Kornienko33a35df2013-06-04 16:58:03 +0000363 const char *BufferStart = L.getBuffer().data();
Mike Stump11289f42009-09-09 15:08:12 +0000364
365 // Inform the preprocessor that we want to retain comments as tokens, so we
Chris Lattner060d8aa2008-04-16 06:11:58 +0000366 // can highlight them.
Chris Lattnere9786c32008-04-16 20:54:51 +0000367 L.SetCommentRetentionState(true);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnera5a75e72008-04-16 06:32:08 +0000369 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
370 // macros.
Chris Lattner060d8aa2008-04-16 06:11:58 +0000371 Token Tok;
Chris Lattner50c90502008-10-12 01:15:46 +0000372 L.LexFromRawLexer(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000373
Chris Lattner03b8fcc2008-04-16 06:53:09 +0000374 while (Tok.isNot(tok::eof)) {
375 // Since we are lexing unexpanded tokens, all tokens are from the main
376 // FileID.
Chris Lattner9ef847b2009-02-13 19:33:24 +0000377 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner060d8aa2008-04-16 06:11:58 +0000378 unsigned TokLen = Tok.getLength();
379 switch (Tok.getKind()) {
Chris Lattner0bb0e7e2008-04-16 20:51:51 +0000380 default: break;
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000381 case tok::identifier:
382 llvm_unreachable("tok::identifier in raw lexing mode!");
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000383 case tok::raw_identifier: {
384 // Fill in Result.IdentifierInfo and update the token kind,
385 // looking up the identifier in the identifier table.
386 PP.LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Chris Lattner0bb0e7e2008-04-16 20:51:51 +0000388 // If this is a pp-identifier, for a keyword, highlight it as such.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000389 if (Tok.isNot(tok::identifier))
Chris Lattner8e3006a2008-04-16 22:45:51 +0000390 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
391 "<span class='keyword'>", "</span>");
Chris Lattner5e69a2d2008-04-16 06:35:07 +0000392 break;
Chris Lattner0bb0e7e2008-04-16 20:51:51 +0000393 }
Chris Lattner060d8aa2008-04-16 06:11:58 +0000394 case tok::comment:
Chris Lattner8e3006a2008-04-16 22:45:51 +0000395 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
396 "<span class='comment'>", "</span>");
Chris Lattner060d8aa2008-04-16 06:11:58 +0000397 break;
Douglas Gregorfb65e592011-07-27 05:40:30 +0000398 case tok::utf8_string_literal:
399 // Chop off the u part of u8 prefix
400 ++TokOffs;
401 --TokLen;
402 // FALL THROUGH to chop the 8
Ted Kremenekafe2a9f2008-08-31 16:37:56 +0000403 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000404 case tok::utf16_string_literal:
405 case tok::utf32_string_literal:
406 // Chop off the L, u, U or 8 prefix
Ted Kremenekafe2a9f2008-08-31 16:37:56 +0000407 ++TokOffs;
408 --TokLen;
409 // FALL THROUGH.
410 case tok::string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +0000411 // FIXME: Exclude the optional ud-suffix from the highlighted range.
Ted Kremenekafe2a9f2008-08-31 16:37:56 +0000412 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
413 "<span class='string_literal'>", "</span>");
414 break;
Chris Lattner775fdd32008-04-16 23:21:17 +0000415 case tok::hash: {
Chris Lattner03b8fcc2008-04-16 06:53:09 +0000416 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner775fdd32008-04-16 23:21:17 +0000417 if (!Tok.isAtStartOfLine())
418 break;
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner775fdd32008-04-16 23:21:17 +0000420 // Eat all of the tokens until we get to the next one at the start of
421 // line.
422 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner50c90502008-10-12 01:15:46 +0000423 L.LexFromRawLexer(Tok);
Chris Lattner775fdd32008-04-16 23:21:17 +0000424 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner9ef847b2009-02-13 19:33:24 +0000425 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner50c90502008-10-12 01:15:46 +0000426 L.LexFromRawLexer(Tok);
Chris Lattner03b8fcc2008-04-16 06:53:09 +0000427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner775fdd32008-04-16 23:21:17 +0000429 // Find end of line. This is a hack.
430 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
431 "<span class='directive'>", "</span>");
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattner775fdd32008-04-16 23:21:17 +0000433 // Don't skip the next token.
434 continue;
435 }
Chris Lattner060d8aa2008-04-16 06:11:58 +0000436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner50c90502008-10-12 01:15:46 +0000438 L.LexFromRawLexer(Tok);
Chris Lattner03b8fcc2008-04-16 06:53:09 +0000439 }
Chris Lattner060d8aa2008-04-16 06:11:58 +0000440}
Chris Lattnera5a75e72008-04-16 06:32:08 +0000441
442/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner9ef847b2009-02-13 19:33:24 +0000443/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnera5a75e72008-04-16 06:32:08 +0000444/// macro expansions. This won't be perfectly perfect, but it will be
445/// reasonably close.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000446void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
Chris Lattner9ef847b2009-02-13 19:33:24 +0000447 // Re-lex the raw token stream into a token buffer.
448 const SourceManager &SM = PP.getSourceManager();
449 std::vector<Token> TokenStream;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattner710bb872009-11-30 04:18:44 +0000451 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000452 Lexer L(FID, FromFile, SM, PP.getLangOpts());
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattner9ef847b2009-02-13 19:33:24 +0000454 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
455 // macros.
456 while (1) {
457 Token Tok;
458 L.LexFromRawLexer(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattner9ef847b2009-02-13 19:33:24 +0000460 // If this is a # at the start of a line, discard it from the token stream.
461 // We don't want the re-preprocess step to see #defines, #includes or other
462 // preprocessor directives.
463 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
464 continue;
Chris Lattner59da7392009-02-24 05:29:33 +0000465
466 // If this is a ## token, change its kind to unknown so that repreprocessing
467 // it will not produce an error.
468 if (Tok.is(tok::hashhash))
469 Tok.setKind(tok::unknown);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattner9ef847b2009-02-13 19:33:24 +0000471 // If this raw token is an identifier, the raw lexer won't have looked up
472 // the corresponding identifier info for it. Do this now so that it will be
473 // macro expanded when we re-preprocess it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000474 if (Tok.is(tok::raw_identifier))
475 PP.LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner9ef847b2009-02-13 19:33:24 +0000477 TokenStream.push_back(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner9ef847b2009-02-13 19:33:24 +0000479 if (Tok.is(tok::eof)) break;
480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattnere07ea352009-03-13 21:44:46 +0000482 // Temporarily change the diagnostics object so that we ignore any generated
483 // diagnostics from this pass.
David Blaikie9c902b52011-09-25 23:23:43 +0000484 DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
Douglas Gregor811db4e2012-10-23 22:26:28 +0000485 &PP.getDiagnostics().getDiagnosticOptions(),
David Blaikie2cb2a832011-09-25 23:44:35 +0000486 new IgnoringDiagConsumer);
Mike Stump11289f42009-09-09 15:08:12 +0000487
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000488 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
489 // its state, but we aren't actually changing it (we hope). This should really
490 // construct a copy of the preprocessor.
491 Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
David Blaikie9c902b52011-09-25 23:23:43 +0000492 DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000493 TmpPP.setDiagnostics(TmpDiags);
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnera5a75e72008-04-16 06:32:08 +0000495 // Inform the preprocessor that we don't want comments.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000496 TmpPP.SetCommentRetentionState(false, false);
Chris Lattner9ef847b2009-02-13 19:33:24 +0000497
Jordan Rosede1a2922012-06-08 18:06:21 +0000498 // We don't want pragmas either. Although we filtered out #pragma, removing
499 // _Pragma and __pragma is much harder.
500 bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
501 TmpPP.setPragmasEnabled(false);
502
Chris Lattner9ef847b2009-02-13 19:33:24 +0000503 // Enter the tokens we just lexed. This will cause them to be macro expanded
504 // but won't enter sub-files (because we removed #'s).
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000505 TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Mike Stump11289f42009-09-09 15:08:12 +0000506
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000507 TokenConcatenation ConcatInfo(TmpPP);
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattnera5a75e72008-04-16 06:32:08 +0000509 // Lex all the tokens.
Chris Lattnera5a75e72008-04-16 06:32:08 +0000510 Token Tok;
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000511 TmpPP.Lex(Tok);
Chris Lattnera5a75e72008-04-16 06:32:08 +0000512 while (Tok.isNot(tok::eof)) {
513 // Ignore non-macro tokens.
514 if (!Tok.getLocation().isMacroID()) {
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000515 TmpPP.Lex(Tok);
Chris Lattnera5a75e72008-04-16 06:32:08 +0000516 continue;
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner20cf4302009-02-15 21:32:34 +0000519 // Okay, we have the first token of a macro expansion: highlight the
Chandler Carruth5d4aff02011-07-15 00:04:40 +0000520 // expansion by inserting a start tag before the macro expansion and
Chris Lattner20cf4302009-02-15 21:32:34 +0000521 // end tag after it.
522 std::pair<SourceLocation, SourceLocation> LLoc =
Chandler Carruth6d28d7f2011-07-25 16:56:02 +0000523 SM.getExpansionRange(Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattner20cf4302009-02-15 21:32:34 +0000525 // Ignore tokens whose instantiation location was not the main file.
526 if (SM.getFileID(LLoc.first) != FID) {
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000527 TmpPP.Lex(Tok);
Chris Lattnera5a75e72008-04-16 06:32:08 +0000528 continue;
529 }
Chris Lattner20cf4302009-02-15 21:32:34 +0000530
Chris Lattner20cf4302009-02-15 21:32:34 +0000531 assert(SM.getFileID(LLoc.second) == FID &&
532 "Start and end of expansion must be in the same ultimate file!");
Chris Lattner8cad67b2009-02-17 00:51:07 +0000533
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000534 std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000535 unsigned LineLen = Expansion.size();
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner0384e6352010-04-14 03:57:19 +0000537 Token PrevPrevTok;
Chris Lattner837b9902009-02-13 00:51:30 +0000538 Token PrevTok = Tok;
Chris Lattnera5a75e72008-04-16 06:32:08 +0000539 // Okay, eat this token, getting the next one.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000540 TmpPP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattnera5a75e72008-04-16 06:32:08 +0000542 // Skip all the rest of the tokens that are part of this macro
543 // instantiation. It would be really nice to pop up a window with all the
544 // spelling of the tokens or something.
545 while (!Tok.is(tok::eof) &&
Chandler Carruth35f53202011-07-25 16:49:02 +0000546 SM.getExpansionLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000547 // Insert a newline if the macro expansion is getting large.
548 if (LineLen > 60) {
549 Expansion += "<br>";
550 LineLen = 0;
551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000553 LineLen -= Expansion.size();
Mike Stump11289f42009-09-09 15:08:12 +0000554
Chris Lattner837b9902009-02-13 00:51:30 +0000555 // If the tokens were already space separated, or if they must be to avoid
556 // them being implicitly pasted, add a space between them.
557 if (Tok.hasLeadingSpace() ||
Chris Lattner0384e6352010-04-14 03:57:19 +0000558 ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
Chris Lattner837b9902009-02-13 00:51:30 +0000559 Expansion += ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner54349c52008-04-17 23:03:14 +0000561 // Escape any special characters in the token text.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000562 Expansion += EscapeText(TmpPP.getSpelling(Tok));
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000563 LineLen += Expansion.size();
Mike Stump11289f42009-09-09 15:08:12 +0000564
Chris Lattner0384e6352010-04-14 03:57:19 +0000565 PrevPrevTok = PrevTok;
Chris Lattner837b9902009-02-13 00:51:30 +0000566 PrevTok = Tok;
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000567 TmpPP.Lex(Tok);
Chris Lattnerf1c70d72008-04-17 00:40:45 +0000568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattner8cad67b2009-02-17 00:51:07 +0000570
571 // Insert the expansion as the end tag, so that multi-line macros all get
572 // highlighted.
573 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
574
575 HighlightRange(R, LLoc.first, LLoc.second,
576 "<span class='macro'>", Expansion.c_str());
Chris Lattnera5a75e72008-04-16 06:32:08 +0000577 }
Chris Lattnere07ea352009-03-13 21:44:46 +0000578
Jordan Rosede1a2922012-06-08 18:06:21 +0000579 // Restore the preprocessor's old state.
Daniel Dunbara2ff3b42009-11-05 01:54:02 +0000580 TmpPP.setDiagnostics(*OldDiags);
Jordan Rosede1a2922012-06-08 18:06:21 +0000581 TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
Chris Lattnera5a75e72008-04-16 06:32:08 +0000582}