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