blob: 0a32df1c61593ef2029aed43677791a0f256e1eb [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;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000125 case '\f':
126 RB.ReplaceText(FilePos, 1, "<hr>", 4);
127 ColNo = 0;
128 break;
129
Chris Lattner5c176f72008-04-18 04:54:20 +0000130 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000131 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000132 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000133 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000134 if (EscapeSpaces)
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000135 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Chris Lattner5c176f72008-04-18 04:54:20 +0000136 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces);
Chris Lattner73527142008-04-16 04:33:23 +0000137 else
Chris Lattner5c176f72008-04-18 04:54:20 +0000138 RB.ReplaceText(FilePos, 1, " ", NumSpaces);
139 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000140 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000141 }
Chris Lattner73527142008-04-16 04:33:23 +0000142 case '<':
143 RB.ReplaceText(FilePos, 1, "&lt;", 4);
Chris Lattner5c176f72008-04-18 04:54:20 +0000144 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000145 break;
146
147 case '>':
148 RB.ReplaceText(FilePos, 1, "&gt;", 4);
Chris Lattner5c176f72008-04-18 04:54:20 +0000149 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000150 break;
151
152 case '&':
153 RB.ReplaceText(FilePos, 1, "&amp;", 5);
Chris Lattner5c176f72008-04-18 04:54:20 +0000154 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000155 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000156 }
157 }
158}
159
Ted Kremenekfa5be362008-04-08 22:37:58 +0000160std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
161 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +0000162
163 unsigned len = s.size();
164 std::ostringstream os;
165
166 for (unsigned i = 0 ; i < len; ++i) {
167
168 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000169 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000170 default:
171 os << c; break;
172
173 case ' ':
174 if (EscapeSpaces) os << "&nbsp;";
175 else os << ' ';
176 break;
177
178 case '\t':
179 if (ReplaceTabs)
180 for (unsigned i = 0; i < 4; ++i)
181 os << "&nbsp;";
182 else
183 os << c;
184
Ted Kremenek053ef592008-03-27 17:15:29 +0000185 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000186
187 case '<': os << "&lt;"; break;
188 case '>': os << "&gt;"; break;
189 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000190 }
191 }
192
193 return os.str();
194}
195
Chris Lattner8570f0b2008-04-16 04:37:29 +0000196static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
197 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000198 llvm::SmallString<100> Str;
199 Str += "<tr><td class=\"num\" id=\"LN";
200 Str.append_uint(LineNo);
201 Str += "\">";
202 Str.append_uint(LineNo);
203 Str += "</td><td class=\"line\">";
204
Ted Kremenek49cd6352008-04-03 07:12:29 +0000205 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000206 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000207 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000208 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000209 RB.InsertTextBefore(B, &Str[0], Str.size());
210 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000211 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000212}
213
214void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
215
216 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
217 const char* FileBeg = Buf->getBufferStart();
218 const char* FileEnd = Buf->getBufferEnd();
219 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000220 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000221
222 assert (C <= FileEnd);
223
224 unsigned LineNo = 0;
225 unsigned FilePos = 0;
226
227 while (C != FileEnd) {
228
229 ++LineNo;
230 unsigned LineStartPos = FilePos;
231 unsigned LineEndPos = FileEnd - FileBeg;
232
233 assert (FilePos <= LineEndPos);
234 assert (C < FileEnd);
235
236 // Scan until the newline (or end-of-file).
237
Ted Kremenek49cd6352008-04-03 07:12:29 +0000238 while (C != FileEnd) {
239 char c = *C;
240 ++C;
241
242 if (c == '\n') {
243 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000244 break;
245 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000246
247 ++FilePos;
248 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000249
Chris Lattner8570f0b2008-04-16 04:37:29 +0000250 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000251 }
252
Chris Lattner8570f0b2008-04-16 04:37:29 +0000253 // Add one big table tag that surrounds all of the code.
254 RB.InsertTextBefore(0, "<table class=\"code\">\n",
255 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000256
Chris Lattner8570f0b2008-04-16 04:37:29 +0000257 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000258}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000259
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000260void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID,
261 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000262
263 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
264 const char* FileStart = Buf->getBufferStart();
265 const char* FileEnd = Buf->getBufferEnd();
266
267 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
268 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
269
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000270 std::ostringstream os;
271 os << "<!doctype html>\n" // Use HTML 5 doctype
272 "<html>\n<head>\n";
273
274 if (title)
275 os << "<title>" << html::EscapeText(title) << "</title>\n";
276
277 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000278 " body { color:#000000; background-color:#ffffff }\n"
279 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000280 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000281 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000282 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
283 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000284 " .comment { color: green; font-style: oblique }\n"
285 " .keyword { color: blue }\n"
286 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000287 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000288 " .expansion { display: none; }\n"
289 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000290 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000291 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000292 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000293 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000294 // Macros are position: relative to provide base for expansions.
295 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000296 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
297 " .num { text-align:right; font-size: smaller }\n"
298 " .num { color:#444444 }\n"
299 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
300 " .line { white-space: pre }\n"
301 " .msg { background-color:#fff8b4; color:#000000 }\n"
302 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
303 " .msg { -webkit-border-radius:5px }\n"
304 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
305 " .msg { font-weight: bold }\n"
306 " .msg { float:left }\n"
307 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
308 " .msg { margin-top:10px; margin-bottom:10px }\n"
309 " .mrange { background-color:#dfddf3 }\n"
310 " .mrange { border-bottom:1px solid #6F9DBE }\n"
311 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000312 " table.simpletable {\n"
313 " padding: 5px;\n"
314 " font-size:12pt;\n"
315 " margin:20px;\n"
316 " border-collapse: collapse; border-spacing: 0px;\n"
317 " }\n"
318 " td.rowname {\n"
319 " text-align:right; font-weight:bold; color:#444444;\n"
320 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000321 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000322
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000323 // Generate header
324 R.InsertStrBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000325 // Generate footer
326
Ted Kremenek70bcba62008-04-09 15:40:40 +0000327 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000328}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000329
330/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
331/// information about keywords, macro expansions etc. This uses the macro
332/// table state from the end of the file, so it won't be perfectly perfect,
333/// but it will be reasonably close.
334void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
335 RewriteBuffer &RB = R.getEditBuffer(FileID);
336
Chris Lattnera745e8c2008-04-16 20:51:51 +0000337 const SourceManager &SourceMgr = PP.getSourceManager();
338 std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID);
339 const char *BufferStart = File.first;
340
341 Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(),
342 File.first, File.second);
343
Chris Lattner3245a0a2008-04-16 06:11:58 +0000344 // Inform the preprocessor that we want to retain comments as tokens, so we
345 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000346 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000347
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000348 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
349 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000350 Token Tok;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000351 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000352
Chris Lattner74ea3e52008-04-16 06:53:09 +0000353 while (Tok.isNot(tok::eof)) {
354 // Since we are lexing unexpanded tokens, all tokens are from the main
355 // FileID.
356 unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000357 unsigned TokLen = Tok.getLength();
358 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000359 default: break;
360 case tok::identifier: {
361 // Fill in Result.IdentifierInfo, looking up the identifier in the
362 // identifier table.
363 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
364
365 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000366 if (II->getTokenID() != tok::identifier)
367 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
368 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000369 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000370 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000371 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000372 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
373 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000374 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000375 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000376 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000377 if (!Tok.isAtStartOfLine())
378 break;
379
380 // Eat all of the tokens until we get to the next one at the start of
381 // line.
382 unsigned TokEnd = TokOffs+TokLen;
383 L.LexRawToken(Tok);
384 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
385 TokEnd = SourceMgr.getFullFilePos(Tok.getLocation())+Tok.getLength();
386 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000387 }
Chris Lattner5deb96d2008-04-16 23:21:17 +0000388
389 // Find end of line. This is a hack.
390 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
391 "<span class='directive'>", "</span>");
392
393 // Don't skip the next token.
394 continue;
395 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000396 }
397
Chris Lattnera745e8c2008-04-16 20:51:51 +0000398 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000399 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000400}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000401
402/// HighlightMacros - This uses the macro table state from the end of the
403/// file, to reexpand macros and insert (into the HTML) information about the
404/// macro expansions. This won't be perfectly perfect, but it will be
405/// reasonably close.
Ted Kremenekfb586092008-04-18 05:34:33 +0000406void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor& PP) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000407
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000408 RewriteBuffer &RB = R.getEditBuffer(FileID);
409
410 // Inform the preprocessor that we don't want comments.
Ted Kremenekfb586092008-04-18 05:34:33 +0000411 PP.SetCommentRetentionState(false, false);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000412
413 // Start parsing the specified input file.
Ted Kremenekfb586092008-04-18 05:34:33 +0000414 PP.EnterMainSourceFile();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000415
416 // Lex all the tokens.
Ted Kremenekfb586092008-04-18 05:34:33 +0000417 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000418 Token Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000419 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000420 while (Tok.isNot(tok::eof)) {
421 // Ignore non-macro tokens.
422 if (!Tok.getLocation().isMacroID()) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000423 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000424 continue;
425 }
426
427 // Ignore tokens whose logical location was not the main file.
428 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
429 std::pair<unsigned, unsigned> LLocInfo =
430 SourceMgr.getDecomposedFileLoc(LLoc);
431
432 if (LLocInfo.first != FileID) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000433 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000434 continue;
435 }
436
437 // Okay, we have the first token of a macro expansion: highlight the
438 // instantiation.
439
440 // Get the size of current macro call itself.
441 // FIXME: This should highlight the args of a function-like
442 // macro, using a heuristic.
443 unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
444
445 unsigned TokOffs = LLocInfo.second;
Chris Lattner6f46be22008-04-17 00:40:45 +0000446 // Highlight the macro invocation itself.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000447 RB.InsertTextAfter(TokOffs, "<span class='macro'>",
448 strlen("<span class='macro'>"));
449 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
450
Ted Kremenekfb586092008-04-18 05:34:33 +0000451 std::string Expansion = PP.getSpelling(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000452 unsigned LineLen = Expansion.size();
453
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000454 // Okay, eat this token, getting the next one.
Ted Kremenekfb586092008-04-18 05:34:33 +0000455 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000456
457 // Skip all the rest of the tokens that are part of this macro
458 // instantiation. It would be really nice to pop up a window with all the
459 // spelling of the tokens or something.
460 while (!Tok.is(tok::eof) &&
Chris Lattner6f46be22008-04-17 00:40:45 +0000461 SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc) {
462 // Insert a newline if the macro expansion is getting large.
463 if (LineLen > 60) {
464 Expansion += "<br>";
465 LineLen = 0;
466 }
467
468 LineLen -= Expansion.size();
Chris Lattner9227c692008-04-17 23:03:14 +0000469 // Escape any special characters in the token text.
Ted Kremenekfb586092008-04-18 05:34:33 +0000470 Expansion += ' ' + EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000471 LineLen += Expansion.size();
Ted Kremenekfb586092008-04-18 05:34:33 +0000472 PP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000473 }
Chris Lattner9227c692008-04-17 23:03:14 +0000474
Chris Lattner6f46be22008-04-17 00:40:45 +0000475 // Insert the information about the expansion inside the macro span.
476 Expansion = "<span class='expansion'>" + Expansion + "</span>";
477 RB.InsertTextBefore(TokOffs+TokLen, Expansion.c_str(), Expansion.size());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000478 }
479}
480
Ted Kremenekfb586092008-04-18 05:34:33 +0000481void html::HighlightMacros(Rewriter &R, unsigned FileID,
482 PreprocessorFactory &PPF) {
483
484 llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
485 HighlightMacros(R, FileID, *PP);
486}