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