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,
49 const std::string &OutputFile, const Preprocessor &PP,
50 const cross_tu::CrossTranslationUnitContext &CTU,
51 bool supportsMultipleFiles);
52
~PlistDiagnostics()53 ~PlistDiagnostics() override {}
54
55 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
56 FilesMade *filesMade) override;
57
getName() const58 StringRef getName() const override {
59 return "PlistDiagnostics";
60 }
61
getGenerationScheme() const62 PathGenerationScheme getGenerationScheme() const override {
63 return Extensive;
64 }
supportsLogicalOpControlFlow() const65 bool supportsLogicalOpControlFlow() const override { return true; }
supportsCrossFileDiagnostics() const66 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:
PlistPrinter(const FIDMap & FM,AnalyzerOptions & AnOpts,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU)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
ReportDiag(raw_ostream & o,const PathDiagnosticPiece & P)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:
ReportPiece(raw_ostream & o,const PathDiagnosticPiece & P,unsigned indent,unsigned depth,bool includeControlFlow,bool isKeyEvent=false)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;
ExpansionInfo__anon504ee5450311::ExpansionInfo162 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
EmitRanges(raw_ostream & o,const ArrayRef<SourceRange> Ranges,unsigned indent)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
EmitMessage(raw_ostream & o,StringRef Message,unsigned indent)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
EmitFixits(raw_ostream & o,ArrayRef<FixItHint> fixits,unsigned indent)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
ReportControlFlow(raw_ostream & o,const PathDiagnosticControlFlowPiece & P,unsigned indent)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
ReportEvent(raw_ostream & o,const PathDiagnosticEventPiece & P,unsigned indent,unsigned depth,bool isKeyEvent)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
ReportCall(raw_ostream & o,const PathDiagnosticCallPiece & P,unsigned indent,unsigned depth)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
ReportMacroSubPieces(raw_ostream & o,const PathDiagnosticMacroPiece & P,unsigned indent,unsigned depth)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
ReportMacroExpansions(raw_ostream & o,unsigned indent)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
ReportNote(raw_ostream & o,const PathDiagnosticNotePiece & P,unsigned indent)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
ReportPopUp(raw_ostream & o,const PathDiagnosticPopUpPiece & P,unsigned indent)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.
printCoverage(const PathDiagnostic * D,unsigned InputIndentLevel,SmallVectorImpl<FileID> & Fids,FIDMap & FM,llvm::raw_fd_ostream & o)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
printBugPath(llvm::raw_ostream & o,const FIDMap & FM,AnalyzerOptions & AnOpts,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU,const PathPieces & Path)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
PlistDiagnostics(AnalyzerOptions & AnalyzerOpts,const std::string & output,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU,bool supportsMultipleFiles)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
createPlistDiagnosticConsumer(AnalyzerOptions & AnalyzerOpts,PathDiagnosticConsumers & C,const std::string & OutputFile,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU)583 void ento::createPlistDiagnosticConsumer(
584 AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
585 const std::string &OutputFile, const Preprocessor &PP,
586 const cross_tu::CrossTranslationUnitContext &CTU) {
587
588 // TODO: Emit an error here.
589 if (OutputFile.empty())
590 return;
591
592 C.push_back(new PlistDiagnostics(AnalyzerOpts, OutputFile, PP, CTU,
593 /*supportsMultipleFiles*/ false));
594 createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputFile, PP, CTU);
595 }
596
createPlistMultiFileDiagnosticConsumer(AnalyzerOptions & AnalyzerOpts,PathDiagnosticConsumers & C,const std::string & OutputFile,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU)597 void ento::createPlistMultiFileDiagnosticConsumer(
598 AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
599 const std::string &OutputFile, const Preprocessor &PP,
600 const cross_tu::CrossTranslationUnitContext &CTU) {
601
602 // TODO: Emit an error here.
603 if (OutputFile.empty())
604 return;
605
606 C.push_back(new PlistDiagnostics(AnalyzerOpts, OutputFile, PP, CTU,
607 /*supportsMultipleFiles*/ true));
608 createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputFile, PP, CTU);
609 }
610
FlushDiagnosticsImpl(std::vector<const PathDiagnostic * > & Diags,FilesMade * filesMade)611 void PlistDiagnostics::FlushDiagnosticsImpl(
612 std::vector<const PathDiagnostic *> &Diags,
613 FilesMade *filesMade) {
614 // Build up a set of FIDs that we use by scanning the locations and
615 // ranges of the diagnostics.
616 FIDMap FM;
617 SmallVector<FileID, 10> Fids;
618 const SourceManager& SM = PP.getSourceManager();
619 const LangOptions &LangOpts = PP.getLangOpts();
620
621 auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
622 AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
623 ArrayRef<SourceRange> Ranges = Piece.getRanges();
624 for (const SourceRange &Range : Ranges) {
625 AddFID(FM, Fids, SM, Range.getBegin());
626 AddFID(FM, Fids, SM, Range.getEnd());
627 }
628 };
629
630 for (const PathDiagnostic *D : Diags) {
631
632 SmallVector<const PathPieces *, 5> WorkList;
633 WorkList.push_back(&D->path);
634
635 while (!WorkList.empty()) {
636 const PathPieces &Path = *WorkList.pop_back_val();
637
638 for (const auto &Iter : Path) {
639 const PathDiagnosticPiece &Piece = *Iter;
640 AddPieceFID(Piece);
641
642 if (const PathDiagnosticCallPiece *Call =
643 dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
644 if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
645 AddPieceFID(*CallEnterWithin);
646
647 if (auto CallEnterEvent = Call->getCallEnterEvent())
648 AddPieceFID(*CallEnterEvent);
649
650 WorkList.push_back(&Call->path);
651 } else if (const PathDiagnosticMacroPiece *Macro =
652 dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
653 WorkList.push_back(&Macro->subPieces);
654 }
655 }
656 }
657 }
658
659 // Open the file.
660 std::error_code EC;
661 llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
662 if (EC) {
663 llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
664 return;
665 }
666
667 EmitPlistHeader(o);
668
669 // Write the root object: a <dict> containing...
670 // - "clang_version", the string representation of clang version
671 // - "files", an <array> mapping from FIDs to file names
672 // - "diagnostics", an <array> containing the path diagnostics
673 o << "<dict>\n" <<
674 " <key>clang_version</key>\n";
675 EmitString(o, getClangFullVersion()) << '\n';
676 o << " <key>diagnostics</key>\n"
677 " <array>\n";
678
679 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
680 DE = Diags.end(); DI!=DE; ++DI) {
681
682 o << " <dict>\n";
683
684 const PathDiagnostic *D = *DI;
685 printBugPath(o, FM, AnOpts, PP, CTU, D->path);
686
687 // Output the bug type and bug category.
688 o << " <key>description</key>";
689 EmitString(o, D->getShortDescription()) << '\n';
690 o << " <key>category</key>";
691 EmitString(o, D->getCategory()) << '\n';
692 o << " <key>type</key>";
693 EmitString(o, D->getBugType()) << '\n';
694 o << " <key>check_name</key>";
695 EmitString(o, D->getCheckerName()) << '\n';
696
697 o << " <!-- This hash is experimental and going to change! -->\n";
698 o << " <key>issue_hash_content_of_line_in_context</key>";
699 PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
700 FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
701 ? UPDLoc.asLocation()
702 : D->getLocation().asLocation()),
703 SM);
704 const Decl *DeclWithIssue = D->getDeclWithIssue();
705 EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
706 DeclWithIssue, LangOpts))
707 << '\n';
708
709 // Output information about the semantic context where
710 // the issue occurred.
711 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
712 // FIXME: handle blocks, which have no name.
713 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
714 StringRef declKind;
715 switch (ND->getKind()) {
716 case Decl::CXXRecord:
717 declKind = "C++ class";
718 break;
719 case Decl::CXXMethod:
720 declKind = "C++ method";
721 break;
722 case Decl::ObjCMethod:
723 declKind = "Objective-C method";
724 break;
725 case Decl::Function:
726 declKind = "function";
727 break;
728 default:
729 break;
730 }
731 if (!declKind.empty()) {
732 const std::string &declName = ND->getDeclName().getAsString();
733 o << " <key>issue_context_kind</key>";
734 EmitString(o, declKind) << '\n';
735 o << " <key>issue_context</key>";
736 EmitString(o, declName) << '\n';
737 }
738
739 // Output the bug hash for issue unique-ing. Currently, it's just an
740 // offset from the beginning of the function.
741 if (const Stmt *Body = DeclWithIssue->getBody()) {
742
743 // If the bug uniqueing location exists, use it for the hash.
744 // For example, this ensures that two leaks reported on the same line
745 // will have different issue_hashes and that the hash will identify
746 // the leak location even after code is added between the allocation
747 // site and the end of scope (leak report location).
748 if (UPDLoc.isValid()) {
749 FullSourceLoc UFunL(
750 SM.getExpansionLoc(
751 D->getUniqueingDecl()->getBody()->getBeginLoc()),
752 SM);
753 o << " <key>issue_hash_function_offset</key><string>"
754 << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
755 << "</string>\n";
756
757 // Otherwise, use the location on which the bug is reported.
758 } else {
759 FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
760 o << " <key>issue_hash_function_offset</key><string>"
761 << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
762 << "</string>\n";
763 }
764
765 }
766 }
767 }
768
769 // Output the location of the bug.
770 o << " <key>location</key>\n";
771 EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
772
773 // Output the diagnostic to the sub-diagnostic client, if any.
774 if (!filesMade->empty()) {
775 StringRef lastName;
776 PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
777 if (files) {
778 for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
779 CE = files->end(); CI != CE; ++CI) {
780 StringRef newName = CI->first;
781 if (newName != lastName) {
782 if (!lastName.empty()) {
783 o << " </array>\n";
784 }
785 lastName = newName;
786 o << " <key>" << lastName << "_files</key>\n";
787 o << " <array>\n";
788 }
789 o << " <string>" << CI->second << "</string>\n";
790 }
791 o << " </array>\n";
792 }
793 }
794
795 printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
796
797 // Close up the entry.
798 o << " </dict>\n";
799 }
800
801 o << " </array>\n";
802
803 o << " <key>files</key>\n"
804 " <array>\n";
805 for (FileID FID : Fids)
806 EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
807 o << " </array>\n";
808
809 if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
810 o << " <key>statistics</key>\n";
811 std::string stats;
812 llvm::raw_string_ostream os(stats);
813 llvm::PrintStatisticsJSON(os);
814 os.flush();
815 EmitString(o, html::EscapeText(stats)) << '\n';
816 }
817
818 // Finish.
819 o << "</dict>\n</plist>\n";
820 }
821
822 //===----------------------------------------------------------------------===//
823 // Declarations of helper functions and data structures for expanding macros.
824 //===----------------------------------------------------------------------===//
825
826 namespace {
827
828 using ExpArgTokens = llvm::SmallVector<Token, 2>;
829
830 /// Maps unexpanded macro arguments to expanded arguments. A macro argument may
831 /// need to expanded further when it is nested inside another macro.
832 class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
833 public:
834 void expandFromPrevMacro(const MacroArgMap &Super);
835 };
836
837 struct MacroNameAndArgs {
838 std::string Name;
839 const MacroInfo *MI = nullptr;
840 MacroArgMap Args;
841
MacroNameAndArgs__anon504ee5450711::MacroNameAndArgs842 MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
843 : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
844 };
845
846 class TokenPrinter {
847 llvm::raw_ostream &OS;
848 const Preprocessor &PP;
849
850 Token PrevTok, PrevPrevTok;
851 TokenConcatenation ConcatInfo;
852
853 public:
TokenPrinter(llvm::raw_ostream & OS,const Preprocessor & PP)854 TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
855 : OS(OS), PP(PP), ConcatInfo(PP) {
856 PrevTok.setKind(tok::unknown);
857 PrevPrevTok.setKind(tok::unknown);
858 }
859
860 void printToken(const Token &Tok);
861 };
862
863 } // end of anonymous namespace
864
865 /// The implementation method of getMacroExpansion: It prints the expansion of
866 /// a macro to \p Printer, and returns with the name of the macro.
867 ///
868 /// Since macros can be nested in one another, this function may call itself
869 /// recursively.
870 ///
871 /// Unfortunately, macro arguments have to expanded manually. To understand why,
872 /// observe the following example:
873 ///
874 /// #define PRINT(x) print(x)
875 /// #define DO_SOMETHING(str) PRINT(str)
876 ///
877 /// DO_SOMETHING("Cute panda cubs.");
878 ///
879 /// As we expand the last line, we'll immediately replace PRINT(str) with
880 /// print(x). The information that both 'str' and 'x' refers to the same string
881 /// is an information we have to forward, hence the argument \p PrevArgs.
882 ///
883 /// To avoid infinite recursion we maintain the already processed tokens in
884 /// a set. This is carried as a parameter through the recursive calls. The set
885 /// is extended with the currently processed token and after processing it, the
886 /// token is removed. If the token is already in the set, then recursion stops:
887 ///
888 /// #define f(y) x
889 /// #define x f(x)
890 static std::string getMacroNameAndPrintExpansion(
891 TokenPrinter &Printer,
892 SourceLocation MacroLoc,
893 const Preprocessor &PP,
894 const MacroArgMap &PrevArgs,
895 llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);
896
897 /// Retrieves the name of the macro and what it's arguments expand into
898 /// at \p ExpanLoc.
899 ///
900 /// For example, for the following macro expansion:
901 ///
902 /// #define SET_TO_NULL(x) x = 0
903 /// #define NOT_SUSPICIOUS(a) \
904 /// { \
905 /// int b = 0; \
906 /// } \
907 /// SET_TO_NULL(a)
908 ///
909 /// int *ptr = new int(4);
910 /// NOT_SUSPICIOUS(&ptr);
911 /// *ptr = 5;
912 ///
913 /// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
914 /// and the MacroArgMap map { (a, &ptr) } will be returned.
915 ///
916 /// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
917 /// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
918 /// { (x, a) } will be returned.
919 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
920 const Preprocessor &PP);
921
922 /// Retrieves the ')' token that matches '(' \p It points to.
923 static MacroInfo::tokens_iterator getMatchingRParen(
924 MacroInfo::tokens_iterator It,
925 MacroInfo::tokens_iterator End);
926
927 /// Retrieves the macro info for \p II refers to at \p Loc. This is important
928 /// because macros can be redefined or undefined.
929 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
930 const SourceManager &SM,
931 const IdentifierInfo *II,
932 SourceLocation Loc);
933
934 //===----------------------------------------------------------------------===//
935 // Definitions of helper functions and methods for expanding macros.
936 //===----------------------------------------------------------------------===//
937
938 static ExpansionInfo
getExpandedMacro(SourceLocation MacroLoc,const Preprocessor & PP,const cross_tu::CrossTranslationUnitContext & CTU)939 getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
940 const cross_tu::CrossTranslationUnitContext &CTU) {
941
942 const Preprocessor *PPToUse = &PP;
943 if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
944 MacroLoc = LocAndUnit->first;
945 PPToUse = &LocAndUnit->second->getPreprocessor();
946 }
947
948 llvm::SmallString<200> ExpansionBuf;
949 llvm::raw_svector_ostream OS(ExpansionBuf);
950 TokenPrinter Printer(OS, *PPToUse);
951 llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
952
953 std::string MacroName = getMacroNameAndPrintExpansion(
954 Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
955 return {MacroName, std::string(OS.str())};
956 }
957
getMacroNameAndPrintExpansion(TokenPrinter & Printer,SourceLocation MacroLoc,const Preprocessor & PP,const MacroArgMap & PrevArgs,llvm::SmallPtrSet<IdentifierInfo *,8> & AlreadyProcessedTokens)958 static std::string getMacroNameAndPrintExpansion(
959 TokenPrinter &Printer,
960 SourceLocation MacroLoc,
961 const Preprocessor &PP,
962 const MacroArgMap &PrevArgs,
963 llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
964
965 const SourceManager &SM = PP.getSourceManager();
966
967 MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
968 IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
969
970 // TODO: If the macro definition contains another symbol then this function is
971 // called recursively. In case this symbol is the one being defined, it will
972 // be an infinite recursion which is stopped by this "if" statement. However,
973 // in this case we don't get the full expansion text in the Plist file. See
974 // the test file where "value" is expanded to "garbage_" instead of
975 // "garbage_value".
976 if (!AlreadyProcessedTokens.insert(IDInfo).second)
977 return Info.Name;
978
979 if (!Info.MI)
980 return Info.Name;
981
982 // Manually expand its arguments from the previous macro.
983 Info.Args.expandFromPrevMacro(PrevArgs);
984
985 // Iterate over the macro's tokens and stringify them.
986 for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
987 ++It) {
988 Token T = *It;
989
990 // If this token is not an identifier, we only need to print it.
991 if (T.isNot(tok::identifier)) {
992 Printer.printToken(T);
993 continue;
994 }
995
996 const auto *II = T.getIdentifierInfo();
997 assert(II &&
998 "This token is an identifier but has no IdentifierInfo!");
999
1000 // If this token is a macro that should be expanded inside the current
1001 // macro.
1002 if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
1003 getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
1004 AlreadyProcessedTokens);
1005
1006 // If this is a function-like macro, skip its arguments, as
1007 // getExpandedMacro() already printed them. If this is the case, let's
1008 // first jump to the '(' token.
1009 auto N = std::next(It);
1010 if (N != E && N->is(tok::l_paren))
1011 It = getMatchingRParen(++It, E);
1012 continue;
1013 }
1014
1015 // If this token is the current macro's argument, we should expand it.
1016 auto ArgMapIt = Info.Args.find(II);
1017 if (ArgMapIt != Info.Args.end()) {
1018 for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
1019 ArgEnd = ArgMapIt->second.end();
1020 ArgIt != ArgEnd; ++ArgIt) {
1021
1022 // These tokens may still be macros, if that is the case, handle it the
1023 // same way we did above.
1024 const auto *ArgII = ArgIt->getIdentifierInfo();
1025 if (!ArgII) {
1026 Printer.printToken(*ArgIt);
1027 continue;
1028 }
1029
1030 const auto *MI = PP.getMacroInfo(ArgII);
1031 if (!MI) {
1032 Printer.printToken(*ArgIt);
1033 continue;
1034 }
1035
1036 getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
1037 Info.Args, AlreadyProcessedTokens);
1038 // Peek the next token if it is a tok::l_paren. This way we can decide
1039 // if this is the application or just a reference to a function maxro
1040 // symbol:
1041 //
1042 // #define apply(f) ...
1043 // #define func(x) ...
1044 // apply(func)
1045 // apply(func(42))
1046 auto N = std::next(ArgIt);
1047 if (N != ArgEnd && N->is(tok::l_paren))
1048 ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
1049 }
1050 continue;
1051 }
1052
1053 // If control reached here, then this token isn't a macro identifier, nor an
1054 // unexpanded macro argument that we need to handle, print it.
1055 Printer.printToken(T);
1056 }
1057
1058 AlreadyProcessedTokens.erase(IDInfo);
1059
1060 return Info.Name;
1061 }
1062
getMacroNameAndArgs(SourceLocation ExpanLoc,const Preprocessor & PP)1063 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
1064 const Preprocessor &PP) {
1065
1066 const SourceManager &SM = PP.getSourceManager();
1067 const LangOptions &LangOpts = PP.getLangOpts();
1068
1069 // First, we create a Lexer to lex *at the expansion location* the tokens
1070 // referring to the macro's name and its arguments.
1071 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
1072 const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
1073 const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
1074
1075 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
1076 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
1077
1078 // Acquire the macro's name.
1079 Token TheTok;
1080 RawLexer.LexFromRawLexer(TheTok);
1081
1082 std::string MacroName = PP.getSpelling(TheTok);
1083
1084 const auto *II = PP.getIdentifierInfo(MacroName);
1085 assert(II && "Failed to acquire the IndetifierInfo for the macro!");
1086
1087 const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
1088 // assert(MI && "The macro must've been defined at it's expansion location!");
1089 //
1090 // We should always be able to obtain the MacroInfo in a given TU, but if
1091 // we're running the analyzer with CTU, the Preprocessor won't contain the
1092 // directive history (or anything for that matter) from another TU.
1093 // TODO: assert when we're not running with CTU.
1094 if (!MI)
1095 return { MacroName, MI, {} };
1096
1097 // Acquire the macro's arguments.
1098 //
1099 // The rough idea here is to lex from the first left parentheses to the last
1100 // right parentheses, and map the macro's unexpanded arguments to what they
1101 // will be expanded to. An expanded macro argument may contain several tokens
1102 // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
1103 // which point we start lexing the next argument or finish.
1104 ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
1105 if (MacroArgs.empty())
1106 return { MacroName, MI, {} };
1107
1108 RawLexer.LexFromRawLexer(TheTok);
1109 // When this is a token which expands to another macro function then its
1110 // parentheses are not at its expansion locaiton. For example:
1111 //
1112 // #define foo(x) int bar() { return x; }
1113 // #define apply_zero(f) f(0)
1114 // apply_zero(foo)
1115 // ^
1116 // This is not a tok::l_paren, but foo is a function.
1117 if (TheTok.isNot(tok::l_paren))
1118 return { MacroName, MI, {} };
1119
1120 MacroArgMap Args;
1121
1122 // When the macro's argument is a function call, like
1123 // CALL_FN(someFunctionName(param1, param2))
1124 // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
1125 // actual macro arguments, or do not represent the macro argument's closing
1126 // parentheses, so we'll count how many parentheses aren't closed yet.
1127 // If ParanthesesDepth
1128 // * = 0, then there are no more arguments to lex.
1129 // * = 1, then if we find a tok::comma, we can start lexing the next arg.
1130 // * > 1, then tok::comma is a part of the current arg.
1131 int ParenthesesDepth = 1;
1132
1133 // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
1134 // even if we lex a tok::comma and ParanthesesDepth == 1.
1135 const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
1136
1137 for (const IdentifierInfo *UnexpArgII : MacroArgs) {
1138 MacroArgMap::mapped_type ExpandedArgTokens;
1139
1140 // One could also simply not supply a single argument to __VA_ARGS__ -- this
1141 // results in a preprocessor warning, but is not an error:
1142 // #define VARIADIC(ptr, ...) \
1143 // someVariadicTemplateFunction(__VA_ARGS__)
1144 //
1145 // int *ptr;
1146 // VARIADIC(ptr); // Note that there are no commas, this isn't just an
1147 // // empty parameter -- there are no parameters for '...'.
1148 // In any other case, ParenthesesDepth mustn't be 0 here.
1149 if (ParenthesesDepth != 0) {
1150
1151 // Lex the first token of the next macro parameter.
1152 RawLexer.LexFromRawLexer(TheTok);
1153
1154 while (!(ParenthesesDepth == 1 &&
1155 (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
1156 assert(TheTok.isNot(tok::eof) &&
1157 "EOF encountered while looking for expanded macro args!");
1158
1159 if (TheTok.is(tok::l_paren))
1160 ++ParenthesesDepth;
1161
1162 if (TheTok.is(tok::r_paren))
1163 --ParenthesesDepth;
1164
1165 if (ParenthesesDepth == 0)
1166 break;
1167
1168 if (TheTok.is(tok::raw_identifier))
1169 PP.LookUpIdentifierInfo(TheTok);
1170
1171 ExpandedArgTokens.push_back(TheTok);
1172 RawLexer.LexFromRawLexer(TheTok);
1173 }
1174 } else {
1175 assert(UnexpArgII == __VA_ARGS__II);
1176 }
1177
1178 Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1179 }
1180
1181 assert(TheTok.is(tok::r_paren) &&
1182 "Expanded macro argument acquisition failed! After the end of the loop"
1183 " this token should be ')'!");
1184
1185 return { MacroName, MI, Args };
1186 }
1187
getMatchingRParen(MacroInfo::tokens_iterator It,MacroInfo::tokens_iterator End)1188 static MacroInfo::tokens_iterator getMatchingRParen(
1189 MacroInfo::tokens_iterator It,
1190 MacroInfo::tokens_iterator End) {
1191
1192 assert(It->is(tok::l_paren) && "This token should be '('!");
1193
1194 // Skip until we find the closing ')'.
1195 int ParenthesesDepth = 1;
1196 while (ParenthesesDepth != 0) {
1197 ++It;
1198
1199 assert(It->isNot(tok::eof) &&
1200 "Encountered EOF while attempting to skip macro arguments!");
1201 assert(It != End &&
1202 "End of the macro definition reached before finding ')'!");
1203
1204 if (It->is(tok::l_paren))
1205 ++ParenthesesDepth;
1206
1207 if (It->is(tok::r_paren))
1208 --ParenthesesDepth;
1209 }
1210 return It;
1211 }
1212
getMacroInfoForLocation(const Preprocessor & PP,const SourceManager & SM,const IdentifierInfo * II,SourceLocation Loc)1213 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1214 const SourceManager &SM,
1215 const IdentifierInfo *II,
1216 SourceLocation Loc) {
1217
1218 const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1219 if (!MD)
1220 return nullptr;
1221
1222 return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1223 }
1224
expandFromPrevMacro(const MacroArgMap & Super)1225 void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1226
1227 for (value_type &Pair : *this) {
1228 ExpArgTokens &CurrExpArgTokens = Pair.second;
1229
1230 // For each token in the expanded macro argument.
1231 auto It = CurrExpArgTokens.begin();
1232 while (It != CurrExpArgTokens.end()) {
1233 if (It->isNot(tok::identifier)) {
1234 ++It;
1235 continue;
1236 }
1237
1238 const auto *II = It->getIdentifierInfo();
1239 assert(II);
1240
1241 // Is this an argument that "Super" expands further?
1242 if (!Super.count(II)) {
1243 ++It;
1244 continue;
1245 }
1246
1247 const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1248
1249 It = CurrExpArgTokens.insert(
1250 It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1251 std::advance(It, SuperExpArgTokens.size());
1252 It = CurrExpArgTokens.erase(It);
1253 }
1254 }
1255 }
1256
printToken(const Token & Tok)1257 void TokenPrinter::printToken(const Token &Tok) {
1258 // If this is the first token to be printed, don't print space.
1259 if (PrevTok.isNot(tok::unknown)) {
1260 // If the tokens were already space separated, or if they must be to avoid
1261 // them being implicitly pasted, add a space between them.
1262 if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1263 Tok)) {
1264 // AvoidConcat doesn't check for ##, don't print a space around it.
1265 if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1266 OS << ' ';
1267 }
1268 }
1269 }
1270
1271 if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1272 if (PrevTok.is(tok::hash))
1273 OS << '\"' << PP.getSpelling(Tok) << '\"';
1274 else
1275 OS << PP.getSpelling(Tok);
1276 }
1277
1278 PrevPrevTok = PrevTok;
1279 PrevTok = Tok;
1280 }
1281