HTMLRewrite.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the HTMLRewriter class, which is used to translate the
  11. // text of a source file into prettified HTML.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Rewrite/Core/HTMLRewrite.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. #include "clang/Lex/TokenConcatenation.h"
  18. #include "clang/Rewrite/Core/Rewriter.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <memory>
  24. using namespace clang;
  25. /// HighlightRange - Highlight a range in the source code with the specified
  26. /// start/end tags. B/E must be in the same file. This ensures that
  27. /// start/end tags are placed at the start/end of each line if the range is
  28. /// multiline.
  29. void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
  30. const char *StartTag, const char *EndTag,
  31. bool IsTokenRange) {
  32. SourceManager &SM = R.getSourceMgr();
  33. B = SM.getExpansionLoc(B);
  34. E = SM.getExpansionLoc(E);
  35. FileID FID = SM.getFileID(B);
  36. assert(SM.getFileID(E) == FID && "B/E not in the same file!");
  37. unsigned BOffset = SM.getFileOffset(B);
  38. unsigned EOffset = SM.getFileOffset(E);
  39. // Include the whole end token in the range.
  40. if (IsTokenRange)
  41. EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
  42. bool Invalid = false;
  43. const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
  44. if (Invalid)
  45. return;
  46. HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
  47. BufferStart, StartTag, EndTag);
  48. }
  49. /// HighlightRange - This is the same as the above method, but takes
  50. /// decomposed file locations.
  51. void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
  52. const char *BufferStart,
  53. const char *StartTag, const char *EndTag) {
  54. // Insert the tag at the absolute start/end of the range.
  55. RB.InsertTextAfter(B, StartTag);
  56. RB.InsertTextBefore(E, EndTag);
  57. // Scan the range to see if there is a \r or \n. If so, and if the line is
  58. // not blank, insert tags on that line as well.
  59. bool HadOpenTag = true;
  60. unsigned LastNonWhiteSpace = B;
  61. for (unsigned i = B; i != E; ++i) {
  62. switch (BufferStart[i]) {
  63. case '\r':
  64. case '\n':
  65. // Okay, we found a newline in the range. If we have an open tag, we need
  66. // to insert a close tag at the first non-whitespace before the newline.
  67. if (HadOpenTag)
  68. RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
  69. // Instead of inserting an open tag immediately after the newline, we
  70. // wait until we see a non-whitespace character. This prevents us from
  71. // inserting tags around blank lines, and also allows the open tag to
  72. // be put *after* whitespace on a non-blank line.
  73. HadOpenTag = false;
  74. break;
  75. case '\0':
  76. case ' ':
  77. case '\t':
  78. case '\f':
  79. case '\v':
  80. // Ignore whitespace.
  81. break;
  82. default:
  83. // If there is no tag open, do it now.
  84. if (!HadOpenTag) {
  85. RB.InsertTextAfter(i, StartTag);
  86. HadOpenTag = true;
  87. }
  88. // Remember this character.
  89. LastNonWhiteSpace = i;
  90. break;
  91. }
  92. }
  93. }
  94. void html::EscapeText(Rewriter &R, FileID FID,
  95. bool EscapeSpaces, bool ReplaceTabs) {
  96. const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
  97. const char* C = Buf->getBufferStart();
  98. const char* FileEnd = Buf->getBufferEnd();
  99. assert (C <= FileEnd);
  100. RewriteBuffer &RB = R.getEditBuffer(FID);
  101. unsigned ColNo = 0;
  102. for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
  103. switch (*C) {
  104. default: ++ColNo; break;
  105. case '\n':
  106. case '\r':
  107. ColNo = 0;
  108. break;
  109. case ' ':
  110. if (EscapeSpaces)
  111. RB.ReplaceText(FilePos, 1, "&nbsp;");
  112. ++ColNo;
  113. break;
  114. case '\f':
  115. RB.ReplaceText(FilePos, 1, "<hr>");
  116. ColNo = 0;
  117. break;
  118. case '\t': {
  119. if (!ReplaceTabs)
  120. break;
  121. unsigned NumSpaces = 8-(ColNo&7);
  122. if (EscapeSpaces)
  123. RB.ReplaceText(FilePos, 1,
  124. StringRef("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
  125. "&nbsp;&nbsp;&nbsp;", 6*NumSpaces));
  126. else
  127. RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces));
  128. ColNo += NumSpaces;
  129. break;
  130. }
  131. case '<':
  132. RB.ReplaceText(FilePos, 1, "&lt;");
  133. ++ColNo;
  134. break;
  135. case '>':
  136. RB.ReplaceText(FilePos, 1, "&gt;");
  137. ++ColNo;
  138. break;
  139. case '&':
  140. RB.ReplaceText(FilePos, 1, "&amp;");
  141. ++ColNo;
  142. break;
  143. }
  144. }
  145. }
  146. std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
  147. unsigned len = s.size();
  148. std::string Str;
  149. llvm::raw_string_ostream os(Str);
  150. for (unsigned i = 0 ; i < len; ++i) {
  151. char c = s[i];
  152. switch (c) {
  153. default:
  154. os << c; break;
  155. case ' ':
  156. if (EscapeSpaces) os << "&nbsp;";
  157. else os << ' ';
  158. break;
  159. case '\t':
  160. if (ReplaceTabs) {
  161. if (EscapeSpaces)
  162. for (unsigned i = 0; i < 4; ++i)
  163. os << "&nbsp;";
  164. else
  165. for (unsigned i = 0; i < 4; ++i)
  166. os << " ";
  167. }
  168. else
  169. os << c;
  170. break;
  171. case '<': os << "&lt;"; break;
  172. case '>': os << "&gt;"; break;
  173. case '&': os << "&amp;"; break;
  174. }
  175. }
  176. return os.str();
  177. }
  178. static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
  179. unsigned B, unsigned E) {
  180. SmallString<256> Str;
  181. llvm::raw_svector_ostream OS(Str);
  182. OS << "<tr class=\"codeline\" data-linenumber=\"" << LineNo << "\">"
  183. << "<td class=\"num\" id=\"LN" << LineNo << "\">" << LineNo
  184. << "</td><td class=\"line\">";
  185. if (B == E) { // Handle empty lines.
  186. OS << " </td></tr>";
  187. RB.InsertTextBefore(B, OS.str());
  188. } else {
  189. RB.InsertTextBefore(B, OS.str());
  190. RB.InsertTextBefore(E, "</td></tr>");
  191. }
  192. }
  193. void html::AddLineNumbers(Rewriter& R, FileID FID) {
  194. const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
  195. const char* FileBeg = Buf->getBufferStart();
  196. const char* FileEnd = Buf->getBufferEnd();
  197. const char* C = FileBeg;
  198. RewriteBuffer &RB = R.getEditBuffer(FID);
  199. assert (C <= FileEnd);
  200. unsigned LineNo = 0;
  201. unsigned FilePos = 0;
  202. while (C != FileEnd) {
  203. ++LineNo;
  204. unsigned LineStartPos = FilePos;
  205. unsigned LineEndPos = FileEnd - FileBeg;
  206. assert (FilePos <= LineEndPos);
  207. assert (C < FileEnd);
  208. // Scan until the newline (or end-of-file).
  209. while (C != FileEnd) {
  210. char c = *C;
  211. ++C;
  212. if (c == '\n') {
  213. LineEndPos = FilePos++;
  214. break;
  215. }
  216. ++FilePos;
  217. }
  218. AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
  219. }
  220. // Add one big table tag that surrounds all of the code.
  221. std::string s;
  222. llvm::raw_string_ostream os(s);
  223. os << "<table class=\"code\" data-fileid=\"" << FID.getHashValue() << "\">\n";
  224. RB.InsertTextBefore(0, os.str());
  225. RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
  226. }
  227. void html::AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID,
  228. StringRef title) {
  229. const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
  230. const char* FileStart = Buf->getBufferStart();
  231. const char* FileEnd = Buf->getBufferEnd();
  232. SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
  233. SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
  234. std::string s;
  235. llvm::raw_string_ostream os(s);
  236. os << "<!doctype html>\n" // Use HTML 5 doctype
  237. "<html>\n<head>\n";
  238. if (!title.empty())
  239. os << "<title>" << html::EscapeText(title) << "</title>\n";
  240. os << R"<<<(
  241. <style type="text/css">
  242. body { color:#000000; background-color:#ffffff }
  243. body { font-family:Helvetica, sans-serif; font-size:10pt }
  244. h1 { font-size:14pt }
  245. .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }
  246. .FileNav { margin-left: 5px; margin-right: 5px; display: inline; }
  247. .FileNav a { text-decoration:none; font-size: larger; }
  248. .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }
  249. .divider { background-color: gray; }
  250. .code { border-collapse:collapse; width:100%; }
  251. .code { font-family: "Monospace", monospace; font-size:10pt }
  252. .code { line-height: 1.2em }
  253. .comment { color: green; font-style: oblique }
  254. .keyword { color: blue }
  255. .string_literal { color: red }
  256. .directive { color: darkmagenta }
  257. /* Macro expansions. */
  258. .expansion { display: none; }
  259. .macro:hover .expansion {
  260. display: block;
  261. border: 2px solid #FF0000;
  262. padding: 2px;
  263. background-color:#FFF0F0;
  264. font-weight: normal;
  265. -webkit-border-radius:5px;
  266. -webkit-box-shadow:1px 1px 7px #000;
  267. border-radius:5px;
  268. box-shadow:1px 1px 7px #000;
  269. position: absolute;
  270. top: -1em;
  271. left:10em;
  272. z-index: 1
  273. }
  274. #tooltiphint {
  275. position: fixed;
  276. width: 50em;
  277. margin-left: -25em;
  278. left: 50%;
  279. padding: 10px;
  280. border: 1px solid #b0b0b0;
  281. border-radius: 2px;
  282. box-shadow: 1px 1px 7px black;
  283. background-color: #c0c0c0;
  284. z-index: 2;
  285. }
  286. .macro {
  287. color: darkmagenta;
  288. background-color:LemonChiffon;
  289. /* Macros are position: relative to provide base for expansions. */
  290. position: relative;
  291. }
  292. .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
  293. .num { text-align:right; font-size:8pt }
  294. .num { color:#444444 }
  295. .line { padding-left: 1ex; border-left: 3px solid #ccc }
  296. .line { white-space: pre }
  297. .msg { -webkit-box-shadow:1px 1px 7px #000 }
  298. .msg { box-shadow:1px 1px 7px #000 }
  299. .msg { -webkit-border-radius:5px }
  300. .msg { border-radius:5px }
  301. .msg { font-family:Helvetica, sans-serif; font-size:8pt }
  302. .msg { float:left }
  303. .msg { padding:0.25em 1ex 0.25em 1ex }
  304. .msg { margin-top:10px; margin-bottom:10px }
  305. .msg { font-weight:bold }
  306. .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
  307. .msgT { padding:0x; spacing:0x }
  308. .msgEvent { background-color:#fff8b4; color:#000000 }
  309. .msgControl { background-color:#bbbbbb; color:#000000 }
  310. .msgNote { background-color:#ddeeff; color:#000000 }
  311. .mrange { background-color:#dfddf3 }
  312. .mrange { border-bottom:1px solid #6F9DBE }
  313. .PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
  314. .PathIndex { -webkit-border-radius:8px }
  315. .PathIndex { border-radius:8px }
  316. .PathIndexEvent { background-color:#bfba87 }
  317. .PathIndexControl { background-color:#8c8c8c }
  318. .PathNav a { text-decoration:none; font-size: larger }
  319. .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
  320. .CodeRemovalHint { background-color:#de1010 }
  321. .CodeRemovalHint { border-bottom:1px solid #6F9DBE }
  322. .selected{ background-color:orange !important; }
  323. table.simpletable {
  324. padding: 5px;
  325. font-size:12pt;
  326. margin:20px;
  327. border-collapse: collapse; border-spacing: 0px;
  328. }
  329. td.rowname {
  330. text-align: right;
  331. vertical-align: top;
  332. font-weight: bold;
  333. color:#444444;
  334. padding-right:2ex;
  335. }
  336. /* Hidden text. */
  337. input.spoilerhider + label {
  338. cursor: pointer;
  339. text-decoration: underline;
  340. display: block;
  341. }
  342. input.spoilerhider {
  343. display: none;
  344. }
  345. input.spoilerhider ~ .spoiler {
  346. overflow: hidden;
  347. margin: 10px auto 0;
  348. height: 0;
  349. opacity: 0;
  350. }
  351. input.spoilerhider:checked + label + .spoiler{
  352. height: auto;
  353. opacity: 1;
  354. }
  355. </style>
  356. </head>
  357. <body>)<<<";
  358. // Generate header
  359. R.InsertTextBefore(StartLoc, os.str());
  360. // Generate footer
  361. R.InsertTextAfter(EndLoc, "</body></html>\n");
  362. }
  363. /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
  364. /// information about keywords, macro expansions etc. This uses the macro
  365. /// table state from the end of the file, so it won't be perfectly perfect,
  366. /// but it will be reasonably close.
  367. void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
  368. RewriteBuffer &RB = R.getEditBuffer(FID);
  369. const SourceManager &SM = PP.getSourceManager();
  370. const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
  371. Lexer L(FID, FromFile, SM, PP.getLangOpts());
  372. const char *BufferStart = L.getBuffer().data();
  373. // Inform the preprocessor that we want to retain comments as tokens, so we
  374. // can highlight them.
  375. L.SetCommentRetentionState(true);
  376. // Lex all the tokens in raw mode, to avoid entering #includes or expanding
  377. // macros.
  378. Token Tok;
  379. L.LexFromRawLexer(Tok);
  380. while (Tok.isNot(tok::eof)) {
  381. // Since we are lexing unexpanded tokens, all tokens are from the main
  382. // FileID.
  383. unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
  384. unsigned TokLen = Tok.getLength();
  385. switch (Tok.getKind()) {
  386. default: break;
  387. case tok::identifier:
  388. llvm_unreachable("tok::identifier in raw lexing mode!");
  389. case tok::raw_identifier: {
  390. // Fill in Result.IdentifierInfo and update the token kind,
  391. // looking up the identifier in the identifier table.
  392. PP.LookUpIdentifierInfo(Tok);
  393. // If this is a pp-identifier, for a keyword, highlight it as such.
  394. if (Tok.isNot(tok::identifier))
  395. HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
  396. "<span class='keyword'>", "</span>");
  397. break;
  398. }
  399. case tok::comment:
  400. HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
  401. "<span class='comment'>", "</span>");
  402. break;
  403. case tok::utf8_string_literal:
  404. // Chop off the u part of u8 prefix
  405. ++TokOffs;
  406. --TokLen;
  407. // FALL THROUGH to chop the 8
  408. LLVM_FALLTHROUGH;
  409. case tok::wide_string_literal:
  410. case tok::utf16_string_literal:
  411. case tok::utf32_string_literal:
  412. // Chop off the L, u, U or 8 prefix
  413. ++TokOffs;
  414. --TokLen;
  415. // FALL THROUGH.
  416. case tok::string_literal:
  417. // FIXME: Exclude the optional ud-suffix from the highlighted range.
  418. HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
  419. "<span class='string_literal'>", "</span>");
  420. break;
  421. case tok::hash: {
  422. // If this is a preprocessor directive, all tokens to end of line are too.
  423. if (!Tok.isAtStartOfLine())
  424. break;
  425. // Eat all of the tokens until we get to the next one at the start of
  426. // line.
  427. unsigned TokEnd = TokOffs+TokLen;
  428. L.LexFromRawLexer(Tok);
  429. while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
  430. TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
  431. L.LexFromRawLexer(Tok);
  432. }
  433. // Find end of line. This is a hack.
  434. HighlightRange(RB, TokOffs, TokEnd, BufferStart,
  435. "<span class='directive'>", "</span>");
  436. // Don't skip the next token.
  437. continue;
  438. }
  439. }
  440. L.LexFromRawLexer(Tok);
  441. }
  442. }
  443. /// HighlightMacros - This uses the macro table state from the end of the
  444. /// file, to re-expand macros and insert (into the HTML) information about the
  445. /// macro expansions. This won't be perfectly perfect, but it will be
  446. /// reasonably close.
  447. void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
  448. // Re-lex the raw token stream into a token buffer.
  449. const SourceManager &SM = PP.getSourceManager();
  450. std::vector<Token> TokenStream;
  451. const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
  452. Lexer L(FID, FromFile, SM, PP.getLangOpts());
  453. // Lex all the tokens in raw mode, to avoid entering #includes or expanding
  454. // macros.
  455. while (1) {
  456. Token Tok;
  457. L.LexFromRawLexer(Tok);
  458. // If this is a # at the start of a line, discard it from the token stream.
  459. // We don't want the re-preprocess step to see #defines, #includes or other
  460. // preprocessor directives.
  461. if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
  462. continue;
  463. // If this is a ## token, change its kind to unknown so that repreprocessing
  464. // it will not produce an error.
  465. if (Tok.is(tok::hashhash))
  466. Tok.setKind(tok::unknown);
  467. // If this raw token is an identifier, the raw lexer won't have looked up
  468. // the corresponding identifier info for it. Do this now so that it will be
  469. // macro expanded when we re-preprocess it.
  470. if (Tok.is(tok::raw_identifier))
  471. PP.LookUpIdentifierInfo(Tok);
  472. TokenStream.push_back(Tok);
  473. if (Tok.is(tok::eof)) break;
  474. }
  475. // Temporarily change the diagnostics object so that we ignore any generated
  476. // diagnostics from this pass.
  477. DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
  478. &PP.getDiagnostics().getDiagnosticOptions(),
  479. new IgnoringDiagConsumer);
  480. // FIXME: This is a huge hack; we reuse the input preprocessor because we want
  481. // its state, but we aren't actually changing it (we hope). This should really
  482. // construct a copy of the preprocessor.
  483. Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
  484. DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
  485. TmpPP.setDiagnostics(TmpDiags);
  486. // Inform the preprocessor that we don't want comments.
  487. TmpPP.SetCommentRetentionState(false, false);
  488. // We don't want pragmas either. Although we filtered out #pragma, removing
  489. // _Pragma and __pragma is much harder.
  490. bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
  491. TmpPP.setPragmasEnabled(false);
  492. // Enter the tokens we just lexed. This will cause them to be macro expanded
  493. // but won't enter sub-files (because we removed #'s).
  494. TmpPP.EnterTokenStream(TokenStream, false);
  495. TokenConcatenation ConcatInfo(TmpPP);
  496. // Lex all the tokens.
  497. Token Tok;
  498. TmpPP.Lex(Tok);
  499. while (Tok.isNot(tok::eof)) {
  500. // Ignore non-macro tokens.
  501. if (!Tok.getLocation().isMacroID()) {
  502. TmpPP.Lex(Tok);
  503. continue;
  504. }
  505. // Okay, we have the first token of a macro expansion: highlight the
  506. // expansion by inserting a start tag before the macro expansion and
  507. // end tag after it.
  508. CharSourceRange LLoc = SM.getExpansionRange(Tok.getLocation());
  509. // Ignore tokens whose instantiation location was not the main file.
  510. if (SM.getFileID(LLoc.getBegin()) != FID) {
  511. TmpPP.Lex(Tok);
  512. continue;
  513. }
  514. assert(SM.getFileID(LLoc.getEnd()) == FID &&
  515. "Start and end of expansion must be in the same ultimate file!");
  516. std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
  517. unsigned LineLen = Expansion.size();
  518. Token PrevPrevTok;
  519. Token PrevTok = Tok;
  520. // Okay, eat this token, getting the next one.
  521. TmpPP.Lex(Tok);
  522. // Skip all the rest of the tokens that are part of this macro
  523. // instantiation. It would be really nice to pop up a window with all the
  524. // spelling of the tokens or something.
  525. while (!Tok.is(tok::eof) &&
  526. SM.getExpansionLoc(Tok.getLocation()) == LLoc.getBegin()) {
  527. // Insert a newline if the macro expansion is getting large.
  528. if (LineLen > 60) {
  529. Expansion += "<br>";
  530. LineLen = 0;
  531. }
  532. LineLen -= Expansion.size();
  533. // If the tokens were already space separated, or if they must be to avoid
  534. // them being implicitly pasted, add a space between them.
  535. if (Tok.hasLeadingSpace() ||
  536. ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
  537. Expansion += ' ';
  538. // Escape any special characters in the token text.
  539. Expansion += EscapeText(TmpPP.getSpelling(Tok));
  540. LineLen += Expansion.size();
  541. PrevPrevTok = PrevTok;
  542. PrevTok = Tok;
  543. TmpPP.Lex(Tok);
  544. }
  545. // Insert the expansion as the end tag, so that multi-line macros all get
  546. // highlighted.
  547. Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
  548. HighlightRange(R, LLoc.getBegin(), LLoc.getEnd(), "<span class='macro'>",
  549. Expansion.c_str(), LLoc.isTokenRange());
  550. }
  551. // Restore the preprocessor's old state.
  552. TmpPP.setDiagnostics(*OldDiags);
  553. TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
  554. }