blob: 7b78070a27f81356d7733baf4ed1f4659b9af9ec [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
Douglas Gregorf715ca12010-03-16 00:06:06 +000046 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +000047 const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
Douglas Gregorf715ca12010-03-16 00:06:06 +000048 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +000049 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +000050
Chris Lattner2b2453a2009-01-17 06:22:33 +000051 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
Douglas Gregoraea67db2010-03-15 22:54:52 +000052 BufferStart, StartTag, EndTag);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000053}
54
55/// HighlightRange - This is the same as the above method, but takes
56/// decomposed file locations.
57void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
58 const char *BufferStart,
59 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000060 // Insert the tag at the absolute start/end of the range.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000061 RB.InsertTextAfter(B, StartTag);
62 RB.InsertTextBefore(E, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattner9402b572008-04-16 23:06:45 +000064 // Scan the range to see if there is a \r or \n. If so, and if the line is
65 // not blank, insert tags on that line as well.
66 bool HadOpenTag = true;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner9402b572008-04-16 23:06:45 +000068 unsigned LastNonWhiteSpace = B;
69 for (unsigned i = B; i != E; ++i) {
70 switch (BufferStart[i]) {
71 case '\r':
72 case '\n':
73 // Okay, we found a newline in the range. If we have an open tag, we need
74 // to insert a close tag at the first non-whitespace before the newline.
75 if (HadOpenTag)
Daniel Dunbard7407dc2009-08-19 19:10:30 +000076 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner9402b572008-04-16 23:06:45 +000078 // Instead of inserting an open tag immediately after the newline, we
79 // wait until we see a non-whitespace character. This prevents us from
80 // inserting tags around blank lines, and also allows the open tag to
81 // be put *after* whitespace on a non-blank line.
82 HadOpenTag = false;
83 break;
84 case '\0':
85 case ' ':
86 case '\t':
87 case '\f':
88 case '\v':
89 // Ignore whitespace.
90 break;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattner9402b572008-04-16 23:06:45 +000092 default:
93 // If there is no tag open, do it now.
94 if (!HadOpenTag) {
Daniel Dunbard7407dc2009-08-19 19:10:30 +000095 RB.InsertTextAfter(i, StartTag);
Chris Lattner9402b572008-04-16 23:06:45 +000096 HadOpenTag = true;
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner9402b572008-04-16 23:06:45 +000099 // Remember this character.
100 LastNonWhiteSpace = i;
101 break;
102 }
103 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000104}
105
Chris Lattner2b2453a2009-01-17 06:22:33 +0000106void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenekfa5be362008-04-08 22:37:58 +0000107 bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner2b2453a2009-01-17 06:22:33 +0000109 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek6a340832008-03-18 21:19:49 +0000110 const char* C = Buf->getBufferStart();
111 const char* FileEnd = Buf->getBufferEnd();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek6a340832008-03-18 21:19:49 +0000113 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner2b2453a2009-01-17 06:22:33 +0000115 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000116
117 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000118 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000119 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000120 default: ++ColNo; break;
121 case '\n':
122 case '\r':
123 ColNo = 0;
124 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattner73527142008-04-16 04:33:23 +0000126 case ' ':
127 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000128 RB.ReplaceText(FilePos, 1, "&nbsp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000129 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000130 break;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000131 case '\f':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000132 RB.ReplaceText(FilePos, 1, "<hr>");
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000133 ColNo = 0;
134 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattner5c176f72008-04-18 04:54:20 +0000136 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000137 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000138 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000139 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000140 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000141 RB.ReplaceText(FilePos, 1,
142 llvm::StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
143 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
Chris Lattner73527142008-04-16 04:33:23 +0000144 else
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000145 RB.ReplaceText(FilePos, 1, llvm::StringRef(" ", NumSpaces));
Chris Lattner5c176f72008-04-18 04:54:20 +0000146 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000147 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000148 }
Chris Lattner73527142008-04-16 04:33:23 +0000149 case '<':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000150 RB.ReplaceText(FilePos, 1, "&lt;");
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, "&gt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000156 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000157 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner73527142008-04-16 04:33:23 +0000159 case '&':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000160 RB.ReplaceText(FilePos, 1, "&amp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000161 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000162 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000163 }
164 }
165}
166
Ted Kremenekfa5be362008-04-08 22:37:58 +0000167std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
168 bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek053ef592008-03-27 17:15:29 +0000170 unsigned len = s.size();
Ted Kremeneka95d3752008-09-13 05:16:45 +0000171 std::string Str;
172 llvm::raw_string_ostream os(Str);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenek053ef592008-03-27 17:15:29 +0000174 for (unsigned i = 0 ; i < len; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek053ef592008-03-27 17:15:29 +0000176 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000177 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000178 default:
179 os << c; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner8570f0b2008-04-16 04:37:29 +0000181 case ' ':
182 if (EscapeSpaces) os << "&nbsp;";
183 else os << ' ';
184 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000186 case '\t':
187 if (ReplaceTabs) {
188 if (EscapeSpaces)
189 for (unsigned i = 0; i < 4; ++i)
190 os << "&nbsp;";
191 else
192 for (unsigned i = 0; i < 4; ++i)
193 os << " ";
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195 else
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000196 os << c;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000198 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000200 case '<': os << "&lt;"; break;
201 case '>': os << "&gt;"; break;
202 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000203 }
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek053ef592008-03-27 17:15:29 +0000206 return os.str();
207}
208
Chris Lattner8570f0b2008-04-16 04:37:29 +0000209static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
210 unsigned B, unsigned E) {
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000211 llvm::SmallString<256> Str;
212 llvm::raw_svector_ostream OS(Str);
213
214 OS << "<tr><td class=\"num\" id=\"LN"
215 << LineNo << "\">"
216 << LineNo << "</td><td class=\"line\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek49cd6352008-04-03 07:12:29 +0000218 if (B == E) { // Handle empty lines.
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000219 OS << " </td></tr>";
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000220 RB.InsertTextBefore(B, OS.str());
Chris Lattner57df3b92008-04-16 04:11:35 +0000221 } else {
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000222 RB.InsertTextBefore(B, OS.str());
223 RB.InsertTextBefore(E, "</td></tr>");
Ted Kremenek49cd6352008-04-03 07:12:29 +0000224 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000225}
226
Chris Lattner2b2453a2009-01-17 06:22:33 +0000227void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000228
Chris Lattner2b2453a2009-01-17 06:22:33 +0000229 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000230 const char* FileBeg = Buf->getBufferStart();
231 const char* FileEnd = Buf->getBufferEnd();
232 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000233 RewriteBuffer &RB = R.getEditBuffer(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenekb485cd12008-03-18 23:08:51 +0000235 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenekb485cd12008-03-18 23:08:51 +0000237 unsigned LineNo = 0;
238 unsigned FilePos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
240 while (C != FileEnd) {
241
Ted Kremenekb485cd12008-03-18 23:08:51 +0000242 ++LineNo;
243 unsigned LineStartPos = FilePos;
244 unsigned LineEndPos = FileEnd - FileBeg;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenekb485cd12008-03-18 23:08:51 +0000246 assert (FilePos <= LineEndPos);
247 assert (C < FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenekb485cd12008-03-18 23:08:51 +0000249 // Scan until the newline (or end-of-file).
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek49cd6352008-04-03 07:12:29 +0000251 while (C != FileEnd) {
252 char c = *C;
253 ++C;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek49cd6352008-04-03 07:12:29 +0000255 if (c == '\n') {
256 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000257 break;
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Ted Kremenek49cd6352008-04-03 07:12:29 +0000260 ++FilePos;
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner8570f0b2008-04-16 04:37:29 +0000263 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner8570f0b2008-04-16 04:37:29 +0000266 // Add one big table tag that surrounds all of the code.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000267 RB.InsertTextBefore(0, "<table class=\"code\">\n");
268 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
Ted Kremenekb485cd12008-03-18 23:08:51 +0000269}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000270
Mike Stump1eb44332009-09-09 15:08:12 +0000271void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000272 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000273
Chris Lattner2b2453a2009-01-17 06:22:33 +0000274 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000275 const char* FileStart = Buf->getBufferStart();
276 const char* FileEnd = Buf->getBufferEnd();
277
Chris Lattner2b2453a2009-01-17 06:22:33 +0000278 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
279 SourceLocation EndLoc = StartLoc.getFileLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000280
Ted Kremeneka95d3752008-09-13 05:16:45 +0000281 std::string s;
282 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000283 os << "<!doctype html>\n" // Use HTML 5 doctype
284 "<html>\n<head>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000286 if (title)
287 os << "<title>" << html::EscapeText(title) << "</title>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000289 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000290 " body { color:#000000; background-color:#ffffff }\n"
291 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000292 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000293 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000294 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
295 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000296 " .comment { color: green; font-style: oblique }\n"
297 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000298 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000299 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000300 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000301 " .expansion { display: none; }\n"
302 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000303 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000304 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000305 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000306 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000307 // Macros are position: relative to provide base for expansions.
308 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000309 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000310 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000311 " .num { color:#444444 }\n"
312 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
313 " .line { white-space: pre }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000314 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
315 " .msg { -webkit-border-radius:5px }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000316 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000317 " .msg { float:left }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000318 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000319 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000320 " .msg { font-weight:bold }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000321 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
322 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000323 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000324 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000325 " .mrange { background-color:#dfddf3 }\n"
326 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000327 " .PathIndex { font-weight: bold; padding:0px 5px 0px 5px; "
328 "margin-right:5px; }\n"
Ted Kremenek00f01e42009-03-02 23:39:27 +0000329 " .PathIndex { -webkit-border-radius:8px }\n"
330 " .PathIndexEvent { background-color:#bfba87 }\n"
331 " .PathIndexControl { background-color:#8c8c8c }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000332 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
333 " .CodeRemovalHint { background-color:#de1010 }\n"
334 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000335 " table.simpletable {\n"
336 " padding: 5px;\n"
337 " font-size:12pt;\n"
338 " margin:20px;\n"
339 " border-collapse: collapse; border-spacing: 0px;\n"
340 " }\n"
341 " td.rowname {\n"
342 " text-align:right; font-weight:bold; color:#444444;\n"
343 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000344 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000345
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000346 // Generate header
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000347 R.InsertTextBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000348 // Generate footer
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000350 R.InsertTextAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000351}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000352
353/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
354/// information about keywords, macro expansions etc. This uses the macro
355/// table state from the end of the file, so it won't be perfectly perfect,
356/// but it will be reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000357void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000358 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000359
Chris Lattner05db4272009-02-13 19:33:24 +0000360 const SourceManager &SM = PP.getSourceManager();
Chris Lattner6e290142009-11-30 04:18:44 +0000361 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
362 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000363 const char *BufferStart = L.getBufferStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000364
365 // Inform the preprocessor that we want to retain comments as tokens, so we
Chris Lattner3245a0a2008-04-16 06:11:58 +0000366 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000367 L.SetCommentRetentionState(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000369 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
370 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000371 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000372 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Chris Lattner74ea3e52008-04-16 06:53:09 +0000374 while (Tok.isNot(tok::eof)) {
375 // Since we are lexing unexpanded tokens, all tokens are from the main
376 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000377 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000378 unsigned TokLen = Tok.getLength();
379 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000380 default: break;
381 case tok::identifier: {
382 // Fill in Result.IdentifierInfo, looking up the identifier in the
383 // identifier table.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000384 const IdentifierInfo *II =
385 PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattnera745e8c2008-04-16 20:51:51 +0000387 // If this is a pp-identifier, for a keyword, highlight it as such.
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000388 if (II->getTokenID() != tok::identifier)
389 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
390 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000391 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000392 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000393 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000394 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
395 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000396 break;
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000397 case tok::wide_string_literal:
398 // Chop off the L prefix
399 ++TokOffs;
400 --TokLen;
401 // FALL THROUGH.
402 case tok::string_literal:
403 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
404 "<span class='string_literal'>", "</span>");
405 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000406 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000407 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000408 if (!Tok.isAtStartOfLine())
409 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattner5deb96d2008-04-16 23:21:17 +0000411 // Eat all of the tokens until we get to the next one at the start of
412 // line.
413 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000414 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000415 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000416 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000417 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner5deb96d2008-04-16 23:21:17 +0000420 // Find end of line. This is a hack.
421 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
422 "<span class='directive'>", "</span>");
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner5deb96d2008-04-16 23:21:17 +0000424 // Don't skip the next token.
425 continue;
426 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattner590f0cc2008-10-12 01:15:46 +0000429 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000430 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000431}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000432
Chris Lattner7c175fb2009-03-13 21:44:46 +0000433namespace {
434/// IgnoringDiagClient - This is a diagnostic client that just ignores all
435/// diags.
436class IgnoringDiagClient : public DiagnosticClient {
437 void HandleDiagnostic(Diagnostic::Level DiagLevel,
438 const DiagnosticInfo &Info) {
439 // Just ignore it.
440 }
441};
442}
443
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000444/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000445/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000446/// macro expansions. This won't be perfectly perfect, but it will be
447/// reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000448void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000449 // Re-lex the raw token stream into a token buffer.
450 const SourceManager &SM = PP.getSourceManager();
451 std::vector<Token> TokenStream;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner6e290142009-11-30 04:18:44 +0000453 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
454 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner05db4272009-02-13 19:33:24 +0000456 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
457 // macros.
458 while (1) {
459 Token Tok;
460 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner05db4272009-02-13 19:33:24 +0000462 // If this is a # at the start of a line, discard it from the token stream.
463 // We don't want the re-preprocess step to see #defines, #includes or other
464 // preprocessor directives.
465 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
466 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000467
468 // If this is a ## token, change its kind to unknown so that repreprocessing
469 // it will not produce an error.
470 if (Tok.is(tok::hashhash))
471 Tok.setKind(tok::unknown);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner05db4272009-02-13 19:33:24 +0000473 // If this raw token is an identifier, the raw lexer won't have looked up
474 // the corresponding identifier info for it. Do this now so that it will be
475 // macro expanded when we re-preprocess it.
476 if (Tok.is(tok::identifier)) {
477 // Change the kind of this identifier to the appropriate token kind, e.g.
478 // turning "for" into a keyword.
479 Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000480 }
481
Chris Lattner05db4272009-02-13 19:33:24 +0000482 TokenStream.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner05db4272009-02-13 19:33:24 +0000484 if (Tok.is(tok::eof)) break;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattner7c175fb2009-03-13 21:44:46 +0000487 // Temporarily change the diagnostics object so that we ignore any generated
488 // diagnostics from this pass.
489 IgnoringDiagClient TmpDC;
490 Diagnostic TmpDiags(&TmpDC);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000492 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
493 // its state, but we aren't actually changing it (we hope). This should really
494 // construct a copy of the preprocessor.
495 Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
496 Diagnostic *OldDiags = &TmpPP.getDiagnostics();
497 TmpPP.setDiagnostics(TmpDiags);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000499 // Inform the preprocessor that we don't want comments.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000500 TmpPP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000501
502 // Enter the tokens we just lexed. This will cause them to be macro expanded
503 // but won't enter sub-files (because we removed #'s).
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000504 TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000506 TokenConcatenation ConcatInfo(TmpPP);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000508 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000509 Token Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000510 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000511 while (Tok.isNot(tok::eof)) {
512 // Ignore non-macro tokens.
513 if (!Tok.getLocation().isMacroID()) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000514 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000515 continue;
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattnerb7949a92009-02-15 21:32:34 +0000518 // Okay, we have the first token of a macro expansion: highlight the
519 // instantiation by inserting a start tag before the macro instantiation and
520 // end tag after it.
521 std::pair<SourceLocation, SourceLocation> LLoc =
522 SM.getInstantiationRange(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattnerb7949a92009-02-15 21:32:34 +0000524 // Ignore tokens whose instantiation location was not the main file.
525 if (SM.getFileID(LLoc.first) != FID) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000526 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000527 continue;
528 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000529
Chris Lattnerb7949a92009-02-15 21:32:34 +0000530 assert(SM.getFileID(LLoc.second) == FID &&
531 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000532
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000533 std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000534 unsigned LineLen = Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattner867924d2009-02-13 00:51:30 +0000536 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000537 // Okay, eat this token, getting the next one.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000538 TmpPP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000540 // Skip all the rest of the tokens that are part of this macro
541 // instantiation. It would be really nice to pop up a window with all the
542 // spelling of the tokens or something.
543 while (!Tok.is(tok::eof) &&
Chris Lattnerb7949a92009-02-15 21:32:34 +0000544 SM.getInstantiationLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000545 // Insert a newline if the macro expansion is getting large.
546 if (LineLen > 60) {
547 Expansion += "<br>";
548 LineLen = 0;
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner6f46be22008-04-17 00:40:45 +0000551 LineLen -= Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattner867924d2009-02-13 00:51:30 +0000553 // If the tokens were already space separated, or if they must be to avoid
554 // them being implicitly pasted, add a space between them.
555 if (Tok.hasLeadingSpace() ||
556 ConcatInfo.AvoidConcat(PrevTok, Tok))
557 Expansion += ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner9227c692008-04-17 23:03:14 +0000559 // Escape any special characters in the token text.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000560 Expansion += EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000561 LineLen += Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner867924d2009-02-13 00:51:30 +0000563 PrevTok = Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000564 TmpPP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000567
568 // Insert the expansion as the end tag, so that multi-line macros all get
569 // highlighted.
570 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
571
572 HighlightRange(R, LLoc.first, LLoc.second,
573 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000574 }
Chris Lattner7c175fb2009-03-13 21:44:46 +0000575
576 // Restore diagnostics object back to its own thing.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000577 TmpPP.setDiagnostics(*OldDiags);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000578}