blob: 692af80488714be2f917ef6e47b01db03d4972fb [file] [log] [blame]
Ted Kremenek6a340832008-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//
10// This file defines the HTMLRewriter clas, which is used to translate the
11// text of a source file into prettified HTML.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek339b9c22008-04-17 22:31:54 +000015#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000016#include "clang/Rewrite/Rewriter.h"
17#include "clang/Rewrite/HTMLRewrite.h"
Chris Lattner867924d2009-02-13 00:51:30 +000018#include "clang/Lex/TokenConcatenation.h"
Chris Lattner3245a0a2008-04-16 06:11:58 +000019#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenek339b9c22008-04-17 22:31:54 +000022#include "llvm/ADT/OwningPtr.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000023#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000025using namespace clang;
26
Chris Lattner9402b572008-04-16 23:06:45 +000027
Chris Lattner5ef3e2c2008-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();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000035 B = SM.getInstantiationLoc(B);
36 E = SM.getInstantiationLoc(E);
Chris Lattnera11d6172009-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 Lattner5ef3e2c2008-04-16 22:45:51 +000039
Chris Lattner52c29082009-01-27 06:27:13 +000040 unsigned BOffset = SM.getFileOffset(B);
41 unsigned EOffset = SM.getFileOffset(E);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000042
43 // Include the whole end token in the range.
44 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr());
45
Chris Lattner2b2453a2009-01-17 06:22:33 +000046 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
47 SM.getBufferData(FID).first, StartTag, EndTag);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000048}
49
50/// HighlightRange - This is the same as the above method, but takes
51/// decomposed file locations.
52void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
53 const char *BufferStart,
54 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000055 // Insert the tag at the absolute start/end of the range.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000056 RB.InsertTextAfter(B, StartTag, strlen(StartTag));
57 RB.InsertTextBefore(E, EndTag, strlen(EndTag));
58
Chris Lattner9402b572008-04-16 23:06:45 +000059 // Scan the range to see if there is a \r or \n. If so, and if the line is
60 // not blank, insert tags on that line as well.
61 bool HadOpenTag = true;
62
63 unsigned LastNonWhiteSpace = B;
64 for (unsigned i = B; i != E; ++i) {
65 switch (BufferStart[i]) {
66 case '\r':
67 case '\n':
68 // Okay, we found a newline in the range. If we have an open tag, we need
69 // to insert a close tag at the first non-whitespace before the newline.
70 if (HadOpenTag)
71 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag, strlen(EndTag));
72
73 // Instead of inserting an open tag immediately after the newline, we
74 // wait until we see a non-whitespace character. This prevents us from
75 // inserting tags around blank lines, and also allows the open tag to
76 // be put *after* whitespace on a non-blank line.
77 HadOpenTag = false;
78 break;
79 case '\0':
80 case ' ':
81 case '\t':
82 case '\f':
83 case '\v':
84 // Ignore whitespace.
85 break;
86
87 default:
88 // If there is no tag open, do it now.
89 if (!HadOpenTag) {
90 RB.InsertTextAfter(i, StartTag, strlen(StartTag));
91 HadOpenTag = true;
92 }
93
94 // Remember this character.
95 LastNonWhiteSpace = i;
96 break;
97 }
98 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000099}
100
Chris Lattner2b2453a2009-01-17 06:22:33 +0000101void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenekfa5be362008-04-08 22:37:58 +0000102 bool EscapeSpaces, bool ReplaceTabs) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000103
Chris Lattner2b2453a2009-01-17 06:22:33 +0000104 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek6a340832008-03-18 21:19:49 +0000105 const char* C = Buf->getBufferStart();
106 const char* FileEnd = Buf->getBufferEnd();
107
108 assert (C <= FileEnd);
109
Chris Lattner2b2453a2009-01-17 06:22:33 +0000110 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000111
112 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000113 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000114 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000115 default: ++ColNo; break;
116 case '\n':
117 case '\r':
118 ColNo = 0;
119 break;
Chris Lattner73527142008-04-16 04:33:23 +0000120
121 case ' ':
122 if (EscapeSpaces)
123 RB.ReplaceText(FilePos, 1, "&nbsp;", 6);
Chris Lattner5c176f72008-04-18 04:54:20 +0000124 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000125 break;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000126 case '\f':
127 RB.ReplaceText(FilePos, 1, "<hr>", 4);
128 ColNo = 0;
129 break;
130
Chris Lattner5c176f72008-04-18 04:54:20 +0000131 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000132 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000133 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000134 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000135 if (EscapeSpaces)
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000136 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Chris Lattner5c176f72008-04-18 04:54:20 +0000137 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces);
Chris Lattner73527142008-04-16 04:33:23 +0000138 else
Chris Lattner5c176f72008-04-18 04:54:20 +0000139 RB.ReplaceText(FilePos, 1, " ", NumSpaces);
140 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000141 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000142 }
Chris Lattner73527142008-04-16 04:33:23 +0000143 case '<':
144 RB.ReplaceText(FilePos, 1, "&lt;", 4);
Chris Lattner5c176f72008-04-18 04:54:20 +0000145 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000146 break;
147
148 case '>':
149 RB.ReplaceText(FilePos, 1, "&gt;", 4);
Chris Lattner5c176f72008-04-18 04:54:20 +0000150 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000151 break;
152
153 case '&':
154 RB.ReplaceText(FilePos, 1, "&amp;", 5);
Chris Lattner5c176f72008-04-18 04:54:20 +0000155 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000156 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000157 }
158 }
159}
160
Ted Kremenekfa5be362008-04-08 22:37:58 +0000161std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
162 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +0000163
164 unsigned len = s.size();
Ted Kremeneka95d3752008-09-13 05:16:45 +0000165 std::string Str;
166 llvm::raw_string_ostream os(Str);
Ted Kremenek053ef592008-03-27 17:15:29 +0000167
168 for (unsigned i = 0 ; i < len; ++i) {
169
170 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000171 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000172 default:
173 os << c; break;
174
175 case ' ':
176 if (EscapeSpaces) os << "&nbsp;";
177 else os << ' ';
178 break;
179
180 case '\t':
Nico Weber63366302008-08-16 22:24:33 +0000181 if (ReplaceTabs) {
182 if (EscapeSpaces)
183 for (unsigned i = 0; i < 4; ++i)
184 os << "&nbsp;";
185 else
186 for (unsigned i = 0; i < 4; ++i)
187 os << " ";
188 }
Chris Lattner8570f0b2008-04-16 04:37:29 +0000189 else
190 os << c;
191
Ted Kremenek053ef592008-03-27 17:15:29 +0000192 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000193
194 case '<': os << "&lt;"; break;
195 case '>': os << "&gt;"; break;
196 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000197 }
198 }
199
200 return os.str();
201}
202
Chris Lattner8570f0b2008-04-16 04:37:29 +0000203static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
204 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000205 llvm::SmallString<100> Str;
206 Str += "<tr><td class=\"num\" id=\"LN";
207 Str.append_uint(LineNo);
208 Str += "\">";
209 Str.append_uint(LineNo);
210 Str += "</td><td class=\"line\">";
211
Ted Kremenek49cd6352008-04-03 07:12:29 +0000212 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000213 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000214 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000215 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000216 RB.InsertTextBefore(B, &Str[0], Str.size());
217 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000218 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000219}
220
Chris Lattner2b2453a2009-01-17 06:22:33 +0000221void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000222
Chris Lattner2b2453a2009-01-17 06:22:33 +0000223 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000224 const char* FileBeg = Buf->getBufferStart();
225 const char* FileEnd = Buf->getBufferEnd();
226 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000227 RewriteBuffer &RB = R.getEditBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000228
229 assert (C <= FileEnd);
230
231 unsigned LineNo = 0;
232 unsigned FilePos = 0;
233
234 while (C != FileEnd) {
235
236 ++LineNo;
237 unsigned LineStartPos = FilePos;
238 unsigned LineEndPos = FileEnd - FileBeg;
239
240 assert (FilePos <= LineEndPos);
241 assert (C < FileEnd);
242
243 // Scan until the newline (or end-of-file).
244
Ted Kremenek49cd6352008-04-03 07:12:29 +0000245 while (C != FileEnd) {
246 char c = *C;
247 ++C;
248
249 if (c == '\n') {
250 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000251 break;
252 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000253
254 ++FilePos;
255 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000256
Chris Lattner8570f0b2008-04-16 04:37:29 +0000257 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000258 }
259
Chris Lattner8570f0b2008-04-16 04:37:29 +0000260 // Add one big table tag that surrounds all of the code.
261 RB.InsertTextBefore(0, "<table class=\"code\">\n",
262 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000263
Chris Lattner8570f0b2008-04-16 04:37:29 +0000264 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000265}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000266
Chris Lattner2b2453a2009-01-17 06:22:33 +0000267void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000268 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000269
Chris Lattner2b2453a2009-01-17 06:22:33 +0000270 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000271 const char* FileStart = Buf->getBufferStart();
272 const char* FileEnd = Buf->getBufferEnd();
273
Chris Lattner2b2453a2009-01-17 06:22:33 +0000274 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
275 SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000276
Ted Kremeneka95d3752008-09-13 05:16:45 +0000277 std::string s;
278 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000279 os << "<!doctype html>\n" // Use HTML 5 doctype
280 "<html>\n<head>\n";
281
282 if (title)
283 os << "<title>" << html::EscapeText(title) << "</title>\n";
284
285 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000286 " body { color:#000000; background-color:#ffffff }\n"
287 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000288 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000289 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000290 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
291 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000292 " .comment { color: green; font-style: oblique }\n"
293 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000294 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000295 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000296 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000297 " .expansion { display: none; }\n"
298 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000299 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000300 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000301 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000302 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000303 // Macros are position: relative to provide base for expansions.
304 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000305 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
306 " .num { text-align:right; font-size: smaller }\n"
307 " .num { color:#444444 }\n"
308 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
309 " .line { white-space: pre }\n"
310 " .msg { background-color:#fff8b4; color:#000000 }\n"
311 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
312 " .msg { -webkit-border-radius:5px }\n"
313 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
314 " .msg { font-weight: bold }\n"
315 " .msg { float:left }\n"
316 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
317 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek170db7c2008-07-11 23:13:22 +0000318 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap;}\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000319 " .mrange { background-color:#dfddf3 }\n"
320 " .mrange { border-bottom:1px solid #6F9DBE }\n"
321 " .PathIndex { font-weight: bold }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000322 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
323 " .CodeRemovalHint { background-color:#de1010 }\n"
324 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000325 " table.simpletable {\n"
326 " padding: 5px;\n"
327 " font-size:12pt;\n"
328 " margin:20px;\n"
329 " border-collapse: collapse; border-spacing: 0px;\n"
330 " }\n"
331 " td.rowname {\n"
332 " text-align:right; font-weight:bold; color:#444444;\n"
333 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000334 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000335
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000336 // Generate header
337 R.InsertStrBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000338 // Generate footer
339
Ted Kremenek70bcba62008-04-09 15:40:40 +0000340 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000341}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000342
343/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
344/// information about keywords, macro expansions etc. This uses the macro
345/// table state from the end of the file, so it won't be perfectly perfect,
346/// but it will be reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000347void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
348 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000349
Chris Lattner05db4272009-02-13 19:33:24 +0000350 const SourceManager &SM = PP.getSourceManager();
351 Lexer L(FID, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000352 const char *BufferStart = L.getBufferStart();
Chris Lattnera745e8c2008-04-16 20:51:51 +0000353
Chris Lattner3245a0a2008-04-16 06:11:58 +0000354 // Inform the preprocessor that we want to retain comments as tokens, so we
355 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000356 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000357
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000358 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
359 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000360 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000361 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000362
Chris Lattner74ea3e52008-04-16 06:53:09 +0000363 while (Tok.isNot(tok::eof)) {
364 // Since we are lexing unexpanded tokens, all tokens are from the main
365 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000366 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000367 unsigned TokLen = Tok.getLength();
368 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000369 default: break;
370 case tok::identifier: {
371 // Fill in Result.IdentifierInfo, looking up the identifier in the
372 // identifier table.
373 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
374
375 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000376 if (II->getTokenID() != tok::identifier)
377 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
378 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000379 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000380 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000381 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000382 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
383 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000384 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000385 case tok::wide_string_literal:
386 // Chop off the L prefix
387 ++TokOffs;
388 --TokLen;
389 // FALL THROUGH.
390 case tok::string_literal:
391 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
392 "<span class='string_literal'>", "</span>");
393 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000394 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000395 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000396 if (!Tok.isAtStartOfLine())
397 break;
398
399 // Eat all of the tokens until we get to the next one at the start of
400 // line.
401 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000402 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000403 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000404 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000405 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000406 }
Chris Lattner5deb96d2008-04-16 23:21:17 +0000407
408 // Find end of line. This is a hack.
409 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
410 "<span class='directive'>", "</span>");
411
412 // Don't skip the next token.
413 continue;
414 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000415 }
416
Chris Lattner590f0cc2008-10-12 01:15:46 +0000417 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000418 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000419}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000420
421/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000422/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000423/// macro expansions. This won't be perfectly perfect, but it will be
424/// reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000425void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000426 // Re-lex the raw token stream into a token buffer.
427 const SourceManager &SM = PP.getSourceManager();
428 std::vector<Token> TokenStream;
429
430 Lexer L(FID, SM, PP.getLangOptions());
431
432 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
433 // macros.
434 while (1) {
435 Token Tok;
436 L.LexFromRawLexer(Tok);
437
438 // If this is a # at the start of a line, discard it from the token stream.
439 // We don't want the re-preprocess step to see #defines, #includes or other
440 // preprocessor directives.
441 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
442 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000443
444 // If this is a ## token, change its kind to unknown so that repreprocessing
445 // it will not produce an error.
446 if (Tok.is(tok::hashhash))
447 Tok.setKind(tok::unknown);
Chris Lattner05db4272009-02-13 19:33:24 +0000448
449 // If this raw token is an identifier, the raw lexer won't have looked up
450 // the corresponding identifier info for it. Do this now so that it will be
451 // macro expanded when we re-preprocess it.
452 if (Tok.is(tok::identifier)) {
453 // Change the kind of this identifier to the appropriate token kind, e.g.
454 // turning "for" into a keyword.
455 Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID());
456 }
457
458 TokenStream.push_back(Tok);
459
460 if (Tok.is(tok::eof)) break;
461 }
462
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000463 // Inform the preprocessor that we don't want comments.
Ted Kremenekfb586092008-04-18 05:34:33 +0000464 PP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000465
466 // Enter the tokens we just lexed. This will cause them to be macro expanded
467 // but won't enter sub-files (because we removed #'s).
468 PP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000469
Chris Lattner867924d2009-02-13 00:51:30 +0000470 TokenConcatenation ConcatInfo(PP);
471
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000472 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000473 Token Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000474 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000475 while (Tok.isNot(tok::eof)) {
476 // Ignore non-macro tokens.
477 if (!Tok.getLocation().isMacroID()) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000478 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000479 continue;
480 }
481
Chris Lattnerb7949a92009-02-15 21:32:34 +0000482 // Okay, we have the first token of a macro expansion: highlight the
483 // instantiation by inserting a start tag before the macro instantiation and
484 // end tag after it.
485 std::pair<SourceLocation, SourceLocation> LLoc =
486 SM.getInstantiationRange(Tok.getLocation());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000487
Chris Lattnerb7949a92009-02-15 21:32:34 +0000488 // Ignore tokens whose instantiation location was not the main file.
489 if (SM.getFileID(LLoc.first) != FID) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000490 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000491 continue;
492 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000493
Chris Lattnerb7949a92009-02-15 21:32:34 +0000494 assert(SM.getFileID(LLoc.second) == FID &&
495 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000496
Ted Kremenekfb586092008-04-18 05:34:33 +0000497 std::string Expansion = PP.getSpelling(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000498 unsigned LineLen = Expansion.size();
499
Chris Lattner867924d2009-02-13 00:51:30 +0000500 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000501 // Okay, eat this token, getting the next one.
Ted Kremenekfb586092008-04-18 05:34:33 +0000502 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000503
504 // Skip all the rest of the tokens that are part of this macro
505 // instantiation. It would be really nice to pop up a window with all the
506 // spelling of the tokens or something.
507 while (!Tok.is(tok::eof) &&
Chris Lattnerb7949a92009-02-15 21:32:34 +0000508 SM.getInstantiationLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000509 // Insert a newline if the macro expansion is getting large.
510 if (LineLen > 60) {
511 Expansion += "<br>";
512 LineLen = 0;
513 }
514
515 LineLen -= Expansion.size();
Chris Lattner867924d2009-02-13 00:51:30 +0000516
517 // If the tokens were already space separated, or if they must be to avoid
518 // them being implicitly pasted, add a space between them.
519 if (Tok.hasLeadingSpace() ||
520 ConcatInfo.AvoidConcat(PrevTok, Tok))
521 Expansion += ' ';
522
Chris Lattner9227c692008-04-17 23:03:14 +0000523 // Escape any special characters in the token text.
Chris Lattner867924d2009-02-13 00:51:30 +0000524 Expansion += EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000525 LineLen += Expansion.size();
Chris Lattner867924d2009-02-13 00:51:30 +0000526
527 PrevTok = Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000528 PP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000529 }
Chris Lattner9227c692008-04-17 23:03:14 +0000530
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000531
532 // Insert the expansion as the end tag, so that multi-line macros all get
533 // highlighted.
534 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
535
536 HighlightRange(R, LLoc.first, LLoc.second,
537 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000538 }
539}
540
Chris Lattner2b2453a2009-01-17 06:22:33 +0000541void html::HighlightMacros(Rewriter &R, FileID FID,
Ted Kremenekfb586092008-04-18 05:34:33 +0000542 PreprocessorFactory &PPF) {
543
544 llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
Chris Lattner2b2453a2009-01-17 06:22:33 +0000545 HighlightMacros(R, FID, *PP);
Ted Kremenekfb586092008-04-18 05:34:33 +0000546}