blob: 43c04793f0855734ab1564722f6d64b01c50b0ea [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':
Nico Weber63366302008-08-16 22:24:33 +0000179 if (ReplaceTabs) {
180 if (EscapeSpaces)
181 for (unsigned i = 0; i < 4; ++i)
182 os << "&nbsp;";
183 else
184 for (unsigned i = 0; i < 4; ++i)
185 os << " ";
186 }
Chris Lattner8570f0b2008-04-16 04:37:29 +0000187 else
188 os << c;
189
Ted Kremenek053ef592008-03-27 17:15:29 +0000190 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000191
192 case '<': os << "&lt;"; break;
193 case '>': os << "&gt;"; break;
194 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000195 }
196 }
197
198 return os.str();
199}
200
Chris Lattner8570f0b2008-04-16 04:37:29 +0000201static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
202 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000203 llvm::SmallString<100> Str;
204 Str += "<tr><td class=\"num\" id=\"LN";
205 Str.append_uint(LineNo);
206 Str += "\">";
207 Str.append_uint(LineNo);
208 Str += "</td><td class=\"line\">";
209
Ted Kremenek49cd6352008-04-03 07:12:29 +0000210 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000211 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000212 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000213 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000214 RB.InsertTextBefore(B, &Str[0], Str.size());
215 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000216 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000217}
218
219void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
220
221 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
222 const char* FileBeg = Buf->getBufferStart();
223 const char* FileEnd = Buf->getBufferEnd();
224 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000225 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000226
227 assert (C <= FileEnd);
228
229 unsigned LineNo = 0;
230 unsigned FilePos = 0;
231
232 while (C != FileEnd) {
233
234 ++LineNo;
235 unsigned LineStartPos = FilePos;
236 unsigned LineEndPos = FileEnd - FileBeg;
237
238 assert (FilePos <= LineEndPos);
239 assert (C < FileEnd);
240
241 // Scan until the newline (or end-of-file).
242
Ted Kremenek49cd6352008-04-03 07:12:29 +0000243 while (C != FileEnd) {
244 char c = *C;
245 ++C;
246
247 if (c == '\n') {
248 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000249 break;
250 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000251
252 ++FilePos;
253 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000254
Chris Lattner8570f0b2008-04-16 04:37:29 +0000255 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000256 }
257
Chris Lattner8570f0b2008-04-16 04:37:29 +0000258 // Add one big table tag that surrounds all of the code.
259 RB.InsertTextBefore(0, "<table class=\"code\">\n",
260 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000261
Chris Lattner8570f0b2008-04-16 04:37:29 +0000262 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000263}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000264
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000265void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID,
266 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000267
268 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
269 const char* FileStart = Buf->getBufferStart();
270 const char* FileEnd = Buf->getBufferEnd();
271
272 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
273 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
274
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000275 std::ostringstream os;
276 os << "<!doctype html>\n" // Use HTML 5 doctype
277 "<html>\n<head>\n";
278
279 if (title)
280 os << "<title>" << html::EscapeText(title) << "</title>\n";
281
282 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000283 " body { color:#000000; background-color:#ffffff }\n"
284 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000285 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000286 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000287 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
288 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000289 " .comment { color: green; font-style: oblique }\n"
290 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000291 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000292 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000293 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000294 " .expansion { display: none; }\n"
295 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000296 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000297 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000298 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000299 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000300 // Macros are position: relative to provide base for expansions.
301 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000302 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
303 " .num { text-align:right; font-size: smaller }\n"
304 " .num { color:#444444 }\n"
305 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
306 " .line { white-space: pre }\n"
307 " .msg { background-color:#fff8b4; color:#000000 }\n"
308 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
309 " .msg { -webkit-border-radius:5px }\n"
310 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
311 " .msg { font-weight: bold }\n"
312 " .msg { float:left }\n"
313 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
314 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek170db7c2008-07-11 23:13:22 +0000315 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap;}\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000316 " .mrange { background-color:#dfddf3 }\n"
317 " .mrange { border-bottom:1px solid #6F9DBE }\n"
318 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000319 " table.simpletable {\n"
320 " padding: 5px;\n"
321 " font-size:12pt;\n"
322 " margin:20px;\n"
323 " border-collapse: collapse; border-spacing: 0px;\n"
324 " }\n"
325 " td.rowname {\n"
326 " text-align:right; font-weight:bold; color:#444444;\n"
327 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000328 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000329
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000330 // Generate header
331 R.InsertStrBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000332 // Generate footer
333
Ted Kremenek70bcba62008-04-09 15:40:40 +0000334 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000335}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000336
337/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
338/// information about keywords, macro expansions etc. This uses the macro
339/// table state from the end of the file, so it won't be perfectly perfect,
340/// but it will be reasonably close.
341void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
342 RewriteBuffer &RB = R.getEditBuffer(FileID);
343
Chris Lattnera745e8c2008-04-16 20:51:51 +0000344 const SourceManager &SourceMgr = PP.getSourceManager();
345 std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID);
346 const char *BufferStart = File.first;
347
348 Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(),
349 File.first, File.second);
350
Chris Lattner3245a0a2008-04-16 06:11:58 +0000351 // Inform the preprocessor that we want to retain comments as tokens, so we
352 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000353 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000354
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000355 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
356 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000357 Token Tok;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000358 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000359
Chris Lattner74ea3e52008-04-16 06:53:09 +0000360 while (Tok.isNot(tok::eof)) {
361 // Since we are lexing unexpanded tokens, all tokens are from the main
362 // FileID.
363 unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000364 unsigned TokLen = Tok.getLength();
365 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000366 default: break;
367 case tok::identifier: {
368 // Fill in Result.IdentifierInfo, looking up the identifier in the
369 // identifier table.
370 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
371
372 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000373 if (II->getTokenID() != tok::identifier)
374 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
375 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000376 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000377 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000378 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000379 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
380 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000381 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000382 case tok::wide_string_literal:
383 // Chop off the L prefix
384 ++TokOffs;
385 --TokLen;
386 // FALL THROUGH.
387 case tok::string_literal:
388 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
389 "<span class='string_literal'>", "</span>");
390 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000391 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000392 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000393 if (!Tok.isAtStartOfLine())
394 break;
395
396 // Eat all of the tokens until we get to the next one at the start of
397 // line.
398 unsigned TokEnd = TokOffs+TokLen;
399 L.LexRawToken(Tok);
400 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
401 TokEnd = SourceMgr.getFullFilePos(Tok.getLocation())+Tok.getLength();
402 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000403 }
Chris Lattner5deb96d2008-04-16 23:21:17 +0000404
405 // Find end of line. This is a hack.
406 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
407 "<span class='directive'>", "</span>");
408
409 // Don't skip the next token.
410 continue;
411 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000412 }
413
Chris Lattnera745e8c2008-04-16 20:51:51 +0000414 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000415 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000416}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000417
418/// HighlightMacros - This uses the macro table state from the end of the
419/// file, to reexpand macros and insert (into the HTML) information about the
420/// macro expansions. This won't be perfectly perfect, but it will be
421/// reasonably close.
Ted Kremenekfb586092008-04-18 05:34:33 +0000422void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor& PP) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000423
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000424 RewriteBuffer &RB = R.getEditBuffer(FileID);
425
426 // Inform the preprocessor that we don't want comments.
Ted Kremenekfb586092008-04-18 05:34:33 +0000427 PP.SetCommentRetentionState(false, false);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000428
429 // Start parsing the specified input file.
Ted Kremenekfb586092008-04-18 05:34:33 +0000430 PP.EnterMainSourceFile();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000431
432 // Lex all the tokens.
Ted Kremenekfb586092008-04-18 05:34:33 +0000433 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000434 Token Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000435 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000436 while (Tok.isNot(tok::eof)) {
437 // Ignore non-macro tokens.
438 if (!Tok.getLocation().isMacroID()) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000439 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000440 continue;
441 }
442
443 // Ignore tokens whose logical location was not the main file.
444 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
445 std::pair<unsigned, unsigned> LLocInfo =
446 SourceMgr.getDecomposedFileLoc(LLoc);
447
448 if (LLocInfo.first != FileID) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000449 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000450 continue;
451 }
452
453 // Okay, we have the first token of a macro expansion: highlight the
454 // instantiation.
455
456 // Get the size of current macro call itself.
457 // FIXME: This should highlight the args of a function-like
458 // macro, using a heuristic.
459 unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
460
461 unsigned TokOffs = LLocInfo.second;
Chris Lattner6f46be22008-04-17 00:40:45 +0000462 // Highlight the macro invocation itself.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000463 RB.InsertTextAfter(TokOffs, "<span class='macro'>",
464 strlen("<span class='macro'>"));
465 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
466
Ted Kremenekfb586092008-04-18 05:34:33 +0000467 std::string Expansion = PP.getSpelling(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000468 unsigned LineLen = Expansion.size();
469
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000470 // Okay, eat this token, getting the next one.
Ted Kremenekfb586092008-04-18 05:34:33 +0000471 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000472
473 // Skip all the rest of the tokens that are part of this macro
474 // instantiation. It would be really nice to pop up a window with all the
475 // spelling of the tokens or something.
476 while (!Tok.is(tok::eof) &&
Chris Lattner6f46be22008-04-17 00:40:45 +0000477 SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc) {
478 // Insert a newline if the macro expansion is getting large.
479 if (LineLen > 60) {
480 Expansion += "<br>";
481 LineLen = 0;
482 }
483
484 LineLen -= Expansion.size();
Chris Lattner9227c692008-04-17 23:03:14 +0000485 // Escape any special characters in the token text.
Ted Kremenekfb586092008-04-18 05:34:33 +0000486 Expansion += ' ' + EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000487 LineLen += Expansion.size();
Ted Kremenekfb586092008-04-18 05:34:33 +0000488 PP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000489 }
Chris Lattner9227c692008-04-17 23:03:14 +0000490
Chris Lattner6f46be22008-04-17 00:40:45 +0000491 // Insert the information about the expansion inside the macro span.
492 Expansion = "<span class='expansion'>" + Expansion + "</span>";
493 RB.InsertTextBefore(TokOffs+TokLen, Expansion.c_str(), Expansion.size());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000494 }
495}
496
Ted Kremenekfb586092008-04-18 05:34:33 +0000497void html::HighlightMacros(Rewriter &R, unsigned FileID,
498 PreprocessorFactory &PPF) {
499
500 llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
501 HighlightMacros(R, FileID, *PP);
502}