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     ~PlistDiagnostics() override {}
54 
55     void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
56                               FilesMade *filesMade) override;
57 
58     StringRef getName() const override {
59       return "PlistDiagnostics";
60     }
61 
62     PathGenerationScheme getGenerationScheme() const override {
63       return Extensive;
64     }
65     bool supportsLogicalOpControlFlow() const override { return true; }
66     bool supportsCrossFileDiagnostics() const override {
67       return SupportsCrossFileDiagnostics;
68     }
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     : FM(FM), AnOpts(AnOpts), PP(PP), CTU(CTU) {
87   }
88 
89   void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
90     ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
91 
92     // Don't emit a warning about an unused private field.
93     (void)AnOpts;
94   }
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                    bool isKeyEvent = false) {
108     switch (P.getKind()) {
109       case PathDiagnosticPiece::ControlFlow:
110         if (includeControlFlow)
111           ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
112         break;
113       case PathDiagnosticPiece::Call:
114         ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
115                    depth);
116         break;
117       case PathDiagnosticPiece::Event:
118         ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
119                     isKeyEvent);
120         break;
121       case PathDiagnosticPiece::Macro:
122         ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
123                              depth);
124         break;
125       case PathDiagnosticPiece::Note:
126         ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
127         break;
128       case PathDiagnosticPiece::PopUp:
129         ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
130         break;
131     }
132   }
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     : 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                               unsigned indent) {
192 
193   if (Ranges.empty())
194     return;
195 
196   Indent(o, indent) << "<key>ranges</key>\n";
197   Indent(o, indent) << "<array>\n";
198   ++indent;
199 
200   const SourceManager &SM = PP.getSourceManager();
201   const LangOptions &LangOpts = PP.getLangOpts();
202 
203   for (auto &R : Ranges)
204     EmitRange(o, SM,
205               Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
206               FM, indent + 1);
207   --indent;
208   Indent(o, indent) << "</array>\n";
209 }
210 
211 void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
212                                unsigned indent) {
213   // Output the text.
214   assert(!Message.empty());
215   Indent(o, indent) << "<key>extended_message</key>\n";
216   Indent(o, indent);
217   EmitString(o, Message) << '\n';
218 
219   // Output the short text.
220   // FIXME: Really use a short string.
221   Indent(o, indent) << "<key>message</key>\n";
222   Indent(o, indent);
223   EmitString(o, Message) << '\n';
224 }
225 
226 void PlistPrinter::EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits,
227                               unsigned indent) {
228   if (fixits.size() == 0)
229     return;
230 
231   const SourceManager &SM = PP.getSourceManager();
232   const LangOptions &LangOpts = PP.getLangOpts();
233 
234   Indent(o, indent) << "<key>fixits</key>\n";
235   Indent(o, indent) << "<array>\n";
236   for (const auto &fixit : fixits) {
237     assert(!fixit.isNull());
238     // FIXME: Add support for InsertFromRange and BeforePreviousInsertion.
239     assert(!fixit.InsertFromRange.isValid() && "Not implemented yet!");
240     assert(!fixit.BeforePreviousInsertions && "Not implemented yet!");
241     Indent(o, indent) << " <dict>\n";
242     Indent(o, indent) << "  <key>remove_range</key>\n";
243     EmitRange(o, SM, Lexer::getAsCharRange(fixit.RemoveRange, SM, LangOpts),
244               FM, indent + 2);
245     Indent(o, indent) << "  <key>insert_string</key>";
246     EmitString(o, fixit.CodeToInsert);
247     o << "\n";
248     Indent(o, indent) << " </dict>\n";
249   }
250   Indent(o, indent) << "</array>\n";
251 }
252 
253 void PlistPrinter::ReportControlFlow(raw_ostream &o,
254                                      const PathDiagnosticControlFlowPiece& P,
255                                      unsigned indent) {
256 
257   const SourceManager &SM = PP.getSourceManager();
258   const LangOptions &LangOpts = PP.getLangOpts();
259 
260   Indent(o, indent) << "<dict>\n";
261   ++indent;
262 
263   Indent(o, indent) << "<key>kind</key><string>control</string>\n";
264 
265   // Emit edges.
266   Indent(o, indent) << "<key>edges</key>\n";
267   ++indent;
268   Indent(o, indent) << "<array>\n";
269   ++indent;
270   for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
271        I!=E; ++I) {
272     Indent(o, indent) << "<dict>\n";
273     ++indent;
274 
275     // Make the ranges of the start and end point self-consistent with adjacent edges
276     // by forcing to use only the beginning of the range.  This simplifies the layout
277     // logic for clients.
278     Indent(o, indent) << "<key>start</key>\n";
279     SourceRange StartEdge(
280         SM.getExpansionLoc(I->getStart().asRange().getBegin()));
281     EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
282               indent + 1);
283 
284     Indent(o, indent) << "<key>end</key>\n";
285     SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
286     EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
287               indent + 1);
288 
289     --indent;
290     Indent(o, indent) << "</dict>\n";
291   }
292   --indent;
293   Indent(o, indent) << "</array>\n";
294   --indent;
295 
296   // Output any helper text.
297   const auto &s = P.getString();
298   if (!s.empty()) {
299     Indent(o, indent) << "<key>alternate</key>";
300     EmitString(o, s) << '\n';
301   }
302 
303   assert(P.getFixits().size() == 0 &&
304          "Fixits on constrol flow pieces are not implemented yet!");
305 
306   --indent;
307   Indent(o, indent) << "</dict>\n";
308 }
309 
310 void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
311                                unsigned indent, unsigned depth,
312                                bool isKeyEvent) {
313 
314   const SourceManager &SM = PP.getSourceManager();
315 
316   Indent(o, indent) << "<dict>\n";
317   ++indent;
318 
319   Indent(o, indent) << "<key>kind</key><string>event</string>\n";
320 
321   if (isKeyEvent) {
322     Indent(o, indent) << "<key>key_event</key><true/>\n";
323   }
324 
325   // Output the location.
326   FullSourceLoc L = P.getLocation().asLocation();
327 
328   Indent(o, indent) << "<key>location</key>\n";
329   EmitLocation(o, SM, L, FM, indent);
330 
331   // Output the ranges (if any).
332   ArrayRef<SourceRange> Ranges = P.getRanges();
333   EmitRanges(o, Ranges, indent);
334 
335   // Output the call depth.
336   Indent(o, indent) << "<key>depth</key>";
337   EmitInteger(o, depth) << '\n';
338 
339   // Output the text.
340   EmitMessage(o, P.getString(), indent);
341 
342   // Output the fixits.
343   EmitFixits(o, P.getFixits(), indent);
344 
345   // Finish up.
346   --indent;
347   Indent(o, indent); o << "</dict>\n";
348 }
349 
350 void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
351                               unsigned indent,
352                               unsigned depth) {
353 
354   if (auto callEnter = P.getCallEnterEvent())
355     ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
356                 P.isLastInMainSourceFile());
357 
358 
359   ++depth;
360 
361   if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
362     ReportPiece(o, *callEnterWithinCaller, indent, depth,
363                 /*includeControlFlow*/ true);
364 
365   for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
366     ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
367 
368   --depth;
369 
370   if (auto callExit = P.getCallExitEvent())
371     ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
372 
373   assert(P.getFixits().size() == 0 &&
374          "Fixits on call pieces are not implemented yet!");
375 }
376 
377 void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
378                                         const PathDiagnosticMacroPiece& P,
379                                         unsigned indent, unsigned depth) {
380   MacroPieces.push_back(&P);
381 
382   for (PathPieces::const_iterator I = P.subPieces.begin(),
383                                   E = P.subPieces.end();
384        I != E; ++I) {
385     ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
386   }
387 
388   assert(P.getFixits().size() == 0 &&
389          "Fixits on constrol flow pieces are not implemented yet!");
390 }
391 
392 void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
393 
394   for (const PathDiagnosticMacroPiece *P : MacroPieces) {
395     const SourceManager &SM = PP.getSourceManager();
396     ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU);
397 
398     Indent(o, indent) << "<dict>\n";
399     ++indent;
400 
401     // Output the location.
402     FullSourceLoc L = P->getLocation().asLocation();
403 
404     Indent(o, indent) << "<key>location</key>\n";
405     EmitLocation(o, SM, L, FM, indent);
406 
407     // Output the ranges (if any).
408     ArrayRef<SourceRange> Ranges = P->getRanges();
409     EmitRanges(o, Ranges, indent);
410 
411     // Output the macro name.
412     Indent(o, indent) << "<key>name</key>";
413     EmitString(o, EI.MacroName) << '\n';
414 
415     // Output what it expands into.
416     Indent(o, indent) << "<key>expansion</key>";
417     EmitString(o, EI.Expansion) << '\n';
418 
419     // Finish up.
420     --indent;
421     Indent(o, indent);
422     o << "</dict>\n";
423   }
424 }
425 
426 void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
427                               unsigned indent) {
428 
429   const SourceManager &SM = PP.getSourceManager();
430 
431   Indent(o, indent) << "<dict>\n";
432   ++indent;
433 
434   // Output the location.
435   FullSourceLoc L = P.getLocation().asLocation();
436 
437   Indent(o, indent) << "<key>location</key>\n";
438   EmitLocation(o, SM, L, FM, indent);
439 
440   // Output the ranges (if any).
441   ArrayRef<SourceRange> Ranges = P.getRanges();
442   EmitRanges(o, Ranges, indent);
443 
444   // Output the text.
445   EmitMessage(o, P.getString(), indent);
446 
447   // Output the fixits.
448   EmitFixits(o, P.getFixits(), indent);
449 
450   // Finish up.
451   --indent;
452   Indent(o, indent); o << "</dict>\n";
453 }
454 
455 void PlistPrinter::ReportPopUp(raw_ostream &o,
456                                const PathDiagnosticPopUpPiece &P,
457                                unsigned indent) {
458   const SourceManager &SM = PP.getSourceManager();
459 
460   Indent(o, indent) << "<dict>\n";
461   ++indent;
462 
463   Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";
464 
465   // Output the location.
466   FullSourceLoc L = P.getLocation().asLocation();
467 
468   Indent(o, indent) << "<key>location</key>\n";
469   EmitLocation(o, SM, L, FM, indent);
470 
471   // Output the ranges (if any).
472   ArrayRef<SourceRange> Ranges = P.getRanges();
473   EmitRanges(o, Ranges, indent);
474 
475   // Output the text.
476   EmitMessage(o, P.getString(), indent);
477 
478   assert(P.getFixits().size() == 0 &&
479          "Fixits on pop-up pieces are not implemented yet!");
480 
481   // Finish up.
482   --indent;
483   Indent(o, indent) << "</dict>\n";
484 }
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                           llvm::raw_fd_ostream &o) {
497   unsigned IndentLevel = InputIndentLevel;
498 
499   Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
500   Indent(o, IndentLevel) << "<dict>\n";
501   IndentLevel++;
502 
503   // Mapping from file IDs to executed lines.
504   const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
505   for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) {
506     unsigned FileKey = AddFID(FM, Fids, I->first);
507     Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
508     Indent(o, IndentLevel) << "<array>\n";
509     IndentLevel++;
510     for (unsigned LineNo : I->second) {
511       Indent(o, IndentLevel);
512       EmitInteger(o, LineNo) << "\n";
513     }
514     IndentLevel--;
515     Indent(o, IndentLevel) << "</array>\n";
516   }
517   IndentLevel--;
518   Indent(o, IndentLevel) << "</dict>\n";
519 
520   assert(IndentLevel == InputIndentLevel);
521 }
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                          const PathPieces &Path) {
527   PlistPrinter Printer(FM, AnOpts, PP, CTU);
528   assert(std::is_partitioned(Path.begin(), Path.end(),
529                              [](const PathDiagnosticPieceRef &E) {
530                                return E->getKind() == PathDiagnosticPiece::Note;
531                              }) &&
532          "PathDiagnostic is not partitioned so that notes precede the rest");
533 
534   PathPieces::const_iterator FirstNonNote = std::partition_point(
535       Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) {
536         return E->getKind() == PathDiagnosticPiece::Note;
537       });
538 
539   PathPieces::const_iterator I = Path.begin();
540 
541   if (FirstNonNote != Path.begin()) {
542     o << "   <key>notes</key>\n"
543          "   <array>\n";
544 
545     for (; I != FirstNonNote; ++I)
546       Printer.ReportDiag(o, **I);
547 
548     o << "   </array>\n";
549   }
550 
551   o << "   <key>path</key>\n";
552 
553   o << "   <array>\n";
554 
555   for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
556     Printer.ReportDiag(o, **I);
557 
558   o << "   </array>\n";
559 
560   if (!AnOpts.ShouldDisplayMacroExpansions)
561     return;
562 
563   o << "   <key>macro_expansions</key>\n"
564        "   <array>\n";
565   Printer.ReportMacroExpansions(o, /* indent */ 4);
566   o << "   </array>\n";
567 }
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       SupportsCrossFileDiagnostics(supportsMultipleFiles) {
579   // FIXME: Will be used by a later planned change.
580   (void)this->CTU;
581 }
582 
583 void ento::createPlistDiagnosticConsumer(
584     AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
585     const std::string &s, const Preprocessor &PP,
586     const cross_tu::CrossTranslationUnitContext &CTU) {
587   C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
588                                    /*supportsMultipleFiles*/ false));
589 }
590 
591 void ento::createPlistMultiFileDiagnosticConsumer(
592     AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
593     const std::string &s, const Preprocessor &PP,
594     const cross_tu::CrossTranslationUnitContext &CTU) {
595   C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
596                                    /*supportsMultipleFiles*/ true));
597 }
598 void PlistDiagnostics::FlushDiagnosticsImpl(
599                                     std::vector<const PathDiagnostic *> &Diags,
600                                     FilesMade *filesMade) {
601   // Build up a set of FIDs that we use by scanning the locations and
602   // ranges of the diagnostics.
603   FIDMap FM;
604   SmallVector<FileID, 10> Fids;
605   const SourceManager& SM = PP.getSourceManager();
606   const LangOptions &LangOpts = PP.getLangOpts();
607 
608   auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
609     AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
610     ArrayRef<SourceRange> Ranges = Piece.getRanges();
611     for (const SourceRange &Range : Ranges) {
612       AddFID(FM, Fids, SM, Range.getBegin());
613       AddFID(FM, Fids, SM, Range.getEnd());
614     }
615   };
616 
617   for (const PathDiagnostic *D : Diags) {
618 
619     SmallVector<const PathPieces *, 5> WorkList;
620     WorkList.push_back(&D->path);
621 
622     while (!WorkList.empty()) {
623       const PathPieces &Path = *WorkList.pop_back_val();
624 
625       for (const auto &Iter : Path) {
626         const PathDiagnosticPiece &Piece = *Iter;
627         AddPieceFID(Piece);
628 
629         if (const PathDiagnosticCallPiece *Call =
630                 dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
631           if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
632             AddPieceFID(*CallEnterWithin);
633 
634           if (auto CallEnterEvent = Call->getCallEnterEvent())
635             AddPieceFID(*CallEnterEvent);
636 
637           WorkList.push_back(&Call->path);
638         } else if (const PathDiagnosticMacroPiece *Macro =
639                        dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
640           WorkList.push_back(&Macro->subPieces);
641         }
642       }
643     }
644   }
645 
646   // Open the file.
647   std::error_code EC;
648   llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
649   if (EC) {
650     llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
651     return;
652   }
653 
654   EmitPlistHeader(o);
655 
656   // Write the root object: a <dict> containing...
657   //  - "clang_version", the string representation of clang version
658   //  - "files", an <array> mapping from FIDs to file names
659   //  - "diagnostics", an <array> containing the path diagnostics
660   o << "<dict>\n" <<
661        " <key>clang_version</key>\n";
662   EmitString(o, getClangFullVersion()) << '\n';
663   o << " <key>diagnostics</key>\n"
664        " <array>\n";
665 
666   for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
667        DE = Diags.end(); DI!=DE; ++DI) {
668 
669     o << "  <dict>\n";
670 
671     const PathDiagnostic *D = *DI;
672     printBugPath(o, FM, AnOpts, PP, CTU, D->path);
673 
674     // Output the bug type and bug category.
675     o << "   <key>description</key>";
676     EmitString(o, D->getShortDescription()) << '\n';
677     o << "   <key>category</key>";
678     EmitString(o, D->getCategory()) << '\n';
679     o << "   <key>type</key>";
680     EmitString(o, D->getBugType()) << '\n';
681     o << "   <key>check_name</key>";
682     EmitString(o, D->getCheckerName()) << '\n';
683 
684     o << "   <!-- This hash is experimental and going to change! -->\n";
685     o << "   <key>issue_hash_content_of_line_in_context</key>";
686     PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
687     FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
688                                             ? UPDLoc.asLocation()
689                                             : D->getLocation().asLocation()),
690                     SM);
691     const Decl *DeclWithIssue = D->getDeclWithIssue();
692     EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
693                                DeclWithIssue, LangOpts))
694         << '\n';
695 
696     // Output information about the semantic context where
697     // the issue occurred.
698     if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
699       // FIXME: handle blocks, which have no name.
700       if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
701         StringRef declKind;
702         switch (ND->getKind()) {
703           case Decl::CXXRecord:
704             declKind = "C++ class";
705             break;
706           case Decl::CXXMethod:
707             declKind = "C++ method";
708             break;
709           case Decl::ObjCMethod:
710             declKind = "Objective-C method";
711             break;
712           case Decl::Function:
713             declKind = "function";
714             break;
715           default:
716             break;
717         }
718         if (!declKind.empty()) {
719           const std::string &declName = ND->getDeclName().getAsString();
720           o << "  <key>issue_context_kind</key>";
721           EmitString(o, declKind) << '\n';
722           o << "  <key>issue_context</key>";
723           EmitString(o, declName) << '\n';
724         }
725 
726         // Output the bug hash for issue unique-ing. Currently, it's just an
727         // offset from the beginning of the function.
728         if (const Stmt *Body = DeclWithIssue->getBody()) {
729 
730           // If the bug uniqueing location exists, use it for the hash.
731           // For example, this ensures that two leaks reported on the same line
732           // will have different issue_hashes and that the hash will identify
733           // the leak location even after code is added between the allocation
734           // site and the end of scope (leak report location).
735           if (UPDLoc.isValid()) {
736             FullSourceLoc UFunL(
737                 SM.getExpansionLoc(
738                     D->getUniqueingDecl()->getBody()->getBeginLoc()),
739                 SM);
740             o << "  <key>issue_hash_function_offset</key><string>"
741               << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
742               << "</string>\n";
743 
744           // Otherwise, use the location on which the bug is reported.
745           } else {
746             FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
747             o << "  <key>issue_hash_function_offset</key><string>"
748               << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
749               << "</string>\n";
750           }
751 
752         }
753       }
754     }
755 
756     // Output the location of the bug.
757     o << "  <key>location</key>\n";
758     EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
759 
760     // Output the diagnostic to the sub-diagnostic client, if any.
761     if (!filesMade->empty()) {
762       StringRef lastName;
763       PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
764       if (files) {
765         for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
766                 CE = files->end(); CI != CE; ++CI) {
767           StringRef newName = CI->first;
768           if (newName != lastName) {
769             if (!lastName.empty()) {
770               o << "  </array>\n";
771             }
772             lastName = newName;
773             o <<  "  <key>" << lastName << "_files</key>\n";
774             o << "  <array>\n";
775           }
776           o << "   <string>" << CI->second << "</string>\n";
777         }
778         o << "  </array>\n";
779       }
780     }
781 
782     printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
783 
784     // Close up the entry.
785     o << "  </dict>\n";
786   }
787 
788   o << " </array>\n";
789 
790   o << " <key>files</key>\n"
791        " <array>\n";
792   for (FileID FID : Fids)
793     EmitString(o << "  ", SM.getFileEntryForID(FID)->getName()) << '\n';
794   o << " </array>\n";
795 
796   if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
797     o << " <key>statistics</key>\n";
798     std::string stats;
799     llvm::raw_string_ostream os(stats);
800     llvm::PrintStatisticsJSON(os);
801     os.flush();
802     EmitString(o, html::EscapeText(stats)) << '\n';
803   }
804 
805   // Finish.
806   o << "</dict>\n</plist>\n";
807 }
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     : 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     : OS(OS), PP(PP), ConcatInfo(PP) {
843     PrevTok.setKind(tok::unknown);
844     PrevPrevTok.setKind(tok::unknown);
845   }
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                  const cross_tu::CrossTranslationUnitContext &CTU) {
928 
929   const Preprocessor *PPToUse = &PP;
930   if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
931     MacroLoc = LocAndUnit->first;
932     PPToUse = &LocAndUnit->second->getPreprocessor();
933   }
934 
935   llvm::SmallString<200> ExpansionBuf;
936   llvm::raw_svector_ostream OS(ExpansionBuf);
937   TokenPrinter Printer(OS, *PPToUse);
938   llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
939 
940   std::string MacroName = getMacroNameAndPrintExpansion(
941       Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
942   return { MacroName, OS.str() };
943 }
944 
945 static std::string getMacroNameAndPrintExpansion(
946     TokenPrinter &Printer,
947     SourceLocation MacroLoc,
948     const Preprocessor &PP,
949     const MacroArgMap &PrevArgs,
950     llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
951 
952   const SourceManager &SM = PP.getSourceManager();
953 
954   MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
955   IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
956 
957   // TODO: If the macro definition contains another symbol then this function is
958   // called recursively. In case this symbol is the one being defined, it will
959   // be an infinite recursion which is stopped by this "if" statement. However,
960   // in this case we don't get the full expansion text in the Plist file. See
961   // the test file where "value" is expanded to "garbage_" instead of
962   // "garbage_value".
963   if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
964     return Info.Name;
965   AlreadyProcessedTokens.insert(IDInfo);
966 
967   if (!Info.MI)
968     return Info.Name;
969 
970   // Manually expand its arguments from the previous macro.
971   Info.Args.expandFromPrevMacro(PrevArgs);
972 
973   // Iterate over the macro's tokens and stringify them.
974   for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
975        ++It) {
976     Token T = *It;
977 
978     // If this token is not an identifier, we only need to print it.
979     if (T.isNot(tok::identifier)) {
980       Printer.printToken(T);
981       continue;
982     }
983 
984     const auto *II = T.getIdentifierInfo();
985     assert(II &&
986           "This token is an identifier but has no IdentifierInfo!");
987 
988     // If this token is a macro that should be expanded inside the current
989     // macro.
990     if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
991       getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
992                                     AlreadyProcessedTokens);
993 
994       // If this is a function-like macro, skip its arguments, as
995       // getExpandedMacro() already printed them. If this is the case, let's
996       // first jump to the '(' token.
997       auto N = std::next(It);
998       if (N != E && N->is(tok::l_paren))
999         It = getMatchingRParen(++It, E);
1000       continue;
1001     }
1002 
1003     // If this token is the current macro's argument, we should expand it.
1004     auto ArgMapIt = Info.Args.find(II);
1005     if (ArgMapIt != Info.Args.end()) {
1006       for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
1007                                       ArgEnd = ArgMapIt->second.end();
1008            ArgIt != ArgEnd; ++ArgIt) {
1009 
1010         // These tokens may still be macros, if that is the case, handle it the
1011         // same way we did above.
1012         const auto *ArgII = ArgIt->getIdentifierInfo();
1013         if (!ArgII) {
1014           Printer.printToken(*ArgIt);
1015           continue;
1016         }
1017 
1018         const auto *MI = PP.getMacroInfo(ArgII);
1019         if (!MI) {
1020           Printer.printToken(*ArgIt);
1021           continue;
1022         }
1023 
1024         getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
1025                                       Info.Args, AlreadyProcessedTokens);
1026         // Peek the next token if it is a tok::l_paren. This way we can decide
1027         // if this is the application or just a reference to a function maxro
1028         // symbol:
1029         //
1030         // #define apply(f) ...
1031         // #define func(x) ...
1032         // apply(func)
1033         // apply(func(42))
1034         auto N = std::next(ArgIt);
1035         if (N != ArgEnd && N->is(tok::l_paren))
1036           ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
1037       }
1038       continue;
1039     }
1040 
1041     // If control reached here, then this token isn't a macro identifier, nor an
1042     // unexpanded macro argument that we need to handle, print it.
1043     Printer.printToken(T);
1044   }
1045 
1046   AlreadyProcessedTokens.erase(IDInfo);
1047 
1048   return Info.Name;
1049 }
1050 
1051 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
1052                                             const Preprocessor &PP) {
1053 
1054   const SourceManager &SM = PP.getSourceManager();
1055   const LangOptions &LangOpts = PP.getLangOpts();
1056 
1057   // First, we create a Lexer to lex *at the expansion location* the tokens
1058   // referring to the macro's name and its arguments.
1059   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
1060   const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
1061   const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
1062 
1063   Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
1064                  MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
1065 
1066   // Acquire the macro's name.
1067   Token TheTok;
1068   RawLexer.LexFromRawLexer(TheTok);
1069 
1070   std::string MacroName = PP.getSpelling(TheTok);
1071 
1072   const auto *II = PP.getIdentifierInfo(MacroName);
1073   assert(II && "Failed to acquire the IndetifierInfo for the macro!");
1074 
1075   const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
1076   // assert(MI && "The macro must've been defined at it's expansion location!");
1077   //
1078   // We should always be able to obtain the MacroInfo in a given TU, but if
1079   // we're running the analyzer with CTU, the Preprocessor won't contain the
1080   // directive history (or anything for that matter) from another TU.
1081   // TODO: assert when we're not running with CTU.
1082   if (!MI)
1083     return { MacroName, MI, {} };
1084 
1085   // Acquire the macro's arguments.
1086   //
1087   // The rough idea here is to lex from the first left parentheses to the last
1088   // right parentheses, and map the macro's unexpanded arguments to what they
1089   // will be expanded to. An expanded macro argument may contain several tokens
1090   // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
1091   // which point we start lexing the next argument or finish.
1092   ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
1093   if (MacroArgs.empty())
1094     return { MacroName, MI, {} };
1095 
1096   RawLexer.LexFromRawLexer(TheTok);
1097   // When this is a token which expands to another macro function then its
1098   // parentheses are not at its expansion locaiton. For example:
1099   //
1100   // #define foo(x) int bar() { return x; }
1101   // #define apply_zero(f) f(0)
1102   // apply_zero(foo)
1103   //               ^
1104   //               This is not a tok::l_paren, but foo is a function.
1105   if (TheTok.isNot(tok::l_paren))
1106     return { MacroName, MI, {} };
1107 
1108   MacroArgMap Args;
1109 
1110   // When the macro's argument is a function call, like
1111   //   CALL_FN(someFunctionName(param1, param2))
1112   // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
1113   // actual macro arguments, or do not represent the macro argument's closing
1114   // parentheses, so we'll count how many parentheses aren't closed yet.
1115   // If ParanthesesDepth
1116   //   * = 0, then there are no more arguments to lex.
1117   //   * = 1, then if we find a tok::comma, we can start lexing the next arg.
1118   //   * > 1, then tok::comma is a part of the current arg.
1119   int ParenthesesDepth = 1;
1120 
1121   // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
1122   // even if we lex a tok::comma and ParanthesesDepth == 1.
1123   const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
1124 
1125   for (const IdentifierInfo *UnexpArgII : MacroArgs) {
1126     MacroArgMap::mapped_type ExpandedArgTokens;
1127 
1128     // One could also simply not supply a single argument to __VA_ARGS__ -- this
1129     // results in a preprocessor warning, but is not an error:
1130     //   #define VARIADIC(ptr, ...) \
1131     //     someVariadicTemplateFunction(__VA_ARGS__)
1132     //
1133     //   int *ptr;
1134     //   VARIADIC(ptr); // Note that there are no commas, this isn't just an
1135     //                  // empty parameter -- there are no parameters for '...'.
1136     // In any other case, ParenthesesDepth mustn't be 0 here.
1137     if (ParenthesesDepth != 0) {
1138 
1139       // Lex the first token of the next macro parameter.
1140       RawLexer.LexFromRawLexer(TheTok);
1141 
1142       while (!(ParenthesesDepth == 1 &&
1143               (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
1144         assert(TheTok.isNot(tok::eof) &&
1145                "EOF encountered while looking for expanded macro args!");
1146 
1147         if (TheTok.is(tok::l_paren))
1148           ++ParenthesesDepth;
1149 
1150         if (TheTok.is(tok::r_paren))
1151           --ParenthesesDepth;
1152 
1153         if (ParenthesesDepth == 0)
1154           break;
1155 
1156         if (TheTok.is(tok::raw_identifier))
1157           PP.LookUpIdentifierInfo(TheTok);
1158 
1159         ExpandedArgTokens.push_back(TheTok);
1160         RawLexer.LexFromRawLexer(TheTok);
1161       }
1162     } else {
1163       assert(UnexpArgII == __VA_ARGS__II);
1164     }
1165 
1166     Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1167   }
1168 
1169   assert(TheTok.is(tok::r_paren) &&
1170          "Expanded macro argument acquisition failed! After the end of the loop"
1171          " this token should be ')'!");
1172 
1173   return { MacroName, MI, Args };
1174 }
1175 
1176 static MacroInfo::tokens_iterator getMatchingRParen(
1177     MacroInfo::tokens_iterator It,
1178     MacroInfo::tokens_iterator End) {
1179 
1180   assert(It->is(tok::l_paren) && "This token should be '('!");
1181 
1182   // Skip until we find the closing ')'.
1183   int ParenthesesDepth = 1;
1184   while (ParenthesesDepth != 0) {
1185     ++It;
1186 
1187     assert(It->isNot(tok::eof) &&
1188            "Encountered EOF while attempting to skip macro arguments!");
1189     assert(It != End &&
1190            "End of the macro definition reached before finding ')'!");
1191 
1192     if (It->is(tok::l_paren))
1193       ++ParenthesesDepth;
1194 
1195     if (It->is(tok::r_paren))
1196       --ParenthesesDepth;
1197   }
1198   return It;
1199 }
1200 
1201 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1202                                                 const SourceManager &SM,
1203                                                 const IdentifierInfo *II,
1204                                                 SourceLocation Loc) {
1205 
1206   const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1207   if (!MD)
1208     return nullptr;
1209 
1210   return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1211 }
1212 
1213 void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1214 
1215   for (value_type &Pair : *this) {
1216     ExpArgTokens &CurrExpArgTokens = Pair.second;
1217 
1218     // For each token in the expanded macro argument.
1219     auto It = CurrExpArgTokens.begin();
1220     while (It != CurrExpArgTokens.end()) {
1221       if (It->isNot(tok::identifier)) {
1222         ++It;
1223         continue;
1224       }
1225 
1226       const auto *II = It->getIdentifierInfo();
1227       assert(II);
1228 
1229       // Is this an argument that "Super" expands further?
1230       if (!Super.count(II)) {
1231         ++It;
1232         continue;
1233       }
1234 
1235       const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1236 
1237       It = CurrExpArgTokens.insert(
1238           It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1239       std::advance(It, SuperExpArgTokens.size());
1240       It = CurrExpArgTokens.erase(It);
1241     }
1242   }
1243 }
1244 
1245 void TokenPrinter::printToken(const Token &Tok) {
1246   // If this is the first token to be printed, don't print space.
1247   if (PrevTok.isNot(tok::unknown)) {
1248     // If the tokens were already space separated, or if they must be to avoid
1249     // them being implicitly pasted, add a space between them.
1250     if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1251                                                        Tok)) {
1252       // AvoidConcat doesn't check for ##, don't print a space around it.
1253       if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1254         OS << ' ';
1255       }
1256     }
1257   }
1258 
1259   if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1260     if (PrevTok.is(tok::hash))
1261       OS << '\"' << PP.getSpelling(Tok) << '\"';
1262     else
1263       OS << PP.getSpelling(Tok);
1264   }
1265 
1266   PrevPrevTok = PrevTok;
1267   PrevTok = Tok;
1268 }
1269