blob: 44d635a527c9fb761c4ae4e64c36da50f54e2ffa [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"
Benjamin Kramerfdd15602012-02-04 13:02:15 +000020#include "clang/Basic/Diagnostic.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000022#include "llvm/ADT/SmallString.h"
Ted Kremenek339b9c22008-04-17 22:31:54 +000023#include "llvm/ADT/OwningPtr.h"
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +000024#include "llvm/Support/ErrorHandling.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000025#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000027using namespace clang;
28
Chris Lattner9402b572008-04-16 23:06:45 +000029
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000030/// HighlightRange - Highlight a range in the source code with the specified
31/// start/end tags. B/E must be in the same file. This ensures that
32/// start/end tags are placed at the start/end of each line if the range is
33/// multiline.
34void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
35 const char *StartTag, const char *EndTag) {
36 SourceManager &SM = R.getSourceMgr();
Chandler Carruth40278532011-07-25 16:49:02 +000037 B = SM.getExpansionLoc(B);
38 E = SM.getExpansionLoc(E);
Chris Lattnera11d6172009-01-19 07:46:45 +000039 FileID FID = SM.getFileID(B);
40 assert(SM.getFileID(E) == FID && "B/E not in the same file!");
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000041
Chris Lattner52c29082009-01-27 06:27:13 +000042 unsigned BOffset = SM.getFileOffset(B);
43 unsigned EOffset = SM.getFileOffset(E);
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000045 // Include the whole end token in the range.
Chris Lattner2c78b872009-04-14 23:22:57 +000046 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregorf715ca12010-03-16 00:06:06 +000048 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +000049 const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
Douglas Gregorf715ca12010-03-16 00:06:06 +000050 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +000051 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +000052
Chris Lattner2b2453a2009-01-17 06:22:33 +000053 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
Douglas Gregoraea67db2010-03-15 22:54:52 +000054 BufferStart, StartTag, EndTag);
Chris Lattner5ef3e2c2008-04-16 22:45:51 +000055}
56
57/// HighlightRange - This is the same as the above method, but takes
58/// decomposed file locations.
59void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
60 const char *BufferStart,
61 const char *StartTag, const char *EndTag) {
Chris Lattner9402b572008-04-16 23:06:45 +000062 // Insert the tag at the absolute start/end of the range.
Daniel Dunbard7407dc2009-08-19 19:10:30 +000063 RB.InsertTextAfter(B, StartTag);
64 RB.InsertTextBefore(E, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner9402b572008-04-16 23:06:45 +000066 // Scan the range to see if there is a \r or \n. If so, and if the line is
67 // not blank, insert tags on that line as well.
68 bool HadOpenTag = true;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner9402b572008-04-16 23:06:45 +000070 unsigned LastNonWhiteSpace = B;
71 for (unsigned i = B; i != E; ++i) {
72 switch (BufferStart[i]) {
73 case '\r':
74 case '\n':
75 // Okay, we found a newline in the range. If we have an open tag, we need
76 // to insert a close tag at the first non-whitespace before the newline.
77 if (HadOpenTag)
Daniel Dunbard7407dc2009-08-19 19:10:30 +000078 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner9402b572008-04-16 23:06:45 +000080 // Instead of inserting an open tag immediately after the newline, we
81 // wait until we see a non-whitespace character. This prevents us from
82 // inserting tags around blank lines, and also allows the open tag to
83 // be put *after* whitespace on a non-blank line.
84 HadOpenTag = false;
85 break;
86 case '\0':
87 case ' ':
88 case '\t':
89 case '\f':
90 case '\v':
91 // Ignore whitespace.
92 break;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner9402b572008-04-16 23:06:45 +000094 default:
95 // If there is no tag open, do it now.
96 if (!HadOpenTag) {
Daniel Dunbard7407dc2009-08-19 19:10:30 +000097 RB.InsertTextAfter(i, StartTag);
Chris Lattner9402b572008-04-16 23:06:45 +000098 HadOpenTag = true;
99 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner9402b572008-04-16 23:06:45 +0000101 // Remember this character.
102 LastNonWhiteSpace = i;
103 break;
104 }
105 }
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000106}
107
Chris Lattner2b2453a2009-01-17 06:22:33 +0000108void html::EscapeText(Rewriter &R, FileID FID,
Ted Kremenekfa5be362008-04-08 22:37:58 +0000109 bool EscapeSpaces, bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner2b2453a2009-01-17 06:22:33 +0000111 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenek6a340832008-03-18 21:19:49 +0000112 const char* C = Buf->getBufferStart();
113 const char* FileEnd = Buf->getBufferEnd();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek6a340832008-03-18 21:19:49 +0000115 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner2b2453a2009-01-17 06:22:33 +0000117 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner5c176f72008-04-18 04:54:20 +0000118
119 unsigned ColNo = 0;
Ted Kremenek6a340832008-03-18 21:19:49 +0000120 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek6a340832008-03-18 21:19:49 +0000121 switch (*C) {
Chris Lattner5c176f72008-04-18 04:54:20 +0000122 default: ++ColNo; break;
123 case '\n':
124 case '\r':
125 ColNo = 0;
126 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner73527142008-04-16 04:33:23 +0000128 case ' ':
129 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000130 RB.ReplaceText(FilePos, 1, "&nbsp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000131 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000132 break;
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000133 case '\f':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000134 RB.ReplaceText(FilePos, 1, "<hr>");
Chris Lattnerf3d8d192008-04-19 23:56:30 +0000135 ColNo = 0;
136 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner5c176f72008-04-18 04:54:20 +0000138 case '\t': {
Chris Lattner73527142008-04-16 04:33:23 +0000139 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +0000140 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000141 unsigned NumSpaces = 8-(ColNo&7);
Chris Lattner73527142008-04-16 04:33:23 +0000142 if (EscapeSpaces)
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000143 RB.ReplaceText(FilePos, 1,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000144 StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000145 "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
Chris Lattner73527142008-04-16 04:33:23 +0000146 else
Chris Lattner5f9e2722011-07-23 10:55:15 +0000147 RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces));
Chris Lattner5c176f72008-04-18 04:54:20 +0000148 ColNo += NumSpaces;
Chris Lattner73527142008-04-16 04:33:23 +0000149 break;
Chris Lattner5c176f72008-04-18 04:54:20 +0000150 }
Chris Lattner73527142008-04-16 04:33:23 +0000151 case '<':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000152 RB.ReplaceText(FilePos, 1, "&lt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000153 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000154 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner73527142008-04-16 04:33:23 +0000156 case '>':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000157 RB.ReplaceText(FilePos, 1, "&gt;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000158 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000159 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattner73527142008-04-16 04:33:23 +0000161 case '&':
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000162 RB.ReplaceText(FilePos, 1, "&amp;");
Chris Lattner5c176f72008-04-18 04:54:20 +0000163 ++ColNo;
Chris Lattner73527142008-04-16 04:33:23 +0000164 break;
Ted Kremenek6a340832008-03-18 21:19:49 +0000165 }
166 }
167}
168
Ted Kremenekfa5be362008-04-08 22:37:58 +0000169std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
170 bool ReplaceTabs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek053ef592008-03-27 17:15:29 +0000172 unsigned len = s.size();
Ted Kremeneka95d3752008-09-13 05:16:45 +0000173 std::string Str;
174 llvm::raw_string_ostream os(Str);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek053ef592008-03-27 17:15:29 +0000176 for (unsigned i = 0 ; i < len; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek053ef592008-03-27 17:15:29 +0000178 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +0000179 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000180 default:
181 os << c; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner8570f0b2008-04-16 04:37:29 +0000183 case ' ':
184 if (EscapeSpaces) os << "&nbsp;";
185 else os << ' ';
186 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000188 case '\t':
189 if (ReplaceTabs) {
190 if (EscapeSpaces)
191 for (unsigned i = 0; i < 4; ++i)
192 os << "&nbsp;";
193 else
194 for (unsigned i = 0; i < 4; ++i)
195 os << " ";
196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197 else
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000198 os << c;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000200 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Zhongxing Xuc01b46e2009-08-17 14:13:14 +0000202 case '<': os << "&lt;"; break;
203 case '>': os << "&gt;"; break;
204 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +0000205 }
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Ted Kremenek053ef592008-03-27 17:15:29 +0000208 return os.str();
209}
210
Chris Lattner8570f0b2008-04-16 04:37:29 +0000211static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
212 unsigned B, unsigned E) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000213 SmallString<256> Str;
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000214 llvm::raw_svector_ostream OS(Str);
215
216 OS << "<tr><td class=\"num\" id=\"LN"
217 << LineNo << "\">"
218 << LineNo << "</td><td class=\"line\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenek49cd6352008-04-03 07:12:29 +0000220 if (B == E) { // Handle empty lines.
Daniel Dunbar7e37c812009-08-19 18:30:37 +0000221 OS << " </td></tr>";
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000222 RB.InsertTextBefore(B, OS.str());
Chris Lattner57df3b92008-04-16 04:11:35 +0000223 } else {
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000224 RB.InsertTextBefore(B, OS.str());
225 RB.InsertTextBefore(E, "</td></tr>");
Ted Kremenek49cd6352008-04-03 07:12:29 +0000226 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000227}
228
Chris Lattner2b2453a2009-01-17 06:22:33 +0000229void html::AddLineNumbers(Rewriter& R, FileID FID) {
Ted Kremenekb485cd12008-03-18 23:08:51 +0000230
Chris Lattner2b2453a2009-01-17 06:22:33 +0000231 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000232 const char* FileBeg = Buf->getBufferStart();
233 const char* FileEnd = Buf->getBufferEnd();
234 const char* C = FileBeg;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 RewriteBuffer &RB = R.getEditBuffer(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenekb485cd12008-03-18 23:08:51 +0000237 assert (C <= FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenekb485cd12008-03-18 23:08:51 +0000239 unsigned LineNo = 0;
240 unsigned FilePos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
242 while (C != FileEnd) {
243
Ted Kremenekb485cd12008-03-18 23:08:51 +0000244 ++LineNo;
245 unsigned LineStartPos = FilePos;
246 unsigned LineEndPos = FileEnd - FileBeg;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenekb485cd12008-03-18 23:08:51 +0000248 assert (FilePos <= LineEndPos);
249 assert (C < FileEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenekb485cd12008-03-18 23:08:51 +0000251 // Scan until the newline (or end-of-file).
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek49cd6352008-04-03 07:12:29 +0000253 while (C != FileEnd) {
254 char c = *C;
255 ++C;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek49cd6352008-04-03 07:12:29 +0000257 if (c == '\n') {
258 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000259 break;
260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek49cd6352008-04-03 07:12:29 +0000262 ++FilePos;
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattner8570f0b2008-04-16 04:37:29 +0000265 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner8570f0b2008-04-16 04:37:29 +0000268 // Add one big table tag that surrounds all of the code.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000269 RB.InsertTextBefore(0, "<table class=\"code\">\n");
270 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
Ted Kremenekb485cd12008-03-18 23:08:51 +0000271}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000272
Mike Stump1eb44332009-09-09 15:08:12 +0000273void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000274 const char *title) {
Ted Kremenekad0a2032008-03-19 06:14:37 +0000275
Chris Lattner2b2453a2009-01-17 06:22:33 +0000276 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000277 const char* FileStart = Buf->getBufferStart();
278 const char* FileEnd = Buf->getBufferEnd();
279
Chris Lattner2b2453a2009-01-17 06:22:33 +0000280 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000281 SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
Ted Kremenekad0a2032008-03-19 06:14:37 +0000282
Ted Kremeneka95d3752008-09-13 05:16:45 +0000283 std::string s;
284 llvm::raw_string_ostream os(s);
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000285 os << "<!doctype html>\n" // Use HTML 5 doctype
286 "<html>\n<head>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000288 if (title)
289 os << "<title>" << html::EscapeText(title) << "</title>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000291 os << "<style type=\"text/css\">\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000292 " body { color:#000000; background-color:#ffffff }\n"
293 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000294 " h1 { font-size:14pt }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000295 " .code { border-collapse:collapse; width:100%; }\n"
Ted Kremenek3eaaa992012-01-20 20:39:55 +0000296 " .code { font-family: \"Monospace\", monospace; font-size:10pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000297 " .code { line-height: 1.2em }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000298 " .comment { color: green; font-style: oblique }\n"
299 " .keyword { color: blue }\n"
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000300 " .string_literal { color: red }\n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000301 " .directive { color: darkmagenta }\n"
Chris Lattner6f46be22008-04-17 00:40:45 +0000302 // Macro expansions.
Ted Kremenek07339a62008-04-17 19:57:27 +0000303 " .expansion { display: none; }\n"
304 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; "
Chris Lattnerdc5be472008-04-17 21:32:46 +0000305 "padding: 2px; background-color:#FFF0F0; font-weight: normal; "
Chris Lattner6f46be22008-04-17 00:40:45 +0000306 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; "
Chris Lattner8aa06ac2008-04-17 21:28:41 +0000307 "position: absolute; top: -1em; left:10em; z-index: 1 } \n"
Ted Kremenekf5016262008-04-18 02:12:39 +0000308 " .macro { color: darkmagenta; background-color:LemonChiffon;"
Chris Lattner6f46be22008-04-17 00:40:45 +0000309 // Macros are position: relative to provide base for expansions.
310 " position: relative }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000311 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000312 " .num { text-align:right; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000313 " .num { color:#444444 }\n"
314 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
315 " .line { white-space: pre }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000316 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
317 " .msg { -webkit-border-radius:5px }\n"
Ted Kremenek22236222009-03-10 05:14:32 +0000318 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000319 " .msg { float:left }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000320 " .msg { padding:0.25em 1ex 0.25em 1ex }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000321 " .msg { margin-top:10px; margin-bottom:10px }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000322 " .msg { font-weight:bold }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000323 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n"
324 " .msgT { padding:0x; spacing:0x }\n"
Ted Kremenek2f103982009-03-02 21:42:01 +0000325 " .msgEvent { background-color:#fff8b4; color:#000000 }\n"
Ted Kremenek80bae762009-03-02 23:05:40 +0000326 " .msgControl { background-color:#bbbbbb; color:#000000 }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000327 " .mrange { background-color:#dfddf3 }\n"
328 " .mrange { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek3c598232009-03-03 03:00:21 +0000329 " .PathIndex { font-weight: bold; padding:0px 5px 0px 5px; "
330 "margin-right:5px; }\n"
Ted Kremenek00f01e42009-03-02 23:39:27 +0000331 " .PathIndex { -webkit-border-radius:8px }\n"
332 " .PathIndexEvent { background-color:#bfba87 }\n"
333 " .PathIndexControl { background-color:#8c8c8c }\n"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000334 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n"
335 " .CodeRemovalHint { background-color:#de1010 }\n"
336 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000337 " table.simpletable {\n"
338 " padding: 5px;\n"
339 " font-size:12pt;\n"
340 " margin:20px;\n"
341 " border-collapse: collapse; border-spacing: 0px;\n"
342 " }\n"
343 " td.rowname {\n"
344 " text-align:right; font-weight:bold; color:#444444;\n"
345 " padding-right:2ex; }\n"
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000346 "</style>\n</head>\n<body>";
Ted Kremenek70bcba62008-04-09 15:40:40 +0000347
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000348 // Generate header
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000349 R.InsertTextBefore(StartLoc, os.str());
Ted Kremenekad0a2032008-03-19 06:14:37 +0000350 // Generate footer
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000352 R.InsertTextAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000353}
Chris Lattner3245a0a2008-04-16 06:11:58 +0000354
355/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
356/// information about keywords, macro expansions etc. This uses the macro
357/// table state from the end of the file, so it won't be perfectly perfect,
358/// but it will be reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000359void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000360 RewriteBuffer &RB = R.getEditBuffer(FID);
Chris Lattner3245a0a2008-04-16 06:11:58 +0000361
Chris Lattner05db4272009-02-13 19:33:24 +0000362 const SourceManager &SM = PP.getSourceManager();
Chris Lattner6e290142009-11-30 04:18:44 +0000363 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
364 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Chris Lattner025c3a62009-01-17 07:35:14 +0000365 const char *BufferStart = L.getBufferStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000366
367 // Inform the preprocessor that we want to retain comments as tokens, so we
Chris Lattner3245a0a2008-04-16 06:11:58 +0000368 // can highlight them.
Chris Lattner678c6352008-04-16 20:54:51 +0000369 L.SetCommentRetentionState(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000371 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
372 // macros.
Chris Lattner3245a0a2008-04-16 06:11:58 +0000373 Token Tok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000374 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner74ea3e52008-04-16 06:53:09 +0000376 while (Tok.isNot(tok::eof)) {
377 // Since we are lexing unexpanded tokens, all tokens are from the main
378 // FileID.
Chris Lattner05db4272009-02-13 19:33:24 +0000379 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
Chris Lattner3245a0a2008-04-16 06:11:58 +0000380 unsigned TokLen = Tok.getLength();
381 switch (Tok.getKind()) {
Chris Lattnera745e8c2008-04-16 20:51:51 +0000382 default: break;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000383 case tok::identifier:
384 llvm_unreachable("tok::identifier in raw lexing mode!");
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000385 case tok::raw_identifier: {
386 // Fill in Result.IdentifierInfo and update the token kind,
387 // looking up the identifier in the identifier table.
388 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnera745e8c2008-04-16 20:51:51 +0000390 // If this is a pp-identifier, for a keyword, highlight it as such.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000391 if (Tok.isNot(tok::identifier))
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000392 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
393 "<span class='keyword'>", "</span>");
Chris Lattnerc4586c22008-04-16 06:35:07 +0000394 break;
Chris Lattnera745e8c2008-04-16 20:51:51 +0000395 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000396 case tok::comment:
Chris Lattner5ef3e2c2008-04-16 22:45:51 +0000397 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
398 "<span class='comment'>", "</span>");
Chris Lattner3245a0a2008-04-16 06:11:58 +0000399 break;
Douglas Gregor5cee1192011-07-27 05:40:30 +0000400 case tok::utf8_string_literal:
401 // Chop off the u part of u8 prefix
402 ++TokOffs;
403 --TokLen;
404 // FALL THROUGH to chop the 8
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000405 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000406 case tok::utf16_string_literal:
407 case tok::utf32_string_literal:
408 // Chop off the L, u, U or 8 prefix
Ted Kremenekcc1b8532008-08-31 16:37:56 +0000409 ++TokOffs;
410 --TokLen;
411 // FALL THROUGH.
412 case tok::string_literal:
413 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
414 "<span class='string_literal'>", "</span>");
415 break;
Chris Lattner5deb96d2008-04-16 23:21:17 +0000416 case tok::hash: {
Chris Lattner74ea3e52008-04-16 06:53:09 +0000417 // If this is a preprocessor directive, all tokens to end of line are too.
Chris Lattner5deb96d2008-04-16 23:21:17 +0000418 if (!Tok.isAtStartOfLine())
419 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner5deb96d2008-04-16 23:21:17 +0000421 // Eat all of the tokens until we get to the next one at the start of
422 // line.
423 unsigned TokEnd = TokOffs+TokLen;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000424 L.LexFromRawLexer(Tok);
Chris Lattner5deb96d2008-04-16 23:21:17 +0000425 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Chris Lattner05db4272009-02-13 19:33:24 +0000426 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
Chris Lattner590f0cc2008-10-12 01:15:46 +0000427 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattner5deb96d2008-04-16 23:21:17 +0000430 // Find end of line. This is a hack.
431 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
432 "<span class='directive'>", "</span>");
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner5deb96d2008-04-16 23:21:17 +0000434 // Don't skip the next token.
435 continue;
436 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattner590f0cc2008-10-12 01:15:46 +0000439 L.LexFromRawLexer(Tok);
Chris Lattner74ea3e52008-04-16 06:53:09 +0000440 }
Chris Lattner3245a0a2008-04-16 06:11:58 +0000441}
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000442
443/// HighlightMacros - This uses the macro table state from the end of the
Chris Lattner05db4272009-02-13 19:33:24 +0000444/// file, to re-expand macros and insert (into the HTML) information about the
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000445/// macro expansions. This won't be perfectly perfect, but it will be
446/// reasonably close.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000447void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
Chris Lattner05db4272009-02-13 19:33:24 +0000448 // Re-lex the raw token stream into a token buffer.
449 const SourceManager &SM = PP.getSourceManager();
450 std::vector<Token> TokenStream;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattner6e290142009-11-30 04:18:44 +0000452 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
453 Lexer L(FID, FromFile, SM, PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattner05db4272009-02-13 19:33:24 +0000455 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
456 // macros.
457 while (1) {
458 Token Tok;
459 L.LexFromRawLexer(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner05db4272009-02-13 19:33:24 +0000461 // If this is a # at the start of a line, discard it from the token stream.
462 // We don't want the re-preprocess step to see #defines, #includes or other
463 // preprocessor directives.
464 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
465 continue;
Chris Lattnerf0b26b12009-02-24 05:29:33 +0000466
467 // If this is a ## token, change its kind to unknown so that repreprocessing
468 // it will not produce an error.
469 if (Tok.is(tok::hashhash))
470 Tok.setKind(tok::unknown);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner05db4272009-02-13 19:33:24 +0000472 // If this raw token is an identifier, the raw lexer won't have looked up
473 // the corresponding identifier info for it. Do this now so that it will be
474 // macro expanded when we re-preprocess it.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000475 if (Tok.is(tok::raw_identifier))
476 PP.LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner05db4272009-02-13 19:33:24 +0000478 TokenStream.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattner05db4272009-02-13 19:33:24 +0000480 if (Tok.is(tok::eof)) break;
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattner7c175fb2009-03-13 21:44:46 +0000483 // Temporarily change the diagnostics object so that we ignore any generated
484 // diagnostics from this pass.
David Blaikied6471f72011-09-25 23:23:43 +0000485 DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
David Blaikief40c0ac2011-09-25 23:44:35 +0000486 new IgnoringDiagConsumer);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000488 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
489 // its state, but we aren't actually changing it (we hope). This should really
490 // construct a copy of the preprocessor.
491 Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
David Blaikied6471f72011-09-25 23:23:43 +0000492 DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000493 TmpPP.setDiagnostics(TmpDiags);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000495 // Inform the preprocessor that we don't want comments.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000496 TmpPP.SetCommentRetentionState(false, false);
Chris Lattner05db4272009-02-13 19:33:24 +0000497
498 // Enter the tokens we just lexed. This will cause them to be macro expanded
499 // but won't enter sub-files (because we removed #'s).
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000500 TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000502 TokenConcatenation ConcatInfo(TmpPP);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000504 // Lex all the tokens.
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000505 Token Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000506 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000507 while (Tok.isNot(tok::eof)) {
508 // Ignore non-macro tokens.
509 if (!Tok.getLocation().isMacroID()) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000510 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000511 continue;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnerb7949a92009-02-15 21:32:34 +0000514 // Okay, we have the first token of a macro expansion: highlight the
Chandler Carruth7c5c6762011-07-15 00:04:40 +0000515 // expansion by inserting a start tag before the macro expansion and
Chris Lattnerb7949a92009-02-15 21:32:34 +0000516 // end tag after it.
517 std::pair<SourceLocation, SourceLocation> LLoc =
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000518 SM.getExpansionRange(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattnerb7949a92009-02-15 21:32:34 +0000520 // Ignore tokens whose instantiation location was not the main file.
521 if (SM.getFileID(LLoc.first) != FID) {
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000522 TmpPP.Lex(Tok);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000523 continue;
524 }
Chris Lattnerb7949a92009-02-15 21:32:34 +0000525
Chris Lattnerb7949a92009-02-15 21:32:34 +0000526 assert(SM.getFileID(LLoc.second) == FID &&
527 "Start and end of expansion must be in the same ultimate file!");
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000528
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000529 std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000530 unsigned LineLen = Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner88773212010-04-14 03:57:19 +0000532 Token PrevPrevTok;
Chris Lattner867924d2009-02-13 00:51:30 +0000533 Token PrevTok = Tok;
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000534 // Okay, eat this token, getting the next one.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000535 TmpPP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000537 // Skip all the rest of the tokens that are part of this macro
538 // instantiation. It would be really nice to pop up a window with all the
539 // spelling of the tokens or something.
540 while (!Tok.is(tok::eof) &&
Chandler Carruth40278532011-07-25 16:49:02 +0000541 SM.getExpansionLoc(Tok.getLocation()) == LLoc.first) {
Chris Lattner6f46be22008-04-17 00:40:45 +0000542 // Insert a newline if the macro expansion is getting large.
543 if (LineLen > 60) {
544 Expansion += "<br>";
545 LineLen = 0;
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner6f46be22008-04-17 00:40:45 +0000548 LineLen -= Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner867924d2009-02-13 00:51:30 +0000550 // If the tokens were already space separated, or if they must be to avoid
551 // them being implicitly pasted, add a space between them.
552 if (Tok.hasLeadingSpace() ||
Chris Lattner88773212010-04-14 03:57:19 +0000553 ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
Chris Lattner867924d2009-02-13 00:51:30 +0000554 Expansion += ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattner9227c692008-04-17 23:03:14 +0000556 // Escape any special characters in the token text.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000557 Expansion += EscapeText(TmpPP.getSpelling(Tok));
Chris Lattner6f46be22008-04-17 00:40:45 +0000558 LineLen += Expansion.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner88773212010-04-14 03:57:19 +0000560 PrevPrevTok = PrevTok;
Chris Lattner867924d2009-02-13 00:51:30 +0000561 PrevTok = Tok;
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000562 TmpPP.Lex(Tok);
Chris Lattner6f46be22008-04-17 00:40:45 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattnere9e6cb92009-02-17 00:51:07 +0000565
566 // Insert the expansion as the end tag, so that multi-line macros all get
567 // highlighted.
568 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
569
570 HighlightRange(R, LLoc.first, LLoc.second,
571 "<span class='macro'>", Expansion.c_str());
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000572 }
Chris Lattner7c175fb2009-03-13 21:44:46 +0000573
574 // Restore diagnostics object back to its own thing.
Daniel Dunbarff6912b2009-11-05 01:54:02 +0000575 TmpPP.setDiagnostics(*OldDiags);
Chris Lattnerc54d50a2008-04-16 06:32:08 +0000576}