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 | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame^] | 24 | /// 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. |
| 28 | void 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. |
| 48 | void 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 Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 56 | void html::EscapeText(Rewriter& R, unsigned FileID, |
| 57 | bool EscapeSpaces, bool ReplaceTabs) { |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 58 | |
| 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 Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 65 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 66 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 67 | for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 69 | switch (*C) { |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 70 | default: break; |
| 71 | |
| 72 | case ' ': |
| 73 | if (EscapeSpaces) |
| 74 | RB.ReplaceText(FilePos, 1, " ", 6); |
| 75 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 77 | case '\t': |
| 78 | if (!ReplaceTabs) |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 79 | break; |
Chris Lattner | 7352714 | 2008-04-16 04:33:23 +0000 | [diff] [blame] | 80 | if (EscapeSpaces) |
| 81 | RB.ReplaceText(FilePos, 1, " ", 6*4); |
| 82 | else |
| 83 | RB.ReplaceText(FilePos, 1, " ", 4); |
| 84 | break; |
| 85 | |
| 86 | case '<': |
| 87 | RB.ReplaceText(FilePos, 1, "<", 4); |
| 88 | break; |
| 89 | |
| 90 | case '>': |
| 91 | RB.ReplaceText(FilePos, 1, ">", 4); |
| 92 | break; |
| 93 | |
| 94 | case '&': |
| 95 | RB.ReplaceText(FilePos, 1, "&", 5); |
| 96 | break; |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 101 | std::string html::EscapeText(const std::string& s, bool EscapeSpaces, |
| 102 | bool ReplaceTabs) { |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 103 | |
| 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 Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 110 | switch (c) { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 111 | default: |
| 112 | os << c; break; |
| 113 | |
| 114 | case ' ': |
| 115 | if (EscapeSpaces) os << " "; |
| 116 | else os << ' '; |
| 117 | break; |
| 118 | |
| 119 | case '\t': |
| 120 | if (ReplaceTabs) |
| 121 | for (unsigned i = 0; i < 4; ++i) |
| 122 | os << " "; |
| 123 | else |
| 124 | os << c; |
| 125 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 126 | break; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 127 | |
| 128 | case '<': os << "<"; break; |
| 129 | case '>': os << ">"; break; |
| 130 | case '&': os << "&"; break; |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | return os.str(); |
| 135 | } |
| 136 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 137 | static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, |
| 138 | unsigned B, unsigned E) { |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 139 | 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 Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 146 | if (B == E) { // Handle empty lines. |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 147 | Str += " </td></tr>"; |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 148 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 149 | } else { |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 150 | RB.InsertTextBefore(B, &Str[0], Str.size()); |
| 151 | RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>")); |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 152 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void 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 Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 161 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 162 | |
| 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 Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 179 | while (C != FileEnd) { |
| 180 | char c = *C; |
| 181 | ++C; |
| 182 | |
| 183 | if (c == '\n') { |
| 184 | LineEndPos = FilePos++; |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 185 | break; |
| 186 | } |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 187 | |
| 188 | ++FilePos; |
| 189 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 191 | AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 194 | // 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 Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 8570f0b | 2008-04-16 04:37:29 +0000 | [diff] [blame] | 198 | RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>")); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 200 | |
| 201 | void 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 Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 211 | 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 Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 216 | " h1 { font-size:14pt }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 217 | " .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 Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame^] | 220 | " .comment { color: #A0A0A0; font-style: oblique }\n" |
Chris Lattner | c4586c2 | 2008-04-16 06:35:07 +0000 | [diff] [blame] | 221 | " .keyword { color: #FF00FF }\n" |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 222 | " .directive { color: #FFFF00 }\n" |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 223 | " .macro { color: #FF0000; background-color:#FFC0C0 }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 224 | " .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 Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 240 | " 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 Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 249 | "</style>\n</head>\n<body>"); |
| 250 | |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 251 | // Generate footer |
| 252 | |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 253 | R.InsertCStrAfter(EndLoc, "</body></html>\n"); |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 255 | |
| 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. |
| 260 | void html::SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP) { |
| 261 | RewriteBuffer &RB = R.getEditBuffer(FileID); |
| 262 | |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 263 | 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 Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 270 | // Inform the preprocessor that we want to retain comments as tokens, so we |
| 271 | // can highlight them. |
Chris Lattner | 678c635 | 2008-04-16 20:54:51 +0000 | [diff] [blame] | 272 | L.SetCommentRetentionState(true); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 273 | |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 274 | // Lex all the tokens in raw mode, to avoid entering #includes or expanding |
| 275 | // macros. |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 276 | Token Tok; |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 277 | L.LexRawToken(Tok); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 279 | 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 Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 283 | unsigned TokLen = Tok.getLength(); |
| 284 | switch (Tok.getKind()) { |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 285 | 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 Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame^] | 292 | if (II->getTokenID() != tok::identifier) |
| 293 | HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, |
| 294 | "<span class='keyword'>", "</span>"); |
Chris Lattner | c4586c2 | 2008-04-16 06:35:07 +0000 | [diff] [blame] | 295 | break; |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 297 | case tok::comment: |
Chris Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame^] | 298 | HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, |
| 299 | "<span class='comment'>", "</span>"); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 300 | break; |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 301 | 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 Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 307 | // 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 Lattner | 5ef3e2c | 2008-04-16 22:45:51 +0000 | [diff] [blame^] | 310 | HighlightRange(RB, TokOffs, TokEnd, BufferStart, |
| 311 | "<span class='directive'>", "</span>"); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 312 | } |
| 313 | break; |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | a745e8c | 2008-04-16 20:51:51 +0000 | [diff] [blame] | 316 | L.LexRawToken(Tok); |
Chris Lattner | 74ea3e5 | 2008-04-16 06:53:09 +0000 | [diff] [blame] | 317 | } |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | c54d50a | 2008-04-16 06:32:08 +0000 | [diff] [blame] | 319 | |
| 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. |
| 324 | void 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 | |