Coverage Report

Created: 2018-09-19 20:53

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