1243791Sdim//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-// 2243791Sdim// 3243791Sdim// The LLVM Compiler Infrastructure 4243791Sdim// 5243791Sdim// This file is distributed under the University of Illinois Open Source 6243791Sdim// License. See LICENSE.TXT for details. 7243791Sdim// 8243791Sdim//===----------------------------------------------------------------------===// 9243791Sdim// 10243791Sdim// This file defines the HTMLRewriter clas, which is used to translate the 11243791Sdim// text of a source file into prettified HTML. 12243791Sdim// 13243791Sdim//===----------------------------------------------------------------------===// 14243791Sdim 15249423Sdim#include "clang/Rewrite/Core/HTMLRewrite.h" 16249423Sdim#include "clang/Basic/SourceManager.h" 17243791Sdim#include "clang/Lex/Preprocessor.h" 18249423Sdim#include "clang/Lex/TokenConcatenation.h" 19243791Sdim#include "clang/Rewrite/Core/Rewriter.h" 20249423Sdim#include "llvm/ADT/OwningPtr.h" 21243791Sdim#include "llvm/ADT/SmallString.h" 22243791Sdim#include "llvm/Support/ErrorHandling.h" 23243791Sdim#include "llvm/Support/MemoryBuffer.h" 24243791Sdim#include "llvm/Support/raw_ostream.h" 25243791Sdimusing namespace clang; 26243791Sdim 27243791Sdim 28243791Sdim/// HighlightRange - Highlight a range in the source code with the specified 29243791Sdim/// start/end tags. B/E must be in the same file. This ensures that 30243791Sdim/// start/end tags are placed at the start/end of each line if the range is 31243791Sdim/// multiline. 32243791Sdimvoid html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, 33243791Sdim const char *StartTag, const char *EndTag) { 34243791Sdim SourceManager &SM = R.getSourceMgr(); 35243791Sdim B = SM.getExpansionLoc(B); 36243791Sdim E = SM.getExpansionLoc(E); 37243791Sdim FileID FID = SM.getFileID(B); 38243791Sdim assert(SM.getFileID(E) == FID && "B/E not in the same file!"); 39243791Sdim 40243791Sdim unsigned BOffset = SM.getFileOffset(B); 41243791Sdim unsigned EOffset = SM.getFileOffset(E); 42243791Sdim 43243791Sdim // Include the whole end token in the range. 44243791Sdim EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts()); 45243791Sdim 46243791Sdim bool Invalid = false; 47243791Sdim const char *BufferStart = SM.getBufferData(FID, &Invalid).data(); 48243791Sdim if (Invalid) 49243791Sdim return; 50243791Sdim 51243791Sdim HighlightRange(R.getEditBuffer(FID), BOffset, EOffset, 52243791Sdim BufferStart, StartTag, EndTag); 53243791Sdim} 54243791Sdim 55243791Sdim/// HighlightRange - This is the same as the above method, but takes 56243791Sdim/// decomposed file locations. 57243791Sdimvoid html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, 58243791Sdim const char *BufferStart, 59243791Sdim const char *StartTag, const char *EndTag) { 60243791Sdim // Insert the tag at the absolute start/end of the range. 61243791Sdim RB.InsertTextAfter(B, StartTag); 62243791Sdim RB.InsertTextBefore(E, EndTag); 63243791Sdim 64243791Sdim // Scan the range to see if there is a \r or \n. If so, and if the line is 65243791Sdim // not blank, insert tags on that line as well. 66243791Sdim bool HadOpenTag = true; 67243791Sdim 68243791Sdim unsigned LastNonWhiteSpace = B; 69243791Sdim for (unsigned i = B; i != E; ++i) { 70243791Sdim switch (BufferStart[i]) { 71243791Sdim case '\r': 72243791Sdim case '\n': 73243791Sdim // Okay, we found a newline in the range. If we have an open tag, we need 74243791Sdim // to insert a close tag at the first non-whitespace before the newline. 75243791Sdim if (HadOpenTag) 76243791Sdim RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag); 77243791Sdim 78243791Sdim // Instead of inserting an open tag immediately after the newline, we 79243791Sdim // wait until we see a non-whitespace character. This prevents us from 80243791Sdim // inserting tags around blank lines, and also allows the open tag to 81243791Sdim // be put *after* whitespace on a non-blank line. 82243791Sdim HadOpenTag = false; 83243791Sdim break; 84243791Sdim case '\0': 85243791Sdim case ' ': 86243791Sdim case '\t': 87243791Sdim case '\f': 88243791Sdim case '\v': 89243791Sdim // Ignore whitespace. 90243791Sdim break; 91243791Sdim 92243791Sdim default: 93243791Sdim // If there is no tag open, do it now. 94243791Sdim if (!HadOpenTag) { 95243791Sdim RB.InsertTextAfter(i, StartTag); 96243791Sdim HadOpenTag = true; 97243791Sdim } 98243791Sdim 99243791Sdim // Remember this character. 100243791Sdim LastNonWhiteSpace = i; 101243791Sdim break; 102243791Sdim } 103243791Sdim } 104243791Sdim} 105243791Sdim 106243791Sdimvoid html::EscapeText(Rewriter &R, FileID FID, 107243791Sdim bool EscapeSpaces, bool ReplaceTabs) { 108243791Sdim 109243791Sdim const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID); 110243791Sdim const char* C = Buf->getBufferStart(); 111243791Sdim const char* FileEnd = Buf->getBufferEnd(); 112243791Sdim 113243791Sdim assert (C <= FileEnd); 114243791Sdim 115243791Sdim RewriteBuffer &RB = R.getEditBuffer(FID); 116243791Sdim 117243791Sdim unsigned ColNo = 0; 118243791Sdim for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { 119243791Sdim switch (*C) { 120243791Sdim default: ++ColNo; break; 121243791Sdim case '\n': 122243791Sdim case '\r': 123243791Sdim ColNo = 0; 124243791Sdim break; 125243791Sdim 126243791Sdim case ' ': 127243791Sdim if (EscapeSpaces) 128243791Sdim RB.ReplaceText(FilePos, 1, " "); 129243791Sdim ++ColNo; 130243791Sdim break; 131243791Sdim case '\f': 132243791Sdim RB.ReplaceText(FilePos, 1, "<hr>"); 133243791Sdim ColNo = 0; 134243791Sdim break; 135243791Sdim 136243791Sdim case '\t': { 137243791Sdim if (!ReplaceTabs) 138243791Sdim break; 139243791Sdim unsigned NumSpaces = 8-(ColNo&7); 140243791Sdim if (EscapeSpaces) 141243791Sdim RB.ReplaceText(FilePos, 1, 142243791Sdim StringRef(" " 143243791Sdim " ", 6*NumSpaces)); 144243791Sdim else 145243791Sdim RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces)); 146243791Sdim ColNo += NumSpaces; 147243791Sdim break; 148243791Sdim } 149243791Sdim case '<': 150243791Sdim RB.ReplaceText(FilePos, 1, "<"); 151243791Sdim ++ColNo; 152243791Sdim break; 153243791Sdim 154243791Sdim case '>': 155243791Sdim RB.ReplaceText(FilePos, 1, ">"); 156243791Sdim ++ColNo; 157243791Sdim break; 158243791Sdim 159243791Sdim case '&': 160243791Sdim RB.ReplaceText(FilePos, 1, "&"); 161243791Sdim ++ColNo; 162243791Sdim break; 163243791Sdim } 164243791Sdim } 165243791Sdim} 166243791Sdim 167263508Sdimstd::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) { 168243791Sdim 169243791Sdim unsigned len = s.size(); 170243791Sdim std::string Str; 171243791Sdim llvm::raw_string_ostream os(Str); 172243791Sdim 173243791Sdim for (unsigned i = 0 ; i < len; ++i) { 174243791Sdim 175243791Sdim char c = s[i]; 176243791Sdim switch (c) { 177243791Sdim default: 178243791Sdim os << c; break; 179243791Sdim 180243791Sdim case ' ': 181243791Sdim if (EscapeSpaces) os << " "; 182243791Sdim else os << ' '; 183243791Sdim break; 184243791Sdim 185243791Sdim case '\t': 186243791Sdim if (ReplaceTabs) { 187243791Sdim if (EscapeSpaces) 188243791Sdim for (unsigned i = 0; i < 4; ++i) 189243791Sdim os << " "; 190243791Sdim else 191243791Sdim for (unsigned i = 0; i < 4; ++i) 192243791Sdim os << " "; 193243791Sdim } 194243791Sdim else 195243791Sdim os << c; 196243791Sdim 197243791Sdim break; 198243791Sdim 199243791Sdim case '<': os << "<"; break; 200243791Sdim case '>': os << ">"; break; 201243791Sdim case '&': os << "&"; break; 202243791Sdim } 203243791Sdim } 204243791Sdim 205243791Sdim return os.str(); 206243791Sdim} 207243791Sdim 208243791Sdimstatic void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, 209243791Sdim unsigned B, unsigned E) { 210243791Sdim SmallString<256> Str; 211243791Sdim llvm::raw_svector_ostream OS(Str); 212243791Sdim 213243791Sdim OS << "<tr><td class=\"num\" id=\"LN" 214243791Sdim << LineNo << "\">" 215243791Sdim << LineNo << "</td><td class=\"line\">"; 216243791Sdim 217243791Sdim if (B == E) { // Handle empty lines. 218243791Sdim OS << " </td></tr>"; 219243791Sdim RB.InsertTextBefore(B, OS.str()); 220243791Sdim } else { 221243791Sdim RB.InsertTextBefore(B, OS.str()); 222243791Sdim RB.InsertTextBefore(E, "</td></tr>"); 223243791Sdim } 224243791Sdim} 225243791Sdim 226243791Sdimvoid html::AddLineNumbers(Rewriter& R, FileID FID) { 227243791Sdim 228243791Sdim const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID); 229243791Sdim const char* FileBeg = Buf->getBufferStart(); 230243791Sdim const char* FileEnd = Buf->getBufferEnd(); 231243791Sdim const char* C = FileBeg; 232243791Sdim RewriteBuffer &RB = R.getEditBuffer(FID); 233243791Sdim 234243791Sdim assert (C <= FileEnd); 235243791Sdim 236243791Sdim unsigned LineNo = 0; 237243791Sdim unsigned FilePos = 0; 238243791Sdim 239243791Sdim while (C != FileEnd) { 240243791Sdim 241243791Sdim ++LineNo; 242243791Sdim unsigned LineStartPos = FilePos; 243243791Sdim unsigned LineEndPos = FileEnd - FileBeg; 244243791Sdim 245243791Sdim assert (FilePos <= LineEndPos); 246243791Sdim assert (C < FileEnd); 247243791Sdim 248243791Sdim // Scan until the newline (or end-of-file). 249243791Sdim 250243791Sdim while (C != FileEnd) { 251243791Sdim char c = *C; 252243791Sdim ++C; 253243791Sdim 254243791Sdim if (c == '\n') { 255243791Sdim LineEndPos = FilePos++; 256243791Sdim break; 257243791Sdim } 258243791Sdim 259243791Sdim ++FilePos; 260243791Sdim } 261243791Sdim 262243791Sdim AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); 263243791Sdim } 264243791Sdim 265243791Sdim // Add one big table tag that surrounds all of the code. 266243791Sdim RB.InsertTextBefore(0, "<table class=\"code\">\n"); 267243791Sdim RB.InsertTextAfter(FileEnd - FileBeg, "</table>"); 268243791Sdim} 269243791Sdim 270243791Sdimvoid html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, 271243791Sdim const char *title) { 272243791Sdim 273243791Sdim const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID); 274243791Sdim const char* FileStart = Buf->getBufferStart(); 275243791Sdim const char* FileEnd = Buf->getBufferEnd(); 276243791Sdim 277243791Sdim SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID); 278243791Sdim SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart); 279243791Sdim 280243791Sdim std::string s; 281243791Sdim llvm::raw_string_ostream os(s); 282243791Sdim os << "<!doctype html>\n" // Use HTML 5 doctype 283243791Sdim "<html>\n<head>\n"; 284243791Sdim 285243791Sdim if (title) 286243791Sdim os << "<title>" << html::EscapeText(title) << "</title>\n"; 287243791Sdim 288243791Sdim os << "<style type=\"text/css\">\n" 289243791Sdim " body { color:#000000; background-color:#ffffff }\n" 290243791Sdim " body { font-family:Helvetica, sans-serif; font-size:10pt }\n" 291243791Sdim " h1 { font-size:14pt }\n" 292243791Sdim " .code { border-collapse:collapse; width:100%; }\n" 293243791Sdim " .code { font-family: \"Monospace\", monospace; font-size:10pt }\n" 294243791Sdim " .code { line-height: 1.2em }\n" 295243791Sdim " .comment { color: green; font-style: oblique }\n" 296243791Sdim " .keyword { color: blue }\n" 297243791Sdim " .string_literal { color: red }\n" 298243791Sdim " .directive { color: darkmagenta }\n" 299243791Sdim // Macro expansions. 300243791Sdim " .expansion { display: none; }\n" 301243791Sdim " .macro:hover .expansion { display: block; border: 2px solid #FF0000; " 302243791Sdim "padding: 2px; background-color:#FFF0F0; font-weight: normal; " 303243791Sdim " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; " 304243791Sdim "position: absolute; top: -1em; left:10em; z-index: 1 } \n" 305243791Sdim " .macro { color: darkmagenta; background-color:LemonChiffon;" 306243791Sdim // Macros are position: relative to provide base for expansions. 307243791Sdim " position: relative }\n" 308243791Sdim " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n" 309243791Sdim " .num { text-align:right; font-size:8pt }\n" 310243791Sdim " .num { color:#444444 }\n" 311243791Sdim " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n" 312243791Sdim " .line { white-space: pre }\n" 313243791Sdim " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n" 314243791Sdim " .msg { -webkit-border-radius:5px }\n" 315243791Sdim " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n" 316243791Sdim " .msg { float:left }\n" 317243791Sdim " .msg { padding:0.25em 1ex 0.25em 1ex }\n" 318243791Sdim " .msg { margin-top:10px; margin-bottom:10px }\n" 319243791Sdim " .msg { font-weight:bold }\n" 320243791Sdim " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n" 321243791Sdim " .msgT { padding:0x; spacing:0x }\n" 322243791Sdim " .msgEvent { background-color:#fff8b4; color:#000000 }\n" 323243791Sdim " .msgControl { background-color:#bbbbbb; color:#000000 }\n" 324243791Sdim " .mrange { background-color:#dfddf3 }\n" 325243791Sdim " .mrange { border-bottom:1px solid #6F9DBE }\n" 326243791Sdim " .PathIndex { font-weight: bold; padding:0px 5px; " 327243791Sdim "margin-right:5px; }\n" 328243791Sdim " .PathIndex { -webkit-border-radius:8px }\n" 329243791Sdim " .PathIndexEvent { background-color:#bfba87 }\n" 330243791Sdim " .PathIndexControl { background-color:#8c8c8c }\n" 331243791Sdim " .PathNav a { text-decoration:none; font-size: larger }\n" 332243791Sdim " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n" 333243791Sdim " .CodeRemovalHint { background-color:#de1010 }\n" 334243791Sdim " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n" 335243791Sdim " table.simpletable {\n" 336243791Sdim " padding: 5px;\n" 337243791Sdim " font-size:12pt;\n" 338243791Sdim " margin:20px;\n" 339243791Sdim " border-collapse: collapse; border-spacing: 0px;\n" 340243791Sdim " }\n" 341243791Sdim " td.rowname {\n" 342243791Sdim " text-align:right; font-weight:bold; color:#444444;\n" 343243791Sdim " padding-right:2ex; }\n" 344243791Sdim "</style>\n</head>\n<body>"; 345243791Sdim 346243791Sdim // Generate header 347243791Sdim R.InsertTextBefore(StartLoc, os.str()); 348243791Sdim // Generate footer 349243791Sdim 350243791Sdim R.InsertTextAfter(EndLoc, "</body></html>\n"); 351243791Sdim} 352243791Sdim 353243791Sdim/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with 354243791Sdim/// information about keywords, macro expansions etc. This uses the macro 355243791Sdim/// table state from the end of the file, so it won't be perfectly perfect, 356243791Sdim/// but it will be reasonably close. 357243791Sdimvoid html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) { 358243791Sdim RewriteBuffer &RB = R.getEditBuffer(FID); 359243791Sdim 360243791Sdim const SourceManager &SM = PP.getSourceManager(); 361243791Sdim const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 362243791Sdim Lexer L(FID, FromFile, SM, PP.getLangOpts()); 363263508Sdim const char *BufferStart = L.getBuffer().data(); 364243791Sdim 365243791Sdim // Inform the preprocessor that we want to retain comments as tokens, so we 366243791Sdim // can highlight them. 367243791Sdim L.SetCommentRetentionState(true); 368243791Sdim 369243791Sdim // Lex all the tokens in raw mode, to avoid entering #includes or expanding 370243791Sdim // macros. 371243791Sdim Token Tok; 372243791Sdim L.LexFromRawLexer(Tok); 373243791Sdim 374243791Sdim while (Tok.isNot(tok::eof)) { 375243791Sdim // Since we are lexing unexpanded tokens, all tokens are from the main 376243791Sdim // FileID. 377243791Sdim unsigned TokOffs = SM.getFileOffset(Tok.getLocation()); 378243791Sdim unsigned TokLen = Tok.getLength(); 379243791Sdim switch (Tok.getKind()) { 380243791Sdim default: break; 381243791Sdim case tok::identifier: 382243791Sdim llvm_unreachable("tok::identifier in raw lexing mode!"); 383243791Sdim case tok::raw_identifier: { 384243791Sdim // Fill in Result.IdentifierInfo and update the token kind, 385243791Sdim // looking up the identifier in the identifier table. 386243791Sdim PP.LookUpIdentifierInfo(Tok); 387243791Sdim 388243791Sdim // If this is a pp-identifier, for a keyword, highlight it as such. 389243791Sdim if (Tok.isNot(tok::identifier)) 390243791Sdim HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, 391243791Sdim "<span class='keyword'>", "</span>"); 392243791Sdim break; 393243791Sdim } 394243791Sdim case tok::comment: 395243791Sdim HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, 396243791Sdim "<span class='comment'>", "</span>"); 397243791Sdim break; 398243791Sdim case tok::utf8_string_literal: 399243791Sdim // Chop off the u part of u8 prefix 400243791Sdim ++TokOffs; 401243791Sdim --TokLen; 402243791Sdim // FALL THROUGH to chop the 8 403243791Sdim case tok::wide_string_literal: 404243791Sdim case tok::utf16_string_literal: 405243791Sdim case tok::utf32_string_literal: 406243791Sdim // Chop off the L, u, U or 8 prefix 407243791Sdim ++TokOffs; 408243791Sdim --TokLen; 409243791Sdim // FALL THROUGH. 410243791Sdim case tok::string_literal: 411243791Sdim // FIXME: Exclude the optional ud-suffix from the highlighted range. 412243791Sdim HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, 413243791Sdim "<span class='string_literal'>", "</span>"); 414243791Sdim break; 415243791Sdim case tok::hash: { 416243791Sdim // If this is a preprocessor directive, all tokens to end of line are too. 417243791Sdim if (!Tok.isAtStartOfLine()) 418243791Sdim break; 419243791Sdim 420243791Sdim // Eat all of the tokens until we get to the next one at the start of 421243791Sdim // line. 422243791Sdim unsigned TokEnd = TokOffs+TokLen; 423243791Sdim L.LexFromRawLexer(Tok); 424243791Sdim while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) { 425243791Sdim TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength(); 426243791Sdim L.LexFromRawLexer(Tok); 427243791Sdim } 428243791Sdim 429243791Sdim // Find end of line. This is a hack. 430243791Sdim HighlightRange(RB, TokOffs, TokEnd, BufferStart, 431243791Sdim "<span class='directive'>", "</span>"); 432243791Sdim 433243791Sdim // Don't skip the next token. 434243791Sdim continue; 435243791Sdim } 436243791Sdim } 437243791Sdim 438243791Sdim L.LexFromRawLexer(Tok); 439243791Sdim } 440243791Sdim} 441243791Sdim 442243791Sdim/// HighlightMacros - This uses the macro table state from the end of the 443243791Sdim/// file, to re-expand macros and insert (into the HTML) information about the 444243791Sdim/// macro expansions. This won't be perfectly perfect, but it will be 445243791Sdim/// reasonably close. 446243791Sdimvoid html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) { 447243791Sdim // Re-lex the raw token stream into a token buffer. 448243791Sdim const SourceManager &SM = PP.getSourceManager(); 449243791Sdim std::vector<Token> TokenStream; 450243791Sdim 451243791Sdim const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 452243791Sdim Lexer L(FID, FromFile, SM, PP.getLangOpts()); 453243791Sdim 454243791Sdim // Lex all the tokens in raw mode, to avoid entering #includes or expanding 455243791Sdim // macros. 456243791Sdim while (1) { 457243791Sdim Token Tok; 458243791Sdim L.LexFromRawLexer(Tok); 459243791Sdim 460243791Sdim // If this is a # at the start of a line, discard it from the token stream. 461243791Sdim // We don't want the re-preprocess step to see #defines, #includes or other 462243791Sdim // preprocessor directives. 463243791Sdim if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) 464243791Sdim continue; 465243791Sdim 466243791Sdim // If this is a ## token, change its kind to unknown so that repreprocessing 467243791Sdim // it will not produce an error. 468243791Sdim if (Tok.is(tok::hashhash)) 469243791Sdim Tok.setKind(tok::unknown); 470243791Sdim 471243791Sdim // If this raw token is an identifier, the raw lexer won't have looked up 472243791Sdim // the corresponding identifier info for it. Do this now so that it will be 473243791Sdim // macro expanded when we re-preprocess it. 474243791Sdim if (Tok.is(tok::raw_identifier)) 475243791Sdim PP.LookUpIdentifierInfo(Tok); 476243791Sdim 477243791Sdim TokenStream.push_back(Tok); 478243791Sdim 479243791Sdim if (Tok.is(tok::eof)) break; 480243791Sdim } 481243791Sdim 482243791Sdim // Temporarily change the diagnostics object so that we ignore any generated 483243791Sdim // diagnostics from this pass. 484243791Sdim DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(), 485243791Sdim &PP.getDiagnostics().getDiagnosticOptions(), 486243791Sdim new IgnoringDiagConsumer); 487243791Sdim 488243791Sdim // FIXME: This is a huge hack; we reuse the input preprocessor because we want 489243791Sdim // its state, but we aren't actually changing it (we hope). This should really 490243791Sdim // construct a copy of the preprocessor. 491243791Sdim Preprocessor &TmpPP = const_cast<Preprocessor&>(PP); 492243791Sdim DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics(); 493243791Sdim TmpPP.setDiagnostics(TmpDiags); 494243791Sdim 495243791Sdim // Inform the preprocessor that we don't want comments. 496243791Sdim TmpPP.SetCommentRetentionState(false, false); 497243791Sdim 498243791Sdim // We don't want pragmas either. Although we filtered out #pragma, removing 499243791Sdim // _Pragma and __pragma is much harder. 500243791Sdim bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled(); 501243791Sdim TmpPP.setPragmasEnabled(false); 502243791Sdim 503243791Sdim // Enter the tokens we just lexed. This will cause them to be macro expanded 504243791Sdim // but won't enter sub-files (because we removed #'s). 505243791Sdim TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false); 506243791Sdim 507243791Sdim TokenConcatenation ConcatInfo(TmpPP); 508243791Sdim 509243791Sdim // Lex all the tokens. 510243791Sdim Token Tok; 511243791Sdim TmpPP.Lex(Tok); 512243791Sdim while (Tok.isNot(tok::eof)) { 513243791Sdim // Ignore non-macro tokens. 514243791Sdim if (!Tok.getLocation().isMacroID()) { 515243791Sdim TmpPP.Lex(Tok); 516243791Sdim continue; 517243791Sdim } 518243791Sdim 519243791Sdim // Okay, we have the first token of a macro expansion: highlight the 520243791Sdim // expansion by inserting a start tag before the macro expansion and 521243791Sdim // end tag after it. 522243791Sdim std::pair<SourceLocation, SourceLocation> LLoc = 523243791Sdim SM.getExpansionRange(Tok.getLocation()); 524243791Sdim 525243791Sdim // Ignore tokens whose instantiation location was not the main file. 526243791Sdim if (SM.getFileID(LLoc.first) != FID) { 527243791Sdim TmpPP.Lex(Tok); 528243791Sdim continue; 529243791Sdim } 530243791Sdim 531243791Sdim assert(SM.getFileID(LLoc.second) == FID && 532243791Sdim "Start and end of expansion must be in the same ultimate file!"); 533243791Sdim 534243791Sdim std::string Expansion = EscapeText(TmpPP.getSpelling(Tok)); 535243791Sdim unsigned LineLen = Expansion.size(); 536243791Sdim 537243791Sdim Token PrevPrevTok; 538243791Sdim Token PrevTok = Tok; 539243791Sdim // Okay, eat this token, getting the next one. 540243791Sdim TmpPP.Lex(Tok); 541243791Sdim 542243791Sdim // Skip all the rest of the tokens that are part of this macro 543243791Sdim // instantiation. It would be really nice to pop up a window with all the 544243791Sdim // spelling of the tokens or something. 545243791Sdim while (!Tok.is(tok::eof) && 546243791Sdim SM.getExpansionLoc(Tok.getLocation()) == LLoc.first) { 547243791Sdim // Insert a newline if the macro expansion is getting large. 548243791Sdim if (LineLen > 60) { 549243791Sdim Expansion += "<br>"; 550243791Sdim LineLen = 0; 551243791Sdim } 552243791Sdim 553243791Sdim LineLen -= Expansion.size(); 554243791Sdim 555243791Sdim // If the tokens were already space separated, or if they must be to avoid 556243791Sdim // them being implicitly pasted, add a space between them. 557243791Sdim if (Tok.hasLeadingSpace() || 558243791Sdim ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok)) 559243791Sdim Expansion += ' '; 560243791Sdim 561243791Sdim // Escape any special characters in the token text. 562243791Sdim Expansion += EscapeText(TmpPP.getSpelling(Tok)); 563243791Sdim LineLen += Expansion.size(); 564243791Sdim 565243791Sdim PrevPrevTok = PrevTok; 566243791Sdim PrevTok = Tok; 567243791Sdim TmpPP.Lex(Tok); 568243791Sdim } 569243791Sdim 570243791Sdim 571243791Sdim // Insert the expansion as the end tag, so that multi-line macros all get 572243791Sdim // highlighted. 573243791Sdim Expansion = "<span class='expansion'>" + Expansion + "</span></span>"; 574243791Sdim 575243791Sdim HighlightRange(R, LLoc.first, LLoc.second, 576243791Sdim "<span class='macro'>", Expansion.c_str()); 577243791Sdim } 578243791Sdim 579243791Sdim // Restore the preprocessor's old state. 580243791Sdim TmpPP.setDiagnostics(*OldDiags); 581243791Sdim TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled); 582243791Sdim} 583