blob: 445bf30a1f98f67cbe170334c4ff67f571928862 [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"
Ted Kremenek6a340832008-03-18 21:19:49 +000025using namespace clang;
26
Chris Lattner9402b572008-04-16 23:06:45 +000027
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000028/// HighlightRange - Highlight a range in the source code with the specified
29/// start/end tags. B/E must be in the same file. This ensures that
30/// start/end tags are placed at the start/end of each line if the range is
31/// multiline.
32void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
33 const char *StartTag, const char *EndTag) {
34 SourceManager &SM = R.getSourceMgr();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000035 B = SM.getInstantiationLoc(B);
36 E = SM.getInstantiationLoc(E);
Chris Lattnera11d6172009-01-19 07:46:45 +000037 FileID FID = SM.getFileID(B);
38 assert(SM.getFileID(E) == FID && "B/E not in the same file!");
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000039
Chris Lattner52c29082009-01-27 06:27:13 +000040 unsigned BOffset = SM.getFileOffset(B);
41 unsigned EOffset = SM.getFileOffset(E);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000043 // Include the whole end token in the range.
Chris Lattner2c78b872009-04-14 23:22:57 +000044 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
Mike Stump1eb44332009-09-09 15:08:12 +000045
Chris Lattner2b2453a2009-01-17 06:22:33 +000046 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
47 SM.getBufferData(FID).first, StartTag, EndTag);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000048}
49
50/// HighlightRange - This is the same as the above method, but takes
51/// decomposed file locations.
52void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
53 const char *BufferStart,
54 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000055 // Insert the tag at the absolute start/end of the range.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000056 RB.InsertTextAfter(B, StartTag);
57 RB.InsertTextBefore(E, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner9402b572008-04-16 23:06:45 +000059 // Scan the range to see if there is a \r or \n. If so, and if the line is
60 // not blank, insert tags on that line as well.
61 bool HadOpenTag = true;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Chris Lattner9402b572008-04-16 23:06:45 +000063 unsigned LastNonWhiteSpace = B;
64 for (unsigned i = B; i != E; ++i) {
65 switch (BufferStart[i]) {
66 case '\r':
67 case '\n':
68 // Okay, we found a newline in the range. If we have an open tag, we need
69 // to insert a close tag at the first non-whitespace before the newline.
70 if (HadOpenTag)
Daniel Dunbard7407dc2009-08-19 19:10:30 +000071 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner9402b572008-04-16 23:06:45 +000073 // Instead of inserting an open tag immediately after the newline, we
74 // wait until we see a non-whitespace character. This prevents us from
75 // inserting tags around blank lines, and also allows the open tag to
76 // be put *after* whitespace on a non-blank line.
77 HadOpenTag = false;
78 break;
79 case '\0':
80 case ' ':
81 case '\t':
82 case '\f':
83 case '\v':
84 // Ignore whitespace.
85 break;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner9402b572008-04-16 23:06:45 +000087 default:
88 // If there is no tag open, do it now.
89 if (!HadOpenTag) {
Daniel Dunbard7407dc2009-08-19 19:10:30 +000090 RB.InsertTextAfter(i, StartTag);
Chris Lattner9402b572008-04-16 23:06:45 +000091 HadOpenTag = true;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner9402b572008-04-16 23:06:45 +000094 // Remember this character.
95 LastNonWhiteSpace = i;
96 break;
97 }
98 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000099}
100
Chris Lattner2b2453a2009-01-17 06:22:33 +0000101void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenekfa5be362008-04-08 22:37:58 +0000102 bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner2b2453a2009-01-17 06:22:33 +0000104 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek6a340832008-03-18 21:19:49 +0000105 const char* C = Buf->getBufferStart();
106 const char* FileEnd = Buf->getBufferEnd();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek6a340832008-03-18 21:19:49 +0000108 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner2b2453a2009-01-17 06:22:33 +0000110 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000111
112 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000113 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000114 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000115 default: ++ColNo; break;
116 case '\n':
117 case '\r':
118 ColNo = 0;
119 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chris Lattner73527142008-04-16 04:33:23 +0000121 case ' ':
122 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000123 RB.ReplaceText(FilePos, 1, "&nbsp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000124 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000125 break;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000126 case '\f':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000127 RB.ReplaceText(FilePos, 1, "<hr>");
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000128 ColNo = 0;
129 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Chris Lattner5c176f72008-04-18 04:54:20 +0000131 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000132 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000133 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000134 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000135 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000136 RB.ReplaceText(FilePos, 1,
137 llvm::StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
138 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
Chris Lattner73527142008-04-16 04:33:23 +0000139 else
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000140 RB.ReplaceText(FilePos, 1, llvm::StringRef(" ", NumSpaces));
Chris Lattner5c176f72008-04-18 04:54:20 +0000141 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000142 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000143 }
Chris Lattner73527142008-04-16 04:33:23 +0000144 case '<':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000145 RB.ReplaceText(FilePos, 1, "&lt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000146 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000147 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattner73527142008-04-16 04:33:23 +0000149 case '>':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000150 RB.ReplaceText(FilePos, 1, "&gt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000151 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000152 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattner73527142008-04-16 04:33:23 +0000154 case '&':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000155 RB.ReplaceText(FilePos, 1, "&amp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000156 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000157 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000158 }
159 }
160}
161
Ted Kremenekfa5be362008-04-08 22:37:58 +0000162std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
163 bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek053ef592008-03-27 17:15:29 +0000165 unsigned len = s.size();
Ted Kremeneka95d3752008-09-13 05:16:45 +0000166 std::string Str;
167 llvm::raw_string_ostream os(Str);
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Ted Kremenek053ef592008-03-27 17:15:29 +0000169 for (unsigned i = 0 ; i < len; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek053ef592008-03-27 17:15:29 +0000171 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000172 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000173 default:
174 os << c; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner8570f0b2008-04-16 04:37:29 +0000176 case ' ':
177 if (EscapeSpaces) os << "&nbsp;";
178 else os << ' ';
179 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000181 case '\t':
182 if (ReplaceTabs) {
183 if (EscapeSpaces)
184 for (unsigned i = 0; i < 4; ++i)
185 os << "&nbsp;";
186 else
187 for (unsigned i = 0; i < 4; ++i)
188 os << " ";
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190 else
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000191 os << c;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000193 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000195 case '<': os << "&lt;"; break;
196 case '>': os << "&gt;"; break;
197 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000198 }
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek053ef592008-03-27 17:15:29 +0000201 return os.str();
202}
203
Chris Lattner8570f0b2008-04-16 04:37:29 +0000204static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
205 unsigned B, unsigned E) {
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000206 llvm::SmallString<256> Str;
207 llvm::raw_svector_ostream OS(Str);
208
209 OS << "<tr><td class=\"num\" id=\"LN"
210 << LineNo << "\">"
211 << LineNo << "</td><td class=\"line\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek49cd6352008-04-03 07:12:29 +0000213 if (B == E) { // Handle empty lines.
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000214 OS << " </td></tr>";
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000215 RB.InsertTextBefore(B, OS.str());
Chris Lattner57df3b92008-04-16 04:11:35 +0000216 } else {
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000217 RB.InsertTextBefore(B, OS.str());
218 RB.InsertTextBefore(E, "</td></tr>");
Ted Kremenek49cd6352008-04-03 07:12:29 +0000219 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000220}
221
Chris Lattner2b2453a2009-01-17 06:22:33 +0000222void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000223
Chris Lattner2b2453a2009-01-17 06:22:33 +0000224 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000225 const char* FileBeg = Buf->getBufferStart();
226 const char* FileEnd = Buf->getBufferEnd();
227 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000228 RewriteBuffer &RB = R.getEditBuffer(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenekb485cd12008-03-18 23:08:51 +0000230 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenekb485cd12008-03-18 23:08:51 +0000232 unsigned LineNo = 0;
233 unsigned FilePos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
235 while (C != FileEnd) {
236
Ted Kremenekb485cd12008-03-18 23:08:51 +0000237 ++LineNo;
238 unsigned LineStartPos = FilePos;
239 unsigned LineEndPos = FileEnd - FileBeg;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekb485cd12008-03-18 23:08:51 +0000241 assert (FilePos <= LineEndPos);
242 assert (C < FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenekb485cd12008-03-18 23:08:51 +0000244 // Scan until the newline (or end-of-file).
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenek49cd6352008-04-03 07:12:29 +0000246 while (C != FileEnd) {
247 char c = *C;
248 ++C;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenek49cd6352008-04-03 07:12:29 +0000250 if (c == '\n') {
251 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000252 break;
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek49cd6352008-04-03 07:12:29 +0000255 ++FilePos;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner8570f0b2008-04-16 04:37:29 +0000258 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Chris Lattner8570f0b2008-04-16 04:37:29 +0000261 // Add one big table tag that surrounds all of the code.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000262 RB.InsertTextBefore(0, "<table class=\"code\">\n");
263 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
Ted Kremenekb485cd12008-03-18 23:08:51 +0000264}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000265
Mike Stump1eb44332009-09-09 15:08:12 +0000266void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000267 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000268
Chris Lattner2b2453a2009-01-17 06:22:33 +0000269 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000270 const char* FileStart = Buf->getBufferStart();
271 const char* FileEnd = Buf->getBufferEnd();
272
Chris Lattner2b2453a2009-01-17 06:22:33 +0000273 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
274 SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000275
Ted Kremeneka95d3752008-09-13 05:16:45 +0000276 std::string s;
277 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000278 os << "<!doctype html>\n" // Use HTML 5 doctype
279 "<html>\n<head>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000281 if (title)
282 os << "<title>" << html::EscapeText(title) << "</title>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000284 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000285 " body { color:#000000; background-color:#ffffff }\n"
286 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000287 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000288 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000289 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
290 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000291 " .comment { color: green; font-style: oblique }\n"
292 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000293 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000294 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000295 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000296 " .expansion { display: none; }\n"
297 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000298 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000299 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000300 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000301 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000302 // Macros are position: relative to provide base for expansions.
303 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000304 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000305 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000306 " .num { color:#444444 }\n"
307 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
308 " .line { white-space: pre }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000309 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
310 " .msg { -webkit-border-radius:5px }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000311 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000312 " .msg { float:left }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000313 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000314 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000315 " .msg { font-weight:bold }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000316 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
317 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000318 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000319 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000320 " .mrange { background-color:#dfddf3 }\n"
321 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000322 " .PathIndex { font-weight: bold; padding:0px 5px 0px 5px; "
323 "margin-right:5px; }\n"
Ted Kremenek00f01e42009-03-02 23:39:27 +0000324 " .PathIndex { -webkit-border-radius:8px }\n"
325 " .PathIndexEvent { background-color:#bfba87 }\n"
326 " .PathIndexControl { background-color:#8c8c8c }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000327 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
328 " .CodeRemovalHint { background-color:#de1010 }\n"
329 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000330 " table.simpletable {\n"
331 " padding: 5px;\n"
332 " font-size:12pt;\n"
333 " margin:20px;\n"
334 " border-collapse: collapse; border-spacing: 0px;\n"
335 " }\n"
336 " td.rowname {\n"
337 " text-align:right; font-weight:bold; color:#444444;\n"
338 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000339 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000340
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000341 // Generate header
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000342 R.InsertTextBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000343 // Generate footer
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000345 R.InsertTextAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000346}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000347
348/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
349/// information about keywords, macro expansions etc. This uses the macro
350/// table state from the end of the file, so it won't be perfectly perfect,
351/// but it will be reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000352void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
353 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000354
Chris Lattner05db4272009-02-13 19:33:24 +0000355 const SourceManager &SM = PP.getSourceManager();
356 Lexer L(FID, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000357 const char *BufferStart = L.getBufferStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
359 // Inform the preprocessor that we want to retain comments as tokens, so we
Chris Lattner3245a0a2008-04-16 06:11:58 +0000360 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000361 L.SetCommentRetentionState(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000363 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
364 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000365 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000366 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattner74ea3e52008-04-16 06:53:09 +0000368 while (Tok.isNot(tok::eof)) {
369 // Since we are lexing unexpanded tokens, all tokens are from the main
370 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000371 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000372 unsigned TokLen = Tok.getLength();
373 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000374 default: break;
375 case tok::identifier: {
376 // Fill in Result.IdentifierInfo, looking up the identifier in the
377 // identifier table.
378 IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnera745e8c2008-04-16 20:51:51 +0000380 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000381 if (II->getTokenID() != tok::identifier)
382 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
383 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000384 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000385 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000386 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000387 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
388 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000389 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000390 case tok::wide_string_literal:
391 // Chop off the L prefix
392 ++TokOffs;
393 --TokLen;
394 // FALL THROUGH.
395 case tok::string_literal:
396 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
397 "<span class='string_literal'>", "</span>");
398 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000399 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000400 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000401 if (!Tok.isAtStartOfLine())
402 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner5deb96d2008-04-16 23:21:17 +0000404 // Eat all of the tokens until we get to the next one at the start of
405 // line.
406 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000407 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000408 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000409 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000410 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner5deb96d2008-04-16 23:21:17 +0000413 // Find end of line. This is a hack.
414 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
415 "<span class='directive'>", "</span>");
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner5deb96d2008-04-16 23:21:17 +0000417 // Don't skip the next token.
418 continue;
419 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner590f0cc2008-10-12 01:15:46 +0000422 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000423 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000424}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000425
Chris Lattner7c175fb2009-03-13 21:44:46 +0000426namespace {
427/// IgnoringDiagClient - This is a diagnostic client that just ignores all
428/// diags.
429class IgnoringDiagClient : public DiagnosticClient {
430 void HandleDiagnostic(Diagnostic::Level DiagLevel,
431 const DiagnosticInfo &Info) {
432 // Just ignore it.
433 }
434};
435}
436
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000437/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000438/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000439/// macro expansions. This won't be perfectly perfect, but it will be
440/// reasonably close.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000441void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000442 // Re-lex the raw token stream into a token buffer.
443 const SourceManager &SM = PP.getSourceManager();
444 std::vector<Token> TokenStream;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner05db4272009-02-13 19:33:24 +0000446 Lexer L(FID, SM, PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner05db4272009-02-13 19:33:24 +0000448 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
449 // macros.
450 while (1) {
451 Token Tok;
452 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner05db4272009-02-13 19:33:24 +0000454 // If this is a # at the start of a line, discard it from the token stream.
455 // We don't want the re-preprocess step to see #defines, #includes or other
456 // preprocessor directives.
457 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
458 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000459
460 // If this is a ## token, change its kind to unknown so that repreprocessing
461 // it will not produce an error.
462 if (Tok.is(tok::hashhash))
463 Tok.setKind(tok::unknown);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner05db4272009-02-13 19:33:24 +0000465 // If this raw token is an identifier, the raw lexer won't have looked up
466 // the corresponding identifier info for it. Do this now so that it will be
467 // macro expanded when we re-preprocess it.
468 if (Tok.is(tok::identifier)) {
469 // Change the kind of this identifier to the appropriate token kind, e.g.
470 // turning "for" into a keyword.
471 Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000472 }
473
Chris Lattner05db4272009-02-13 19:33:24 +0000474 TokenStream.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattner05db4272009-02-13 19:33:24 +0000476 if (Tok.is(tok::eof)) break;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner7c175fb2009-03-13 21:44:46 +0000479 // Temporarily change the diagnostics object so that we ignore any generated
480 // diagnostics from this pass.
481 IgnoringDiagClient TmpDC;
482 Diagnostic TmpDiags(&TmpDC);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner7c175fb2009-03-13 21:44:46 +0000484 Diagnostic *OldDiags = &PP.getDiagnostics();
485 PP.setDiagnostics(TmpDiags);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000487 // Inform the preprocessor that we don't want comments.
Ted Kremenekfb586092008-04-18 05:34:33 +0000488 PP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000489
490 // Enter the tokens we just lexed. This will cause them to be macro expanded
491 // but won't enter sub-files (because we removed #'s).
492 PP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner867924d2009-02-13 00:51:30 +0000494 TokenConcatenation ConcatInfo(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000496 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000497 Token Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000498 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000499 while (Tok.isNot(tok::eof)) {
500 // Ignore non-macro tokens.
501 if (!Tok.getLocation().isMacroID()) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000502 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000503 continue;
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattnerb7949a92009-02-15 21:32:34 +0000506 // Okay, we have the first token of a macro expansion: highlight the
507 // instantiation by inserting a start tag before the macro instantiation and
508 // end tag after it.
509 std::pair<SourceLocation, SourceLocation> LLoc =
510 SM.getInstantiationRange(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattnerb7949a92009-02-15 21:32:34 +0000512 // Ignore tokens whose instantiation location was not the main file.
513 if (SM.getFileID(LLoc.first) != FID) {
Ted Kremenekfb586092008-04-18 05:34:33 +0000514 PP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000515 continue;
516 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000517
Chris Lattnerb7949a92009-02-15 21:32:34 +0000518 assert(SM.getFileID(LLoc.second) == FID &&
519 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000520
Ted Kremenekb83ded02009-07-21 21:16:46 +0000521 std::string Expansion = EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000522 unsigned LineLen = Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattner867924d2009-02-13 00:51:30 +0000524 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000525 // Okay, eat this token, getting the next one.
Ted Kremenekfb586092008-04-18 05:34:33 +0000526 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000528 // Skip all the rest of the tokens that are part of this macro
529 // instantiation. It would be really nice to pop up a window with all the
530 // spelling of the tokens or something.
531 while (!Tok.is(tok::eof) &&
Chris Lattnerb7949a92009-02-15 21:32:34 +0000532 SM.getInstantiationLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000533 // Insert a newline if the macro expansion is getting large.
534 if (LineLen > 60) {
535 Expansion += "<br>";
536 LineLen = 0;
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner6f46be22008-04-17 00:40:45 +0000539 LineLen -= Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner867924d2009-02-13 00:51:30 +0000541 // If the tokens were already space separated, or if they must be to avoid
542 // them being implicitly pasted, add a space between them.
543 if (Tok.hasLeadingSpace() ||
544 ConcatInfo.AvoidConcat(PrevTok, Tok))
545 Expansion += ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattner9227c692008-04-17 23:03:14 +0000547 // Escape any special characters in the token text.
Chris Lattner867924d2009-02-13 00:51:30 +0000548 Expansion += EscapeText(PP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000549 LineLen += Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner867924d2009-02-13 00:51:30 +0000551 PrevTok = Tok;
Ted Kremenekfb586092008-04-18 05:34:33 +0000552 PP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000555
556 // Insert the expansion as the end tag, so that multi-line macros all get
557 // highlighted.
558 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
559
560 HighlightRange(R, LLoc.first, LLoc.second,
561 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000562 }
Chris Lattner7c175fb2009-03-13 21:44:46 +0000563
564 // Restore diagnostics object back to its own thing.
565 PP.setDiagnostics(*OldDiags);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000566}