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(
94           EndPathNode->getLocationContext()->getDecl()->getBodyRBrace(),
95           BRC.getSourceManager());
96     }
97   }
98 
99   if (!L.isValid()) {
100     const Stmt *S = BR.getStmt();
101 
102     if (!S)
103       return NULL;
104 
105     L = PathDiagnosticLocation(S, BRC.getSourceManager());
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());
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());
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   return new PathDiagnosticEventPiece(L, "No method actually called "
429       "because the receiver is nil");
430 }
431 
432 // Registers every VarDecl inside a Stmt with a last store visitor.
433 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
434                                                        const Stmt *S) {
435   const ExplodedNode *N = BR.getErrorNode();
436   std::deque<const Stmt *> WorkList;
437   WorkList.push_back(S);
438 
439   while (!WorkList.empty()) {
440     const Stmt *Head = WorkList.front();
441     WorkList.pop_front();
442 
443     const ProgramState *state = N->getState();
444     ProgramStateManager &StateMgr = state->getStateManager();
445 
446     if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
447       if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
448         const VarRegion *R =
449         StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
450 
451         // What did we load?
452         SVal V = state->getSVal(S);
453 
454         if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
455           // Register a new visitor with the BugReport.
456           BR.addVisitor(new FindLastStoreBRVisitor(V, R));
457         }
458       }
459     }
460 
461     for (Stmt::const_child_iterator I = Head->child_begin();
462         I != Head->child_end(); ++I)
463       WorkList.push_back(*I);
464   }
465 }
466 
467 //===----------------------------------------------------------------------===//
468 // Visitor that tries to report interesting diagnostics from conditions.
469 //===----------------------------------------------------------------------===//
470 PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
471                                                    const ExplodedNode *Prev,
472                                                    BugReporterContext &BRC,
473                                                    BugReport &BR) {
474 
475   const ProgramPoint &progPoint = N->getLocation();
476 
477   const ProgramState *CurrentState = N->getState();
478   const ProgramState *PrevState = Prev->getState();
479 
480   // Compare the GDMs of the state, because that is where constraints
481   // are managed.  Note that ensure that we only look at nodes that
482   // were generated by the analyzer engine proper, not checkers.
483   if (CurrentState->getGDM().getRoot() ==
484       PrevState->getGDM().getRoot())
485     return 0;
486 
487   // If an assumption was made on a branch, it should be caught
488   // here by looking at the state transition.
489   if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
490     const CFGBlock *srcBlk = BE->getSrc();
491     if (const Stmt *term = srcBlk->getTerminator())
492       return VisitTerminator(term, CurrentState, PrevState,
493                              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, BRC);
507     if (tag == tags.second)
508       return VisitTrueTest(cast<Expr>(PS->getStmt()), false, BRC);
509 
510     return 0;
511   }
512 
513   return 0;
514 }
515 
516 PathDiagnosticPiece *
517 ConditionBRVisitor::VisitTerminator(const Stmt *Term,
518                                     const ProgramState *CurrentState,
519                                     const ProgramState *PrevState,
520                                     const CFGBlock *srcBlk,
521                                     const CFGBlock *dstBlk,
522                                     BugReporterContext &BRC) {
523   const Expr *Cond = 0;
524 
525   switch (Term->getStmtClass()) {
526   default:
527     return 0;
528   case Stmt::IfStmtClass:
529     Cond = cast<IfStmt>(Term)->getCond();
530     break;
531   case Stmt::ConditionalOperatorClass:
532     Cond = cast<ConditionalOperator>(Term)->getCond();
533     break;
534   }
535 
536   assert(Cond);
537   assert(srcBlk->succ_size() == 2);
538   const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
539   return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
540                        tookTrue, BRC);
541 }
542 
543 PathDiagnosticPiece *
544 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
545                                   bool tookTrue,
546                                   BugReporterContext &BRC) {
547 
548   const Expr *Ex = Cond;
549 
550   while (true) {
551     Ex = Ex->IgnoreParens();
552     switch (Ex->getStmtClass()) {
553       default:
554         return 0;
555       case Stmt::BinaryOperatorClass:
556         return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC);
557       case Stmt::DeclRefExprClass:
558         return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC);
559       case Stmt::UnaryOperatorClass: {
560         const UnaryOperator *UO = cast<UnaryOperator>(Ex);
561         if (UO->getOpcode() == UO_LNot) {
562           tookTrue = !tookTrue;
563           Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
564           continue;
565         }
566         return 0;
567       }
568     }
569   }
570 }
571 
572 bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
573                                       BugReporterContext &BRC) {
574   const Expr *OriginalExpr = Ex;
575   Ex = Ex->IgnoreParenCasts();
576 
577   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
578     const bool quotes = isa<VarDecl>(DR->getDecl());
579     if (quotes)
580       Out << '\'';
581     Out << DR->getDecl()->getDeclName().getAsString();
582     if (quotes)
583       Out << '\'';
584     return quotes;
585   }
586 
587   if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
588     QualType OriginalTy = OriginalExpr->getType();
589     if (OriginalTy->isPointerType()) {
590       if (IL->getValue() == 0) {
591         Out << "null";
592         return false;
593       }
594     }
595     else if (OriginalTy->isObjCObjectPointerType()) {
596       if (IL->getValue() == 0) {
597         Out << "nil";
598         return false;
599       }
600     }
601 
602     Out << IL->getValue();
603     return false;
604   }
605 
606   return false;
607 }
608 
609 PathDiagnosticPiece *
610 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
611                                   const BinaryOperator *BExpr,
612                                   const bool tookTrue,
613                                   BugReporterContext &BRC) {
614 
615   bool shouldInvert = false;
616 
617   llvm::SmallString<128> LhsString, RhsString;
618   {
619     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
620     const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
621     const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
622 
623     shouldInvert = !isVarLHS && isVarRHS;
624   }
625 
626   if (LhsString.empty() || RhsString.empty())
627     return 0;
628 
629   // Should we invert the strings if the LHS is not a variable name?
630 
631   llvm::SmallString<256> buf;
632   llvm::raw_svector_ostream Out(buf);
633   Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
634 
635   // Do we need to invert the opcode?
636   BinaryOperator::Opcode Op = BExpr->getOpcode();
637 
638   if (shouldInvert)
639     switch (Op) {
640       default: break;
641       case BO_LT: Op = BO_GT; break;
642       case BO_GT: Op = BO_LT; break;
643       case BO_LE: Op = BO_GE; break;
644       case BO_GE: Op = BO_LE; break;
645     }
646 
647   if (!tookTrue)
648     switch (Op) {
649       case BO_EQ: Op = BO_NE; break;
650       case BO_NE: Op = BO_EQ; break;
651       case BO_LT: Op = BO_GE; break;
652       case BO_GT: Op = BO_LE; break;
653       case BO_LE: Op = BO_GT; break;
654       case BO_GE: Op = BO_LT; break;
655       default:
656         return 0;
657     }
658 
659   switch (BExpr->getOpcode()) {
660     case BO_EQ:
661       Out << "equal to ";
662       break;
663     case BO_NE:
664       Out << "not equal to ";
665       break;
666     default:
667       Out << BinaryOperator::getOpcodeStr(Op) << ' ';
668       break;
669   }
670 
671   Out << (shouldInvert ? LhsString : RhsString);
672 
673   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
674   return new PathDiagnosticEventPiece(Loc, Out.str());
675 }
676 
677 PathDiagnosticPiece *
678 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
679                                   const DeclRefExpr *DR,
680                                   const bool tookTrue,
681                                   BugReporterContext &BRC) {
682 
683   const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
684   if (!VD)
685     return 0;
686 
687   llvm::SmallString<256> Buf;
688   llvm::raw_svector_ostream Out(Buf);
689 
690   Out << "Assuming '";
691   VD->getDeclName().printName(Out);
692   Out << "' is ";
693 
694   QualType VDTy = VD->getType();
695 
696   if (VDTy->isPointerType())
697     Out << (tookTrue ? "non-null" : "null");
698   else if (VDTy->isObjCObjectPointerType())
699     Out << (tookTrue ? "non-nil" : "nil");
700   else if (VDTy->isScalarType())
701     Out << (tookTrue ? "not equal to 0" : "0");
702   else
703     return 0;
704 
705   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
706   return new PathDiagnosticEventPiece(Loc, Out.str());
707 }
708