Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- 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 PlistDiagnostics object.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/FileManager.h"
14
#include "clang/Basic/PlistSupport.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Basic/Version.h"
17
#include "clang/CrossTU/CrossTranslationUnit.h"
18
#include "clang/Lex/Preprocessor.h"
19
#include "clang/Lex/TokenConcatenation.h"
20
#include "clang/Rewrite/Core/HTMLRewrite.h"
21
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
22
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
23
#include "clang/StaticAnalyzer/Core/IssueHash.h"
24
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
25
#include "llvm/ADT/SmallPtrSet.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/ADT/Statistic.h"
28
#include "llvm/Support/Casting.h"
29
30
using namespace clang;
31
using namespace ento;
32
using namespace markup;
33
34
//===----------------------------------------------------------------------===//
35
// Declarations of helper classes and functions for emitting bug reports in
36
// plist format.
37
//===----------------------------------------------------------------------===//
38
39
namespace {
40
  class PlistDiagnostics : public PathDiagnosticConsumer {
41
    const std::string OutputFile;
42
    const Preprocessor &PP;
43
    const cross_tu::CrossTranslationUnitContext &CTU;
44
    AnalyzerOptions &AnOpts;
45
    const bool SupportsCrossFileDiagnostics;
46
  public:
47
    PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &prefix,
48
                     const Preprocessor &PP,
49
                     const cross_tu::CrossTranslationUnitContext &CTU,
50
                     bool supportsMultipleFiles);
51
52
53
    ~PlistDiagnostics() override {}
53
54
    void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
55
                              FilesMade *filesMade) override;
56
57
0
    StringRef getName() const override {
58
0
      return "PlistDiagnostics";
59
0
    }
60
61
581
    PathGenerationScheme getGenerationScheme() const override {
62
581
      return Extensive;
63
581
    }
64
0
    bool supportsLogicalOpControlFlow() const override { return true; }
65
596
    bool supportsCrossFileDiagnostics() const override {
66
596
      return SupportsCrossFileDiagnostics;
67
596
    }
68
  };
69
} // end anonymous namespace
70
71
namespace {
72
73
/// A helper class for emitting a single report.
74
class PlistPrinter {
75
  const FIDMap& FM;
76
  AnalyzerOptions &AnOpts;
77
  const Preprocessor &PP;
78
  llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;
79
80
public:
81
  PlistPrinter(const FIDMap& FM, AnalyzerOptions &AnOpts,
82
               const Preprocessor &PP)
83
590
    : FM(FM), AnOpts(AnOpts), PP(PP) {
84
590
  }
85
86
3.37k
  void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
87
3.37k
    ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
88
3.37k
89
3.37k
    // Don't emit a warning about an unused private field.
90
3.37k
    (void)AnOpts;
91
3.37k
  }
92
93
  /// Print the expansions of the collected macro pieces.
94
  ///
95
  /// Each time ReportDiag is called on a PathDiagnosticMacroPiece (or, if one
96
  /// is found through a call piece, etc), it's subpieces are reported, and the
97
  /// piece itself is collected. Call this function after the entire bugpath
98
  /// was reported.
99
  void ReportMacroExpansions(raw_ostream &o, unsigned indent);
100
101
private:
102
  void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P,
103
                   unsigned indent, unsigned depth, bool includeControlFlow,
104
4.26k
                   bool isKeyEvent = false) {
105
4.26k
    switch (P.getKind()) {
106
4.26k
      case PathDiagnosticPiece::ControlFlow:
107
2.14k
        if (includeControlFlow)
108
2.11k
          ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
109
2.14k
        break;
110
4.26k
      case PathDiagnosticPiece::Call:
111
142
        ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
112
142
                   depth);
113
142
        break;
114
4.26k
      case PathDiagnosticPiece::Event:
115
1.90k
        ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
116
1.90k
                    isKeyEvent);
117
1.90k
        break;
118
4.26k
      case PathDiagnosticPiece::Macro:
119
31
        ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
120
31
                             depth);
121
31
        break;
122
4.26k
      case PathDiagnosticPiece::Note:
123
1
        ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
124
1
        break;
125
4.26k
      case PathDiagnosticPiece::PopUp:
126
33
        ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
127
33
        break;
128
4.26k
    }
129
4.26k
  }
130
131
  void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
132
                  unsigned indent);
133
  void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
134
135
  void ReportControlFlow(raw_ostream &o,
136
                         const PathDiagnosticControlFlowPiece& P,
137
                         unsigned indent);
138
  void ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
139
                   unsigned indent, unsigned depth, bool isKeyEvent = false);
140
  void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
141
                  unsigned indent, unsigned depth);
142
  void ReportMacroSubPieces(raw_ostream &o, const PathDiagnosticMacroPiece& P,
143
                            unsigned indent, unsigned depth);
144
  void ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
145
                  unsigned indent);
146
147
  void ReportPopUp(raw_ostream &o, const PathDiagnosticPopUpPiece &P,
148
                   unsigned indent);
149
};
150
151
} // end of anonymous namespace
152
153
namespace {
154
155
struct ExpansionInfo {
156
  std::string MacroName;
157
  std::string Expansion;
158
  ExpansionInfo(std::string N, std::string E)
159
31
    : MacroName(std::move(N)), Expansion(std::move(E)) {}
160
};
161
162
} // end of anonymous namespace
163
164
static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
165
                         AnalyzerOptions &AnOpts,
166
                         const Preprocessor &PP,
167
                         const PathPieces &Path);
168
169
/// Print coverage information to output stream {@code o}.
170
/// May modify the used list of files {@code Fids} by inserting new ones.
171
static void printCoverage(const PathDiagnostic *D,
172
                          unsigned InputIndentLevel,
173
                          SmallVectorImpl<FileID> &Fids,
174
                          FIDMap &FM,
175
                          llvm::raw_fd_ostream &o);
176
177
static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc,
178
                                      const Preprocessor &PP);
