1 //===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the PlistDiagnostics object.
11 //===----------------------------------------------------------------------===//
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"
31 using namespace clang;
33 using namespace markup;
35 //===----------------------------------------------------------------------===//
36 // Declarations of helper classes and functions for emitting bug reports in
38 //===----------------------------------------------------------------------===//
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;
48 PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &prefix,
49 const Preprocessor &PP,
50 const cross_tu::CrossTranslationUnitContext &CTU,
51 bool supportsMultipleFiles);
53 ~PlistDiagnostics() override {}
55 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
56 FilesMade *filesMade) override;
58 StringRef getName() const override {
59 return "PlistDiagnostics";
62 PathGenerationScheme getGenerationScheme() const override {
65 bool supportsLogicalOpControlFlow() const override { return true; }
66 bool supportsCrossFileDiagnostics() const override {
67 return SupportsCrossFileDiagnostics;
70 } // end anonymous namespace
74 /// A helper class for emitting a single report.
77 AnalyzerOptions &AnOpts;
78 const Preprocessor &PP;
79 const cross_tu::CrossTranslationUnitContext &CTU;
80 llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;
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) {
89 void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
90 ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);
92 // Don't emit a warning about an unused private field.
96 /// Print the expansions of the collected macro pieces.
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
102 void ReportMacroExpansions(raw_ostream &o, unsigned indent);
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);
113 case PathDiagnosticPiece::Call:
114 ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
117 case PathDiagnosticPiece::Event:
118 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
121 case PathDiagnosticPiece::Macro:
122 ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
125 case PathDiagnosticPiece::Note:
126 ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
128 case PathDiagnosticPiece::PopUp:
129 ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
134 void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
136 void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
137 void EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits, unsigned indent);
139 void ReportControlFlow(raw_ostream &o,
140 const PathDiagnosticControlFlowPiece& P,
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,
151 void ReportPopUp(raw_ostream &o, const PathDiagnosticPopUpPiece &P,
155 } // end of anonymous namespace
159 struct ExpansionInfo {
160 std::string MacroName;
161 std::string Expansion;
162 ExpansionInfo(std::string N, std::string E)
163 : MacroName(std::move(N)), Expansion(std::move(E)) {}
166 } // end of anonymous namespace
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);
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,
179 llvm::raw_fd_ostream &o);
182 getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
183 const cross_tu::CrossTranslationUnitContext &CTU);
185 //===----------------------------------------------------------------------===//
186 // Methods of PlistPrinter.
187 //===----------------------------------------------------------------------===//
189 void PlistPrinter::EmitRanges(raw_ostream &o,
190 const ArrayRef<SourceRange> Ranges,
196 Indent(o, indent) << "<key>ranges</key>\n";
197 Indent(o, indent) << "<array>\n";
200 const SourceManager &SM = PP.getSourceManager();
201 const LangOptions &LangOpts = PP.getLangOpts();
203 for (auto &R : Ranges)
205 Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
208 Indent(o, indent) << "</array>\n";
211 void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
214 assert(!Message.empty());
215 Indent(o, indent) << "<key>extended_message</key>\n";
217 EmitString(o, Message) << '\n';
219 // Output the short text.
220 // FIXME: Really use a short string.
221 Indent(o, indent) << "<key>message</key>\n";
223 EmitString(o, Message) << '\n';
226 void PlistPrinter::EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits,
228 if (fixits.size() == 0)
231 const SourceManager &SM = PP.getSourceManager();
232 const LangOptions &LangOpts = PP.getLangOpts();
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),
245 Indent(o, indent) << " <key>insert_string</key>";
246 EmitString(o, fixit.CodeToInsert);
248 Indent(o, indent) << " </dict>\n";
250 Indent(o, indent) << "</array>\n";
253 void PlistPrinter::ReportControlFlow(raw_ostream &o,
254 const PathDiagnosticControlFlowPiece& P,
257 const SourceManager &SM = PP.getSourceManager();
258 const LangOptions &LangOpts = PP.getLangOpts();
260 Indent(o, indent) << "<dict>\n";
263 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
266 Indent(o, indent) << "<key>edges</key>\n";
268 Indent(o, indent) << "<array>\n";
270 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
272 Indent(o, indent) << "<dict>\n";
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,
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,
290 Indent(o, indent) << "</dict>\n";
293 Indent(o, indent) << "</array>\n";
296 // Output any helper text.
297 const auto &s = P.getString();
299 Indent(o, indent) << "<key>alternate</key>";
300 EmitString(o, s) << '\n';
303 assert(P.getFixits().size() == 0 &&
304 "Fixits on constrol flow pieces are not implemented yet!");
307 Indent(o, indent) << "</dict>\n";
310 void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
311 unsigned indent, unsigned depth,
314 const SourceManager &SM = PP.getSourceManager();
316 Indent(o, indent) << "<dict>\n";
319 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
322 Indent(o, indent) << "<key>key_event</key><true/>\n";
325 // Output the location.
326 FullSourceLoc L = P.getLocation().asLocation();
328 Indent(o, indent) << "<key>location</key>\n";
329 EmitLocation(o, SM, L, FM, indent);
331 // Output the ranges (if any).
332 ArrayRef<SourceRange> Ranges = P.getRanges();
333 EmitRanges(o, Ranges, indent);
335 // Output the call depth.
336 Indent(o, indent) << "<key>depth</key>";
337 EmitInteger(o, depth) << '\n';
340 EmitMessage(o, P.getString(), indent);
342 // Output the fixits.
343 EmitFixits(o, P.getFixits(), indent);
347 Indent(o, indent); o << "</dict>\n";
350 void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
354 if (auto callEnter = P.getCallEnterEvent())
355 ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
356 P.isLastInMainSourceFile());
361 if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
362 ReportPiece(o, *callEnterWithinCaller, indent, depth,
363 /*includeControlFlow*/ true);
365 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
366 ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);
370 if (auto callExit = P.getCallExitEvent())
371 ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);
373 assert(P.getFixits().size() == 0 &&
374 "Fixits on call pieces are not implemented yet!");
377 void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
378 const PathDiagnosticMacroPiece& P,
379 unsigned indent, unsigned depth) {
380 MacroPieces.push_back(&P);
382 for (PathPieces::const_iterator I = P.subPieces.begin(),
383 E = P.subPieces.end();
385 ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
388 assert(P.getFixits().size() == 0 &&
389 "Fixits on constrol flow pieces are not implemented yet!");
392 void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
394 for (const PathDiagnosticMacroPiece *P : MacroPieces) {
395 const SourceManager &SM = PP.getSourceManager();
396 ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU);
398 Indent(o, indent) << "<dict>\n";
401 // Output the location.
402 FullSourceLoc L = P->getLocation().asLocation();
404 Indent(o, indent) << "<key>location</key>\n";
405 EmitLocation(o, SM, L, FM, indent);
407 // Output the ranges (if any).
408 ArrayRef<SourceRange> Ranges = P->getRanges();
409 EmitRanges(o, Ranges, indent);
411 // Output the macro name.
412 Indent(o, indent) << "<key>name</key>";
413 EmitString(o, EI.MacroName) << '\n';
415 // Output what it expands into.
416 Indent(o, indent) << "<key>expansion</key>";
417 EmitString(o, EI.Expansion) << '\n';
426 void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
429 const SourceManager &SM = PP.getSourceManager();
431 Indent(o, indent) << "<dict>\n";
434 // Output the location.
435 FullSourceLoc L = P.getLocation().asLocation();
437 Indent(o, indent) << "<key>location</key>\n";
438 EmitLocation(o, SM, L, FM, indent);
440 // Output the ranges (if any).
441 ArrayRef<SourceRange> Ranges = P.getRanges();
442 EmitRanges(o, Ranges, indent);
445 EmitMessage(o, P.getString(), indent);
447 // Output the fixits.
448 EmitFixits(o, P.getFixits(), indent);
452 Indent(o, indent); o << "</dict>\n";
455 void PlistPrinter::ReportPopUp(raw_ostream &o,
456 const PathDiagnosticPopUpPiece &P,
458 const SourceManager &SM = PP.getSourceManager();
460 Indent(o, indent) << "<dict>\n";
463 Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";
465 // Output the location.
466 FullSourceLoc L = P.getLocation().asLocation();
468 Indent(o, indent) << "<key>location</key>\n";
469 EmitLocation(o, SM, L, FM, indent);
471 // Output the ranges (if any).
472 ArrayRef<SourceRange> Ranges = P.getRanges();
473 EmitRanges(o, Ranges, indent);
476 EmitMessage(o, P.getString(), indent);
478 assert(P.getFixits().size() == 0 &&
479 "Fixits on pop-up pieces are not implemented yet!");
483 Indent(o, indent) << "</dict>\n";
486 //===----------------------------------------------------------------------===//
487 // Static function definitions.
488 //===----------------------------------------------------------------------===//
490 /// Print coverage information to output stream {@code o}.
491 /// May modify the used list of files {@code Fids} by inserting new ones.
492 static void printCoverage(const PathDiagnostic *D,
493 unsigned InputIndentLevel,
494 SmallVectorImpl<FileID> &Fids,
496 llvm::raw_fd_ostream &o) {
497 unsigned IndentLevel = InputIndentLevel;
499 Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
500 Indent(o, IndentLevel) << "<dict>\n";
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";
510 for (unsigned LineNo : I->second) {
511 Indent(o, IndentLevel);
512 EmitInteger(o, LineNo) << "\n";
515 Indent(o, IndentLevel) << "</array>\n";
518 Indent(o, IndentLevel) << "</dict>\n";
520 assert(IndentLevel == InputIndentLevel);
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;
532 "PathDiagnostic is not partitioned so that notes precede the rest");
534 PathPieces::const_iterator FirstNonNote = std::partition_point(
535 Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) {
536 return E->getKind() == PathDiagnosticPiece::Note;
539 PathPieces::const_iterator I = Path.begin();
541 if (FirstNonNote != Path.begin()) {
542 o << " <key>notes</key>\n"
545 for (; I != FirstNonNote; ++I)
546 Printer.ReportDiag(o, **I);
551 o << " <key>path</key>\n";
555 for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
556 Printer.ReportDiag(o, **I);
560 if (!AnOpts.ShouldDisplayMacroExpansions)
563 o << " <key>macro_expansions</key>\n"
565 Printer.ReportMacroExpansions(o, /* indent */ 4);
569 //===----------------------------------------------------------------------===//
570 // Methods of PlistDiagnostics.
571 //===----------------------------------------------------------------------===//
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.
583 void ento::createPlistDiagnosticConsumer(
584 AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
585 const std::string &s, const Preprocessor &PP,
586 const cross_tu::CrossTranslationUnitContext &CTU) {
587 C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
588 /*supportsMultipleFiles*/ false));
591 void ento::createPlistMultiFileDiagnosticConsumer(
592 AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
593 const std::string &s, const Preprocessor &PP,
594 const cross_tu::CrossTranslationUnitContext &CTU) {
595 C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
596 /*supportsMultipleFiles*/ true));
598 void PlistDiagnostics::FlushDiagnosticsImpl(
599 std::vector<const PathDiagnostic *> &Diags,
600 FilesMade *filesMade) {
601 // Build up a set of FIDs that we use by scanning the locations and
602 // ranges of the diagnostics.
604 SmallVector<FileID, 10> Fids;
605 const SourceManager& SM = PP.getSourceManager();
606 const LangOptions &LangOpts = PP.getLangOpts();
608 auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
609 AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
610 ArrayRef<SourceRange> Ranges = Piece.getRanges();
611 for (const SourceRange &Range : Ranges) {
612 AddFID(FM, Fids, SM, Range.getBegin());
613 AddFID(FM, Fids, SM, Range.getEnd());
617 for (const PathDiagnostic *D : Diags) {
619 SmallVector<const PathPieces *, 5> WorkList;
620 WorkList.push_back(&D->path);
622 while (!WorkList.empty()) {
623 const PathPieces &Path = *WorkList.pop_back_val();
625 for (const auto &Iter : Path) {
626 const PathDiagnosticPiece &Piece = *Iter;
629 if (const PathDiagnosticCallPiece *Call =
630 dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
631 if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
632 AddPieceFID(*CallEnterWithin);
634 if (auto CallEnterEvent = Call->getCallEnterEvent())
635 AddPieceFID(*CallEnterEvent);
637 WorkList.push_back(&Call->path);
638 } else if (const PathDiagnosticMacroPiece *Macro =
639 dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
640 WorkList.push_back(&Macro->subPieces);
648 llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
650 llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
656 // Write the root object: a <dict> containing...
657 // - "clang_version", the string representation of clang version
658 // - "files", an <array> mapping from FIDs to file names
659 // - "diagnostics", an <array> containing the path diagnostics
661 " <key>clang_version</key>\n";
662 EmitString(o, getClangFullVersion()) << '\n';
663 o << " <key>diagnostics</key>\n"
666 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
667 DE = Diags.end(); DI!=DE; ++DI) {
671 const PathDiagnostic *D = *DI;
672 printBugPath(o, FM, AnOpts, PP, CTU, D->path);
674 // Output the bug type and bug category.
675 o << " <key>description</key>";
676 EmitString(o, D->getShortDescription()) << '\n';
677 o << " <key>category</key>";
678 EmitString(o, D->getCategory()) << '\n';
679 o << " <key>type</key>";
680 EmitString(o, D->getBugType()) << '\n';
681 o << " <key>check_name</key>";
682 EmitString(o, D->getCheckerName()) << '\n';
684 o << " <!-- This hash is experimental and going to change! -->\n";
685 o << " <key>issue_hash_content_of_line_in_context</key>";
686 PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
687 FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
688 ? UPDLoc.asLocation()
689 : D->getLocation().asLocation()),
691 const Decl *DeclWithIssue = D->getDeclWithIssue();
692 EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
693 DeclWithIssue, LangOpts))
696 // Output information about the semantic context where
697 // the issue occurred.
698 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
699 // FIXME: handle blocks, which have no name.
700 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
702 switch (ND->getKind()) {
703 case Decl::CXXRecord:
704 declKind = "C++ class";
706 case Decl::CXXMethod:
707 declKind = "C++ method";
709 case Decl::ObjCMethod:
710 declKind = "Objective-C method";
713 declKind = "function";
718 if (!declKind.empty()) {
719 const std::string &declName = ND->getDeclName().getAsString();
720 o << " <key>issue_context_kind</key>";
721 EmitString(o, declKind) << '\n';
722 o << " <key>issue_context</key>";
723 EmitString(o, declName) << '\n';
726 // Output the bug hash for issue unique-ing. Currently, it's just an
727 // offset from the beginning of the function.
728 if (const Stmt *Body = DeclWithIssue->getBody()) {
730 // If the bug uniqueing location exists, use it for the hash.
731 // For example, this ensures that two leaks reported on the same line
732 // will have different issue_hashes and that the hash will identify
733 // the leak location even after code is added between the allocation
734 // site and the end of scope (leak report location).
735 if (UPDLoc.isValid()) {
738 D->getUniqueingDecl()->getBody()->getBeginLoc()),
740 o << " <key>issue_hash_function_offset</key><string>"
741 << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
744 // Otherwise, use the location on which the bug is reported.
746 FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
747 o << " <key>issue_hash_function_offset</key><string>"
748 << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
756 // Output the location of the bug.
757 o << " <key>location</key>\n";
758 EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);
760 // Output the diagnostic to the sub-diagnostic client, if any.
761 if (!filesMade->empty()) {
763 PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
765 for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
766 CE = files->end(); CI != CE; ++CI) {
767 StringRef newName = CI->first;
768 if (newName != lastName) {
769 if (!lastName.empty()) {
773 o << " <key>" << lastName << "_files</key>\n";
776 o << " <string>" << CI->second << "</string>\n";
782 printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);
784 // Close up the entry.
790 o << " <key>files</key>\n"
792 for (FileID FID : Fids)
793 EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
796 if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
797 o << " <key>statistics</key>\n";
799 llvm::raw_string_ostream os(stats);
800 llvm::PrintStatisticsJSON(os);
802 EmitString(o, html::EscapeText(stats)) << '\n';
806 o << "</dict>\n</plist>\n";
809 //===----------------------------------------------------------------------===//
810 // Declarations of helper functions and data structures for expanding macros.
811 //===----------------------------------------------------------------------===//
815 using ExpArgTokens = llvm::SmallVector<Token, 2>;
817 /// Maps unexpanded macro arguments to expanded arguments. A macro argument may
818 /// need to expanded further when it is nested inside another macro.
819 class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
821 void expandFromPrevMacro(const MacroArgMap &Super);
824 struct MacroNameAndArgs {
826 const MacroInfo *MI = nullptr;
829 MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
830 : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
834 llvm::raw_ostream &OS;
835 const Preprocessor &PP;
837 Token PrevTok, PrevPrevTok;
838 TokenConcatenation ConcatInfo;
841 TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
842 : OS(OS), PP(PP), ConcatInfo(PP) {
843 PrevTok.setKind(tok::unknown);
844 PrevPrevTok.setKind(tok::unknown);
847 void printToken(const Token &Tok);
850 } // end of anonymous namespace
852 /// The implementation method of getMacroExpansion: It prints the expansion of
853 /// a macro to \p Printer, and returns with the name of the macro.
855 /// Since macros can be nested in one another, this function may call itself
858 /// Unfortunately, macro arguments have to expanded manually. To understand why,
859 /// observe the following example:
861 /// #define PRINT(x) print(x)
862 /// #define DO_SOMETHING(str) PRINT(str)
864 /// DO_SOMETHING("Cute panda cubs.");
866 /// As we expand the last line, we'll immediately replace PRINT(str) with
867 /// print(x). The information that both 'str' and 'x' refers to the same string
868 /// is an information we have to forward, hence the argument \p PrevArgs.
870 /// To avoid infinite recursion we maintain the already processed tokens in
871 /// a set. This is carried as a parameter through the recursive calls. The set
872 /// is extended with the currently processed token and after processing it, the
873 /// token is removed. If the token is already in the set, then recursion stops:
877 static std::string getMacroNameAndPrintExpansion(
878 TokenPrinter &Printer,
879 SourceLocation MacroLoc,
880 const Preprocessor &PP,
881 const MacroArgMap &PrevArgs,
882 llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);
884 /// Retrieves the name of the macro and what it's arguments expand into
887 /// For example, for the following macro expansion:
889 /// #define SET_TO_NULL(x) x = 0
890 /// #define NOT_SUSPICIOUS(a) \
896 /// int *ptr = new int(4);
897 /// NOT_SUSPICIOUS(&ptr);
900 /// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
901 /// and the MacroArgMap map { (a, &ptr) } will be returned.
903 /// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
904 /// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
905 /// { (x, a) } will be returned.
906 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
907 const Preprocessor &PP);
909 /// Retrieves the ')' token that matches '(' \p It points to.
910 static MacroInfo::tokens_iterator getMatchingRParen(
911 MacroInfo::tokens_iterator It,
912 MacroInfo::tokens_iterator End);
914 /// Retrieves the macro info for \p II refers to at \p Loc. This is important
915 /// because macros can be redefined or undefined.
916 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
917 const SourceManager &SM,
918 const IdentifierInfo *II,
921 //===----------------------------------------------------------------------===//
922 // Definitions of helper functions and methods for expanding macros.
923 //===----------------------------------------------------------------------===//
926 getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
927 const cross_tu::CrossTranslationUnitContext &CTU) {
929 const Preprocessor *PPToUse = &PP;
930 if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
931 MacroLoc = LocAndUnit->first;
932 PPToUse = &LocAndUnit->second->getPreprocessor();
935 llvm::SmallString<200> ExpansionBuf;
936 llvm::raw_svector_ostream OS(ExpansionBuf);
937 TokenPrinter Printer(OS, *PPToUse);
938 llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;
940 std::string MacroName = getMacroNameAndPrintExpansion(
941 Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
942 return { MacroName, OS.str() };
945 static std::string getMacroNameAndPrintExpansion(
946 TokenPrinter &Printer,
947 SourceLocation MacroLoc,
948 const Preprocessor &PP,
949 const MacroArgMap &PrevArgs,
950 llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {
952 const SourceManager &SM = PP.getSourceManager();
954 MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
955 IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);
957 // TODO: If the macro definition contains another symbol then this function is
958 // called recursively. In case this symbol is the one being defined, it will
959 // be an infinite recursion which is stopped by this "if" statement. However,
960 // in this case we don't get the full expansion text in the Plist file. See
961 // the test file where "value" is expanded to "garbage_" instead of
963 if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
965 AlreadyProcessedTokens.insert(IDInfo);
970 // Manually expand its arguments from the previous macro.
971 Info.Args.expandFromPrevMacro(PrevArgs);
973 // Iterate over the macro's tokens and stringify them.
974 for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
978 // If this token is not an identifier, we only need to print it.
979 if (T.isNot(tok::identifier)) {
980 Printer.printToken(T);
984 const auto *II = T.getIdentifierInfo();
986 "This token is an identifier but has no IdentifierInfo!");
988 // If this token is a macro that should be expanded inside the current
990 if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
991 getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
992 AlreadyProcessedTokens);
994 // If this is a function-like macro, skip its arguments, as
995 // getExpandedMacro() already printed them. If this is the case, let's
996 // first jump to the '(' token.
997 auto N = std::next(It);
998 if (N != E && N->is(tok::l_paren))
999 It = getMatchingRParen(++It, E);
1003 // If this token is the current macro's argument, we should expand it.
1004 auto ArgMapIt = Info.Args.find(II);
1005 if (ArgMapIt != Info.Args.end()) {
1006 for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
1007 ArgEnd = ArgMapIt->second.end();
1008 ArgIt != ArgEnd; ++ArgIt) {
1010 // These tokens may still be macros, if that is the case, handle it the
1011 // same way we did above.
1012 const auto *ArgII = ArgIt->getIdentifierInfo();
1014 Printer.printToken(*ArgIt);
1018 const auto *MI = PP.getMacroInfo(ArgII);
1020 Printer.printToken(*ArgIt);
1024 getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
1025 Info.Args, AlreadyProcessedTokens);
1026 // Peek the next token if it is a tok::l_paren. This way we can decide
1027 // if this is the application or just a reference to a function maxro
1030 // #define apply(f) ...
1031 // #define func(x) ...
1034 auto N = std::next(ArgIt);
1035 if (N != ArgEnd && N->is(tok::l_paren))
1036 ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
1041 // If control reached here, then this token isn't a macro identifier, nor an
1042 // unexpanded macro argument that we need to handle, print it.
1043 Printer.printToken(T);
1046 AlreadyProcessedTokens.erase(IDInfo);
1051 static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
1052 const Preprocessor &PP) {
1054 const SourceManager &SM = PP.getSourceManager();
1055 const LangOptions &LangOpts = PP.getLangOpts();
1057 // First, we create a Lexer to lex *at the expansion location* the tokens
1058 // referring to the macro's name and its arguments.
1059 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
1060 const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
1061 const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;
1063 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
1064 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());
1066 // Acquire the macro's name.
1068 RawLexer.LexFromRawLexer(TheTok);
1070 std::string MacroName = PP.getSpelling(TheTok);
1072 const auto *II = PP.getIdentifierInfo(MacroName);
1073 assert(II && "Failed to acquire the IndetifierInfo for the macro!");
1075 const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
1076 // assert(MI && "The macro must've been defined at it's expansion location!");
1078 // We should always be able to obtain the MacroInfo in a given TU, but if
1079 // we're running the analyzer with CTU, the Preprocessor won't contain the
1080 // directive history (or anything for that matter) from another TU.
1081 // TODO: assert when we're not running with CTU.
1083 return { MacroName, MI, {} };
1085 // Acquire the macro's arguments.
1087 // The rough idea here is to lex from the first left parentheses to the last
1088 // right parentheses, and map the macro's unexpanded arguments to what they
1089 // will be expanded to. An expanded macro argument may contain several tokens
1090 // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
1091 // which point we start lexing the next argument or finish.
1092 ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
1093 if (MacroArgs.empty())
1094 return { MacroName, MI, {} };
1096 RawLexer.LexFromRawLexer(TheTok);
1097 // When this is a token which expands to another macro function then its
1098 // parentheses are not at its expansion locaiton. For example:
1100 // #define foo(x) int bar() { return x; }
1101 // #define apply_zero(f) f(0)
1104 // This is not a tok::l_paren, but foo is a function.
1105 if (TheTok.isNot(tok::l_paren))
1106 return { MacroName, MI, {} };
1110 // When the macro's argument is a function call, like
1111 // CALL_FN(someFunctionName(param1, param2))
1112 // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
1113 // actual macro arguments, or do not represent the macro argument's closing
1114 // parentheses, so we'll count how many parentheses aren't closed yet.
1115 // If ParanthesesDepth
1116 // * = 0, then there are no more arguments to lex.
1117 // * = 1, then if we find a tok::comma, we can start lexing the next arg.
1118 // * > 1, then tok::comma is a part of the current arg.
1119 int ParenthesesDepth = 1;
1121 // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
1122 // even if we lex a tok::comma and ParanthesesDepth == 1.
1123 const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");
1125 for (const IdentifierInfo *UnexpArgII : MacroArgs) {
1126 MacroArgMap::mapped_type ExpandedArgTokens;
1128 // One could also simply not supply a single argument to __VA_ARGS__ -- this
1129 // results in a preprocessor warning, but is not an error:
1130 // #define VARIADIC(ptr, ...) \
1131 // someVariadicTemplateFunction(__VA_ARGS__)
1134 // VARIADIC(ptr); // Note that there are no commas, this isn't just an
1135 // // empty parameter -- there are no parameters for '...'.
1136 // In any other case, ParenthesesDepth mustn't be 0 here.
1137 if (ParenthesesDepth != 0) {
1139 // Lex the first token of the next macro parameter.
1140 RawLexer.LexFromRawLexer(TheTok);
1142 while (!(ParenthesesDepth == 1 &&
1143 (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
1144 assert(TheTok.isNot(tok::eof) &&
1145 "EOF encountered while looking for expanded macro args!");
1147 if (TheTok.is(tok::l_paren))
1150 if (TheTok.is(tok::r_paren))
1153 if (ParenthesesDepth == 0)
1156 if (TheTok.is(tok::raw_identifier))
1157 PP.LookUpIdentifierInfo(TheTok);
1159 ExpandedArgTokens.push_back(TheTok);
1160 RawLexer.LexFromRawLexer(TheTok);
1163 assert(UnexpArgII == __VA_ARGS__II);
1166 Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
1169 assert(TheTok.is(tok::r_paren) &&
1170 "Expanded macro argument acquisition failed! After the end of the loop"
1171 " this token should be ')'!");
1173 return { MacroName, MI, Args };
1176 static MacroInfo::tokens_iterator getMatchingRParen(
1177 MacroInfo::tokens_iterator It,
1178 MacroInfo::tokens_iterator End) {
1180 assert(It->is(tok::l_paren) && "This token should be '('!");
1182 // Skip until we find the closing ')'.
1183 int ParenthesesDepth = 1;
1184 while (ParenthesesDepth != 0) {
1187 assert(It->isNot(tok::eof) &&
1188 "Encountered EOF while attempting to skip macro arguments!");
1190 "End of the macro definition reached before finding ')'!");
1192 if (It->is(tok::l_paren))
1195 if (It->is(tok::r_paren))
1201 static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
1202 const SourceManager &SM,
1203 const IdentifierInfo *II,
1204 SourceLocation Loc) {
1206 const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
1210 return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
1213 void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {
1215 for (value_type &Pair : *this) {
1216 ExpArgTokens &CurrExpArgTokens = Pair.second;
1218 // For each token in the expanded macro argument.
1219 auto It = CurrExpArgTokens.begin();
1220 while (It != CurrExpArgTokens.end()) {
1221 if (It->isNot(tok::identifier)) {
1226 const auto *II = It->getIdentifierInfo();
1229 // Is this an argument that "Super" expands further?
1230 if (!Super.count(II)) {
1235 const ExpArgTokens &SuperExpArgTokens = Super.at(II);
1237 It = CurrExpArgTokens.insert(
1238 It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
1239 std::advance(It, SuperExpArgTokens.size());
1240 It = CurrExpArgTokens.erase(It);
1245 void TokenPrinter::printToken(const Token &Tok) {
1246 // If this is the first token to be printed, don't print space.
1247 if (PrevTok.isNot(tok::unknown)) {
1248 // If the tokens were already space separated, or if they must be to avoid
1249 // them being implicitly pasted, add a space between them.
1250 if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
1252 // AvoidConcat doesn't check for ##, don't print a space around it.
1253 if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
1259 if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
1260 if (PrevTok.is(tok::hash))
1261 OS << '\"' << PP.getSpelling(Tok) << '\"';
1263 OS << PP.getSpelling(Tok);
1266 PrevPrevTok = PrevTok;