Coverage Report

Created: 2020-09-15 12:33

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