blob: 68edda222b28e55d35a9c6e9e73963ae8b98e1ac [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.
Chris Lattner2c78b872009-04-14 23:22:57 +000044 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000045
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
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000180 case '\t':
181 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 }
189 else
190 os << c;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000191
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000192 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000193
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000194 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) {
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000205 llvm::SmallString<256> Str;
206 llvm::raw_svector_ostream OS(Str);
207
208 OS << "<tr><td class=\"num\" id=\"LN"
209 << LineNo << "\">"
210 << LineNo << "</td><td class=\"line\">";
Chris Lattner57df3b92008-04-16 04:11:35 +0000211
Ted Kremenek49cd6352008-04-03 07:12:29 +0000212 if (B == E) { // Handle empty lines.
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000213 OS << " </td></tr>";
214 OS.flush();
Chris Lattner8570f0b2008-04-16 04:37:29 +0000215 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000216 } else {
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000217 OS.flush();
Chris Lattner8570f0b2008-04-16 04:37:29 +0000218 RB.InsertTextBefore(B, &Str[0], Str.size());
219 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000220 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000221}
222
Chris Lattner2b2453a2009-01-17 06:22:33 +0000223void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000224
Chris Lattner2b2453a2009-01-17 06:22:33 +0000225 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000226 const char* FileBeg = Buf->getBufferStart();
227 const char* FileEnd = Buf->getBufferEnd();
228 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000229 RewriteBuffer &RB = R.getEditBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000230
231 assert (C <= FileEnd);
232
233 unsigned LineNo = 0;
234 unsigned FilePos = 0;
235
236 while (C != FileEnd) {
237
238 ++LineNo;
239 unsigned LineStartPos = FilePos;
240 unsigned LineEndPos = FileEnd - FileBeg;
241
242 assert (FilePos <= LineEndPos);
243 assert (C < FileEnd);
244
245 // Scan until the newline (or end-of-file).
246
Ted Kremenek49cd6352008-04-03 07:12:29 +0000247 while (C != FileEnd) {
248 char c = *C;
249 ++C;
250
251 if (c == '\n') {
252 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000253 break;
254 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000255
256 ++FilePos;
257 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000258
Chris Lattner8570f0b2008-04-16 04:37:29 +0000259 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000260 }
261
Chris Lattner8570f0b2008-04-16 04:37:29 +0000262 // Add one big table tag that surrounds all of the code.
263 RB.InsertTextBefore(0, "<table class=\"code\">\n",
264 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000265
Chris Lattner8570f0b2008-04-16 04:37:29 +0000266 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000267}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000268
Chris Lattner2b2453a2009-01-17 06:22:33 +0000269void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000270 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000271
Chris Lattner2b2453a2009-01-17 06:22:33 +0000272 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000273 const char* FileStart = Buf->getBufferStart();
274 const char* FileEnd = Buf->getBufferEnd();
275
Chris Lattner2b2453a2009-01-17 06:22:33 +0000276 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
277 SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000278
Ted Kremeneka95d3752008-09-13 05:16:45 +0000279 std::string s;
280 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000281 os << "<!doctype html>\n" // Use HTML 5 doctype
282 "<html>\n<head>\n";
283
284 if (title)
285 os << "<title>" << html::EscapeText(title) << "</title>\n";
286
287 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000288 " body { color:#000000; background-color:#ffffff }\n"
289 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000290 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000291 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000292 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
293 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000294 " .comment { color: green; font-style: oblique }\n"
295 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000296 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000297 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000298 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000299 " .expansion { display: none; }\n"
300 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000301 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000302 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000303 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000304 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000305 // Macros are position: relative to provide base for expansions.
306 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000307 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000308 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000309 " .num { color:#444444 }\n"
310 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
311 " .line { white-space: pre }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000312 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
313 " .msg { -webkit-border-radius:5px }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000314 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000315 " .msg { float:left }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000316 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000317 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000318 " .msg { font-weight:bold }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000319 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
320 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000321 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000322 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000323 " .mrange { background-color:#dfddf3 }\n"
324 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000325 " .PathIndex { font-weight: bold; padding:0px 5px 0px 5px; "
326 "margin-right:5px; }\n"
Ted Kremenek00f01e42009-03-02 23:39:27 +0000327 " .PathIndex { -webkit-border-radius:8px }\n"
328 " .PathIndexEvent { background-color:#bfba87 }\n"
329 " .PathIndexControl { background-color:#8c8c8c }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000330 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
331 " .CodeRemovalHint { background-color:#de1010 }\n"
332 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000333 " table.simpletable {\n"
334 " padding: 5px;\n"
335 " font-size:12pt;\n"
336 " margin:20px;\n"
337 " border-collapse: collapse; border-spacing: 0px;\n"
338 " }\n"
339 " td.rowname {\n"
340 " text-align:right; font-weight:bold; color:#444444;\n"
341 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000342 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000343
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000344 // Generate header
345 R.InsertStrBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000346 // Generate footer
347
Ted Kremenek70bcba62008-04-09 15:40:40 +0000348 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000349}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000350
351/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
352/// information about keywords, macro expansions etc. This uses the macro
353/// table state from the end of the file, so it won't be perfectly perfect,
354/// but it will be reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000355void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
356 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000357
Chris Lattner05db4272009-02-13 19:33:24 +0000358 const SourceManager &SM = PP.getSourceManager();
359 Lexer L(FID, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000360 const char *BufferStart = L.getBufferStart();
Chris Lattnera745e8c2008-04-16 20:51:51 +0000361
Chris Lattner3245a0a2008-04-16 06:11:58 +0000362 // Inform the preprocessor that we want to retain comments as tokens, so we
363 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000364 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000365
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000366 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
367 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000368 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000369 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000370
Chris Lattner74ea3e52008-04-16 06:53:09 +0000371 while (Tok.isNot(tok::eof)) {
372 // Since we are lexing unexpanded tokens, all tokens are from the main
373 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000374 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000375 unsigned TokLen = Tok.getLength();
376 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000377 default: break;
378 case tok::identifier: {
379 // Fill in Result.IdentifierInfo, looking up the identifier in the
380 // identifier table.
381 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
382
383 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000384 if (II->getTokenID() != tok::identifier)
385 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
386 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000387 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000388 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000389 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000390 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
391 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000392 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000393 case tok::wide_string_literal:
394 // Chop off the L prefix
395 ++TokOffs;
396 --TokLen;
397 // FALL THROUGH.
398 case tok::string_literal:
399 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
400 "<span class='string_literal'>", "</span>");
401 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000402 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000403 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000404 if (!Tok.isAtStartOfLine())
405 break;
406
407 // Eat all of the tokens until we get to the next one at the start of
408 // line.
409 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000410 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000411 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000412 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000413 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000414 }
Chris Lattner5deb96d2008-04-16 23:21:17 +0000415
416 // Find end of line. This is a hack.
417 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
418 "<span class='directive'>", "</span>");
419
420 // Don't skip the next token.
421 continue;
422 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000423 }
424
Chris Lattner590f0cc2008-10-12 01:15:46 +0000425 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000426 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000427}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000428
Chris Lattner7c175fb2009-03-13 21:44:46 +0000429namespace {
430/// IgnoringDiagClient - This is a diagnostic client that just ignores all
431/// diags.
432class IgnoringDiagClient : public DiagnosticClient {
433 void HandleDiagnostic(Diagnostic::Level DiagLevel,
434 const DiagnosticInfo &Info) {
435 // Just ignore it.
436 }
437};
438}
439
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000440/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000441/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000442/// macro expansions. This won't be perfectly perfect, but it will be
443/// reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000444void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000445 // Re-lex the raw token stream into a token buffer.
446 const SourceManager &SM = PP.getSourceManager();
447 std::vector<Token> TokenStream;
448
449 Lexer L(FID, SM, PP.getLangOptions());
450
451 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
452 // macros.
453 while (1) {
454 Token Tok;
455 L.LexFromRawLexer(Tok);
456
457 // If this is a # at the start of a line, discard it from the token stream.
458 // We don't want the re-preprocess step to see #defines, #includes or other
459 // preprocessor directives.
460 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
461 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000462
463 // If this is a ## token, change its kind to unknown so that repreprocessing
464 // it will not produce an error.
465 if (Tok.is(tok::hashhash))
466 Tok.setKind(tok::unknown);
Chris Lattner05db4272009-02-13 19:33:24 +0000467
468 // If this raw token is an identifier, the raw lexer won't have looked up
469 // the corresponding identifier info for it. Do this now so that it will be
470 // macro expanded when we re-preprocess it.
471 if (Tok.is(tok::identifier)) {
472 // Change the kind of this identifier to the appropriate token kind, e.g.
473 // turning "for" into a keyword.
474 Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID());
475 }
476
477 TokenStream.push_back(Tok);
478
479 if (Tok.is(tok::eof)) break;
480 }
481
Chris Lattner7c175fb2009-03-13 21:44:46 +0000482 // Temporarily change the diagnostics object so that we ignore any generated
483 // diagnostics from this pass.
484 IgnoringDiagClient TmpDC;
485 Diagnostic TmpDiags(&TmpDC);
486
487 Diagnostic *OldDiags = &PP.getDiagnostics();
488 PP.setDiagnostics(TmpDiags);
489
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000490 // Inform the preprocessor that we don't want comments.
Ted Kremenekfb586092008-04-18 05:34:33 +0000491 PP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000492
493 // Enter the tokens we just lexed. This will cause them to be macro expanded
494 // but won't enter sub-files (because we removed #'s).
495 PP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000496
Chris Lattner867924d2009-02-13 00:51:30 +0000497 TokenConcatenation ConcatInfo(PP);
498
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000499 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000500 Token Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000501 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000502 while (Tok.isNot(tok::eof)) {
503 // Ignore non-macro tokens.
504 if (!Tok.getLocation().isMacroID()) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000505 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000506 continue;
507 }
508
Chris Lattnerb7949a92009-02-15 21:32:34 +0000509 // Okay, we have the first token of a macro expansion: highlight the
510 // instantiation by inserting a start tag before the macro instantiation and
511 // end tag after it.
512 std::pair<SourceLocation, SourceLocation> LLoc =
513 SM.getInstantiationRange(Tok.getLocation());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000514
Chris Lattnerb7949a92009-02-15 21:32:34 +0000515 // Ignore tokens whose instantiation location was not the main file.
516 if (SM.getFileID(LLoc.first) != FID) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000517 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000518 continue;
519 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000520
Chris Lattnerb7949a92009-02-15 21:32:34 +0000521 assert(SM.getFileID(LLoc.second) == FID &&
522 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000523
Ted Kremenekb83ded02009-07-21 21:16:46 +0000524 std::string Expansion = EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000525 unsigned LineLen = Expansion.size();
526
Chris Lattner867924d2009-02-13 00:51:30 +0000527 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000528 // Okay, eat this token, getting the next one.
Ted Kremenekfb586092008-04-18 05:34:33 +0000529 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000530
531 // Skip all the rest of the tokens that are part of this macro
532 // instantiation. It would be really nice to pop up a window with all the
533 // spelling of the tokens or something.
534 while (!Tok.is(tok::eof) &&
Chris Lattnerb7949a92009-02-15 21:32:34 +0000535 SM.getInstantiationLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000536 // Insert a newline if the macro expansion is getting large.
537 if (LineLen > 60) {
538 Expansion += "<br>";
539 LineLen = 0;
540 }
541
542 LineLen -= Expansion.size();
Chris Lattner867924d2009-02-13 00:51:30 +0000543
544 // If the tokens were already space separated, or if they must be to avoid
545 // them being implicitly pasted, add a space between them.
546 if (Tok.hasLeadingSpace() ||
547 ConcatInfo.AvoidConcat(PrevTok, Tok))
548 Expansion += ' ';
549
Chris Lattner9227c692008-04-17 23:03:14 +0000550 // Escape any special characters in the token text.
Chris Lattner867924d2009-02-13 00:51:30 +0000551 Expansion += EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000552 LineLen += Expansion.size();
Chris Lattner867924d2009-02-13 00:51:30 +0000553
554 PrevTok = Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000555 PP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000556 }
Chris Lattner9227c692008-04-17 23:03:14 +0000557
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000558
559 // Insert the expansion as the end tag, so that multi-line macros all get
560 // highlighted.
561 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
562
563 HighlightRange(R, LLoc.first, LLoc.second,
564 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000565 }
Chris Lattner7c175fb2009-03-13 21:44:46 +0000566
567 // Restore diagnostics object back to its own thing.
568 PP.setDiagnostics(*OldDiags);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000569}
570
Chris Lattner2b2453a2009-01-17 06:22:33 +0000571void html::HighlightMacros(Rewriter &R, FileID FID,
Ted Kremenekfb586092008-04-18 05:34:33 +0000572 PreprocessorFactory &PPF) {
573
574 llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor());
Chris Lattner2b2453a2009-01-17 06:22:33 +0000575 HighlightMacros(R, FID, *PP);
Ted Kremenekfb586092008-04-18 05:34:33 +0000576}