Coverage Report

Created: 2018-11-16 02:38

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