Coverage Report

Created: 2019-02-20 07:29

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