Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1 | //== 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 Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include <sstream> |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 24 | void html::EscapeText(Rewriter& R, unsigned FileID, |
| 25 | bool EscapeSpaces, bool ReplaceTabs) { |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 26 | |
| 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 Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 33 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 34 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 35 | for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 37 | switch (*C) { |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 38 | default: break; |
| 39 | |
| 40 | case ' ': |
| 41 | if (EscapeSpaces) |
| 42 | RB.ReplaceText(FilePos, 1, " ", 6); |
| 43 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 45 | case '\t': |
| 46 | if (!ReplaceTabs) |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 47 | break; |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 48 | if (EscapeSpaces) |
| 49 | RB.ReplaceText(FilePos, 1, " ", 6*4); |
| 50 | else |
| 51 | RB.ReplaceText(FilePos, 1, " ", 4); |
| 52 | break; |
| 53 | |
| 54 | case '<': |
| 55 | RB.ReplaceText(FilePos, 1, "<", 4); |
| 56 | break; |
| 57 | |
| 58 | case '>': |
| 59 | RB.ReplaceText(FilePos, 1, ">", 4); |
| 60 | break; |
| 61 | |
| 62 | case '&': |
| 63 | RB.ReplaceText(FilePos, 1, "&", 5); |
| 64 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 69 | std::string html::EscapeText(const std::string& s, bool EscapeSpaces, |
| 70 | bool ReplaceTabs) { |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 71 | |
| 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 Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 78 | switch (c) { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 79 | default: |
| 80 | os << c; break; |
| 81 | |
| 82 | case ' ': |
| 83 | if (EscapeSpaces) os << " "; |
| 84 | else os << ' '; |
| 85 | break; |
| 86 | |
| 87 | case '\t': |
| 88 | if (ReplaceTabs) |
| 89 | for (unsigned i = 0; i < 4; ++i) |
| 90 | os << " "; |
| 91 | else |
| 92 | os << c; |
| 93 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 94 | break; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 95 | |
| 96 | case '<': os << "<"; break; |
| 97 | case '>': os << ">"; break; |
| 98 | case '&': os << "&"; break; |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | return os.str(); |
| 103 | } |
| 104 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 105 | static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, |
| 106 | unsigned B, unsigned E) { |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 107 | 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 Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 114 | if (B == E) { // Handle empty lines. |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 115 | Str += " </td></tr>"; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 116 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 117 | } else { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 118 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
| 119 | RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>")); |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 120 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void 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 Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 129 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 130 | |
| 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 Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 147 | while (C != FileEnd) { |
| 148 | char c = *C; |
| 149 | ++C; |
| 150 | |
| 151 | if (c == '\n') { |
| 152 | LineEndPos = FilePos++; |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 153 | break; |
| 154 | } |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 155 | |
| 156 | ++FilePos; |
| 157 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 159 | AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 162 | // 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 Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 166 | RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>")); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 167 | } |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 168 | |
| 169 | void 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 Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 179 | 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 Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 184 | " h1 { font-size:14pt }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 185 | " .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 Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 188 | " .comment { color: #A0A0A0 }\n" |
| 189 | " .macro { color: #FF0000; background-color:#FFC0C0 }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 190 | " .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 Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 206 | " 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 Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 215 | "</style>\n</head>\n<body>"); |
| 216 | |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 217 | // Generate footer |
| 218 | |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 219 | R.InsertCStrAfter(EndLoc, "</body></html>\n"); |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 221 | |
| 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. |
| 226 | void 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 Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 236 | // Lex all the tokens in raw mode, to avoid entering #includes or expanding |
| 237 | // macros. |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 238 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 239 | Token Tok; |
| 240 | do { |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 241 | PP.LexUnexpandedToken(Tok); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 242 | // 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 Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 254 | // FIXME: Add keywords here. |
| 255 | |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 256 | 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 Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 264 | PP.SetCommentRetentionState(false, false); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame^] | 266 | |
| 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. |
| 271 | void 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 | |