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/ParentMap.h"
20 #include "clang/AST/StmtObjC.h"
21 #include "clang/Analysis/CFG.h"
22 #include "clang/Analysis/ProgramPoint.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
25 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/IntrusiveRefCntPtr.h"
29 #include "llvm/ADT/OwningPtr.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SmallString.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <queue>
34 
35 using namespace clang;
36 using namespace ento;
37 
38 BugReporterVisitor::~BugReporterVisitor() {}
39 
40 void BugReporterContext::anchor() {}
41 
42 //===----------------------------------------------------------------------===//
43 // Helper routines for walking the ExplodedGraph and fetching statements.
44 //===----------------------------------------------------------------------===//
45 
46 static inline const Stmt *GetStmt(const ProgramPoint &P) {
47   if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48     return SP->getStmt();
49   else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
50     return BE->getSrc()->getTerminator();
51   else if (const CallEnter *CE = dyn_cast<CallEnter>(&P))
52     return CE->getCallExpr();
53   else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P))
54     return CEE->getCalleeContext()->getCallSite();
55 
56   return 0;
57 }
58 
59 static inline const ExplodedNode*
60 GetPredecessorNode(const ExplodedNode *N) {
61   return N->pred_empty() ? NULL : *(N->pred_begin());
62 }
63 
64 static inline const ExplodedNode*
65 GetSuccessorNode(const ExplodedNode *N) {
66   return N->succ_empty() ? NULL : *(N->succ_begin());
67 }
68 
69 static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
70   for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
71     if (const Stmt *S = GetStmt(N->getLocation()))
72       return S;
73 
74   return 0;
75 }
76 
77 static const Stmt *GetNextStmt(const ExplodedNode *N) {
78   for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
79     if (const Stmt *S = GetStmt(N->getLocation())) {
80       // Check if the statement is '?' or '&&'/'||'.  These are "merges",
81       // not actual statement points.
82       switch (S->getStmtClass()) {
83         case Stmt::ChooseExprClass:
84         case Stmt::BinaryConditionalOperatorClass: continue;
85         case Stmt::ConditionalOperatorClass: continue;
86         case Stmt::BinaryOperatorClass: {
87           BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88           if (Op == BO_LAnd || Op == BO_LOr)
89             continue;
90           break;
91         }
92         default:
93           break;
94       }
95       return S;
96     }
97 
98   return 0;
99 }
100 
101 static inline const Stmt*
102 GetCurrentOrPreviousStmt(const ExplodedNode *N) {
103   if (const Stmt *S = GetStmt(N->getLocation()))
104     return S;
105 
106   return GetPreviousStmt(N);
107 }
108 
109 static inline const Stmt*
110 GetCurrentOrNextStmt(const ExplodedNode *N) {
111   if (const Stmt *S = GetStmt(N->getLocation()))
112     return S;
113 
114   return GetNextStmt(N);
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // Diagnostic cleanup.
119 //===----------------------------------------------------------------------===//
120 
121 static PathDiagnosticEventPiece *
122 eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
123                             PathDiagnosticEventPiece *Y) {
124   // Prefer diagnostics that come from ConditionBRVisitor over
125   // those that came from TrackConstraintBRVisitor.
126   const void *tagPreferred = ConditionBRVisitor::getTag();
127   const void *tagLesser = TrackConstraintBRVisitor::getTag();
128 
129   if (X->getLocation() != Y->getLocation())
130     return 0;
131 
132   if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
133     return X;
134 
135   if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
136     return Y;
137 
138   return 0;
139 }
140 
141 /// An optimization pass over PathPieces that removes redundant diagnostics
142 /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor.  Both
143 /// BugReporterVisitors use different methods to generate diagnostics, with
144 /// one capable of emitting diagnostics in some cases but not in others.  This
145 /// can lead to redundant diagnostic pieces at the same point in a path.
146 static void removeRedundantMsgs(PathPieces &path) {
147   unsigned N = path.size();
148   if (N < 2)
149     return;
150   // NOTE: this loop intentionally is not using an iterator.  Instead, we
151   // are streaming the path and modifying it in place.  This is done by
152   // grabbing the front, processing it, and if we decide to keep it append
153   // it to the end of the path.  The entire path is processed in this way.
154   for (unsigned i = 0; i < N; ++i) {
155     IntrusiveRefCntPtr<PathDiagnosticPiece> piece(path.front());
156     path.pop_front();
157 
158     switch (piece->getKind()) {
159       case clang::ento::PathDiagnosticPiece::Call:
160         removeRedundantMsgs(cast<PathDiagnosticCallPiece>(piece)->path);
161         break;
162       case clang::ento::PathDiagnosticPiece::Macro:
163         removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(piece)->subPieces);
164         break;
165       case clang::ento::PathDiagnosticPiece::ControlFlow:
166         break;
167       case clang::ento::PathDiagnosticPiece::Event: {
168         if (i == N-1)
169           break;
170 
171         if (PathDiagnosticEventPiece *nextEvent =
172             dyn_cast<PathDiagnosticEventPiece>(path.front().getPtr())) {
173           PathDiagnosticEventPiece *event =
174             cast<PathDiagnosticEventPiece>(piece);
175           // Check to see if we should keep one of the two pieces.  If we
176           // come up with a preference, record which piece to keep, and consume
177           // another piece from the path.
178           if (PathDiagnosticEventPiece *pieceToKeep =
179               eventsDescribeSameCondition(event, nextEvent)) {
180             piece = pieceToKeep;
181             path.pop_front();
182             ++i;
183           }
184         }
185         break;
186       }
187     }
188     path.push_back(piece);
189   }
190 }
191 
192 /// Recursively scan through a path and prune out calls and macros pieces
193 /// that aren't needed.  Return true if afterwards the path contains
194 /// "interesting stuff" which means it shouldn't be pruned from the parent path.
195 bool BugReporter::RemoveUnneededCalls(PathPieces &pieces, BugReport *R) {
196   bool containsSomethingInteresting = false;
197   const unsigned N = pieces.size();
198 
199   for (unsigned i = 0 ; i < N ; ++i) {
200     // Remove the front piece from the path.  If it is still something we
201     // want to keep once we are done, we will push it back on the end.
202     IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
203     pieces.pop_front();
204 
205     // Throw away pieces with invalid locations. Note that we can't throw away
206     // calls just yet because they might have something interesting inside them.
207     // If so, their locations will be adjusted as necessary later.
208     if (piece->getKind() != PathDiagnosticPiece::Call &&
209         piece->getLocation().asLocation().isInvalid())
210       continue;
211 
212     switch (piece->getKind()) {
213       case PathDiagnosticPiece::Call: {
214         PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
215         // Check if the location context is interesting.
216         assert(LocationContextMap.count(call));
217         if (R->isInteresting(LocationContextMap[call])) {
218           containsSomethingInteresting = true;
219           break;
220         }
221 
222         if (!RemoveUnneededCalls(call->path, R))
223           continue;
224 
225         containsSomethingInteresting = true;
226         break;
227       }
228       case PathDiagnosticPiece::Macro: {
229         PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
230         if (!RemoveUnneededCalls(macro->subPieces, R))
231           continue;
232         containsSomethingInteresting = true;
233         break;
234       }
235       case PathDiagnosticPiece::Event: {
236         PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
237 
238         // We never throw away an event, but we do throw it away wholesale
239         // as part of a path if we throw the entire path away.
240         containsSomethingInteresting |= !event->isPrunable();
241         break;
242       }
243       case PathDiagnosticPiece::ControlFlow:
244         break;
245     }
246 
247     pieces.push_back(piece);
248   }
249 
250   return containsSomethingInteresting;
251 }
252 
253 /// Recursively scan through a path and make sure that all call pieces have
254 /// valid locations. Note that all other pieces with invalid locations should
255 /// have already been pruned out.
256 static void adjustCallLocations(PathPieces &Pieces,
257                                 PathDiagnosticLocation *LastCallLocation = 0) {
258   for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E; ++I) {
259     PathDiagnosticCallPiece *Call = dyn_cast<PathDiagnosticCallPiece>(*I);
260 
261     if (!Call) {
262       assert((*I)->getLocation().asLocation().isValid());
263       continue;
264     }
265 
266     if (LastCallLocation) {
267       if (!Call->callEnter.asLocation().isValid() ||
268           Call->getCaller()->isImplicit())
269         Call->callEnter = *LastCallLocation;
270       if (!Call->callReturn.asLocation().isValid() ||
271           Call->getCaller()->isImplicit())
272         Call->callReturn = *LastCallLocation;
273     }
274 
275     // Recursively clean out the subclass.  Keep this call around if
276     // it contains any informative diagnostics.
277     PathDiagnosticLocation *ThisCallLocation;
278     if (Call->callEnterWithin.asLocation().isValid() &&
279         !Call->getCallee()->isImplicit())
280       ThisCallLocation = &Call->callEnterWithin;
281     else
282       ThisCallLocation = &Call->callEnter;
283 
284     assert(ThisCallLocation && "Outermost call has an invalid location");
285     adjustCallLocations(Call->path, ThisCallLocation);
286   }
287 }
288 
289 //===----------------------------------------------------------------------===//
290 // PathDiagnosticBuilder and its associated routines and helper objects.
291 //===----------------------------------------------------------------------===//
292 
293 typedef llvm::DenseMap<const ExplodedNode*,
294 const ExplodedNode*> NodeBackMap;
295 
296 namespace {
297 class NodeMapClosure : public BugReport::NodeResolver {
298   NodeBackMap& M;
299 public:
300   NodeMapClosure(NodeBackMap *m) : M(*m) {}
301   ~NodeMapClosure() {}
302 
303   const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
304     NodeBackMap::iterator I = M.find(N);
305     return I == M.end() ? 0 : I->second;
306   }
307 };
308 
309 class PathDiagnosticBuilder : public BugReporterContext {
310   BugReport *R;
311   PathDiagnosticConsumer *PDC;
312   NodeMapClosure NMC;
313 public:
314   const LocationContext *LC;
315 
316   PathDiagnosticBuilder(GRBugReporter &br,
317                         BugReport *r, NodeBackMap *Backmap,
318                         PathDiagnosticConsumer *pdc)
319     : BugReporterContext(br),
320       R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
321   {}
322 
323   PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
324 
325   PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
326                                             const ExplodedNode *N);
327 
328   BugReport *getBugReport() { return R; }
329 
330   Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
331 
332   ParentMap& getParentMap() { return LC->getParentMap(); }
333 
334   const Stmt *getParent(const Stmt *S) {
335     return getParentMap().getParent(S);
336   }
337 
338   virtual NodeMapClosure& getNodeResolver() { return NMC; }
339 
340   PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
341 
342   PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
343     return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
344   }
345 
346   bool supportsLogicalOpControlFlow() const {
347     return PDC ? PDC->supportsLogicalOpControlFlow() : true;
348   }
349 };
350 } // end anonymous namespace
351 
352 PathDiagnosticLocation
353 PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
354   if (const Stmt *S = GetNextStmt(N))
355     return PathDiagnosticLocation(S, getSourceManager(), LC);
356 
357   return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
358                                                getSourceManager());
359 }
360 
361 PathDiagnosticLocation
362 PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
363                                           const ExplodedNode *N) {
364 
365   // Slow, but probably doesn't matter.
366   if (os.str().empty())
367     os << ' ';
368 
369   const PathDiagnosticLocation &Loc = ExecutionContinues(N);
370 
371   if (Loc.asStmt())
372     os << "Execution continues on line "
373        << getSourceManager().getExpansionLineNumber(Loc.asLocation())
374        << '.';
375   else {
376     os << "Execution jumps to the end of the ";
377     const Decl *D = N->getLocationContext()->getDecl();
378     if (isa<ObjCMethodDecl>(D))
379       os << "method";
380     else if (isa<FunctionDecl>(D))
381       os << "function";
382     else {
383       assert(isa<BlockDecl>(D));
384       os << "anonymous block";
385     }
386     os << '.';
387   }
388 
389   return Loc;
390 }
391 
392 static bool IsNested(const Stmt *S, ParentMap &PM) {
393   if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
394     return true;
395 
396   const Stmt *Parent = PM.getParentIgnoreParens(S);
397 
398   if (Parent)
399     switch (Parent->getStmtClass()) {
400       case Stmt::ForStmtClass:
401       case Stmt::DoStmtClass:
402       case Stmt::WhileStmtClass:
403         return true;
404       default:
405         break;
406     }
407 
408   return false;
409 }
410 
411 PathDiagnosticLocation
412 PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
413   assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
414   ParentMap &P = getParentMap();
415   SourceManager &SMgr = getSourceManager();
416 
417   while (IsNested(S, P)) {
418     const Stmt *Parent = P.getParentIgnoreParens(S);
419 
420     if (!Parent)
421       break;
422 
423     switch (Parent->getStmtClass()) {
424       case Stmt::BinaryOperatorClass: {
425         const BinaryOperator *B = cast<BinaryOperator>(Parent);
426         if (B->isLogicalOp())
427           return PathDiagnosticLocation(S, SMgr, LC);
428         break;
429       }
430       case Stmt::CompoundStmtClass:
431       case Stmt::StmtExprClass:
432         return PathDiagnosticLocation(S, SMgr, LC);
433       case Stmt::ChooseExprClass:
434         // Similar to '?' if we are referring to condition, just have the edge
435         // point to the entire choose expression.
436         if (cast<ChooseExpr>(Parent)->getCond() == S)
437           return PathDiagnosticLocation(Parent, SMgr, LC);
438         else
439           return PathDiagnosticLocation(S, SMgr, LC);
440       case Stmt::BinaryConditionalOperatorClass:
441       case Stmt::ConditionalOperatorClass:
442         // For '?', if we are referring to condition, just have the edge point
443         // to the entire '?' expression.
444         if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
445           return PathDiagnosticLocation(Parent, SMgr, LC);
446         else
447           return PathDiagnosticLocation(S, SMgr, LC);
448       case Stmt::DoStmtClass:
449           return PathDiagnosticLocation(S, SMgr, LC);
450       case Stmt::ForStmtClass:
451         if (cast<ForStmt>(Parent)->getBody() == S)
452           return PathDiagnosticLocation(S, SMgr, LC);
453         break;
454       case Stmt::IfStmtClass:
455         if (cast<IfStmt>(Parent)->getCond() != S)
456           return PathDiagnosticLocation(S, SMgr, LC);
457         break;
458       case Stmt::ObjCForCollectionStmtClass:
459         if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
460           return PathDiagnosticLocation(S, SMgr, LC);
461         break;
462       case Stmt::WhileStmtClass:
463         if (cast<WhileStmt>(Parent)->getCond() != S)
464           return PathDiagnosticLocation(S, SMgr, LC);
465         break;
466       default:
467         break;
468     }
469 
470     S = Parent;
471   }
472 
473   assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
474 
475   // Special case: DeclStmts can appear in for statement declarations, in which
476   //  case the ForStmt is the context.
477   if (isa<DeclStmt>(S)) {
478     if (const Stmt *Parent = P.getParent(S)) {
479       switch (Parent->getStmtClass()) {
480         case Stmt::ForStmtClass:
481         case Stmt::ObjCForCollectionStmtClass:
482           return PathDiagnosticLocation(Parent, SMgr, LC);
483         default:
484           break;
485       }
486     }
487   }
488   else if (isa<BinaryOperator>(S)) {
489     // Special case: the binary operator represents the initialization
490     // code in a for statement (this can happen when the variable being
491     // initialized is an old variable.
492     if (const ForStmt *FS =
493           dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
494       if (FS->getInit() == S)
495         return PathDiagnosticLocation(FS, SMgr, LC);
496     }
497   }
498 
499   return PathDiagnosticLocation(S, SMgr, LC);
500 }
501 
502 //===----------------------------------------------------------------------===//
503 // "Visitors only" path diagnostic generation algorithm.
504 //===----------------------------------------------------------------------===//
505 static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
506                                                PathDiagnosticBuilder &PDB,
507                                                const ExplodedNode *N,
508                                       ArrayRef<BugReporterVisitor *> visitors) {
509   // All path generation skips the very first node (the error node).
510   // This is because there is special handling for the end-of-path note.
511   N = N->getFirstPred();
512   if (!N)
513     return true;
514 
515   BugReport *R = PDB.getBugReport();
516   while (const ExplodedNode *Pred = N->getFirstPred()) {
517     for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
518                                                   E = visitors.end();
519          I != E; ++I) {
520       // Visit all the node pairs, but throw the path pieces away.
521       PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
522       delete Piece;
523     }
524 
525     N = Pred;
526   }
527 
528   return R->isValid();
529 }
530 
531 //===----------------------------------------------------------------------===//
532 // "Minimal" path diagnostic generation algorithm.
533 //===----------------------------------------------------------------------===//
534 typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
535 typedef SmallVector<StackDiagPair, 6> StackDiagVector;
536 
537 static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
538                                          StackDiagVector &CallStack) {
539   // If the piece contains a special message, add it to all the call
540   // pieces on the active stack.
541   if (PathDiagnosticEventPiece *ep =
542         dyn_cast<PathDiagnosticEventPiece>(P)) {
543 
544     if (ep->hasCallStackHint())
545       for (StackDiagVector::iterator I = CallStack.begin(),
546                                      E = CallStack.end(); I != E; ++I) {
547         PathDiagnosticCallPiece *CP = I->first;
548         const ExplodedNode *N = I->second;
549         std::string stackMsg = ep->getCallStackMessage(N);
550 
551         // The last message on the path to final bug is the most important
552         // one. Since we traverse the path backwards, do not add the message
553         // if one has been previously added.
554         if  (!CP->hasCallStackMessage())
555           CP->setCallStackMessage(stackMsg);
556       }
557   }
558 }
559 
560 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
561 
562 static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
563                                           PathDiagnosticBuilder &PDB,
564                                           const ExplodedNode *N,
565                                       ArrayRef<BugReporterVisitor *> visitors) {
566 
567   SourceManager& SMgr = PDB.getSourceManager();
568   const LocationContext *LC = PDB.LC;
569   const ExplodedNode *NextNode = N->pred_empty()
570                                         ? NULL : *(N->pred_begin());
571 
572   StackDiagVector CallStack;
573 
574   while (NextNode) {
575     N = NextNode;
576     PDB.LC = N->getLocationContext();
577     NextNode = GetPredecessorNode(N);
578 
579     ProgramPoint P = N->getLocation();
580 
581     do {
582       if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
583         PathDiagnosticCallPiece *C =
584             PathDiagnosticCallPiece::construct(N, *CE, SMgr);
585         GRBugReporter& BR = PDB.getBugReporter();
586         BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
587         PD.getActivePath().push_front(C);
588         PD.pushActivePath(&C->path);
589         CallStack.push_back(StackDiagPair(C, N));
590         break;
591       }
592 
593       if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
594         // Flush all locations, and pop the active path.
595         bool VisitedEntireCall = PD.isWithinCall();
596         PD.popActivePath();
597 
598         // Either we just added a bunch of stuff to the top-level path, or
599         // we have a previous CallExitEnd.  If the former, it means that the
600         // path terminated within a function call.  We must then take the
601         // current contents of the active path and place it within
602         // a new PathDiagnosticCallPiece.
603         PathDiagnosticCallPiece *C;
604         if (VisitedEntireCall) {
605           C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
606         } else {
607           const Decl *Caller = CE->getLocationContext()->getDecl();
608           C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
609           GRBugReporter& BR = PDB.getBugReporter();
610           BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
611         }
612 
613         C->setCallee(*CE, SMgr);
614         if (!CallStack.empty()) {
615           assert(CallStack.back().first == C);
616           CallStack.pop_back();
617         }
618         break;
619       }
620 
621       if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
622         const CFGBlock *Src = BE->getSrc();
623         const CFGBlock *Dst = BE->getDst();
624         const Stmt *T = Src->getTerminator();
625 
626         if (!T)
627           break;
628 
629         PathDiagnosticLocation Start =
630             PathDiagnosticLocation::createBegin(T, SMgr,
631                 N->getLocationContext());
632 
633         switch (T->getStmtClass()) {
634         default:
635           break;
636 
637         case Stmt::GotoStmtClass:
638         case Stmt::IndirectGotoStmtClass: {
639           const Stmt *S = GetNextStmt(N);
640 
641           if (!S)
642             break;
643 
644           std::string sbuf;
645           llvm::raw_string_ostream os(sbuf);
646           const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
647 
648           os << "Control jumps to line "
649               << End.asLocation().getExpansionLineNumber();
650           PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
651               Start, End, os.str()));
652           break;
653         }
654 
655         case Stmt::SwitchStmtClass: {
656           // Figure out what case arm we took.
657           std::string sbuf;
658           llvm::raw_string_ostream os(sbuf);
659 
660           if (const Stmt *S = Dst->getLabel()) {
661             PathDiagnosticLocation End(S, SMgr, LC);
662 
663             switch (S->getStmtClass()) {
664             default:
665               os << "No cases match in the switch statement. "
666               "Control jumps to line "
667               << End.asLocation().getExpansionLineNumber();
668               break;
669             case Stmt::DefaultStmtClass:
670               os << "Control jumps to the 'default' case at line "
671               << End.asLocation().getExpansionLineNumber();
672               break;
673 
674             case Stmt::CaseStmtClass: {
675               os << "Control jumps to 'case ";
676               const CaseStmt *Case = cast<CaseStmt>(S);
677               const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
678 
679               // Determine if it is an enum.
680               bool GetRawInt = true;
681 
682               if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
683                 // FIXME: Maybe this should be an assertion.  Are there cases
684                 // were it is not an EnumConstantDecl?
685                 const EnumConstantDecl *D =
686                     dyn_cast<EnumConstantDecl>(DR->getDecl());
687 
688                 if (D) {
689                   GetRawInt = false;
690                   os << *D;
691                 }
692               }
693 
694               if (GetRawInt)
695                 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
696 
697               os << ":'  at line "
698                   << End.asLocation().getExpansionLineNumber();
699               break;
700             }
701             }
702             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
703                 Start, End, os.str()));
704           }
705           else {
706             os << "'Default' branch taken. ";
707             const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
708             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
709                 Start, End, os.str()));
710           }
711 
712           break;
713         }
714 
715         case Stmt::BreakStmtClass:
716         case Stmt::ContinueStmtClass: {
717           std::string sbuf;
718           llvm::raw_string_ostream os(sbuf);
719           PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
720           PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
721               Start, End, os.str()));
722           break;
723         }
724 
725         // Determine control-flow for ternary '?'.
726         case Stmt::BinaryConditionalOperatorClass:
727         case Stmt::ConditionalOperatorClass: {
728           std::string sbuf;
729           llvm::raw_string_ostream os(sbuf);
730           os << "'?' condition is ";
731 
732           if (*(Src->succ_begin()+1) == Dst)
733             os << "false";
734           else
735             os << "true";
736 
737           PathDiagnosticLocation End = PDB.ExecutionContinues(N);
738 
739           if (const Stmt *S = End.asStmt())
740             End = PDB.getEnclosingStmtLocation(S);
741 
742           PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
743               Start, End, os.str()));
744           break;
745         }
746 
747         // Determine control-flow for short-circuited '&&' and '||'.
748         case Stmt::BinaryOperatorClass: {
749           if (!PDB.supportsLogicalOpControlFlow())
750             break;
751 
752           const BinaryOperator *B = cast<BinaryOperator>(T);
753           std::string sbuf;
754           llvm::raw_string_ostream os(sbuf);
755           os << "Left side of '";
756 
757           if (B->getOpcode() == BO_LAnd) {
758             os << "&&" << "' is ";
759 
760             if (*(Src->succ_begin()+1) == Dst) {
761               os << "false";
762               PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
763               PathDiagnosticLocation Start =
764                   PathDiagnosticLocation::createOperatorLoc(B, SMgr);
765               PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
766                   Start, End, os.str()));
767             }
768             else {
769               os << "true";
770               PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
771               PathDiagnosticLocation End = PDB.ExecutionContinues(N);
772               PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
773                   Start, End, os.str()));
774             }
775           }
776           else {
777             assert(B->getOpcode() == BO_LOr);
778             os << "||" << "' is ";
779 
780             if (*(Src->succ_begin()+1) == Dst) {
781               os << "false";
782               PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
783               PathDiagnosticLocation End = PDB.ExecutionContinues(N);
784               PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
785                   Start, End, os.str()));
786             }
787             else {
788               os << "true";
789               PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
790               PathDiagnosticLocation Start =
791                   PathDiagnosticLocation::createOperatorLoc(B, SMgr);
792               PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
793                   Start, End, os.str()));
794             }
795           }
796 
797           break;
798         }
799 
800         case Stmt::DoStmtClass:  {
801           if (*(Src->succ_begin()) == Dst) {
802             std::string sbuf;
803             llvm::raw_string_ostream os(sbuf);
804 
805             os << "Loop condition is true. ";
806             PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
807 
808             if (const Stmt *S = End.asStmt())
809               End = PDB.getEnclosingStmtLocation(S);
810 
811             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
812                 Start, End, os.str()));
813           }
814           else {
815             PathDiagnosticLocation End = PDB.ExecutionContinues(N);
816 
817             if (const Stmt *S = End.asStmt())
818               End = PDB.getEnclosingStmtLocation(S);
819 
820             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
821                 Start, End, "Loop condition is false.  Exiting loop"));
822           }
823 
824           break;
825         }
826 
827         case Stmt::WhileStmtClass:
828         case Stmt::ForStmtClass: {
829           if (*(Src->succ_begin()+1) == Dst) {
830             std::string sbuf;
831             llvm::raw_string_ostream os(sbuf);
832 
833             os << "Loop condition is false. ";
834             PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
835             if (const Stmt *S = End.asStmt())
836               End = PDB.getEnclosingStmtLocation(S);
837 
838             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
839                 Start, End, os.str()));
840           }
841           else {
842             PathDiagnosticLocation End = PDB.ExecutionContinues(N);
843             if (const Stmt *S = End.asStmt())
844               End = PDB.getEnclosingStmtLocation(S);
845 
846             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
847                 Start, End, "Loop condition is true.  Entering loop body"));
848           }
849 
850           break;
851         }
852 
853         case Stmt::IfStmtClass: {
854           PathDiagnosticLocation End = PDB.ExecutionContinues(N);
855 
856           if (const Stmt *S = End.asStmt())
857             End = PDB.getEnclosingStmtLocation(S);
858 
859           if (*(Src->succ_begin()+1) == Dst)
860             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
861                 Start, End, "Taking false branch"));
862           else
863             PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
864                 Start, End, "Taking true branch"));
865 
866           break;
867         }
868         }
869       }
870     } while(0);
871 
872     if (NextNode) {
873       // Add diagnostic pieces from custom visitors.
874       BugReport *R = PDB.getBugReport();
875       for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
876                                                     E = visitors.end();
877            I != E; ++I) {
878         if (PathDiagnosticPiece *p = (*I)->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 class EdgeBuilder {
929   std::vector<ContextLocation> CLocs;
930   typedef std::vector<ContextLocation>::iterator iterator;
931   PathDiagnostic &PD;
932   PathDiagnosticBuilder &PDB;
933   PathDiagnosticLocation PrevLoc;
934 
935   bool IsConsumedExpr(const PathDiagnosticLocation &L);
936 
937   bool containsLocation(const PathDiagnosticLocation &Container,
938                         const PathDiagnosticLocation &Containee);
939 
940   PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
941 
942   PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
943                                          bool firstCharOnly = false) {
944     if (const Stmt *S = L.asStmt()) {
945       const Stmt *Original = S;
946       while (1) {
947         // Adjust the location for some expressions that are best referenced
948         // by one of their subexpressions.
949         switch (S->getStmtClass()) {
950           default:
951             break;
952           case Stmt::ParenExprClass:
953           case Stmt::GenericSelectionExprClass:
954             S = cast<Expr>(S)->IgnoreParens();
955             firstCharOnly = true;
956             continue;
957           case Stmt::BinaryConditionalOperatorClass:
958           case Stmt::ConditionalOperatorClass:
959             S = cast<AbstractConditionalOperator>(S)->getCond();
960             firstCharOnly = true;
961             continue;
962           case Stmt::ChooseExprClass:
963             S = cast<ChooseExpr>(S)->getCond();
964             firstCharOnly = true;
965             continue;
966           case Stmt::BinaryOperatorClass:
967             S = cast<BinaryOperator>(S)->getLHS();
968             firstCharOnly = true;
969             continue;
970         }
971 
972         break;
973       }
974 
975       if (S != Original)
976         L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
977     }
978 
979     if (firstCharOnly)
980       L  = PathDiagnosticLocation::createSingleLocation(L);
981 
982     return L;
983   }
984 
985   void popLocation() {
986     if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
987       // For contexts, we only one the first character as the range.
988       rawAddEdge(cleanUpLocation(CLocs.back(), true));
989     }
990     CLocs.pop_back();
991   }
992 
993 public:
994   EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
995     : PD(pd), PDB(pdb) {
996 
997       // If the PathDiagnostic already has pieces, add the enclosing statement
998       // of the first piece as a context as well.
999       if (!PD.path.empty()) {
1000         PrevLoc = (*PD.path.begin())->getLocation();
1001 
1002         if (const Stmt *S = PrevLoc.asStmt())
1003           addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1004       }
1005   }
1006 
1007   ~EdgeBuilder() {
1008     while (!CLocs.empty()) popLocation();
1009 
1010     // Finally, add an initial edge from the start location of the first
1011     // statement (if it doesn't already exist).
1012     PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
1013                                                        PDB.LC,
1014                                                        PDB.getSourceManager());
1015     if (L.isValid())
1016       rawAddEdge(L);
1017   }
1018 
1019   void flushLocations() {
1020     while (!CLocs.empty())
1021       popLocation();
1022     PrevLoc = PathDiagnosticLocation();
1023   }
1024 
1025   void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
1026 
1027   void rawAddEdge(PathDiagnosticLocation NewLoc);
1028 
1029   void addContext(const Stmt *S);
1030   void addContext(const PathDiagnosticLocation &L);
1031   void addExtendedContext(const Stmt *S);
1032 };
1033 } // end anonymous namespace
1034 
1035 
1036 PathDiagnosticLocation
1037 EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1038   if (const Stmt *S = L.asStmt()) {
1039     if (IsControlFlowExpr(S))
1040       return L;
1041 
1042     return PDB.getEnclosingStmtLocation(S);
1043   }
1044 
1045   return L;
1046 }
1047 
1048 bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1049                                    const PathDiagnosticLocation &Containee) {
1050 
1051   if (Container == Containee)
1052     return true;
1053 
1054   if (Container.asDecl())
1055     return true;
1056 
1057   if (const Stmt *S = Containee.asStmt())
1058     if (const Stmt *ContainerS = Container.asStmt()) {
1059       while (S) {
1060         if (S == ContainerS)
1061           return true;
1062         S = PDB.getParent(S);
1063       }
1064       return false;
1065     }
1066 
1067   // Less accurate: compare using source ranges.
1068   SourceRange ContainerR = Container.asRange();
1069   SourceRange ContaineeR = Containee.asRange();
1070 
1071   SourceManager &SM = PDB.getSourceManager();
1072   SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1073   SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1074   SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1075   SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
1076 
1077   unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1078   unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1079   unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1080   unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
1081 
1082   assert(ContainerBegLine <= ContainerEndLine);
1083   assert(ContaineeBegLine <= ContaineeEndLine);
1084 
1085   return (ContainerBegLine <= ContaineeBegLine &&
1086           ContainerEndLine >= ContaineeEndLine &&
1087           (ContainerBegLine != ContaineeBegLine ||
1088            SM.getExpansionColumnNumber(ContainerRBeg) <=
1089            SM.getExpansionColumnNumber(ContaineeRBeg)) &&
1090           (ContainerEndLine != ContaineeEndLine ||
1091            SM.getExpansionColumnNumber(ContainerREnd) >=
1092            SM.getExpansionColumnNumber(ContaineeREnd)));
1093 }
1094 
1095 void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1096   if (!PrevLoc.isValid()) {
1097     PrevLoc = NewLoc;
1098     return;
1099   }
1100 
1101   const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1102   const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
1103 
1104   if (PrevLocClean.asLocation().isInvalid()) {
1105     PrevLoc = NewLoc;
1106     return;
1107   }
1108 
1109   if (NewLocClean.asLocation() == PrevLocClean.asLocation())
1110     return;
1111 
1112   // FIXME: Ignore intra-macro edges for now.
1113   if (NewLocClean.asLocation().getExpansionLoc() ==
1114       PrevLocClean.asLocation().getExpansionLoc())
1115     return;
1116 
1117   PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1118   PrevLoc = NewLoc;
1119 }
1120 
1121 void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
1122 
1123   if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1124     return;
1125 
1126   const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1127 
1128   while (!CLocs.empty()) {
1129     ContextLocation &TopContextLoc = CLocs.back();
1130 
1131     // Is the top location context the same as the one for the new location?
1132     if (TopContextLoc == CLoc) {
1133       if (alwaysAdd) {
1134         if (IsConsumedExpr(TopContextLoc) &&
1135             !IsControlFlowExpr(TopContextLoc.asStmt()))
1136             TopContextLoc.markDead();
1137 
1138         rawAddEdge(NewLoc);
1139       }
1140 
1141       return;
1142     }
1143 
1144     if (containsLocation(TopContextLoc, CLoc)) {
1145       if (alwaysAdd) {
1146         rawAddEdge(NewLoc);
1147 
1148         if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
1149           CLocs.push_back(ContextLocation(CLoc, true));
1150           return;
1151         }
1152       }
1153 
1154       CLocs.push_back(CLoc);
1155       return;
1156     }
1157 
1158     // Context does not contain the location.  Flush it.
1159     popLocation();
1160   }
1161 
1162   // If we reach here, there is no enclosing context.  Just add the edge.
1163   rawAddEdge(NewLoc);
1164 }
1165 
1166 bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1167   if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1168     return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
1169 
1170   return false;
1171 }
1172 
1173 void EdgeBuilder::addExtendedContext(const Stmt *S) {
1174   if (!S)
1175     return;
1176 
1177   const Stmt *Parent = PDB.getParent(S);
1178   while (Parent) {
1179     if (isa<CompoundStmt>(Parent))
1180       Parent = PDB.getParent(Parent);
1181     else
1182       break;
1183   }
1184 
1185   if (Parent) {
1186     switch (Parent->getStmtClass()) {
1187       case Stmt::DoStmtClass:
1188       case Stmt::ObjCAtSynchronizedStmtClass:
1189         addContext(Parent);
1190       default:
1191         break;
1192     }
1193   }
1194 
1195   addContext(S);
1196 }
1197 
1198 void EdgeBuilder::addContext(const Stmt *S) {
1199   if (!S)
1200     return;
1201 
1202   PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
1203   addContext(L);
1204 }
1205 
1206 void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
1207   while (!CLocs.empty()) {
1208     const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1209 
1210     // Is the top location context the same as the one for the new location?
1211     if (TopContextLoc == L)
1212       return;
1213 
1214     if (containsLocation(TopContextLoc, L)) {
1215       CLocs.push_back(L);
1216       return;
1217     }
1218 
1219     // Context does not contain the location.  Flush it.
1220     popLocation();
1221   }
1222 
1223   CLocs.push_back(L);
1224 }
1225 
1226 // Cone-of-influence: support the reverse propagation of "interesting" symbols
1227 // and values by tracing interesting calculations backwards through evaluated
1228 // expressions along a path.  This is probably overly complicated, but the idea
1229 // is that if an expression computed an "interesting" value, the child
1230 // expressions are are also likely to be "interesting" as well (which then
1231 // propagates to the values they in turn compute).  This reverse propagation
1232 // is needed to track interesting correlations across function call boundaries,
1233 // where formal arguments bind to actual arguments, etc.  This is also needed
1234 // because the constraint solver sometimes simplifies certain symbolic values
1235 // into constants when appropriate, and this complicates reasoning about
1236 // interesting values.
1237 typedef llvm::DenseSet<const Expr *> InterestingExprs;
1238 
1239 static void reversePropagateIntererstingSymbols(BugReport &R,
1240                                                 InterestingExprs &IE,
1241                                                 const ProgramState *State,
1242                                                 const Expr *Ex,
1243                                                 const LocationContext *LCtx) {
1244   SVal V = State->getSVal(Ex, LCtx);
1245   if (!(R.isInteresting(V) || IE.count(Ex)))
1246     return;
1247 
1248   switch (Ex->getStmtClass()) {
1249     default:
1250       if (!isa<CastExpr>(Ex))
1251         break;
1252       // Fall through.
1253     case Stmt::BinaryOperatorClass:
1254     case Stmt::UnaryOperatorClass: {
1255       for (Stmt::const_child_iterator CI = Ex->child_begin(),
1256             CE = Ex->child_end();
1257             CI != CE; ++CI) {
1258         if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1259           IE.insert(child);
1260           SVal ChildV = State->getSVal(child, LCtx);
1261           R.markInteresting(ChildV);
1262         }
1263         break;
1264       }
1265     }
1266   }
1267 
1268   R.markInteresting(V);
1269 }
1270 
1271 static void reversePropagateInterestingSymbols(BugReport &R,
1272                                                InterestingExprs &IE,
1273                                                const ProgramState *State,
1274                                                const LocationContext *CalleeCtx,
1275                                                const LocationContext *CallerCtx)
1276 {
1277   // FIXME: Handle non-CallExpr-based CallEvents.
1278   const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1279   const Stmt *CallSite = Callee->getCallSite();
1280   if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
1281     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1282       FunctionDecl::param_const_iterator PI = FD->param_begin(),
1283                                          PE = FD->param_end();
1284       CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1285       for (; AI != AE && PI != PE; ++AI, ++PI) {
1286         if (const Expr *ArgE = *AI) {
1287           if (const ParmVarDecl *PD = *PI) {
1288             Loc LV = State->getLValue(PD, CalleeCtx);
1289             if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1290               IE.insert(ArgE);
1291           }
1292         }
1293       }
1294     }
1295   }
1296 }
1297 
1298 /// Return true if the terminator is a loop and the destination is the
1299 /// false branch.
1300 static bool isLoopJumpPastBody(const Stmt *Term, const BlockEdge *BE) {
1301   switch (Term->getStmtClass()) {
1302     case Stmt::ForStmtClass:
1303     case Stmt::WhileStmtClass:
1304       break;
1305     default:
1306       // Note that we intentionally do not include do..while here.
1307       return false;
1308   }
1309 
1310   // Did we take the false branch?
1311   const CFGBlock *Src = BE->getSrc();
1312   assert(Src->succ_size() == 2);
1313   return (*(Src->succ_begin()+1) == BE->getDst());
1314 }
1315 
1316 static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1317                                             PathDiagnosticBuilder &PDB,
1318                                             const ExplodedNode *N,
1319                                       ArrayRef<BugReporterVisitor *> visitors) {
1320   EdgeBuilder EB(PD, PDB);
1321   const SourceManager& SM = PDB.getSourceManager();
1322   StackDiagVector CallStack;
1323   InterestingExprs IE;
1324 
1325   // Record the last "looping back" diagnostic.  This is used
1326   // for determining if we should emit a diagnostic for skipped loops.
1327   std::pair<const Stmt *, PathDiagnosticEventPiece *>
1328     LastLoopDiagnostic((Stmt*)0, (PathDiagnosticEventPiece*)0);
1329 
1330   const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
1331   while (NextNode) {
1332     N = NextNode;
1333     NextNode = GetPredecessorNode(N);
1334     ProgramPoint P = N->getLocation();
1335 
1336     do {
1337       if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1338         if (const Expr *Ex = PS->getStmtAs<Expr>())
1339           reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1340                                               N->getState().getPtr(), Ex,
1341                                               N->getLocationContext());
1342       }
1343 
1344       if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
1345         const Stmt *S = CE->getCalleeContext()->getCallSite();
1346         if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1347             reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1348                                                 N->getState().getPtr(), Ex,
1349                                                 N->getLocationContext());
1350         }
1351 
1352         PathDiagnosticCallPiece *C =
1353           PathDiagnosticCallPiece::construct(N, *CE, SM);
1354         GRBugReporter& BR = PDB.getBugReporter();
1355         BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
1356 
1357         EB.addEdge(C->callReturn, true);
1358         EB.flushLocations();
1359 
1360         PD.getActivePath().push_front(C);
1361         PD.pushActivePath(&C->path);
1362         CallStack.push_back(StackDiagPair(C, N));
1363         break;
1364       }
1365 
1366       // Pop the call hierarchy if we are done walking the contents
1367       // of a function call.
1368       if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
1369         // Add an edge to the start of the function.
1370         const Decl *D = CE->getCalleeContext()->getDecl();
1371         PathDiagnosticLocation pos =
1372           PathDiagnosticLocation::createBegin(D, SM);
1373         EB.addEdge(pos);
1374 
1375         // Flush all locations, and pop the active path.
1376         bool VisitedEntireCall = PD.isWithinCall();
1377         EB.flushLocations();
1378         PD.popActivePath();
1379         PDB.LC = N->getLocationContext();
1380 
1381         // Either we just added a bunch of stuff to the top-level path, or
1382         // we have a previous CallExitEnd.  If the former, it means that the
1383         // path terminated within a function call.  We must then take the
1384         // current contents of the active path and place it within
1385         // a new PathDiagnosticCallPiece.
1386         PathDiagnosticCallPiece *C;
1387         if (VisitedEntireCall) {
1388           C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1389         } else {
1390           const Decl *Caller = CE->getLocationContext()->getDecl();
1391           C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1392           GRBugReporter& BR = PDB.getBugReporter();
1393           BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
1394         }
1395 
1396         C->setCallee(*CE, SM);
1397         EB.addContext(C->getLocation());
1398 
1399         if (!CallStack.empty()) {
1400           assert(CallStack.back().first == C);
1401           CallStack.pop_back();
1402         }
1403         break;
1404       }
1405 
1406       // Note that is important that we update the LocationContext
1407       // after looking at CallExits.  CallExit basically adds an
1408       // edge in the *caller*, so we don't want to update the LocationContext
1409       // too soon.
1410       PDB.LC = N->getLocationContext();
1411 
1412       // Block edges.
1413       if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1414         // Does this represent entering a call?  If so, look at propagating
1415         // interesting symbols across call boundaries.
1416         if (NextNode) {
1417           const LocationContext *CallerCtx = NextNode->getLocationContext();
1418           const LocationContext *CalleeCtx = PDB.LC;
1419           if (CallerCtx != CalleeCtx) {
1420             reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1421                                                N->getState().getPtr(),
1422                                                CalleeCtx, CallerCtx);
1423           }
1424         }
1425 
1426         // Are we jumping to the head of a loop?  Add a special diagnostic.
1427         if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1428           PathDiagnosticLocation L(Loop, SM, PDB.LC);
1429           const CompoundStmt *CS = NULL;
1430 
1431           if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1432             CS = dyn_cast<CompoundStmt>(FS->getBody());
1433           else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1434             CS = dyn_cast<CompoundStmt>(WS->getBody());
1435 
1436           PathDiagnosticEventPiece *p =
1437             new PathDiagnosticEventPiece(L,
1438                                         "Looping back to the head of the loop");
1439           p->setPrunable(true);
1440 
1441           // Record the loop diagnostic for later consultation.  We can
1442           // use this to determine whether or not to emit a "skipped loop"
1443           // event.
1444           LastLoopDiagnostic.first = Loop;
1445           LastLoopDiagnostic.second = p;
1446 
1447           EB.addEdge(p->getLocation(), true);
1448           PD.getActivePath().push_front(p);
1449 
1450           if (CS) {
1451             PathDiagnosticLocation BL =
1452               PathDiagnosticLocation::createEndBrace(CS, SM);
1453             EB.addEdge(BL);
1454           }
1455         }
1456 
1457         if (const Stmt *Term = BE->getSrc()->getTerminator()) {
1458           // Are we jumping past the loop body without ever executing the
1459           // loop (because the condition was false)?
1460           if (isLoopJumpPastBody(Term, BE) &&
1461               !PD.getActivePath().empty() &&
1462               PD.getActivePath().front() != LastLoopDiagnostic.second &&
1463               Term != LastLoopDiagnostic.first)
1464           {
1465             PathDiagnosticLocation L(Term, SM, PDB.LC);
1466             PathDiagnosticEventPiece *PE =
1467             new PathDiagnosticEventPiece(L,
1468                                          "Loop body executed 0 times");
1469             PE->setPrunable(true);
1470             LastLoopDiagnostic.first = 0;
1471             LastLoopDiagnostic.second = 0;
1472 
1473             EB.addEdge(PE->getLocation(), true);
1474             PD.getActivePath().push_front(PE);
1475           }
1476 
1477           EB.addContext(Term);
1478         }
1479 
1480         break;
1481       }
1482 
1483       if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
1484         CFGElement First = BE->getFirstElement();
1485         if (const CFGStmt *S = First.getAs<CFGStmt>()) {
1486           const Stmt *stmt = S->getStmt();
1487           if (IsControlFlowExpr(stmt)) {
1488             // Add the proper context for '&&', '||', and '?'.
1489             EB.addContext(stmt);
1490           }
1491           else
1492             EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
1493         }
1494 
1495         break;
1496       }
1497 
1498 
1499     } while (0);
1500 
1501     if (!NextNode)
1502       continue;
1503 
1504     // Add pieces from custom visitors.
1505     BugReport *R = PDB.getBugReport();
1506     for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1507                                                   E = visitors.end();
1508          I != E; ++I) {
1509       if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
1510         const PathDiagnosticLocation &Loc = p->getLocation();
1511         EB.addEdge(Loc, true);
1512         PD.getActivePath().push_front(p);
1513         updateStackPiecesWithMessage(p, CallStack);
1514 
1515         if (const Stmt *S = Loc.asStmt())
1516           EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1517       }
1518     }
1519   }
1520 
1521   return PDB.getBugReport()->isValid();
1522 }
1523 
1524 //===----------------------------------------------------------------------===//
1525 // Methods for BugType and subclasses.
1526 //===----------------------------------------------------------------------===//
1527 BugType::~BugType() { }
1528 
1529 void BugType::FlushReports(BugReporter &BR) {}
1530 
1531 void BuiltinBug::anchor() {}
1532 
1533 //===----------------------------------------------------------------------===//
1534 // Methods for BugReport and subclasses.
1535 //===----------------------------------------------------------------------===//
1536 
1537 void BugReport::NodeResolver::anchor() {}
1538 
1539 void BugReport::addVisitor(BugReporterVisitor* visitor) {
1540   if (!visitor)
1541     return;
1542 
1543   llvm::FoldingSetNodeID ID;
1544   visitor->Profile(ID);
1545   void *InsertPos;
1546 
1547   if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1548     delete visitor;
1549     return;
1550   }
1551 
1552   CallbacksSet.InsertNode(visitor, InsertPos);
1553   Callbacks.push_back(visitor);
1554   ++ConfigurationChangeToken;
1555 }
1556 
1557 BugReport::~BugReport() {
1558   for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
1559     delete *I;
1560   }
1561   while (!interestingSymbols.empty()) {
1562     popInterestingSymbolsAndRegions();
1563   }
1564 }
1565 
1566 const Decl *BugReport::getDeclWithIssue() const {
1567   if (DeclWithIssue)
1568     return DeclWithIssue;
1569 
1570   const ExplodedNode *N = getErrorNode();
1571   if (!N)
1572     return 0;
1573 
1574   const LocationContext *LC = N->getLocationContext();
1575   return LC->getCurrentStackFrame()->getDecl();
1576 }
1577 
1578 void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1579   hash.AddPointer(&BT);
1580   hash.AddString(Description);
1581   PathDiagnosticLocation UL = getUniqueingLocation();
1582   if (UL.isValid()) {
1583     UL.Profile(hash);
1584   } else if (Location.isValid()) {
1585     Location.Profile(hash);
1586   } else {
1587     assert(ErrorNode);
1588     hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1589   }
1590 
1591   for (SmallVectorImpl<SourceRange>::const_iterator I =
1592       Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1593     const SourceRange range = *I;
1594     if (!range.isValid())
1595       continue;
1596     hash.AddInteger(range.getBegin().getRawEncoding());
1597     hash.AddInteger(range.getEnd().getRawEncoding());
1598   }
1599 }
1600 
1601 void BugReport::markInteresting(SymbolRef sym) {
1602   if (!sym)
1603     return;
1604 
1605   // If the symbol wasn't already in our set, note a configuration change.
1606   if (getInterestingSymbols().insert(sym).second)
1607     ++ConfigurationChangeToken;
1608 
1609   if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
1610     getInterestingRegions().insert(meta->getRegion());
1611 }
1612 
1613 void BugReport::markInteresting(const MemRegion *R) {
1614   if (!R)
1615     return;
1616 
1617   // If the base region wasn't already in our set, note a configuration change.
1618   R = R->getBaseRegion();
1619   if (getInterestingRegions().insert(R).second)
1620     ++ConfigurationChangeToken;
1621 
1622   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1623     getInterestingSymbols().insert(SR->getSymbol());
1624 }
1625 
1626 void BugReport::markInteresting(SVal V) {
1627   markInteresting(V.getAsRegion());
1628   markInteresting(V.getAsSymbol());
1629 }
1630 
1631 void BugReport::markInteresting(const LocationContext *LC) {
1632   if (!LC)
1633     return;
1634   InterestingLocationContexts.insert(LC);
1635 }
1636 
1637 bool BugReport::isInteresting(SVal V) {
1638   return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1639 }
1640 
1641 bool BugReport::isInteresting(SymbolRef sym) {
1642   if (!sym)
1643     return false;
1644   // We don't currently consider metadata symbols to be interesting
1645   // even if we know their region is interesting. Is that correct behavior?
1646   return getInterestingSymbols().count(sym);
1647 }
1648 
1649 bool BugReport::isInteresting(const MemRegion *R) {
1650   if (!R)
1651     return false;
1652   R = R->getBaseRegion();
1653   bool b = getInterestingRegions().count(R);
1654   if (b)
1655     return true;
1656   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1657     return getInterestingSymbols().count(SR->getSymbol());
1658   return false;
1659 }
1660 
1661 bool BugReport::isInteresting(const LocationContext *LC) {
1662   if (!LC)
1663     return false;
1664   return InterestingLocationContexts.count(LC);
1665 }
1666 
1667 void BugReport::lazyInitializeInterestingSets() {
1668   if (interestingSymbols.empty()) {
1669     interestingSymbols.push_back(new Symbols());
1670     interestingRegions.push_back(new Regions());
1671   }
1672 }
1673 
1674 BugReport::Symbols &BugReport::getInterestingSymbols() {
1675   lazyInitializeInterestingSets();
1676   return *interestingSymbols.back();
1677 }
1678 
1679 BugReport::Regions &BugReport::getInterestingRegions() {
1680   lazyInitializeInterestingSets();
1681   return *interestingRegions.back();
1682 }
1683 
1684 void BugReport::pushInterestingSymbolsAndRegions() {
1685   interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1686   interestingRegions.push_back(new Regions(getInterestingRegions()));
1687 }
1688 
1689 void BugReport::popInterestingSymbolsAndRegions() {
1690   delete interestingSymbols.back();
1691   interestingSymbols.pop_back();
1692   delete interestingRegions.back();
1693   interestingRegions.pop_back();
1694 }
1695 
1696 const Stmt *BugReport::getStmt() const {
1697   if (!ErrorNode)
1698     return 0;
1699 
1700   ProgramPoint ProgP = ErrorNode->getLocation();
1701   const Stmt *S = NULL;
1702 
1703   if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
1704     CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
1705     if (BE->getBlock() == &Exit)
1706       S = GetPreviousStmt(ErrorNode);
1707   }
1708   if (!S)
1709     S = GetStmt(ProgP);
1710 
1711   return S;
1712 }
1713 
1714 std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1715 BugReport::getRanges() {
1716     // If no custom ranges, add the range of the statement corresponding to
1717     // the error node.
1718     if (Ranges.empty()) {
1719       if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1720         addRange(E->getSourceRange());
1721       else
1722         return std::make_pair(ranges_iterator(), ranges_iterator());
1723     }
1724 
1725     // User-specified absence of range info.
1726     if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1727       return std::make_pair(ranges_iterator(), ranges_iterator());
1728 
1729     return std::make_pair(Ranges.begin(), Ranges.end());
1730 }
1731 
1732 PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
1733   if (ErrorNode) {
1734     assert(!Location.isValid() &&
1735      "Either Location or ErrorNode should be specified but not both.");
1736 
1737     if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
1738       const LocationContext *LC = ErrorNode->getLocationContext();
1739 
1740       // For member expressions, return the location of the '.' or '->'.
1741       if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
1742         return PathDiagnosticLocation::createMemberLoc(ME, SM);
1743       // For binary operators, return the location of the operator.
1744       if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1745         return PathDiagnosticLocation::createOperatorLoc(B, SM);
1746 
1747       if (isa<PostStmtPurgeDeadSymbols>(ErrorNode->getLocation()))
1748         return PathDiagnosticLocation::createEnd(S, SM, LC);
1749 
1750       return PathDiagnosticLocation::createBegin(S, SM, LC);
1751     }
1752   } else {
1753     assert(Location.isValid());
1754     return Location;
1755   }
1756 
1757   return PathDiagnosticLocation();
1758 }
1759 
1760 //===----------------------------------------------------------------------===//
1761 // Methods for BugReporter and subclasses.
1762 //===----------------------------------------------------------------------===//
1763 
1764 BugReportEquivClass::~BugReportEquivClass() { }
1765 GRBugReporter::~GRBugReporter() { }
1766 BugReporterData::~BugReporterData() {}
1767 
1768 ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
1769 
1770 ProgramStateManager&
1771 GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1772 
1773 BugReporter::~BugReporter() {
1774   FlushReports();
1775 
1776   // Free the bug reports we are tracking.
1777   typedef std::vector<BugReportEquivClass *> ContTy;
1778   for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1779        I != E; ++I) {
1780     delete *I;
1781   }
1782 }
1783 
1784 void BugReporter::FlushReports() {
1785   if (BugTypes.isEmpty())
1786     return;
1787 
1788   // First flush the warnings for each BugType.  This may end up creating new
1789   // warnings and new BugTypes.
1790   // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1791   // Turn NSErrorChecker into a proper checker and remove this.
1792   SmallVector<const BugType*, 16> bugTypes;
1793   for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1794     bugTypes.push_back(*I);
1795   for (SmallVector<const BugType*, 16>::iterator
1796          I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
1797     const_cast<BugType*>(*I)->FlushReports(*this);
1798 
1799   // We need to flush reports in deterministic order to ensure the order
1800   // of the reports is consistent between runs.
1801   typedef std::vector<BugReportEquivClass *> ContVecTy;
1802   for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1803        EI != EE; ++EI){
1804     BugReportEquivClass& EQ = **EI;
1805     FlushReport(EQ);
1806   }
1807 
1808   // BugReporter owns and deletes only BugTypes created implicitly through
1809   // EmitBasicReport.
1810   // FIXME: There are leaks from checkers that assume that the BugTypes they
1811   // create will be destroyed by the BugReporter.
1812   for (llvm::StringMap<BugType*>::iterator
1813          I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1814     delete I->second;
1815 
1816   // Remove all references to the BugType objects.
1817   BugTypes = F.getEmptySet();
1818 }
1819 
1820 //===----------------------------------------------------------------------===//
1821 // PathDiagnostics generation.
1822 //===----------------------------------------------------------------------===//
1823 
1824 static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
1825                  std::pair<ExplodedNode*, unsigned> >
1826 MakeReportGraph(const ExplodedGraph* G,
1827                 SmallVectorImpl<const ExplodedNode*> &nodes) {
1828 
1829   // Create the trimmed graph.  It will contain the shortest paths from the
1830   // error nodes to the root.  In the new graph we should only have one
1831   // error node unless there are two or more error nodes with the same minimum
1832   // path length.
1833   ExplodedGraph* GTrim;
1834   InterExplodedGraphMap* NMap;
1835 
1836   llvm::DenseMap<const void*, const void*> InverseMap;
1837   llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1838                                    &InverseMap);
1839 
1840   // Create owning pointers for GTrim and NMap just to ensure that they are
1841   // released when this function exists.
1842   OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1843   OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
1844 
1845   // Find the (first) error node in the trimmed graph.  We just need to consult
1846   // the node map (NMap) which maps from nodes in the original graph to nodes
1847   // in the new graph.
1848 
1849   std::queue<const ExplodedNode*> WS;
1850   typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
1851   IndexMapTy IndexMap;
1852 
1853   for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1854     const ExplodedNode *originalNode = nodes[nodeIndex];
1855     if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
1856       WS.push(N);
1857       IndexMap[originalNode] = nodeIndex;
1858     }
1859   }
1860 
1861   assert(!WS.empty() && "No error node found in the trimmed graph.");
1862 
1863   // Create a new (third!) graph with a single path.  This is the graph
1864   // that will be returned to the caller.
1865   ExplodedGraph *GNew = new ExplodedGraph();
1866 
1867   // Sometimes the trimmed graph can contain a cycle.  Perform a reverse BFS
1868   // to the root node, and then construct a new graph that contains only
1869   // a single path.
1870   llvm::DenseMap<const void*,unsigned> Visited;
1871 
1872   unsigned cnt = 0;
1873   const ExplodedNode *Root = 0;
1874 
1875   while (!WS.empty()) {
1876     const ExplodedNode *Node = WS.front();
1877     WS.pop();
1878 
1879     if (Visited.find(Node) != Visited.end())
1880       continue;
1881 
1882     Visited[Node] = cnt++;
1883 
1884     if (Node->pred_empty()) {
1885       Root = Node;
1886       break;
1887     }
1888 
1889     for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
1890          E=Node->pred_end(); I!=E; ++I)
1891       WS.push(*I);
1892   }
1893 
1894   assert(Root);
1895 
1896   // Now walk from the root down the BFS path, always taking the successor
1897   // with the lowest number.
1898   ExplodedNode *Last = 0, *First = 0;
1899   NodeBackMap *BM = new NodeBackMap();
1900   unsigned NodeIndex = 0;
1901 
1902   for ( const ExplodedNode *N = Root ;;) {
1903     // Lookup the number associated with the current node.
1904     llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
1905     assert(I != Visited.end());
1906 
1907     // Create the equivalent node in the new graph with the same state
1908     // and location.
1909     ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
1910 
1911     // Store the mapping to the original node.
1912     llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1913     assert(IMitr != InverseMap.end() && "No mapping to original node.");
1914     (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
1915 
1916     // Link up the new node with the previous node.
1917     if (Last)
1918       NewN->addPredecessor(Last, *GNew);
1919 
1920     Last = NewN;
1921 
1922     // Are we at the final node?
1923     IndexMapTy::iterator IMI =
1924       IndexMap.find((const ExplodedNode*)(IMitr->second));
1925     if (IMI != IndexMap.end()) {
1926       First = NewN;
1927       NodeIndex = IMI->second;
1928       break;
1929     }
1930 
1931     // Find the next successor node.  We choose the node that is marked
1932     // with the lowest DFS number.
1933     ExplodedNode::const_succ_iterator SI = N->succ_begin();
1934     ExplodedNode::const_succ_iterator SE = N->succ_end();
1935     N = 0;
1936 
1937     for (unsigned MinVal = 0; SI != SE; ++SI) {
1938 
1939       I = Visited.find(*SI);
1940 
1941       if (I == Visited.end())
1942         continue;
1943 
1944       if (!N || I->second < MinVal) {
1945         N = *SI;
1946         MinVal = I->second;
1947       }
1948     }
1949 
1950     assert(N);
1951   }
1952 
1953   assert(First);
1954 
1955   return std::make_pair(std::make_pair(GNew, BM),
1956                         std::make_pair(First, NodeIndex));
1957 }
1958 
1959 /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1960 ///  and collapses PathDiagosticPieces that are expanded by macros.
1961 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
1962   typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1963                                 SourceLocation> > MacroStackTy;
1964 
1965   typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
1966           PiecesTy;
1967 
1968   MacroStackTy MacroStack;
1969   PiecesTy Pieces;
1970 
1971   for (PathPieces::const_iterator I = path.begin(), E = path.end();
1972        I!=E; ++I) {
1973 
1974     PathDiagnosticPiece *piece = I->getPtr();
1975 
1976     // Recursively compact calls.
1977     if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1978       CompactPathDiagnostic(call->path, SM);
1979     }
1980 
1981     // Get the location of the PathDiagnosticPiece.
1982     const FullSourceLoc Loc = piece->getLocation().asLocation();
1983 
1984     // Determine the instantiation location, which is the location we group
1985     // related PathDiagnosticPieces.
1986     SourceLocation InstantiationLoc = Loc.isMacroID() ?
1987                                       SM.getExpansionLoc(Loc) :
1988                                       SourceLocation();
1989 
1990     if (Loc.isFileID()) {
1991       MacroStack.clear();
1992       Pieces.push_back(piece);
1993       continue;
1994     }
1995 
1996     assert(Loc.isMacroID());
1997 
1998     // Is the PathDiagnosticPiece within the same macro group?
1999     if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
2000       MacroStack.back().first->subPieces.push_back(piece);
2001       continue;
2002     }
2003 
2004     // We aren't in the same group.  Are we descending into a new macro
2005     // or are part of an old one?
2006     IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
2007 
2008     SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2009                                           SM.getExpansionLoc(Loc) :
2010                                           SourceLocation();
2011 
2012     // Walk the entire macro stack.
2013     while (!MacroStack.empty()) {
2014       if (InstantiationLoc == MacroStack.back().second) {
2015         MacroGroup = MacroStack.back().first;
2016         break;
2017       }
2018 
2019       if (ParentInstantiationLoc == MacroStack.back().second) {
2020         MacroGroup = MacroStack.back().first;
2021         break;
2022       }
2023 
2024       MacroStack.pop_back();
2025     }
2026 
2027     if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
2028       // Create a new macro group and add it to the stack.
2029       PathDiagnosticMacroPiece *NewGroup =
2030         new PathDiagnosticMacroPiece(
2031           PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
2032 
2033       if (MacroGroup)
2034         MacroGroup->subPieces.push_back(NewGroup);
2035       else {
2036         assert(InstantiationLoc.isFileID());
2037         Pieces.push_back(NewGroup);
2038       }
2039 
2040       MacroGroup = NewGroup;
2041       MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
2042     }
2043 
2044     // Finally, add the PathDiagnosticPiece to the group.
2045     MacroGroup->subPieces.push_back(piece);
2046   }
2047 
2048   // Now take the pieces and construct a new PathDiagnostic.
2049   path.clear();
2050 
2051   for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
2052     path.push_back(*I);
2053 }
2054 
2055 bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
2056                                            PathDiagnosticConsumer &PC,
2057                                            ArrayRef<BugReport *> &bugReports) {
2058   assert(!bugReports.empty());
2059 
2060   bool HasValid = false;
2061   SmallVector<const ExplodedNode *, 10> errorNodes;
2062   for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
2063                                       E = bugReports.end(); I != E; ++I) {
2064     if ((*I)->isValid()) {
2065       HasValid = true;
2066       errorNodes.push_back((*I)->getErrorNode());
2067     } else {
2068       errorNodes.push_back(0);
2069     }
2070   }
2071 
2072   // If all the reports have been marked invalid, we're done.
2073   if (!HasValid)
2074     return false;
2075 
2076   // Construct a new graph that contains only a single path from the error
2077   // node to a root.
2078   const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
2079   std::pair<ExplodedNode*, unsigned> >&
2080     GPair = MakeReportGraph(&getGraph(), errorNodes);
2081 
2082   // Find the BugReport with the original location.
2083   assert(GPair.second.second < bugReports.size());
2084   BugReport *R = bugReports[GPair.second.second];
2085   assert(R && "No original report found for sliced graph.");
2086   assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
2087 
2088   OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
2089   OwningPtr<NodeBackMap> BackMap(GPair.first.second);
2090   const ExplodedNode *N = GPair.second.first;
2091 
2092   // Start building the path diagnostic...
2093   PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
2094 
2095   // Register additional node visitors.
2096   R->addVisitor(new NilReceiverBRVisitor());
2097   R->addVisitor(new ConditionBRVisitor());
2098   R->addVisitor(new LikelyFalsePositiveSuppressionBRVisitor());
2099 
2100   BugReport::VisitorList visitors;
2101   unsigned originalReportConfigToken, finalReportConfigToken;
2102 
2103   // While generating diagnostics, it's possible the visitors will decide
2104   // new symbols and regions are interesting, or add other visitors based on
2105   // the information they find. If they do, we need to regenerate the path
2106   // based on our new report configuration.
2107   do {
2108     // Get a clean copy of all the visitors.
2109     for (BugReport::visitor_iterator I = R->visitor_begin(),
2110                                      E = R->visitor_end(); I != E; ++I)
2111        visitors.push_back((*I)->clone());
2112 
2113     // Clear out the active path from any previous work.
2114     PD.resetPath();
2115     originalReportConfigToken = R->getConfigurationChangeToken();
2116 
2117     // Generate the very last diagnostic piece - the piece is visible before
2118     // the trace is expanded.
2119     PathDiagnosticPiece *LastPiece = 0;
2120     for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
2121         I != E; ++I) {
2122       if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
2123         assert (!LastPiece &&
2124             "There can only be one final piece in a diagnostic.");
2125         LastPiece = Piece;
2126       }
2127     }
2128 
2129     if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
2130       if (!LastPiece)
2131         LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
2132       if (LastPiece)
2133         PD.setEndOfPath(LastPiece);
2134       else
2135         return false;
2136     }
2137 
2138     switch (PDB.getGenerationScheme()) {
2139     case PathDiagnosticConsumer::Extensive:
2140       if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
2141         assert(!R->isValid() && "Failed on valid report");
2142         // Try again. We'll filter out the bad report when we trim the graph.
2143         // FIXME: It would be more efficient to use the same intermediate
2144         // trimmed graph, and just repeat the shortest-path search.
2145         return generatePathDiagnostic(PD, PC, bugReports);
2146       }
2147       break;
2148     case PathDiagnosticConsumer::Minimal:
2149       if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
2150         assert(!R->isValid() && "Failed on valid report");
2151         // Try again. We'll filter out the bad report when we trim the graph.
2152         return generatePathDiagnostic(PD, PC, bugReports);
2153       }
2154       break;
2155     case PathDiagnosticConsumer::None:
2156       if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2157         assert(!R->isValid() && "Failed on valid report");
2158         // Try again. We'll filter out the bad report when we trim the graph.
2159         return generatePathDiagnostic(PD, PC, bugReports);
2160       }
2161       break;
2162     }
2163 
2164     // Clean up the visitors we used.
2165     llvm::DeleteContainerPointers(visitors);
2166 
2167     // Did anything change while generating this path?
2168     finalReportConfigToken = R->getConfigurationChangeToken();
2169   } while(finalReportConfigToken != originalReportConfigToken);
2170 
2171   // Finally, prune the diagnostic path of uninteresting stuff.
2172   if (!PD.path.empty()) {
2173     // Remove messages that are basically the same.
2174     removeRedundantMsgs(PD.getMutablePieces());
2175 
2176     if (R->shouldPrunePath() &&
2177         getEngine().getAnalysisManager().options.shouldPrunePaths()) {
2178       bool hasSomethingInteresting = RemoveUnneededCalls(PD.getMutablePieces(),
2179                                                          R);
2180       assert(hasSomethingInteresting);
2181       (void) hasSomethingInteresting;
2182     }
2183 
2184     adjustCallLocations(PD.getMutablePieces());
2185   }
2186 
2187   return true;
2188 }
2189 
2190 void BugReporter::Register(BugType *BT) {
2191   BugTypes = F.add(BugTypes, BT);
2192 }
2193 
2194 void BugReporter::emitReport(BugReport* R) {
2195   // Compute the bug report's hash to determine its equivalence class.
2196   llvm::FoldingSetNodeID ID;
2197   R->Profile(ID);
2198 
2199   // Lookup the equivance class.  If there isn't one, create it.
2200   BugType& BT = R->getBugType();
2201   Register(&BT);
2202   void *InsertPos;
2203   BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
2204 
2205   if (!EQ) {
2206     EQ = new BugReportEquivClass(R);
2207     EQClasses.InsertNode(EQ, InsertPos);
2208     EQClassesVector.push_back(EQ);
2209   }
2210   else
2211     EQ->AddReport(R);
2212 }
2213 
2214 
2215 //===----------------------------------------------------------------------===//
2216 // Emitting reports in equivalence classes.
2217 //===----------------------------------------------------------------------===//
2218 
2219 namespace {
2220 struct FRIEC_WLItem {
2221   const ExplodedNode *N;
2222   ExplodedNode::const_succ_iterator I, E;
2223 
2224   FRIEC_WLItem(const ExplodedNode *n)
2225   : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2226 };
2227 }
2228 
2229 static BugReport *
2230 FindReportInEquivalenceClass(BugReportEquivClass& EQ,
2231                              SmallVectorImpl<BugReport*> &bugReports) {
2232 
2233   BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2234   assert(I != E);
2235   BugType& BT = I->getBugType();
2236 
2237   // If we don't need to suppress any of the nodes because they are
2238   // post-dominated by a sink, simply add all the nodes in the equivalence class
2239   // to 'Nodes'.  Any of the reports will serve as a "representative" report.
2240   if (!BT.isSuppressOnSink()) {
2241     BugReport *R = I;
2242     for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
2243       const ExplodedNode *N = I->getErrorNode();
2244       if (N) {
2245         R = I;
2246         bugReports.push_back(R);
2247       }
2248     }
2249     return R;
2250   }
2251 
2252   // For bug reports that should be suppressed when all paths are post-dominated
2253   // by a sink node, iterate through the reports in the equivalence class
2254   // until we find one that isn't post-dominated (if one exists).  We use a
2255   // DFS traversal of the ExplodedGraph to find a non-sink node.  We could write
2256   // this as a recursive function, but we don't want to risk blowing out the
2257   // stack for very long paths.
2258   BugReport *exampleReport = 0;
2259 
2260   for (; I != E; ++I) {
2261     const ExplodedNode *errorNode = I->getErrorNode();
2262 
2263     if (!errorNode)
2264       continue;
2265     if (errorNode->isSink()) {
2266       llvm_unreachable(
2267            "BugType::isSuppressSink() should not be 'true' for sink end nodes");
2268     }
2269     // No successors?  By definition this nodes isn't post-dominated by a sink.
2270     if (errorNode->succ_empty()) {
2271       bugReports.push_back(I);
2272       if (!exampleReport)
2273         exampleReport = I;
2274       continue;
2275     }
2276 
2277     // At this point we know that 'N' is not a sink and it has at least one
2278     // successor.  Use a DFS worklist to find a non-sink end-of-path node.
2279     typedef FRIEC_WLItem WLItem;
2280     typedef SmallVector<WLItem, 10> DFSWorkList;
2281     llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2282 
2283     DFSWorkList WL;
2284     WL.push_back(errorNode);
2285     Visited[errorNode] = 1;
2286 
2287     while (!WL.empty()) {
2288       WLItem &WI = WL.back();
2289       assert(!WI.N->succ_empty());
2290 
2291       for (; WI.I != WI.E; ++WI.I) {
2292         const ExplodedNode *Succ = *WI.I;
2293         // End-of-path node?
2294         if (Succ->succ_empty()) {
2295           // If we found an end-of-path node that is not a sink.
2296           if (!Succ->isSink()) {
2297             bugReports.push_back(I);
2298             if (!exampleReport)
2299               exampleReport = I;
2300             WL.clear();
2301             break;
2302           }
2303           // Found a sink?  Continue on to the next successor.
2304           continue;
2305         }
2306         // Mark the successor as visited.  If it hasn't been explored,
2307         // enqueue it to the DFS worklist.
2308         unsigned &mark = Visited[Succ];
2309         if (!mark) {
2310           mark = 1;
2311           WL.push_back(Succ);
2312           break;
2313         }
2314       }
2315 
2316       // The worklist may have been cleared at this point.  First
2317       // check if it is empty before checking the last item.
2318       if (!WL.empty() && &WL.back() == &WI)
2319         WL.pop_back();
2320     }
2321   }
2322 
2323   // ExampleReport will be NULL if all the nodes in the equivalence class
2324   // were post-dominated by sinks.
2325   return exampleReport;
2326 }
2327 
2328 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
2329   SmallVector<BugReport*, 10> bugReports;
2330   BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
2331   if (exampleReport) {
2332     const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2333     for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2334                                                  E=C.end(); I != E; ++I) {
2335       FlushReport(exampleReport, **I, bugReports);
2336     }
2337   }
2338 }
2339 
2340 void BugReporter::FlushReport(BugReport *exampleReport,
2341                               PathDiagnosticConsumer &PD,
2342                               ArrayRef<BugReport*> bugReports) {
2343 
2344   // FIXME: Make sure we use the 'R' for the path that was actually used.
2345   // Probably doesn't make a difference in practice.
2346   BugType& BT = exampleReport->getBugType();
2347 
2348   OwningPtr<PathDiagnostic>
2349     D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2350                          exampleReport->getBugType().getName(),
2351                          exampleReport->getDescription(),
2352                          exampleReport->getShortDescription(/*Fallback=*/false),
2353                          BT.getCategory(),
2354                          exampleReport->getUniqueingLocation(),
2355                          exampleReport->getUniqueingDecl()));
2356 
2357   // Generate the full path diagnostic, using the generation scheme
2358   // specified by the PathDiagnosticConsumer. Note that we have to generate
2359   // path diagnostics even for consumers which do not support paths, because
2360   // the BugReporterVisitors may mark this bug as a false positive.
2361   if (!bugReports.empty())
2362     if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2363       return;
2364 
2365   // If the path is empty, generate a single step path with the location
2366   // of the issue.
2367   if (D->path.empty()) {
2368     PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2369     PathDiagnosticPiece *piece =
2370       new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2371     BugReport::ranges_iterator Beg, End;
2372     llvm::tie(Beg, End) = exampleReport->getRanges();
2373     for ( ; Beg != End; ++Beg)
2374       piece->addRange(*Beg);
2375     D->setEndOfPath(piece);
2376   }
2377 
2378   // Get the meta data.
2379   const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
2380   for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2381                                                 e = Meta.end(); i != e; ++i) {
2382     D->addMeta(*i);
2383   }
2384 
2385   PD.HandlePathDiagnostic(D.take());
2386 }
2387 
2388 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
2389                                   StringRef name,
2390                                   StringRef category,
2391                                   StringRef str, PathDiagnosticLocation Loc,
2392                                   SourceRange* RBeg, unsigned NumRanges) {
2393 
2394   // 'BT' is owned by BugReporter.
2395   BugType *BT = getBugTypeForName(name, category);
2396   BugReport *R = new BugReport(*BT, str, Loc);
2397   R->setDeclWithIssue(DeclWithIssue);
2398   for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2399   emitReport(R);
2400 }
2401 
2402 BugType *BugReporter::getBugTypeForName(StringRef name,
2403                                         StringRef category) {
2404   SmallString<136> fullDesc;
2405   llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2406   llvm::StringMapEntry<BugType *> &
2407       entry = StrBugTypes.GetOrCreateValue(fullDesc);
2408   BugType *BT = entry.getValue();
2409   if (!BT) {
2410     BT = new BugType(name, category);
2411     entry.setValue(BT);
2412   }
2413   return BT;
2414 }
2415