179
180
//===----------------------------------------------------------------------===//
181
// Methods of PlistPrinter.
182
//===----------------------------------------------------------------------===//
183
184
void PlistPrinter::EmitRanges(raw_ostream &o,
185
                              const ArrayRef<SourceRange> Ranges,
186
1.97k
                              unsigned indent) {
187
1.97k
188
1.97k
  if (Ranges.empty())
189
272
    return;
190
1.69k
191
1.69k
  Indent(o, indent) << "<key>ranges</key>\n";
192
1.69k
  Indent(o, indent) << "<array>\n";
193
1.69k
  ++indent;
194
1.69k
195
1.69k
  const SourceManager &SM = PP.getSourceManager();
196
1.69k
  const LangOptions &LangOpts = PP.getLangOpts();
197
1.69k
198
1.69k
  for (auto &R : Ranges)
199
1.82k
    EmitRange(o, SM,
200
1.82k
              Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
201
1.82k
              FM, indent + 1);
202
1.69k
  --indent;
203
1.69k
  Indent(o, indent) << "</array>\n";
204
1.69k
}
205
206
void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
207
1.93k
                               unsigned indent) {
208
1.93k
  // Output the text.
209
1.93k
  assert(!Message.empty());
210
1.93k
  Indent(o, indent) << "<key>extended_message</key>\n";
211
1.93k
  Indent(o, indent);
212
1.93k
  EmitString(o, Message) << '\n';
213
1.93k
214
1.93k
  // Output the short text.
215
1.93k
  // FIXME: Really use a short string.
216
1.93k
  Indent(o, indent) << "<key>message</key>\n";
217
1.93k
  Indent(o, indent);
218
1.93k
  EmitString(o, Message) << '\n';
219
1.93k
}
220
221
void PlistPrinter::ReportControlFlow(raw_ostream &o,
222
                                     const PathDiagnosticControlFlowPiece& P,
223
2.11k
                                     unsigned indent) {
224
2.11k
225
2.11k
  const SourceManager &SM = PP.getSourceManager();
226
2.11k
  const LangOptions &LangOpts = PP.getLangOpts();
227
2.11k
228
2.11k
  Indent(o, indent) << "<dict>\n";
229
2.11k
  ++indent;
230
2.11k
231
2.11k
  Indent(o, indent) << "<key>kind</key><string>control</string>\n";
232
2.11k
233
2.11k
  // Emit edges.
234
2.11k
  Indent(o, indent) << "<key>edges</key>\n";
235
2.11k
  ++indent;
236
2.11k
  Indent(o, indent) << "<array>\n";
237
2.11k
  ++indent;
238
2.11k
  for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
239
4.22k
       I!=E; 
++I2.11k
) {
240
2.11k
    Indent(o, indent) << "<dict>\n";
241
2.11k
    ++indent;
242
2.11k
243
2.11k
    // Make the ranges of the start and end point self-consistent with adjacent edges
244
2.11k
    // by forcing to use only the beginning of the range.  This simplifies the layout
245
2.11k
    // logic for clients.
246
2.11k
    Indent(o, indent) << "<key>start</key>\n";
247
2.11k
    SourceRange StartEdge(
248
2.11k
        SM.getExpansionLoc(I->getStart().asRange().getBegin()));
249
2.11k
    EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
250
2.11k
              indent + 1);
251
2.11k
252
2.11k
    Indent(o, indent) << "<key>end</key>\n";
253
2.11k
    SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
254
2.11k
    EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
255
2.11k
              indent + 1);
256
2.11k
257
2.11k
    --indent;
258
2.11k
    Indent(o, indent) << "</dict>\n";
259
2.11k
  }
260
2.11k
  --indent;
261
2.11k
  Indent(o, indent) << "</array>\n";
262
2.11k
  --indent;
263
2.11k
264
2.11k
  // Output any helper text.
265
2.11k
  const auto &s = P.getString();
266
2.11k
  if (!s.empty()) {
267
0
    Indent(o, indent) << "<key>alternate</key>";
268
0
    EmitString(o, s) << '\n';
269
0
  }
270
2.11k
271
2.11k
  --indent;
272
2.11k
  Indent(o, indent) << "</dict>\n";
273
2.11k
}
274
275
void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
276
                               unsigned indent, unsigned depth,
277
1.90k
                               bool isKeyEvent) {
278
1.90k
279
1.90k
  const SourceManager &SM = PP.getSourceManager();
280
1.90k
281
1.90k
  Indent(o, indent) << "<dict>\n";
282
1.90k
  ++indent;
283
1.90k
284
1.90k
  Indent(o, indent) << "<key>kind</key><string>event</string>\n";
285
1.90k
286
1.90k
  if (isKeyEvent) {
287
2
    Indent(o, indent) << "<key>key_event</key><true/>\n";
288
2
  }
289
1.90k
290
1.90k
  // Output the location.
291
1.90k
  FullSourceLoc L = P.getLocation().asLocation();
292
1.90k
293
1.90k
  Indent(o, indent) << "<key>location</key>\n";
294
1.90k
  EmitLocation(o, SM, L, FM, indent);
295
1.90k
296
1.90k
  // Output the ranges (if any).
297
1.90k
  ArrayRef<SourceRange> Ranges = P.getRanges();
298
1.90k
  EmitRanges(o, Ranges, indent);
299
1.90k
300
1.90k
  // Output the call depth.
301
1.90k
  Indent(o, indent) << "<key>depth</key>";
302
1.90k
  EmitInteger(o, depth) << '\n';
303
1.90k
304
1.90k
  // Output the text.
305
1.90k
  EmitMessage(o, P.getString(), indent);
306
1.90k
307
1.90k
  // Finish up.
308
1.90k
  --indent;
309
1.90k
  Indent(o, indent); o << "</dict>\n";
310
1.90k
}
311
312
void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
313
                              unsigned indent,
