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 | |
Chris Lattner | 9402b57 | 2008-04-16 23:06:45 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 25 | /// HighlightRange - Highlight a range in the source code with the specified |
| 26 | /// start/end tags. B/E must be in the same file. This ensures that |
| 27 | /// start/end tags are placed at the start/end of each line if the range is |
| 28 | /// multiline. |
| 29 | void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, |
| 30 | const char *StartTag, const char *EndTag) { |
| 31 | SourceManager &SM = R.getSourceMgr(); |
| 32 | B = SM.getLogicalLoc(B); |
| 33 | E = SM.getLogicalLoc(E); |
| 34 | unsigned FileID = SM.getCanonicalFileID(B); |
| 35 | assert(SM.getCanonicalFileID(E) == FileID && "B/E not in the same file!"); |
| 36 | |
| 37 | unsigned BOffset = SM.getFullFilePos(B); |
| 38 | unsigned EOffset = SM.getFullFilePos(E); |
| 39 | |
| 40 | // Include the whole end token in the range. |
| 41 | EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr()); |
| 42 | |
| 43 | HighlightRange(R.getEditBuffer(FileID), BOffset, EOffset, |
| 44 | SM.getBufferData(FileID).first, StartTag, EndTag); |
| 45 | } |
| 46 | |
| 47 | /// HighlightRange - This is the same as the above method, but takes |
| 48 | /// decomposed file locations. |
| 49 | void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, |
| 50 | const char *BufferStart, |
| 51 | const char *StartTag, const char *EndTag) { |
Chris Lattner | 9402b57 | 2008-04-16 23:06:45 +0000 | [diff] [blame] | 52 | // Insert the tag at the absolute start/end of the range. |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 53 | RB.InsertTextAfter(B, StartTag, strlen(StartTag)); |
| 54 | RB.InsertTextBefore(E, EndTag, strlen(EndTag)); |
| 55 | |
Chris Lattner | 9402b57 | 2008-04-16 23:06:45 +0000 | [diff] [blame] | 56 | // Scan the range to see if there is a \r or \n. If so, and if the line is |
| 57 | // not blank, insert tags on that line as well. |
| 58 | bool HadOpenTag = true; |
| 59 | |
| 60 | unsigned LastNonWhiteSpace = B; |
| 61 | for (unsigned i = B; i != E; ++i) { |
| 62 | switch (BufferStart[i]) { |
| 63 | case '\r': |
| 64 | case '\n': |
| 65 | // Okay, we found a newline in the range. If we have an open tag, we need |
| 66 | // to insert a close tag at the first non-whitespace before the newline. |
| 67 | if (HadOpenTag) |
| 68 | RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag, strlen(EndTag)); |
| 69 | |
| 70 | // Instead of inserting an open tag immediately after the newline, we |
| 71 | // wait until we see a non-whitespace character. This prevents us from |
| 72 | // inserting tags around blank lines, and also allows the open tag to |
| 73 | // be put *after* whitespace on a non-blank line. |
| 74 | HadOpenTag = false; |
| 75 | break; |
| 76 | case '\0': |
| 77 | case ' ': |
| 78 | case '\t': |
| 79 | case '\f': |
| 80 | case '\v': |
| 81 | // Ignore whitespace. |
| 82 | break; |
| 83 | |
| 84 | default: |
| 85 | // If there is no tag open, do it now. |
| 86 | if (!HadOpenTag) { |
| 87 | RB.InsertTextAfter(i, StartTag, strlen(StartTag)); |
| 88 | HadOpenTag = true; |
| 89 | } |
| 90 | |
| 91 | // Remember this character. |
| 92 | LastNonWhiteSpace = i; |
| 93 | break; |
| 94 | } |
| 95 | } |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 98 | void html::EscapeText(Rewriter& R, unsigned FileID, |
| 99 | bool EscapeSpaces, bool ReplaceTabs) { |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 100 | |
| 101 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 102 | const char* C = Buf->getBufferStart(); |
| 103 | const char* FileEnd = Buf->getBufferEnd(); |
| 104 | |
| 105 | assert (C <= FileEnd); |
| 106 | |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 107 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 108 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 109 | for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 111 | switch (*C) { |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 112 | default: break; |
| 113 | |
| 114 | case ' ': |
| 115 | if (EscapeSpaces) |
| 116 | RB.ReplaceText(FilePos, 1, " ", 6); |
| 117 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 119 | case '\t': |
| 120 | if (!ReplaceTabs) |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 121 | break; |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 122 | if (EscapeSpaces) |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 123 | RB.ReplaceText(FilePos, 1, " ", 6*8); |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 124 | else |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 125 | RB.ReplaceText(FilePos, 1, " ", 8); |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case '<': |
| 129 | RB.ReplaceText(FilePos, 1, "<", 4); |
| 130 | break; |
| 131 | |
| 132 | case '>': |
| 133 | RB.ReplaceText(FilePos, 1, ">", 4); |
| 134 | break; |
| 135 | |
| 136 | case '&': |
| 137 | RB.ReplaceText(FilePos, 1, "&", 5); |
| 138 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 143 | std::string html::EscapeText(const std::string& s, bool EscapeSpaces, |
| 144 | bool ReplaceTabs) { |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 145 | |
| 146 | unsigned len = s.size(); |
| 147 | std::ostringstream os; |
| 148 | |
| 149 | for (unsigned i = 0 ; i < len; ++i) { |
| 150 | |
| 151 | char c = s[i]; |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 152 | switch (c) { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 153 | default: |
| 154 | os << c; break; |
| 155 | |
| 156 | case ' ': |
| 157 | if (EscapeSpaces) os << " "; |
| 158 | else os << ' '; |
| 159 | break; |
| 160 | |
| 161 | case '\t': |
| 162 | if (ReplaceTabs) |
| 163 | for (unsigned i = 0; i < 4; ++i) |
| 164 | os << " "; |
| 165 | else |
| 166 | os << c; |
| 167 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 168 | break; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 169 | |
| 170 | case '<': os << "<"; break; |
| 171 | case '>': os << ">"; break; |
| 172 | case '&': os << "&"; break; |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | return os.str(); |
| 177 | } |
| 178 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 179 | static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, |
| 180 | unsigned B, unsigned E) { |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 181 | llvm::SmallString<100> Str; |
| 182 | Str += "<tr><td class=\"num\" id=\"LN"; |
| 183 | Str.append_uint(LineNo); |
| 184 | Str += "\">"; |
| 185 | Str.append_uint(LineNo); |
| 186 | Str += "</td><td class=\"line\">"; |
| 187 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 188 | if (B == E) { // Handle empty lines. |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 189 | Str += " </td></tr>"; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 190 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 191 | } else { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 192 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
| 193 | RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>")); |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 194 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void html::AddLineNumbers(Rewriter& R, unsigned FileID) { |
| 198 | |
| 199 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 200 | const char* FileBeg = Buf->getBufferStart(); |
| 201 | const char* FileEnd = Buf->getBufferEnd(); |
| 202 | const char* C = FileBeg; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 203 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 204 | |
| 205 | assert (C <= FileEnd); |
| 206 | |
| 207 | unsigned LineNo = 0; |
| 208 | unsigned FilePos = 0; |
| 209 | |
| 210 | while (C != FileEnd) { |
| 211 | |
| 212 | ++LineNo; |
| 213 | unsigned LineStartPos = FilePos; |
| 214 | unsigned LineEndPos = FileEnd - FileBeg; |
| 215 | |
| 216 | assert (FilePos <= LineEndPos); |
| 217 | assert (C < FileEnd); |
| 218 | |
| 219 | // Scan until the newline (or end-of-file). |
| 220 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 221 | while (C != FileEnd) { |
| 222 | char c = *C; |
| 223 | ++C; |
| 224 | |
| 225 | if (c == '\n') { |
| 226 | LineEndPos = FilePos++; |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 229 | |
| 230 | ++FilePos; |
| 231 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 233 | AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 236 | // Add one big table tag that surrounds all of the code. |
| 237 | RB.InsertTextBefore(0, "<table class=\"code\">\n", |
| 238 | strlen("<table class=\"code\">\n")); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 240 | RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>")); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 241 | } |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 242 | |
| 243 | void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) { |
| 244 | |
| 245 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 246 | const char* FileStart = Buf->getBufferStart(); |
| 247 | const char* FileEnd = Buf->getBufferEnd(); |
| 248 | |
| 249 | SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0); |
| 250 | SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart); |
| 251 | |
| 252 | // Generate header |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 253 | R.InsertCStrBefore(StartLoc, |
Ted Kremenek | 07339a6 | 2008-04-17 19:57:27 +0000 | [diff] [blame] | 254 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
| 255 | "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" |
| 256 | "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 257 | "<style type=\"text/css\">\n" |
| 258 | " body { color:#000000; background-color:#ffffff }\n" |
| 259 | " body { font-family:Helvetica, sans-serif; font-size:10pt }\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 260 | " h1 { font-size:14pt }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 261 | " .code { border-spacing:0px; width:100%; }\n" |
| 262 | " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n" |
| 263 | " .code { line-height: 1.2em }\n" |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 264 | " .comment { color: #A0A0A0; font-style: oblique }\n" |
Chris Lattner | c4586c2 | 2008-04-16 06:35:07 +0000 | [diff] [blame] | 265 | " .keyword { color: #FF00FF }\n" |
Chris Lattner | 5deb96d | 2008-04-16 23:21:17 +0000 | [diff] [blame] | 266 | " .directive { color: #00A000 }\n" |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 267 | // Macro expansions. |
Ted Kremenek | 07339a6 | 2008-04-17 19:57:27 +0000 | [diff] [blame] | 268 | " .expansion { display: none; }\n" |
| 269 | " .macro:hover .expansion { display: block; border: 2px solid #FF0000; " |
| 270 | "padding: 2px; background-color:#FFF0F0;" |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 271 | " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; " |
| 272 | "position: absolute; top: -1em; left:10em } \n" |
| 273 | " .macro { color: #FF0000; background-color:#FFC0C0;" |
| 274 | // Macros are position: relative to provide base for expansions. |
| 275 | " position: relative }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 276 | " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n" |
| 277 | " .num { text-align:right; font-size: smaller }\n" |
| 278 | " .num { color:#444444 }\n" |
| 279 | " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n" |
| 280 | " .line { white-space: pre }\n" |
| 281 | " .msg { background-color:#fff8b4; color:#000000 }\n" |
| 282 | " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n" |
| 283 | " .msg { -webkit-border-radius:5px }\n" |
| 284 | " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n" |
| 285 | " .msg { font-weight: bold }\n" |
| 286 | " .msg { float:left }\n" |
| 287 | " .msg { padding:0.5em 1ex 0.5em 1ex }\n" |
| 288 | " .msg { margin-top:10px; margin-bottom:10px }\n" |
| 289 | " .mrange { background-color:#dfddf3 }\n" |
| 290 | " .mrange { border-bottom:1px solid #6F9DBE }\n" |
| 291 | " .PathIndex { font-weight: bold }\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 292 | " table.simpletable {\n" |
| 293 | " padding: 5px;\n" |
| 294 | " font-size:12pt;\n" |
| 295 | " margin:20px;\n" |
| 296 | " border-collapse: collapse; border-spacing: 0px;\n" |
| 297 | " }\n" |
| 298 | " td.rowname {\n" |
| 299 | " text-align:right; font-weight:bold; color:#444444;\n" |
| 300 | " padding-right:2ex; }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 301 | "</style>\n</head>\n<body>"); |
| 302 | |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 303 | // Generate footer |
| 304 | |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 305 | R.InsertCStrAfter(EndLoc, "</body></html>\n"); |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 306 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 307 | |
| 308 | /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with |
| 309 | /// information about keywords, macro expansions etc. This uses the macro |
| 310 | /// table state from the end of the file, so it won't be perfectly perfect, |
| 311 | /// but it will be reasonably close. |
| 312 | void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) { |
| 313 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 314 | |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 315 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 316 | std::pair<const char*, const char*> File = SourceMgr.getBufferData(FileID); |
| 317 | const char *BufferStart = File.first; |
| 318 | |
| 319 | Lexer L(SourceLocation::getFileLoc(FileID, 0), PP.getLangOptions(), |
| 320 | File.first, File.second); |
| 321 | |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 322 | // Inform the preprocessor that we want to retain comments as tokens, so we |
| 323 | // can highlight them. |
Chris Lattner | 678c635 | 2008-04-16 20:54:51 +0000 | [diff] [blame] | 324 | L.SetCommentRetentionState(true); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 325 | |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 326 | // Lex all the tokens in raw mode, to avoid entering #includes or expanding |
| 327 | // macros. |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 328 | Token Tok; |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 329 | L.LexRawToken(Tok); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 331 | while (Tok.isNot(tok::eof)) { |
| 332 | // Since we are lexing unexpanded tokens, all tokens are from the main |
| 333 | // FileID. |
| 334 | unsigned TokOffs = SourceMgr.getFullFilePos(Tok.getLocation()); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 335 | unsigned TokLen = Tok.getLength(); |
| 336 | switch (Tok.getKind()) { |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 337 | default: break; |
| 338 | case tok::identifier: { |
| 339 | // Fill in Result.IdentifierInfo, looking up the identifier in the |
| 340 | // identifier table. |
| 341 | IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs); |
| 342 | |
| 343 | // If this is a pp-identifier, for a keyword, highlight it as such. |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 344 | if (II->getTokenID() != tok::identifier) |
| 345 | HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, |
| 346 | "<span class='keyword'>", "</span>"); |
Chris Lattner | c4586c2 | 2008-04-16 06:35:07 +0000 | [diff] [blame] | 347 | break; |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 349 | case tok::comment: |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame] | 350 | HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, |
| 351 | "<span class='comment'>", "</span>"); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 352 | break; |
Chris Lattner | 5deb96d | 2008-04-16 23:21:17 +0000 | [diff] [blame] | 353 | case tok::hash: { |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 354 | // If this is a preprocessor directive, all tokens to end of line are too. |
Chris Lattner | 5deb96d | 2008-04-16 23:21:17 +0000 | [diff] [blame] | 355 | if (!Tok.isAtStartOfLine()) |
| 356 | break; |
| 357 | |
| 358 | // Eat all of the tokens until we get to the next one at the start of |
| 359 | // line. |
| 360 | unsigned TokEnd = TokOffs+TokLen; |
| 361 | L.LexRawToken(Tok); |
| 362 | while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) { |
| 363 | TokEnd = SourceMgr.getFullFilePos(Tok.getLocation())+Tok.getLength(); |
| 364 | L.LexRawToken(Tok); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 365 | } |
Chris Lattner | 5deb96d | 2008-04-16 23:21:17 +0000 | [diff] [blame] | 366 | |
| 367 | // Find end of line. This is a hack. |
| 368 | HighlightRange(RB, TokOffs, TokEnd, BufferStart, |
| 369 | "<span class='directive'>", "</span>"); |
| 370 | |
| 371 | // Don't skip the next token. |
| 372 | continue; |
| 373 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 376 | L.LexRawToken(Tok); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 379 | |
| 380 | /// HighlightMacros - This uses the macro table state from the end of the |
| 381 | /// file, to reexpand macros and insert (into the HTML) information about the |
| 382 | /// macro expansions. This won't be perfectly perfect, but it will be |
| 383 | /// reasonably close. |
| 384 | void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP) { |
| 385 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 386 | |
| 387 | // Inform the preprocessor that we don't want comments. |
| 388 | PP.SetCommentRetentionState(false, false); |
| 389 | |
| 390 | // Start parsing the specified input file. |
| 391 | PP.EnterMainSourceFile(); |
| 392 | |
| 393 | // Lex all the tokens. |
| 394 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 395 | Token Tok; |
| 396 | PP.Lex(Tok); |
| 397 | while (Tok.isNot(tok::eof)) { |
| 398 | // Ignore non-macro tokens. |
| 399 | if (!Tok.getLocation().isMacroID()) { |
| 400 | PP.Lex(Tok); |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | // Ignore tokens whose logical location was not the main file. |
| 405 | SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation()); |
| 406 | std::pair<unsigned, unsigned> LLocInfo = |
| 407 | SourceMgr.getDecomposedFileLoc(LLoc); |
| 408 | |
| 409 | if (LLocInfo.first != FileID) { |
| 410 | PP.Lex(Tok); |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | // Okay, we have the first token of a macro expansion: highlight the |
| 415 | // instantiation. |
| 416 | |
| 417 | // Get the size of current macro call itself. |
| 418 | // FIXME: This should highlight the args of a function-like |
| 419 | // macro, using a heuristic. |
| 420 | unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr); |
| 421 | |
| 422 | unsigned TokOffs = LLocInfo.second; |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 423 | // Highlight the macro invocation itself. |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 424 | RB.InsertTextAfter(TokOffs, "<span class='macro'>", |
| 425 | strlen("<span class='macro'>")); |
| 426 | RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>")); |
| 427 | |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 428 | std::string Expansion = PP.getSpelling(Tok); |
| 429 | unsigned LineLen = Expansion.size(); |
| 430 | |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 431 | // Okay, eat this token, getting the next one. |
| 432 | PP.Lex(Tok); |
| 433 | |
| 434 | // Skip all the rest of the tokens that are part of this macro |
| 435 | // instantiation. It would be really nice to pop up a window with all the |
| 436 | // spelling of the tokens or something. |
| 437 | while (!Tok.is(tok::eof) && |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 438 | SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc) { |
| 439 | // Insert a newline if the macro expansion is getting large. |
| 440 | if (LineLen > 60) { |
| 441 | Expansion += "<br>"; |
| 442 | LineLen = 0; |
| 443 | } |
| 444 | |
| 445 | LineLen -= Expansion.size(); |
| 446 | Expansion += ' ' + PP.getSpelling(Tok); |
| 447 | LineLen += Expansion.size(); |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 448 | PP.Lex(Tok); |
Chris Lattner | 6f46be2 | 2008-04-17 00:40:45 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Insert the information about the expansion inside the macro span. |
| 452 | Expansion = "<span class='expansion'>" + Expansion + "</span>"; |
| 453 | RB.InsertTextBefore(TokOffs+TokLen, Expansion.c_str(), Expansion.size()); |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | |