blob: ad7e23716aef841e30a4eccc74c3da874d03c638 [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
Ted Kremenekfa5be362008-04-08 22:37:58 +000024void html::EscapeText(Rewriter& R, unsigned FileID,
25 bool EscapeSpaces, bool ReplaceTabs) {
Ted Kremenek6a340832008-03-18 21:19:49 +000026
27 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
28 const char* C = Buf->getBufferStart();
29 const char* FileEnd = Buf->getBufferEnd();
30
31 assert (C <= FileEnd);
32
Chris Lattner73527142008-04-16 04:33:23 +000033 RewriteBuffer &RB = R.getEditBuffer(FileID);
34
Ted Kremenek6a340832008-03-18 21:19:49 +000035 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek49cd6352008-04-03 07:12:29 +000036
Ted Kremenek6a340832008-03-18 21:19:49 +000037 switch (*C) {
Chris Lattner73527142008-04-16 04:33:23 +000038 default: break;
39
40 case ' ':
41 if (EscapeSpaces)
42 RB.ReplaceText(FilePos, 1, "&nbsp;", 6);
43 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000044
Chris Lattner73527142008-04-16 04:33:23 +000045 case '\t':
46 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +000047 break;
Chris Lattner73527142008-04-16 04:33:23 +000048 if (EscapeSpaces)
49 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;", 6*4);
50 else
51 RB.ReplaceText(FilePos, 1, " ", 4);
52 break;
53
54 case '<':
55 RB.ReplaceText(FilePos, 1, "&lt;", 4);
56 break;
57
58 case '>':
59 RB.ReplaceText(FilePos, 1, "&gt;", 4);
60 break;
61
62 case '&':
63 RB.ReplaceText(FilePos, 1, "&amp;", 5);
64 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000065 }
66 }
67}
68
Ted Kremenekfa5be362008-04-08 22:37:58 +000069std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
70 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +000071
72 unsigned len = s.size();
73 std::ostringstream os;
74
75 for (unsigned i = 0 ; i < len; ++i) {
76
77 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +000078 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +000079 default:
80 os << c; break;
81
82 case ' ':
83 if (EscapeSpaces) os << "&nbsp;";
84 else os << ' ';
85 break;
86
87 case '\t':
88 if (ReplaceTabs)
89 for (unsigned i = 0; i < 4; ++i)
90 os << "&nbsp;";
91 else
92 os << c;
93
Ted Kremenek053ef592008-03-27 17:15:29 +000094 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +000095
96 case '<': os << "&lt;"; break;
97 case '>': os << "&gt;"; break;
98 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +000099 }
100 }
101
102 return os.str();
103}
104
Chris Lattner8570f0b2008-04-16 04:37:29 +0000105static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
106 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000107 llvm::SmallString<100> Str;
108 Str += "<tr><td class=\"num\" id=\"LN";
109 Str.append_uint(LineNo);
110 Str += "\">";
111 Str.append_uint(LineNo);
112 Str += "</td><td class=\"line\">";
113
Ted Kremenek49cd6352008-04-03 07:12:29 +0000114 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000115 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000116 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000117 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000118 RB.InsertTextBefore(B, &Str[0], Str.size());
119 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000120 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000121}
122
123void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
124
125 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
126 const char* FileBeg = Buf->getBufferStart();
127 const char* FileEnd = Buf->getBufferEnd();
128 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000129 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000130
131 assert (C <= FileEnd);
132
133 unsigned LineNo = 0;
134 unsigned FilePos = 0;
135
136 while (C != FileEnd) {
137
138 ++LineNo;
139 unsigned LineStartPos = FilePos;
140 unsigned LineEndPos = FileEnd - FileBeg;
141
142 assert (FilePos <= LineEndPos);
143 assert (C < FileEnd);
144
145 // Scan until the newline (or end-of-file).
146
Ted Kremenek49cd6352008-04-03 07:12:29 +0000147 while (C != FileEnd) {
148 char c = *C;
149 ++C;
150
151 if (c == '\n') {
152 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000153 break;
154 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000155
156 ++FilePos;
157 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000158
Chris Lattner8570f0b2008-04-16 04:37:29 +0000159 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000160 }
161
Chris Lattner8570f0b2008-04-16 04:37:29 +0000162 // Add one big table tag that surrounds all of the code.
163 RB.InsertTextBefore(0, "<table class=\"code\">\n",
164 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000165
Chris Lattner8570f0b2008-04-16 04:37:29 +0000166 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000167}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000168
169void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) {
170
171 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
172 const char* FileStart = Buf->getBufferStart();
173 const char* FileEnd = Buf->getBufferEnd();
174
175 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
176 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
177
178 // Generate header
Ted Kremenek70bcba62008-04-09 15:40:40 +0000179 R.InsertCStrBefore(StartLoc,
180 "<html>\n<head>\n"
181 "<style type=\"text/css\">\n"
182 " body { color:#000000; background-color:#ffffff }\n"
183 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000184 " h1 { font-size:14pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000185 " .code { border-spacing:0px; width:100%; }\n"
186 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
187 " .code { line-height: 1.2em }\n"
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000188 " .comment { color: #A0A0A0 }\n"
189 " .macro { color: #FF0000; background-color:#FFC0C0 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000190 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
191 " .num { text-align:right; font-size: smaller }\n"
192 " .num { color:#444444 }\n"
193 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
194 " .line { white-space: pre }\n"
195 " .msg { background-color:#fff8b4; color:#000000 }\n"
196 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
197 " .msg { -webkit-border-radius:5px }\n"
198 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
199 " .msg { font-weight: bold }\n"
200 " .msg { float:left }\n"
201 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
202 " .msg { margin-top:10px; margin-bottom:10px }\n"
203 " .mrange { background-color:#dfddf3 }\n"
204 " .mrange { border-bottom:1px solid #6F9DBE }\n"
205 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000206 " table.simpletable {\n"
207 " padding: 5px;\n"
208 " font-size:12pt;\n"
209 " margin:20px;\n"
210 " border-collapse: collapse; border-spacing: 0px;\n"
211 " }\n"
212 " td.rowname {\n"
213 " text-align:right; font-weight:bold; color:#444444;\n"
214 " padding-right:2ex; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000215 "</style>\n</head>\n<body>");
216
Ted Kremenekad0a2032008-03-19 06:14:37 +0000217 // Generate footer
218
Ted Kremenek70bcba62008-04-09 15:40:40 +0000219 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000220}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000221
222/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
223/// information about keywords, macro expansions etc. This uses the macro
224/// table state from the end of the file, so it won't be perfectly perfect,
225/// but it will be reasonably close.
226void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) {
227 RewriteBuffer &RB = R.getEditBuffer(FileID);
228
229 // Inform the preprocessor that we want to retain comments as tokens, so we
230 // can highlight them.
231 PP.SetCommentRetentionState(true, false);
232
233 // Start parsing the specified input file.
234 PP.EnterMainSourceFile();
235
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000236 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
237 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000238 const SourceManager &SourceMgr = PP.getSourceManager();
239 Token Tok;
240 do {
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000241 PP.LexUnexpandedToken(Tok);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000242 // Ignore tokens whose logical location was not the main file.
243 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
244 std::pair<unsigned, unsigned> LLocInfo =
245 SourceMgr.getDecomposedFileLoc(LLoc);
246
247 if (LLocInfo.first != FileID)
248 continue;
249
250 unsigned TokOffs = LLocInfo.second;
251 unsigned TokLen = Tok.getLength();
252 switch (Tok.getKind()) {
253 default: break;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000254 // FIXME: Add keywords here.
255
Chris Lattner3245a0a2008-04-16 06:11:58 +0000256 case tok::comment:
257 RB.InsertTextAfter(TokOffs, "<span class='comment'>",
258 strlen("<span class='comment'>"));
259 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
260 break;
261 }
262
263 } while (Tok.isNot(tok::eof));
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000264 PP.SetCommentRetentionState(false, false);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000265}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000266
267/// HighlightMacros - This uses the macro table state from the end of the
268/// file, to reexpand macros and insert (into the HTML) information about the
269/// macro expansions. This won't be perfectly perfect, but it will be
270/// reasonably close.
271void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP) {
272 RewriteBuffer &RB = R.getEditBuffer(FileID);
273
274 // Inform the preprocessor that we don't want comments.
275 PP.SetCommentRetentionState(false, false);
276
277 // Start parsing the specified input file.
278 PP.EnterMainSourceFile();
279
280 // Lex all the tokens.
281 const SourceManager &SourceMgr = PP.getSourceManager();
282 Token Tok;
283 PP.Lex(Tok);
284 while (Tok.isNot(tok::eof)) {
285 // Ignore non-macro tokens.
286 if (!Tok.getLocation().isMacroID()) {
287 PP.Lex(Tok);
288 continue;
289 }
290
291 // Ignore tokens whose logical location was not the main file.
292 SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
293 std::pair<unsigned, unsigned> LLocInfo =
294 SourceMgr.getDecomposedFileLoc(LLoc);
295
296 if (LLocInfo.first != FileID) {
297 PP.Lex(Tok);
298 continue;
299 }
300
301 // Okay, we have the first token of a macro expansion: highlight the
302 // instantiation.
303
304 // Get the size of current macro call itself.
305 // FIXME: This should highlight the args of a function-like
306 // macro, using a heuristic.
307 unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
308
309 unsigned TokOffs = LLocInfo.second;
310 RB.InsertTextAfter(TokOffs, "<span class='macro'>",
311 strlen("<span class='macro'>"));
312 RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
313
314 // Okay, eat this token, getting the next one.
315 PP.Lex(Tok);
316
317 // Skip all the rest of the tokens that are part of this macro
318 // instantiation. It would be really nice to pop up a window with all the
319 // spelling of the tokens or something.
320 while (!Tok.is(tok::eof) &&
321 SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc)
322 PP.Lex(Tok);
323 }
324}
325
326