314
142
                              unsigned depth) {
315
142
316
142
  if (auto callEnter = P.getCallEnterEvent())
317
141
    ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
318
141
                P.isLastInMainSourceFile());
319
142
320
142
321
142
  ++depth;
322
142
323
142
  if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
324
130
    ReportPiece(o, *callEnterWithinCaller, indent, depth,
325
130
                /*includeControlFlow*/ true);
326
142
327
620
  for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;
++I478
)
328
478
    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
329
142
330
142
  --depth;
331
142
332
142
  if (auto callExit = P.getCallExitEvent())
333
69
    ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
334
142
}
335
336
void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
337
                                        const PathDiagnosticMacroPiece& P,
338
31
                                        unsigned indent, unsigned depth) {
339
31
  MacroPieces.push_back(&P);
340
31
341
31
  for (PathPieces::const_iterator I = P.subPieces.begin(),
342
31
                                  E = P.subPieces.end();
343
102
       I != E; 
++I71
) {
344
71
    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
345
71
  }
346
31
}
347
348
29
void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
349
29
350
31
  for (const PathDiagnosticMacroPiece *P : MacroPieces) {
351
31
    const SourceManager &SM = PP.getSourceManager();
352
31
    ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP);
353
31
354
31
    Indent(o, indent) << "<dict>\n";
355
31
    ++indent;
356
31
357
31
    // Output the location.
358
31
    FullSourceLoc L = P->getLocation().asLocation();
359
31
360
31
    Indent(o, indent) << "<key>location</key>\n";
361
31
    EmitLocation(o, SM, L, FM, indent);
362
31
363
31
    // Output the ranges (if any).
364
31
    ArrayRef<SourceRange> Ranges = P->getRanges();
365
31
    EmitRanges(o, Ranges, indent);
366
31
367
31
    // Output the macro name.
368
31
    Indent(o, indent) << "<key>name</key>";
369
31
    EmitString(o, EI.MacroName) << '\n';
370
31
371
31
    // Output what it expands into.
372
31
    Indent(o, indent) << "<key>expansion</key>";
373
31
    EmitString(o, EI.Expansion) << '\n';
374
31
375
31
    // Finish up.
376
31
    --indent;
377
31
    Indent(o, indent);
378
31
    o << "</dict>\n";
379
31
  }
380
29
}
381
382
void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
383
1
                              unsigned indent) {
384
1
385
1
  const SourceManager &SM = PP.getSourceManager();
386
1
387
1
  Indent(o, indent) << "<dict>\n";
388
1
  ++indent;
389
1
390
1
  // Output the location.
391
1
  FullSourceLoc L = P.getLocation().asLocation();
392
1
393
1
  Indent(o, indent) << "<key>location</key>\n";
394
1
  EmitLocation(o, SM, L, FM, indent);
395
1
396
1
  // Output the ranges (if any).
397
1
  ArrayRef<SourceRange> Ranges = P.getRanges();
398
1
  EmitRanges(o, Ranges, indent);
399
1
400
1
  // Output the text.
401
1
  EmitMessage(o, P.getString(), indent);
402
1
403
1
  // Finish up.
404
1
  --indent;
405
1
  Indent(o, indent); o << "</dict>\n";
406
1
}
407
408
void PlistPrinter::ReportPopUp(raw_ostream &o,
409
                               const PathDiagnosticPopUpPiece &P,
410
33
                               unsigned indent) {
411
33
  const SourceManager &SM = PP.getSourceManager();
412
33
413
33
  Indent(o, indent) << "<dict>\n";
414
33
  ++indent;
415
33
416
33
  Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";
417
33
418
33
  // Output the location.
419
33
  FullSourceLoc L = P.getLocation().asLocation();
420
33
421
33
  Indent(o, indent) << "<key>location</key>\n";
422
33
  EmitLocation(o, SM, L, FM, indent);
423
33
424
33
  // Output the ranges (if any).
425
33
  ArrayRef<SourceRange> Ranges = P.getRanges();
426
33
  EmitRanges(o, Ranges, indent);
427
33
428
33
  // Output the text.
429
33
  EmitMessage(o, P.getString(), indent);
430
33
431
33
  // Finish up.
432
33
  --indent;
433
33
  Indent(o, indent) << "</dict>\n";
434
33
}
435
436
//===----------------------------------------------------------------------===//
437
// Static function definitions.
438
//===----------------------------------------------------------------------===//
439
440
/// Print coverage information to output stream {@code o}.
441
/// May modify the used list of files {@code Fids} by inserting new ones.
442
static void printCoverage(const PathDiagnostic *D,
443
                          unsigned InputIndentLevel,
444
                          SmallVectorImpl<FileID> &Fids,
445
                          FIDMap &FM,
446
590
                          llvm::raw_fd_ostream &o) {
447
590
  unsigned IndentLevel = InputIndentLevel;
448
590
449
590
  Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
450
590
  Indent(o, IndentLevel) << "<dict>\n";
451
590
  IndentLevel++;
452
590
453
590
  // Mapping from file IDs to executed lines.
454
590
  const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
455
1.19k
  for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; 
++I600
) {
456
600
    unsigned FileKey = AddFID(FM, Fids, I->first);
457
600
    Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
458
600
    Indent(o, IndentLevel) << "<array>\n";
459
600
    IndentLevel++;
460
3.23k
    for (unsigned LineNo : I->second) {
461
3.23k
      Indent(o, IndentLevel);
462
3.23k
      EmitInteger(o, LineNo) << "\n";
463
3.23k
    }
464
600
    IndentLevel--;
465
600
    Indent(o, IndentLevel) << "</array>\n";
466
600
  }
467
590
  IndentLevel--;
468
590
  Indent(o, IndentLevel) << "</dict>\n";
469
590
470
590
  assert(IndentLevel == InputIndentLevel);
471
590
}
472
473
static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
474
                         AnalyzerOptions &AnOpts,
475
                         const Preprocessor &PP,
