1218887Sdim//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===//
2218887Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6218887Sdim//
7218887Sdim//===----------------------------------------------------------------------===//
8218887Sdim//
9218887Sdim//  This file defines the PlistDiagnostics object.
10218887Sdim//
11218887Sdim//===----------------------------------------------------------------------===//
12218887Sdim
13360784Sdim#include "clang/Analysis/PathDiagnostic.h"
14243830Sdim#include "clang/Basic/FileManager.h"
15276479Sdim#include "clang/Basic/PlistSupport.h"
16218887Sdim#include "clang/Basic/SourceManager.h"
17243830Sdim#include "clang/Basic/Version.h"
18360784Sdim#include "clang/CrossTU/CrossTranslationUnit.h"
19360784Sdim#include "clang/Frontend/ASTUnit.h"
20218887Sdim#include "clang/Lex/Preprocessor.h"
21344779Sdim#include "clang/Lex/TokenConcatenation.h"
22341825Sdim#include "clang/Rewrite/Core/HTMLRewrite.h"
23341825Sdim#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
24296417Sdim#include "clang/StaticAnalyzer/Core/IssueHash.h"
25249423Sdim#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
26353358Sdim#include "llvm/ADT/SmallPtrSet.h"
27218887Sdim#include "llvm/ADT/SmallVector.h"
28360784Sdim#include "llvm/ADT/Statistic.h"
29249423Sdim#include "llvm/Support/Casting.h"
30344779Sdim
31218887Sdimusing namespace clang;
32218887Sdimusing namespace ento;
33276479Sdimusing namespace markup;
34218887Sdim
35344779Sdim//===----------------------------------------------------------------------===//
36344779Sdim// Declarations of helper classes and functions for emitting bug reports in
37344779Sdim// plist format.
38344779Sdim//===----------------------------------------------------------------------===//
39344779Sdim
40218887Sdimnamespace {
41226633Sdim  class PlistDiagnostics : public PathDiagnosticConsumer {
42218887Sdim    const std::string OutputFile;
43344779Sdim    const Preprocessor &PP;
44360784Sdim    const cross_tu::CrossTranslationUnitContext &CTU;
45344779Sdim    AnalyzerOptions &AnOpts;
46234353Sdim    const bool SupportsCrossFileDiagnostics;
47218887Sdim  public:
48360784Sdim    PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &prefix,
49344779Sdim                     const Preprocessor &PP,
50360784Sdim                     const cross_tu::CrossTranslationUnitContext &CTU,
51239462Sdim                     bool supportsMultipleFiles);
52218887Sdim
53288943Sdim    ~PlistDiagnostics() override {}
54218887Sdim
55234353Sdim    void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
56276479Sdim                              FilesMade *filesMade) override;
57276479Sdim
58280031Sdim    StringRef getName() const override {
59218887Sdim      return "PlistDiagnostics";
60218887Sdim    }
61218887Sdim
62276479Sdim    PathGenerationScheme getGenerationScheme() const override {
63276479Sdim      return Extensive;
64276479Sdim    }
65276479Sdim    bool supportsLogicalOpControlFlow() const override { return true; }
66276479Sdim    bool supportsCrossFileDiagnostics() const override {
67234353Sdim      return SupportsCrossFileDiagnostics;
68234353Sdim    }
69218887Sdim  };
70218887Sdim} // end anonymous namespace
71218887Sdim
72344779Sdimnamespace {
73218887Sdim
74344779Sdim/// A helper class for emitting a single report.
75344779Sdimclass PlistPrinter {
76344779Sdim  const FIDMap& FM;
77344779Sdim  AnalyzerOptions &AnOpts;
78344779Sdim  const Preprocessor &PP;
79360784Sdim  const cross_tu::CrossTranslationUnitContext &CTU;
80344779Sdim  llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;
81218887Sdim
82344779Sdimpublic:
83344779Sdim  PlistPrinter(const FIDMap& FM, AnalyzerOptions &AnOpts,
84360784Sdim               const Preprocessor &PP,
85360784Sdim               const cross_tu::CrossTranslationUnitContext &CTU)
86360784Sdim    : FM(FM), AnOpts(AnOpts), PP(PP), CTU(CTU) {
87344779Sdim  }
88234353Sdim
89344779Sdim  void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
90344779Sdim    ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
91341825Sdim
92344779Sdim    // Don't emit a warning about an unused private field.
93344779Sdim    (void)AnOpts;
94344779Sdim  }
95344779Sdim
96344779Sdim  /// Print the expansions of the collected macro pieces.
97344779Sdim  ///
98344779Sdim  /// Each time ReportDiag is called on a PathDiagnosticMacroPiece (or, if one
99344779Sdim  /// is found through a call piece, etc), it's subpieces are reported, and the
100344779Sdim  /// piece itself is collected. Call this function after the entire bugpath
101344779Sdim  /// was reported.
102344779Sdim  void ReportMacroExpansions(raw_ostream &o, unsigned indent);
103344779Sdim
104344779Sdimprivate:
105344779Sdim  void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P,
106344779Sdim                   unsigned indent, unsigned depth, bool includeControlFlow,
107344779Sdim                   bool isKeyEvent = false) {
108344779Sdim    switch (P.getKind()) {
109344779Sdim      case PathDiagnosticPiece::ControlFlow:
110344779Sdim        if (includeControlFlow)
111344779Sdim          ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
112344779Sdim        break;
113344779Sdim      case PathDiagnosticPiece::Call:
114344779Sdim        ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
115344779Sdim                   depth);
116344779Sdim        break;
117344779Sdim      case PathDiagnosticPiece::Event:
118344779Sdim        ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
119344779Sdim                    isKeyEvent);
120344779Sdim        break;
121344779Sdim      case PathDiagnosticPiece::Macro:
122344779Sdim        ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
123344779Sdim                             depth);
124344779Sdim        break;
125344779Sdim      case PathDiagnosticPiece::Note:
126344779Sdim        ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
127344779Sdim        break;
128353358Sdim      case PathDiagnosticPiece::PopUp:
129353358Sdim        ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
130353358Sdim        break;
131344779Sdim    }
132344779Sdim  }
133344779Sdim
134344779Sdim  void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
135344779Sdim                  unsigned indent);
136344779Sdim  void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
137360784Sdim  void EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits, unsigned indent);
138344779Sdim
139344779Sdim  void ReportControlFlow(raw_ostream &o,
140344779Sdim                         const PathDiagnosticControlFlowPiece& P,
141344779Sdim                         unsigned indent);
142344779Sdim  void ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
143344779Sdim                   unsigned indent, unsigned depth, bool isKeyEvent = false);
144344779Sdim  void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
145344779Sdim                  unsigned indent, unsigned depth);
146344779Sdim  void ReportMacroSubPieces(raw_ostream &o, const PathDiagnosticMacroPiece& P,
147344779Sdim                            unsigned indent, unsigned depth);
148344779Sdim  void ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
149344779Sdim                  unsigned indent);
150353358Sdim
151353358Sdim  void ReportPopUp(raw_ostream &o, const PathDiagnosticPopUpPiece &P,
152353358Sdim                   unsigned indent);
153344779Sdim};
154344779Sdim
155344779Sdim} // end of anonymous namespace
156344779Sdim
157344779Sdimnamespace {
158344779Sdim
159344779Sdimstruct ExpansionInfo {
160344779Sdim  std::string MacroName;
161344779Sdim  std::string Expansion;
162344779Sdim  ExpansionInfo(std::string N, std::string E)
163344779Sdim    : MacroName(std::move(N)), Expansion(std::move(E)) {}
164344779Sdim};
165344779Sdim
166344779Sdim} // end of anonymous namespace
167344779Sdim
168344779Sdimstatic void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
169360784Sdim                         AnalyzerOptions &AnOpts, const Preprocessor &PP,
170360784Sdim                         const cross_tu::CrossTranslationUnitContext &CTU,
171344779Sdim                         const PathPieces &Path);
172344779Sdim
173344779Sdim/// Print coverage information to output stream {@code o}.
174344779Sdim/// May modify the used list of files {@code Fids} by inserting new ones.
175344779Sdimstatic void printCoverage(const PathDiagnostic *D,
176344779Sdim                          unsigned InputIndentLevel,
177344779Sdim                          SmallVectorImpl<FileID> &Fids,
178344779Sdim                          FIDMap &FM,
179344779Sdim                          llvm::raw_fd_ostream &o);
180344779Sdim
181360784Sdimstatic ExpansionInfo
182360784SdimgetExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
183360784Sdim                 const cross_tu::CrossTranslationUnitContext &CTU);
184344779Sdim
185344779Sdim//===----------------------------------------------------------------------===//
186344779Sdim// Methods of PlistPrinter.
187344779Sdim//===----------------------------------------------------------------------===//
188344779Sdim
189344779Sdimvoid PlistPrinter::EmitRanges(raw_ostream &o,
190344779Sdim                              const ArrayRef<SourceRange> Ranges,
191344779Sdim                              unsigned indent) {
192344779Sdim
193341825Sdim  if (Ranges.empty())
194341825Sdim    return;
195341825Sdim
196341825Sdim  Indent(o, indent) << "<key>ranges</key>\n";
197341825Sdim  Indent(o, indent) << "<array>\n";
198341825Sdim  ++indent;
199344779Sdim
200344779Sdim  const SourceManager &SM = PP.getSourceManager();
201344779Sdim  const LangOptions &LangOpts = PP.getLangOpts();
202344779Sdim
203341825Sdim  for (auto &R : Ranges)
204341825Sdim    EmitRange(o, SM,
205341825Sdim              Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
206341825Sdim              FM, indent + 1);
207341825Sdim  --indent;
208341825Sdim  Indent(o, indent) << "</array>\n";
209341825Sdim}
210341825Sdim
211344779Sdimvoid PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
212344779Sdim                               unsigned indent) {
213341825Sdim  // Output the text.
214341825Sdim  assert(!Message.empty());
215341825Sdim  Indent(o, indent) << "<key>extended_message</key>\n";
216341825Sdim  Indent(o, indent);
217341825Sdim  EmitString(o, Message) << '\n';
218341825Sdim
219341825Sdim  // Output the short text.
220341825Sdim  // FIXME: Really use a short string.
221341825Sdim  Indent(o, indent) << "<key>message</key>\n";
222341825Sdim  Indent(o, indent);
223341825Sdim  EmitString(o, Message) << '\n';
224341825Sdim}
225341825Sdim
226360784Sdimvoid PlistPrinter::EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits,
227360784Sdim                              unsigned indent) {
228360784Sdim  if (fixits.size() == 0)
229360784Sdim    return;
230360784Sdim
231360784Sdim  const SourceManager &SM = PP.getSourceManager();
232360784Sdim  const LangOptions &LangOpts = PP.getLangOpts();
233360784Sdim
234360784Sdim  Indent(o, indent) << "<key>fixits</key>\n";
235360784Sdim  Indent(o, indent) << "<array>\n";
236360784Sdim  for (const auto &fixit : fixits) {
237360784Sdim    assert(!fixit.isNull());
238360784Sdim    // FIXME: Add support for InsertFromRange and BeforePreviousInsertion.
239360784Sdim    assert(!fixit.InsertFromRange.isValid() && "Not implemented yet!");
240360784Sdim    assert(!fixit.BeforePreviousInsertions && "Not implemented yet!");
241360784Sdim    Indent(o, indent) << " <dict>\n";
242360784Sdim    Indent(o, indent) << "  <key>remove_range</key>\n";
243360784Sdim    EmitRange(o, SM, Lexer::getAsCharRange(fixit.RemoveRange, SM, LangOpts),
244360784Sdim              FM, indent + 2);
245360784Sdim    Indent(o, indent) << "  <key>insert_string</key>";
246360784Sdim    EmitString(o, fixit.CodeToInsert);
247360784Sdim    o << "\n";
248360784Sdim    Indent(o, indent) << " </dict>\n";
249360784Sdim  }
250360784Sdim  Indent(o, indent) << "</array>\n";
251360784Sdim}
252360784Sdim
253344779Sdimvoid PlistPrinter::ReportControlFlow(raw_ostream &o,
254344779Sdim                                     const PathDiagnosticControlFlowPiece& P,
255344779Sdim                                     unsigned indent) {
256218887Sdim
257344779Sdim  const SourceManager &SM = PP.getSourceManager();
258344779Sdim  const LangOptions &LangOpts = PP.getLangOpts();
259344779Sdim
260218887Sdim  Indent(o, indent) << "<dict>\n";
261218887Sdim  ++indent;
262218887Sdim
263218887Sdim  Indent(o, indent) << "<key>kind</key><string>control</string>\n";
264218887Sdim
265218887Sdim  // Emit edges.
266218887Sdim  Indent(o, indent) << "<key>edges</key>\n";
267218887Sdim  ++indent;
268218887Sdim  Indent(o, indent) << "<array>\n";
269218887Sdim  ++indent;
270218887Sdim  for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
271218887Sdim       I!=E; ++I) {
272218887Sdim    Indent(o, indent) << "<dict>\n";
273218887Sdim    ++indent;
274239462Sdim
275239462Sdim    // Make the ranges of the start and end point self-consistent with adjacent edges
276239462Sdim    // by forcing to use only the beginning of the range.  This simplifies the layout
277239462Sdim    // logic for clients.
278218887Sdim    Indent(o, indent) << "<key>start</key>\n";
279288943Sdim    SourceRange StartEdge(
280288943Sdim        SM.getExpansionLoc(I->getStart().asRange().getBegin()));
281288943Sdim    EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
282276479Sdim              indent + 1);
283239462Sdim
284218887Sdim    Indent(o, indent) << "<key>end</key>\n";
285288943Sdim    SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
286288943Sdim    EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
287276479Sdim              indent + 1);
288239462Sdim
289218887Sdim    --indent;
290218887Sdim    Indent(o, indent) << "</dict>\n";
291218887Sdim  }
292218887Sdim  --indent;
293218887Sdim  Indent(o, indent) << "</array>\n";
294218887Sdim  --indent;
295218887Sdim
296218887Sdim  // Output any helper text.
297309124Sdim  const auto &s = P.getString();
298218887Sdim  if (!s.empty()) {
299218887Sdim    Indent(o, indent) << "<key>alternate</key>";
300218887Sdim    EmitString(o, s) << '\n';
301218887Sdim  }
302218887Sdim
303360784Sdim  assert(P.getFixits().size() == 0 &&
304360784Sdim         "Fixits on constrol flow pieces are not implemented yet!");
305360784Sdim
306218887Sdim  --indent;
307218887Sdim  Indent(o, indent) << "</dict>\n";
308218887Sdim}
309218887Sdim
310344779Sdimvoid PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
311344779Sdim                               unsigned indent, unsigned depth,
312344779Sdim                               bool isKeyEvent) {
313218887Sdim
314344779Sdim  const SourceManager &SM = PP.getSourceManager();
315344779Sdim
316218887Sdim  Indent(o, indent) << "<dict>\n";
317218887Sdim  ++indent;
318218887Sdim
319218887Sdim  Indent(o, indent) << "<key>kind</key><string>event</string>\n";
320218887Sdim
321261991Sdim  if (isKeyEvent) {
322261991Sdim    Indent(o, indent) << "<key>key_event</key><true/>\n";
323261991Sdim  }
324261991Sdim
325218887Sdim  // Output the location.
326218887Sdim  FullSourceLoc L = P.getLocation().asLocation();
327218887Sdim
328218887Sdim  Indent(o, indent) << "<key>location</key>\n";
329288943Sdim  EmitLocation(o, SM, L, FM, indent);
330218887Sdim
331218887Sdim  // Output the ranges (if any).
332239462Sdim  ArrayRef<SourceRange> Ranges = P.getRanges();
333344779Sdim  EmitRanges(o, Ranges, indent);
334218887Sdim
335234353Sdim  // Output the call depth.
336276479Sdim  Indent(o, indent) << "<key>depth</key>";
337276479Sdim  EmitInteger(o, depth) << '\n';
338218887Sdim
339218887Sdim  // Output the text.
340341825Sdim  EmitMessage(o, P.getString(), indent);
341218887Sdim
342360784Sdim  // Output the fixits.
343360784Sdim  EmitFixits(o, P.getFixits(), indent);
344360784Sdim
345218887Sdim  // Finish up.
346218887Sdim  --indent;
347218887Sdim  Indent(o, indent); o << "</dict>\n";
348218887Sdim}
349218887Sdim
350344779Sdimvoid PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
351344779Sdim                              unsigned indent,
352344779Sdim                              unsigned depth) {
353234353Sdim
354314564Sdim  if (auto callEnter = P.getCallEnterEvent())
355344779Sdim    ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
356261991Sdim                P.isLastInMainSourceFile());
357234353Sdim
358296417Sdim
359234353Sdim  ++depth;
360296417Sdim
361314564Sdim  if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
362344779Sdim    ReportPiece(o, *callEnterWithinCaller, indent, depth,
363344779Sdim                /*includeControlFlow*/ true);
364296417Sdim
365234353Sdim  for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
366344779Sdim    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
367251662Sdim
368251662Sdim  --depth;
369296417Sdim
370314564Sdim  if (auto callExit = P.getCallExitEvent())
371344779Sdim    ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
372360784Sdim
373360784Sdim  assert(P.getFixits().size() == 0 &&
374360784Sdim         "Fixits on call pieces are not implemented yet!");
375234353Sdim}
376234353Sdim
377344779Sdimvoid PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
378344779Sdim                                        const PathDiagnosticMacroPiece& P,
379344779Sdim                                        unsigned indent, unsigned depth) {
380344779Sdim  MacroPieces.push_back(&P);
381218887Sdim
382344779Sdim  for (PathPieces::const_iterator I = P.subPieces.begin(),
383344779Sdim                                  E = P.subPieces.end();
384344779Sdim       I != E; ++I) {
385344779Sdim    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
386218887Sdim  }
387360784Sdim
388360784Sdim  assert(P.getFixits().size() == 0 &&
389360784Sdim         "Fixits on constrol flow pieces are not implemented yet!");
390218887Sdim}
391218887Sdim
392344779Sdimvoid PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
393341825Sdim
394344779Sdim  for (const PathDiagnosticMacroPiece *P : MacroPieces) {
395344779Sdim    const SourceManager &SM = PP.getSourceManager();
396360784Sdim    ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU);
397344779Sdim
398344779Sdim    Indent(o, indent) << "<dict>\n";
399344779Sdim    ++indent;
400344779Sdim
401344779Sdim    // Output the location.
402344779Sdim    FullSourceLoc L = P->getLocation().asLocation();
403344779Sdim
404344779Sdim    Indent(o, indent) << "<key>location</key>\n";
405344779Sdim    EmitLocation(o, SM, L, FM, indent);
406344779Sdim
407344779Sdim    // Output the ranges (if any).
408344779Sdim    ArrayRef<SourceRange> Ranges = P->getRanges();
409344779Sdim    EmitRanges(o, Ranges, indent);
410344779Sdim
411344779Sdim    // Output the macro name.
412344779Sdim    Indent(o, indent) << "<key>name</key>";
413344779Sdim    EmitString(o, EI.MacroName) << '\n';
414344779Sdim
415344779Sdim    // Output what it expands into.
416344779Sdim    Indent(o, indent) << "<key>expansion</key>";
417344779Sdim    EmitString(o, EI.Expansion) << '\n';
418344779Sdim
419344779Sdim    // Finish up.
420344779Sdim    --indent;
421344779Sdim    Indent(o, indent);
422344779Sdim    o << "</dict>\n";
423344779Sdim  }
424344779Sdim}
425344779Sdim
426344779Sdimvoid PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
427344779Sdim                              unsigned indent) {
428344779Sdim
429344779Sdim  const SourceManager &SM = PP.getSourceManager();
430344779Sdim
431341825Sdim  Indent(o, indent) << "<dict>\n";
432341825Sdim  ++indent;
433341825Sdim
434341825Sdim  // Output the location.
435341825Sdim  FullSourceLoc L = P.getLocation().asLocation();
436341825Sdim
437341825Sdim  Indent(o, indent) << "<key>location</key>\n";
438341825Sdim  EmitLocation(o, SM, L, FM, indent);
439341825Sdim
440341825Sdim  // Output the ranges (if any).
441341825Sdim  ArrayRef<SourceRange> Ranges = P.getRanges();
442344779Sdim  EmitRanges(o, Ranges, indent);
443341825Sdim
444341825Sdim  // Output the text.
445341825Sdim  EmitMessage(o, P.getString(), indent);
446341825Sdim
447360784Sdim  // Output the fixits.
448360784Sdim  EmitFixits(o, P.getFixits(), indent);
449360784Sdim
450341825Sdim  // Finish up.
451341825Sdim  --indent;
452341825Sdim  Indent(o, indent); o << "</dict>\n";
453341825Sdim}
454341825Sdim
455353358Sdimvoid PlistPrinter::ReportPopUp(raw_ostream &o,
456353358Sdim                               const PathDiagnosticPopUpPiece &P,
457353358Sdim                               unsigned indent) {
458353358Sdim  const SourceManager &SM = PP.getSourceManager();
459353358Sdim
460353358Sdim  Indent(o, indent) << "<dict>\n";
461353358Sdim  ++indent;
462353358Sdim
463353358Sdim  Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";
464353358Sdim
465353358Sdim  // Output the location.
466353358Sdim  FullSourceLoc L = P.getLocation().asLocation();
467353358Sdim
468353358Sdim  Indent(o, indent) << "<key>location</key>\n";
469353358Sdim  EmitLocation(o, SM, L, FM, indent);
470353358Sdim
471353358Sdim  // Output the ranges (if any).
472353358Sdim  ArrayRef<SourceRange> Ranges = P.getRanges();
473353358Sdim  EmitRanges(o, Ranges, indent);
474353358Sdim
475353358Sdim  // Output the text.
476353358Sdim  EmitMessage(o, P.getString(), indent);
477353358Sdim
478360784Sdim  assert(P.getFixits().size() == 0 &&
479360784Sdim         "Fixits on pop-up pieces are not implemented yet!");
480360784Sdim
481353358Sdim  // Finish up.
482353358Sdim  --indent;
483353358Sdim  Indent(o, indent) << "</dict>\n";
484353358Sdim}
485353358Sdim
486344779Sdim//===----------------------------------------------------------------------===//
487344779Sdim// Static function definitions.
488344779Sdim//===----------------------------------------------------------------------===//
489344779Sdim
490344779Sdim/// Print coverage information to output stream {@code o}.
491344779Sdim/// May modify the used list of files {@code Fids} by inserting new ones.
492344779Sdimstatic void printCoverage(const PathDiagnostic *D,
493344779Sdim                          unsigned InputIndentLevel,
494344779Sdim                          SmallVectorImpl<FileID> &Fids,
495344779Sdim                          FIDMap &FM,
496344779Sdim                          llvm::raw_fd_ostream &o) {
497344779Sdim  unsigned IndentLevel = InputIndentLevel;
498344779Sdim
499344779Sdim  Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
500344779Sdim  Indent(o, IndentLevel) << "<dict>\n";
501344779Sdim  IndentLevel++;
502344779Sdim
503344779Sdim  // Mapping from file IDs to executed lines.
504344779Sdim  const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
505344779Sdim  for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) {
506344779Sdim    unsigned FileKey = AddFID(FM, Fids, I->first);
507344779Sdim    Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
508344779Sdim    Indent(o, IndentLevel) << "<array>\n";
509344779Sdim    IndentLevel++;
510344779Sdim    for (unsigned LineNo : I->second) {
511344779Sdim      Indent(o, IndentLevel);
512344779Sdim      EmitInteger(o, LineNo) << "\n";
513344779Sdim    }
514344779Sdim    IndentLevel--;
515344779Sdim    Indent(o, IndentLevel) << "</array>\n";
516344779Sdim  }
517344779Sdim  IndentLevel--;
518344779Sdim  Indent(o, IndentLevel) << "</dict>\n";
519344779Sdim
520344779Sdim  assert(IndentLevel == InputIndentLevel);
521234353Sdim}
522218887Sdim
523344779Sdimstatic void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
524360784Sdim                         AnalyzerOptions &AnOpts, const Preprocessor &PP,
525360784Sdim                         const cross_tu::CrossTranslationUnitContext &CTU,
526344779Sdim                         const PathPieces &Path) {
527360784Sdim  PlistPrinter Printer(FM, AnOpts, PP, CTU);
528360784Sdim  assert(std::is_partitioned(Path.begin(), Path.end(),
529360784Sdim                             [](const PathDiagnosticPieceRef &E) {
530360784Sdim                               return E->getKind() == PathDiagnosticPiece::Note;
531360784Sdim                             }) &&
532344779Sdim         "PathDiagnostic is not partitioned so that notes precede the rest");
533344779Sdim
534344779Sdim  PathPieces::const_iterator FirstNonNote = std::partition_point(
535360784Sdim      Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) {
536360784Sdim        return E->getKind() == PathDiagnosticPiece::Note;
537360784Sdim      });
538344779Sdim
539344779Sdim  PathPieces::const_iterator I = Path.begin();
540344779Sdim
541344779Sdim  if (FirstNonNote != Path.begin()) {
542344779Sdim    o << "   <key>notes</key>\n"
543344779Sdim         "   <array>\n";
544344779Sdim
545344779Sdim    for (; I != FirstNonNote; ++I)
546344779Sdim      Printer.ReportDiag(o, **I);
547344779Sdim
548344779Sdim    o << "   </array>\n";
549218887Sdim  }
550344779Sdim
551344779Sdim  o << "   <key>path</key>\n";
552344779Sdim
553344779Sdim  o << "   <array>\n";
554344779Sdim
555344779Sdim  for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
556344779Sdim    Printer.ReportDiag(o, **I);
557344779Sdim
558344779Sdim  o << "   </array>\n";
559344779Sdim
560344779Sdim  if (!AnOpts.ShouldDisplayMacroExpansions)
561344779Sdim    return;
562344779Sdim
563344779Sdim  o << "   <key>macro_expansions</key>\n"
564344779Sdim       "   <array>\n";
565344779Sdim  Printer.ReportMacroExpansions(o, /* indent */ 4);
566344779Sdim  o << "   </array>\n";
567218887Sdim}
568218887Sdim
569344779Sdim//===----------------------------------------------------------------------===//
570344779Sdim// Methods of PlistDiagnostics.
571344779Sdim//===----------------------------------------------------------------------===//
572344779Sdim
573360784SdimPlistDiagnostics::PlistDiagnostics(
574360784Sdim    AnalyzerOptions &AnalyzerOpts, const std::string &output,
575360784Sdim    const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU,
576360784Sdim    bool supportsMultipleFiles)
577360784Sdim    : OutputFile(output), PP(PP), CTU(CTU), AnOpts(AnalyzerOpts),
578360784Sdim      SupportsCrossFileDiagnostics(supportsMultipleFiles) {
579360784Sdim  // FIXME: Will be used by a later planned change.
580360784Sdim  (void)this->CTU;
581360784Sdim}
582344779Sdim
583360784Sdimvoid ento::createPlistDiagnosticConsumer(
584360784Sdim    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
585360784Sdim    const std::string &s, const Preprocessor &PP,
586360784Sdim    const cross_tu::CrossTranslationUnitContext &CTU) {
587360784Sdim  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
588344779Sdim                                   /*supportsMultipleFiles*/ false));
589344779Sdim}
590344779Sdim
591360784Sdimvoid ento::createPlistMultiFileDiagnosticConsumer(
592360784Sdim    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
593360784Sdim    const std::string &s, const Preprocessor &PP,
594360784Sdim    const cross_tu::CrossTranslationUnitContext &CTU) {
595360784Sdim  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
596344779Sdim                                   /*supportsMultipleFiles*/ true));
597344779Sdim}
598234353Sdimvoid PlistDiagnostics::FlushDiagnosticsImpl(
599234353Sdim                                    std::vector<const PathDiagnostic *> &Diags,
600239462Sdim                                    FilesMade *filesMade) {
601218887Sdim  // Build up a set of FIDs that we use by scanning the locations and
602218887Sdim  // ranges of the diagnostics.
603218887Sdim  FIDMap FM;
604226633Sdim  SmallVector<FileID, 10> Fids;
605344779Sdim  const SourceManager& SM = PP.getSourceManager();
606344779Sdim  const LangOptions &LangOpts = PP.getLangOpts();
607218887Sdim
608344779Sdim  auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
609344779Sdim    AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
610314564Sdim    ArrayRef<SourceRange> Ranges = Piece.getRanges();
611314564Sdim    for (const SourceRange &Range : Ranges) {
612344779Sdim      AddFID(FM, Fids, SM, Range.getBegin());
613344779Sdim      AddFID(FM, Fids, SM, Range.getEnd());
614314564Sdim    }
615314564Sdim  };
616296417Sdim
617314564Sdim  for (const PathDiagnostic *D : Diags) {
618218887Sdim
619249423Sdim    SmallVector<const PathPieces *, 5> WorkList;
620234353Sdim    WorkList.push_back(&D->path);
621218887Sdim
622234353Sdim    while (!WorkList.empty()) {
623314564Sdim      const PathPieces &Path = *WorkList.pop_back_val();
624261991Sdim
625314564Sdim      for (const auto &Iter : Path) {
626314564Sdim        const PathDiagnosticPiece &Piece = *Iter;
627314564Sdim        AddPieceFID(Piece);
628234353Sdim
629314564Sdim        if (const PathDiagnosticCallPiece *Call =
630314564Sdim                dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
631314564Sdim          if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
632314564Sdim            AddPieceFID(*CallEnterWithin);
633239462Sdim
634314564Sdim          if (auto CallEnterEvent = Call->getCallEnterEvent())
635314564Sdim            AddPieceFID(*CallEnterEvent);
636314564Sdim
637314564Sdim          WorkList.push_back(&Call->path);
638314564Sdim        } else if (const PathDiagnosticMacroPiece *Macro =
639314564Sdim                       dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
640314564Sdim          WorkList.push_back(&Macro->subPieces);
641234353Sdim        }
642218887Sdim      }
643218887Sdim    }
644218887Sdim  }
645218887Sdim
646218887Sdim  // Open the file.
647280031Sdim  std::error_code EC;
648360784Sdim  llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
649280031Sdim  if (EC) {
650280031Sdim    llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
651218887Sdim    return;
652218887Sdim  }
653218887Sdim
654276479Sdim  EmitPlistHeader(o);
655218887Sdim
656218887Sdim  // Write the root object: a <dict> containing...
657243830Sdim  //  - "clang_version", the string representation of clang version
658218887Sdim  //  - "files", an <array> mapping from FIDs to file names
659218887Sdim  //  - "diagnostics", an <array> containing the path diagnostics
660243830Sdim  o << "<dict>\n" <<
661243830Sdim       " <key>clang_version</key>\n";
662243830Sdim  EmitString(o, getClangFullVersion()) << '\n';
663344779Sdim  o << " <key>diagnostics</key>\n"
664218887Sdim       " <array>\n";
665218887Sdim
666234353Sdim  for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
667234353Sdim       DE = Diags.end(); DI!=DE; ++DI) {
668218887Sdim
669341825Sdim    o << "  <dict>\n";
670218887Sdim
671218887Sdim    const PathDiagnostic *D = *DI;
672360784Sdim    printBugPath(o, FM, AnOpts, PP, CTU, D->path);
673218887Sdim
674218887Sdim    // Output the bug type and bug category.
675218887Sdim    o << "   <key>description</key>";
676243830Sdim    EmitString(o, D->getShortDescription()) << '\n';
677218887Sdim    o << "   <key>category</key>";
678218887Sdim    EmitString(o, D->getCategory()) << '\n';
679218887Sdim    o << "   <key>type</key>";
680218887Sdim    EmitString(o, D->getBugType()) << '\n';
681288943Sdim    o << "   <key>check_name</key>";
682360784Sdim    EmitString(o, D->getCheckerName()) << '\n';
683296417Sdim
684296417Sdim    o << "   <!-- This hash is experimental and going to change! -->\n";
685296417Sdim    o << "   <key>issue_hash_content_of_line_in_context</key>";
686296417Sdim    PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
687344779Sdim    FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
688296417Sdim                                            ? UPDLoc.asLocation()
689296417Sdim                                            : D->getLocation().asLocation()),
690344779Sdim                    SM);
691296417Sdim    const Decl *DeclWithIssue = D->getDeclWithIssue();
692360784Sdim    EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
693296417Sdim                               DeclWithIssue, LangOpts))
694296417Sdim        << '\n';
695296417Sdim
696234353Sdim    // Output information about the semantic context where
697234353Sdim    // the issue occurred.
698234353Sdim    if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
699234353Sdim      // FIXME: handle blocks, which have no name.
700234353Sdim      if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
701234353Sdim        StringRef declKind;
702234353Sdim        switch (ND->getKind()) {
703234353Sdim          case Decl::CXXRecord:
704234353Sdim            declKind = "C++ class";
705234353Sdim            break;
706234353Sdim          case Decl::CXXMethod:
707234353Sdim            declKind = "C++ method";
708234353Sdim            break;
709234353Sdim          case Decl::ObjCMethod:
710234353Sdim            declKind = "Objective-C method";
711234353Sdim            break;
712234353Sdim          case Decl::Function:
713234353Sdim            declKind = "function";
714234353Sdim            break;
715234353Sdim          default:
716234353Sdim            break;
717234353Sdim        }
718234353Sdim        if (!declKind.empty()) {
719234353Sdim          const std::string &declName = ND->getDeclName().getAsString();
720234353Sdim          o << "  <key>issue_context_kind</key>";
721234353Sdim          EmitString(o, declKind) << '\n';
722234353Sdim          o << "  <key>issue_context</key>";
723234353Sdim          EmitString(o, declName) << '\n';
724234353Sdim        }
725239462Sdim
726239462Sdim        // Output the bug hash for issue unique-ing. Currently, it's just an
727239462Sdim        // offset from the beginning of the function.
728239462Sdim        if (const Stmt *Body = DeclWithIssue->getBody()) {
729296417Sdim
730249423Sdim          // If the bug uniqueing location exists, use it for the hash.
731249423Sdim          // For example, this ensures that two leaks reported on the same line
732249423Sdim          // will have different issue_hashes and that the hash will identify
733249423Sdim          // the leak location even after code is added between the allocation
734249423Sdim          // site and the end of scope (leak report location).
735249423Sdim          if (UPDLoc.isValid()) {
736344779Sdim            FullSourceLoc UFunL(
737344779Sdim                SM.getExpansionLoc(
738344779Sdim                    D->getUniqueingDecl()->getBody()->getBeginLoc()),
739344779Sdim                SM);
740296417Sdim            o << "  <key>issue_hash_function_offset</key><string>"
741296417Sdim              << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
742249423Sdim              << "</string>\n";
743249423Sdim
744249423Sdim          // Otherwise, use the location on which the bug is reported.
745249423Sdim          } else {
746344779Sdim            FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
747296417Sdim            o << "  <key>issue_hash_function_offset</key><string>"
748249423Sdim              << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
749249423Sdim              << "</string>\n";
750249423Sdim          }
751249423Sdim
752239462Sdim        }
753234353Sdim      }
754234353Sdim    }
755218887Sdim
756218887Sdim    // Output the location of the bug.
757218887Sdim    o << "  <key>location</key>\n";
758344779Sdim    EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
759218887Sdim
760218887Sdim    // Output the diagnostic to the sub-diagnostic client, if any.
761239462Sdim    if (!filesMade->empty()) {
762239462Sdim      StringRef lastName;
763243830Sdim      PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
764243830Sdim      if (files) {
765243830Sdim        for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
766243830Sdim                CE = files->end(); CI != CE; ++CI) {
767243830Sdim          StringRef newName = CI->first;
768243830Sdim          if (newName != lastName) {
769243830Sdim            if (!lastName.empty()) {
770243830Sdim              o << "  </array>\n";
771243830Sdim            }
772243830Sdim            lastName = newName;
773243830Sdim            o <<  "  <key>" << lastName << "_files</key>\n";
774243830Sdim            o << "  <array>\n";
775243830Sdim          }
776243830Sdim          o << "   <string>" << CI->second << "</string>\n";
777239462Sdim        }
778243830Sdim        o << "  </array>\n";
779218887Sdim      }
780218887Sdim    }
781218887Sdim
782344779Sdim    printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
783344779Sdim
784218887Sdim    // Close up the entry.
785218887Sdim    o << "  </dict>\n";
786218887Sdim  }
787218887Sdim
788218887Sdim  o << " </array>\n";
789218887Sdim
790344779Sdim  o << " <key>files</key>\n"
791344779Sdim       " <array>\n";
792344779Sdim  for (FileID FID : Fids)
793344779Sdim    EmitString(o << "  ", SM.getFileEntryForID(FID)->getName()) << '\n';
794344779Sdim  o << " </array>\n";
795344779Sdim
796344779Sdim  if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
797341825Sdim    o << " <key>statistics</key>\n";
798341825Sdim    std::string stats;
799341825Sdim    llvm::raw_string_ostream os(stats);
800341825Sdim    llvm::PrintStatisticsJSON(os);
801341825Sdim    os.flush();
802341825Sdim    EmitString(o, html::EscapeText(stats)) << '\n';
803341825Sdim  }
804341825Sdim
805218887Sdim  // Finish.
806353358Sdim  o << "</dict>\n</plist>\n";
807218887Sdim}
808344779Sdim
809344779Sdim//===----------------------------------------------------------------------===//
810344779Sdim// Declarations of helper functions and data structures for expanding macros.
811344779Sdim//===----------------------------------------------------------------------===//
812344779Sdim
813344779Sdimnamespace {
814344779Sdim
815344779Sdimusing ExpArgTokens = llvm::SmallVector<Token, 2>;
816344779Sdim
817344779Sdim/// Maps unexpanded macro arguments to expanded arguments. A macro argument may
818344779Sdim/// need to expanded further when it is nested inside another macro.
819344779Sdimclass MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
820344779Sdimpublic:
821344779Sdim  void expandFromPrevMacro(const MacroArgMap &Super);
822344779Sdim};
823344779Sdim
824344779Sdimstruct MacroNameAndArgs {
825344779Sdim  std::string Name;
826344779Sdim  const MacroInfo *MI = nullptr;
827344779Sdim  MacroArgMap Args;
828344779Sdim
829344779Sdim  MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
830344779Sdim    : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
831344779Sdim};
832344779Sdim
833344779Sdimclass TokenPrinter {
834344779Sdim  llvm::raw_ostream &OS;
835344779Sdim  const Preprocessor &PP;
836344779Sdim
837344779Sdim  Token PrevTok, PrevPrevTok;
838344779Sdim  TokenConcatenation ConcatInfo;
839344779Sdim
840344779Sdimpublic:
841344779Sdim  TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
842344779Sdim    : OS(OS), PP(PP), ConcatInfo(PP) {
843344779Sdim    PrevTok.setKind(tok::unknown);
844344779Sdim    PrevPrevTok.setKind(tok::unknown);
845344779Sdim  }
846344779Sdim
847344779Sdim  void printToken(const Token &Tok);
848344779Sdim};
849344779Sdim
850344779Sdim} // end of anonymous namespace
851344779Sdim
852344779Sdim/// The implementation method of getMacroExpansion: It prints the expansion of
853344779Sdim/// a macro to \p Printer, and returns with the name of the macro.
854344779Sdim///
855344779Sdim/// Since macros can be nested in one another, this function may call itself
856344779Sdim/// recursively.
857344779Sdim///
858344779Sdim/// Unfortunately, macro arguments have to expanded manually. To understand why,
859344779Sdim/// observe the following example:
860344779Sdim///
861344779Sdim///   #define PRINT(x) print(x)
862344779Sdim///   #define DO_SOMETHING(str) PRINT(str)
863344779Sdim///
864344779Sdim///   DO_SOMETHING("Cute panda cubs.");
865344779Sdim///
866344779Sdim/// As we expand the last line, we'll immediately replace PRINT(str) with
867344779Sdim/// print(x). The information that both 'str' and 'x' refers to the same string
868344779Sdim/// is an information we have to forward, hence the argument \p PrevArgs.
869353358Sdim///
870353358Sdim/// To avoid infinite recursion we maintain the already processed tokens in
871353358Sdim/// a set. This is carried as a parameter through the recursive calls. The set
872353358Sdim/// is extended with the currently processed token and after processing it, the
873353358Sdim/// token is removed. If the token is already in the set, then recursion stops:
874353358Sdim///
875353358Sdim/// #define f(y) x
876353358Sdim/// #define x f(x)
877353358Sdimstatic std::string getMacroNameAndPrintExpansion(
878353358Sdim    TokenPrinter &Printer,
879353358Sdim    SourceLocation MacroLoc,
880353358Sdim    const Preprocessor &PP,
881353358Sdim    const MacroArgMap &PrevArgs,
882353358Sdim    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);
883344779Sdim
884344779Sdim/// Retrieves the name of the macro and what it's arguments expand into
885344779Sdim/// at \p ExpanLoc.
886344779Sdim///
887344779Sdim/// For example, for the following macro expansion:
888344779Sdim///
889344779Sdim///   #define SET_TO_NULL(x) x = 0
890344779Sdim///   #define NOT_SUSPICIOUS(a) \
891344779Sdim///     {                       \
892344779Sdim///       int b = 0;            \
893344779Sdim///     }                       \
894344779Sdim///     SET_TO_NULL(a)
895344779Sdim///
896344779Sdim///   int *ptr = new int(4);
897344779Sdim///   NOT_SUSPICIOUS(&ptr);
898344779Sdim///   *ptr = 5;
899344779Sdim///
900344779Sdim/// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
901344779Sdim/// and the MacroArgMap map { (a, &ptr) } will be returned.
902344779Sdim///
903344779Sdim/// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
904344779Sdim/// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
905344779Sdim/// { (x, a) } will be returned.
906344779Sdimstatic MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
907344779Sdim                                            const Preprocessor &PP);
908344779Sdim
909344779Sdim/// Retrieves the ')' token that matches '(' \p It points to.
910344779Sdimstatic MacroInfo::tokens_iterator getMatchingRParen(
911344779Sdim    MacroInfo::tokens_iterator It,
912344779Sdim    MacroInfo::tokens_iterator End);
913344779Sdim
914344779Sdim/// Retrieves the macro info for \p II refers to at \p Loc. This is important
915344779Sdim/// because macros can be redefined or undefined.
916344779Sdimstatic const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
917344779Sdim                                                const SourceManager &SM,
918344779Sdim                                                const IdentifierInfo *II,
919344779Sdim                                                SourceLocation Loc);
920344779Sdim
921344779Sdim//===----------------------------------------------------------------------===//
922344779Sdim// Definitions of helper functions and methods for expanding macros.
923344779Sdim//===----------------------------------------------------------------------===//
924344779Sdim
925360784Sdimstatic ExpansionInfo
926360784SdimgetExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
927360784Sdim                 const cross_tu::CrossTranslationUnitContext &CTU) {
928344779Sdim
929360784Sdim  const Preprocessor *PPToUse = &PP;
930360784Sdim  if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
931360784Sdim    MacroLoc = LocAndUnit->first;
932360784Sdim    PPToUse = &LocAndUnit->second->getPreprocessor();
933360784Sdim  }
934360784Sdim
935344779Sdim  llvm::SmallString<200> ExpansionBuf;
936344779Sdim  llvm::raw_svector_ostream OS(ExpansionBuf);
937360784Sdim  TokenPrinter Printer(OS, *PPToUse);
938353358Sdim  llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
939353358Sdim
940360784Sdim  std::string MacroName = getMacroNameAndPrintExpansion(
941360784Sdim      Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
942344779Sdim  return { MacroName, OS.str() };
943344779Sdim}
944344779Sdim
945353358Sdimstatic std::string getMacroNameAndPrintExpansion(
946353358Sdim    TokenPrinter &Printer,
947353358Sdim    SourceLocation MacroLoc,
948353358Sdim    const Preprocessor &PP,
949353358Sdim    const MacroArgMap &PrevArgs,
950353358Sdim    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
951344779Sdim
952344779Sdim  const SourceManager &SM = PP.getSourceManager();
953344779Sdim
954344779Sdim  MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
955353358Sdim  IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
956344779Sdim
957353358Sdim  // TODO: If the macro definition contains another symbol then this function is
958353358Sdim  // called recursively. In case this symbol is the one being defined, it will
959353358Sdim  // be an infinite recursion which is stopped by this "if" statement. However,
960353358Sdim  // in this case we don't get the full expansion text in the Plist file. See
961353358Sdim  // the test file where "value" is expanded to "garbage_" instead of
962353358Sdim  // "garbage_value".
963353358Sdim  if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
964353358Sdim    return Info.Name;
965353358Sdim  AlreadyProcessedTokens.insert(IDInfo);
966353358Sdim
967353358Sdim  if (!Info.MI)
968353358Sdim    return Info.Name;
969353358Sdim
970344779Sdim  // Manually expand its arguments from the previous macro.
971344779Sdim  Info.Args.expandFromPrevMacro(PrevArgs);
972344779Sdim
973344779Sdim  // Iterate over the macro's tokens and stringify them.
974344779Sdim  for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
975344779Sdim       ++It) {
976344779Sdim    Token T = *It;
977344779Sdim
978344779Sdim    // If this token is not an identifier, we only need to print it.
979344779Sdim    if (T.isNot(tok::identifier)) {
980344779Sdim      Printer.printToken(T);
981344779Sdim      continue;
982344779Sdim    }
983344779Sdim
984344779Sdim    const auto *II = T.getIdentifierInfo();
985344779Sdim    assert(II &&
986344779Sdim          "This token is an identifier but has no IdentifierInfo!");
987344779Sdim
988344779Sdim    // If this token is a macro that should be expanded inside the current
989344779Sdim    // macro.
990353358Sdim    if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
991353358Sdim      getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
992353358Sdim                                    AlreadyProcessedTokens);
993344779Sdim
994344779Sdim      // If this is a function-like macro, skip its arguments, as
995344779Sdim      // getExpandedMacro() already printed them. If this is the case, let's
996344779Sdim      // first jump to the '(' token.
997353358Sdim      auto N = std::next(It);
998353358Sdim      if (N != E && N->is(tok::l_paren))
999344779Sdim        It = getMatchingRParen(++It, E);
1000344779Sdim      continue;
1001344779Sdim    }
1002344779Sdim
1003344779Sdim    // If this token is the current macro's argument, we should expand it.
1004344779Sdim    auto ArgMapIt = Info.Args.find(II);
1005344779Sdim    if (ArgMapIt != Info.Args.end()) {
1006344779Sdim      for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
1007344779Sdim                                      ArgEnd = ArgMapIt->second.end();
1008344779Sdim           ArgIt != ArgEnd; ++ArgIt) {
1009344779Sdim
1010344779Sdim        // These tokens may still be macros, if that is the case, handle it the
1011344779Sdim        // same way we did above.
1012344779Sdim        const auto *ArgII = ArgIt->getIdentifierInfo();
1013344779Sdim        if (!ArgII) {
1014344779Sdim          Printer.printToken(*ArgIt);
1015344779Sdim          continue;
1016344779Sdim        }
1017344779Sdim
1018344779Sdim        const auto *MI = PP.getMacroInfo(ArgII);
1019344779Sdim        if (!MI) {
1020344779Sdim          Printer.printToken(*ArgIt);
1021344779Sdim          continue;
1022344779Sdim        }
1023344779Sdim
1024344779Sdim        getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
1025353358Sdim                                      Info.Args, AlreadyProcessedTokens);
1026353358Sdim        // Peek the next token if it is a tok::l_paren. This way we can decide
1027353358Sdim        // if this is the application or just a reference to a function maxro
1028353358Sdim        // symbol:
1029353358Sdim        //
1030353358Sdim        // #define apply(f) ...
1031353358Sdim        // #define func(x) ...
1032353358Sdim        // apply(func)
1033353358Sdim        // apply(func(42))
1034353358Sdim        auto N = std::next(ArgIt);
1035353358Sdim        if (N != ArgEnd && N->is(tok::l_paren))
1036344779Sdim          ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
1037344779Sdim      }
1038344779Sdim      continue;
1039344779Sdim    }
1040344779Sdim
1041344779Sdim    // If control reached here, then this token isn't a macro identifier, nor an
1042344779Sdim    // unexpanded macro argument that we need to handle, print it.
1043344779Sdim    Printer.printToken(T);
1044344779Sdim  }
1045344779Sdim
1046353358Sdim  AlreadyProcessedTokens.erase(IDInfo);
1047353358Sdim
1048344779Sdim  return Info.Name;
1049344779Sdim}
1050344779Sdim
1051344779Sdimstatic MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
1052344779Sdim                                            const Preprocessor &PP) {
1053344779Sdim
1054344779Sdim  const SourceManager &SM = PP.getSourceManager();
1055344779Sdim  const LangOptions &LangOpts = PP.getLangOpts();
1056344779Sdim
1057344779Sdim  // First, we create a Lexer to lex *at the expansion location* the tokens
1058344779Sdim  // referring to the macro's name and its arguments.
1059344779Sdim  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
1060344779Sdim  const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
1061344779Sdim  const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
1062344779Sdim
1063344779Sdim  Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
1064344779Sdim                 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
1065344779Sdim
1066344779Sdim  // Acquire the macro's name.
1067344779Sdim  Token TheTok;
1068344779Sdim  RawLexer.LexFromRawLexer(TheTok);
1069344779Sdim
1070344779Sdim  std::string MacroName = PP.getSpelling(TheTok);
1071344779Sdim
1072344779Sdim  const auto *II = PP.getIdentifierInfo(MacroName);
1073344779Sdim  assert(II && "Failed to acquire the IndetifierInfo for the macro!");
1074344779Sdim
1075344779Sdim  const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
1076353358Sdim  // assert(MI && "The macro must've been defined at it's expansion location!");
1077353358Sdim  //
1078353358Sdim  // We should always be able to obtain the MacroInfo in a given TU, but if
1079353358Sdim  // we're running the analyzer with CTU, the Preprocessor won't contain the
1080353358Sdim  // directive history (or anything for that matter) from another TU.
1081353358Sdim  // TODO: assert when we're not running with CTU.
1082353358Sdim  if (!MI)
1083353358Sdim    return { MacroName, MI, {} };
1084344779Sdim
1085344779Sdim  // Acquire the macro's arguments.
1086344779Sdim  //
1087344779Sdim  // The rough idea here is to lex from the first left parentheses to the last
1088344779Sdim  // right parentheses, and map the macro's unexpanded arguments to what they
1089344779Sdim  // will be expanded to. An expanded macro argument may contain several tokens
1090344779Sdim  // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
1091344779Sdim  // which point we start lexing the next argument or finish.
1092344779Sdim  ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
1093344779Sdim  if (MacroArgs.empty())
1094344779Sdim    return { MacroName, MI, {} };
1095344779Sdim
1096344779Sdim  RawLexer.LexFromRawLexer(TheTok);
1097353358Sdim  // When this is a token which expands to another macro function then its
1098353358Sdim  // parentheses are not at its expansion locaiton. For example:
1099353358Sdim  //
1100353358Sdim  // #define foo(x) int bar() { return x; }
1101353358Sdim  // #define apply_zero(f) f(0)
1102353358Sdim  // apply_zero(foo)
1103353358Sdim  //               ^
1104353358Sdim  //               This is not a tok::l_paren, but foo is a function.
1105353358Sdim  if (TheTok.isNot(tok::l_paren))
1106353358Sdim    return { MacroName, MI, {} };
1107344779Sdim
1108344779Sdim  MacroArgMap Args;
1109344779Sdim
1110344779Sdim  // When the macro's argument is a function call, like
1111344779Sdim  //   CALL_FN(someFunctionName(param1, param2))
1112344779Sdim  // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
1113344779Sdim  // actual macro arguments, or do not represent the macro argument's closing
1114344779Sdim  // parentheses, so we'll count how many parentheses aren't closed yet.
1115344779Sdim  // If ParanthesesDepth
1116344779Sdim  //   * = 0, then there are no more arguments to lex.
1117344779Sdim  //   * = 1, then if we find a tok::comma, we can start lexing the next arg.
1118344779Sdim  //   * > 1, then tok::comma is a part of the current arg.
1119344779Sdim  int ParenthesesDepth = 1;
1120344779Sdim
1121344779Sdim  // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
1122344779Sdim  // even if we lex a tok::comma and ParanthesesDepth == 1.
1123344779Sdim  const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
1124344779Sdim
1125344779Sdim  for (const IdentifierInfo *UnexpArgII : MacroArgs) {
1126344779Sdim    MacroArgMap::mapped_type ExpandedArgTokens;
1127344779Sdim
1128344779Sdim    // One could also simply not supply a single argument to __VA_ARGS__ -- this
1129344779Sdim    // results in a preprocessor warning, but is not an error:
1130344779Sdim    //   #define VARIADIC(ptr, ...) \
1131344779Sdim    //     someVariadicTemplateFunction(__VA_ARGS__)
1132344779Sdim    //
1133344779Sdim    //   int *ptr;
1134344779Sdim    //   VARIADIC(ptr); // Note that there are no commas, this isn't just an
1135344779Sdim    //                  // empty parameter -- there are no parameters for '...'.
1136344779Sdim    // In any other case, ParenthesesDepth mustn't be 0 here.
1137344779Sdim    if (ParenthesesDepth != 0) {
1138344779Sdim
1139344779Sdim      // Lex the first token of the next macro parameter.
1140344779Sdim      RawLexer.LexFromRawLexer(TheTok);
1141344779Sdim
1142344779Sdim      while (!(ParenthesesDepth == 1 &&
1143344779Sdim              (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
1144344779Sdim        assert(TheTok.isNot(tok::eof) &&
1145344779Sdim               "EOF encountered while looking for expanded macro args!");
1146344779Sdim
1147344779Sdim        if (TheTok.is(tok::l_paren))
1148344779Sdim          ++ParenthesesDepth;
1149344779Sdim
1150344779Sdim        if (TheTok.is(tok::r_paren))
1151344779Sdim          --ParenthesesDepth;
1152344779Sdim
1153344779Sdim        if (ParenthesesDepth == 0)
1154344779Sdim          break;
1155344779Sdim
1156344779Sdim        if (TheTok.is(tok::raw_identifier))
1157344779Sdim          PP.LookUpIdentifierInfo(TheTok);
1158344779Sdim
1159344779Sdim        ExpandedArgTokens.push_back(TheTok);
1160344779Sdim        RawLexer.LexFromRawLexer(TheTok);
1161344779Sdim      }
1162344779Sdim    } else {
1163344779Sdim      assert(UnexpArgII == __VA_ARGS__II);
1164344779Sdim    }
1165344779Sdim
1166344779Sdim    Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1167344779Sdim  }
1168344779Sdim
1169344779Sdim  assert(TheTok.is(tok::r_paren) &&
1170344779Sdim         "Expanded macro argument acquisition failed! After the end of the loop"
1171344779Sdim         " this token should be ')'!");
1172344779Sdim
1173344779Sdim  return { MacroName, MI, Args };
1174344779Sdim}
1175344779Sdim
1176344779Sdimstatic MacroInfo::tokens_iterator getMatchingRParen(
1177344779Sdim    MacroInfo::tokens_iterator It,
1178344779Sdim    MacroInfo::tokens_iterator End) {
1179344779Sdim
1180344779Sdim  assert(It->is(tok::l_paren) && "This token should be '('!");
1181344779Sdim
1182344779Sdim  // Skip until we find the closing ')'.
1183344779Sdim  int ParenthesesDepth = 1;
1184344779Sdim  while (ParenthesesDepth != 0) {
1185344779Sdim    ++It;
1186344779Sdim
1187344779Sdim    assert(It->isNot(tok::eof) &&
1188344779Sdim           "Encountered EOF while attempting to skip macro arguments!");
1189344779Sdim    assert(It != End &&
1190344779Sdim           "End of the macro definition reached before finding ')'!");
1191344779Sdim
1192344779Sdim    if (It->is(tok::l_paren))
1193344779Sdim      ++ParenthesesDepth;
1194344779Sdim
1195344779Sdim    if (It->is(tok::r_paren))
1196344779Sdim      --ParenthesesDepth;
1197344779Sdim  }
1198344779Sdim  return It;
1199344779Sdim}
1200344779Sdim
1201344779Sdimstatic const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1202344779Sdim                                                const SourceManager &SM,
1203344779Sdim                                                const IdentifierInfo *II,
1204344779Sdim                                                SourceLocation Loc) {
1205344779Sdim
1206344779Sdim  const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1207344779Sdim  if (!MD)
1208344779Sdim    return nullptr;
1209344779Sdim
1210344779Sdim  return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1211344779Sdim}
1212344779Sdim
1213344779Sdimvoid MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1214344779Sdim
1215344779Sdim  for (value_type &Pair : *this) {
1216344779Sdim    ExpArgTokens &CurrExpArgTokens = Pair.second;
1217344779Sdim
1218344779Sdim    // For each token in the expanded macro argument.
1219344779Sdim    auto It = CurrExpArgTokens.begin();
1220344779Sdim    while (It != CurrExpArgTokens.end()) {
1221344779Sdim      if (It->isNot(tok::identifier)) {
1222344779Sdim        ++It;
1223344779Sdim        continue;
1224344779Sdim      }
1225344779Sdim
1226344779Sdim      const auto *II = It->getIdentifierInfo();
1227344779Sdim      assert(II);
1228344779Sdim
1229344779Sdim      // Is this an argument that "Super" expands further?
1230344779Sdim      if (!Super.count(II)) {
1231344779Sdim        ++It;
1232344779Sdim        continue;
1233344779Sdim      }
1234344779Sdim
1235344779Sdim      const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1236344779Sdim
1237344779Sdim      It = CurrExpArgTokens.insert(
1238344779Sdim          It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1239344779Sdim      std::advance(It, SuperExpArgTokens.size());
1240344779Sdim      It = CurrExpArgTokens.erase(It);
1241344779Sdim    }
1242344779Sdim  }
1243344779Sdim}
1244344779Sdim
1245344779Sdimvoid TokenPrinter::printToken(const Token &Tok) {
1246344779Sdim  // If this is the first token to be printed, don't print space.
1247344779Sdim  if (PrevTok.isNot(tok::unknown)) {
1248344779Sdim    // If the tokens were already space separated, or if they must be to avoid
1249344779Sdim    // them being implicitly pasted, add a space between them.
1250344779Sdim    if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1251344779Sdim                                                       Tok)) {
1252344779Sdim      // AvoidConcat doesn't check for ##, don't print a space around it.
1253344779Sdim      if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1254344779Sdim        OS << ' ';
1255344779Sdim      }
1256344779Sdim    }
1257344779Sdim  }
1258344779Sdim
1259344779Sdim  if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1260344779Sdim    if (PrevTok.is(tok::hash))
1261344779Sdim      OS << '\"' << PP.getSpelling(Tok) << '\"';
1262344779Sdim    else
1263344779Sdim      OS << PP.getSpelling(Tok);
1264344779Sdim  }
1265344779Sdim
1266344779Sdim  PrevPrevTok = PrevTok;
1267344779Sdim  PrevTok = Tok;
1268344779Sdim}
1269