blob: 6fe3fc07db2cb4d5cc91b24c91e60512b9a41478 [file] [log] [blame]
Ted Kremenek6a340832008-03-18 21:19:49 +00001//== 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 Kremenek339b9c22008-04-17 22:31:54 +000015#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000016#include "clang/Rewrite/Rewriter.h"
17#include "clang/Rewrite/HTMLRewrite.h"
Chris Lattner867924d2009-02-13 00:51:30 +000018#include "clang/Lex/TokenConcatenation.h"
Chris Lattner3245a0a2008-04-16 06:11:58 +000019#include "clang/Lex/Preprocessor.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenek339b9c22008-04-17 22:31:54 +000022#include "llvm/ADT/OwningPtr.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000023#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregorf9b0a582010-03-15 23:33:37 +000025#include <cstdio>
Ted Kremenek6a340832008-03-18 21:19:49 +000026using namespace clang;
27
Chris Lattner9402b572008-04-16 23:06:45 +000028
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000029/// HighlightRange - Highlight a range in the source code with the specified
30/// start/end tags. B/E must be in the same file. This ensures that
31/// start/end tags are placed at the start/end of each line if the range is
32/// multiline.
33void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
34 const char *StartTag, const char *EndTag) {
35 SourceManager &SM = R.getSourceMgr();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000036 B = SM.getInstantiationLoc(B);
37 E = SM.getInstantiationLoc(E);
Chris Lattnera11d6172009-01-19 07:46:45 +000038 FileID FID = SM.getFileID(B);
39 assert(SM.getFileID(E) == FID && "B/E not in the same file!");
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000040
Chris Lattner52c29082009-01-27 06:27:13 +000041 unsigned BOffset = SM.getFileOffset(B);
42 unsigned EOffset = SM.getFileOffset(E);
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000044 // Include the whole end token in the range.
Chris Lattner2c78b872009-04-14 23:22:57 +000045 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
Mike Stump1eb44332009-09-09 15:08:12 +000046
Douglas Gregoraea67db2010-03-15 22:54:52 +000047 llvm::StringRef FileName;
48 std::string ErrorStr;
49 const char *BufferStart = SM.getBufferData(FID, FileName, ErrorStr).first;
50 if (!BufferStart) {
51 // FIXME: Add a diagnostic object somewhere?
52 fprintf(stderr, "error: cannot open file '%s': %s\n",
53 FileName.str().c_str(), ErrorStr.c_str());
54 return;
55 }
56
Chris Lattner2b2453a2009-01-17 06:22:33 +000057 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
Douglas Gregoraea67db2010-03-15 22:54:52 +000058 BufferStart, StartTag, EndTag);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000059}
60
61/// HighlightRange - This is the same as the above method, but takes
62/// decomposed file locations.
63void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
64 const char *BufferStart,
65 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000066 // Insert the tag at the absolute start/end of the range.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000067 RB.InsertTextAfter(B, StartTag);
68 RB.InsertTextBefore(E, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner9402b572008-04-16 23:06:45 +000070 // Scan the range to see if there is a \r or \n. If so, and if the line is
71 // not blank, insert tags on that line as well.
72 bool HadOpenTag = true;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner9402b572008-04-16 23:06:45 +000074 unsigned LastNonWhiteSpace = B;
75 for (unsigned i = B; i != E; ++i) {
76 switch (BufferStart[i]) {
77 case '\r':
78 case '\n':
79 // Okay, we found a newline in the range. If we have an open tag, we need
80 // to insert a close tag at the first non-whitespace before the newline.
81 if (HadOpenTag)
Daniel Dunbard7407dc2009-08-19 19:10:30 +000082 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner9402b572008-04-16 23:06:45 +000084 // Instead of inserting an open tag immediately after the newline, we
85 // wait until we see a non-whitespace character. This prevents us from
86 // inserting tags around blank lines, and also allows the open tag to
87 // be put *after* whitespace on a non-blank line.
88 HadOpenTag = false;
89 break;
90 case '\0':
91 case ' ':
92 case '\t':
93 case '\f':
94 case '\v':
95 // Ignore whitespace.
96 break;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner9402b572008-04-16 23:06:45 +000098 default:
99 // If there is no tag open, do it now.
100 if (!HadOpenTag) {
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000101 RB.InsertTextAfter(i, StartTag);
Chris Lattner9402b572008-04-16 23:06:45 +0000102 HadOpenTag = true;
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattner9402b572008-04-16 23:06:45 +0000105 // Remember this character.
106 LastNonWhiteSpace = i;
107 break;
108 }
109 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000110}
111
Chris Lattner2b2453a2009-01-17 06:22:33 +0000112void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenekfa5be362008-04-08 22:37:58 +0000113 bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner2b2453a2009-01-17 06:22:33 +0000115 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek6a340832008-03-18 21:19:49 +0000116 const char* C = Buf->getBufferStart();
117 const char* FileEnd = Buf->getBufferEnd();
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek6a340832008-03-18 21:19:49 +0000119 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chris Lattner2b2453a2009-01-17 06:22:33 +0000121 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000122
123 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000124 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000125 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000126 default: ++ColNo; break;
127 case '\n':
128 case '\r':
129 ColNo = 0;
130 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner73527142008-04-16 04:33:23 +0000132 case ' ':
133 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000134 RB.ReplaceText(FilePos, 1, "&nbsp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000135 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000136 break;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000137 case '\f':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000138 RB.ReplaceText(FilePos, 1, "<hr>");
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000139 ColNo = 0;
140 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattner5c176f72008-04-18 04:54:20 +0000142 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000143 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000144 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000145 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000146 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000147 RB.ReplaceText(FilePos, 1,
148 llvm::StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
149 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
Chris Lattner73527142008-04-16 04:33:23 +0000150 else
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000151 RB.ReplaceText(FilePos, 1, llvm::StringRef(" ", NumSpaces));
Chris Lattner5c176f72008-04-18 04:54:20 +0000152 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000153 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000154 }
Chris Lattner73527142008-04-16 04:33:23 +0000155 case '<':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000156 RB.ReplaceText(FilePos, 1, "&lt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000157 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000158 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner73527142008-04-16 04:33:23 +0000160 case '>':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000161 RB.ReplaceText(FilePos, 1, "&gt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000162 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000163 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattner73527142008-04-16 04:33:23 +0000165 case '&':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000166 RB.ReplaceText(FilePos, 1, "&amp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000167 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000168 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000169 }
170 }
171}
172
Ted Kremenekfa5be362008-04-08 22:37:58 +0000173std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
174 bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek053ef592008-03-27 17:15:29 +0000176 unsigned len = s.size();
Ted Kremeneka95d3752008-09-13 05:16:45 +0000177 std::string Str;
178 llvm::raw_string_ostream os(Str);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek053ef592008-03-27 17:15:29 +0000180 for (unsigned i = 0 ; i < len; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek053ef592008-03-27 17:15:29 +0000182 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000183 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000184 default:
185 os << c; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner8570f0b2008-04-16 04:37:29 +0000187 case ' ':
188 if (EscapeSpaces) os << "&nbsp;";
189 else os << ' ';
190 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000192 case '\t':
193 if (ReplaceTabs) {
194 if (EscapeSpaces)
195 for (unsigned i = 0; i < 4; ++i)
196 os << "&nbsp;";
197 else
198 for (unsigned i = 0; i < 4; ++i)
199 os << " ";
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201 else
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000202 os << c;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000204 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000206 case '<': os << "&lt;"; break;
207 case '>': os << "&gt;"; break;
208 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000209 }
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek053ef592008-03-27 17:15:29 +0000212 return os.str();
213}
214
Chris Lattner8570f0b2008-04-16 04:37:29 +0000215static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
216 unsigned B, unsigned E) {
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000217 llvm::SmallString<256> Str;
218 llvm::raw_svector_ostream OS(Str);
219
220 OS << "<tr><td class=\"num\" id=\"LN"
221 << LineNo << "\">"
222 << LineNo << "</td><td class=\"line\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenek49cd6352008-04-03 07:12:29 +0000224 if (B == E) { // Handle empty lines.
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000225 OS << " </td></tr>";
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000226 RB.InsertTextBefore(B, OS.str());
Chris Lattner57df3b92008-04-16 04:11:35 +0000227 } else {
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000228 RB.InsertTextBefore(B, OS.str());
229 RB.InsertTextBefore(E, "</td></tr>");
Ted Kremenek49cd6352008-04-03 07:12:29 +0000230 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000231}
232
Chris Lattner2b2453a2009-01-17 06:22:33 +0000233void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000234
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000236 const char* FileBeg = Buf->getBufferStart();
237 const char* FileEnd = Buf->getBufferEnd();
238 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000239 RewriteBuffer &RB = R.getEditBuffer(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekb485cd12008-03-18 23:08:51 +0000241 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenekb485cd12008-03-18 23:08:51 +0000243 unsigned LineNo = 0;
244 unsigned FilePos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
246 while (C != FileEnd) {
247
Ted Kremenekb485cd12008-03-18 23:08:51 +0000248 ++LineNo;
249 unsigned LineStartPos = FilePos;
250 unsigned LineEndPos = FileEnd - FileBeg;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenekb485cd12008-03-18 23:08:51 +0000252 assert (FilePos <= LineEndPos);
253 assert (C < FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenekb485cd12008-03-18 23:08:51 +0000255 // Scan until the newline (or end-of-file).
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek49cd6352008-04-03 07:12:29 +0000257 while (C != FileEnd) {
258 char c = *C;
259 ++C;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek49cd6352008-04-03 07:12:29 +0000261 if (c == '\n') {
262 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000263 break;
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek49cd6352008-04-03 07:12:29 +0000266 ++FilePos;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner8570f0b2008-04-16 04:37:29 +0000269 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner8570f0b2008-04-16 04:37:29 +0000272 // Add one big table tag that surrounds all of the code.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000273 RB.InsertTextBefore(0, "<table class=\"code\">\n");
274 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
Ted Kremenekb485cd12008-03-18 23:08:51 +0000275}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000276
Mike Stump1eb44332009-09-09 15:08:12 +0000277void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000278 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000279
Chris Lattner2b2453a2009-01-17 06:22:33 +0000280 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000281 const char* FileStart = Buf->getBufferStart();
282 const char* FileEnd = Buf->getBufferEnd();
283
Chris Lattner2b2453a2009-01-17 06:22:33 +0000284 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
285 SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000286
Ted Kremeneka95d3752008-09-13 05:16:45 +0000287 std::string s;
288 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000289 os << "<!doctype html>\n" // Use HTML 5 doctype
290 "<html>\n<head>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000292 if (title)
293 os << "<title>" << html::EscapeText(title) << "</title>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000295 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000296 " body { color:#000000; background-color:#ffffff }\n"
297 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000298 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000299 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000300 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
301 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000302 " .comment { color: green; font-style: oblique }\n"
303 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000304 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000305 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000306 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000307 " .expansion { display: none; }\n"
308 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000309 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000310 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000311 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000312 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000313 // Macros are position: relative to provide base for expansions.
314 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000315 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000316 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000317 " .num { color:#444444 }\n"
318 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
319 " .line { white-space: pre }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000320 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
321 " .msg { -webkit-border-radius:5px }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000322 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000323 " .msg { float:left }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000324 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000325 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000326 " .msg { font-weight:bold }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000327 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
328 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000329 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000330 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000331 " .mrange { background-color:#dfddf3 }\n"
332 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000333 " .PathIndex { font-weight: bold; padding:0px 5px 0px 5px; "
334 "margin-right:5px; }\n"
Ted Kremenek00f01e42009-03-02 23:39:27 +0000335 " .PathIndex { -webkit-border-radius:8px }\n"
336 " .PathIndexEvent { background-color:#bfba87 }\n"
337 " .PathIndexControl { background-color:#8c8c8c }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000338 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
339 " .CodeRemovalHint { background-color:#de1010 }\n"
340 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000341 " table.simpletable {\n"
342 " padding: 5px;\n"
343 " font-size:12pt;\n"
344 " margin:20px;\n"
345 " border-collapse: collapse; border-spacing: 0px;\n"
346 " }\n"
347 " td.rowname {\n"
348 " text-align:right; font-weight:bold; color:#444444;\n"
349 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000350 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000351
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000352 // Generate header
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000353 R.InsertTextBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000354 // Generate footer
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000356 R.InsertTextAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000357}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000358
359/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
360/// information about keywords, macro expansions etc. This uses the macro
361/// table state from the end of the file, so it won't be perfectly perfect,
362/// but it will be reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000363void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000364 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000365
Chris Lattner05db4272009-02-13 19:33:24 +0000366 const SourceManager &SM = PP.getSourceManager();
Chris Lattner6e290142009-11-30 04:18:44 +0000367 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
368 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000369 const char *BufferStart = L.getBufferStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000370
371 // Inform the preprocessor that we want to retain comments as tokens, so we
Chris Lattner3245a0a2008-04-16 06:11:58 +0000372 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000373 L.SetCommentRetentionState(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000375 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
376 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000377 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000378 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner74ea3e52008-04-16 06:53:09 +0000380 while (Tok.isNot(tok::eof)) {
381 // Since we are lexing unexpanded tokens, all tokens are from the main
382 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000383 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000384 unsigned TokLen = Tok.getLength();
385 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000386 default: break;
387 case tok::identifier: {
388 // Fill in Result.IdentifierInfo, looking up the identifier in the
389 // identifier table.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000390 const IdentifierInfo *II =
391 PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Chris Lattnera745e8c2008-04-16 20:51:51 +0000393 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000394 if (II->getTokenID() != tok::identifier)
395 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
396 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000397 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000398 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000399 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000400 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
401 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000402 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000403 case tok::wide_string_literal:
404 // Chop off the L prefix
405 ++TokOffs;
406 --TokLen;
407 // FALL THROUGH.
408 case tok::string_literal:
409 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
410 "<span class='string_literal'>", "</span>");
411 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000412 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000413 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000414 if (!Tok.isAtStartOfLine())
415 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner5deb96d2008-04-16 23:21:17 +0000417 // Eat all of the tokens until we get to the next one at the start of
418 // line.
419 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000420 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000421 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000422 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000423 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner5deb96d2008-04-16 23:21:17 +0000426 // Find end of line. This is a hack.
427 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
428 "<span class='directive'>", "</span>");
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattner5deb96d2008-04-16 23:21:17 +0000430 // Don't skip the next token.
431 continue;
432 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattner590f0cc2008-10-12 01:15:46 +0000435 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000436 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000437}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000438
Chris Lattner7c175fb2009-03-13 21:44:46 +0000439namespace {
440/// IgnoringDiagClient - This is a diagnostic client that just ignores all
441/// diags.
442class IgnoringDiagClient : public DiagnosticClient {
443 void HandleDiagnostic(Diagnostic::Level DiagLevel,
444 const DiagnosticInfo &Info) {
445 // Just ignore it.
446 }
447};
448}
449
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000450/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000451/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000452/// macro expansions. This won't be perfectly perfect, but it will be
453/// reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000454void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000455 // Re-lex the raw token stream into a token buffer.
456 const SourceManager &SM = PP.getSourceManager();
457 std::vector<Token> TokenStream;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattner6e290142009-11-30 04:18:44 +0000459 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
460 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner05db4272009-02-13 19:33:24 +0000462 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
463 // macros.
464 while (1) {
465 Token Tok;
466 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner05db4272009-02-13 19:33:24 +0000468 // If this is a # at the start of a line, discard it from the token stream.
469 // We don't want the re-preprocess step to see #defines, #includes or other
470 // preprocessor directives.
471 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
472 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000473
474 // If this is a ## token, change its kind to unknown so that repreprocessing
475 // it will not produce an error.
476 if (Tok.is(tok::hashhash))
477 Tok.setKind(tok::unknown);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner05db4272009-02-13 19:33:24 +0000479 // If this raw token is an identifier, the raw lexer won't have looked up
480 // the corresponding identifier info for it. Do this now so that it will be
481 // macro expanded when we re-preprocess it.
482 if (Tok.is(tok::identifier)) {
483 // Change the kind of this identifier to the appropriate token kind, e.g.
484 // turning "for" into a keyword.
485 Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000486 }
487
Chris Lattner05db4272009-02-13 19:33:24 +0000488 TokenStream.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner05db4272009-02-13 19:33:24 +0000490 if (Tok.is(tok::eof)) break;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner7c175fb2009-03-13 21:44:46 +0000493 // Temporarily change the diagnostics object so that we ignore any generated
494 // diagnostics from this pass.
495 IgnoringDiagClient TmpDC;
496 Diagnostic TmpDiags(&TmpDC);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000498 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
499 // its state, but we aren't actually changing it (we hope). This should really
500 // construct a copy of the preprocessor.
501 Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
502 Diagnostic *OldDiags = &TmpPP.getDiagnostics();
503 TmpPP.setDiagnostics(TmpDiags);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000505 // Inform the preprocessor that we don't want comments.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000506 TmpPP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000507
508 // Enter the tokens we just lexed. This will cause them to be macro expanded
509 // but won't enter sub-files (because we removed #'s).
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000510 TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000512 TokenConcatenation ConcatInfo(TmpPP);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000514 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000515 Token Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000516 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000517 while (Tok.isNot(tok::eof)) {
518 // Ignore non-macro tokens.
519 if (!Tok.getLocation().isMacroID()) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000520 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000521 continue;
522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattnerb7949a92009-02-15 21:32:34 +0000524 // Okay, we have the first token of a macro expansion: highlight the
525 // instantiation by inserting a start tag before the macro instantiation and
526 // end tag after it.
527 std::pair<SourceLocation, SourceLocation> LLoc =
528 SM.getInstantiationRange(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnerb7949a92009-02-15 21:32:34 +0000530 // Ignore tokens whose instantiation location was not the main file.
531 if (SM.getFileID(LLoc.first) != FID) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000532 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000533 continue;
534 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000535
Chris Lattnerb7949a92009-02-15 21:32:34 +0000536 assert(SM.getFileID(LLoc.second) == FID &&
537 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000538
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000539 std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000540 unsigned LineLen = Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattner867924d2009-02-13 00:51:30 +0000542 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000543 // Okay, eat this token, getting the next one.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000544 TmpPP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000546 // Skip all the rest of the tokens that are part of this macro
547 // instantiation. It would be really nice to pop up a window with all the
548 // spelling of the tokens or something.
549 while (!Tok.is(tok::eof) &&
Chris Lattnerb7949a92009-02-15 21:32:34 +0000550 SM.getInstantiationLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000551 // Insert a newline if the macro expansion is getting large.
552 if (LineLen > 60) {
553 Expansion += "<br>";
554 LineLen = 0;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner6f46be22008-04-17 00:40:45 +0000557 LineLen -= Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner867924d2009-02-13 00:51:30 +0000559 // If the tokens were already space separated, or if they must be to avoid
560 // them being implicitly pasted, add a space between them.
561 if (Tok.hasLeadingSpace() ||
562 ConcatInfo.AvoidConcat(PrevTok, Tok))
563 Expansion += ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattner9227c692008-04-17 23:03:14 +0000565 // Escape any special characters in the token text.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000566 Expansion += EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000567 LineLen += Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattner867924d2009-02-13 00:51:30 +0000569 PrevTok = Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000570 TmpPP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000573
574 // Insert the expansion as the end tag, so that multi-line macros all get
575 // highlighted.
576 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
577
578 HighlightRange(R, LLoc.first, LLoc.second,
579 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000580 }
Chris Lattner7c175fb2009-03-13 21:44:46 +0000581
582 // Restore diagnostics object back to its own thing.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000583 TmpPP.setDiagnostics(*OldDiags);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000584}