476
590
                         const PathPieces &Path) {
477
590
  PlistPrinter Printer(FM, AnOpts, PP);
478
590
  assert(std::is_partitioned(
479
590
           Path.begin(), Path.end(),
480
590
           [](const std::shared_ptr<PathDiagnosticPiece> &E)
481
590
             { return E->getKind() == PathDiagnosticPiece::Note; }) &&
482
590
         "PathDiagnostic is not partitioned so that notes precede the rest");
483
590
484
590
  PathPieces::const_iterator FirstNonNote = std::partition_point(
485
590
      Path.begin(), Path.end(),
486
590
      [](const std::shared_ptr<PathDiagnosticPiece> &E)
487
1.68k
        { return E->getKind() == PathDiagnosticPiece::Note; });
488
590
489
590
  PathPieces::const_iterator I = Path.begin();
490
590
491
590
  if (FirstNonNote != Path.begin()) {
492
1
    o << "   <key>notes</key>\n"
493
1
         "   <array>\n";
494
1
495
2
    for (; I != FirstNonNote; 
++I1
)
496
1
      Printer.ReportDiag(o, **I);
497
1
498
1
    o << "   </array>\n";
499
1
  }
500
590
501
590
  o << "   <key>path</key>\n";
502
590
503
590
  o << "   <array>\n";
504
590
505
3.96k
  for (PathPieces::const_iterator E = Path.end(); I != E; 
++I3.37k
)
506
3.37k
    Printer.ReportDiag(o, **I);
507
590
508
590
  o << "   </array>\n";
509
590
510
590
  if (!AnOpts.ShouldDisplayMacroExpansions)
511
561
    return;
512
29
513
29
  o << "   <key>macro_expansions</key>\n"
514
29
       "   <array>\n";
515
29
  Printer.ReportMacroExpansions(o, /* indent */ 4);
516
29
  o << "   </array>\n";
517
29
}
518
519
//===----------------------------------------------------------------------===//
520
// Methods of PlistDiagnostics.
521
//===----------------------------------------------------------------------===//
522
523
PlistDiagnostics::PlistDiagnostics(
524
    AnalyzerOptions &AnalyzerOpts, const std::string &output,
525
    const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU,
526
    bool supportsMultipleFiles)
527
    : OutputFile(output), PP(PP), CTU(CTU), AnOpts(AnalyzerOpts),
528
53
      SupportsCrossFileDiagnostics(supportsMultipleFiles) {
529
53
  // FIXME: Will be used by a later planned change.
530
53
  (void)this->CTU;
531
53
}
532
533
void ento::createPlistDiagnosticConsumer(
534
    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
535
    const std::string &s, const Preprocessor &PP,
536
33
    const cross_tu::CrossTranslationUnitContext &CTU) {
537
33
  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
538
33
                                   /*supportsMultipleFiles*/ false));
539
33
}
540
541
void ento::createPlistMultiFileDiagnosticConsumer(
542
    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
543
    const std::string &s, const Preprocessor &PP,
544
20
    const cross_tu::CrossTranslationUnitContext &CTU) {
545
20
  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
546
20
                                   /*supportsMultipleFiles*/ true));
547
20
}
548
void PlistDiagnostics::FlushDiagnosticsImpl(
549
                                    std::vector<const PathDiagnostic *> &Diags,
550
53
                                    FilesMade *filesMade) {
551
53
  // Build up a set of FIDs that we use by scanning the locations and
552
53
  // ranges of the diagnostics.
553
53
  FIDMap FM;
554
53
  SmallVector<FileID, 10> Fids;
555
53
  const SourceManager& SM = PP.getSourceManager();
556
53
  const LangOptions &LangOpts = PP.getLangOpts();
557
53
558
4.19k
  auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
559
4.19k
    AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
560
4.19k
    ArrayRef<SourceRange> Ranges = Piece.getRanges();
561
4.19k
    for (const SourceRange &Range : Ranges) {
562
1.75k
      AddFID(FM, Fids, SM, Range.getBegin());
563
1.75k
      AddFID(FM, Fids, SM, Range.getEnd());
564
1.75k
    }
565
4.19k
  };
566
53
567
590
  for (const PathDiagnostic *D : Diags) {
568
590
569
590
    SmallVector<const PathPieces *, 5> WorkList;
570
590
    WorkList.push_back(&D->path);
571
590
572
1.35k
    while (!WorkList.empty()) {
573
763
      const PathPieces &Path = *WorkList.pop_back_val();
574
763
575
3.92k
      for (const auto &Iter : Path) {
576
3.92k
        const PathDiagnosticPiece &Piece = *Iter;
577
3.92k
        AddPieceFID(Piece);
578
3.92k
579
3.92k
        if (const PathDiagnosticCallPiece *Call =
580
142
                dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
581
142
          if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
582
130
            AddPieceFID(*CallEnterWithin);
583
142
584
142
          if (auto CallEnterEvent = Call->getCallEnterEvent())
585
141
            AddPieceFID(*CallEnterEvent);
586
142
587
142
          WorkList.push_back(&Call->path);
588
3.77k
        } else if (const PathDiagnosticMacroPiece *Macro =
589
31
                       dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
590
31
          WorkList.push_back(&Macro->subPieces);
591
31
        }
592
3.92k
      }
593
763
    }
594
590
  }
595
53
596
53
  // Open the file.
597
53
  std::error_code EC;
598
53
  llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::F_Text);
599
53
  if (EC) {
600
0
    llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
601
0
    return;
602
0
  }
603
53
604
53
  EmitPlistHeader(o);
605
53
606
53
  // Write the root object: a <dict> containing...
607
53
  //  - "clang_version", the string representation of clang version
608
53
  //  - "files", an <array> mapping from FIDs to file names
609
53
  //  - "diagnostics", an <array> containing the path diagnostics
610
53
  o << "<dict>\n" <<
611
53
       " <key>clang_version</key>\n";
612
53
  EmitString(o, getClangFullVersion()) << '\n';
