xref: /llvm-project/clang/lib/StaticAnalyzer/Core/BugReporter.cpp (revision b3b0d09ccee52472691570072ac0e30f1addbb0a)
1 //===- BugReporter.cpp - Generate PathDiagnostics for bugs ----------------===//
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 BugReporter, a utility class for generating
10 //  PathDiagnostics.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
15 #include "clang/AST/ASTTypeTraits.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/ParentMap.h"
23 #include "clang/AST/ParentMapContext.h"
24 #include "clang/AST/Stmt.h"
25 #include "clang/AST/StmtCXX.h"
26 #include "clang/AST/StmtObjC.h"
27 #include "clang/Analysis/AnalysisDeclContext.h"
28 #include "clang/Analysis/CFG.h"
29 #include "clang/Analysis/CFGStmtMap.h"
30 #include "clang/Analysis/PathDiagnostic.h"
31 #include "clang/Analysis/ProgramPoint.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/SourceLocation.h"
34 #include "clang/Basic/SourceManager.h"
35 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
37 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
38 #include "clang/StaticAnalyzer/Core/BugReporter/Z3CrosscheckVisitor.h"
39 #include "clang/StaticAnalyzer/Core/Checker.h"
40 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
41 #include "clang/StaticAnalyzer/Core/CheckerRegistryData.h"
42 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
43 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
44 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
45 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
46 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h"
47 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
48 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/DenseMap.h"
51 #include "llvm/ADT/DenseSet.h"
52 #include "llvm/ADT/FoldingSet.h"
53 #include "llvm/ADT/STLExtras.h"
54 #include "llvm/ADT/SmallPtrSet.h"
55 #include "llvm/ADT/SmallString.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/Statistic.h"
58 #include "llvm/ADT/StringExtras.h"
59 #include "llvm/ADT/StringRef.h"
60 #include "llvm/ADT/iterator_range.h"
61 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/Compiler.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/Support/MemoryBuffer.h"
65 #include "llvm/Support/raw_ostream.h"
66 #include <algorithm>
67 #include <cassert>
68 #include <cstddef>
69 #include <iterator>
70 #include <memory>
71 #include <optional>
72 #include <queue>
73 #include <string>
74 #include <tuple>
75 #include <utility>
76 #include <vector>
77 
78 using namespace clang;
79 using namespace ento;
80 using namespace llvm;
81 
82 #define DEBUG_TYPE "BugReporter"
83 
84 STATISTIC(MaxBugClassSize,
85           "The maximum number of bug reports in the same equivalence class");
86 STATISTIC(MaxValidBugClassSize,
87           "The maximum number of bug reports in the same equivalence class "
88           "where at least one report is valid (not suppressed)");
89 
90 STATISTIC(NumTimesReportPassesZ3, "Number of reports passed Z3");
91 STATISTIC(NumTimesReportRefuted, "Number of reports refuted by Z3");
92 STATISTIC(NumTimesReportEQClassWasExhausted,
93           "Number of times all reports of an equivalence class was refuted");
94 
95 BugReporterVisitor::~BugReporterVisitor() = default;
96 
97 void BugReporterContext::anchor() {}
98 
99 //===----------------------------------------------------------------------===//
100 // PathDiagnosticBuilder and its associated routines and helper objects.
101 //===----------------------------------------------------------------------===//
102 
103 namespace {
104 
105 /// A (CallPiece, node assiciated with its CallEnter) pair.
106 using CallWithEntry =
107     std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>;
108 using CallWithEntryStack = SmallVector<CallWithEntry, 6>;
109 
110 /// Map from each node to the diagnostic pieces visitors emit for them.
111 using VisitorsDiagnosticsTy =
112     llvm::DenseMap<const ExplodedNode *, std::vector<PathDiagnosticPieceRef>>;
113 
114 /// A map from PathDiagnosticPiece to the LocationContext of the inlined
115 /// function call it represents.
116 using LocationContextMap =
117     llvm::DenseMap<const PathPieces *, const LocationContext *>;
118 
119 /// A helper class that contains everything needed to construct a
120 /// PathDiagnostic object. It does no much more then providing convenient
121 /// getters and some well placed asserts for extra security.
122 class PathDiagnosticConstruct {
123   /// The consumer we're constructing the bug report for.
124   const PathDiagnosticConsumer *Consumer;
125   /// Our current position in the bug path, which is owned by
126   /// PathDiagnosticBuilder.
127   const ExplodedNode *CurrentNode;
128   /// A mapping from parts of the bug path (for example, a function call, which
129   /// would span backwards from a CallExit to a CallEnter with the nodes in
130   /// between them) with the location contexts it is associated with.
131   LocationContextMap LCM;
132   const SourceManager &SM;
133 
134 public:
135   /// We keep stack of calls to functions as we're ascending the bug path.
136   /// TODO: PathDiagnostic has a stack doing the same thing, shouldn't we use
137   /// that instead?
138   CallWithEntryStack CallStack;
139   /// The bug report we're constructing. For ease of use, this field is kept
140   /// public, though some "shortcut" getters are provided for commonly used
141   /// methods of PathDiagnostic.
142   std::unique_ptr<PathDiagnostic> PD;
143 
144 public:
145   PathDiagnosticConstruct(const PathDiagnosticConsumer *PDC,
146                           const ExplodedNode *ErrorNode,
147                           const PathSensitiveBugReport *R,
148                           const Decl *AnalysisEntryPoint);
149 
150   /// \returns the location context associated with the current position in the
151   /// bug path.
152   const LocationContext *getCurrLocationContext() const {
153     assert(CurrentNode && "Already reached the root!");
154     return CurrentNode->getLocationContext();
155   }
156 
157   /// Same as getCurrLocationContext (they should always return the same
158   /// location context), but works after reaching the root of the bug path as
159   /// well.
160   const LocationContext *getLocationContextForActivePath() const {
161     return LCM.find(&PD->getActivePath())->getSecond();
162   }
163 
164   const ExplodedNode *getCurrentNode() const { return CurrentNode; }
165 
166   /// Steps the current node to its predecessor.
167   /// \returns whether we reached the root of the bug path.
168   bool ascendToPrevNode() {
169     CurrentNode = CurrentNode->getFirstPred();
170     return static_cast<bool>(CurrentNode);
171   }
172 
173   const ParentMap &getParentMap() const {
174     return getCurrLocationContext()->getParentMap();
175   }
176 
177   const SourceManager &getSourceManager() const { return SM; }
178 
179   const Stmt *getParent(const Stmt *S) const {
180     return getParentMap().getParent(S);
181   }
182 
183   void updateLocCtxMap(const PathPieces *Path, const LocationContext *LC) {
184     assert(Path && LC);
185     LCM[Path] = LC;
186   }
187 
188   const LocationContext *getLocationContextFor(const PathPieces *Path) const {
189     assert(LCM.count(Path) &&
190            "Failed to find the context associated with these pieces!");
191     return LCM.find(Path)->getSecond();
192   }
193 
194   bool isInLocCtxMap(const PathPieces *Path) const { return LCM.count(Path); }
195 
196   PathPieces &getActivePath() { return PD->getActivePath(); }
197   PathPieces &getMutablePieces() { return PD->getMutablePieces(); }
198 
199   bool shouldAddPathEdges() const { return Consumer->shouldAddPathEdges(); }
200   bool shouldAddControlNotes() const {
201     return Consumer->shouldAddControlNotes();
202   }
203   bool shouldGenerateDiagnostics() const {
204     return Consumer->shouldGenerateDiagnostics();
205   }
206   bool supportsLogicalOpControlFlow() const {
207     return Consumer->supportsLogicalOpControlFlow();
208   }
209 };
210 
211 /// Contains every contextual information needed for constructing a
212 /// PathDiagnostic object for a given bug report. This class and its fields are
213 /// immutable, and passes a BugReportConstruct object around during the
214 /// construction.
215 class PathDiagnosticBuilder : public BugReporterContext {
216   /// A linear path from the error node to the root.
217   std::unique_ptr<const ExplodedGraph> BugPath;
218   /// The bug report we're describing. Visitors create their diagnostics with
219   /// them being the last entities being able to modify it (for example,
220   /// changing interestingness here would cause inconsistencies as to how this
221   /// file and visitors construct diagnostics), hence its const.
222   const PathSensitiveBugReport *R;
223   /// The leaf of the bug path. This isn't the same as the bug reports error
224   /// node, which refers to the *original* graph, not the bug path.
225   const ExplodedNode *const ErrorNode;
226   /// The diagnostic pieces visitors emitted, which is expected to be collected
227   /// by the time this builder is constructed.
228   std::unique_ptr<const VisitorsDiagnosticsTy> VisitorsDiagnostics;
229 
230 public:
231   /// Find a non-invalidated report for a given equivalence class,  and returns
232   /// a PathDiagnosticBuilder able to construct bug reports for different
233   /// consumers. Returns std::nullopt if no valid report is found.
234   static std::optional<PathDiagnosticBuilder>
235   findValidReport(ArrayRef<PathSensitiveBugReport *> &bugReports,
236                   PathSensitiveBugReporter &Reporter);
237 
238   PathDiagnosticBuilder(
239       BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
240       PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
241       std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics);
242 
243   /// This function is responsible for generating diagnostic pieces that are
244   /// *not* provided by bug report visitors.
245   /// These diagnostics may differ depending on the consumer's settings,
246   /// and are therefore constructed separately for each consumer.
247   ///
248   /// There are two path diagnostics generation modes: with adding edges (used
249   /// for plists) and without  (used for HTML and text). When edges are added,
250   /// the path is modified to insert artificially generated edges.
251   /// Otherwise, more detailed diagnostics is emitted for block edges,
252   /// explaining the transitions in words.
253   std::unique_ptr<PathDiagnostic>
254   generate(const PathDiagnosticConsumer *PDC) const;
255 
256 private:
257   void updateStackPiecesWithMessage(PathDiagnosticPieceRef P,
258                                     const CallWithEntryStack &CallStack) const;
259   void generatePathDiagnosticsForNode(PathDiagnosticConstruct &C,
260                                       PathDiagnosticLocation &PrevLoc) const;
261 
262   void generateMinimalDiagForBlockEdge(PathDiagnosticConstruct &C,
263                                        BlockEdge BE) const;
264 
265   PathDiagnosticPieceRef
266   generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S,
267                         PathDiagnosticLocation &Start) const;
268 
269   PathDiagnosticPieceRef
270   generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst,
271                           PathDiagnosticLocation &Start) const;
272 
273   PathDiagnosticPieceRef
274   generateDiagForBinaryOP(const PathDiagnosticConstruct &C, const Stmt *T,
275                           const CFGBlock *Src, const CFGBlock *DstC) const;
276 
277   PathDiagnosticLocation
278   ExecutionContinues(const PathDiagnosticConstruct &C) const;
279 
280   PathDiagnosticLocation
281   ExecutionContinues(llvm::raw_string_ostream &os,
282                      const PathDiagnosticConstruct &C) const;
283 
284   const PathSensitiveBugReport *getBugReport() const { return R; }
285 };
286 
287 } // namespace
288 
289 //===----------------------------------------------------------------------===//
290 // Base implementation of stack hint generators.
291 //===----------------------------------------------------------------------===//
292 
293 StackHintGenerator::~StackHintGenerator() = default;
294 
295 std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
296   if (!N)
297     return getMessageForSymbolNotFound();
298 
299   ProgramPoint P = N->getLocation();
300   CallExitEnd CExit = P.castAs<CallExitEnd>();
301 
302   // FIXME: Use CallEvent to abstract this over all calls.
303   const Stmt *CallSite = CExit.getCalleeContext()->getCallSite();
304   const auto *CE = dyn_cast_or_null<CallExpr>(CallSite);
305   if (!CE)
306     return {};
307 
308   // Check if one of the parameters are set to the interesting symbol.
309   for (auto [Idx, ArgExpr] : llvm::enumerate(CE->arguments())) {
310     SVal SV = N->getSVal(ArgExpr);
311 
312     // Check if the variable corresponding to the symbol is passed by value.
313     SymbolRef AS = SV.getAsLocSymbol();
314     if (AS == Sym) {
315       return getMessageForArg(ArgExpr, Idx);
316     }
317 
318     // Check if the parameter is a pointer to the symbol.
319     if (std::optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) {
320       // Do not attempt to dereference void*.
321       if (ArgExpr->getType()->isVoidPointerType())
322         continue;
323       SVal PSV = N->getState()->getSVal(Reg->getRegion());
324       SymbolRef AS = PSV.getAsLocSymbol();
325       if (AS == Sym) {
326         return getMessageForArg(ArgExpr, Idx);
327       }
328     }
329   }
330 
331   // Check if we are returning the interesting symbol.
332   SVal SV = N->getSVal(CE);
333   SymbolRef RetSym = SV.getAsLocSymbol();
334   if (RetSym == Sym) {
335     return getMessageForReturn(CE);
336   }
337 
338   return getMessageForSymbolNotFound();
339 }
340 
341 std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE,
342                                                           unsigned ArgIndex) {
343   // Printed parameters start at 1, not 0.
344   ++ArgIndex;
345 
346   return (llvm::Twine(Msg) + " via " + std::to_string(ArgIndex) +
347           llvm::getOrdinalSuffix(ArgIndex) + " parameter").str();
348 }
349 
350 //===----------------------------------------------------------------------===//
351 // Diagnostic cleanup.
352 //===----------------------------------------------------------------------===//
353 
354 static PathDiagnosticEventPiece *
355 eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
356                             PathDiagnosticEventPiece *Y) {
357   // Prefer diagnostics that come from ConditionBRVisitor over
358   // those that came from TrackConstraintBRVisitor,
359   // unless the one from ConditionBRVisitor is
360   // its generic fallback diagnostic.
361   const void *tagPreferred = ConditionBRVisitor::getTag();
362   const void *tagLesser = TrackConstraintBRVisitor::getTag();
363 
364   if (X->getLocation() != Y->getLocation())
365     return nullptr;
366 
367   if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
368     return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X;
369 
370   if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
371     return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y;
372 
373   return nullptr;
374 }
375 
376 /// An optimization pass over PathPieces that removes redundant diagnostics
377 /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor.  Both
378 /// BugReporterVisitors use different methods to generate diagnostics, with
379 /// one capable of emitting diagnostics in some cases but not in others.  This
380 /// can lead to redundant diagnostic pieces at the same point in a path.
381 static void removeRedundantMsgs(PathPieces &path) {
382   unsigned N = path.size();
383   if (N < 2)
384     return;
385   // NOTE: this loop intentionally is not using an iterator.  Instead, we
386   // are streaming the path and modifying it in place.  This is done by
387   // grabbing the front, processing it, and if we decide to keep it append
388   // it to the end of the path.  The entire path is processed in this way.
389   for (unsigned i = 0; i < N; ++i) {
390     auto piece = std::move(path.front());
391     path.pop_front();
392 
393     switch (piece->getKind()) {
394       case PathDiagnosticPiece::Call:
395         removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
396         break;
397       case PathDiagnosticPiece::Macro:
398         removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
399         break;
400       case PathDiagnosticPiece::Event: {
401         if (i == N-1)
402           break;
403 
404         if (auto *nextEvent =
405             dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
406           auto *event = cast<PathDiagnosticEventPiece>(piece.get());
407           // Check to see if we should keep one of the two pieces.  If we
408           // come up with a preference, record which piece to keep, and consume
409           // another piece from the path.
410           if (auto *pieceToKeep =
411                   eventsDescribeSameCondition(event, nextEvent)) {
412             piece = std::move(pieceToKeep == event ? piece : path.front());
413             path.pop_front();
414             ++i;
415           }
416         }
417         break;
418       }
419       case PathDiagnosticPiece::ControlFlow:
420       case PathDiagnosticPiece::Note:
421       case PathDiagnosticPiece::PopUp:
422         break;
423     }
424     path.push_back(std::move(piece));
425   }
426 }
427 
428 /// Recursively scan through a path and prune out calls and macros pieces
429 /// that aren't needed.  Return true if afterwards the path contains
430 /// "interesting stuff" which means it shouldn't be pruned from the parent path.
431 static bool removeUnneededCalls(const PathDiagnosticConstruct &C,
432                                 PathPieces &pieces,
433                                 const PathSensitiveBugReport *R,
434                                 bool IsInteresting = false) {
435   bool containsSomethingInteresting = IsInteresting;
436   const unsigned N = pieces.size();
437 
438   for (unsigned i = 0 ; i < N ; ++i) {
439     // Remove the front piece from the path.  If it is still something we
440     // want to keep once we are done, we will push it back on the end.
441     auto piece = std::move(pieces.front());
442     pieces.pop_front();
443 
444     switch (piece->getKind()) {
445       case PathDiagnosticPiece::Call: {
446         auto &call = cast<PathDiagnosticCallPiece>(*piece);
447         // Check if the location context is interesting.
448         if (!removeUnneededCalls(
449                 C, call.path, R,
450                 R->isInteresting(C.getLocationContextFor(&call.path))))
451           continue;
452 
453         containsSomethingInteresting = true;
454         break;
455       }
456       case PathDiagnosticPiece::Macro: {
457         auto &macro = cast<PathDiagnosticMacroPiece>(*piece);
458         if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting))
459           continue;
460         containsSomethingInteresting = true;
461         break;
462       }
463       case PathDiagnosticPiece::Event: {
464         auto &event = cast<PathDiagnosticEventPiece>(*piece);
465 
466         // We never throw away an event, but we do throw it away wholesale
467         // as part of a path if we throw the entire path away.
468         containsSomethingInteresting |= !event.isPrunable();
469         break;
470       }
471       case PathDiagnosticPiece::ControlFlow:
472       case PathDiagnosticPiece::Note:
473       case PathDiagnosticPiece::PopUp:
474         break;
475     }
476 
477     pieces.push_back(std::move(piece));
478   }
479 
480   return containsSomethingInteresting;
481 }
482 
483 /// Same logic as above to remove extra pieces.
484 static void removePopUpNotes(PathPieces &Path) {
485   for (unsigned int i = 0; i < Path.size(); ++i) {
486     auto Piece = std::move(Path.front());
487     Path.pop_front();
488     if (!isa<PathDiagnosticPopUpPiece>(*Piece))
489       Path.push_back(std::move(Piece));
490   }
491 }
492 
493 /// Returns true if the given decl has been implicitly given a body, either by
494 /// the analyzer or by the compiler proper.
495 static bool hasImplicitBody(const Decl *D) {
496   assert(D);
497   return D->isImplicit() || !D->hasBody();
498 }
499 
500 /// Recursively scan through a path and make sure that all call pieces have
501 /// valid locations.
502 static void
503 adjustCallLocations(PathPieces &Pieces,
504                     PathDiagnosticLocation *LastCallLocation = nullptr) {
505   for (const auto &I : Pieces) {
506     auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get());
507 
508     if (!Call)
509       continue;
510 
511     if (LastCallLocation) {
512       bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
513       if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
514         Call->callEnter = *LastCallLocation;
515       if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
516         Call->callReturn = *LastCallLocation;
517     }
518 
519     // Recursively clean out the subclass.  Keep this call around if
520     // it contains any informative diagnostics.
521     PathDiagnosticLocation *ThisCallLocation;
522     if (Call->callEnterWithin.asLocation().isValid() &&
523         !hasImplicitBody(Call->getCallee()))
524       ThisCallLocation = &Call->callEnterWithin;
525     else
526       ThisCallLocation = &Call->callEnter;
527 
528     assert(ThisCallLocation && "Outermost call has an invalid location");
529     adjustCallLocations(Call->path, ThisCallLocation);
530   }
531 }
532 
533 /// Remove edges in and out of C++ default initializer expressions. These are
534 /// for fields that have in-class initializers, as opposed to being initialized
535 /// explicitly in a constructor or braced list.
536 static void removeEdgesToDefaultInitializers(PathPieces &Pieces) {
537   for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
538     if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
539       removeEdgesToDefaultInitializers(C->path);
540 
541     if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
542       removeEdgesToDefaultInitializers(M->subPieces);
543 
544     if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) {
545       const Stmt *Start = CF->getStartLocation().asStmt();
546       const Stmt *End = CF->getEndLocation().asStmt();
547       if (isa_and_nonnull<CXXDefaultInitExpr>(Start)) {
548         I = Pieces.erase(I);
549         continue;
550       } else if (isa_and_nonnull<CXXDefaultInitExpr>(End)) {
551         PathPieces::iterator Next = std::next(I);
552         if (Next != E) {
553           if (auto *NextCF =
554                   dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) {
555             NextCF->setStartLocation(CF->getStartLocation());
556           }
557         }
558         I = Pieces.erase(I);
559         continue;
560       }
561     }
562 
563     I++;
564   }
565 }
566 
567 /// Remove all pieces with invalid locations as these cannot be serialized.
568 /// We might have pieces with invalid locations as a result of inlining Body
569 /// Farm generated functions.
570 static void removePiecesWithInvalidLocations(PathPieces &Pieces) {
571   for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
572     if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
573       removePiecesWithInvalidLocations(C->path);
574 
575     if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
576       removePiecesWithInvalidLocations(M->subPieces);
577 
578     if (!(*I)->getLocation().isValid() ||
579         !(*I)->getLocation().asLocation().isValid()) {
580       I = Pieces.erase(I);
581       continue;
582     }
583     I++;
584   }
585 }
586 
587 PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
588     const PathDiagnosticConstruct &C) const {
589   if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
590     return PathDiagnosticLocation(S, getSourceManager(),
591                                   C.getCurrLocationContext());
592 
593   return PathDiagnosticLocation::createDeclEnd(C.getCurrLocationContext(),
594                                                getSourceManager());
595 }
596 
597 PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
598     llvm::raw_string_ostream &os, const PathDiagnosticConstruct &C) const {
599   // Slow, but probably doesn't matter.
600   if (os.str().empty())
601     os << ' ';
602 
603   const PathDiagnosticLocation &Loc = ExecutionContinues(C);
604 
605   if (Loc.asStmt())
606     os << "Execution continues on line "
607        << getSourceManager().getExpansionLineNumber(Loc.asLocation())
608        << '.';
609   else {
610     os << "Execution jumps to the end of the ";
611     const Decl *D = C.getCurrLocationContext()->getDecl();
612     if (isa<ObjCMethodDecl>(D))
613       os << "method";
614     else if (isa<FunctionDecl>(D))
615       os << "function";
616     else {
617       assert(isa<BlockDecl>(D));
618       os << "anonymous block";
619     }
620     os << '.';
621   }
622 
623   return Loc;
624 }
625 
626 static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
627   if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
628     return PM.getParentIgnoreParens(S);
629 
630   const Stmt *Parent = PM.getParentIgnoreParens(S);
631   if (!Parent)
632     return nullptr;
633 
634   switch (Parent->getStmtClass()) {
635   case Stmt::ForStmtClass:
636   case Stmt::DoStmtClass:
637   case Stmt::WhileStmtClass:
638   case Stmt::ObjCForCollectionStmtClass:
639   case Stmt::CXXForRangeStmtClass:
640     return Parent;
641   default:
642     break;
643   }
644 
645   return nullptr;
646 }
647 
648 static PathDiagnosticLocation
649 getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC,
650                          bool allowNestedContexts = false) {
651   if (!S)
652     return {};
653 
654   const SourceManager &SMgr = LC->getDecl()->getASTContext().getSourceManager();
655 
656   while (const Stmt *Parent = getEnclosingParent(S, LC->getParentMap())) {
657     switch (Parent->getStmtClass()) {
658       case Stmt::BinaryOperatorClass: {
659         const auto *B = cast<BinaryOperator>(Parent);
660         if (B->isLogicalOp())
661           return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
662         break;
663       }
664       case Stmt::CompoundStmtClass:
665       case Stmt::StmtExprClass:
666         return PathDiagnosticLocation(S, SMgr, LC);
667       case Stmt::ChooseExprClass:
668         // Similar to '?' if we are referring to condition, just have the edge
669         // point to the entire choose expression.
670         if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
671           return PathDiagnosticLocation(Parent, SMgr, LC);
672         else
673           return PathDiagnosticLocation(S, SMgr, LC);
674       case Stmt::BinaryConditionalOperatorClass:
675       case Stmt::ConditionalOperatorClass:
676         // For '?', if we are referring to condition, just have the edge point
677         // to the entire '?' expression.
678         if (allowNestedContexts ||
679             cast<AbstractConditionalOperator>(Parent)->getCond() == S)
680           return PathDiagnosticLocation(Parent, SMgr, LC);
681         else
682           return PathDiagnosticLocation(S, SMgr, LC);
683       case Stmt::CXXForRangeStmtClass:
684         if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
685           return PathDiagnosticLocation(S, SMgr, LC);
686         break;
687       case Stmt::DoStmtClass:
688           return PathDiagnosticLocation(S, SMgr, LC);
689       case Stmt::ForStmtClass:
690         if (cast<ForStmt>(Parent)->getBody() == S)
691           return PathDiagnosticLocation(S, SMgr, LC);
692         break;
693       case Stmt::IfStmtClass:
694         if (cast<IfStmt>(Parent)->getCond() != S)
695           return PathDiagnosticLocation(S, SMgr, LC);
696         break;
697       case Stmt::ObjCForCollectionStmtClass:
698         if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
699           return PathDiagnosticLocation(S, SMgr, LC);
700         break;
701       case Stmt::WhileStmtClass:
702         if (cast<WhileStmt>(Parent)->getCond() != S)
703           return PathDiagnosticLocation(S, SMgr, LC);
704         break;
705       default:
706         break;
707     }
708 
709     S = Parent;
710   }
711 
712   assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
713 
714   return PathDiagnosticLocation(S, SMgr, LC);
715 }
716 
717 //===----------------------------------------------------------------------===//
718 // "Minimal" path diagnostic generation algorithm.
719 //===----------------------------------------------------------------------===//
720 
721 /// If the piece contains a special message, add it to all the call pieces on
722 /// the active stack. For example, my_malloc allocated memory, so MallocChecker
723 /// will construct an event at the call to malloc(), and add a stack hint that
724 /// an allocated memory was returned. We'll use this hint to construct a message
725 /// when returning from the call to my_malloc
726 ///
727 ///   void *my_malloc() { return malloc(sizeof(int)); }
728 ///   void fishy() {
729 ///     void *ptr = my_malloc(); // returned allocated memory
730 ///   } // leak
731 void PathDiagnosticBuilder::updateStackPiecesWithMessage(
732     PathDiagnosticPieceRef P, const CallWithEntryStack &CallStack) const {
733   if (R->hasCallStackHint(P))
734     for (const auto &I : CallStack) {
735       PathDiagnosticCallPiece *CP = I.first;
736       const ExplodedNode *N = I.second;
737       std::string stackMsg = R->getCallStackMessage(P, N);
738 
739       // The last message on the path to final bug is the most important
740       // one. Since we traverse the path backwards, do not add the message
741       // if one has been previously added.
742       if (!CP->hasCallStackMessage())
743         CP->setCallStackMessage(stackMsg);
744     }
745 }
746 
747 static void CompactMacroExpandedPieces(PathPieces &path,
748                                        const SourceManager& SM);
749 
750 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP(
751     const PathDiagnosticConstruct &C, const CFGBlock *Dst,
752     PathDiagnosticLocation &Start) const {
753 
754   const SourceManager &SM = getSourceManager();
755   // Figure out what case arm we took.
756   std::string sbuf;
757   llvm::raw_string_ostream os(sbuf);
758   PathDiagnosticLocation End;
759 
760   if (const Stmt *S = Dst->getLabel()) {
761     End = PathDiagnosticLocation(S, SM, C.getCurrLocationContext());
762 
763     switch (S->getStmtClass()) {
764     default:
765       os << "No cases match in the switch statement. "
766         "Control jumps to line "
767         << End.asLocation().getExpansionLineNumber();
768       break;
769     case Stmt::DefaultStmtClass:
770       os << "Control jumps to the 'default' case at line "
771         << End.asLocation().getExpansionLineNumber();
772       break;
773 
774     case Stmt::CaseStmtClass: {
775       os << "Control jumps to 'case ";
776       const auto *Case = cast<CaseStmt>(S);
777       const Expr *LHS = Case->getLHS()->IgnoreParenImpCasts();
778 
779       // Determine if it is an enum.
780       bool GetRawInt = true;
781 
782       if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) {
783         // FIXME: Maybe this should be an assertion.  Are there cases
784         // were it is not an EnumConstantDecl?
785         const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl());
786 
787         if (D) {
788           GetRawInt = false;
789           os << *D;
790         }
791       }
792 
793       if (GetRawInt)
794         os << LHS->EvaluateKnownConstInt(getASTContext());
795 
796       os << ":'  at line " << End.asLocation().getExpansionLineNumber();
797       break;
798     }
799     }
800   } else {
801     os << "'Default' branch taken. ";
802     End = ExecutionContinues(os, C);
803   }
804   return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
805                                                        os.str());
806 }
807 
808 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
809     const PathDiagnosticConstruct &C, const Stmt *S,
810     PathDiagnosticLocation &Start) const {
811   std::string sbuf;
812   llvm::raw_string_ostream os(sbuf);
813   const PathDiagnosticLocation &End =
814       getEnclosingStmtLocation(S, C.getCurrLocationContext());
815   os << "Control jumps to line " << End.asLocation().getExpansionLineNumber();
816   return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str());
817 }
818 
819 PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
820     const PathDiagnosticConstruct &C, const Stmt *T, const CFGBlock *Src,
821     const CFGBlock *Dst) const {
822 
823   const SourceManager &SM = getSourceManager();
824 
825   const auto *B = cast<BinaryOperator>(T);
826   std::string sbuf;
827   llvm::raw_string_ostream os(sbuf);
828   os << "Left side of '";
829   PathDiagnosticLocation Start, End;
830 
831   if (B->getOpcode() == BO_LAnd) {
832     os << "&&"
833       << "' is ";
834 
835     if (*(Src->succ_begin() + 1) == Dst) {
836       os << "false";
837       End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
838       Start =
839         PathDiagnosticLocation::createOperatorLoc(B, SM);
840     } else {
841       os << "true";
842       Start =
843           PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
844       End = ExecutionContinues(C);
845     }
846   } else {
847     assert(B->getOpcode() == BO_LOr);
848     os << "||"
849       << "' is ";
850 
851     if (*(Src->succ_begin() + 1) == Dst) {
852       os << "false";
853       Start =
854           PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
855       End = ExecutionContinues(C);
856     } else {
857       os << "true";
858       End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
859       Start =
860         PathDiagnosticLocation::createOperatorLoc(B, SM);
861     }
862   }
863   return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
864                                                          os.str());
865 }
866 
867 void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
868     PathDiagnosticConstruct &C, BlockEdge BE) const {
869   const SourceManager &SM = getSourceManager();
870   const LocationContext *LC = C.getCurrLocationContext();
871   const CFGBlock *Src = BE.getSrc();
872   const CFGBlock *Dst = BE.getDst();
873   const Stmt *T = Src->getTerminatorStmt();
874   if (!T)
875     return;
876 
877   auto Start = PathDiagnosticLocation::createBegin(T, SM, LC);
878   switch (T->getStmtClass()) {
879   default:
880     break;
881 
882   case Stmt::GotoStmtClass:
883   case Stmt::IndirectGotoStmtClass: {
884     if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
885       C.getActivePath().push_front(generateDiagForGotoOP(C, S, Start));
886     break;
887   }
888 
889   case Stmt::SwitchStmtClass: {
890     C.getActivePath().push_front(generateDiagForSwitchOP(C, Dst, Start));
891     break;
892   }
893 
894   case Stmt::BreakStmtClass:
895   case Stmt::ContinueStmtClass: {
896     std::string sbuf;
897     llvm::raw_string_ostream os(sbuf);
898     PathDiagnosticLocation End = ExecutionContinues(os, C);
899     C.getActivePath().push_front(
900         std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
901     break;
902   }
903 
904   // Determine control-flow for ternary '?'.
905   case Stmt::BinaryConditionalOperatorClass:
906   case Stmt::ConditionalOperatorClass: {
907     std::string sbuf;
908     llvm::raw_string_ostream os(sbuf);
909     os << "'?' condition is ";
910 
911     if (*(Src->succ_begin() + 1) == Dst)
912       os << "false";
913     else
914       os << "true";
915 
916     PathDiagnosticLocation End = ExecutionContinues(C);
917 
918     if (const Stmt *S = End.asStmt())
919       End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
920 
921     C.getActivePath().push_front(
922         std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
923     break;
924   }
925 
926   // Determine control-flow for short-circuited '&&' and '||'.
927   case Stmt::BinaryOperatorClass: {
928     if (!C.supportsLogicalOpControlFlow())
929       break;
930 
931     C.getActivePath().push_front(generateDiagForBinaryOP(C, T, Src, Dst));
932     break;
933   }
934 
935   case Stmt::DoStmtClass:
936     if (*(Src->succ_begin()) == Dst) {
937       std::string sbuf;
938       llvm::raw_string_ostream os(sbuf);
939 
940       os << "Loop condition is true. ";
941       PathDiagnosticLocation End = ExecutionContinues(os, C);
942 
943       if (const Stmt *S = End.asStmt())
944         End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
945 
946       C.getActivePath().push_front(
947           std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
948                                                            os.str()));
949     } else {
950       PathDiagnosticLocation End = ExecutionContinues(C);
951 
952       if (const Stmt *S = End.asStmt())
953         End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
954 
955       C.getActivePath().push_front(
956           std::make_shared<PathDiagnosticControlFlowPiece>(
957               Start, End, "Loop condition is false.  Exiting loop"));
958     }
959     break;
960 
961   case Stmt::WhileStmtClass:
962   case Stmt::ForStmtClass:
963     if (*(Src->succ_begin() + 1) == Dst) {
964       std::string sbuf;
965       llvm::raw_string_ostream os(sbuf);
966 
967       os << "Loop condition is false. ";
968       PathDiagnosticLocation End = ExecutionContinues(os, C);
969       if (const Stmt *S = End.asStmt())
970         End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
971 
972       C.getActivePath().push_front(
973           std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
974                                                            os.str()));
975     } else {
976       PathDiagnosticLocation End = ExecutionContinues(C);
977       if (const Stmt *S = End.asStmt())
978         End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
979 
980       C.getActivePath().push_front(
981           std::make_shared<PathDiagnosticControlFlowPiece>(
982               Start, End, "Loop condition is true.  Entering loop body"));
983     }
984 
985     break;
986 
987   case Stmt::IfStmtClass: {
988     PathDiagnosticLocation End = ExecutionContinues(C);
989 
990     if (const Stmt *S = End.asStmt())
991       End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
992 
993     if (*(Src->succ_begin() + 1) == Dst)
994       C.getActivePath().push_front(
995           std::make_shared<PathDiagnosticControlFlowPiece>(
996               Start, End, "Taking false branch"));
997     else
998       C.getActivePath().push_front(
999           std::make_shared<PathDiagnosticControlFlowPiece>(
1000               Start, End, "Taking true branch"));
1001 
1002     break;
1003   }
1004   }
1005 }
1006 
1007 //===----------------------------------------------------------------------===//
1008 // Functions for determining if a loop was executed 0 times.
1009 //===----------------------------------------------------------------------===//
1010 
1011 static bool isLoop(const Stmt *Term) {
1012   switch (Term->getStmtClass()) {
1013     case Stmt::ForStmtClass:
1014     case Stmt::WhileStmtClass:
1015     case Stmt::ObjCForCollectionStmtClass:
1016     case Stmt::CXXForRangeStmtClass:
1017       return true;
1018     default:
1019       // Note that we intentionally do not include do..while here.
1020       return false;
1021   }
1022 }
1023 
1024 static bool isJumpToFalseBranch(const BlockEdge *BE) {
1025   const CFGBlock *Src = BE->getSrc();
1026   assert(Src->succ_size() == 2);
1027   return (*(Src->succ_begin()+1) == BE->getDst());
1028 }
1029 
1030 static bool isContainedByStmt(const ParentMap &PM, const Stmt *S,
1031                               const Stmt *SubS) {
1032   while (SubS) {
1033     if (SubS == S)
1034       return true;
1035     SubS = PM.getParent(SubS);
1036   }
1037   return false;
1038 }
1039 
1040 static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term,
1041                                      const ExplodedNode *N) {
1042   while (N) {
1043     std::optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1044     if (SP) {
1045       const Stmt *S = SP->getStmt();
1046       if (!isContainedByStmt(PM, Term, S))
1047         return S;
1048     }
1049     N = N->getFirstPred();
1050   }
1051   return nullptr;
1052 }
1053 
1054 static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) {
1055   const Stmt *LoopBody = nullptr;
1056   switch (Term->getStmtClass()) {
1057     case Stmt::CXXForRangeStmtClass: {
1058       const auto *FR = cast<CXXForRangeStmt>(Term);
1059       if (isContainedByStmt(PM, FR->getInc(), S))
1060         return true;
1061       if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
1062         return true;
1063       LoopBody = FR->getBody();
1064       break;
1065     }
1066     case Stmt::ForStmtClass: {
1067       const auto *FS = cast<ForStmt>(Term);
1068       if (isContainedByStmt(PM, FS->getInc(), S))
1069         return true;
1070       LoopBody = FS->getBody();
1071       break;
1072     }
1073     case Stmt::ObjCForCollectionStmtClass: {
1074       const auto *FC = cast<ObjCForCollectionStmt>(Term);
1075       LoopBody = FC->getBody();
1076       break;
1077     }
1078     case Stmt::WhileStmtClass:
1079       LoopBody = cast<WhileStmt>(Term)->getBody();
1080       break;
1081     default:
1082       return false;
1083   }
1084   return isContainedByStmt(PM, LoopBody, S);
1085 }
1086 
1087 /// Adds a sanitized control-flow diagnostic edge to a path.
1088 static void addEdgeToPath(PathPieces &path,
1089                           PathDiagnosticLocation &PrevLoc,
1090                           PathDiagnosticLocation NewLoc) {
1091   if (!NewLoc.isValid())
1092     return;
1093 
1094   SourceLocation NewLocL = NewLoc.asLocation();
1095   if (NewLocL.isInvalid())
1096     return;
1097 
1098   if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
1099     PrevLoc = NewLoc;
1100     return;
1101   }
1102 
1103   // Ignore self-edges, which occur when there are multiple nodes at the same
1104   // statement.
1105   if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
1106     return;
1107 
1108   path.push_front(
1109       std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc));
1110   PrevLoc = NewLoc;
1111 }
1112 
1113 /// A customized wrapper for CFGBlock::getTerminatorCondition()
1114 /// which returns the element for ObjCForCollectionStmts.
1115 static const Stmt *getTerminatorCondition(const CFGBlock *B) {
1116   const Stmt *S = B->getTerminatorCondition();
1117   if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S))
1118     return FS->getElement();
1119   return S;
1120 }
1121 
1122 constexpr llvm::StringLiteral StrEnteringLoop = "Entering loop body";
1123 constexpr llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
1124 constexpr llvm::StringLiteral StrLoopRangeEmpty =
1125     "Loop body skipped when range is empty";
1126 constexpr llvm::StringLiteral StrLoopCollectionEmpty =
1127     "Loop body skipped when collection is empty";
1128 
1129 static std::unique_ptr<FilesToLineNumsMap>
1130 findExecutedLines(const SourceManager &SM, const ExplodedNode *N);
1131 
1132 void PathDiagnosticBuilder::generatePathDiagnosticsForNode(
1133     PathDiagnosticConstruct &C, PathDiagnosticLocation &PrevLoc) const {
1134   ProgramPoint P = C.getCurrentNode()->getLocation();
1135   const SourceManager &SM = getSourceManager();
1136 
1137   // Have we encountered an entrance to a call?  It may be
1138   // the case that we have not encountered a matching
1139   // call exit before this point.  This means that the path
1140   // terminated within the call itself.
1141   if (auto CE = P.getAs<CallEnter>()) {
1142 
1143     if (C.shouldAddPathEdges()) {
1144       // Add an edge to the start of the function.
1145       const StackFrameContext *CalleeLC = CE->getCalleeContext();
1146       const Decl *D = CalleeLC->getDecl();
1147       // Add the edge only when the callee has body. We jump to the beginning
1148       // of the *declaration*, however we expect it to be followed by the
1149       // body. This isn't the case for autosynthesized property accessors in
1150       // Objective-C. No need for a similar extra check for CallExit points
1151       // because the exit edge comes from a statement (i.e. return),
1152       // not from declaration.
1153       if (D->hasBody())
1154         addEdgeToPath(C.getActivePath(), PrevLoc,
1155                       PathDiagnosticLocation::createBegin(D, SM));
1156     }
1157 
1158     // Did we visit an entire call?
1159     bool VisitedEntireCall = C.PD->isWithinCall();
1160     C.PD->popActivePath();
1161 
1162     PathDiagnosticCallPiece *Call;
1163     if (VisitedEntireCall) {
1164       Call = cast<PathDiagnosticCallPiece>(C.getActivePath().front().get());
1165     } else {
1166       // The path terminated within a nested location context, create a new
1167       // call piece to encapsulate the rest of the path pieces.
1168       const Decl *Caller = CE->getLocationContext()->getDecl();
1169       Call = PathDiagnosticCallPiece::construct(C.getActivePath(), Caller);
1170       assert(C.getActivePath().size() == 1 &&
1171              C.getActivePath().front().get() == Call);
1172 
1173       // Since we just transferred the path over to the call piece, reset the
1174       // mapping of the active path to the current location context.
1175       assert(C.isInLocCtxMap(&C.getActivePath()) &&
1176              "When we ascend to a previously unvisited call, the active path's "
1177              "address shouldn't change, but rather should be compacted into "
1178              "a single CallEvent!");
1179       C.updateLocCtxMap(&C.getActivePath(), C.getCurrLocationContext());
1180 
1181       // Record the location context mapping for the path within the call.
1182       assert(!C.isInLocCtxMap(&Call->path) &&
1183              "When we ascend to a previously unvisited call, this must be the "
1184              "first time we encounter the caller context!");
1185       C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1186     }
1187     Call->setCallee(*CE, SM);
1188 
1189     // Update the previous location in the active path.
1190     PrevLoc = Call->getLocation();
1191 
1192     if (!C.CallStack.empty()) {
1193       assert(C.CallStack.back().first == Call);
1194       C.CallStack.pop_back();
1195     }
1196     return;
1197   }
1198 
1199   assert(C.getCurrLocationContext() == C.getLocationContextForActivePath() &&
1200          "The current position in the bug path is out of sync with the "
1201          "location context associated with the active path!");
1202 
1203   // Have we encountered an exit from a function call?
1204   if (std::optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1205 
1206     // We are descending into a call (backwards).  Construct
1207     // a new call piece to contain the path pieces for that call.
1208     auto Call = PathDiagnosticCallPiece::construct(*CE, SM);
1209     // Record the mapping from call piece to LocationContext.
1210     assert(!C.isInLocCtxMap(&Call->path) &&
1211            "We just entered a call, this must've been the first time we "
1212            "encounter its context!");
1213     C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1214 
1215     if (C.shouldAddPathEdges()) {
1216       // Add the edge to the return site.
1217       addEdgeToPath(C.getActivePath(), PrevLoc, Call->callReturn);
1218       PrevLoc.invalidate();
1219     }
1220 
1221     auto *P = Call.get();
1222     C.getActivePath().push_front(std::move(Call));
1223 
1224     // Make the contents of the call the active path for now.
1225     C.PD->pushActivePath(&P->path);
1226     C.CallStack.push_back(CallWithEntry(P, C.getCurrentNode()));
1227     return;
1228   }
1229 
1230   if (auto PS = P.getAs<PostStmt>()) {
1231     if (!C.shouldAddPathEdges())
1232       return;
1233 
1234     // Add an edge.  If this is an ObjCForCollectionStmt do
1235     // not add an edge here as it appears in the CFG both
1236     // as a terminator and as a terminator condition.
1237     if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
1238       PathDiagnosticLocation L =
1239           PathDiagnosticLocation(PS->getStmt(), SM, C.getCurrLocationContext());
1240       addEdgeToPath(C.getActivePath(), PrevLoc, L);
1241     }
1242 
1243   } else if (auto BE = P.getAs<BlockEdge>()) {
1244 
1245     if (C.shouldAddControlNotes()) {
1246       generateMinimalDiagForBlockEdge(C, *BE);
1247     }
1248 
1249     if (!C.shouldAddPathEdges()) {
1250       return;
1251     }
1252 
1253     // Are we jumping to the head of a loop?  Add a special diagnostic.
1254     if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1255       PathDiagnosticLocation L(Loop, SM, C.getCurrLocationContext());
1256       const Stmt *Body = nullptr;
1257 
1258       if (const auto *FS = dyn_cast<ForStmt>(Loop))
1259         Body = FS->getBody();
1260       else if (const auto *WS = dyn_cast<WhileStmt>(Loop))
1261         Body = WS->getBody();
1262       else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) {
1263         Body = OFS->getBody();
1264       } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) {
1265         Body = FRS->getBody();
1266       }
1267       // do-while statements are explicitly excluded here
1268 
1269       auto p = std::make_shared<PathDiagnosticEventPiece>(
1270           L, "Looping back to the head of the loop");
1271       p->setPrunable(true);
1272 
1273       addEdgeToPath(C.getActivePath(), PrevLoc, p->getLocation());
1274       // We might've added a very similar control node already
1275       if (!C.shouldAddControlNotes()) {
1276         C.getActivePath().push_front(std::move(p));
1277       }
1278 
1279       if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
1280         addEdgeToPath(C.getActivePath(), PrevLoc,
1281                       PathDiagnosticLocation::createEndBrace(CS, SM));
1282       }
1283     }
1284 
1285     const CFGBlock *BSrc = BE->getSrc();
1286     const ParentMap &PM = C.getParentMap();
1287 
1288     if (const Stmt *Term = BSrc->getTerminatorStmt()) {
1289       // Are we jumping past the loop body without ever executing the
1290       // loop (because the condition was false)?
1291       if (isLoop(Term)) {
1292         const Stmt *TermCond = getTerminatorCondition(BSrc);
1293         bool IsInLoopBody = isInLoopBody(
1294             PM, getStmtBeforeCond(PM, TermCond, C.getCurrentNode()), Term);
1295 
1296         StringRef str;
1297 
1298         if (isJumpToFalseBranch(&*BE)) {
1299           if (!IsInLoopBody) {
1300             if (isa<ObjCForCollectionStmt>(Term)) {
1301               str = StrLoopCollectionEmpty;
1302             } else if (isa<CXXForRangeStmt>(Term)) {
1303               str = StrLoopRangeEmpty;
1304             } else {
1305               str = StrLoopBodyZero;
1306             }
1307           }
1308         } else {
1309           str = StrEnteringLoop;
1310         }
1311 
1312         if (!str.empty()) {
1313           PathDiagnosticLocation L(TermCond ? TermCond : Term, SM,
1314                                    C.getCurrLocationContext());
1315           auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
1316           PE->setPrunable(true);
1317           addEdgeToPath(C.getActivePath(), PrevLoc, PE->getLocation());
1318 
1319           // We might've added a very similar control node already
1320           if (!C.shouldAddControlNotes()) {
1321             C.getActivePath().push_front(std::move(PE));
1322           }
1323         }
1324       } else if (isa<BreakStmt, ContinueStmt, GotoStmt>(Term)) {
1325         PathDiagnosticLocation L(Term, SM, C.getCurrLocationContext());
1326         addEdgeToPath(C.getActivePath(), PrevLoc, L);
1327       }
1328     }
1329   }
1330 }
1331 
1332 static std::unique_ptr<PathDiagnostic>
1333 generateDiagnosticForBasicReport(const BasicBugReport *R,
1334                                  const Decl *AnalysisEntryPoint) {
1335   const BugType &BT = R->getBugType();
1336   return std::make_unique<PathDiagnostic>(
1337       BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(),
1338       R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1339       BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(),
1340       AnalysisEntryPoint, std::make_unique<FilesToLineNumsMap>());
1341 }
1342 
1343 static std::unique_ptr<PathDiagnostic>
1344 generateEmptyDiagnosticForReport(const PathSensitiveBugReport *R,
1345                                  const SourceManager &SM,
1346                                  const Decl *AnalysisEntryPoint) {
1347   const BugType &BT = R->getBugType();
1348   return std::make_unique<PathDiagnostic>(
1349       BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(),
1350       R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1351       BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(),
1352       AnalysisEntryPoint, findExecutedLines(SM, R->getErrorNode()));
1353 }
1354 
1355 static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
1356   if (!S)
1357     return nullptr;
1358 
1359   while (true) {
1360     S = PM.getParentIgnoreParens(S);
1361 
1362     if (!S)
1363       break;
1364 
1365     if (isa<FullExpr, CXXBindTemporaryExpr, SubstNonTypeTemplateParmExpr>(S))
1366       continue;
1367 
1368     break;
1369   }
1370 
1371   return S;
1372 }
1373 
1374 static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
1375   switch (S->getStmtClass()) {
1376     case Stmt::BinaryOperatorClass: {
1377       const auto *BO = cast<BinaryOperator>(S);
1378       if (!BO->isLogicalOp())
1379         return false;
1380       return BO->getLHS() == Cond || BO->getRHS() == Cond;
1381     }
1382     case Stmt::IfStmtClass:
1383       return cast<IfStmt>(S)->getCond() == Cond;
1384     case Stmt::ForStmtClass:
1385       return cast<ForStmt>(S)->getCond() == Cond;
1386     case Stmt::WhileStmtClass:
1387       return cast<WhileStmt>(S)->getCond() == Cond;
1388     case Stmt::DoStmtClass:
1389       return cast<DoStmt>(S)->getCond() == Cond;
1390     case Stmt::ChooseExprClass:
1391       return cast<ChooseExpr>(S)->getCond() == Cond;
1392     case Stmt::IndirectGotoStmtClass:
1393       return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
1394     case Stmt::SwitchStmtClass:
1395       return cast<SwitchStmt>(S)->getCond() == Cond;
1396     case Stmt::BinaryConditionalOperatorClass:
1397       return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
1398     case Stmt::ConditionalOperatorClass: {
1399       const auto *CO = cast<ConditionalOperator>(S);
1400       return CO->getCond() == Cond ||
1401              CO->getLHS() == Cond ||
1402              CO->getRHS() == Cond;
1403     }
1404     case Stmt::ObjCForCollectionStmtClass:
1405       return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
1406     case Stmt::CXXForRangeStmtClass: {
1407       const auto *FRS = cast<CXXForRangeStmt>(S);
1408       return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
1409     }
1410     default:
1411       return false;
1412   }
1413 }
1414 
1415 static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1416   if (const auto *FS = dyn_cast<ForStmt>(FL))
1417     return FS->getInc() == S || FS->getInit() == S;
1418   if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL))
1419     return FRS->getInc() == S || FRS->getRangeStmt() == S ||
1420            FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
1421   return false;
1422 }
1423 
1424 using OptimizedCallsSet = llvm::DenseSet<const PathDiagnosticCallPiece *>;
1425 
1426 /// Adds synthetic edges from top-level statements to their subexpressions.
1427 ///
1428 /// This avoids a "swoosh" effect, where an edge from a top-level statement A
1429 /// points to a sub-expression B.1 that's not at the start of B. In these cases,
1430 /// we'd like to see an edge from A to B, then another one from B to B.1.
1431 static void addContextEdges(PathPieces &pieces, const LocationContext *LC) {
1432   const ParentMap &PM = LC->getParentMap();
1433   PathPieces::iterator Prev = pieces.end();
1434   for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
1435        Prev = I, ++I) {
1436     auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1437 
1438     if (!Piece)
1439       continue;
1440 
1441     PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
1442     SmallVector<PathDiagnosticLocation, 4> SrcContexts;
1443 
1444     PathDiagnosticLocation NextSrcContext = SrcLoc;
1445     const Stmt *InnerStmt = nullptr;
1446     while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
1447       SrcContexts.push_back(NextSrcContext);
1448       InnerStmt = NextSrcContext.asStmt();
1449       NextSrcContext = getEnclosingStmtLocation(InnerStmt, LC,
1450                                                 /*allowNested=*/true);
1451     }
1452 
1453     // Repeatedly split the edge as necessary.
1454     // This is important for nested logical expressions (||, &&, ?:) where we
1455     // want to show all the levels of context.
1456     while (true) {
1457       const Stmt *Dst = Piece->getEndLocation().getStmtOrNull();
1458 
1459       // We are looking at an edge. Is the destination within a larger
1460       // expression?
1461       PathDiagnosticLocation DstContext =
1462           getEnclosingStmtLocation(Dst, LC, /*allowNested=*/true);
1463       if (!DstContext.isValid() || DstContext.asStmt() == Dst)
1464         break;
1465 
1466       // If the source is in the same context, we're already good.
1467       if (llvm::is_contained(SrcContexts, DstContext))
1468         break;
1469 
1470       // Update the subexpression node to point to the context edge.
1471       Piece->setStartLocation(DstContext);
1472 
1473       // Try to extend the previous edge if it's at the same level as the source
1474       // context.
1475       if (Prev != E) {
1476         auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get());
1477 
1478         if (PrevPiece) {
1479           if (const Stmt *PrevSrc =
1480                   PrevPiece->getStartLocation().getStmtOrNull()) {
1481             const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
1482             if (PrevSrcParent ==
1483                 getStmtParent(DstContext.getStmtOrNull(), PM)) {
1484               PrevPiece->setEndLocation(DstContext);
1485               break;
1486             }
1487           }
1488         }
1489       }
1490 
1491       // Otherwise, split the current edge into a context edge and a
1492       // subexpression edge. Note that the context statement may itself have
1493       // context.
1494       auto P =
1495           std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext);
1496       Piece = P.get();
1497       I = pieces.insert(I, std::move(P));
1498     }
1499   }
1500 }
1501 
1502 /// Move edges from a branch condition to a branch target
1503 ///        when the condition is simple.
1504 ///
1505 /// This restructures some of the work of addContextEdges.  That function
1506 /// creates edges this may destroy, but they work together to create a more
1507 /// aesthetically set of edges around branches.  After the call to
1508 /// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
1509 /// the branch to the branch condition, and (3) an edge from the branch
1510 /// condition to the branch target.  We keep (1), but may wish to remove (2)
1511 /// and move the source of (3) to the branch if the branch condition is simple.
1512 static void simplifySimpleBranches(PathPieces &pieces) {
1513   for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
1514     const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1515 
1516     if (!PieceI)
1517       continue;
1518 
1519     const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1520     const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1521 
1522     if (!s1Start || !s1End)
1523       continue;
1524 
1525     PathPieces::iterator NextI = I; ++NextI;
1526     if (NextI == E)
1527       break;
1528 
1529     PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
1530 
1531     while (true) {
1532       if (NextI == E)
1533         break;
1534 
1535       const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1536       if (EV) {
1537         StringRef S = EV->getString();
1538         if (S == StrEnteringLoop || S == StrLoopBodyZero ||
1539             S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) {
1540           ++NextI;
1541           continue;
1542         }
1543         break;
1544       }
1545 
1546       PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1547       break;
1548     }
1549 
1550     if (!PieceNextI)
1551       continue;
1552 
1553     const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1554     const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1555 
1556     if (!s2Start || !s2End || s1End != s2Start)
1557       continue;
1558 
1559     // We only perform this transformation for specific branch kinds.
1560     // We don't want to do this for do..while, for example.
1561     if (!isa<ForStmt, WhileStmt, IfStmt, ObjCForCollectionStmt,
1562              CXXForRangeStmt>(s1Start))
1563       continue;
1564 
1565     // Is s1End the branch condition?
1566     if (!isConditionForTerminator(s1Start, s1End))
1567       continue;
1568 
1569     // Perform the hoisting by eliminating (2) and changing the start
1570     // location of (3).
1571     PieceNextI->setStartLocation(PieceI->getStartLocation());
1572     I = pieces.erase(I);
1573   }
1574 }
1575 
1576 /// Returns the number of bytes in the given (character-based) SourceRange.
1577 ///
1578 /// If the locations in the range are not on the same line, returns
1579 /// std::nullopt.
1580 ///
1581 /// Note that this does not do a precise user-visible character or column count.
1582 static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1583                                                    SourceRange Range) {
1584   SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
1585                              SM.getExpansionRange(Range.getEnd()).getEnd());
1586 
1587   FileID FID = SM.getFileID(ExpansionRange.getBegin());
1588   if (FID != SM.getFileID(ExpansionRange.getEnd()))
1589     return std::nullopt;
1590 
1591   std::optional<MemoryBufferRef> Buffer = SM.getBufferOrNone(FID);
1592   if (!Buffer)
1593     return std::nullopt;
1594 
1595   unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
1596   unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
1597   StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
1598 
1599   // We're searching the raw bytes of the buffer here, which might include
1600   // escaped newlines and such. That's okay; we're trying to decide whether the
1601   // SourceRange is covering a large or small amount of space in the user's
1602   // editor.
1603   if (Snippet.find_first_of("\r\n") != StringRef::npos)
1604     return std::nullopt;
1605 
1606   // This isn't Unicode-aware, but it doesn't need to be.
1607   return Snippet.size();
1608 }
1609 
1610 /// \sa getLengthOnSingleLine(SourceManager, SourceRange)
1611 static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1612                                                    const Stmt *S) {
1613   return getLengthOnSingleLine(SM, S->getSourceRange());
1614 }
1615 
1616 /// Eliminate two-edge cycles created by addContextEdges().
1617 ///
1618 /// Once all the context edges are in place, there are plenty of cases where
1619 /// there's a single edge from a top-level statement to a subexpression,
1620 /// followed by a single path note, and then a reverse edge to get back out to
1621 /// the top level. If the statement is simple enough, the subexpression edges
1622 /// just add noise and make it harder to understand what's going on.
1623 ///
1624 /// This function only removes edges in pairs, because removing only one edge
1625 /// might leave other edges dangling.
1626 ///
1627 /// This will not remove edges in more complicated situations:
1628 /// - if there is more than one "hop" leading to or from a subexpression.
1629 /// - if there is an inlined call between the edges instead of a single event.
1630 /// - if the whole statement is large enough that having subexpression arrows
1631 ///   might be helpful.
1632 static void removeContextCycles(PathPieces &Path, const SourceManager &SM) {
1633   for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
1634     // Pattern match the current piece and its successor.
1635     const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1636 
1637     if (!PieceI) {
1638       ++I;
1639       continue;
1640     }
1641 
1642     const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1643     const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1644 
1645     PathPieces::iterator NextI = I; ++NextI;
1646     if (NextI == E)
1647       break;
1648 
1649     const auto *PieceNextI =
1650         dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1651 
1652     if (!PieceNextI) {
1653       if (isa<PathDiagnosticEventPiece>(NextI->get())) {
1654         ++NextI;
1655         if (NextI == E)
1656           break;
1657         PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1658       }
1659 
1660       if (!PieceNextI) {
1661         ++I;
1662         continue;
1663       }
1664     }
1665 
1666     const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1667     const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1668 
1669     if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
1670       const size_t MAX_SHORT_LINE_LENGTH = 80;
1671       std::optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
1672       if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
1673         std::optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
1674         if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
1675           Path.erase(I);
1676           I = Path.erase(NextI);
1677           continue;
1678         }
1679       }
1680     }
1681 
1682     ++I;
1683   }
1684 }
1685 
1686 /// Return true if X is contained by Y.
1687 static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) {
1688   while (X) {
1689     if (X == Y)
1690       return true;
1691     X = PM.getParent(X);
1692   }
1693   return false;
1694 }
1695 
1696 // Remove short edges on the same line less than 3 columns in difference.
1697 static void removePunyEdges(PathPieces &path, const SourceManager &SM,
1698                             const ParentMap &PM) {
1699   bool erased = false;
1700 
1701   for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
1702        erased ? I : ++I) {
1703     erased = false;
1704 
1705     const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1706 
1707     if (!PieceI)
1708       continue;
1709 
1710     const Stmt *start = PieceI->getStartLocation().getStmtOrNull();
1711     const Stmt *end   = PieceI->getEndLocation().getStmtOrNull();
1712 
1713     if (!start || !end)
1714       continue;
1715 
1716     const Stmt *endParent = PM.getParent(end);
1717     if (!endParent)
1718       continue;
1719 
1720     if (isConditionForTerminator(end, endParent))
1721       continue;
1722 
1723     SourceLocation FirstLoc = start->getBeginLoc();
1724     SourceLocation SecondLoc = end->getBeginLoc();
1725 
1726     if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
1727       continue;
1728     if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
1729       std::swap(SecondLoc, FirstLoc);
1730 
1731     SourceRange EdgeRange(FirstLoc, SecondLoc);
1732     std::optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
1733 
1734     // If the statements are on different lines, continue.
1735     if (!ByteWidth)
1736       continue;
1737 
1738     const size_t MAX_PUNY_EDGE_LENGTH = 2;
1739     if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
1740       // FIXME: There are enough /bytes/ between the endpoints of the edge, but
1741       // there might not be enough /columns/. A proper user-visible column count
1742       // is probably too expensive, though.
1743       I = path.erase(I);
1744       erased = true;
1745       continue;
1746     }
1747   }
1748 }
1749 
1750 static void removeIdenticalEvents(PathPieces &path) {
1751   for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
1752     const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get());
1753 
1754     if (!PieceI)
1755       continue;
1756 
1757     PathPieces::iterator NextI = I; ++NextI;
1758     if (NextI == E)
1759       return;
1760 
1761     const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1762 
1763     if (!PieceNextI)
1764       continue;
1765 
1766     // Erase the second piece if it has the same exact message text.
1767     if (PieceI->getString() == PieceNextI->getString()) {
1768       path.erase(NextI);
1769     }
1770   }
1771 }
1772 
1773 static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path,
1774                           OptimizedCallsSet &OCS) {
1775   bool hasChanges = false;
1776   const LocationContext *LC = C.getLocationContextFor(&path);
1777   assert(LC);
1778   const ParentMap &PM = LC->getParentMap();
1779   const SourceManager &SM = C.getSourceManager();
1780 
1781   for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
1782     // Optimize subpaths.
1783     if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) {
1784       // Record the fact that a call has been optimized so we only do the
1785       // effort once.
1786       if (!OCS.count(CallI)) {
1787         while (optimizeEdges(C, CallI->path, OCS)) {
1788         }
1789         OCS.insert(CallI);
1790       }
1791       ++I;
1792       continue;
1793     }
1794 
1795     // Pattern match the current piece and its successor.
1796     auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1797 
1798     if (!PieceI) {
1799       ++I;
1800       continue;
1801     }
1802 
1803     const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1804     const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1805     const Stmt *level1 = getStmtParent(s1Start, PM);
1806     const Stmt *level2 = getStmtParent(s1End, PM);
1807 
1808     PathPieces::iterator NextI = I; ++NextI;
1809     if (NextI == E)
1810       break;
1811 
1812     const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1813 
1814     if (!PieceNextI) {
1815       ++I;
1816       continue;
1817     }
1818 
1819     const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1820     const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1821     const Stmt *level3 = getStmtParent(s2Start, PM);
1822     const Stmt *level4 = getStmtParent(s2End, PM);
1823 
1824     // Rule I.
1825     //
1826     // If we have two consecutive control edges whose end/begin locations
1827     // are at the same level (e.g. statements or top-level expressions within
1828     // a compound statement, or siblings share a single ancestor expression),
1829     // then merge them if they have no interesting intermediate event.
1830     //
1831     // For example:
1832     //
1833     // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
1834     // parent is '1'.  Here 'x.y.z' represents the hierarchy of statements.
1835     //
1836     // NOTE: this will be limited later in cases where we add barriers
1837     // to prevent this optimization.
1838     if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
1839       PieceI->setEndLocation(PieceNextI->getEndLocation());
1840       path.erase(NextI);
1841       hasChanges = true;
1842       continue;
1843     }
1844 
1845     // Rule II.
1846     //
1847     // Eliminate edges between subexpressions and parent expressions
1848     // when the subexpression is consumed.
1849     //
1850     // NOTE: this will be limited later in cases where we add barriers
1851     // to prevent this optimization.
1852     if (s1End && s1End == s2Start && level2) {
1853       bool removeEdge = false;
1854       // Remove edges into the increment or initialization of a
1855       // loop that have no interleaving event.  This means that
1856       // they aren't interesting.
1857       if (isIncrementOrInitInForLoop(s1End, level2))
1858         removeEdge = true;
1859       // Next only consider edges that are not anchored on
1860       // the condition of a terminator.  This are intermediate edges
1861       // that we might want to trim.
1862       else if (!isConditionForTerminator(level2, s1End)) {
1863         // Trim edges on expressions that are consumed by
1864         // the parent expression.
1865         if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
1866           removeEdge = true;
1867         }
1868         // Trim edges where a lexical containment doesn't exist.
1869         // For example:
1870         //
1871         //  X -> Y -> Z
1872         //
1873         // If 'Z' lexically contains Y (it is an ancestor) and
1874         // 'X' does not lexically contain Y (it is a descendant OR
1875         // it has no lexical relationship at all) then trim.
1876         //
1877         // This can eliminate edges where we dive into a subexpression
1878         // and then pop back out, etc.
1879         else if (s1Start && s2End &&
1880                  lexicalContains(PM, s2Start, s2End) &&
1881                  !lexicalContains(PM, s1End, s1Start)) {
1882           removeEdge = true;
1883         }
1884         // Trim edges from a subexpression back to the top level if the
1885         // subexpression is on a different line.
1886         //
1887         // A.1 -> A -> B
1888         // becomes
1889         // A.1 -> B
1890         //
1891         // These edges just look ugly and don't usually add anything.
1892         else if (s1Start && s2End &&
1893                  lexicalContains(PM, s1Start, s1End)) {
1894           SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
1895                                 PieceI->getStartLocation().asLocation());
1896           if (!getLengthOnSingleLine(SM, EdgeRange))
1897             removeEdge = true;
1898         }
1899       }
1900 
1901       if (removeEdge) {
1902         PieceI->setEndLocation(PieceNextI->getEndLocation());
1903         path.erase(NextI);
1904         hasChanges = true;
1905         continue;
1906       }
1907     }
1908 
1909     // Optimize edges for ObjC fast-enumeration loops.
1910     //
1911     // (X -> collection) -> (collection -> element)
1912     //
1913     // becomes:
1914     //
1915     // (X -> element)
1916     if (s1End == s2Start) {
1917       const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3);
1918       if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
1919           s2End == FS->getElement()) {
1920         PieceI->setEndLocation(PieceNextI->getEndLocation());
1921         path.erase(NextI);
1922         hasChanges = true;
1923         continue;
1924       }
1925     }
1926 
1927     // No changes at this index?  Move to the next one.
1928     ++I;
1929   }
1930 
1931   if (!hasChanges) {
1932     // Adjust edges into subexpressions to make them more uniform
1933     // and aesthetically pleasing.
1934     addContextEdges(path, LC);
1935     // Remove "cyclical" edges that include one or more context edges.
1936     removeContextCycles(path, SM);
1937     // Hoist edges originating from branch conditions to branches
1938     // for simple branches.
1939     simplifySimpleBranches(path);
1940     // Remove any puny edges left over after primary optimization pass.
1941     removePunyEdges(path, SM, PM);
1942     // Remove identical events.
1943     removeIdenticalEvents(path);
1944   }
1945 
1946   return hasChanges;
1947 }
1948 
1949 /// Drop the very first edge in a path, which should be a function entry edge.
1950 ///
1951 /// If the first edge is not a function entry edge (say, because the first
1952 /// statement had an invalid source location), this function does nothing.
1953 // FIXME: We should just generate invalid edges anyway and have the optimizer
1954 // deal with them.
1955 static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C,
1956                                   PathPieces &Path) {
1957   const auto *FirstEdge =
1958       dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
1959   if (!FirstEdge)
1960     return;
1961 
1962   const Decl *D = C.getLocationContextFor(&Path)->getDecl();
1963   PathDiagnosticLocation EntryLoc =
1964       PathDiagnosticLocation::createBegin(D, C.getSourceManager());
1965   if (FirstEdge->getStartLocation() != EntryLoc)
1966     return;
1967 
1968   Path.pop_front();
1969 }
1970 
1971 /// Populate executes lines with lines containing at least one diagnostics.
1972 static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD) {
1973 
1974   PathPieces path = PD.path.flatten(/*ShouldFlattenMacros=*/true);
1975   FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines();
1976 
1977   for (const auto &P : path) {
1978     FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc();
1979     FileID FID = Loc.getFileID();
1980     unsigned LineNo = Loc.getLineNumber();
1981     assert(FID.isValid());
1982     ExecutedLines[FID].insert(LineNo);
1983   }
1984 }
1985 
1986 PathDiagnosticConstruct::PathDiagnosticConstruct(
1987     const PathDiagnosticConsumer *PDC, const ExplodedNode *ErrorNode,
1988     const PathSensitiveBugReport *R, const Decl *AnalysisEntryPoint)
1989     : Consumer(PDC), CurrentNode(ErrorNode),
1990       SM(CurrentNode->getCodeDecl().getASTContext().getSourceManager()),
1991       PD(generateEmptyDiagnosticForReport(R, getSourceManager(),
1992                                           AnalysisEntryPoint)) {
1993   LCM[&PD->getActivePath()] = ErrorNode->getLocationContext();
1994 }
1995 
1996 PathDiagnosticBuilder::PathDiagnosticBuilder(
1997     BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
1998     PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
1999     std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics)
2000     : BugReporterContext(BRC), BugPath(std::move(BugPath)), R(r),
2001       ErrorNode(ErrorNode),
2002       VisitorsDiagnostics(std::move(VisitorsDiagnostics)) {}
2003 
2004 std::unique_ptr<PathDiagnostic>
2005 PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
2006   const Decl *EntryPoint = getBugReporter().getAnalysisEntryPoint();
2007   PathDiagnosticConstruct Construct(PDC, ErrorNode, R, EntryPoint);
2008 
2009   const SourceManager &SM = getSourceManager();
2010   const AnalyzerOptions &Opts = getAnalyzerOptions();
2011 
2012   if (!PDC->shouldGenerateDiagnostics())
2013     return generateEmptyDiagnosticForReport(R, getSourceManager(), EntryPoint);
2014 
2015   // Construct the final (warning) event for the bug report.
2016   auto EndNotes = VisitorsDiagnostics->find(ErrorNode);
2017   PathDiagnosticPieceRef LastPiece;
2018   if (EndNotes != VisitorsDiagnostics->end()) {
2019     assert(!EndNotes->second.empty());
2020     LastPiece = EndNotes->second[0];
2021   } else {
2022     LastPiece = BugReporterVisitor::getDefaultEndPath(*this, ErrorNode,
2023                                                       *getBugReport());
2024   }
2025   Construct.PD->setEndOfPath(LastPiece);
2026 
2027   PathDiagnosticLocation PrevLoc = Construct.PD->getLocation();
2028   // From the error node to the root, ascend the bug path and construct the bug
2029   // report.
2030   while (Construct.ascendToPrevNode()) {
2031     generatePathDiagnosticsForNode(Construct, PrevLoc);
2032 
2033     auto VisitorNotes = VisitorsDiagnostics->find(Construct.getCurrentNode());
2034     if (VisitorNotes == VisitorsDiagnostics->end())
2035       continue;
2036 
2037     // This is a workaround due to inability to put shared PathDiagnosticPiece
2038     // into a FoldingSet.
2039     std::set<llvm::FoldingSetNodeID> DeduplicationSet;
2040 
2041     // Add pieces from custom visitors.
2042     for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) {
2043       llvm::FoldingSetNodeID ID;
2044       Note->Profile(ID);
2045       if (!DeduplicationSet.insert(ID).second)
2046         continue;
2047 
2048       if (PDC->shouldAddPathEdges())
2049         addEdgeToPath(Construct.getActivePath(), PrevLoc, Note->getLocation());
2050       updateStackPiecesWithMessage(Note, Construct.CallStack);
2051       Construct.getActivePath().push_front(Note);
2052     }
2053   }
2054 
2055   if (PDC->shouldAddPathEdges()) {
2056     // Add an edge to the start of the function.
2057     // We'll prune it out later, but it helps make diagnostics more uniform.
2058     const StackFrameContext *CalleeLC =
2059         Construct.getLocationContextForActivePath()->getStackFrame();
2060     const Decl *D = CalleeLC->getDecl();
2061     addEdgeToPath(Construct.getActivePath(), PrevLoc,
2062                   PathDiagnosticLocation::createBegin(D, SM));
2063   }
2064 
2065 
2066   // Finally, prune the diagnostic path of uninteresting stuff.
2067   if (!Construct.PD->path.empty()) {
2068     if (R->shouldPrunePath() && Opts.ShouldPrunePaths) {
2069       bool stillHasNotes =
2070           removeUnneededCalls(Construct, Construct.getMutablePieces(), R);
2071       assert(stillHasNotes);
2072       (void)stillHasNotes;
2073     }
2074 
2075     // Remove pop-up notes if needed.
2076     if (!Opts.ShouldAddPopUpNotes)
2077       removePopUpNotes(Construct.getMutablePieces());
2078 
2079     // Redirect all call pieces to have valid locations.
2080     adjustCallLocations(Construct.getMutablePieces());
2081     removePiecesWithInvalidLocations(Construct.getMutablePieces());
2082 
2083     if (PDC->shouldAddPathEdges()) {
2084 
2085       // Reduce the number of edges from a very conservative set
2086       // to an aesthetically pleasing subset that conveys the
2087       // necessary information.
2088       OptimizedCallsSet OCS;
2089       while (optimizeEdges(Construct, Construct.getMutablePieces(), OCS)) {
2090       }
2091 
2092       // Drop the very first function-entry edge. It's not really necessary
2093       // for top-level functions.
2094       dropFunctionEntryEdge(Construct, Construct.getMutablePieces());
2095     }
2096 
2097     // Remove messages that are basically the same, and edges that may not
2098     // make sense.
2099     // We have to do this after edge optimization in the Extensive mode.
2100     removeRedundantMsgs(Construct.getMutablePieces());
2101     removeEdgesToDefaultInitializers(Construct.getMutablePieces());
2102   }
2103 
2104   if (Opts.ShouldDisplayMacroExpansions)
2105     CompactMacroExpandedPieces(Construct.getMutablePieces(), SM);
2106 
2107   return std::move(Construct.PD);
2108 }
2109 
2110 //===----------------------------------------------------------------------===//
2111 // Methods for BugType and subclasses.
2112 //===----------------------------------------------------------------------===//
2113 
2114 void BugType::anchor() {}
2115 
2116 //===----------------------------------------------------------------------===//
2117 // Methods for BugReport and subclasses.
2118 //===----------------------------------------------------------------------===//
2119 
2120 LLVM_ATTRIBUTE_USED static bool
2121 isDependency(const CheckerRegistryData &Registry, StringRef CheckerName) {
2122   for (const std::pair<StringRef, StringRef> &Pair : Registry.Dependencies) {
2123     if (Pair.second == CheckerName)
2124       return true;
2125   }
2126   return false;
2127 }
2128 
2129 LLVM_ATTRIBUTE_USED static bool isHidden(const CheckerRegistryData &Registry,
2130                                          StringRef CheckerName) {
2131   for (const CheckerInfo &Checker : Registry.Checkers) {
2132     if (Checker.FullName == CheckerName)
2133       return Checker.IsHidden;
2134   }
2135   llvm_unreachable(
2136       "Checker name not found in CheckerRegistry -- did you retrieve it "
2137       "correctly from CheckerManager::getCurrentCheckerName?");
2138 }
2139 
2140 PathSensitiveBugReport::PathSensitiveBugReport(
2141     const BugType &bt, StringRef shortDesc, StringRef desc,
2142     const ExplodedNode *errorNode, PathDiagnosticLocation LocationToUnique,
2143     const Decl *DeclToUnique)
2144     : BugReport(Kind::PathSensitive, bt, shortDesc, desc), ErrorNode(errorNode),
2145       ErrorNodeRange(getStmt() ? getStmt()->getSourceRange() : SourceRange()),
2146       UniqueingLocation(LocationToUnique), UniqueingDecl(DeclToUnique) {
2147   assert(!isDependency(ErrorNode->getState()
2148                            ->getAnalysisManager()
2149                            .getCheckerManager()
2150                            ->getCheckerRegistryData(),
2151                        bt.getCheckerName()) &&
2152          "Some checkers depend on this one! We don't allow dependency "
2153          "checkers to emit warnings, because checkers should depend on "
2154          "*modeling*, not *diagnostics*.");
2155 
2156   assert((bt.getCheckerName().starts_with("debug") ||
2157           !isHidden(ErrorNode->getState()
2158                         ->getAnalysisManager()
2159                         .getCheckerManager()
2160                         ->getCheckerRegistryData(),
2161                     bt.getCheckerName())) &&
2162          "Hidden checkers musn't emit diagnostics as they are by definition "
2163          "non-user facing!");
2164 }
2165 
2166 void PathSensitiveBugReport::addVisitor(
2167     std::unique_ptr<BugReporterVisitor> visitor) {
2168   if (!visitor)
2169     return;
2170 
2171   llvm::FoldingSetNodeID ID;
2172   visitor->Profile(ID);
2173 
2174   void *InsertPos = nullptr;
2175   if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
2176     return;
2177   }
2178 
2179   Callbacks.push_back(std::move(visitor));
2180 }
2181 
2182 void PathSensitiveBugReport::clearVisitors() {
2183   Callbacks.clear();
2184 }
2185 
2186 const Decl *PathSensitiveBugReport::getDeclWithIssue() const {
2187   const ExplodedNode *N = getErrorNode();
2188   if (!N)
2189     return nullptr;
2190 
2191   const LocationContext *LC = N->getLocationContext();
2192   return LC->getStackFrame()->getDecl();
2193 }
2194 
2195 void BasicBugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2196   hash.AddInteger(static_cast<int>(getKind()));
2197   hash.AddPointer(&BT);
2198   hash.AddString(Description);
2199   assert(Location.isValid());
2200   Location.Profile(hash);
2201 
2202   for (SourceRange range : Ranges) {
2203     if (!range.isValid())
2204       continue;
2205     hash.Add(range.getBegin());
2206     hash.Add(range.getEnd());
2207   }
2208 }
2209 
2210 void PathSensitiveBugReport::Profile(llvm::FoldingSetNodeID &hash) const {
2211   hash.AddInteger(static_cast<int>(getKind()));
2212   hash.AddPointer(&BT);
2213   hash.AddString(Description);
2214   PathDiagnosticLocation UL = getUniqueingLocation();
2215   if (UL.isValid()) {
2216     UL.Profile(hash);
2217   } else {
2218     // TODO: The statement may be null if the report was emitted before any
2219     // statements were executed. In particular, some checkers by design
2220     // occasionally emit their reports in empty functions (that have no
2221     // statements in their body). Do we profile correctly in this case?
2222     hash.AddPointer(ErrorNode->getCurrentOrPreviousStmtForDiagnostics());
2223   }
2224 
2225   for (SourceRange range : Ranges) {
2226     if (!range.isValid())
2227       continue;
2228     hash.Add(range.getBegin());
2229     hash.Add(range.getEnd());
2230   }
2231 }
2232 
2233 template <class T>
2234 static void insertToInterestingnessMap(
2235     llvm::DenseMap<T, bugreporter::TrackingKind> &InterestingnessMap, T Val,
2236     bugreporter::TrackingKind TKind) {
2237   auto Result = InterestingnessMap.insert({Val, TKind});
2238 
2239   if (Result.second)
2240     return;
2241 
2242   // Even if this symbol/region was already marked as interesting as a
2243   // condition, if we later mark it as interesting again but with
2244   // thorough tracking, overwrite it. Entities marked with thorough
2245   // interestiness are the most important (or most interesting, if you will),
2246   // and we wouldn't like to downplay their importance.
2247 
2248   switch (TKind) {
2249     case bugreporter::TrackingKind::Thorough:
2250       Result.first->getSecond() = bugreporter::TrackingKind::Thorough;
2251       return;
2252     case bugreporter::TrackingKind::Condition:
2253       return;
2254     }
2255 
2256     llvm_unreachable(
2257         "BugReport::markInteresting currently can only handle 2 different "
2258         "tracking kinds! Please define what tracking kind should this entitiy"
2259         "have, if it was already marked as interesting with a different kind!");
2260 }
2261 
2262 void PathSensitiveBugReport::markInteresting(SymbolRef sym,
2263                                              bugreporter::TrackingKind TKind) {
2264   if (!sym)
2265     return;
2266 
2267   insertToInterestingnessMap(InterestingSymbols, sym, TKind);
2268 
2269   // FIXME: No tests exist for this code and it is questionable:
2270   // How to handle multiple metadata for the same region?
2271   if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
2272     markInteresting(meta->getRegion(), TKind);
2273 }
2274 
2275 void PathSensitiveBugReport::markNotInteresting(SymbolRef sym) {
2276   if (!sym)
2277     return;
2278   InterestingSymbols.erase(sym);
2279 
2280   // The metadata part of markInteresting is not reversed here.
2281   // Just making the same region not interesting is incorrect
2282   // in specific cases.
2283   if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
2284     markNotInteresting(meta->getRegion());
2285 }
2286 
2287 void PathSensitiveBugReport::markInteresting(const MemRegion *R,
2288                                              bugreporter::TrackingKind TKind) {
2289   if (!R)
2290     return;
2291 
2292   R = R->getBaseRegion();
2293   insertToInterestingnessMap(InterestingRegions, R, TKind);
2294 
2295   if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2296     markInteresting(SR->getSymbol(), TKind);
2297 }
2298 
2299 void PathSensitiveBugReport::markNotInteresting(const MemRegion *R) {
2300   if (!R)
2301     return;
2302 
2303   R = R->getBaseRegion();
2304   InterestingRegions.erase(R);
2305 
2306   if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2307     markNotInteresting(SR->getSymbol());
2308 }
2309 
2310 void PathSensitiveBugReport::markInteresting(SVal V,
2311                                              bugreporter::TrackingKind TKind) {
2312   markInteresting(V.getAsRegion(), TKind);
2313   markInteresting(V.getAsSymbol(), TKind);
2314 }
2315 
2316 void PathSensitiveBugReport::markInteresting(const LocationContext *LC) {
2317   if (!LC)
2318     return;
2319   InterestingLocationContexts.insert(LC);
2320 }
2321 
2322 std::optional<bugreporter::TrackingKind>
2323 PathSensitiveBugReport::getInterestingnessKind(SVal V) const {
2324   auto RKind = getInterestingnessKind(V.getAsRegion());
2325   auto SKind = getInterestingnessKind(V.getAsSymbol());
2326   if (!RKind)
2327     return SKind;
2328   if (!SKind)
2329     return RKind;
2330 
2331   // If either is marked with throrough tracking, return that, we wouldn't like
2332   // to downplay a note's importance by 'only' mentioning it as a condition.
2333   switch(*RKind) {
2334     case bugreporter::TrackingKind::Thorough:
2335       return RKind;
2336     case bugreporter::TrackingKind::Condition:
2337       return SKind;
2338   }
2339 
2340   llvm_unreachable(
2341       "BugReport::getInterestingnessKind currently can only handle 2 different "
2342       "tracking kinds! Please define what tracking kind should we return here "
2343       "when the kind of getAsRegion() and getAsSymbol() is different!");
2344   return std::nullopt;
2345 }
2346 
2347 std::optional<bugreporter::TrackingKind>
2348 PathSensitiveBugReport::getInterestingnessKind(SymbolRef sym) const {
2349   if (!sym)
2350     return std::nullopt;
2351   // We don't currently consider metadata symbols to be interesting
2352   // even if we know their region is interesting. Is that correct behavior?
2353   auto It = InterestingSymbols.find(sym);
2354   if (It == InterestingSymbols.end())
2355     return std::nullopt;
2356   return It->getSecond();
2357 }
2358 
2359 std::optional<bugreporter::TrackingKind>
2360 PathSensitiveBugReport::getInterestingnessKind(const MemRegion *R) const {
2361   if (!R)
2362     return std::nullopt;
2363 
2364   R = R->getBaseRegion();
2365   auto It = InterestingRegions.find(R);
2366   if (It != InterestingRegions.end())
2367     return It->getSecond();
2368 
2369   if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2370     return getInterestingnessKind(SR->getSymbol());
2371   return std::nullopt;
2372 }
2373 
2374 bool PathSensitiveBugReport::isInteresting(SVal V) const {
2375   return getInterestingnessKind(V).has_value();
2376 }
2377 
2378 bool PathSensitiveBugReport::isInteresting(SymbolRef sym) const {
2379   return getInterestingnessKind(sym).has_value();
2380 }
2381 
2382 bool PathSensitiveBugReport::isInteresting(const MemRegion *R) const {
2383   return getInterestingnessKind(R).has_value();
2384 }
2385 
2386 bool PathSensitiveBugReport::isInteresting(const LocationContext *LC)  const {
2387   if (!LC)
2388     return false;
2389   return InterestingLocationContexts.count(LC);
2390 }
2391 
2392 const Stmt *PathSensitiveBugReport::getStmt() const {
2393   if (!ErrorNode)
2394     return nullptr;
2395 
2396   ProgramPoint ProgP = ErrorNode->getLocation();
2397   const Stmt *S = nullptr;
2398 
2399   if (std::optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2400     CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
2401     if (BE->getBlock() == &Exit)
2402       S = ErrorNode->getPreviousStmtForDiagnostics();
2403   }
2404   if (!S)
2405     S = ErrorNode->getStmtForDiagnostics();
2406 
2407   return S;
2408 }
2409 
2410 ArrayRef<SourceRange>
2411 PathSensitiveBugReport::getRanges() const {
2412   // If no custom ranges, add the range of the statement corresponding to
2413   // the error node.
2414   if (Ranges.empty() && isa_and_nonnull<Expr>(getStmt()))
2415       return ErrorNodeRange;
2416 
2417   return Ranges;
2418 }
2419 
2420 PathDiagnosticLocation
2421 PathSensitiveBugReport::getLocation() const {
2422   assert(ErrorNode && "Cannot create a location with a null node.");
2423   const Stmt *S = ErrorNode->getStmtForDiagnostics();
2424     ProgramPoint P = ErrorNode->getLocation();
2425   const LocationContext *LC = P.getLocationContext();
2426   SourceManager &SM =
2427       ErrorNode->getState()->getStateManager().getContext().getSourceManager();
2428 
2429   if (!S) {
2430     // If this is an implicit call, return the implicit call point location.
2431       if (std::optional<PreImplicitCall> PIE = P.getAs<PreImplicitCall>())
2432       return PathDiagnosticLocation(PIE->getLocation(), SM);
2433     if (auto FE = P.getAs<FunctionExitPoint>()) {
2434       if (const ReturnStmt *RS = FE->getStmt())
2435         return PathDiagnosticLocation::createBegin(RS, SM, LC);
2436     }
2437     S = ErrorNode->getNextStmtForDiagnostics();
2438   }
2439 
2440   if (S) {
2441     // Attributed statements usually have corrupted begin locations,
2442     // it's OK to ignore attributes for our purposes and deal with
2443     // the actual annotated statement.
2444     if (const auto *AS = dyn_cast<AttributedStmt>(S))
2445       S = AS->getSubStmt();
2446 
2447     // For member expressions, return the location of the '.' or '->'.
2448     if (const auto *ME = dyn_cast<MemberExpr>(S))
2449       return PathDiagnosticLocation::createMemberLoc(ME, SM);
2450 
2451     // For binary operators, return the location of the operator.
2452     if (const auto *B = dyn_cast<BinaryOperator>(S))
2453       return PathDiagnosticLocation::createOperatorLoc(B, SM);
2454 
2455     if (P.getAs<PostStmtPurgeDeadSymbols>())
2456       return PathDiagnosticLocation::createEnd(S, SM, LC);
2457 
2458     if (S->getBeginLoc().isValid())
2459       return PathDiagnosticLocation(S, SM, LC);
2460 
2461     return PathDiagnosticLocation(
2462         PathDiagnosticLocation::getValidSourceLocation(S, LC), SM);
2463   }
2464 
2465   return PathDiagnosticLocation::createDeclEnd(ErrorNode->getLocationContext(),
2466                                                SM);
2467 }
2468 
2469 //===----------------------------------------------------------------------===//
2470 // Methods for BugReporter and subclasses.
2471 //===----------------------------------------------------------------------===//
2472 
2473 const ExplodedGraph &PathSensitiveBugReporter::getGraph() const {
2474   return Eng.getGraph();
2475 }
2476 
2477 ProgramStateManager &PathSensitiveBugReporter::getStateManager() const {
2478   return Eng.getStateManager();
2479 }
2480 
2481 BugReporter::BugReporter(BugReporterData &D)
2482     : D(D), UserSuppressions(D.getASTContext()) {}
2483 
2484 BugReporter::~BugReporter() {
2485   // Make sure reports are flushed.
2486   assert(StrBugTypes.empty() &&
2487          "Destroying BugReporter before diagnostics are emitted!");
2488 
2489   // Free the bug reports we are tracking.
2490   for (const auto I : EQClassesVector)
2491     delete I;
2492 }
2493 
2494 void BugReporter::FlushReports() {
2495   // We need to flush reports in deterministic order to ensure the order
2496   // of the reports is consistent between runs.
2497   for (const auto EQ : EQClassesVector)
2498     FlushReport(*EQ);
2499 
2500   // BugReporter owns and deletes only BugTypes created implicitly through
2501   // EmitBasicReport.
2502   // FIXME: There are leaks from checkers that assume that the BugTypes they
2503   // create will be destroyed by the BugReporter.
2504   StrBugTypes.clear();
2505 }
2506 
2507 //===----------------------------------------------------------------------===//
2508 // PathDiagnostics generation.
2509 //===----------------------------------------------------------------------===//
2510 
2511 namespace {
2512 
2513 /// A wrapper around an ExplodedGraph that contains a single path from the root
2514 /// to the error node.
2515 class BugPathInfo {
2516 public:
2517   std::unique_ptr<ExplodedGraph> BugPath;
2518   PathSensitiveBugReport *Report;
2519   const ExplodedNode *ErrorNode;
2520 };
2521 
2522 /// A wrapper around an ExplodedGraph whose leafs are all error nodes. Can
2523 /// conveniently retrieve bug paths from a single error node to the root.
2524 class BugPathGetter {
2525   std::unique_ptr<ExplodedGraph> TrimmedGraph;
2526 
2527   using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>;
2528 
2529   /// Assign each node with its distance from the root.
2530   PriorityMapTy PriorityMap;
2531 
2532   /// Since the getErrorNode() or BugReport refers to the original ExplodedGraph,
2533   /// we need to pair it to the error node of the constructed trimmed graph.
2534   using ReportNewNodePair =
2535       std::pair<PathSensitiveBugReport *, const ExplodedNode *>;
2536   SmallVector<ReportNewNodePair, 32> ReportNodes;
2537 
2538   BugPathInfo CurrentBugPath;
2539 
2540   /// A helper class for sorting ExplodedNodes by priority.
2541   template <bool Descending>
2542   class PriorityCompare {
2543     const PriorityMapTy &PriorityMap;
2544 
2545   public:
2546     PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2547 
2548     bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2549       PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2550       PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2551       PriorityMapTy::const_iterator E = PriorityMap.end();
2552 
2553       if (LI == E)
2554         return Descending;
2555       if (RI == E)
2556         return !Descending;
2557 
2558       return Descending ? LI->second > RI->second
2559                         : LI->second < RI->second;
2560     }
2561 
2562     bool operator()(const ReportNewNodePair &LHS,
2563                     const ReportNewNodePair &RHS) const {
2564       return (*this)(LHS.second, RHS.second);
2565     }
2566   };
2567 
2568 public:
2569   BugPathGetter(const ExplodedGraph *OriginalGraph,
2570                 ArrayRef<PathSensitiveBugReport *> &bugReports);
2571 
2572   BugPathInfo *getNextBugPath();
2573 };
2574 
2575 } // namespace
2576 
2577 BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph,
2578                              ArrayRef<PathSensitiveBugReport *> &bugReports) {
2579   SmallVector<const ExplodedNode *, 32> Nodes;
2580   for (const auto I : bugReports) {
2581     assert(I->isValid() &&
2582            "We only allow BugReporterVisitors and BugReporter itself to "
2583            "invalidate reports!");
2584     Nodes.emplace_back(I->getErrorNode());
2585   }
2586 
2587   // The trimmed graph is created in the body of the constructor to ensure
2588   // that the DenseMaps have been initialized already.
2589   InterExplodedGraphMap ForwardMap;
2590   TrimmedGraph = OriginalGraph->trim(Nodes, &ForwardMap);
2591 
2592   // Find the (first) error node in the trimmed graph.  We just need to consult
2593   // the node map which maps from nodes in the original graph to nodes
2594   // in the new graph.
2595   llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
2596 
2597   for (PathSensitiveBugReport *Report : bugReports) {
2598     const ExplodedNode *NewNode = ForwardMap.lookup(Report->getErrorNode());
2599     assert(NewNode &&
2600            "Failed to construct a trimmed graph that contains this error "
2601            "node!");
2602     ReportNodes.emplace_back(Report, NewNode);
2603     RemainingNodes.insert(NewNode);
2604   }
2605 
2606   assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2607 
2608   // Perform a forward BFS to find all the shortest paths.
2609   std::queue<const ExplodedNode *> WS;
2610 
2611   assert(TrimmedGraph->num_roots() == 1);
2612   WS.push(*TrimmedGraph->roots_begin());
2613   unsigned Priority = 0;
2614 
2615   while (!WS.empty()) {
2616     const ExplodedNode *Node = WS.front();
2617     WS.pop();
2618 
2619     PriorityMapTy::iterator PriorityEntry;
2620     bool IsNew;
2621     std::tie(PriorityEntry, IsNew) = PriorityMap.insert({Node, Priority});
2622     ++Priority;
2623 
2624     if (!IsNew) {
2625       assert(PriorityEntry->second <= Priority);
2626       continue;
2627     }
2628 
2629     if (RemainingNodes.erase(Node))
2630       if (RemainingNodes.empty())
2631         break;
2632 
2633     for (const ExplodedNode *Succ : Node->succs())
2634       WS.push(Succ);
2635   }
2636 
2637   // Sort the error paths from longest to shortest.
2638   llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap));
2639 }
2640 
2641 BugPathInfo *BugPathGetter::getNextBugPath() {
2642   if (ReportNodes.empty())
2643     return nullptr;
2644 
2645   const ExplodedNode *OrigN;
2646   std::tie(CurrentBugPath.Report, OrigN) = ReportNodes.pop_back_val();
2647   assert(PriorityMap.contains(OrigN) && "error node not accessible from root");
2648 
2649   // Create a new graph with a single path. This is the graph that will be
2650   // returned to the caller.
2651   auto GNew = std::make_unique<ExplodedGraph>();
2652 
2653   // Now walk from the error node up the BFS path, always taking the
2654   // predeccessor with the lowest number.
2655   ExplodedNode *Succ = nullptr;
2656   while (true) {
2657     // Create the equivalent node in the new graph with the same state
2658     // and location.
2659     ExplodedNode *NewN = GNew->createUncachedNode(
2660         OrigN->getLocation(), OrigN->getState(),
2661         OrigN->getID(), OrigN->isSink());
2662 
2663     // Link up the new node with the previous node.
2664     if (Succ)
2665       Succ->addPredecessor(NewN, *GNew);
2666     else
2667       CurrentBugPath.ErrorNode = NewN;
2668 
2669     Succ = NewN;
2670 
2671     // Are we at the final node?
2672     if (OrigN->pred_empty()) {
2673       GNew->addRoot(NewN);
2674       break;
2675     }
2676 
2677     // Find the next predeccessor node.  We choose the node that is marked
2678     // with the lowest BFS number.
2679     OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2680                               PriorityCompare<false>(PriorityMap));
2681   }
2682 
2683   CurrentBugPath.BugPath = std::move(GNew);
2684 
2685   return &CurrentBugPath;
2686 }
2687 
2688 /// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic
2689 /// object and collapses PathDiagosticPieces that are expanded by macros.
2690 static void CompactMacroExpandedPieces(PathPieces &path,
2691                                        const SourceManager& SM) {
2692   using MacroStackTy = std::vector<
2693       std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>;
2694 
2695   using PiecesTy = std::vector<PathDiagnosticPieceRef>;
2696 
2697   MacroStackTy MacroStack;
2698   PiecesTy Pieces;
2699 
2700   for (PathPieces::const_iterator I = path.begin(), E = path.end();
2701        I != E; ++I) {
2702     const auto &piece = *I;
2703 
2704     // Recursively compact calls.
2705     if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
2706       CompactMacroExpandedPieces(call->path, SM);
2707     }
2708 
2709     // Get the location of the PathDiagnosticPiece.
2710     const FullSourceLoc Loc = piece->getLocation().asLocation();
2711 
2712     // Determine the instantiation location, which is the location we group
2713     // related PathDiagnosticPieces.
2714     SourceLocation InstantiationLoc = Loc.isMacroID() ?
2715                                       SM.getExpansionLoc(Loc) :
2716                                       SourceLocation();
2717 
2718     if (Loc.isFileID()) {
2719       MacroStack.clear();
2720       Pieces.push_back(piece);
2721       continue;
2722     }
2723 
2724     assert(Loc.isMacroID());
2725 
2726     // Is the PathDiagnosticPiece within the same macro group?
2727     if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
2728       MacroStack.back().first->subPieces.push_back(piece);
2729       continue;
2730     }
2731 
2732     // We aren't in the same group.  Are we descending into a new macro
2733     // or are part of an old one?
2734     std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
2735 
2736     SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2737                                           SM.getExpansionLoc(Loc) :
2738                                           SourceLocation();
2739 
2740     // Walk the entire macro stack.
2741     while (!MacroStack.empty()) {
2742       if (InstantiationLoc == MacroStack.back().second) {
2743         MacroGroup = MacroStack.back().first;
2744         break;
2745       }
2746 
2747       if (ParentInstantiationLoc == MacroStack.back().second) {
2748         MacroGroup = MacroStack.back().first;
2749         break;
2750       }
2751 
2752       MacroStack.pop_back();
2753     }
2754 
2755     if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
2756       // Create a new macro group and add it to the stack.
2757       auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
2758           PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
2759 
2760       if (MacroGroup)
2761         MacroGroup->subPieces.push_back(NewGroup);
2762       else {
2763         assert(InstantiationLoc.isFileID());
2764         Pieces.push_back(NewGroup);
2765       }
2766 
2767       MacroGroup = NewGroup;
2768       MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
2769     }
2770 
2771     // Finally, add the PathDiagnosticPiece to the group.
2772     MacroGroup->subPieces.push_back(piece);
2773   }
2774 
2775   // Now take the pieces and construct a new PathDiagnostic.
2776   path.clear();
2777 
2778   path.insert(path.end(), Pieces.begin(), Pieces.end());
2779 }
2780 
2781 /// Generate notes from all visitors.
2782 /// Notes associated with @c ErrorNode are generated using
2783 /// @c getEndPath, and the rest are generated with @c VisitNode.
2784 static std::unique_ptr<VisitorsDiagnosticsTy>
2785 generateVisitorsDiagnostics(PathSensitiveBugReport *R,
2786                             const ExplodedNode *ErrorNode,
2787                             BugReporterContext &BRC) {
2788   std::unique_ptr<VisitorsDiagnosticsTy> Notes =
2789       std::make_unique<VisitorsDiagnosticsTy>();
2790   PathSensitiveBugReport::VisitorList visitors;
2791 
2792   // Run visitors on all nodes starting from the node *before* the last one.
2793   // The last node is reserved for notes generated with @c getEndPath.
2794   const ExplodedNode *NextNode = ErrorNode->getFirstPred();
2795   while (NextNode) {
2796 
2797     // At each iteration, move all visitors from report to visitor list. This is
2798     // important, because the Profile() functions of the visitors make sure that
2799     // a visitor isn't added multiple times for the same node, but it's fine
2800     // to add the a visitor with Profile() for different nodes (e.g. tracking
2801     // a region at different points of the symbolic execution).
2802     for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors())
2803       visitors.push_back(std::move(Visitor));
2804 
2805     R->clearVisitors();
2806 
2807     const ExplodedNode *Pred = NextNode->getFirstPred();
2808     if (!Pred) {
2809       PathDiagnosticPieceRef LastPiece;
2810       for (auto &V : visitors) {
2811         V->finalizeVisitor(BRC, ErrorNode, *R);
2812 
2813         if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) {
2814           assert(!LastPiece &&
2815                  "There can only be one final piece in a diagnostic.");
2816           assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event &&
2817                  "The final piece must contain a message!");
2818           LastPiece = std::move(Piece);
2819           (*Notes)[ErrorNode].push_back(LastPiece);
2820         }
2821       }
2822       break;
2823     }
2824 
2825     for (auto &V : visitors) {
2826       auto P = V->VisitNode(NextNode, BRC, *R);
2827       if (P)
2828         (*Notes)[NextNode].push_back(std::move(P));
2829     }
2830 
2831     if (!R->isValid())
2832       break;
2833 
2834     NextNode = Pred;
2835   }
2836 
2837   return Notes;
2838 }
2839 
2840 std::optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(
2841     ArrayRef<PathSensitiveBugReport *> &bugReports,
2842     PathSensitiveBugReporter &Reporter) {
2843 
2844   BugPathGetter BugGraph(&Reporter.getGraph(), bugReports);
2845 
2846   while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) {
2847     // Find the BugReport with the original location.
2848     PathSensitiveBugReport *R = BugPath->Report;
2849     assert(R && "No original report found for sliced graph.");
2850     assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
2851     const ExplodedNode *ErrorNode = BugPath->ErrorNode;
2852 
2853     // Register refutation visitors first, if they mark the bug invalid no
2854     // further analysis is required
2855     R->addVisitor<LikelyFalsePositiveSuppressionBRVisitor>();
2856 
2857     // Register additional node visitors.
2858     R->addVisitor<NilReceiverBRVisitor>();
2859     R->addVisitor<ConditionBRVisitor>();
2860     R->addVisitor<TagVisitor>();
2861 
2862     BugReporterContext BRC(Reporter);
2863 
2864     // Run all visitors on a given graph, once.
2865     std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes =
2866         generateVisitorsDiagnostics(R, ErrorNode, BRC);
2867 
2868     if (R->isValid()) {
2869       if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
2870         // If crosscheck is enabled, remove all visitors, add the refutation
2871         // visitor and check again
2872         R->clearVisitors();
2873         Z3CrosscheckVisitor::Z3Result CrosscheckResult;
2874         R->addVisitor<Z3CrosscheckVisitor>(CrosscheckResult);
2875 
2876         // We don't overwrite the notes inserted by other visitors because the
2877         // refutation manager does not add any new note to the path
2878         generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC);
2879         switch (Z3CrosscheckOracle::interpretQueryResult(CrosscheckResult)) {
2880         case Z3CrosscheckOracle::RejectReport:
2881           ++NumTimesReportRefuted;
2882           R->markInvalid("Infeasible constraints", /*Data=*/nullptr);
2883           continue;
2884         case Z3CrosscheckOracle::AcceptReport:
2885           ++NumTimesReportPassesZ3;
2886           break;
2887         }
2888       }
2889 
2890       assert(R->isValid());
2891       return PathDiagnosticBuilder(std::move(BRC), std::move(BugPath->BugPath),
2892                                    BugPath->Report, BugPath->ErrorNode,
2893                                    std::move(visitorNotes));
2894     }
2895   }
2896 
2897   ++NumTimesReportEQClassWasExhausted;
2898   return {};
2899 }
2900 
2901 std::unique_ptr<DiagnosticForConsumerMapTy>
2902 PathSensitiveBugReporter::generatePathDiagnostics(
2903     ArrayRef<PathDiagnosticConsumer *> consumers,
2904     ArrayRef<PathSensitiveBugReport *> &bugReports) {
2905   assert(!bugReports.empty());
2906 
2907   auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
2908 
2909   std::optional<PathDiagnosticBuilder> PDB =
2910       PathDiagnosticBuilder::findValidReport(bugReports, *this);
2911 
2912   if (PDB) {
2913     for (PathDiagnosticConsumer *PC : consumers) {
2914       if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) {
2915         (*Out)[PC] = std::move(PD);
2916       }
2917     }
2918   }
2919 
2920   return Out;
2921 }
2922 
2923 void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
2924   bool ValidSourceLoc = R->getLocation().isValid();
2925   assert(ValidSourceLoc);
2926   // If we mess up in a release build, we'd still prefer to just drop the bug
2927   // instead of trying to go on.
2928   if (!ValidSourceLoc)
2929     return;
2930 
2931   // If the user asked to suppress this report, we should skip it.
2932   if (UserSuppressions.isSuppressed(*R))
2933     return;
2934 
2935   // Compute the bug report's hash to determine its equivalence class.
2936   llvm::FoldingSetNodeID ID;
2937   R->Profile(ID);
2938 
2939   // Lookup the equivance class.  If there isn't one, create it.
2940   void *InsertPos;
2941   BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
2942 
2943   if (!EQ) {
2944     EQ = new BugReportEquivClass(std::move(R));
2945     EQClasses.InsertNode(EQ, InsertPos);
2946     EQClassesVector.push_back(EQ);
2947   } else
2948     EQ->AddReport(std::move(R));
2949 }
2950 
2951 void PathSensitiveBugReporter::emitReport(std::unique_ptr<BugReport> R) {
2952   if (auto PR = dyn_cast<PathSensitiveBugReport>(R.get()))
2953     if (const ExplodedNode *E = PR->getErrorNode()) {
2954       // An error node must either be a sink or have a tag, otherwise
2955       // it could get reclaimed before the path diagnostic is created.
2956       assert((E->isSink() || E->getLocation().getTag()) &&
2957              "Error node must either be a sink or have a tag");
2958 
2959       const AnalysisDeclContext *DeclCtx =
2960           E->getLocationContext()->getAnalysisDeclContext();
2961       // The source of autosynthesized body can be handcrafted AST or a model
2962       // file. The locations from handcrafted ASTs have no valid source
2963       // locations and have to be discarded. Locations from model files should
2964       // be preserved for processing and reporting.
2965       if (DeclCtx->isBodyAutosynthesized() &&
2966           !DeclCtx->isBodyAutosynthesizedFromModelFile())
2967         return;
2968     }
2969 
2970   BugReporter::emitReport(std::move(R));
2971 }
2972 
2973 //===----------------------------------------------------------------------===//
2974 // Emitting reports in equivalence classes.
2975 //===----------------------------------------------------------------------===//
2976 
2977 namespace {
2978 
2979 struct FRIEC_WLItem {
2980   const ExplodedNode *N;
2981   ExplodedNode::const_succ_iterator I, E;
2982 
2983   FRIEC_WLItem(const ExplodedNode *n)
2984       : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2985 };
2986 
2987 } // namespace
2988 
2989 BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass(
2990     BugReportEquivClass &EQ, SmallVectorImpl<BugReport *> &bugReports) {
2991   // If we don't need to suppress any of the nodes because they are
2992   // post-dominated by a sink, simply add all the nodes in the equivalence class
2993   // to 'Nodes'.  Any of the reports will serve as a "representative" report.
2994   assert(EQ.getReports().size() > 0);
2995   const BugType& BT = EQ.getReports()[0]->getBugType();
2996   if (!BT.isSuppressOnSink()) {
2997     BugReport *R = EQ.getReports()[0].get();
2998     for (auto &J : EQ.getReports()) {
2999       if (auto *PR = dyn_cast<PathSensitiveBugReport>(J.get())) {
3000         R = PR;
3001         bugReports.push_back(PR);
3002       }
3003     }
3004     return R;
3005   }
3006 
3007   // For bug reports that should be suppressed when all paths are post-dominated
3008   // by a sink node, iterate through the reports in the equivalence class
3009   // until we find one that isn't post-dominated (if one exists).  We use a
3010   // DFS traversal of the ExplodedGraph to find a non-sink node.  We could write
3011   // this as a recursive function, but we don't want to risk blowing out the
3012   // stack for very long paths.
3013   BugReport *exampleReport = nullptr;
3014 
3015   for (const auto &I: EQ.getReports()) {
3016     auto *R = dyn_cast<PathSensitiveBugReport>(I.get());
3017     if (!R)
3018       continue;
3019 
3020     const ExplodedNode *errorNode = R->getErrorNode();
3021     if (errorNode->isSink()) {
3022       llvm_unreachable(
3023            "BugType::isSuppressSink() should not be 'true' for sink end nodes");
3024     }
3025     // No successors?  By definition this nodes isn't post-dominated by a sink.
3026     if (errorNode->succ_empty()) {
3027       bugReports.push_back(R);
3028       if (!exampleReport)
3029         exampleReport = R;
3030       continue;
3031     }
3032 
3033     // See if we are in a no-return CFG block. If so, treat this similarly
3034     // to being post-dominated by a sink. This works better when the analysis
3035     // is incomplete and we have never reached the no-return function call(s)
3036     // that we'd inevitably bump into on this path.
3037     if (const CFGBlock *ErrorB = errorNode->getCFGBlock())
3038       if (ErrorB->isInevitablySinking())
3039         continue;
3040 
3041     // At this point we know that 'N' is not a sink and it has at least one
3042     // successor.  Use a DFS worklist to find a non-sink end-of-path node.
3043     using WLItem = FRIEC_WLItem;
3044     using DFSWorkList = SmallVector<WLItem, 10>;
3045 
3046     llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
3047 
3048     DFSWorkList WL;
3049     WL.push_back(errorNode);
3050     Visited[errorNode] = 1;
3051 
3052     while (!WL.empty()) {
3053       WLItem &WI = WL.back();
3054       assert(!WI.N->succ_empty());
3055 
3056       for (; WI.I != WI.E; ++WI.I) {
3057         const ExplodedNode *Succ = *WI.I;
3058         // End-of-path node?
3059         if (Succ->succ_empty()) {
3060           // If we found an end-of-path node that is not a sink.
3061           if (!Succ->isSink()) {
3062             bugReports.push_back(R);
3063             if (!exampleReport)
3064               exampleReport = R;
3065             WL.clear();
3066             break;
3067           }
3068           // Found a sink?  Continue on to the next successor.
3069           continue;
3070         }
3071         // Mark the successor as visited.  If it hasn't been explored,
3072         // enqueue it to the DFS worklist.
3073         unsigned &mark = Visited[Succ];
3074         if (!mark) {
3075           mark = 1;
3076           WL.push_back(Succ);
3077           break;
3078         }
3079       }
3080 
3081       // The worklist may have been cleared at this point.  First
3082       // check if it is empty before checking the last item.
3083       if (!WL.empty() && &WL.back() == &WI)
3084         WL.pop_back();
3085     }
3086   }
3087 
3088   // ExampleReport will be NULL if all the nodes in the equivalence class
3089   // were post-dominated by sinks.
3090   return exampleReport;
3091 }
3092 
3093 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
3094   SmallVector<BugReport*, 10> bugReports;
3095   BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
3096   if (!report)
3097     return;
3098 
3099   // See whether we need to silence the checker/package.
3100   for (const std::string &CheckerOrPackage :
3101        getAnalyzerOptions().SilencedCheckersAndPackages) {
3102     if (report->getBugType().getCheckerName().starts_with(CheckerOrPackage))
3103       return;
3104   }
3105 
3106   ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
3107   std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
3108       generateDiagnosticForConsumerMap(report, Consumers, bugReports);
3109 
3110   for (auto &P : *Diagnostics) {
3111     PathDiagnosticConsumer *Consumer = P.first;
3112     std::unique_ptr<PathDiagnostic> &PD = P.second;
3113 
3114     // If the path is empty, generate a single step path with the location
3115     // of the issue.
3116     if (PD->path.empty()) {
3117       PathDiagnosticLocation L = report->getLocation();
3118       auto piece = std::make_unique<PathDiagnosticEventPiece>(
3119         L, report->getDescription());
3120       for (SourceRange Range : report->getRanges())
3121         piece->addRange(Range);
3122       PD->setEndOfPath(std::move(piece));
3123     }
3124 
3125     PathPieces &Pieces = PD->getMutablePieces();
3126     if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) {
3127       // For path diagnostic consumers that don't support extra notes,
3128       // we may optionally convert those to path notes.
3129       for (const auto &I : llvm::reverse(report->getNotes())) {
3130         PathDiagnosticNotePiece *Piece = I.get();
3131         auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
3132           Piece->getLocation(), Piece->getString());
3133         for (const auto &R: Piece->getRanges())
3134           ConvertedPiece->addRange(R);
3135 
3136         Pieces.push_front(std::move(ConvertedPiece));
3137       }
3138     } else {
3139       for (const auto &I : llvm::reverse(report->getNotes()))
3140         Pieces.push_front(I);
3141     }
3142 
3143     for (const auto &I : report->getFixits())
3144       Pieces.back()->addFixit(I);
3145 
3146     updateExecutedLinesWithDiagnosticPieces(*PD);
3147 
3148     // If we are debugging, let's have the entry point as the first note.
3149     if (getAnalyzerOptions().AnalyzerDisplayProgress ||
3150         getAnalyzerOptions().AnalyzerNoteAnalysisEntryPoints) {
3151       const Decl *EntryPoint = getAnalysisEntryPoint();
3152       Pieces.push_front(std::make_shared<PathDiagnosticEventPiece>(
3153           PathDiagnosticLocation{EntryPoint->getLocation(), getSourceManager()},
3154           "[debug] analyzing from " +
3155               AnalysisDeclContext::getFunctionName(EntryPoint)));
3156     }
3157     Consumer->HandlePathDiagnostic(std::move(PD));
3158   }
3159 }
3160 
3161 /// Insert all lines participating in the function signature \p Signature
3162 /// into \p ExecutedLines.
3163 static void populateExecutedLinesWithFunctionSignature(
3164     const Decl *Signature, const SourceManager &SM,
3165     FilesToLineNumsMap &ExecutedLines) {
3166   SourceRange SignatureSourceRange;
3167   const Stmt* Body = Signature->getBody();
3168   if (const auto FD = dyn_cast<FunctionDecl>(Signature)) {
3169     SignatureSourceRange = FD->getSourceRange();
3170   } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) {
3171     SignatureSourceRange = OD->getSourceRange();
3172   } else {
3173     return;
3174   }
3175   SourceLocation Start = SignatureSourceRange.getBegin();
3176   SourceLocation End = Body ? Body->getSourceRange().getBegin()
3177     : SignatureSourceRange.getEnd();
3178   if (!Start.isValid() || !End.isValid())
3179     return;
3180   unsigned StartLine = SM.getExpansionLineNumber(Start);
3181   unsigned EndLine = SM.getExpansionLineNumber(End);
3182 
3183   FileID FID = SM.getFileID(SM.getExpansionLoc(Start));
3184   for (unsigned Line = StartLine; Line <= EndLine; Line++)
3185     ExecutedLines[FID].insert(Line);
3186 }
3187 
3188 static void populateExecutedLinesWithStmt(
3189     const Stmt *S, const SourceManager &SM,
3190     FilesToLineNumsMap &ExecutedLines) {
3191   SourceLocation Loc = S->getSourceRange().getBegin();
3192   if (!Loc.isValid())
3193     return;
3194   SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
3195   FileID FID = SM.getFileID(ExpansionLoc);
3196   unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
3197   ExecutedLines[FID].insert(LineNo);
3198 }
3199 
3200 /// \return all executed lines including function signatures on the path
3201 /// starting from \p N.
3202 static std::unique_ptr<FilesToLineNumsMap>
3203 findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
3204   auto ExecutedLines = std::make_unique<FilesToLineNumsMap>();
3205 
3206   while (N) {
3207     if (N->getFirstPred() == nullptr) {
3208       // First node: show signature of the entrance point.
3209       const Decl *D = N->getLocationContext()->getDecl();
3210       populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3211     } else if (auto CE = N->getLocationAs<CallEnter>()) {
3212       // Inlined function: show signature.
3213       const Decl* D = CE->getCalleeContext()->getDecl();
3214       populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3215     } else if (const Stmt *S = N->getStmtForDiagnostics()) {
3216       populateExecutedLinesWithStmt(S, SM, *ExecutedLines);
3217 
3218       // Show extra context for some parent kinds.
3219       const Stmt *P = N->getParentMap().getParent(S);
3220 
3221       // The path exploration can die before the node with the associated
3222       // return statement is generated, but we do want to show the whole
3223       // return.
3224       if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
3225         populateExecutedLinesWithStmt(RS, SM, *ExecutedLines);
3226         P = N->getParentMap().getParent(RS);
3227       }
3228 
3229       if (isa_and_nonnull<SwitchCase, LabelStmt>(P))
3230         populateExecutedLinesWithStmt(P, SM, *ExecutedLines);
3231     }
3232 
3233     N = N->getFirstPred();
3234   }
3235   return ExecutedLines;
3236 }
3237 
3238 std::unique_ptr<DiagnosticForConsumerMapTy>
3239 BugReporter::generateDiagnosticForConsumerMap(
3240     BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3241     ArrayRef<BugReport *> bugReports) {
3242   auto *basicReport = cast<BasicBugReport>(exampleReport);
3243   auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
3244   for (auto *Consumer : consumers)
3245     (*Out)[Consumer] =
3246         generateDiagnosticForBasicReport(basicReport, AnalysisEntryPoint);
3247   return Out;
3248 }
3249 
3250 static PathDiagnosticCallPiece *
3251 getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
3252                                 const SourceManager &SMgr) {
3253   SourceLocation CallLoc = CP->callEnter.asLocation();
3254 
3255   // If the call is within a macro, don't do anything (for now).
3256   if (CallLoc.isMacroID())
3257     return nullptr;
3258 
3259   assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
3260          "The call piece should not be in a header file.");
3261 
3262   // Check if CP represents a path through a function outside of the main file.
3263   if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
3264     return CP;
3265 
3266   const PathPieces &Path = CP->path;
3267   if (Path.empty())
3268     return nullptr;
3269 
3270   // Check if the last piece in the callee path is a call to a function outside
3271   // of the main file.
3272   if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
3273     return getFirstStackedCallToHeaderFile(CPInner, SMgr);
3274 
3275   // Otherwise, the last piece is in the main file.
3276   return nullptr;
3277 }
3278 
3279 static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) {
3280   if (PD.path.empty())
3281     return;
3282 
3283   PathDiagnosticPiece *LastP = PD.path.back().get();
3284   assert(LastP);
3285   const SourceManager &SMgr = LastP->getLocation().getManager();
3286 
3287   // We only need to check if the report ends inside headers, if the last piece
3288   // is a call piece.
3289   if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
3290     CP = getFirstStackedCallToHeaderFile(CP, SMgr);
3291     if (CP) {
3292       // Mark the piece.
3293        CP->setAsLastInMainSourceFile();
3294 
3295       // Update the path diagnostic message.
3296       const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
3297       if (ND) {
3298         SmallString<200> buf;
3299         llvm::raw_svector_ostream os(buf);
3300         os << " (within a call to '" << ND->getDeclName() << "')";
3301         PD.appendToDesc(os.str());
3302       }
3303 
3304       // Reset the report containing declaration and location.
3305       PD.setDeclWithIssue(CP->getCaller());
3306       PD.setLocation(CP->getLocation());
3307 
3308       return;
3309     }
3310   }
3311 }
3312 
3313 
3314 
3315 std::unique_ptr<DiagnosticForConsumerMapTy>
3316 PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
3317     BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3318     ArrayRef<BugReport *> bugReports) {
3319   std::vector<BasicBugReport *> BasicBugReports;
3320   std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;
3321   if (isa<BasicBugReport>(exampleReport))
3322     return BugReporter::generateDiagnosticForConsumerMap(exampleReport,
3323                                                          consumers, bugReports);
3324 
3325   // Generate the full path sensitive diagnostic, using the generation scheme
3326   // specified by the PathDiagnosticConsumer. Note that we have to generate
3327   // path diagnostics even for consumers which do not support paths, because
3328   // the BugReporterVisitors may mark this bug as a false positive.
3329   assert(!bugReports.empty());
3330   MaxBugClassSize.updateMax(bugReports.size());
3331 
3332   // Avoid copying the whole array because there may be a lot of reports.
3333   ArrayRef<PathSensitiveBugReport *> convertedArrayOfReports(
3334       reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()),
3335       reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end()));
3336   std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics(
3337       consumers, convertedArrayOfReports);
3338 
3339   if (Out->empty())
3340     return Out;
3341 
3342   MaxValidBugClassSize.updateMax(bugReports.size());
3343 
3344   // Examine the report and see if the last piece is in a header. Reset the
3345   // report location to the last piece in the main source file.
3346   const AnalyzerOptions &Opts = getAnalyzerOptions();
3347   for (auto const &P : *Out)
3348     if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
3349       resetDiagnosticLocationToMainFile(*P.second);
3350 
3351   return Out;
3352 }
3353 
3354 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3355                                   const CheckerBase *Checker, StringRef Name,
3356                                   StringRef Category, StringRef Str,
3357                                   PathDiagnosticLocation Loc,
3358                                   ArrayRef<SourceRange> Ranges,
3359                                   ArrayRef<FixItHint> Fixits) {
3360   EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str,
3361                   Loc, Ranges, Fixits);
3362 }
3363 
3364 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3365                                   CheckerNameRef CheckName,
3366                                   StringRef name, StringRef category,
3367                                   StringRef str, PathDiagnosticLocation Loc,
3368                                   ArrayRef<SourceRange> Ranges,
3369                                   ArrayRef<FixItHint> Fixits) {
3370   // 'BT' is owned by BugReporter.
3371   BugType *BT = getBugTypeForName(CheckName, name, category);
3372   auto R = std::make_unique<BasicBugReport>(*BT, str, Loc);
3373   R->setDeclWithIssue(DeclWithIssue);
3374   for (const auto &SR : Ranges)
3375     R->addRange(SR);
3376   for (const auto &FH : Fixits)
3377     R->addFixItHint(FH);
3378   emitReport(std::move(R));
3379 }
3380 
3381 BugType *BugReporter::getBugTypeForName(CheckerNameRef CheckName,
3382                                         StringRef name, StringRef category) {
3383   SmallString<136> fullDesc;
3384   llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3385                                       << ":" << category;
3386   std::unique_ptr<BugType> &BT = StrBugTypes[fullDesc];
3387   if (!BT)
3388     BT = std::make_unique<BugType>(CheckName, name, category);
3389   return BT.get();
3390 }
3391