blob: d0d1ff0b935e106b0e084ce151fcbf5187432b2b [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 Lattner3245a0a2008-04-16 06:11:58 +000018#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000019#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000020#include "llvm/ADT/SmallString.h"
Ted Kremenek339b9c22008-04-17 22:31:54 +000021#include "llvm/ADT/OwningPtr.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000022#include "llvm/Support/MemoryBuffer.h"
23#include <sstream>
Ted Kremenek6a340832008-03-18 21:19:49 +000024using namespace clang;
25
Chris Lattner9402b572008-04-16 23:06:45 +000026
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000027/// HighlightRange - Highlight a range in the source code with the specified
28/// start/end tags. B/E must be in the same file. This ensures that
29/// start/end tags are placed at the start/end of each line if the range is
30/// multiline.
31void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
32 const char *StartTag, const char *EndTag) {
33 SourceManager &SM = R.getSourceMgr();
34 B = SM.getLogicalLoc(B);
35 E = SM.getLogicalLoc(E);
36 unsigned FileID = SM.getCanonicalFileID(B);
37 assert(SM.getCanonicalFileID(E) == FileID && "B/E not in the same file!");
38
39 unsigned BOffset = SM.getFullFilePos(B);
40 unsigned EOffset = SM.getFullFilePos(E);
41
42 // Include the whole end token in the range.
43 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr());
44
45 HighlightRange(R.getEditBuffer(FileID), BOffset, EOffset,
46 SM.getBufferData(FileID).first, StartTag, EndTag);
47}
48
49/// HighlightRange - This is the same as the above method, but takes
50/// decomposed file locations.
51void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
52 const char *BufferStart,
53 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000054 // Insert the tag at the absolute start/end of the range.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000055 RB.InsertTextAfter(B, StartTag, strlen(StartTag));
56 RB.InsertTextBefore(E, EndTag, strlen(EndTag));
57
Chris Lattner9402b572008-04-16 23:06:45 +000058 // Scan the range to see if there is a \r or \n. If so, and if the line is
59 // not blank, insert tags on that line as well.
60 bool HadOpenTag = true;
61
62 unsigned LastNonWhiteSpace = B;
63 for (unsigned i = B; i != E; ++i) {
64 switch (BufferStart[i]) {
65 case '\r':
66 case '\n':
67 // Okay, we found a newline in the range. If we have an open tag, we need
68 // to insert a close tag at the first non-whitespace before the newline.
69 if (HadOpenTag)
70 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag, strlen(EndTag));
71
72 // Instead of inserting an open tag immediately after the newline, we
73 // wait until we see a non-whitespace character. This prevents us from
74 // inserting tags around blank lines, and also allows the open tag to
75 // be put *after* whitespace on a non-blank line.
76 HadOpenTag = false;
77 break;
78 case '\0':
79 case ' ':
80 case '\t':
81 case '\f':
82 case '\v':
83 // Ignore whitespace.
84 break;
85
86 default:
87 // If there is no tag open, do it now.
88 if (!HadOpenTag) {
89 RB.InsertTextAfter(i, StartTag, strlen(StartTag));
90 HadOpenTag = true;
91 }
92
93 // Remember this character.
94 LastNonWhiteSpace = i;
95 break;
96 }
97 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000098}
99
Ted Kremenekfa5be362008-04-08 22:37:58 +0000100void html::EscapeText(Rewriter& R, unsigned FileID,
101 bool EscapeSpaces, bool ReplaceTabs) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000102
103 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
104 const char* C = Buf->getBufferStart();
105 const char* FileEnd = Buf->getBufferEnd();
106
107 assert (C <= FileEnd);
108
Chris Lattner73527142008-04-16 04:33:23 +0000109 RewriteBuffer &RB = R.getEditBuffer(FileID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000110
111 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000112 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000113 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000114 default: ++ColNo; break;
115 case '\n':
116 case '\r':
117 ColNo = 0;
118 break;
Chris Lattner73527142008-04-16 04:33:23 +0000119
120 case ' ':
121 if (EscapeSpaces)
122 RB.ReplaceText(FilePos, 1, "&nbsp;", 6);
Chris Lattner5c176f72008-04-18 04:54:20 +0000123 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000124 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000125
Chris Lattner5c176f72008-04-18 04:54:20 +0000126 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000127 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000128 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000129 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000130 if (EscapeSpaces)
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000131 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Chris Lattner5c176f72008-04-18 04:54:20 +0000132 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces);
Chris Lattner73527142008-04-16 04:33:23 +0000133 else
Chris Lattner5c176f72008-04-18 04:54:20 +0000134 RB.ReplaceText(FilePos, 1, " ", NumSpaces);
135 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000136 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000137 }
Chris Lattner73527142008-04-16 04:33:23 +0000138 case '<':
139 RB.ReplaceText(FilePos, 1, "&lt;", 4);
Chris Lattner5c176f72008-04-18 04:54:20 +0000140 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000141 break;
142
143 case '>':
144 RB.ReplaceText(FilePos, 1, "&gt;", 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, "&amp;", 5);
Chris Lattner5c176f72008-04-18 04:54:20 +0000150 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000151 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000152 }
153 }
154}
155
Ted Kremenekfa5be362008-04-08 22:37:58 +0000156std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
157 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +0000158
159 unsigned len = s.size();
160 std::ostringstream os;
161
162 for (unsigned i = 0 ; i < len; ++i) {
163
164 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000165 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000166 default:
167 os << c; break;
168
169 case ' ':
170 if (EscapeSpaces) os << "&nbsp;";
171 else os << ' ';
172 break;
173
174 case '\t':
175 if (ReplaceTabs)
176 for (unsigned i = 0; i < 4; ++i)
177 os << "&nbsp;";
178 else
179 os << c;
180
Ted Kremenek053ef592008-03-27 17:15:29 +0000181 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000182
183 case '<': os << "&lt;"; break;
184 case '>': os << "&gt;"; break;
185 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000186 }
187 }
188
189 return os.str();
190}
191
Chris Lattner8570f0b2008-04-16 04:37:29 +0000192static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
193 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000194 llvm::SmallString<100> Str;
195 Str += "<tr><td class=\"num\" id=\"LN";
196 Str.append_uint(LineNo);
197 Str += "\">";
198 Str.append_uint(LineNo);
199 Str += "</td><td class=\"line\">";
200
Ted Kremenek49cd6352008-04-03 07:12:29 +0000201 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000202 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000203 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000204 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000205 RB.InsertTextBefore(B, &Str[0], Str.size());
206 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000207 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000208}
209
210void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
211
212 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
213 const char* FileBeg = Buf->getBufferStart();
214 const char* FileEnd = Buf->getBufferEnd();
215 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000216 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000217
218 assert (C <= FileEnd);
219
220 unsigned LineNo = 0;
221 unsigned FilePos = 0;
222
223 while (C != FileEnd) {
224
225 ++LineNo;
226 unsigned LineStartPos = FilePos;
227 unsigned LineEndPos = FileEnd - FileBeg;
228
229 assert (FilePos <= LineEndPos);
230 assert (C < FileEnd);
231
232 // Scan until the newline (or end-of-file).
233
Ted Kremenek49cd6352008-04-03 07:12:29 +0000234 while (C != FileEnd) {
235 char c = *C;
236 ++C;
237
238 if (c == '\n') {
239 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000240 break;
241 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000242
243 ++FilePos;
244 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000245
Chris Lattner8570f0b2008-04-16 04:37:29 +0000246 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000247 }
248
Chris Lattner8570f0b2008-04-16 04:37:29 +0000249 // Add one big table tag that surrounds all of the code.
250 RB.InsertTextBefore(0, "<table class=\"code\">\n",
251 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000252
Chris Lattner8570f0b2008-04-16 04:37:29 +0000253 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000254}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000255
256void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) {
257
258 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
259 const char* FileStart = Buf->getBufferStart();
260 const char* FileEnd = Buf->getBufferEnd();
261
262 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
263 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
264
265 // Generate header
Ted Kremenek70bcba62008-04-09 15:40:40 +0000266 R.InsertCStrBefore(StartLoc,
Ted Kremenek38941b52008-04-18 03:37:38 +0000267 "<!doctype html>\n" // Use HTML 5 doctype
268 "<html>\n<head>\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000269 "<style type=\"text/css\">\n"
270 " body { color:#000000; background-color:#ffffff }\n"
271 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000272 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000273 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000274 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
275 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000276 " .comment { color: green; font-style: oblique }\n"
277 " .keyword { color: blue }\n"
278 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000279 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000280 " .expansion { display: none; }\n"
281 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000282 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000283 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000284 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000285 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000286 // Macros are position: relative to provide base for expansions.
287 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000288 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
289 " .num { text-align:right; font-size: smaller }\n"
290 " .num { color:#444444 }\n"
291 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
292 " .line { white-space: pre }\n"
293 " .msg { background-color:#fff8b4; color:#000000 }\n"
294 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
295 " .msg { -webkit-border-radius:5px }\n"
296 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
297 " .msg { font-weight: bold }\n"
298 " .msg { float:left }\n"
299 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
300 " .msg { margin-top:10px; margin-bottom:10px }\n"
301 " .mrange { background-color:#dfddf3 }\n"
302 " .mrange { border-bottom:1px solid #6F9DBE }\n"
303 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000304 " table.simpletable {\n"
305 " padding: 5px;\n"
306 " font-size:12pt;\n"
307 " margin:20px;\n"
308 " border-collapse: collapse; border-spacing: 0px;\n"
309 " }\n"
310 " td.rowname {\n"
311 " text-align:right; font-weight:bold; color:#444444;\n"
312 " padding-right:2ex; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000313 "</style>\n</head>\n<body>");
314
Ted Kremenekad0a2032008-03-19 06:14:37 +0000315 // Generate footer
316
Ted Kremenek70bcba62008-04-09 15:40:40 +0000317 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000318}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000319
320/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
321/// information about keywords, macro expansions etc. This uses the macro
322/// table state from the end of the file, so it won't be perfectly perfect,
323/// but it will be reasonably close.
324void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
325 RewriteBuffer &RB = R.getEditBuffer(FileID);
326
Chris Lattnera745e8c2008-04-16 20:51:51 +0000327 const SourceManager &SourceMgr = PP.getSourceManager();
328 std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID);
329 const char *BufferStart = File.first;
330
331 Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(),
332 File.first, File.second);
333
Chris Lattner3245a0a2008-04-16 06:11:58 +0000334 // Inform the preprocessor that we want to retain comments as tokens, so we
335 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000336 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000337
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000338 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
339 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000340 Token Tok;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000341 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000342
Chris Lattner74ea3e52008-04-16 06:53:09 +0000343 while (Tok.isNot(tok::eof)) {
344 // Since we are lexing unexpanded tokens, all tokens are from the main
345 // FileID.
346 unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000347 unsigned TokLen = Tok.getLength();
348 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000349 default: break;
350 case tok::identifier: {
351 // Fill in Result.IdentifierInfo, looking up the identifier in the
352 // identifier table.
353 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
354
355 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000356 if (II->getTokenID() != tok::identifier)
357 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
358 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000359 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000360 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000361 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000362 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
363 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000364 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000365 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000366 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000367 if (!Tok.isAtStartOfLine())
368 break;
369
370 // Eat all of the tokens until we get to the next one at the start of
371 // line.
372 unsigned TokEnd = TokOffs+TokLen;
373 L.LexRawToken(Tok);
374 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
375 TokEnd = SourceMgr.getFullFilePos(Tok.getLocation())+Tok.getLength();
376 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000377 }
Chris Lattner5deb96d2008-04-16 23:21:17 +0000378
379 // Find end of line. This is a hack.
380 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
381 "<span class='directive'>", "</span>");
382
383 // Don't skip the next token.
384 continue;
385 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000386 }
387
Chris Lattnera745e8c2008-04-16 20:51:51 +0000388 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000389 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000390}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000391
392/// HighlightMacros - This uses the macro table state from the end of the
393/// file, to reexpand macros and insert (into the HTML) information about the
394/// macro expansions. This won't be perfectly perfect, but it will be
395/// reasonably close.
Ted Kremenek339b9c22008-04-17 22:31:54 +0000396void html::HighlightMacros(Rewriter &R, unsigned FileID,
397 PreprocessorFactory &PPF) {
398
399 llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
400
401
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000402 RewriteBuffer &RB = R.getEditBuffer(FileID);
403
404 // Inform the preprocessor that we don't want comments.
Ted Kremenek339b9c22008-04-17 22:31:54 +0000405 PP->SetCommentRetentionState(false, false);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000406
407 // Start parsing the specified input file.
Ted Kremenek339b9c22008-04-17 22:31:54 +0000408 PP->EnterMainSourceFile();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000409
410 // Lex all the tokens.
Ted Kremenek339b9c22008-04-17 22:31:54 +0000411 const SourceManager &SourceMgr = PP->getSourceManager();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000412 Token Tok;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000413 PP->Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000414 while (Tok.isNot(tok::eof)) {
415 // Ignore non-macro tokens.
416 if (!Tok.getLocation().isMacroID()) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000417 PP->Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000418 continue;
419 }
420
421 // Ignore tokens whose logical location was not the main file.
422 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
423 std::pair<unsigned, unsigned> LLocInfo =
424 SourceMgr.getDecomposedFileLoc(LLoc);
425
426 if (LLocInfo.first != FileID) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000427 PP->Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000428 continue;
429 }
430
431 // Okay, we have the first token of a macro expansion: highlight the
432 // instantiation.
433
434 // Get the size of current macro call itself.
435 // FIXME: This should highlight the args of a function-like
436 // macro, using a heuristic.
437 unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
438
439 unsigned TokOffs = LLocInfo.second;
Chris Lattner6f46be22008-04-17 00:40:45 +0000440 // Highlight the macro invocation itself.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000441 RB.InsertTextAfter(TokOffs, "<span class='macro'>",
442 strlen("<span class='macro'>"));
443 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
444
Ted Kremenek339b9c22008-04-17 22:31:54 +0000445 std::string Expansion = PP->getSpelling(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000446 unsigned LineLen = Expansion.size();
447
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000448 // Okay, eat this token, getting the next one.
Ted Kremenek339b9c22008-04-17 22:31:54 +0000449 PP->Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000450
451 // Skip all the rest of the tokens that are part of this macro
452 // instantiation. It would be really nice to pop up a window with all the
453 // spelling of the tokens or something.
454 while (!Tok.is(tok::eof) &&
Chris Lattner6f46be22008-04-17 00:40:45 +0000455 SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc) {
456 // Insert a newline if the macro expansion is getting large.
457 if (LineLen > 60) {
458 Expansion += "<br>";
459 LineLen = 0;
460 }
461
462 LineLen -= Expansion.size();
Chris Lattner9227c692008-04-17 23:03:14 +0000463 // Escape any special characters in the token text.
464 Expansion += ' ' + EscapeText(PP->getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000465 LineLen += Expansion.size();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000466 PP->Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000467 }
Chris Lattner9227c692008-04-17 23:03:14 +0000468
Chris Lattner6f46be22008-04-17 00:40:45 +0000469 // Insert the information about the expansion inside the macro span.
470 Expansion = "<span class='expansion'>" + Expansion + "</span>";
471 RB.InsertTextBefore(TokOffs+TokLen, Expansion.c_str(), Expansion.size());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000472 }
473}
474
475