613
53
  o << " <key>diagnostics</key>\n"
614
53
       " <array>\n";
615
53
616
53
  for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
617
643
       DE = Diags.end(); DI!=DE; 
++DI590
) {
618
590
619
590
    o << "  <dict>\n";
620
590
621
590
    const PathDiagnostic *D = *DI;
622
590
    printBugPath(o, FM, AnOpts, PP, D->path);
623
590
624
590
    // Output the bug type and bug category.
625
590
    o << "   <key>description</key>";
626
590
    EmitString(o, D->getShortDescription()) << '\n';
627
590
    o << "   <key>category</key>";
628
590
    EmitString(o, D->getCategory()) << '\n';
629
590
    o << "   <key>type</key>";
630
590
    EmitString(o, D->getBugType()) << '\n';
631
590
    o << "   <key>check_name</key>";
632
590
    EmitString(o, D->getCheckName()) << '\n';
633
590
634
590
    o << "   <!-- This hash is experimental and going to change! -->\n";
635
590
    o << "   <key>issue_hash_content_of_line_in_context</key>";
636
590
    PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
637
590
    FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
638
590
                                            ? 
UPDLoc.asLocation()192
639
590
                                            : 
D->getLocation().asLocation()398
),
640
590
                    SM);
641
590
    const Decl *DeclWithIssue = D->getDeclWithIssue();
642
590
    EmitString(o, GetIssueHash(SM, L, D->getCheckName(), D->getBugType(),
643
590
                               DeclWithIssue, LangOpts))
644
590
        << '\n';
645
590
646
590
    // Output information about the semantic context where
647
590
    // the issue occurred.
648
590
    if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
649
588
      // FIXME: handle blocks, which have no name.
650
588
      if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
651
583
        StringRef declKind;
652
583
        switch (ND->getKind()) {
653
583
          case Decl::CXXRecord:
654
0
            declKind = "C++ class";
655
0
            break;
656
583
          case Decl::CXXMethod:
657
7
            declKind = "C++ method";
658
7
            break;
659
583
          case Decl::ObjCMethod:
660
46
            declKind = "Objective-C method";
661
46
            break;
662
583
          case Decl::Function:
663
519
            declKind = "function";
664
519
            break;
665
583
          default:
666
11
            break;
667
583
        }
668
583
        if (!declKind.empty()) {
669
572
          const std::string &declName = ND->getDeclName().getAsString();
670
572
          o << "  <key>issue_context_kind</key>";
671
572
          EmitString(o, declKind) << '\n';
672
572
          o << "  <key>issue_context</key>";
673
572
          EmitString(o, declName) << '\n';
674
572
        }
675
583
676
583
        // Output the bug hash for issue unique-ing. Currently, it's just an
677
583
        // offset from the beginning of the function.
678
583
        if (const Stmt *Body = DeclWithIssue->getBody()) {
679
583
680
583
          // If the bug uniqueing location exists, use it for the hash.
681
583
          // For example, this ensures that two leaks reported on the same line
682
583
          // will have different issue_hashes and that the hash will identify
683
583
          // the leak location even after code is added between the allocation
684
583
          // site and the end of scope (leak report location).
685
583
          if (UPDLoc.isValid()) {
686
191
            FullSourceLoc UFunL(
687
191
                SM.getExpansionLoc(
688
191
                    D->getUniqueingDecl()->getBody()->getBeginLoc()),
689
191
                SM);
690
191
            o << "  <key>issue_hash_function_offset</key><string>"
691
191
              << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
692
191
              << "</string>\n";
693
191
694
191
          // Otherwise, use the location on which the bug is reported.
695
392
          } else {
696
392
            FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
697
392
            o << "  <key>issue_hash_function_offset</key><string>"
698
392
              << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
699
392
              << "</string>\n";
700
392
          }
701
583
702
583
        }
703
583
      }
704
588
    }
705
590
706
590
    // Output the location of the bug.
707
590
    o << "  <key>location</key>\n";
708
590
    EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
709
590
710
590
    // Output the diagnostic to the sub-diagnostic client, if any.
711
590
    if (!filesMade->empty()) {
712
1
      StringRef lastName;
713
1
      PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
714
1
      if (files) {
715
1
        for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
716
2
                CE = files->end(); CI != CE; 
++CI1
) {
717
1
          StringRef newName = CI->first;
718
1
          if (newName != lastName) {
719
1
            if (!lastName.empty()) {
720
0
              o << "  </array>\n";
721
0
            }
722
1
            lastName = newName;
723
1
            o <<  "  <key>" << lastName << "_files</key>\n";
724
1
            o << "  <array>\n";
725
1
          }
726
1
          o << "   <string>" << CI->second << "</string>\n";
727
1
        }
728
1
        o << "  </array>\n";
729
1
      }
730
1
    }
731
590
732
590
    printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
733
590
734
590
    // Close up the entry.
735
590
    o << "  </dict>\n";
736
590
  }
737
53
738
53
  o << " </array>\n";
739
53
740
53
  o << " <key>files</key>\n"
741
53
       " <array>\n";
742
53
  for (FileID FID : Fids)
743
53
    EmitString(o << "  ", SM.getFileEntryForID(FID)->getName()) << '\n';
744
53
  o << " </array>\n";
745
53
746
53
  if (llvm::AreStatisticsEnabled() && 
AnOpts.ShouldSerializeStats0
) {
747
0
    o << " <key>statistics</key>\n";
748
0
    std::string stats;
749
0
    llvm::raw_string_ostream os(stats);
750
0
    llvm::PrintStatisticsJSON(os);
751
0
    os.flush();
752
0
    EmitString(o, html::EscapeText(stats)) << '\n';
753
0
  }
754
53
755
53
  // Finish.
756
53
  o << "</dict>\n</plist>\n";
