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