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