757
53
}
758
759
//===----------------------------------------------------------------------===//
760
// Declarations of helper functions and data structures for expanding macros.
761
//===----------------------------------------------------------------------===//
762
763
namespace {
764
765
using ExpArgTokens = llvm::SmallVector<Token, 2>;
766
767
/// Maps unexpanded macro arguments to expanded arguments. A macro argument may
768
/// need to expanded further when it is nested inside another macro.
769
class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
770
public:
771
  void expandFromPrevMacro(const MacroArgMap &Super);
772
};
773
774
struct MacroNameAndArgs {
775
  std::string Name;
776
  const MacroInfo *MI = nullptr;
777
  MacroArgMap Args;
778
779
  MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
780
62
    : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
781
};
782
783
class TokenPrinter {
784
  llvm::raw_ostream &OS;
785
  const Preprocessor &PP;
786
787
  Token PrevTok, PrevPrevTok;
788
  TokenConcatenation ConcatInfo;
789
790
public:
791
  TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
792
31
    : OS(OS), PP(PP), ConcatInfo(PP) {
793
31
    PrevTok.setKind(tok::unknown);
794
31
    PrevPrevTok.setKind(tok::unknown);
795
31
  }
796
797
  void printToken(const Token &Tok);
798
};
799
800
} // end of anonymous namespace
801
802
/// The implementation method of getMacroExpansion: It prints the expansion of
803
/// a macro to \p Printer, and returns with the name of the macro.
804
///
805
/// Since macros can be nested in one another, this function may call itself
806
/// recursively.
807
///
808
/// Unfortunately, macro arguments have to expanded manually. To understand why,
809
/// observe the following example:
810
///
811
///   #define PRINT(x) print(x)
812
///   #define DO_SOMETHING(str) PRINT(str)
813
///
814
///   DO_SOMETHING("Cute panda cubs.");
815
///
816
/// As we expand the last line, we'll immediately replace PRINT(str) with
817
/// print(x). The information that both 'str' and 'x' refers to the same string
818
/// is an information we have to forward, hence the argument \p PrevArgs.
819
///
820
/// To avoid infinite recursion we maintain the already processed tokens in
821
/// a set. This is carried as a parameter through the recursive calls. The set
822
/// is extended with the currently processed token and after processing it, the
823
/// token is removed. If the token is already in the set, then recursion stops:
824
///
825
/// #define f(y) x
826
/// #define x f(x)
827
static std::string getMacroNameAndPrintExpansion(
828
    TokenPrinter &Printer,
829
    SourceLocation MacroLoc,
830
    const Preprocessor &PP,
831
    const MacroArgMap &PrevArgs,
832
    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);
833
834
/// Retrieves the name of the macro and what it's arguments expand into
835
/// at \p ExpanLoc.
836
///
837
/// For example, for the following macro expansion:
838
///
839
///   #define SET_TO_NULL(x) x = 0
840
///   #define NOT_SUSPICIOUS(a) \
841
///     {                       \
842
///       int b = 0;            \
843
///     }                       \
844
///     SET_TO_NULL(a)
845
///
846
///   int *ptr = new int(4);
847
///   NOT_SUSPICIOUS(&ptr);
848
///   *ptr = 5;
849
///
850
/// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
851
/// and the MacroArgMap map { (a, &ptr) } will be returned.
852
///
853
/// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
854
/// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
855
/// { (x, a) } will be returned.
856
static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
857
                                            const Preprocessor &PP);
858
859
/// Retrieves the ')' token that matches '(' \p It points to.
860
static MacroInfo::tokens_iterator getMatchingRParen(
861
    MacroInfo::tokens_iterator It,
862
    MacroInfo::tokens_iterator End);
863
864
/// Retrieves the macro info for \p II refers to at \p Loc. This is important
865
/// because macros can be redefined or undefined.
866
static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
867
                                                const SourceManager &SM,
868
                                                const IdentifierInfo *II,
869
                                                SourceLocation Loc);
870
871
//===----------------------------------------------------------------------===//
872
// Definitions of helper functions and methods for expanding macros.
873
//===----------------------------------------------------------------------===//
874
875
static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc,
876
31
                                      const Preprocessor &PP) {
877
31
878
31
  llvm::SmallString<200> ExpansionBuf;
879
31
  llvm::raw_svector_ostream OS(ExpansionBuf);
880
31
  TokenPrinter Printer(OS, PP);
881
31
  llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
882
31
883
31
  std::string MacroName =
884
31
            getMacroNameAndPrintExpansion(Printer, MacroLoc, PP, MacroArgMap{},
885
31
                                         AlreadyProcessedTokens);
886
31
  return { MacroName, OS.str() };
887
31
}
888
889
static std::string getMacroNameAndPrintExpansion(
890
    TokenPrinter &Printer,
891
    SourceLocation MacroLoc,
892
    const Preprocessor &PP,
893
    const MacroArgMap &PrevArgs,
894
62
    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
895
62
896
62
  const SourceManager &SM = PP.getSourceManager();
897
62
898
62
  MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
899
62
  IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
900
62
901
62
  // TODO: If the macro definition contains another symbol then this function is
902
62
  // called recursively. In case this symbol is the one being defined, it will
903
62
  // be an infinite recursion which is stopped by this "if" statement. However,
904
62
  // in this case we don't get the full expansion text in the Plist file. See
905
62
  // the test file where "value" is expanded to "garbage_" instead of
906
62
  // "garbage_value".
907
62
  if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
908
1
    return Info.Name;
909
61
  AlreadyProcessedTokens.insert(IDInfo);
910
61
911
61
  if (!Info.MI)
912
0
    return Info.Name;
913
61
914
61
  // Manually expand its arguments from the previous macro.
915
61
  Info.Args.expandFromPrevMacro(PrevArgs);
916
61
917
61
  // Iterate over the macro's tokens and stringify them.
918
319
  for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
919
258
       ++It) {
920
258
    Token T = *It;
921
258
922
258
    // If this token is not an identifier, we only need to print it.
923
258
    if (T.isNot(tok::identifier)) {
924
156
      Printer.printToken(T);
925
156
      continue;
926
156
    }
927
102
928
102
    const auto *II = T.getIdentifierInfo();
929
102
    assert(II &&
930
102
          "This token is an identifier but has no IdentifierInfo!");
931
102
932
102
    // If this token is a macro that should be expanded inside the current
933
102
    // macro.
934
102
    if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
935
22
      getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
936
22
                                    AlreadyProcessedTokens);
