1 // BugReporterVisitors.cpp - Helpers for reporting 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 a set of BugReporter "visitors" which can be used to
11 //  enhance the diagnostics reported for a bug.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
15 
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprObjC.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
23 #include "llvm/ADT/SmallString.h"
24 
25 using namespace clang;
26 using namespace ento;
27 
28 //===----------------------------------------------------------------------===//
29 // Utility functions.
30 //===----------------------------------------------------------------------===//
31 
32 const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
33   // Pattern match for a few useful cases (do something smarter later):
34   //   a[0], p->f, *p
35   const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
36 
37   while (true) {
38     if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
39       assert(B->isAssignmentOp());
40       S = B->getLHS()->IgnoreParenCasts();
41       continue;
42     }
43     else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
44       if (U->getOpcode() == UO_Deref)
45         return U->getSubExpr()->IgnoreParenCasts();
46     }
47     else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
48       return ME->getBase()->IgnoreParenCasts();
49     }
50     else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
51       return AE->getBase();
52     }
53     break;
54   }
55 
56   return NULL;
57 }
58 
59 const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
60   const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
61   if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
62     return BE->getRHS();
63   return NULL;
64 }
65 
66 const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
67   // Callee is checked as a PreVisit to the CallExpr.
68   const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
69   if (const CallExpr *CE = dyn_cast<CallExpr>(S))
70     return CE->getCallee();
71   return NULL;
72 }
73 
74 const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
75   const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
76   if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
77     return RS->getRetValue();
78   return NULL;
79 }
80 
81 //===----------------------------------------------------------------------===//
82 // Definitions for bug reporter visitors.
83 //===----------------------------------------------------------------------===//
84 
85 PathDiagnosticPiece*
86 BugReporterVisitor::getEndPath(BugReporterContext &BRC,
87                                const ExplodedNode *EndPathNode,
88                                BugReport &BR) {
89   return 0;
90 }
91 
92 PathDiagnosticPiece*
93 BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
94                                       const ExplodedNode *EndPathNode,
95                                       BugReport &BR) {
96   PathDiagnosticLocation L =
97     PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
98 
99   BugReport::ranges_iterator Beg, End;
100   llvm::tie(Beg, End) = BR.getRanges();
101 
102   // Only add the statement itself as a range if we didn't specify any
103   // special ranges for this report.
104   PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
105       BR.getDescription(),
106       Beg == End);
107   for (; Beg != End; ++Beg)
108     P->addRange(*Beg);
109 
110   return P;
111 }
112 
113 
114 void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
115   static int tag = 0;
116   ID.AddPointer(&tag);
117   ID.AddPointer(R);
118   ID.Add(V);
119 }
120 
121 PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
122                                                      const ExplodedNode *PrevN,
123                                                      BugReporterContext &BRC,
124                                                      BugReport &BR) {
125 
126   if (satisfied)
127     return NULL;
128 
129   if (!StoreSite) {
130     const ExplodedNode *Node = N, *Last = NULL;
131 
132     for ( ; Node ; Node = Node->getFirstPred()) {
133 
134       if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
135         if (const PostStmt *P = Node->getLocationAs<PostStmt>())
136           if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
137             if (DS->getSingleDecl() == VR->getDecl()) {
138               // Record the last seen initialization point.
139               Last = Node;
140               break;
141             }
142       }
143 
144       // Does the region still bind to value V?  If not, we are done
145       // looking for store sites.
146       if (Node->getState()->getSVal(R) != V)
147         break;
148     }
149 
150     if (!Node || !Last) {
151       satisfied = true;
152       return NULL;
153     }
154 
155     StoreSite = Last;
156   }
157 
158   if (StoreSite != N)
159     return NULL;
160 
161   satisfied = true;
162   SmallString<256> sbuf;
163   llvm::raw_svector_ostream os(sbuf);
164 
165   if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
166     if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
167 
168       if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
169         os << "Variable '" << *VR->getDecl() << "' ";
170       }
171       else
172         return NULL;
173 
174       if (isa<loc::ConcreteInt>(V)) {
175         bool b = false;
176         if (R->isBoundable()) {
177           if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
178             if (TR->getValueType()->isObjCObjectPointerType()) {
179               os << "initialized to nil";
180               b = true;
181             }
182           }
183         }
184 
185         if (!b)
186           os << "initialized to a null pointer value";
187       }
188       else if (isa<nonloc::ConcreteInt>(V)) {
189         os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
190       }
191       else if (V.isUndef()) {
192         if (isa<VarRegion>(R)) {
193           const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
194           if (VD->getInit())
195             os << "initialized to a garbage value";
196           else
197             os << "declared without an initial value";
198         }
199       }
200     }
201   }
202 
203   if (os.str().empty()) {
204     if (isa<loc::ConcreteInt>(V)) {
205       bool b = false;
206       if (R->isBoundable()) {
207         if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
208           if (TR->getValueType()->isObjCObjectPointerType()) {
209             os << "nil object reference stored to ";
210             b = true;
211           }
212         }
213       }
214 
215       if (!b)
216         os << "Null pointer value stored to ";
217     }
218     else if (V.isUndef()) {
219       os << "Uninitialized value stored to ";
220     }
221     else if (isa<nonloc::ConcreteInt>(V)) {
222       os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
223                << " is assigned to ";
224     }
225     else
226       return NULL;
227 
228     if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
229       os << '\'' << *VR->getDecl() << '\'';
230     }
231     else
232       return NULL;
233   }
234 
235   // Construct a new PathDiagnosticPiece.
236   ProgramPoint P = N->getLocation();
237   PathDiagnosticLocation L =
238     PathDiagnosticLocation::create(P, BRC.getSourceManager());
239   if (!L.isValid())
240     return NULL;
241   return new PathDiagnosticEventPiece(L, os.str());
242 }
243 
244 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
245   static int tag = 0;
246   ID.AddPointer(&tag);
247   ID.AddBoolean(Assumption);
248   ID.Add(Constraint);
249 }
250 
251 PathDiagnosticPiece *
252 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
253                                     const ExplodedNode *PrevN,
254                                     BugReporterContext &BRC,
255                                     BugReport &BR) {
256   if (isSatisfied)
257     return NULL;
258 
259   // Check if in the previous state it was feasible for this constraint
260   // to *not* be true.
261   if (PrevN->getState()->assume(Constraint, !Assumption)) {
262 
263     isSatisfied = true;
264 
265     // As a sanity check, make sure that the negation of the constraint
266     // was infeasible in the current state.  If it is feasible, we somehow
267     // missed the transition point.
268     if (N->getState()->assume(Constraint, !Assumption))
269       return NULL;
270 
271     // We found the transition point for the constraint.  We now need to
272     // pretty-print the constraint. (work-in-progress)
273     std::string sbuf;
274     llvm::raw_string_ostream os(sbuf);
275 
276     if (isa<Loc>(Constraint)) {
277       os << "Assuming pointer value is ";
278       os << (Assumption ? "non-null" : "null");
279     }
280 
281     if (os.str().empty())
282       return NULL;
283 
284     // Construct a new PathDiagnosticPiece.
285     ProgramPoint P = N->getLocation();
286     PathDiagnosticLocation L =
287       PathDiagnosticLocation::create(P, BRC.getSourceManager());
288     if (!L.isValid())
289       return NULL;
290     return new PathDiagnosticEventPiece(L, os.str());
291   }
292 
293   return NULL;
294 }
295 
296 BugReporterVisitor *
297 bugreporter::getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
298                                              const Stmt *S,
299                                              BugReport *report) {
300   if (!S || !N)
301     return 0;
302 
303   ProgramStateManager &StateMgr = N->getState()->getStateManager();
304 
305   // Walk through nodes until we get one that matches the statement
306   // exactly.
307   while (N) {
308     const ProgramPoint &pp = N->getLocation();
309     if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
310       if (ps->getStmt() == S)
311         break;
312     }
313     N = N->getFirstPred();
314   }
315 
316   if (!N)
317     return 0;
318 
319   ProgramStateRef state = N->getState();
320 
321   // Walk through lvalue-to-rvalue conversions.
322   const Expr *Ex = dyn_cast<Expr>(S);
323   if (Ex) {
324     Ex = Ex->IgnoreParenLValueCasts();
325     if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
326       if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
327         const VarRegion *R =
328           StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
329 
330         // What did we load?
331         SVal V = state->getRawSVal(loc::MemRegionVal(R));
332         report->markInteresting(R);
333         report->markInteresting(V);
334         return new FindLastStoreBRVisitor(V, R);
335       }
336     }
337   }
338 
339   SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
340 
341   // Uncomment this to find cases where we aren't properly getting the
342   // base value that was dereferenced.
343   // assert(!V.isUnknownOrUndef());
344 
345   // Is it a symbolic value?
346   if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
347     const SubRegion *R = cast<SubRegion>(L->getRegion());
348     while (R && !isa<SymbolicRegion>(R)) {
349       R = dyn_cast<SubRegion>(R->getSuperRegion());
350     }
351 
352     if (R) {
353       report->markInteresting(R);
354       return new TrackConstraintBRVisitor(loc::MemRegionVal(R), false);
355     }
356   }
357 
358   return 0;
359 }
360 
361 BugReporterVisitor *
362 FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
363                                             const MemRegion *R) {
364   assert(R && "The memory region is null.");
365 
366   ProgramStateRef state = N->getState();
367   SVal V = state->getSVal(R);
368   if (V.isUnknown())
369     return 0;
370 
371   return new FindLastStoreBRVisitor(V, R);
372 }
373 
374 
375 PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
376                                                      const ExplodedNode *PrevN,
377                                                      BugReporterContext &BRC,
378                                                      BugReport &BR) {
379   const PostStmt *P = N->getLocationAs<PostStmt>();
380   if (!P)
381     return 0;
382   const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
383   if (!ME)
384     return 0;
385   const Expr *Receiver = ME->getInstanceReceiver();
386   if (!Receiver)
387     return 0;
388   ProgramStateRef state = N->getState();
389   const SVal &V = state->getSVal(Receiver, N->getLocationContext());
390   const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
391   if (!DV)
392     return 0;
393   state = state->assume(*DV, true);
394   if (state)
395     return 0;
396 
397   // The receiver was nil, and hence the method was skipped.
398   // Register a BugReporterVisitor to issue a message telling us how
399   // the receiver was null.
400   BR.addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Receiver, &BR));
401   // Issue a message saying that the method was skipped.
402   PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
403                                      N->getLocationContext());
404   return new PathDiagnosticEventPiece(L, "No method is called "
405       "because the receiver is nil");
406 }
407 
408 // Registers every VarDecl inside a Stmt with a last store visitor.
409 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
410                                                        const Stmt *S) {
411   const ExplodedNode *N = BR.getErrorNode();
412   std::deque<const Stmt *> WorkList;
413   WorkList.push_back(S);
414 
415   while (!WorkList.empty()) {
416     const Stmt *Head = WorkList.front();
417     WorkList.pop_front();
418 
419     ProgramStateRef state = N->getState();
420     ProgramStateManager &StateMgr = state->getStateManager();
421 
422     if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
423       if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
424         const VarRegion *R =
425         StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
426 
427         // What did we load?
428         SVal V = state->getSVal(S, N->getLocationContext());
429 
430         if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
431           // Register a new visitor with the BugReport.
432           BR.addVisitor(new FindLastStoreBRVisitor(V, R));
433         }
434       }
435     }
436 
437     for (Stmt::const_child_iterator I = Head->child_begin();
438         I != Head->child_end(); ++I)
439       WorkList.push_back(*I);
440   }
441 }
442 
443 //===----------------------------------------------------------------------===//
444 // Visitor that tries to report interesting diagnostics from conditions.
445 //===----------------------------------------------------------------------===//
446 PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
447                                                    const ExplodedNode *Prev,
448                                                    BugReporterContext &BRC,
449                                                    BugReport &BR) {
450   PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR);
451   if (PathDiagnosticEventPiece *ev =
452       dyn_cast_or_null<PathDiagnosticEventPiece>(piece))
453     ev->setPrunable(true, /* override */ false);
454   return piece;
455 }
456 
457 PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
458                                                        const ExplodedNode *Prev,
459                                                        BugReporterContext &BRC,
460                                                        BugReport &BR) {
461 
462   const ProgramPoint &progPoint = N->getLocation();
463 
464   ProgramStateRef CurrentState = N->getState();
465   ProgramStateRef PrevState = Prev->getState();
466 
467   // Compare the GDMs of the state, because that is where constraints
468   // are managed.  Note that ensure that we only look at nodes that
469   // were generated by the analyzer engine proper, not checkers.
470   if (CurrentState->getGDM().getRoot() ==
471       PrevState->getGDM().getRoot())
472     return 0;
473 
474   // If an assumption was made on a branch, it should be caught
475   // here by looking at the state transition.
476   if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
477     const CFGBlock *srcBlk = BE->getSrc();
478     if (const Stmt *term = srcBlk->getTerminator())
479       return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
480     return 0;
481   }
482 
483   if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
484     // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
485     // violation.
486     const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
487       cast<GRBugReporter>(BRC.getBugReporter()).
488         getEngine().getEagerlyAssumeTags();
489 
490     const ProgramPointTag *tag = PS->getTag();
491     if (tag == tags.first)
492       return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
493                            BRC, BR, N);
494     if (tag == tags.second)
495       return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
496                            BRC, BR, N);
497 
498     return 0;
499   }
500 
501   return 0;
502 }
503 
504 PathDiagnosticPiece *
505 ConditionBRVisitor::VisitTerminator(const Stmt *Term,
506                                     const ExplodedNode *N,
507                                     const CFGBlock *srcBlk,
508                                     const CFGBlock *dstBlk,
509                                     BugReport &R,
510                                     BugReporterContext &BRC) {
511   const Expr *Cond = 0;
512 
513   switch (Term->getStmtClass()) {
514   default:
515     return 0;
516   case Stmt::IfStmtClass:
517     Cond = cast<IfStmt>(Term)->getCond();
518     break;
519   case Stmt::ConditionalOperatorClass:
520     Cond = cast<ConditionalOperator>(Term)->getCond();
521     break;
522   }
523 
524   assert(Cond);
525   assert(srcBlk->succ_size() == 2);
526   const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
527   return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
528                        tookTrue, BRC, R, N);
529 }
530 
531 PathDiagnosticPiece *
532 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
533                                   bool tookTrue,
534                                   BugReporterContext &BRC,
535                                   BugReport &R,
536                                   const ExplodedNode *N) {
537 
538   const Expr *Ex = Cond;
539 
540   while (true) {
541     Ex = Ex->IgnoreParens();
542     switch (Ex->getStmtClass()) {
543       default:
544         return 0;
545       case Stmt::BinaryOperatorClass:
546         return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
547                              R, N);
548       case Stmt::DeclRefExprClass:
549         return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
550                              R, N);
551       case Stmt::UnaryOperatorClass: {
552         const UnaryOperator *UO = cast<UnaryOperator>(Ex);
553         if (UO->getOpcode() == UO_LNot) {
554           tookTrue = !tookTrue;
555           Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
556           continue;
557         }
558         return 0;
559       }
560     }
561   }
562 }
563 
564 bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
565                                       BugReporterContext &BRC,
566                                       BugReport &report,
567                                       const ExplodedNode *N,
568                                       llvm::Optional<bool> &prunable) {
569   const Expr *OriginalExpr = Ex;
570   Ex = Ex->IgnoreParenCasts();
571 
572   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
573     const bool quotes = isa<VarDecl>(DR->getDecl());
574     if (quotes) {
575       Out << '\'';
576       const LocationContext *LCtx = N->getLocationContext();
577       const ProgramState *state = N->getState().getPtr();
578       if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
579                                                 LCtx).getAsRegion()) {
580         if (report.isInteresting(R))
581           prunable = false;
582         else {
583           const ProgramState *state = N->getState().getPtr();
584           SVal V = state->getSVal(R);
585           if (report.isInteresting(V))
586             prunable = false;
587         }
588       }
589     }
590     Out << DR->getDecl()->getDeclName().getAsString();
591     if (quotes)
592       Out << '\'';
593     return quotes;
594   }
595 
596   if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
597     QualType OriginalTy = OriginalExpr->getType();
598     if (OriginalTy->isPointerType()) {
599       if (IL->getValue() == 0) {
600         Out << "null";
601         return false;
602       }
603     }
604     else if (OriginalTy->isObjCObjectPointerType()) {
605       if (IL->getValue() == 0) {
606         Out << "nil";
607         return false;
608       }
609     }
610 
611     Out << IL->getValue();
612     return false;
613   }
614 
615   return false;
616 }
617 
618 PathDiagnosticPiece *
619 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
620                                   const BinaryOperator *BExpr,
621                                   const bool tookTrue,
622                                   BugReporterContext &BRC,
623                                   BugReport &R,
624                                   const ExplodedNode *N) {
625 
626   bool shouldInvert = false;
627   llvm::Optional<bool> shouldPrune;
628 
629   SmallString<128> LhsString, RhsString;
630   {
631     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
632     const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
633                                        shouldPrune);
634     const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
635                                        shouldPrune);
636 
637     shouldInvert = !isVarLHS && isVarRHS;
638   }
639 
640   BinaryOperator::Opcode Op = BExpr->getOpcode();
641 
642   if (BinaryOperator::isAssignmentOp(Op)) {
643     // For assignment operators, all that we care about is that the LHS
644     // evaluates to "true" or "false".
645     return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
646                                   BRC, R, N);
647   }
648 
649   // For non-assignment operations, we require that we can understand
650   // both the LHS and RHS.
651   if (LhsString.empty() || RhsString.empty())
652     return 0;
653 
654   // Should we invert the strings if the LHS is not a variable name?
655   SmallString<256> buf;
656   llvm::raw_svector_ostream Out(buf);
657   Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
658 
659   // Do we need to invert the opcode?
660   if (shouldInvert)
661     switch (Op) {
662       default: break;
663       case BO_LT: Op = BO_GT; break;
664       case BO_GT: Op = BO_LT; break;
665       case BO_LE: Op = BO_GE; break;
666       case BO_GE: Op = BO_LE; break;
667     }
668 
669   if (!tookTrue)
670     switch (Op) {
671       case BO_EQ: Op = BO_NE; break;
672       case BO_NE: Op = BO_EQ; break;
673       case BO_LT: Op = BO_GE; break;
674       case BO_GT: Op = BO_LE; break;
675       case BO_LE: Op = BO_GT; break;
676       case BO_GE: Op = BO_LT; break;
677       default:
678         return 0;
679     }
680 
681   switch (Op) {
682     case BO_EQ:
683       Out << "equal to ";
684       break;
685     case BO_NE:
686       Out << "not equal to ";
687       break;
688     default:
689       Out << BinaryOperator::getOpcodeStr(Op) << ' ';
690       break;
691   }
692 
693   Out << (shouldInvert ? LhsString : RhsString);
694   const LocationContext *LCtx = N->getLocationContext();
695   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
696   PathDiagnosticEventPiece *event =
697     new PathDiagnosticEventPiece(Loc, Out.str());
698   if (shouldPrune.hasValue())
699     event->setPrunable(shouldPrune.getValue());
700   return event;
701 }
702 
703 PathDiagnosticPiece *
704 ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
705                                            const Expr *CondVarExpr,
706                                            const bool tookTrue,
707                                            BugReporterContext &BRC,
708                                            BugReport &report,
709                                            const ExplodedNode *N) {
710   SmallString<256> buf;
711   llvm::raw_svector_ostream Out(buf);
712   Out << "Assuming " << LhsString << " is ";
713 
714   QualType Ty = CondVarExpr->getType();
715 
716   if (Ty->isPointerType())
717     Out << (tookTrue ? "not null" : "null");
718   else if (Ty->isObjCObjectPointerType())
719     Out << (tookTrue ? "not nil" : "nil");
720   else if (Ty->isBooleanType())
721     Out << (tookTrue ? "true" : "false");
722   else if (Ty->isIntegerType())
723     Out << (tookTrue ? "non-zero" : "zero");
724   else
725     return 0;
726 
727   const LocationContext *LCtx = N->getLocationContext();
728   PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
729   PathDiagnosticEventPiece *event =
730     new PathDiagnosticEventPiece(Loc, Out.str());
731 
732   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
733     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
734       const ProgramState *state = N->getState().getPtr();
735       if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
736         if (report.isInteresting(R))
737           event->setPrunable(false);
738       }
739     }
740   }
741 
742   return event;
743 }
744 
745 PathDiagnosticPiece *
746 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
747                                   const DeclRefExpr *DR,
748                                   const bool tookTrue,
749                                   BugReporterContext &BRC,
750                                   BugReport &report,
751                                   const ExplodedNode *N) {
752 
753   const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
754   if (!VD)
755     return 0;
756 
757   SmallString<256> Buf;
758   llvm::raw_svector_ostream Out(Buf);
759 
760   Out << "Assuming '";
761   VD->getDeclName().printName(Out);
762   Out << "' is ";
763 
764   QualType VDTy = VD->getType();
765 
766   if (VDTy->isPointerType())
767     Out << (tookTrue ? "non-null" : "null");
768   else if (VDTy->isObjCObjectPointerType())
769     Out << (tookTrue ? "non-nil" : "nil");
770   else if (VDTy->isScalarType())
771     Out << (tookTrue ? "not equal to 0" : "0");
772   else
773     return 0;
774 
775   const LocationContext *LCtx = N->getLocationContext();
776   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
777   PathDiagnosticEventPiece *event =
778     new PathDiagnosticEventPiece(Loc, Out.str());
779 
780   const ProgramState *state = N->getState().getPtr();
781   if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
782     if (report.isInteresting(R))
783       event->setPrunable(false);
784     else {
785       SVal V = state->getSVal(R);
786       if (report.isInteresting(V))
787         event->setPrunable(false);
788     }
789   }
790   return event;
791 }
792 
793