BugReporter.cpp revision 0cd59482abd8aec9ed1eaad11f5fe9c1e42639f6
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/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/Analysis/CFG.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ParentMap.h"
22#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
24#include "clang/Analysis/ProgramPoint.h"
25#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/OwningPtr.h"
30#include <queue>
31
32using namespace clang;
33using namespace ento;
34
35BugReporterVisitor::~BugReporterVisitor() {}
36
37//===----------------------------------------------------------------------===//
38// Helper routines for walking the ExplodedGraph and fetching statements.
39//===----------------------------------------------------------------------===//
40
41static inline const Stmt *GetStmt(const ProgramPoint &P) {
42  if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
43    return SP->getStmt();
44  else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
45    return BE->getSrc()->getTerminator();
46
47  return 0;
48}
49
50static inline const ExplodedNode*
51GetPredecessorNode(const ExplodedNode *N) {
52  return N->pred_empty() ? NULL : *(N->pred_begin());
53}
54
55static inline const ExplodedNode*
56GetSuccessorNode(const ExplodedNode *N) {
57  return N->succ_empty() ? NULL : *(N->succ_begin());
58}
59
60static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
61  for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
62    if (const Stmt *S = GetStmt(N->getLocation()))
63      return S;
64
65  return 0;
66}
67
68static const Stmt *GetNextStmt(const ExplodedNode *N) {
69  for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
70    if (const Stmt *S = GetStmt(N->getLocation())) {
71      // Check if the statement is '?' or '&&'/'||'.  These are "merges",
72      // not actual statement points.
73      switch (S->getStmtClass()) {
74        case Stmt::ChooseExprClass:
75        case Stmt::BinaryConditionalOperatorClass: continue;
76        case Stmt::ConditionalOperatorClass: continue;
77        case Stmt::BinaryOperatorClass: {
78          BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
79          if (Op == BO_LAnd || Op == BO_LOr)
80            continue;
81          break;
82        }
83        default:
84          break;
85      }
86      return S;
87    }
88
89  return 0;
90}
91
92static inline const Stmt*
93GetCurrentOrPreviousStmt(const ExplodedNode *N) {
94  if (const Stmt *S = GetStmt(N->getLocation()))
95    return S;
96
97  return GetPreviousStmt(N);
98}
99
100static inline const Stmt*
101GetCurrentOrNextStmt(const ExplodedNode *N) {
102  if (const Stmt *S = GetStmt(N->getLocation()))
103    return S;
104
105  return GetNextStmt(N);
106}
107
108//===----------------------------------------------------------------------===//
109// PathDiagnosticBuilder and its associated routines and helper objects.
110//===----------------------------------------------------------------------===//
111
112typedef llvm::DenseMap<const ExplodedNode*,
113const ExplodedNode*> NodeBackMap;
114
115namespace {
116class NodeMapClosure : public BugReport::NodeResolver {
117  NodeBackMap& M;
118public:
119  NodeMapClosure(NodeBackMap *m) : M(*m) {}
120  ~NodeMapClosure() {}
121
122  const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
123    NodeBackMap::iterator I = M.find(N);
124    return I == M.end() ? 0 : I->second;
125  }
126};
127
128class PathDiagnosticBuilder : public BugReporterContext {
129  BugReport *R;
130  PathDiagnosticClient *PDC;
131  llvm::OwningPtr<ParentMap> PM;
132  NodeMapClosure NMC;
133public:
134  PathDiagnosticBuilder(GRBugReporter &br,
135                        BugReport *r, NodeBackMap *Backmap,
136                        PathDiagnosticClient *pdc)
137    : BugReporterContext(br),
138      R(r), PDC(pdc), NMC(Backmap) {}
139
140  PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
141
142  PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
143                                            const ExplodedNode *N);
144
145  BugReport *getBugReport() { return R; }
146
147  Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
148
149  const LocationContext* getLocationContext() {
150    return R->getErrorNode()->getLocationContext();
151  }
152
153  ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
154
155  const Stmt *getParent(const Stmt *S) {
156    return getParentMap().getParent(S);
157  }
158
159  virtual NodeMapClosure& getNodeResolver() { return NMC; }
160
161  PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
162
163  PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
164    return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
165  }
166
167  bool supportsLogicalOpControlFlow() const {
168    return PDC ? PDC->supportsLogicalOpControlFlow() : true;
169  }
170};
171} // end anonymous namespace
172
173PathDiagnosticLocation
174PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
175  if (const Stmt *S = GetNextStmt(N))
176    return PathDiagnosticLocation(S, getSourceManager(), getLocationContext());
177
178  return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
179                                               getSourceManager());
180}
181
182PathDiagnosticLocation
183PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
184                                          const ExplodedNode *N) {
185
186  // Slow, but probably doesn't matter.
187  if (os.str().empty())
188    os << ' ';
189
190  const PathDiagnosticLocation &Loc = ExecutionContinues(N);
191
192  if (Loc.asStmt())
193    os << "Execution continues on line "
194       << getSourceManager().getExpansionLineNumber(Loc.asLocation())
195       << '.';
196  else {
197    os << "Execution jumps to the end of the ";
198    const Decl *D = N->getLocationContext()->getDecl();
199    if (isa<ObjCMethodDecl>(D))
200      os << "method";
201    else if (isa<FunctionDecl>(D))
202      os << "function";
203    else {
204      assert(isa<BlockDecl>(D));
205      os << "anonymous block";
206    }
207    os << '.';
208  }
209
210  return Loc;
211}
212
213static bool IsNested(const Stmt *S, ParentMap &PM) {
214  if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
215    return true;
216
217  const Stmt *Parent = PM.getParentIgnoreParens(S);
218
219  if (Parent)
220    switch (Parent->getStmtClass()) {
221      case Stmt::ForStmtClass:
222      case Stmt::DoStmtClass:
223      case Stmt::WhileStmtClass:
224        return true;
225      default:
226        break;
227    }
228
229  return false;
230}
231
232PathDiagnosticLocation
233PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
234  assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
235  ParentMap &P = getParentMap();
236  SourceManager &SMgr = getSourceManager();
237  const LocationContext *LC = getLocationContext();
238
239  while (IsNested(S, P)) {
240    const Stmt *Parent = P.getParentIgnoreParens(S);
241
242    if (!Parent)
243      break;
244
245    switch (Parent->getStmtClass()) {
246      case Stmt::BinaryOperatorClass: {
247        const BinaryOperator *B = cast<BinaryOperator>(Parent);
248        if (B->isLogicalOp())
249          return PathDiagnosticLocation(S, SMgr, LC);
250        break;
251      }
252      case Stmt::CompoundStmtClass:
253      case Stmt::StmtExprClass:
254        return PathDiagnosticLocation(S, SMgr, LC);
255      case Stmt::ChooseExprClass:
256        // Similar to '?' if we are referring to condition, just have the edge
257        // point to the entire choose expression.
258        if (cast<ChooseExpr>(Parent)->getCond() == S)
259          return PathDiagnosticLocation(Parent, SMgr, LC);
260        else
261          return PathDiagnosticLocation(S, SMgr, LC);
262      case Stmt::BinaryConditionalOperatorClass:
263      case Stmt::ConditionalOperatorClass:
264        // For '?', if we are referring to condition, just have the edge point
265        // to the entire '?' expression.
266        if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
267          return PathDiagnosticLocation(Parent, SMgr, LC);
268        else
269          return PathDiagnosticLocation(S, SMgr, LC);
270      case Stmt::DoStmtClass:
271          return PathDiagnosticLocation(S, SMgr, LC);
272      case Stmt::ForStmtClass:
273        if (cast<ForStmt>(Parent)->getBody() == S)
274          return PathDiagnosticLocation(S, SMgr, LC);
275        break;
276      case Stmt::IfStmtClass:
277        if (cast<IfStmt>(Parent)->getCond() != S)
278          return PathDiagnosticLocation(S, SMgr, LC);
279        break;
280      case Stmt::ObjCForCollectionStmtClass:
281        if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
282          return PathDiagnosticLocation(S, SMgr, LC);
283        break;
284      case Stmt::WhileStmtClass:
285        if (cast<WhileStmt>(Parent)->getCond() != S)
286          return PathDiagnosticLocation(S, SMgr, LC);
287        break;
288      default:
289        break;
290    }
291
292    S = Parent;
293  }
294
295  assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
296
297  // Special case: DeclStmts can appear in for statement declarations, in which
298  //  case the ForStmt is the context.
299  if (isa<DeclStmt>(S)) {
300    if (const Stmt *Parent = P.getParent(S)) {
301      switch (Parent->getStmtClass()) {
302        case Stmt::ForStmtClass:
303        case Stmt::ObjCForCollectionStmtClass:
304          return PathDiagnosticLocation(Parent, SMgr, LC);
305        default:
306          break;
307      }
308    }
309  }
310  else if (isa<BinaryOperator>(S)) {
311    // Special case: the binary operator represents the initialization
312    // code in a for statement (this can happen when the variable being
313    // initialized is an old variable.
314    if (const ForStmt *FS =
315          dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
316      if (FS->getInit() == S)
317        return PathDiagnosticLocation(FS, SMgr, LC);
318    }
319  }
320
321  return PathDiagnosticLocation(S, SMgr, LC);
322}
323
324//===----------------------------------------------------------------------===//
325// ScanNotableSymbols: closure-like callback for scanning Store bindings.
326//===----------------------------------------------------------------------===//
327
328static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
329                                                  ProgramStateManager& VMgr,
330                                                  SVal X) {
331
332  for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
333
334    ProgramPoint P = N->getLocation();
335
336    if (!isa<PostStmt>(P))
337      continue;
338
339    const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
340
341    if (!DR)
342      continue;
343
344    SVal Y = N->getState()->getSVal(DR);
345
346    if (X != Y)
347      continue;
348
349    const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
350
351    if (!VD)
352      continue;
353
354    return VD;
355  }
356
357  return 0;
358}
359
360namespace {
361class NotableSymbolHandler
362: public StoreManager::BindingsHandler {
363
364  SymbolRef Sym;
365  const ProgramState *PrevSt;
366  const Stmt *S;
367  ProgramStateManager& VMgr;
368  const ExplodedNode *Pred;
369  PathDiagnostic& PD;
370  BugReporter& BR;
371
372public:
373
374  NotableSymbolHandler(SymbolRef sym,
375                       const ProgramState *prevst,
376                       const Stmt *s,
377                       ProgramStateManager& vmgr,
378                       const ExplodedNode *pred,
379                       PathDiagnostic& pd,
380                       BugReporter& br)
381  : Sym(sym),
382    PrevSt(prevst),
383    S(s),
384    VMgr(vmgr),
385    Pred(pred),
386    PD(pd),
387    BR(br) {}
388
389  bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
390                     SVal V) {
391
392    SymbolRef ScanSym = V.getAsSymbol();
393
394    if (ScanSym != Sym)
395      return true;
396
397    // Check if the previous state has this binding.
398    SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
399
400    if (X == V) // Same binding?
401      return true;
402
403    // Different binding.  Only handle assignments for now.  We don't pull
404    // this check out of the loop because we will eventually handle other
405    // cases.
406
407    VarDecl *VD = 0;
408
409    if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
410      if (!B->isAssignmentOp())
411        return true;
412
413      // What variable did we assign to?
414      DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
415
416      if (!DR)
417        return true;
418
419      VD = dyn_cast<VarDecl>(DR->getDecl());
420    }
421    else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
422      // FIXME: Eventually CFGs won't have DeclStmts.  Right now we
423      //  assume that each DeclStmt has a single Decl.  This invariant
424      //  holds by construction in the CFG.
425      VD = dyn_cast<VarDecl>(*DS->decl_begin());
426    }
427
428    if (!VD)
429      return true;
430
431    // What is the most recently referenced variable with this binding?
432    const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
433
434    if (!MostRecent)
435      return true;
436
437    // Create the diagnostic.
438    FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
439
440    if (Loc::isLocType(VD->getType())) {
441      llvm::SmallString<64> buf;
442      llvm::raw_svector_ostream os(buf);
443      os << '\'' << VD << "' now aliases '" << MostRecent << '\'';
444
445      PD.push_front(new PathDiagnosticEventPiece(L, os.str()));
446    }
447
448    return true;
449  }
450};
451}
452
453static void HandleNotableSymbol(const ExplodedNode *N,
454                                const Stmt *S,
455                                SymbolRef Sym, BugReporter& BR,
456                                PathDiagnostic& PD) {
457
458  const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
459  const ProgramState *PrevSt = Pred ? Pred->getState() : 0;
460
461  if (!PrevSt)
462    return;
463
464  // Look at the region bindings of the current state that map to the
465  // specified symbol.  Are any of them not in the previous state?
466  ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
467  NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
468  cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
469}
470
471namespace {
472class ScanNotableSymbols
473: public StoreManager::BindingsHandler {
474
475  llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
476  const ExplodedNode *N;
477  const Stmt *S;
478  GRBugReporter& BR;
479  PathDiagnostic& PD;
480
481public:
482  ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
483                     GRBugReporter& br, PathDiagnostic& pd)
484  : N(n), S(s), BR(br), PD(pd) {}
485
486  bool HandleBinding(StoreManager& SMgr, Store store,
487                     const MemRegion* R, SVal V) {
488
489    SymbolRef ScanSym = V.getAsSymbol();
490
491    if (!ScanSym)
492      return true;
493
494    if (!BR.isNotable(ScanSym))
495      return true;
496
497    if (AlreadyProcessed.count(ScanSym))
498      return true;
499
500    AlreadyProcessed.insert(ScanSym);
501
502    HandleNotableSymbol(N, S, ScanSym, BR, PD);
503    return true;
504  }
505};
506} // end anonymous namespace
507
508//===----------------------------------------------------------------------===//
509// "Minimal" path diagnostic generation algorithm.
510//===----------------------------------------------------------------------===//
511
512static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
513
514static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
515                                          PathDiagnosticBuilder &PDB,
516                                          const ExplodedNode *N) {
517
518  SourceManager& SMgr = PDB.getSourceManager();
519  const LocationContext *LC = PDB.getLocationContext();
520  const ExplodedNode *NextNode = N->pred_empty()
521                                        ? NULL : *(N->pred_begin());
522  while (NextNode) {
523    N = NextNode;
524    NextNode = GetPredecessorNode(N);
525
526    ProgramPoint P = N->getLocation();
527
528    if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
529      const CFGBlock *Src = BE->getSrc();
530      const CFGBlock *Dst = BE->getDst();
531      const Stmt *T = Src->getTerminator();
532
533      if (!T)
534        continue;
535
536      FullSourceLoc Start(T->getLocStart(), SMgr);
537
538      switch (T->getStmtClass()) {
539        default:
540          break;
541
542        case Stmt::GotoStmtClass:
543        case Stmt::IndirectGotoStmtClass: {
544          const Stmt *S = GetNextStmt(N);
545
546          if (!S)
547            continue;
548
549          std::string sbuf;
550          llvm::raw_string_ostream os(sbuf);
551          const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
552
553          os << "Control jumps to line "
554          << End.asLocation().getExpansionLineNumber();
555          PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
556                                                           os.str()));
557          break;
558        }
559
560        case Stmt::SwitchStmtClass: {
561          // Figure out what case arm we took.
562          std::string sbuf;
563          llvm::raw_string_ostream os(sbuf);
564
565          if (const Stmt *S = Dst->getLabel()) {
566            PathDiagnosticLocation End(S, SMgr, LC);
567
568            switch (S->getStmtClass()) {
569              default:
570                os << "No cases match in the switch statement. "
571                "Control jumps to line "
572                << End.asLocation().getExpansionLineNumber();
573                break;
574              case Stmt::DefaultStmtClass:
575                os << "Control jumps to the 'default' case at line "
576                << End.asLocation().getExpansionLineNumber();
577                break;
578
579              case Stmt::CaseStmtClass: {
580                os << "Control jumps to 'case ";
581                const CaseStmt *Case = cast<CaseStmt>(S);
582                const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
583
584                // Determine if it is an enum.
585                bool GetRawInt = true;
586
587                if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
588                  // FIXME: Maybe this should be an assertion.  Are there cases
589                  // were it is not an EnumConstantDecl?
590                  const EnumConstantDecl *D =
591                    dyn_cast<EnumConstantDecl>(DR->getDecl());
592
593                  if (D) {
594                    GetRawInt = false;
595                    os << D;
596                  }
597                }
598
599                if (GetRawInt)
600                  os << LHS->EvaluateAsInt(PDB.getASTContext());
601
602                os << ":'  at line "
603                << End.asLocation().getExpansionLineNumber();
604                break;
605              }
606            }
607            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
608                                                             os.str()));
609          }
610          else {
611            os << "'Default' branch taken. ";
612            const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
613            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
614                                                             os.str()));
615          }
616
617          break;
618        }
619
620        case Stmt::BreakStmtClass:
621        case Stmt::ContinueStmtClass: {
622          std::string sbuf;
623          llvm::raw_string_ostream os(sbuf);
624          PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
625          PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
626                                                           os.str()));
627          break;
628        }
629
630          // Determine control-flow for ternary '?'.
631        case Stmt::BinaryConditionalOperatorClass:
632        case Stmt::ConditionalOperatorClass: {
633          std::string sbuf;
634          llvm::raw_string_ostream os(sbuf);
635          os << "'?' condition is ";
636
637          if (*(Src->succ_begin()+1) == Dst)
638            os << "false";
639          else
640            os << "true";
641
642          PathDiagnosticLocation End = PDB.ExecutionContinues(N);
643
644          if (const Stmt *S = End.asStmt())
645            End = PDB.getEnclosingStmtLocation(S);
646
647          PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
648                                                           os.str()));
649          break;
650        }
651
652          // Determine control-flow for short-circuited '&&' and '||'.
653        case Stmt::BinaryOperatorClass: {
654          if (!PDB.supportsLogicalOpControlFlow())
655            break;
656
657          const BinaryOperator *B = cast<BinaryOperator>(T);
658          std::string sbuf;
659          llvm::raw_string_ostream os(sbuf);
660          os << "Left side of '";
661
662          if (B->getOpcode() == BO_LAnd) {
663            os << "&&" << "' is ";
664
665            if (*(Src->succ_begin()+1) == Dst) {
666              os << "false";
667              PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
668              PathDiagnosticLocation Start =
669                PathDiagnosticLocation::createOperatorLoc(B, SMgr);
670              PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
671                                                               os.str()));
672            }
673            else {
674              os << "true";
675              PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
676              PathDiagnosticLocation End = PDB.ExecutionContinues(N);
677              PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
678                                                               os.str()));
679            }
680          }
681          else {
682            assert(B->getOpcode() == BO_LOr);
683            os << "||" << "' is ";
684
685            if (*(Src->succ_begin()+1) == Dst) {
686              os << "false";
687              PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
688              PathDiagnosticLocation End = PDB.ExecutionContinues(N);
689              PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
690                                                               os.str()));
691            }
692            else {
693              os << "true";
694              PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
695              PathDiagnosticLocation Start =
696                PathDiagnosticLocation::createOperatorLoc(B, SMgr);
697              PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
698                                                               os.str()));
699            }
700          }
701
702          break;
703        }
704
705        case Stmt::DoStmtClass:  {
706          if (*(Src->succ_begin()) == Dst) {
707            std::string sbuf;
708            llvm::raw_string_ostream os(sbuf);
709
710            os << "Loop condition is true. ";
711            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
712
713            if (const Stmt *S = End.asStmt())
714              End = PDB.getEnclosingStmtLocation(S);
715
716            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
717                                                             os.str()));
718          }
719          else {
720            PathDiagnosticLocation End = PDB.ExecutionContinues(N);
721
722            if (const Stmt *S = End.asStmt())
723              End = PDB.getEnclosingStmtLocation(S);
724
725            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
726                              "Loop condition is false.  Exiting loop"));
727          }
728
729          break;
730        }
731
732        case Stmt::WhileStmtClass:
733        case Stmt::ForStmtClass: {
734          if (*(Src->succ_begin()+1) == Dst) {
735            std::string sbuf;
736            llvm::raw_string_ostream os(sbuf);
737
738            os << "Loop condition is false. ";
739            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
740            if (const Stmt *S = End.asStmt())
741              End = PDB.getEnclosingStmtLocation(S);
742
743            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
744                                                             os.str()));
745          }
746          else {
747            PathDiagnosticLocation End = PDB.ExecutionContinues(N);
748            if (const Stmt *S = End.asStmt())
749              End = PDB.getEnclosingStmtLocation(S);
750
751            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
752                            "Loop condition is true.  Entering loop body"));
753          }
754
755          break;
756        }
757
758        case Stmt::IfStmtClass: {
759          PathDiagnosticLocation End = PDB.ExecutionContinues(N);
760
761          if (const Stmt *S = End.asStmt())
762            End = PDB.getEnclosingStmtLocation(S);
763
764          if (*(Src->succ_begin()+1) == Dst)
765            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
766                                                        "Taking false branch"));
767          else
768            PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
769                                                         "Taking true branch"));
770
771          break;
772        }
773      }
774    }
775
776    if (NextNode) {
777      // Add diagnostic pieces from custom visitors.
778      BugReport *R = PDB.getBugReport();
779      for (BugReport::visitor_iterator I = R->visitor_begin(),
780           E = R->visitor_end(); I!=E; ++I) {
781        if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
782          PD.push_front(p);
783      }
784    }
785
786    if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
787      // Scan the region bindings, and see if a "notable" symbol has a new
788      // lval binding.
789      ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
790      PDB.getStateManager().iterBindings(N->getState(), SNS);
791    }
792  }
793
794  // After constructing the full PathDiagnostic, do a pass over it to compact
795  // PathDiagnosticPieces that occur within a macro.
796  CompactPathDiagnostic(PD, PDB.getSourceManager());
797}
798
799//===----------------------------------------------------------------------===//
800// "Extensive" PathDiagnostic generation.
801//===----------------------------------------------------------------------===//
802
803static bool IsControlFlowExpr(const Stmt *S) {
804  const Expr *E = dyn_cast<Expr>(S);
805
806  if (!E)
807    return false;
808
809  E = E->IgnoreParenCasts();
810
811  if (isa<AbstractConditionalOperator>(E))
812    return true;
813
814  if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
815    if (B->isLogicalOp())
816      return true;
817
818  return false;
819}
820
821namespace {
822class ContextLocation : public PathDiagnosticLocation {
823  bool IsDead;
824public:
825  ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
826    : PathDiagnosticLocation(L), IsDead(isdead) {}
827
828  void markDead() { IsDead = true; }
829  bool isDead() const { return IsDead; }
830};
831
832class EdgeBuilder {
833  std::vector<ContextLocation> CLocs;
834  typedef std::vector<ContextLocation>::iterator iterator;
835  PathDiagnostic &PD;
836  PathDiagnosticBuilder &PDB;
837  PathDiagnosticLocation PrevLoc;
838
839  bool IsConsumedExpr(const PathDiagnosticLocation &L);
840
841  bool containsLocation(const PathDiagnosticLocation &Container,
842                        const PathDiagnosticLocation &Containee);
843
844  PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
845
846  PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
847                                         bool firstCharOnly = false) {
848    if (const Stmt *S = L.asStmt()) {
849      const Stmt *Original = S;
850      while (1) {
851        // Adjust the location for some expressions that are best referenced
852        // by one of their subexpressions.
853        switch (S->getStmtClass()) {
854          default:
855            break;
856          case Stmt::ParenExprClass:
857          case Stmt::GenericSelectionExprClass:
858            S = cast<Expr>(S)->IgnoreParens();
859            firstCharOnly = true;
860            continue;
861          case Stmt::BinaryConditionalOperatorClass:
862          case Stmt::ConditionalOperatorClass:
863            S = cast<AbstractConditionalOperator>(S)->getCond();
864            firstCharOnly = true;
865            continue;
866          case Stmt::ChooseExprClass:
867            S = cast<ChooseExpr>(S)->getCond();
868            firstCharOnly = true;
869            continue;
870          case Stmt::BinaryOperatorClass:
871            S = cast<BinaryOperator>(S)->getLHS();
872            firstCharOnly = true;
873            continue;
874        }
875
876        break;
877      }
878
879      if (S != Original)
880        L = PathDiagnosticLocation(S, L.getManager(), L.getLocationContext());
881    }
882
883    if (firstCharOnly)
884      L.setSingleLocKind();
885
886    return L;
887  }
888
889  void popLocation() {
890    if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
891      // For contexts, we only one the first character as the range.
892      rawAddEdge(cleanUpLocation(CLocs.back(), true));
893    }
894    CLocs.pop_back();
895  }
896
897public:
898  EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
899    : PD(pd), PDB(pdb) {
900
901      // If the PathDiagnostic already has pieces, add the enclosing statement
902      // of the first piece as a context as well.
903      if (!PD.empty()) {
904        PrevLoc = PD.begin()->getLocation();
905
906        if (const Stmt *S = PrevLoc.asStmt())
907          addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
908      }
909  }
910
911  ~EdgeBuilder() {
912    while (!CLocs.empty()) popLocation();
913
914    // Finally, add an initial edge from the start location of the first
915    // statement (if it doesn't already exist).
916    PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
917                                                       PDB.getLocationContext(),
918                                                       PDB.getSourceManager());
919    if (L.isValid())
920      rawAddEdge(L);
921  }
922
923  void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
924
925  void rawAddEdge(PathDiagnosticLocation NewLoc);
926
927  void addContext(const Stmt *S);
928  void addExtendedContext(const Stmt *S);
929};
930} // end anonymous namespace
931
932
933PathDiagnosticLocation
934EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
935  if (const Stmt *S = L.asStmt()) {
936    if (IsControlFlowExpr(S))
937      return L;
938
939    return PDB.getEnclosingStmtLocation(S);
940  }
941
942  return L;
943}
944
945bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
946                                   const PathDiagnosticLocation &Containee) {
947
948  if (Container == Containee)
949    return true;
950
951  if (Container.asDecl())
952    return true;
953
954  if (const Stmt *S = Containee.asStmt())
955    if (const Stmt *ContainerS = Container.asStmt()) {
956      while (S) {
957        if (S == ContainerS)
958          return true;
959        S = PDB.getParent(S);
960      }
961      return false;
962    }
963
964  // Less accurate: compare using source ranges.
965  SourceRange ContainerR = Container.asRange();
966  SourceRange ContaineeR = Containee.asRange();
967
968  SourceManager &SM = PDB.getSourceManager();
969  SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
970  SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
971  SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
972  SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
973
974  unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
975  unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
976  unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
977  unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
978
979  assert(ContainerBegLine <= ContainerEndLine);
980  assert(ContaineeBegLine <= ContaineeEndLine);
981
982  return (ContainerBegLine <= ContaineeBegLine &&
983          ContainerEndLine >= ContaineeEndLine &&
984          (ContainerBegLine != ContaineeBegLine ||
985           SM.getExpansionColumnNumber(ContainerRBeg) <=
986           SM.getExpansionColumnNumber(ContaineeRBeg)) &&
987          (ContainerEndLine != ContaineeEndLine ||
988           SM.getExpansionColumnNumber(ContainerREnd) >=
989           SM.getExpansionColumnNumber(ContainerREnd)));
990}
991
992void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
993  if (!PrevLoc.isValid()) {
994    PrevLoc = NewLoc;
995    return;
996  }
997
998  const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
999  const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
1000
1001  if (NewLocClean.asLocation() == PrevLocClean.asLocation())
1002    return;
1003
1004  // FIXME: Ignore intra-macro edges for now.
1005  if (NewLocClean.asLocation().getExpansionLoc() ==
1006      PrevLocClean.asLocation().getExpansionLoc())
1007    return;
1008
1009  PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1010  PrevLoc = NewLoc;
1011}
1012
1013void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
1014
1015  if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1016    return;
1017
1018  const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1019
1020  while (!CLocs.empty()) {
1021    ContextLocation &TopContextLoc = CLocs.back();
1022
1023    // Is the top location context the same as the one for the new location?
1024    if (TopContextLoc == CLoc) {
1025      if (alwaysAdd) {
1026        if (IsConsumedExpr(TopContextLoc) &&
1027            !IsControlFlowExpr(TopContextLoc.asStmt()))
1028            TopContextLoc.markDead();
1029
1030        rawAddEdge(NewLoc);
1031      }
1032
1033      return;
1034    }
1035
1036    if (containsLocation(TopContextLoc, CLoc)) {
1037      if (alwaysAdd) {
1038        rawAddEdge(NewLoc);
1039
1040        if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
1041          CLocs.push_back(ContextLocation(CLoc, true));
1042          return;
1043        }
1044      }
1045
1046      CLocs.push_back(CLoc);
1047      return;
1048    }
1049
1050    // Context does not contain the location.  Flush it.
1051    popLocation();
1052  }
1053
1054  // If we reach here, there is no enclosing context.  Just add the edge.
1055  rawAddEdge(NewLoc);
1056}
1057
1058bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1059  if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1060    return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
1061
1062  return false;
1063}
1064
1065void EdgeBuilder::addExtendedContext(const Stmt *S) {
1066  if (!S)
1067    return;
1068
1069  const Stmt *Parent = PDB.getParent(S);
1070  while (Parent) {
1071    if (isa<CompoundStmt>(Parent))
1072      Parent = PDB.getParent(Parent);
1073    else
1074      break;
1075  }
1076
1077  if (Parent) {
1078    switch (Parent->getStmtClass()) {
1079      case Stmt::DoStmtClass:
1080      case Stmt::ObjCAtSynchronizedStmtClass:
1081        addContext(Parent);
1082      default:
1083        break;
1084    }
1085  }
1086
1087  addContext(S);
1088}
1089
1090void EdgeBuilder::addContext(const Stmt *S) {
1091  if (!S)
1092    return;
1093
1094  PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
1095
1096  while (!CLocs.empty()) {
1097    const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1098
1099    // Is the top location context the same as the one for the new location?
1100    if (TopContextLoc == L)
1101      return;
1102
1103    if (containsLocation(TopContextLoc, L)) {
1104      CLocs.push_back(L);
1105      return;
1106    }
1107
1108    // Context does not contain the location.  Flush it.
1109    popLocation();
1110  }
1111
1112  CLocs.push_back(L);
1113}
1114
1115static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1116                                            PathDiagnosticBuilder &PDB,
1117                                            const ExplodedNode *N) {
1118  EdgeBuilder EB(PD, PDB);
1119  const SourceManager& SM = PDB.getSourceManager();
1120
1121  const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
1122  while (NextNode) {
1123    N = NextNode;
1124    NextNode = GetPredecessorNode(N);
1125    ProgramPoint P = N->getLocation();
1126
1127    do {
1128      // Block edges.
1129      if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1130        const CFGBlock &Blk = *BE->getSrc();
1131        const Stmt *Term = Blk.getTerminator();
1132
1133        // Are we jumping to the head of a loop?  Add a special diagnostic.
1134        if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
1135          PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
1136          const CompoundStmt *CS = NULL;
1137
1138          if (!Term) {
1139            if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1140              CS = dyn_cast<CompoundStmt>(FS->getBody());
1141            else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1142              CS = dyn_cast<CompoundStmt>(WS->getBody());
1143          }
1144
1145          PathDiagnosticEventPiece *p =
1146            new PathDiagnosticEventPiece(L,
1147                                        "Looping back to the head of the loop");
1148
1149          EB.addEdge(p->getLocation(), true);
1150          PD.push_front(p);
1151
1152          if (CS) {
1153            PathDiagnosticLocation BL =
1154              PathDiagnosticLocation::createEndBrace(CS, SM);
1155            EB.addEdge(BL);
1156          }
1157        }
1158
1159        if (Term)
1160          EB.addContext(Term);
1161
1162        break;
1163      }
1164
1165      if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
1166        if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1167          const Stmt *stmt = S->getStmt();
1168          if (IsControlFlowExpr(stmt)) {
1169            // Add the proper context for '&&', '||', and '?'.
1170            EB.addContext(stmt);
1171          }
1172          else
1173            EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
1174        }
1175
1176        break;
1177      }
1178    } while (0);
1179
1180    if (!NextNode)
1181      continue;
1182
1183    // Add pieces from custom visitors.
1184    BugReport *R = PDB.getBugReport();
1185    for (BugReport::visitor_iterator I = R->visitor_begin(),
1186                                     E = R->visitor_end(); I!=E; ++I) {
1187      if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
1188        const PathDiagnosticLocation &Loc = p->getLocation();
1189        EB.addEdge(Loc, true);
1190        PD.push_front(p);
1191        if (const Stmt *S = Loc.asStmt())
1192          EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1193      }
1194    }
1195  }
1196}
1197
1198//===----------------------------------------------------------------------===//
1199// Methods for BugType and subclasses.
1200//===----------------------------------------------------------------------===//
1201BugType::~BugType() { }
1202
1203void BugType::FlushReports(BugReporter &BR) {}
1204
1205//===----------------------------------------------------------------------===//
1206// Methods for BugReport and subclasses.
1207//===----------------------------------------------------------------------===//
1208
1209void BugReport::addVisitor(BugReporterVisitor* visitor) {
1210  if (!visitor)
1211    return;
1212
1213  llvm::FoldingSetNodeID ID;
1214  visitor->Profile(ID);
1215  void *InsertPos;
1216
1217  if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1218    delete visitor;
1219    return;
1220  }
1221
1222  CallbacksSet.InsertNode(visitor, InsertPos);
1223  Callbacks = F.add(visitor, Callbacks);
1224}
1225
1226BugReport::~BugReport() {
1227  for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
1228    delete *I;
1229  }
1230}
1231
1232void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1233  hash.AddPointer(&BT);
1234  hash.AddInteger(getLocation().getRawEncoding());
1235  hash.AddString(Description);
1236
1237  for (SmallVectorImpl<SourceRange>::const_iterator I =
1238      Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1239    const SourceRange range = *I;
1240    if (!range.isValid())
1241      continue;
1242    hash.AddInteger(range.getBegin().getRawEncoding());
1243    hash.AddInteger(range.getEnd().getRawEncoding());
1244  }
1245}
1246
1247const Stmt *BugReport::getStmt() const {
1248  if (!ErrorNode)
1249    return 0;
1250
1251  ProgramPoint ProgP = ErrorNode->getLocation();
1252  const Stmt *S = NULL;
1253
1254  if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
1255    CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
1256    if (BE->getBlock() == &Exit)
1257      S = GetPreviousStmt(ErrorNode);
1258  }
1259  if (!S)
1260    S = GetStmt(ProgP);
1261
1262  return S;
1263}
1264
1265std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1266BugReport::getRanges() {
1267    // If no custom ranges, add the range of the statement corresponding to
1268    // the error node.
1269    if (Ranges.empty()) {
1270      if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1271        addRange(E->getSourceRange());
1272      else
1273        return std::make_pair(ranges_iterator(), ranges_iterator());
1274    }
1275
1276    // User-specified absence of range info.
1277    if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1278      return std::make_pair(ranges_iterator(), ranges_iterator());
1279
1280    return std::make_pair(Ranges.begin(), Ranges.end());
1281}
1282
1283SourceLocation BugReport::getLocation() const {
1284  if (ErrorNode) {
1285    (Location.isInvalid() &&
1286     "Either Location or ErrorNode should be specified but not both.");
1287
1288    if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
1289      // For member expressions, return the location of the '.' or '->'.
1290      if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
1291        return ME->getMemberLoc();
1292      // For binary operators, return the location of the operator.
1293      if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1294        return B->getOperatorLoc();
1295
1296      return S->getLocStart();
1297    }
1298
1299  } else {
1300    assert(Location.isValid());
1301    return Location;
1302  }
1303
1304  return FullSourceLoc();
1305}
1306
1307//===----------------------------------------------------------------------===//
1308// Methods for BugReporter and subclasses.
1309//===----------------------------------------------------------------------===//
1310
1311BugReportEquivClass::~BugReportEquivClass() {
1312  for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
1313}
1314
1315GRBugReporter::~GRBugReporter() { }
1316BugReporterData::~BugReporterData() {}
1317
1318ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
1319
1320ProgramStateManager&
1321GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1322
1323BugReporter::~BugReporter() {
1324  FlushReports();
1325
1326  // Free the bug reports we are tracking.
1327  typedef std::vector<BugReportEquivClass *> ContTy;
1328  for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1329       I != E; ++I) {
1330    delete *I;
1331  }
1332}
1333
1334void BugReporter::FlushReports() {
1335  if (BugTypes.isEmpty())
1336    return;
1337
1338  // First flush the warnings for each BugType.  This may end up creating new
1339  // warnings and new BugTypes.
1340  // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1341  // Turn NSErrorChecker into a proper checker and remove this.
1342  SmallVector<const BugType*, 16> bugTypes;
1343  for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1344    bugTypes.push_back(*I);
1345  for (SmallVector<const BugType*, 16>::iterator
1346         I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
1347    const_cast<BugType*>(*I)->FlushReports(*this);
1348
1349  typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1350  for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1351    BugReportEquivClass& EQ = *EI;
1352    FlushReport(EQ);
1353  }
1354
1355  // BugReporter owns and deletes only BugTypes created implicitly through
1356  // EmitBasicReport.
1357  // FIXME: There are leaks from checkers that assume that the BugTypes they
1358  // create will be destroyed by the BugReporter.
1359  for (llvm::StringMap<BugType*>::iterator
1360         I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1361    delete I->second;
1362
1363  // Remove all references to the BugType objects.
1364  BugTypes = F.getEmptySet();
1365}
1366
1367//===----------------------------------------------------------------------===//
1368// PathDiagnostics generation.
1369//===----------------------------------------------------------------------===//
1370
1371static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
1372                 std::pair<ExplodedNode*, unsigned> >
1373MakeReportGraph(const ExplodedGraph* G,
1374                SmallVectorImpl<const ExplodedNode*> &nodes) {
1375
1376  // Create the trimmed graph.  It will contain the shortest paths from the
1377  // error nodes to the root.  In the new graph we should only have one
1378  // error node unless there are two or more error nodes with the same minimum
1379  // path length.
1380  ExplodedGraph* GTrim;
1381  InterExplodedGraphMap* NMap;
1382
1383  llvm::DenseMap<const void*, const void*> InverseMap;
1384  llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1385                                   &InverseMap);
1386
1387  // Create owning pointers for GTrim and NMap just to ensure that they are
1388  // released when this function exists.
1389  llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1390  llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
1391
1392  // Find the (first) error node in the trimmed graph.  We just need to consult
1393  // the node map (NMap) which maps from nodes in the original graph to nodes
1394  // in the new graph.
1395
1396  std::queue<const ExplodedNode*> WS;
1397  typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
1398  IndexMapTy IndexMap;
1399
1400  for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1401    const ExplodedNode *originalNode = nodes[nodeIndex];
1402    if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
1403      WS.push(N);
1404      IndexMap[originalNode] = nodeIndex;
1405    }
1406  }
1407
1408  assert(!WS.empty() && "No error node found in the trimmed graph.");
1409
1410  // Create a new (third!) graph with a single path.  This is the graph
1411  // that will be returned to the caller.
1412  ExplodedGraph *GNew = new ExplodedGraph();
1413
1414  // Sometimes the trimmed graph can contain a cycle.  Perform a reverse BFS
1415  // to the root node, and then construct a new graph that contains only
1416  // a single path.
1417  llvm::DenseMap<const void*,unsigned> Visited;
1418
1419  unsigned cnt = 0;
1420  const ExplodedNode *Root = 0;
1421
1422  while (!WS.empty()) {
1423    const ExplodedNode *Node = WS.front();
1424    WS.pop();
1425
1426    if (Visited.find(Node) != Visited.end())
1427      continue;
1428
1429    Visited[Node] = cnt++;
1430
1431    if (Node->pred_empty()) {
1432      Root = Node;
1433      break;
1434    }
1435
1436    for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
1437         E=Node->pred_end(); I!=E; ++I)
1438      WS.push(*I);
1439  }
1440
1441  assert(Root);
1442
1443  // Now walk from the root down the BFS path, always taking the successor
1444  // with the lowest number.
1445  ExplodedNode *Last = 0, *First = 0;
1446  NodeBackMap *BM = new NodeBackMap();
1447  unsigned NodeIndex = 0;
1448
1449  for ( const ExplodedNode *N = Root ;;) {
1450    // Lookup the number associated with the current node.
1451    llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
1452    assert(I != Visited.end());
1453
1454    // Create the equivalent node in the new graph with the same state
1455    // and location.
1456    ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
1457
1458    // Store the mapping to the original node.
1459    llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1460    assert(IMitr != InverseMap.end() && "No mapping to original node.");
1461    (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
1462
1463    // Link up the new node with the previous node.
1464    if (Last)
1465      NewN->addPredecessor(Last, *GNew);
1466
1467    Last = NewN;
1468
1469    // Are we at the final node?
1470    IndexMapTy::iterator IMI =
1471      IndexMap.find((const ExplodedNode*)(IMitr->second));
1472    if (IMI != IndexMap.end()) {
1473      First = NewN;
1474      NodeIndex = IMI->second;
1475      break;
1476    }
1477
1478    // Find the next successor node.  We choose the node that is marked
1479    // with the lowest DFS number.
1480    ExplodedNode::const_succ_iterator SI = N->succ_begin();
1481    ExplodedNode::const_succ_iterator SE = N->succ_end();
1482    N = 0;
1483
1484    for (unsigned MinVal = 0; SI != SE; ++SI) {
1485
1486      I = Visited.find(*SI);
1487
1488      if (I == Visited.end())
1489        continue;
1490
1491      if (!N || I->second < MinVal) {
1492        N = *SI;
1493        MinVal = I->second;
1494      }
1495    }
1496
1497    assert(N);
1498  }
1499
1500  assert(First);
1501
1502  return std::make_pair(std::make_pair(GNew, BM),
1503                        std::make_pair(First, NodeIndex));
1504}
1505
1506/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1507///  and collapses PathDiagosticPieces that are expanded by macros.
1508static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1509  typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1510          MacroStackTy;
1511
1512  typedef std::vector<PathDiagnosticPiece*>
1513          PiecesTy;
1514
1515  MacroStackTy MacroStack;
1516  PiecesTy Pieces;
1517
1518  for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1519    // Get the location of the PathDiagnosticPiece.
1520    const FullSourceLoc Loc = I->getLocation().asLocation();
1521
1522    // Determine the instantiation location, which is the location we group
1523    // related PathDiagnosticPieces.
1524    SourceLocation InstantiationLoc = Loc.isMacroID() ?
1525                                      SM.getExpansionLoc(Loc) :
1526                                      SourceLocation();
1527
1528    if (Loc.isFileID()) {
1529      MacroStack.clear();
1530      Pieces.push_back(&*I);
1531      continue;
1532    }
1533
1534    assert(Loc.isMacroID());
1535
1536    // Is the PathDiagnosticPiece within the same macro group?
1537    if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1538      MacroStack.back().first->push_back(&*I);
1539      continue;
1540    }
1541
1542    // We aren't in the same group.  Are we descending into a new macro
1543    // or are part of an old one?
1544    PathDiagnosticMacroPiece *MacroGroup = 0;
1545
1546    SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1547                                          SM.getExpansionLoc(Loc) :
1548                                          SourceLocation();
1549
1550    // Walk the entire macro stack.
1551    while (!MacroStack.empty()) {
1552      if (InstantiationLoc == MacroStack.back().second) {
1553        MacroGroup = MacroStack.back().first;
1554        break;
1555      }
1556
1557      if (ParentInstantiationLoc == MacroStack.back().second) {
1558        MacroGroup = MacroStack.back().first;
1559        break;
1560      }
1561
1562      MacroStack.pop_back();
1563    }
1564
1565    if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1566      // Create a new macro group and add it to the stack.
1567      PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1568
1569      if (MacroGroup)
1570        MacroGroup->push_back(NewGroup);
1571      else {
1572        assert(InstantiationLoc.isFileID());
1573        Pieces.push_back(NewGroup);
1574      }
1575
1576      MacroGroup = NewGroup;
1577      MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1578    }
1579
1580    // Finally, add the PathDiagnosticPiece to the group.
1581    MacroGroup->push_back(&*I);
1582  }
1583
1584  // Now take the pieces and construct a new PathDiagnostic.
1585  PD.resetPath(false);
1586
1587  for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1588    if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1589      if (!MP->containsEvent()) {
1590        delete MP;
1591        continue;
1592      }
1593
1594    PD.push_back(*I);
1595  }
1596}
1597
1598void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
1599                        SmallVectorImpl<BugReport *> &bugReports) {
1600
1601  assert(!bugReports.empty());
1602  SmallVector<const ExplodedNode *, 10> errorNodes;
1603  for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
1604    E = bugReports.end(); I != E; ++I) {
1605      errorNodes.push_back((*I)->getErrorNode());
1606  }
1607
1608  // Construct a new graph that contains only a single path from the error
1609  // node to a root.
1610  const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
1611  std::pair<ExplodedNode*, unsigned> >&
1612    GPair = MakeReportGraph(&getGraph(), errorNodes);
1613
1614  // Find the BugReport with the original location.
1615  assert(GPair.second.second < bugReports.size());
1616  BugReport *R = bugReports[GPair.second.second];
1617  assert(R && "No original report found for sliced graph.");
1618
1619  llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1620  llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
1621  const ExplodedNode *N = GPair.second.first;
1622
1623  // Start building the path diagnostic...
1624  PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
1625
1626  // Register additional node visitors.
1627  R->addVisitor(new NilReceiverBRVisitor());
1628  R->addVisitor(new ConditionBRVisitor());
1629
1630  // Generate the very last diagnostic piece - the piece is visible before
1631  // the trace is expanded.
1632  PathDiagnosticPiece *LastPiece = 0;
1633  for (BugReport::visitor_iterator I = R->visitor_begin(),
1634                                   E = R->visitor_end(); I!=E; ++I) {
1635    if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1636      assert (!LastPiece &&
1637              "There can only be one final piece in a diagnostic.");
1638      LastPiece = Piece;
1639    }
1640  }
1641  if (!LastPiece)
1642    LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1643  if (LastPiece)
1644    PD.push_back(LastPiece);
1645  else
1646    return;
1647
1648  switch (PDB.getGenerationScheme()) {
1649    case PathDiagnosticClient::Extensive:
1650      GenerateExtensivePathDiagnostic(PD, PDB, N);
1651      break;
1652    case PathDiagnosticClient::Minimal:
1653      GenerateMinimalPathDiagnostic(PD, PDB, N);
1654      break;
1655  }
1656}
1657
1658void BugReporter::Register(BugType *BT) {
1659  BugTypes = F.add(BugTypes, BT);
1660}
1661
1662void BugReporter::EmitReport(BugReport* R) {
1663  // Compute the bug report's hash to determine its equivalence class.
1664  llvm::FoldingSetNodeID ID;
1665  R->Profile(ID);
1666
1667  // Lookup the equivance class.  If there isn't one, create it.
1668  BugType& BT = R->getBugType();
1669  Register(&BT);
1670  void *InsertPos;
1671  BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1672
1673  if (!EQ) {
1674    EQ = new BugReportEquivClass(R);
1675    EQClasses.InsertNode(EQ, InsertPos);
1676    EQClassesVector.push_back(EQ);
1677  }
1678  else
1679    EQ->AddReport(R);
1680}
1681
1682
1683//===----------------------------------------------------------------------===//
1684// Emitting reports in equivalence classes.
1685//===----------------------------------------------------------------------===//
1686
1687namespace {
1688struct FRIEC_WLItem {
1689  const ExplodedNode *N;
1690  ExplodedNode::const_succ_iterator I, E;
1691
1692  FRIEC_WLItem(const ExplodedNode *n)
1693  : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1694};
1695}
1696
1697static BugReport *
1698FindReportInEquivalenceClass(BugReportEquivClass& EQ,
1699                             SmallVectorImpl<BugReport*> &bugReports) {
1700
1701  BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1702  assert(I != E);
1703  BugReport *R = *I;
1704  BugType& BT = R->getBugType();
1705
1706  // If we don't need to suppress any of the nodes because they are
1707  // post-dominated by a sink, simply add all the nodes in the equivalence class
1708  // to 'Nodes'.  Any of the reports will serve as a "representative" report.
1709  if (!BT.isSuppressOnSink()) {
1710    for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
1711      const ExplodedNode *N = I->getErrorNode();
1712      if (N) {
1713        R = *I;
1714        bugReports.push_back(R);
1715      }
1716    }
1717    return R;
1718  }
1719
1720  // For bug reports that should be suppressed when all paths are post-dominated
1721  // by a sink node, iterate through the reports in the equivalence class
1722  // until we find one that isn't post-dominated (if one exists).  We use a
1723  // DFS traversal of the ExplodedGraph to find a non-sink node.  We could write
1724  // this as a recursive function, but we don't want to risk blowing out the
1725  // stack for very long paths.
1726  BugReport *exampleReport = 0;
1727
1728  for (; I != E; ++I) {
1729    R = *I;
1730    const ExplodedNode *errorNode = R->getErrorNode();
1731
1732    if (!errorNode)
1733      continue;
1734    if (errorNode->isSink()) {
1735      assert(false &&
1736           "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1737      return 0;
1738    }
1739    // No successors?  By definition this nodes isn't post-dominated by a sink.
1740    if (errorNode->succ_empty()) {
1741      bugReports.push_back(R);
1742      if (!exampleReport)
1743        exampleReport = R;
1744      continue;
1745    }
1746
1747    // At this point we know that 'N' is not a sink and it has at least one
1748    // successor.  Use a DFS worklist to find a non-sink end-of-path node.
1749    typedef FRIEC_WLItem WLItem;
1750    typedef SmallVector<WLItem, 10> DFSWorkList;
1751    llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1752
1753    DFSWorkList WL;
1754    WL.push_back(errorNode);
1755    Visited[errorNode] = 1;
1756
1757    while (!WL.empty()) {
1758      WLItem &WI = WL.back();
1759      assert(!WI.N->succ_empty());
1760
1761      for (; WI.I != WI.E; ++WI.I) {
1762        const ExplodedNode *Succ = *WI.I;
1763        // End-of-path node?
1764        if (Succ->succ_empty()) {
1765          // If we found an end-of-path node that is not a sink.
1766          if (!Succ->isSink()) {
1767            bugReports.push_back(R);
1768            if (!exampleReport)
1769              exampleReport = R;
1770            WL.clear();
1771            break;
1772          }
1773          // Found a sink?  Continue on to the next successor.
1774          continue;
1775        }
1776        // Mark the successor as visited.  If it hasn't been explored,
1777        // enqueue it to the DFS worklist.
1778        unsigned &mark = Visited[Succ];
1779        if (!mark) {
1780          mark = 1;
1781          WL.push_back(Succ);
1782          break;
1783        }
1784      }
1785
1786      // The worklist may have been cleared at this point.  First
1787      // check if it is empty before checking the last item.
1788      if (!WL.empty() && &WL.back() == &WI)
1789        WL.pop_back();
1790    }
1791  }
1792
1793  // ExampleReport will be NULL if all the nodes in the equivalence class
1794  // were post-dominated by sinks.
1795  return exampleReport;
1796}
1797
1798//===----------------------------------------------------------------------===//
1799// DiagnosticCache.  This is a hack to cache analyzer diagnostics.  It
1800// uses global state, which eventually should go elsewhere.
1801//===----------------------------------------------------------------------===//
1802namespace {
1803class DiagCacheItem : public llvm::FoldingSetNode {
1804  llvm::FoldingSetNodeID ID;
1805public:
1806  DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1807    ID.AddString(R->getBugType().getName());
1808    ID.AddString(R->getBugType().getCategory());
1809    ID.AddString(R->getDescription());
1810    ID.AddInteger(R->getLocation().getRawEncoding());
1811    PD->Profile(ID);
1812  }
1813
1814  void Profile(llvm::FoldingSetNodeID &id) {
1815    id = ID;
1816  }
1817
1818  llvm::FoldingSetNodeID &getID() { return ID; }
1819};
1820}
1821
1822static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1823  // FIXME: Eventually this diagnostic cache should reside in something
1824  // like AnalysisManager instead of being a static variable.  This is
1825  // really unsafe in the long term.
1826  typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1827  static DiagnosticCache DC;
1828
1829  void *InsertPos;
1830  DiagCacheItem *Item = new DiagCacheItem(R, PD);
1831
1832  if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1833    delete Item;
1834    return true;
1835  }
1836
1837  DC.InsertNode(Item, InsertPos);
1838  return false;
1839}
1840
1841void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1842  SmallVector<BugReport*, 10> bugReports;
1843  BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1844  if (!exampleReport)
1845    return;
1846
1847  PathDiagnosticClient* PD = getPathDiagnosticClient();
1848
1849  // FIXME: Make sure we use the 'R' for the path that was actually used.
1850  // Probably doesn't make a difference in practice.
1851  BugType& BT = exampleReport->getBugType();
1852
1853  llvm::OwningPtr<PathDiagnostic>
1854    D(new PathDiagnostic(exampleReport->getBugType().getName(),
1855                         !PD || PD->useVerboseDescription()
1856                         ? exampleReport->getDescription()
1857                         : exampleReport->getShortDescription(),
1858                         BT.getCategory()));
1859
1860  if (!bugReports.empty())
1861    GeneratePathDiagnostic(*D.get(), bugReports);
1862
1863  if (IsCachedDiagnostic(exampleReport, D.get()))
1864    return;
1865
1866  // Get the meta data.
1867  const BugReport::ExtraTextList &Meta =
1868                                  exampleReport->getExtraText();
1869  for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1870                                                e = Meta.end(); i != e; ++i) {
1871    D->addMeta(*i);
1872  }
1873
1874  // Emit a summary diagnostic to the regular Diagnostics engine.
1875  BugReport::ranges_iterator Beg, End;
1876  llvm::tie(Beg, End) = exampleReport->getRanges();
1877  Diagnostic &Diag = getDiagnostic();
1878  FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
1879
1880  // Search the description for '%', as that will be interpretted as a
1881  // format character by FormatDiagnostics.
1882  StringRef desc = exampleReport->getShortDescription();
1883  unsigned ErrorDiag;
1884  {
1885    llvm::SmallString<512> TmpStr;
1886    llvm::raw_svector_ostream Out(TmpStr);
1887    for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1888      if (*I == '%')
1889        Out << "%%";
1890      else
1891        Out << *I;
1892
1893    Out.flush();
1894    ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1895  }
1896
1897  {
1898    DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
1899    for (BugReport::ranges_iterator I = Beg; I != End; ++I)
1900      diagBuilder << *I;
1901  }
1902
1903  // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1904  if (!PD)
1905    return;
1906
1907  if (D->empty()) {
1908    PathDiagnosticPiece *piece =
1909      new PathDiagnosticEventPiece(L, exampleReport->getDescription());
1910
1911    for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1912    D->push_back(piece);
1913  }
1914
1915  PD->HandlePathDiagnostic(D.take());
1916}
1917
1918void BugReporter::EmitBasicReport(StringRef name, StringRef str,
1919                                  SourceLocation Loc,
1920                                  SourceRange* RBeg, unsigned NumRanges) {
1921  EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1922}
1923
1924void BugReporter::EmitBasicReport(StringRef name,
1925                                  StringRef category,
1926                                  StringRef str, SourceLocation Loc,
1927                                  SourceRange* RBeg, unsigned NumRanges) {
1928
1929  // 'BT' is owned by BugReporter.
1930  BugType *BT = getBugTypeForName(name, category);
1931  FullSourceLoc L = getContext().getFullLoc(Loc);
1932  BugReport *R = new BugReport(*BT, str, L);
1933  for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1934  EmitReport(R);
1935}
1936
1937BugType *BugReporter::getBugTypeForName(StringRef name,
1938                                        StringRef category) {
1939  llvm::SmallString<136> fullDesc;
1940  llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1941  llvm::StringMapEntry<BugType *> &
1942      entry = StrBugTypes.GetOrCreateValue(fullDesc);
1943  BugType *BT = entry.getValue();
1944  if (!BT) {
1945    BT = new BugType(name, category);
1946    entry.setValue(BT);
1947  }
1948  return BT;
1949}
1950