937
22
938
22
      // If this is a function-like macro, skip its arguments, as
939
22
      // getExpandedMacro() already printed them. If this is the case, let's
940
22
      // first jump to the '(' token.
941
22
      auto N = std::next(It);
942
22
      if (N != E && 
N->is(tok::l_paren)20
)
943
9
        It = getMatchingRParen(++It, E);
944
22
      continue;
945
22
    }
946
80
947
80
    // If this token is the current macro's argument, we should expand it.
948
80
    auto ArgMapIt = Info.Args.find(II);
949
80
    if (ArgMapIt != Info.Args.end()) {
950
56
      for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
951
56
                                      ArgEnd = ArgMapIt->second.end();
952
176
           ArgIt != ArgEnd; 
++ArgIt120
) {
953
120
954
120
        // These tokens may still be macros, if that is the case, handle it the
955
120
        // same way we did above.
956
120
        const auto *ArgII = ArgIt->getIdentifierInfo();
957
120
        if (!ArgII) {
958
61
          Printer.printToken(*ArgIt);
959
61
          continue;
960
61
        }
961
59
962
59
        const auto *MI = PP.getMacroInfo(ArgII);
963
59
        if (!MI) {
964
50
          Printer.printToken(*ArgIt);
965
50
          continue;
966
50
        }
967
9
968
9
        getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
969
9
                                      Info.Args, AlreadyProcessedTokens);
970
9
        // Peek the next token if it is a tok::l_paren. This way we can decide
971
9
        // if this is the application or just a reference to a function maxro
972
9
        // symbol:
973
9
        //
974
9
        // #define apply(f) ...
975
9
        // #define func(x) ...
976
9
        // apply(func)
977
9
        // apply(func(42))
978
9
        auto N = std::next(ArgIt);
979
9
        if (N != ArgEnd && 
N->is(tok::l_paren)5
)
980
3
          ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
981
9
      }
982
56
      continue;
983
56
    }
984
24
985
24
    // If control reached here, then this token isn't a macro identifier, nor an
986
24
    // unexpanded macro argument that we need to handle, print it.
987
24
    Printer.printToken(T);
988
24
  }
989
61
990
61
  AlreadyProcessedTokens.erase(IDInfo);
991
61
992
61
  return Info.Name;
