Coverage Report

Created: 2020-02-25 14:32

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