blob: a21faa18fa684dd6ac4e994cf94f6e3d8f994574 [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
15#include "clang/Rewrite/Rewriter.h"
16#include "clang/Rewrite/HTMLRewrite.h"
Chris Lattner3245a0a2008-04-16 06:11:58 +000017#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000019#include "llvm/ADT/SmallString.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000020#include "llvm/Support/MemoryBuffer.h"
21#include <sstream>
Ted Kremenek6a340832008-03-18 21:19:49 +000022using namespace clang;
23
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000024/// HighlightRange - Highlight a range in the source code with the specified
25/// start/end tags. B/E must be in the same file. This ensures that
26/// start/end tags are placed at the start/end of each line if the range is
27/// multiline.
28void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
29 const char *StartTag, const char *EndTag) {
30 SourceManager &SM = R.getSourceMgr();
31 B = SM.getLogicalLoc(B);
32 E = SM.getLogicalLoc(E);
33 unsigned FileID = SM.getCanonicalFileID(B);
34 assert(SM.getCanonicalFileID(E) == FileID && "B/E not in the same file!");
35
36 unsigned BOffset = SM.getFullFilePos(B);
37 unsigned EOffset = SM.getFullFilePos(E);
38
39 // Include the whole end token in the range.
40 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr());
41
42 HighlightRange(R.getEditBuffer(FileID), BOffset, EOffset,
43 SM.getBufferData(FileID).first, StartTag, EndTag);
44}
45
46/// HighlightRange - This is the same as the above method, but takes
47/// decomposed file locations.
48void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
49 const char *BufferStart,
50 const char *StartTag, const char *EndTag) {
51 RB.InsertTextAfter(B, StartTag, strlen(StartTag));
52 RB.InsertTextBefore(E, EndTag, strlen(EndTag));
53
54}
55
Ted Kremenekfa5be362008-04-08 22:37:58 +000056void html::EscapeText(Rewriter& R, unsigned FileID,
57 bool EscapeSpaces, bool ReplaceTabs) {
Ted Kremenek6a340832008-03-18 21:19:49 +000058
59 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
60 const char* C = Buf->getBufferStart();
61 const char* FileEnd = Buf->getBufferEnd();
62
63 assert (C <= FileEnd);
64
Chris Lattner73527142008-04-16 04:33:23 +000065 RewriteBuffer &RB = R.getEditBuffer(FileID);
66
Ted Kremenek6a340832008-03-18 21:19:49 +000067 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek49cd6352008-04-03 07:12:29 +000068
Ted Kremenek6a340832008-03-18 21:19:49 +000069 switch (*C) {
Chris Lattner73527142008-04-16 04:33:23 +000070 default: break;
71
72 case ' ':
73 if (EscapeSpaces)
74 RB.ReplaceText(FilePos, 1, "&nbsp;", 6);
75 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000076
Chris Lattner73527142008-04-16 04:33:23 +000077 case '\t':
78 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +000079 break;
Chris Lattner73527142008-04-16 04:33:23 +000080 if (EscapeSpaces)
81 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;", 6*4);
82 else
83 RB.ReplaceText(FilePos, 1, " ", 4);
84 break;
85
86 case '<':
87 RB.ReplaceText(FilePos, 1, "&lt;", 4);
88 break;
89
90 case '>':
91 RB.ReplaceText(FilePos, 1, "&gt;", 4);
92 break;
93
94 case '&':
95 RB.ReplaceText(FilePos, 1, "&amp;", 5);
96 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000097 }
98 }
99}
100
Ted Kremenekfa5be362008-04-08 22:37:58 +0000101std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
102 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +0000103
104 unsigned len = s.size();
105 std::ostringstream os;
106
107 for (unsigned i = 0 ; i < len; ++i) {
108
109 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000110 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000111 default:
112 os << c; break;
113
114 case ' ':
115 if (EscapeSpaces) os << "&nbsp;";
116 else os << ' ';
117 break;
118
119 case '\t':
120 if (ReplaceTabs)
121 for (unsigned i = 0; i < 4; ++i)
122 os << "&nbsp;";
123 else
124 os << c;
125
Ted Kremenek053ef592008-03-27 17:15:29 +0000126 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000127
128 case '<': os << "&lt;"; break;
129 case '>': os << "&gt;"; break;
130 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000131 }
132 }
133
134 return os.str();
135}
136
Chris Lattner8570f0b2008-04-16 04:37:29 +0000137static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
138 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000139 llvm::SmallString<100> Str;
140 Str += "<tr><td class=\"num\" id=\"LN";
141 Str.append_uint(LineNo);
142 Str += "\">";
143 Str.append_uint(LineNo);
144 Str += "</td><td class=\"line\">";
145
Ted Kremenek49cd6352008-04-03 07:12:29 +0000146 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000147 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000148 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000149 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000150 RB.InsertTextBefore(B, &Str[0], Str.size());
151 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000152 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000153}
154
155void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
156
157 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
158 const char* FileBeg = Buf->getBufferStart();
159 const char* FileEnd = Buf->getBufferEnd();
160 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000161 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000162
163 assert (C <= FileEnd);
164
165 unsigned LineNo = 0;
166 unsigned FilePos = 0;
167
168 while (C != FileEnd) {
169
170 ++LineNo;
171 unsigned LineStartPos = FilePos;
172 unsigned LineEndPos = FileEnd - FileBeg;
173
174 assert (FilePos <= LineEndPos);
175 assert (C < FileEnd);
176
177 // Scan until the newline (or end-of-file).
178
Ted Kremenek49cd6352008-04-03 07:12:29 +0000179 while (C != FileEnd) {
180 char c = *C;
181 ++C;
182
183 if (c == '\n') {
184 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000185 break;
186 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000187
188 ++FilePos;
189 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000190
Chris Lattner8570f0b2008-04-16 04:37:29 +0000191 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000192 }
193
Chris Lattner8570f0b2008-04-16 04:37:29 +0000194 // Add one big table tag that surrounds all of the code.
195 RB.InsertTextBefore(0, "<table class=\"code\">\n",
196 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000197
Chris Lattner8570f0b2008-04-16 04:37:29 +0000198 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000199}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000200
201void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) {
202
203 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
204 const char* FileStart = Buf->getBufferStart();
205 const char* FileEnd = Buf->getBufferEnd();
206
207 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
208 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
209
210 // Generate header
Ted Kremenek70bcba62008-04-09 15:40:40 +0000211 R.InsertCStrBefore(StartLoc,
212 "<html>\n<head>\n"
213 "<style type=\"text/css\">\n"
214 " body { color:#000000; background-color:#ffffff }\n"
215 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000216 " h1 { font-size:14pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000217 " .code { border-spacing:0px; width:100%; }\n"
218 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
219 " .code { line-height: 1.2em }\n"
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000220 " .comment { color: #A0A0A0; font-style: oblique }\n"
Chris Lattnerc4586c22008-04-16 06:35:07 +0000221 " .keyword { color: #FF00FF }\n"
Chris Lattner74ea3e52008-04-16 06:53:09 +0000222 " .directive { color: #FFFF00 }\n"
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000223 " .macro { color: #FF0000; background-color:#FFC0C0 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000224 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
225 " .num { text-align:right; font-size: smaller }\n"
226 " .num { color:#444444 }\n"
227 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
228 " .line { white-space: pre }\n"
229 " .msg { background-color:#fff8b4; color:#000000 }\n"
230 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
231 " .msg { -webkit-border-radius:5px }\n"
232 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
233 " .msg { font-weight: bold }\n"
234 " .msg { float:left }\n"
235 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
236 " .msg { margin-top:10px; margin-bottom:10px }\n"
237 " .mrange { background-color:#dfddf3 }\n"
238 " .mrange { border-bottom:1px solid #6F9DBE }\n"
239 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000240 " table.simpletable {\n"
241 " padding: 5px;\n"
242 " font-size:12pt;\n"
243 " margin:20px;\n"
244 " border-collapse: collapse; border-spacing: 0px;\n"
245 " }\n"
246 " td.rowname {\n"
247 " text-align:right; font-weight:bold; color:#444444;\n"
248 " padding-right:2ex; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000249 "</style>\n</head>\n<body>");
250
Ted Kremenekad0a2032008-03-19 06:14:37 +0000251 // Generate footer
252
Ted Kremenek70bcba62008-04-09 15:40:40 +0000253 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000254}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000255
256/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
257/// information about keywords, macro expansions etc. This uses the macro
258/// table state from the end of the file, so it won't be perfectly perfect,
259/// but it will be reasonably close.
260void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
261 RewriteBuffer &RB = R.getEditBuffer(FileID);
262
Chris Lattnera745e8c2008-04-16 20:51:51 +0000263 const SourceManager &SourceMgr = PP.getSourceManager();
264 std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID);
265 const char *BufferStart = File.first;
266
267 Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(),
268 File.first, File.second);
269
Chris Lattner3245a0a2008-04-16 06:11:58 +0000270 // Inform the preprocessor that we want to retain comments as tokens, so we
271 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000272 L.SetCommentRetentionState(true);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000273
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000274 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
275 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000276 Token Tok;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000277 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000278
Chris Lattner74ea3e52008-04-16 06:53:09 +0000279 while (Tok.isNot(tok::eof)) {
280 // Since we are lexing unexpanded tokens, all tokens are from the main
281 // FileID.
282 unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000283 unsigned TokLen = Tok.getLength();
284 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000285 default: break;
286 case tok::identifier: {
287 // Fill in Result.IdentifierInfo, looking up the identifier in the
288 // identifier table.
289 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
290
291 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000292 if (II->getTokenID() != tok::identifier)
293 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
294 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000295 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000296 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000297 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000298 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
299 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000300 break;
Chris Lattner74ea3e52008-04-16 06:53:09 +0000301 case tok::hash:
302 // FIXME: This isn't working because we're not in raw mode in the lexer.
303 // Just cons up our own lexer here?
304
305 // If this is a preprocessor directive, all tokens to end of line are too.
306 if (Tok.isAtStartOfLine()) {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000307 // Find end of line. This is a hack.
308 const char *LineEnd = SourceMgr.getCharacterData(Tok.getLocation());
309 unsigned TokEnd = TokOffs+strcspn(LineEnd, "\n\r");
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000310 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
311 "<span class='directive'>", "</span>");
Chris Lattner74ea3e52008-04-16 06:53:09 +0000312 }
313 break;
Chris Lattner3245a0a2008-04-16 06:11:58 +0000314 }
315
Chris Lattnera745e8c2008-04-16 20:51:51 +0000316 L.LexRawToken(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000317 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000318}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000319
320/// HighlightMacros - This uses the macro table state from the end of the
321/// file, to reexpand macros and insert (into the HTML) information about the
322/// macro expansions. This won't be perfectly perfect, but it will be
323/// reasonably close.
324void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP) {
325 RewriteBuffer &RB = R.getEditBuffer(FileID);
326
327 // Inform the preprocessor that we don't want comments.
328 PP.SetCommentRetentionState(false, false);
329
330 // Start parsing the specified input file.
331 PP.EnterMainSourceFile();
332
333 // Lex all the tokens.
334 const SourceManager &SourceMgr = PP.getSourceManager();
335 Token Tok;
336 PP.Lex(Tok);
337 while (Tok.isNot(tok::eof)) {
338 // Ignore non-macro tokens.
339 if (!Tok.getLocation().isMacroID()) {
340 PP.Lex(Tok);
341 continue;
342 }
343
344 // Ignore tokens whose logical location was not the main file.
345 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
346 std::pair<unsigned, unsigned> LLocInfo =
347 SourceMgr.getDecomposedFileLoc(LLoc);
348
349 if (LLocInfo.first != FileID) {
350 PP.Lex(Tok);
351 continue;
352 }
353
354 // Okay, we have the first token of a macro expansion: highlight the
355 // instantiation.
356
357 // Get the size of current macro call itself.
358 // FIXME: This should highlight the args of a function-like
359 // macro, using a heuristic.
360 unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
361
362 unsigned TokOffs = LLocInfo.second;
363 RB.InsertTextAfter(TokOffs, "<span class='macro'>",
364 strlen("<span class='macro'>"));
365 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
366
367 // Okay, eat this token, getting the next one.
368 PP.Lex(Tok);
369
370 // Skip all the rest of the tokens that are part of this macro
371 // instantiation. It would be really nice to pop up a window with all the
372 // spelling of the tokens or something.
373 while (!Tok.is(tok::eof) &&
374 SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc)
375 PP.Lex(Tok);
376 }
377}
378
379