Coverage Report

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