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