993
61
}
994
995
static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
996
62
                                            const Preprocessor &PP) {
997
62
998
62
  const SourceManager &SM = PP.getSourceManager();
999
62
  const LangOptions &LangOpts = PP.getLangOpts();
1000
62
1001
62
  // First, we create a Lexer to lex *at the expansion location* the tokens
1002
62
  // referring to the macro's name and its arguments.
1003
62
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
1004
62
  const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
1005
62
  const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
1006
62
1007
62
  Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
1008
62
                 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
1009
62
1010
62
  // Acquire the macro's name.
1011
62
  Token TheTok;
1012
62
  RawLexer.LexFromRawLexer(TheTok);
1013
62
1014
62
  std::string MacroName = PP.getSpelling(TheTok);
1015
62
1016
62
  const auto *II = PP.getIdentifierInfo(MacroName);
1017
62
  assert(II && "Failed to acquire the IndetifierInfo for the macro!");
1018
62
1019
62
  const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
1020
62
  // assert(MI && "The macro must've been defined at it's expansion location!");
1021
62
  //
1022
62
  // We should always be able to obtain the MacroInfo in a given TU, but if
1023
62
  // we're running the analyzer with CTU, the Preprocessor won't contain the
1024
62
  // directive history (or anything for that matter) from another TU.
1025
62
  // TODO: assert when we're not running with CTU.
1026
62
  if (!MI)
1027
0
    return { MacroName, MI, {} };
1028
62
1029
62
  // Acquire the macro's arguments.
1030
62
  //
1031
62
  // The rough idea here is to lex from the first left parentheses to the last
1032
62
  // right parentheses, and map the macro's unexpanded arguments to what they
1033
62
  // will be expanded to. An expanded macro argument may contain several tokens
1034
62
  // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
1035
62
  // which point we start lexing the next argument or finish.
1036
62
  ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
1037
62
  if (MacroArgs.empty())
1038
22
    return { MacroName, MI, {} };
1039
40
1040
40
  RawLexer.LexFromRawLexer(TheTok);
1041
40
  // When this is a token which expands to another macro function then its
1042
40
  // parentheses are not at its expansion locaiton. For example:
1043
40
  //
1044
40
  // #define foo(x) int bar() { return x; }
1045
40
  // #define apply_zero(f) f(0)
1046
40
  // apply_zero(foo)
1047
40
  //               ^
1048
40
  //               This is not a tok::l_paren, but foo is a function.
1049
40
  if (TheTok.isNot(tok::l_paren))
1050
1
    return { MacroName, MI, {} };
1051
39
1052
39
  MacroArgMap Args;
1053
39
1054
39
  // When the macro's argument is a function call, like
1055
39
  //   CALL_FN(someFunctionName(param1, param2))
1056
39
  // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
1057
39
  // actual macro arguments, or do not represent the macro argument's closing
1058
39
  // parentheses, so we'll count how many parentheses aren't closed yet.
1059
39
  // If ParanthesesDepth
1060
39
  //   * = 0, then there are no more arguments to lex.
1061
39
  //   * = 1, then if we find a tok::comma, we can start lexing the next arg.
1062
39
  //   * > 1, then tok::comma is a part of the current arg.
1063
39
  int ParenthesesDepth = 1;
1064
39
1065
39
  // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
1066
39
  // even if we lex a tok::comma and ParanthesesDepth == 1.
1067
39
  const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
1068
39
1069
57
  for (const IdentifierInfo *UnexpArgII : MacroArgs) {
1070
57
    MacroArgMap::mapped_type ExpandedArgTokens;
1071
57
1072
57
    // One could also simply not supply a single argument to __VA_ARGS__ -- this
1073
57
    // results in a preprocessor warning, but is not an error:
1074
57
    //   #define VARIADIC(ptr, ...) \
1075
57
    //     someVariadicTemplateFunction(__VA_ARGS__)
1076
57
    //
1077
57
    //   int *ptr;
1078
57
    //   VARIADIC(ptr); // Note that there are no commas, this isn't just an
1079
57
    //                  // empty parameter -- there are no parameters for '...'.
1080
57
    // In any other case, ParenthesesDepth mustn't be 0 here.
1081
57
    if (ParenthesesDepth != 0) {
1082
56
1083
56
      // Lex the first token of the next macro parameter.
1084
56
      RawLexer.LexFromRawLexer(TheTok);
1085
56
1086
188
      while (!(ParenthesesDepth == 1 &&
1087
188
              
(UnexpArgII == __VA_ARGS__II 140
?
false6
:
TheTok.is(tok::comma)134
))) {
1088
171
        assert(TheTok.isNot(tok::eof) &&
1089
171
               "EOF encountered while looking for expanded macro args!");
1090
171
1091
171
        if (TheTok.is(tok::l_paren))
1092
10
          ++ParenthesesDepth;
1093
171
1094
171
        if (TheTok.is(tok::r_paren))
1095
49
          --ParenthesesDepth;
1096
171
1097
171
        if (ParenthesesDepth == 0)
1098
39
          break;
1099
132
1100
132
        if (TheTok.is(tok::raw_identifier))
1101
62
          PP.LookUpIdentifierInfo(TheTok);
1102
132
1103
132
        ExpandedArgTokens.push_back(TheTok);
1104
132
        RawLexer.LexFromRawLexer(TheTok);
1105
132
      }
1106
56
    } else {
1107
1
      assert(UnexpArgII == __VA_ARGS__II);
1108
1
    }
1109
57
1110
57
    Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1111
57
  }
1112
39
1113
39
  assert(TheTok.is(tok::r_paren) &&
1114
39
         "Expanded macro argument acquisition failed! After the end of the loop"
1115
39
         " this token should be ')'!");
1116
39
1117
39
  return { MacroName, MI, Args };
1118
39
}
1119
1120
static MacroInfo::tokens_iterator getMatchingRParen(
1121
    MacroInfo::tokens_iterator It,
1122
12
    MacroInfo::tokens_iterator End) {
1123
12
1124
12
  assert(It->is(tok::l_paren) && "This token should be '('!");
1125
12
1126
12
  // Skip until we find the closing ')'.
1127
12
  int ParenthesesDepth = 1;
1128
47
  while (ParenthesesDepth != 0) {
1129
35
    ++It;
1130
35
1131
35
    assert(It->isNot(tok::eof) &&
1132
35
           "Encountered EOF while attempting to skip macro arguments!");
1133
35
    assert(It != End &&
1134
35
           "End of the macro definition reached before finding ')'!");
1135
35
1136
35
    if (It->is(tok::l_paren))
1137
1
      ++ParenthesesDepth;
1138
35
1139
35
    if (It->is(tok::r_paren))
1140
13
      --ParenthesesDepth;
1141
35
  }
1142
12
  return It;
1143
12
}
1144
1145
static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1146
                                                const SourceManager &SM,
1147
                                                const IdentifierInfo *II,
1148
164
                                                SourceLocation Loc) {
1149
164
1150
164
  const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1151
164
  if (!MD)
1152
80
    return nullptr;
1153
84
1154
84
  return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1155
84
}
1156
1157
61
void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1158
61
1159
61
  for (value_type &Pair : *this) {
1160
57
    ExpArgTokens &CurrExpArgTokens = Pair.second;
1161
57
1162
57
    // For each token in the expanded macro argument.
1163
57
    auto It = CurrExpArgTokens.begin();
1164
189
    while (It != CurrExpArgTokens.end()) {
1165
132
      if (It->isNot(tok::identifier)) {
1166
74
        ++It;
1167
74
        continue;
1168
74
      }
1169
58
1170
58
      const auto *II = It->getIdentifierInfo();
1171
58
      assert(II);
1172
58
1173
58
      // Is this an argument that "Super" expands further?
1174
58
      if (!Super.count(II)) {
1175
49
        ++It;
1176
49
        continue;
1177
49
      }
1178
9
1179
9
      const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1180
9
1181
9
      It = CurrExpArgTokens.insert(
1182
9
          It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1183
9
      std::advance(It, SuperExpArgTokens.size());
1184
9
      It = CurrExpArgTokens.erase(It);
1185
9
    }
1186
57
  }
1187
61
}
1188
1189
291
void TokenPrinter::printToken(const Token &Tok) {
1190
291
  // If this is the first token to be printed, don't print space.
1191
291
  if (PrevTok.isNot(tok::unknown)) {
1192
260
    // If the tokens were already space separated, or if they must be to avoid
1193
260
    // them being implicitly pasted, add a space between them.
1194
260
    if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1195
149
                                                       Tok)) {
1196
112
      // AvoidConcat doesn't check for ##, don't print a space around it.
1197
112
      if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1198
112
        OS << ' ';
1199
112
      }
1200
112
    }
1201
260
  }
1202
291
1203
291
  if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1204
288
    if (PrevTok.is(tok::hash))
1205
1
      OS << '\"' << PP.getSpelling(Tok) << '\"';
1206
287
    else
1207
287
      OS << PP.getSpelling(Tok);
1208
288
  }
1209
291
1210
291
  PrevPrevTok = PrevTok;
1211
291
  PrevTok = Tok;
1212
291
}