HTMLRewrite.cpp 20 KB

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