Coverage Report

Created: 2019-07-24 05:18

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