1 //===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PlistDiagnostics object.
12 //===----------------------------------------------------------------------===//
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/Lex/Preprocessor.h"
19 #include "clang/Lex/TokenConcatenation.h"
20 #include "clang/Rewrite/Core/HTMLRewrite.h"
21 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
22 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
23 #include "clang/StaticAnalyzer/Core/IssueHash.h"
24 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/Casting.h"
29 using namespace clang;
31 using namespace markup;
33 //===----------------------------------------------------------------------===//
34 // Declarations of helper classes and functions for emitting bug reports in
36 //===----------------------------------------------------------------------===//
39 class PlistDiagnostics : public PathDiagnosticConsumer {
40 const std::string OutputFile;
41 const Preprocessor &PP;
42 AnalyzerOptions &AnOpts;
43 const bool SupportsCrossFileDiagnostics;
45 PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
46 const std::string& prefix,
47 const Preprocessor &PP,
48 bool supportsMultipleFiles);
50 ~PlistDiagnostics() override {}
52 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
53 FilesMade *filesMade) override;
55 StringRef getName() const override {
56 return "PlistDiagnostics";
59 PathGenerationScheme getGenerationScheme() const override {
62 bool supportsLogicalOpControlFlow() const override { return true; }
63 bool supportsCrossFileDiagnostics() const override {
64 return SupportsCrossFileDiagnostics;
67 } // end anonymous namespace
71 /// A helper class for emitting a single report.
74 AnalyzerOptions &AnOpts;
75 const Preprocessor &PP;
76 llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;
79 PlistPrinter(const FIDMap& FM, AnalyzerOptions &AnOpts,
80 const Preprocessor &PP)
81 : FM(FM), AnOpts(AnOpts), PP(PP) {
84 void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
85 ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
87 // Don't emit a warning about an unused private field.
91 /// Print the expansions of the collected macro pieces.
93 /// Each time ReportDiag is called on a PathDiagnosticMacroPiece (or, if one
94 /// is found through a call piece, etc), it's subpieces are reported, and the
95 /// piece itself is collected. Call this function after the entire bugpath
97 void ReportMacroExpansions(raw_ostream &o, unsigned indent);
100 void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P,
101 unsigned indent, unsigned depth, bool includeControlFlow,
102 bool isKeyEvent = false) {
103 switch (P.getKind()) {
104 case PathDiagnosticPiece::ControlFlow:
105 if (includeControlFlow)
106 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
108 case PathDiagnosticPiece::Call:
109 ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
112 case PathDiagnosticPiece::Event:
113 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
116 case PathDiagnosticPiece::Macro:
117 ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
120 case PathDiagnosticPiece::Note:
121 ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
126 void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
128 void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
130 void ReportControlFlow(raw_ostream &o,
131 const PathDiagnosticControlFlowPiece& P,
133 void ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
134 unsigned indent, unsigned depth, bool isKeyEvent = false);
135 void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
136 unsigned indent, unsigned depth);
137 void ReportMacroSubPieces(raw_ostream &o, const PathDiagnosticMacroPiece& P,
138 unsigned indent, unsigned depth);
139 void ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
143 } // end of anonymous namespace
147 struct ExpansionInfo {
148 std::string MacroName;
149 std::string Expansion;
150 ExpansionInfo(std::string N, std::string E)
151 : MacroName(std::move(N)), Expansion(std::move(E)) {}
154 } // end of anonymous namespace
156 static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
157 AnalyzerOptions &AnOpts,
158 const Preprocessor &PP,
159 const PathPieces &Path);
161 /// Print coverage information to output stream {@code o}.
162 /// May modify the used list of files {@code Fids} by inserting new ones.
163 static void printCoverage(const PathDiagnostic *D,
164 unsigned InputIndentLevel,
165 SmallVectorImpl<FileID> &Fids,
167 llvm::raw_fd_ostream &o);
169 static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc,
170 const Preprocessor &PP);
172 //===----------------------------------------------------------------------===//
173 // Methods of PlistPrinter.
174 //===----------------------------------------------------------------------===//
176 void PlistPrinter::EmitRanges(raw_ostream &o,
177 const ArrayRef<SourceRange> Ranges,
183 Indent(o, indent) << "<key>ranges</key>\n";
184 Indent(o, indent) << "<array>\n";
187 const SourceManager &SM = PP.getSourceManager();
188 const LangOptions &LangOpts = PP.getLangOpts();
190 for (auto &R : Ranges)
192 Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
195 Indent(o, indent) << "</array>\n";
198 void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
201 assert(!Message.empty());
202 Indent(o, indent) << "<key>extended_message</key>\n";
204 EmitString(o, Message) << '\n';
206 // Output the short text.
207 // FIXME: Really use a short string.
208 Indent(o, indent) << "<key>message</key>\n";
210 EmitString(o, Message) << '\n';
213 void PlistPrinter::ReportControlFlow(raw_ostream &o,
214 const PathDiagnosticControlFlowPiece& P,
217 const SourceManager &SM = PP.getSourceManager();
218 const LangOptions &LangOpts = PP.getLangOpts();
220 Indent(o, indent) << "<dict>\n";
223 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
226 Indent(o, indent) << "<key>edges</key>\n";
228 Indent(o, indent) << "<array>\n";
230 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
232 Indent(o, indent) << "<dict>\n";
235 // Make the ranges of the start and end point self-consistent with adjacent edges
236 // by forcing to use only the beginning of the range. This simplifies the layout
237 // logic for clients.
238 Indent(o, indent) << "<key>start</key>\n";
239 SourceRange StartEdge(
240 SM.getExpansionLoc(I->getStart().asRange().getBegin()));
241 EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
244 Indent(o, indent) << "<key>end</key>\n";
245 SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
246 EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
250 Indent(o, indent) << "</dict>\n";
253 Indent(o, indent) << "</array>\n";
256 // Output any helper text.
257 const auto &s = P.getString();
259 Indent(o, indent) << "<key>alternate</key>";
260 EmitString(o, s) << '\n';
264 Indent(o, indent) << "</dict>\n";
267 void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
268 unsigned indent, unsigned depth,
271 const SourceManager &SM = PP.getSourceManager();
273 Indent(o, indent) << "<dict>\n";
276 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
279 Indent(o, indent) << "<key>key_event</key><true/>\n";
282 // Output the location.
283 FullSourceLoc L = P.getLocation().asLocation();
285 Indent(o, indent) << "<key>location</key>\n";
286 EmitLocation(o, SM, L, FM, indent);
288 // Output the ranges (if any).
289 ArrayRef<SourceRange> Ranges = P.getRanges();
290 EmitRanges(o, Ranges, indent);
292 // Output the call depth.
293 Indent(o, indent) << "<key>depth</key>";
294 EmitInteger(o, depth) << '\n';
297 EmitMessage(o, P.getString(), indent);
301 Indent(o, indent); o << "</dict>\n";
304 void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
308 if (auto callEnter = P.getCallEnterEvent())
309 ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
310 P.isLastInMainSourceFile());
315 if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
316 ReportPiece(o, *callEnterWithinCaller, indent, depth,
317 /*includeControlFlow*/ true);
319 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
320 ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
324 if (auto callExit = P.getCallExitEvent())
325 ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
328 void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
329 const PathDiagnosticMacroPiece& P,
330 unsigned indent, unsigned depth) {
331 MacroPieces.push_back(&P);
333 for (PathPieces::const_iterator I = P.subPieces.begin(),
334 E = P.subPieces.end();
336 ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
340 void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
342 for (const PathDiagnosticMacroPiece *P : MacroPieces) {
343 const SourceManager &SM = PP.getSourceManager();
344 ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP);
346 Indent(o, indent) << "<dict>\n";
349 // Output the location.
350 FullSourceLoc L = P->getLocation().asLocation();
352 Indent(o, indent) << "<key>location</key>\n";
353 EmitLocation(o, SM, L, FM, indent);
355 // Output the ranges (if any).
356 ArrayRef<SourceRange> Ranges = P->getRanges();
357 EmitRanges(o, Ranges, indent);
359 // Output the macro name.
360 Indent(o, indent) << "<key>name</key>";
361 EmitString(o, EI.MacroName) << '\n';
363 // Output what it expands into.
364 Indent(o, indent) << "<key>expansion</key>";
365 EmitString(o, EI.Expansion) << '\n';
374 void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
377 const SourceManager &SM = PP.getSourceManager();
379 Indent(o, indent) << "<dict>\n";
382 // Output the location.
383 FullSourceLoc L = P.getLocation().asLocation();
385 Indent(o, indent) << "<key>location</key>\n";
386 EmitLocation(o, SM, L, FM, indent);
388 // Output the ranges (if any).
389 ArrayRef<SourceRange> Ranges = P.getRanges();
390 EmitRanges(o, Ranges, indent);
393 EmitMessage(o, P.getString(), indent);
397 Indent(o, indent); o << "</dict>\n";
400 //===----------------------------------------------------------------------===//
401 // Static function definitions.
402 //===----------------------------------------------------------------------===//
404 /// Print coverage information to output stream {@code o}.
405 /// May modify the used list of files {@code Fids} by inserting new ones.
406 static void printCoverage(const PathDiagnostic *D,
407 unsigned InputIndentLevel,
408 SmallVectorImpl<FileID> &Fids,
410 llvm::raw_fd_ostream &o) {
411 unsigned IndentLevel = InputIndentLevel;
413 Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
414 Indent(o, IndentLevel) << "<dict>\n";
417 // Mapping from file IDs to executed lines.
418 const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
419 for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) {
420 unsigned FileKey = AddFID(FM, Fids, I->first);
421 Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
422 Indent(o, IndentLevel) << "<array>\n";
424 for (unsigned LineNo : I->second) {
425 Indent(o, IndentLevel);
426 EmitInteger(o, LineNo) << "\n";
429 Indent(o, IndentLevel) << "</array>\n";
432 Indent(o, IndentLevel) << "</dict>\n";
434 assert(IndentLevel == InputIndentLevel);
437 static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
438 AnalyzerOptions &AnOpts,
439 const Preprocessor &PP,
440 const PathPieces &Path) {
441 PlistPrinter Printer(FM, AnOpts, PP);
442 assert(std::is_partitioned(
443 Path.begin(), Path.end(),
444 [](const std::shared_ptr<PathDiagnosticPiece> &E)
445 { return E->getKind() == PathDiagnosticPiece::Note; }) &&
446 "PathDiagnostic is not partitioned so that notes precede the rest");
448 PathPieces::const_iterator FirstNonNote = std::partition_point(
449 Path.begin(), Path.end(),
450 [](const std::shared_ptr<PathDiagnosticPiece> &E)
451 { return E->getKind() == PathDiagnosticPiece::Note; });
453 PathPieces::const_iterator I = Path.begin();
455 if (FirstNonNote != Path.begin()) {
456 o << " <key>notes</key>\n"
459 for (; I != FirstNonNote; ++I)
460 Printer.ReportDiag(o, **I);
465 o << " <key>path</key>\n";
469 for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
470 Printer.ReportDiag(o, **I);
474 if (!AnOpts.ShouldDisplayMacroExpansions)
477 o << " <key>macro_expansions</key>\n"
479 Printer.ReportMacroExpansions(o, /* indent */ 4);
483 //===----------------------------------------------------------------------===//
484 // Methods of PlistDiagnostics.
485 //===----------------------------------------------------------------------===//
487 PlistDiagnostics::PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
488 const std::string& output,
489 const Preprocessor &PP,
490 bool supportsMultipleFiles)
491 : OutputFile(output), PP(PP), AnOpts(AnalyzerOpts),
492 SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
494 void ento::createPlistDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
495 PathDiagnosticConsumers &C,
496 const std::string& s,
497 const Preprocessor &PP) {
498 C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP,
499 /*supportsMultipleFiles*/ false));
502 void ento::createPlistMultiFileDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
503 PathDiagnosticConsumers &C,
504 const std::string &s,
505 const Preprocessor &PP) {
506 C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP,
507 /*supportsMultipleFiles*/ true));
509 void PlistDiagnostics::FlushDiagnosticsImpl(
510 std::vector<const PathDiagnostic *> &Diags,
511 FilesMade *filesMade) {
512 // Build up a set of FIDs that we use by scanning the locations and
513 // ranges of the diagnostics.
515 SmallVector<FileID, 10> Fids;
516 const SourceManager& SM = PP.getSourceManager();
517 const LangOptions &LangOpts = PP.getLangOpts();
519 auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
520 AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
521 ArrayRef<SourceRange> Ranges = Piece.getRanges();
522 for (const SourceRange &Range : Ranges) {
523 AddFID(FM, Fids, SM, Range.getBegin());
524 AddFID(FM, Fids, SM, Range.getEnd());
528 for (const PathDiagnostic *D : Diags) {
530 SmallVector<const PathPieces *, 5> WorkList;
531 WorkList.push_back(&D->path);
533 while (!WorkList.empty()) {
534 const PathPieces &Path = *WorkList.pop_back_val();
536 for (const auto &Iter : Path) {
537 const PathDiagnosticPiece &Piece = *Iter;
540 if (const PathDiagnosticCallPiece *Call =
541 dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
542 if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
543 AddPieceFID(*CallEnterWithin);
545 if (auto CallEnterEvent = Call->getCallEnterEvent())
546 AddPieceFID(*CallEnterEvent);
548 WorkList.push_back(&Call->path);
549 } else if (const PathDiagnosticMacroPiece *Macro =
550 dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
551 WorkList.push_back(&Macro->subPieces);
559 llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::F_Text);
561 llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
567 // Write the root object: a <dict> containing...
568 // - "clang_version", the string representation of clang version
569 // - "files", an <array> mapping from FIDs to file names
570 // - "diagnostics", an <array> containing the path diagnostics
572 " <key>clang_version</key>\n";
573 EmitString(o, getClangFullVersion()) << '\n';
574 o << " <key>diagnostics</key>\n"
577 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
578 DE = Diags.end(); DI!=DE; ++DI) {
582 const PathDiagnostic *D = *DI;
583 printBugPath(o, FM, AnOpts, PP, D->path);
585 // Output the bug type and bug category.
586 o << " <key>description</key>";
587 EmitString(o, D->getShortDescription()) << '\n';
588 o << " <key>category</key>";
589 EmitString(o, D->getCategory()) << '\n';
590 o << " <key>type</key>";
591 EmitString(o, D->getBugType()) << '\n';
592 o << " <key>check_name</key>";
593 EmitString(o, D->getCheckName()) << '\n';
595 o << " <!-- This hash is experimental and going to change! -->\n";
596 o << " <key>issue_hash_content_of_line_in_context</key>";
597 PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
598 FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
599 ? UPDLoc.asLocation()
600 : D->getLocation().asLocation()),
602 const Decl *DeclWithIssue = D->getDeclWithIssue();
603 EmitString(o, GetIssueHash(SM, L, D->getCheckName(), D->getBugType(),
604 DeclWithIssue, LangOpts))
607 // Output information about the semantic context where
608 // the issue occurred.
609 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
610 // FIXME: handle blocks, which have no name.
611 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
613 switch (ND->getKind()) {
614 case Decl::CXXRecord:
615 declKind = "C++ class";
617 case Decl::CXXMethod:
618 declKind = "C++ method";
620 case Decl::ObjCMethod:
621 declKind = "Objective-C method";
624 declKind = "function";
629 if (!declKind.empty()) {
630 const std::string &declName = ND->getDeclName().getAsString();
631 o << " <key>issue_context_kind</key>";
632 EmitString(o, declKind) << '\n';
633 o << " <key>issue_context</key>";
634 EmitString(o, declName) << '\n';
637 // Output the bug hash for issue unique-ing. Currently, it's just an
638 // offset from the beginning of the function.
639 if (const Stmt *Body = DeclWithIssue->getBody()) {
641 // If the bug uniqueing location exists, use it for the hash.
642 // For example, this ensures that two leaks reported on the same line
643 // will have different issue_hashes and that the hash will identify
644 // the leak location even after code is added between the allocation
645 // site and the end of scope (leak report location).
646 if (UPDLoc.isValid()) {
649 D->getUniqueingDecl()->getBody()->getBeginLoc()),
651 o << " <key>issue_hash_function_offset</key><string>"
652 << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
655 // Otherwise, use the location on which the bug is reported.
657 FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
658 o << " <key>issue_hash_function_offset</key><string>"
659 << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
667 // Output the location of the bug.
668 o << " <key>location</key>\n";
669 EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
671 // Output the diagnostic to the sub-diagnostic client, if any.
672 if (!filesMade->empty()) {
674 PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
676 for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
677 CE = files->end(); CI != CE; ++CI) {
678 StringRef newName = CI->first;
679 if (newName != lastName) {
680 if (!lastName.empty()) {
684 o << " <key>" << lastName << "_files</key>\n";
687 o << " <string>" << CI->second << "</string>\n";
693 printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
695 // Close up the entry.
701 o << " <key>files</key>\n"
703 for (FileID FID : Fids)
704 EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
707 if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
708 o << " <key>statistics</key>\n";
710 llvm::raw_string_ostream os(stats);
711 llvm::PrintStatisticsJSON(os);
713 EmitString(o, html::EscapeText(stats)) << '\n';
717 o << "</dict>\n</plist>";
720 //===----------------------------------------------------------------------===//
721 // Declarations of helper functions and data structures for expanding macros.
722 //===----------------------------------------------------------------------===//
726 using ExpArgTokens = llvm::SmallVector<Token, 2>;
728 /// Maps unexpanded macro arguments to expanded arguments. A macro argument may
729 /// need to expanded further when it is nested inside another macro.
730 class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
732 void expandFromPrevMacro(const MacroArgMap &Super);
735 struct MacroNameAndArgs {
737 const MacroInfo *MI = nullptr;
740 MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
741 : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
745 llvm::raw_ostream &OS;
746 const Preprocessor &PP;
748 Token PrevTok, PrevPrevTok;
749 TokenConcatenation ConcatInfo;
752 TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
753 : OS(OS), PP(PP), ConcatInfo(PP) {
754 PrevTok.setKind(tok::unknown);
755 PrevPrevTok.setKind(tok::unknown);
758 void printToken(const Token &Tok);
761 } // end of anonymous namespace
763 /// The implementation method of getMacroExpansion: It prints the expansion of
764 /// a macro to \p Printer, and returns with the name of the macro.
766 /// Since macros can be nested in one another, this function may call itself
769 /// Unfortunately, macro arguments have to expanded manually. To understand why,
770 /// observe the following example:
772 /// #define PRINT(x) print(x)
773 /// #define DO_SOMETHING(str) PRINT(str)
775 /// DO_SOMETHING("Cute panda cubs.");
777 /// As we expand the last line, we'll immediately replace PRINT(str) with
778 /// print(x). The information that both 'str' and 'x' refers to the same string
779 /// is an information we have to forward, hence the argument \p PrevArgs.
780 static std::string getMacroNameAndPrintExpansion(TokenPrinter &Printer,
781 SourceLocation MacroLoc,
782 const Preprocessor &PP,
783 const MacroArgMap &PrevArgs);
785 /// Retrieves the name of the macro and what it's arguments expand into
788 /// For example, for the following macro expansion:
790 /// #define SET_TO_NULL(x) x = 0
791 /// #define NOT_SUSPICIOUS(a) \
797 /// int *ptr = new int(4);
798 /// NOT_SUSPICIOUS(&ptr);
801 /// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
802 /// and the MacroArgMap map { (a, &ptr) } will be returned.
804 /// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
805 /// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
806 /// { (x, a) } will be returned.
807 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
808 const Preprocessor &PP);
810 /// Retrieves the ')' token that matches '(' \p It points to.
811 static MacroInfo::tokens_iterator getMatchingRParen(
812 MacroInfo::tokens_iterator It,
813 MacroInfo::tokens_iterator End);
815 /// Retrieves the macro info for \p II refers to at \p Loc. This is important
816 /// because macros can be redefined or undefined.
817 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
818 const SourceManager &SM,
819 const IdentifierInfo *II,
822 //===----------------------------------------------------------------------===//
823 // Definitions of helper functions and methods for expanding macros.
824 //===----------------------------------------------------------------------===//
826 static ExpansionInfo getExpandedMacro(SourceLocation MacroLoc,
827 const Preprocessor &PP) {
829 llvm::SmallString<200> ExpansionBuf;
830 llvm::raw_svector_ostream OS(ExpansionBuf);
831 TokenPrinter Printer(OS, PP);
832 std::string MacroName =
833 getMacroNameAndPrintExpansion(Printer, MacroLoc, PP, MacroArgMap{});
834 return { MacroName, OS.str() };
837 static std::string getMacroNameAndPrintExpansion(TokenPrinter &Printer,
838 SourceLocation MacroLoc,
839 const Preprocessor &PP,
840 const MacroArgMap &PrevArgs) {
842 const SourceManager &SM = PP.getSourceManager();
844 MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
846 // Manually expand its arguments from the previous macro.
847 Info.Args.expandFromPrevMacro(PrevArgs);
849 // Iterate over the macro's tokens and stringify them.
850 for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
854 // If this token is not an identifier, we only need to print it.
855 if (T.isNot(tok::identifier)) {
856 Printer.printToken(T);
860 const auto *II = T.getIdentifierInfo();
862 "This token is an identifier but has no IdentifierInfo!");
864 // If this token is a macro that should be expanded inside the current
866 if (const MacroInfo *MI =
867 getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
868 getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args);
870 // If this is a function-like macro, skip its arguments, as
871 // getExpandedMacro() already printed them. If this is the case, let's
872 // first jump to the '(' token.
873 if (MI->getNumParams() != 0)
874 It = getMatchingRParen(++It, E);
878 // If this token is the current macro's argument, we should expand it.
879 auto ArgMapIt = Info.Args.find(II);
880 if (ArgMapIt != Info.Args.end()) {
881 for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
882 ArgEnd = ArgMapIt->second.end();
883 ArgIt != ArgEnd; ++ArgIt) {
885 // These tokens may still be macros, if that is the case, handle it the
886 // same way we did above.
887 const auto *ArgII = ArgIt->getIdentifierInfo();
889 Printer.printToken(*ArgIt);
893 const auto *MI = PP.getMacroInfo(ArgII);
895 Printer.printToken(*ArgIt);
899 getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
901 if (MI->getNumParams() != 0)
902 ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
907 // If control reached here, then this token isn't a macro identifier, nor an
908 // unexpanded macro argument that we need to handle, print it.
909 Printer.printToken(T);
915 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
916 const Preprocessor &PP) {
918 const SourceManager &SM = PP.getSourceManager();
919 const LangOptions &LangOpts = PP.getLangOpts();
921 // First, we create a Lexer to lex *at the expansion location* the tokens
922 // referring to the macro's name and its arguments.
923 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
924 const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
925 const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
927 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
928 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
930 // Acquire the macro's name.
932 RawLexer.LexFromRawLexer(TheTok);
934 std::string MacroName = PP.getSpelling(TheTok);
936 const auto *II = PP.getIdentifierInfo(MacroName);
937 assert(II && "Failed to acquire the IndetifierInfo for the macro!");
939 const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
940 assert(MI && "The macro must've been defined at it's expansion location!");
942 // Acquire the macro's arguments.
944 // The rough idea here is to lex from the first left parentheses to the last
945 // right parentheses, and map the macro's unexpanded arguments to what they
946 // will be expanded to. An expanded macro argument may contain several tokens
947 // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
948 // which point we start lexing the next argument or finish.
949 ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
950 if (MacroArgs.empty())
951 return { MacroName, MI, {} };
953 RawLexer.LexFromRawLexer(TheTok);
954 assert(TheTok.is(tok::l_paren) &&
955 "The token after the macro's identifier token should be '('!");
959 // When the macro's argument is a function call, like
960 // CALL_FN(someFunctionName(param1, param2))
961 // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
962 // actual macro arguments, or do not represent the macro argument's closing
963 // parentheses, so we'll count how many parentheses aren't closed yet.
964 // If ParanthesesDepth
965 // * = 0, then there are no more arguments to lex.
966 // * = 1, then if we find a tok::comma, we can start lexing the next arg.
967 // * > 1, then tok::comma is a part of the current arg.
968 int ParenthesesDepth = 1;
970 // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
971 // even if we lex a tok::comma and ParanthesesDepth == 1.
972 const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
974 for (const IdentifierInfo *UnexpArgII : MacroArgs) {
975 MacroArgMap::mapped_type ExpandedArgTokens;
977 // One could also simply not supply a single argument to __VA_ARGS__ -- this
978 // results in a preprocessor warning, but is not an error:
979 // #define VARIADIC(ptr, ...) \
980 // someVariadicTemplateFunction(__VA_ARGS__)
983 // VARIADIC(ptr); // Note that there are no commas, this isn't just an
984 // // empty parameter -- there are no parameters for '...'.
985 // In any other case, ParenthesesDepth mustn't be 0 here.
986 if (ParenthesesDepth != 0) {
988 // Lex the first token of the next macro parameter.
989 RawLexer.LexFromRawLexer(TheTok);
991 while (!(ParenthesesDepth == 1 &&
992 (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
993 assert(TheTok.isNot(tok::eof) &&
994 "EOF encountered while looking for expanded macro args!");
996 if (TheTok.is(tok::l_paren))
999 if (TheTok.is(tok::r_paren))
1002 if (ParenthesesDepth == 0)
1005 if (TheTok.is(tok::raw_identifier))
1006 PP.LookUpIdentifierInfo(TheTok);
1008 ExpandedArgTokens.push_back(TheTok);
1009 RawLexer.LexFromRawLexer(TheTok);
1012 assert(UnexpArgII == __VA_ARGS__II);
1015 Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1018 assert(TheTok.is(tok::r_paren) &&
1019 "Expanded macro argument acquisition failed! After the end of the loop"
1020 " this token should be ')'!");
1022 return { MacroName, MI, Args };
1025 static MacroInfo::tokens_iterator getMatchingRParen(
1026 MacroInfo::tokens_iterator It,
1027 MacroInfo::tokens_iterator End) {
1029 assert(It->is(tok::l_paren) && "This token should be '('!");
1031 // Skip until we find the closing ')'.
1032 int ParenthesesDepth = 1;
1033 while (ParenthesesDepth != 0) {
1036 assert(It->isNot(tok::eof) &&
1037 "Encountered EOF while attempting to skip macro arguments!");
1039 "End of the macro definition reached before finding ')'!");
1041 if (It->is(tok::l_paren))
1044 if (It->is(tok::r_paren))
1050 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1051 const SourceManager &SM,
1052 const IdentifierInfo *II,
1053 SourceLocation Loc) {
1055 const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1059 return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1062 void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1064 for (value_type &Pair : *this) {
1065 ExpArgTokens &CurrExpArgTokens = Pair.second;
1067 // For each token in the expanded macro argument.
1068 auto It = CurrExpArgTokens.begin();
1069 while (It != CurrExpArgTokens.end()) {
1070 if (It->isNot(tok::identifier)) {
1075 const auto *II = It->getIdentifierInfo();
1078 // Is this an argument that "Super" expands further?
1079 if (!Super.count(II)) {
1084 const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1086 It = CurrExpArgTokens.insert(
1087 It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1088 std::advance(It, SuperExpArgTokens.size());
1089 It = CurrExpArgTokens.erase(It);
1094 void TokenPrinter::printToken(const Token &Tok) {
1095 // If this is the first token to be printed, don't print space.
1096 if (PrevTok.isNot(tok::unknown)) {
1097 // If the tokens were already space separated, or if they must be to avoid
1098 // them being implicitly pasted, add a space between them.
1099 if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1101 // AvoidConcat doesn't check for ##, don't print a space around it.
1102 if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1108 if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1109 if (PrevTok.is(tok::hash))
1110 OS << '\"' << PP.getSpelling(Tok) << '\"';
1112 OS << PP.getSpelling(Tok);
1115 PrevPrevTok = PrevTok;