blob: d17777714a2601af2bc1f695457c48ebedb25a33 [file] [log] [blame]
Ted Kremenek53500662009-07-22 17:55:28 +00001// 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//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +000014#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
Ted Kremenek53500662009-07-22 17:55:28 +000015
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprObjC.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenek681bc112011-08-16 01:53:41 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Ted Kremenek53500662009-07-22 17:55:28 +000024
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000027
28//===----------------------------------------------------------------------===//
29// Utility functions.
30//===----------------------------------------------------------------------===//
31
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000032const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000033 // Pattern match for a few useful cases (do something smarter later):
34 // a[0], p->f, *p
Jordan Rose3682f1e2012-08-25 01:06:23 +000035 const PostStmt *Loc = N->getLocationAs<PostStmt>();
36 if (!Loc)
37 return 0;
38
Jordan Rose364b9f92012-08-27 20:18:30 +000039 const Expr *S = dyn_cast<Expr>(Loc->getStmt());
40 if (!S)
41 return 0;
42 S = S->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +000043
Ted Kremenek11abcec2012-05-02 00:31:29 +000044 while (true) {
45 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
46 assert(B->isAssignmentOp());
47 S = B->getLHS()->IgnoreParenCasts();
48 continue;
49 }
50 else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
51 if (U->getOpcode() == UO_Deref)
52 return U->getSubExpr()->IgnoreParenCasts();
53 }
54 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
55 return ME->getBase()->IgnoreParenCasts();
56 }
57 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
58 return AE->getBase();
59 }
60 break;
Ted Kremenek53500662009-07-22 17:55:28 +000061 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
63 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000064}
65
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000066const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000067 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000068 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
69 return BE->getRHS();
70 return NULL;
71}
72
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000073const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000074 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
75 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
76 return RS->getRetValue();
77 return NULL;
78}
79
80//===----------------------------------------------------------------------===//
81// Definitions for bug reporter visitors.
82//===----------------------------------------------------------------------===//
Anna Zaks23f395e2011-08-20 01:27:22 +000083
84PathDiagnosticPiece*
85BugReporterVisitor::getEndPath(BugReporterContext &BRC,
86 const ExplodedNode *EndPathNode,
87 BugReport &BR) {
88 return 0;
89}
90
91PathDiagnosticPiece*
92BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
93 const ExplodedNode *EndPathNode,
94 BugReport &BR) {
Anna Zaks5a0917d2012-02-16 03:41:01 +000095 PathDiagnosticLocation L =
96 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
Anna Zaks23f395e2011-08-20 01:27:22 +000097
98 BugReport::ranges_iterator Beg, End;
99 llvm::tie(Beg, End) = BR.getRanges();
100
101 // Only add the statement itself as a range if we didn't specify any
102 // special ranges for this report.
103 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
104 BR.getDescription(),
105 Beg == End);
106 for (; Beg != End; ++Beg)
107 P->addRange(*Beg);
108
109 return P;
110}
111
112
Anna Zaks50bbc162011-08-19 22:33:38 +0000113void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
114 static int tag = 0;
115 ID.AddPointer(&tag);
116 ID.AddPointer(R);
117 ID.Add(V);
118}
Ted Kremenek53500662009-07-22 17:55:28 +0000119
Anna Zaks50bbc162011-08-19 22:33:38 +0000120PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
121 const ExplodedNode *PrevN,
122 BugReporterContext &BRC,
123 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Anna Zaks50bbc162011-08-19 22:33:38 +0000125 if (satisfied)
126 return NULL;
127
128 if (!StoreSite) {
Jordan Rose20165e72012-08-03 23:08:42 +0000129 // Make sure the region is actually bound to value V here.
130 // This is necessary because the region may not actually be live at the
131 // report's error node.
132 if (N->getState()->getSVal(R) != V)
133 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000134
Jordan Rose20165e72012-08-03 23:08:42 +0000135 const ExplodedNode *Node = N, *Last = N;
136
137 // Now look for the store of V.
Ted Kremenekd2e70902012-01-25 22:18:04 +0000138 for ( ; Node ; Node = Node->getFirstPred()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000139 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
140 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
141 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
142 if (DS->getSingleDecl() == VR->getDecl()) {
Ted Kremenekd2e70902012-01-25 22:18:04 +0000143 // Record the last seen initialization point.
Anna Zaks50bbc162011-08-19 22:33:38 +0000144 Last = Node;
145 break;
146 }
147 }
148
Ted Kremenekd2e70902012-01-25 22:18:04 +0000149 // Does the region still bind to value V? If not, we are done
150 // looking for store sites.
Anna Zaks50bbc162011-08-19 22:33:38 +0000151 if (Node->getState()->getSVal(R) != V)
152 break;
Jordan Rose20165e72012-08-03 23:08:42 +0000153
154 Last = Node;
Anna Zaks50bbc162011-08-19 22:33:38 +0000155 }
156
Jordan Rose20165e72012-08-03 23:08:42 +0000157 if (!Node) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000158 satisfied = true;
159 return NULL;
160 }
161
162 StoreSite = Last;
Ted Kremenek1b431022010-03-20 18:01:57 +0000163 }
164
Anna Zaks50bbc162011-08-19 22:33:38 +0000165 if (StoreSite != N)
166 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Anna Zaks50bbc162011-08-19 22:33:38 +0000168 satisfied = true;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000169 SmallString<256> sbuf;
Anna Zaks50bbc162011-08-19 22:33:38 +0000170 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Anna Zaks50bbc162011-08-19 22:33:38 +0000172 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
173 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Anna Zaks50bbc162011-08-19 22:33:38 +0000175 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000176 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000177 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000178 else
Ted Kremenek53500662009-07-22 17:55:28 +0000179 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek53500662009-07-22 17:55:28 +0000181 if (isa<loc::ConcreteInt>(V)) {
182 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000183 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000184 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000185 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000186 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000187 b = true;
188 }
189 }
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek53500662009-07-22 17:55:28 +0000192 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000193 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000194 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000195 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000196 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000197 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000198 else if (V.isUndef()) {
199 if (isa<VarRegion>(R)) {
200 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
201 if (VD->getInit())
202 os << "initialized to a garbage value";
203 else
204 os << "declared without an initial value";
205 }
Ted Kremenek53500662009-07-22 17:55:28 +0000206 }
Jordan Rose68537992012-08-03 23:09:01 +0000207 else {
208 os << "initialized here";
209 }
Ted Kremenek53500662009-07-22 17:55:28 +0000210 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000211 }
212
213 if (os.str().empty()) {
214 if (isa<loc::ConcreteInt>(V)) {
215 bool b = false;
216 if (R->isBoundable()) {
217 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
218 if (TR->getValueType()->isObjCObjectPointerType()) {
219 os << "nil object reference stored to ";
220 b = true;
221 }
222 }
223 }
224
225 if (!b)
226 os << "Null pointer value stored to ";
227 }
228 else if (V.isUndef()) {
229 os << "Uninitialized value stored to ";
230 }
231 else if (isa<nonloc::ConcreteInt>(V)) {
232 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
233 << " is assigned to ";
234 }
235 else
Jordan Rose68537992012-08-03 23:09:01 +0000236 os << "Value assigned to ";
Anna Zaks50bbc162011-08-19 22:33:38 +0000237
238 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000239 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000240 }
241 else
242 return NULL;
243 }
244
Anna Zaks50bbc162011-08-19 22:33:38 +0000245 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000246 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000247 PathDiagnosticLocation L =
248 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000249 if (!L.isValid())
250 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000251 return new PathDiagnosticEventPiece(L, os.str());
252}
253
254void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
255 static int tag = 0;
256 ID.AddPointer(&tag);
257 ID.AddBoolean(Assumption);
258 ID.Add(Constraint);
259}
260
261PathDiagnosticPiece *
262TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
263 const ExplodedNode *PrevN,
264 BugReporterContext &BRC,
265 BugReport &BR) {
266 if (isSatisfied)
267 return NULL;
268
269 // Check if in the previous state it was feasible for this constraint
270 // to *not* be true.
271 if (PrevN->getState()->assume(Constraint, !Assumption)) {
272
273 isSatisfied = true;
274
275 // As a sanity check, make sure that the negation of the constraint
276 // was infeasible in the current state. If it is feasible, we somehow
277 // missed the transition point.
278 if (N->getState()->assume(Constraint, !Assumption))
279 return NULL;
280
281 // We found the transition point for the constraint. We now need to
282 // pretty-print the constraint. (work-in-progress)
283 std::string sbuf;
284 llvm::raw_string_ostream os(sbuf);
285
286 if (isa<Loc>(Constraint)) {
287 os << "Assuming pointer value is ";
288 os << (Assumption ? "non-null" : "null");
289 }
290
291 if (os.str().empty())
292 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremenek53500662009-07-22 17:55:28 +0000294 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000295 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000296 PathDiagnosticLocation L =
297 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000298 if (!L.isValid())
299 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000300 return new PathDiagnosticEventPiece(L, os.str());
301 }
Ted Kremenek53500662009-07-22 17:55:28 +0000302
Anna Zaks50bbc162011-08-19 22:33:38 +0000303 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000304}
305
Jordan Rose23df2432012-08-24 16:34:31 +0000306namespace {
307class ReturnNullVisitor : public BugReporterVisitorImpl<ReturnNullVisitor> {
308 const ExplodedNode *ReturnNode;
309public:
310 ReturnNullVisitor(const ExplodedNode *N) : ReturnNode(N) {}
311
312 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
313 static int Tag = 0;
314 ID.AddPointer(&Tag);
315 ID.Add(*ReturnNode);
316 }
317
318 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
319 const ExplodedNode *PrevN,
320 BugReporterContext &BRC,
321 BugReport &BR) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000322 // Only print a message at the interesting return statement.
Jordan Rose23df2432012-08-24 16:34:31 +0000323 if (ReturnNode != BRC.getNodeResolver().getOriginalNode(N))
324 return 0;
325
326 StmtPoint SP = cast<StmtPoint>(ReturnNode->getLocation());
327 const ReturnStmt *Ret = cast<ReturnStmt>(SP.getStmt());
328 PathDiagnosticLocation L(Ret, BRC.getSourceManager(),
329 N->getLocationContext());
330
331 SmallString<64> Msg;
332 llvm::raw_svector_ostream Out(Msg);
333
334 if (Loc::isLocType(Ret->getRetValue()->getType()))
335 Out << "Returning null pointer";
336 else
337 Out << "Returning zero";
338
339 // FIXME: We should have a more generalized printing mechanism.
340 const Expr *RetE = Ret->getRetValue()->IgnoreParenCasts();
341 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetE))
342 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
343 Out << " (loaded from '" << *DD << "')";
344
345 return new PathDiagnosticEventPiece(L, Out.str());
346 }
347};
348} // end anonymous namespace
349
Jordan Rose68537992012-08-03 23:09:01 +0000350void bugreporter::addTrackNullOrUndefValueVisitor(const ExplodedNode *N,
351 const Stmt *S,
352 BugReport *report) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000353 if (!S || !N)
Jordan Rose68537992012-08-03 23:09:01 +0000354 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anna Zaks8e6431a2011-08-18 22:37:56 +0000356 ProgramStateManager &StateMgr = N->getState()->getStateManager();
357
Jordan Rose364b9f92012-08-27 20:18:30 +0000358 // Walk through nodes until we get one that matches the statement exactly.
Ted Kremenek88299892011-07-28 23:07:59 +0000359 while (N) {
360 const ProgramPoint &pp = N->getLocation();
361 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
362 if (ps->getStmt() == S)
363 break;
Jordan Rose23df2432012-08-24 16:34:31 +0000364 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) {
365 if (CEE->getCalleeContext()->getCallSite() == S)
366 break;
Ted Kremenek88299892011-07-28 23:07:59 +0000367 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000368 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000369 }
370
371 if (!N)
Jordan Rose68537992012-08-03 23:09:01 +0000372 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000373
Ted Kremenek8bef8232012-01-26 21:29:00 +0000374 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Jordan Rose364b9f92012-08-27 20:18:30 +0000376 // See if the expression we're interested refers to a variable.
377 // If so, we can track both its contents and constraints on its value.
378 if (const Expr *Ex = dyn_cast<Expr>(S)) {
379 // Strip off parens and casts. Note that this will never have issues with
380 // C++ user-defined implicit conversions, because those have a constructor
381 // or function call inside.
Jordan Rosee6cd0542012-08-16 00:03:33 +0000382 Ex = Ex->IgnoreParenCasts();
Ted Kremenek76aadc32012-03-09 01:13:14 +0000383 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000384 // FIXME: Right now we only track VarDecls because it's non-trivial to
385 // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812>
Ted Kremenek76aadc32012-03-09 01:13:14 +0000386 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
387 const VarRegion *R =
388 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Jordan Rose364b9f92012-08-27 20:18:30 +0000390 // Mark both the variable region and its contents as interesting.
Ted Kremenek11abcec2012-05-02 00:31:29 +0000391 SVal V = state->getRawSVal(loc::MemRegionVal(R));
Ted Kremenek76aadc32012-03-09 01:13:14 +0000392 report->markInteresting(R);
393 report->markInteresting(V);
Jordan Rose68537992012-08-03 23:09:01 +0000394
Jordan Rose364b9f92012-08-27 20:18:30 +0000395 // If the contents are symbolic, find out when they became null.
Jordan Rose68537992012-08-03 23:09:01 +0000396 if (V.getAsLocSymbol()) {
397 BugReporterVisitor *ConstraintTracker
398 = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false);
399 report->addVisitor(ConstraintTracker);
400 }
401
402 report->addVisitor(new FindLastStoreBRVisitor(V, R));
403 return;
Ted Kremenek53500662009-07-22 17:55:28 +0000404 }
405 }
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Jordan Rose364b9f92012-08-27 20:18:30 +0000408 // If the expression does NOT refer to a variable, we can still track
409 // constraints on its contents.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000410 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek53500662009-07-22 17:55:28 +0000412 // Uncomment this to find cases where we aren't properly getting the
413 // base value that was dereferenced.
414 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek53500662009-07-22 17:55:28 +0000416 // Is it a symbolic value?
417 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000418 const MemRegion *Base = L->getRegion()->getBaseRegion();
419 if (isa<SymbolicRegion>(Base)) {
420 report->markInteresting(Base);
421 report->addVisitor(new TrackConstraintBRVisitor(loc::MemRegionVal(Base),
Jordan Rose68537992012-08-03 23:09:01 +0000422 false));
Ted Kremenek53500662009-07-22 17:55:28 +0000423 }
Jordan Rose364b9f92012-08-27 20:18:30 +0000424 } else if (isa<loc::ConcreteInt>(V)) {
425 // Otherwise, if it's a null constant, we want to know where it came from.
426 // In particular, if it came from an inlined function call,
427 // we need to make sure that function isn't pruned in our output.
428
Jordan Rose23df2432012-08-24 16:34:31 +0000429 // Walk backwards to just before the post-statement checks.
430 ProgramPoint PP = N->getLocation();
431 while (N && isa<PostStmt>(PP = N->getLocation()))
432 N = N->getFirstPred();
433
Jordan Rose364b9f92012-08-27 20:18:30 +0000434 // If we found an inlined call before the post-statement checks, look
435 // for a return statement within the call.
Jordan Rose23df2432012-08-24 16:34:31 +0000436 if (N && isa<CallExitEnd>(PP)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000437 // Walk backwards within the inlined function until we find a statement.
438 // This will look through multiple levels of inlining, but stop if the
439 // last thing that happened was an empty function being inlined.
440 // FIXME: This may not work in the presence of destructors.
Jordan Rose23df2432012-08-24 16:34:31 +0000441 do {
442 N = N->getFirstPred();
443 PP = N->getLocation();
444 } while (!isa<StmtPoint>(PP) && !isa<CallEnter>(PP));
445
Jordan Rose364b9f92012-08-27 20:18:30 +0000446 // If the last statement we found is a 'return <expr>', make sure we
447 // show this return...and recursively track the value being returned.
Jordan Rose23df2432012-08-24 16:34:31 +0000448 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) {
449 if (const ReturnStmt *Ret = SP->getStmtAs<ReturnStmt>()) {
450 if (const Expr *RetE = Ret->getRetValue()) {
451 report->addVisitor(new ReturnNullVisitor(N));
452 addTrackNullOrUndefValueVisitor(N, RetE, report);
453 }
454 }
455 }
456 }
Ted Kremenek53500662009-07-22 17:55:28 +0000457 }
458}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000459
Anna Zaks50bbc162011-08-19 22:33:38 +0000460BugReporterVisitor *
461FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
462 const MemRegion *R) {
463 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000464
Ted Kremenek8bef8232012-01-26 21:29:00 +0000465 ProgramStateRef state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000466 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000467 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000468 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000469
Anna Zaks50bbc162011-08-19 22:33:38 +0000470 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000471}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000472
473
Anna Zaks50bbc162011-08-19 22:33:38 +0000474PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
475 const ExplodedNode *PrevN,
476 BugReporterContext &BRC,
477 BugReport &BR) {
478 const PostStmt *P = N->getLocationAs<PostStmt>();
479 if (!P)
480 return 0;
481 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
482 if (!ME)
483 return 0;
484 const Expr *Receiver = ME->getInstanceReceiver();
485 if (!Receiver)
486 return 0;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000487 ProgramStateRef state = N->getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000488 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000489 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
490 if (!DV)
491 return 0;
492 state = state->assume(*DV, true);
493 if (state)
494 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000495
Anna Zaks50bbc162011-08-19 22:33:38 +0000496 // The receiver was nil, and hence the method was skipped.
497 // Register a BugReporterVisitor to issue a message telling us how
498 // the receiver was null.
Jordan Rose68537992012-08-03 23:09:01 +0000499 bugreporter::addTrackNullOrUndefValueVisitor(N, Receiver, &BR);
Anna Zaks50bbc162011-08-19 22:33:38 +0000500 // Issue a message saying that the method was skipped.
Anna Zaks220ac8c2011-09-15 01:08:34 +0000501 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
502 N->getLocationContext());
Anna Zaks907344e2012-04-05 02:10:19 +0000503 return new PathDiagnosticEventPiece(L, "No method is called "
Anna Zaks50bbc162011-08-19 22:33:38 +0000504 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000505}
Tom Care2bbbe502010-09-02 23:30:22 +0000506
Anna Zaks8e6431a2011-08-18 22:37:56 +0000507// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000508void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
509 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000510 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000511 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000512 WorkList.push_back(S);
513
514 while (!WorkList.empty()) {
515 const Stmt *Head = WorkList.front();
516 WorkList.pop_front();
517
Ted Kremenek8bef8232012-01-26 21:29:00 +0000518 ProgramStateRef state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000519 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000520
521 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
522 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
523 const VarRegion *R =
524 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
525
526 // What did we load?
Ted Kremenek5eca4822012-01-06 22:09:28 +0000527 SVal V = state->getSVal(S, N->getLocationContext());
Tom Care2bbbe502010-09-02 23:30:22 +0000528
529 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000530 // Register a new visitor with the BugReport.
531 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000532 }
533 }
534 }
535
536 for (Stmt::const_child_iterator I = Head->child_begin();
537 I != Head->child_end(); ++I)
538 WorkList.push_back(*I);
539 }
540}
Ted Kremenek993124e2011-08-06 06:54:45 +0000541
542//===----------------------------------------------------------------------===//
543// Visitor that tries to report interesting diagnostics from conditions.
544//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000545PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
546 const ExplodedNode *Prev,
547 BugReporterContext &BRC,
548 BugReport &BR) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000549 PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR);
550 if (PathDiagnosticEventPiece *ev =
551 dyn_cast_or_null<PathDiagnosticEventPiece>(piece))
Ted Kremenek76aadc32012-03-09 01:13:14 +0000552 ev->setPrunable(true, /* override */ false);
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000553 return piece;
554}
555
556PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
557 const ExplodedNode *Prev,
558 BugReporterContext &BRC,
559 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000560
561 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000562
Ted Kremenek8bef8232012-01-26 21:29:00 +0000563 ProgramStateRef CurrentState = N->getState();
564 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000565
566 // Compare the GDMs of the state, because that is where constraints
567 // are managed. Note that ensure that we only look at nodes that
568 // were generated by the analyzer engine proper, not checkers.
569 if (CurrentState->getGDM().getRoot() ==
570 PrevState->getGDM().getRoot())
571 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000572
573 // If an assumption was made on a branch, it should be caught
574 // here by looking at the state transition.
575 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000576 const CFGBlock *srcBlk = BE->getSrc();
577 if (const Stmt *term = srcBlk->getTerminator())
Ted Kremenek76aadc32012-03-09 01:13:14 +0000578 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000579 return 0;
580 }
581
582 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
583 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
584 // violation.
585 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
586 cast<GRBugReporter>(BRC.getBugReporter()).
587 getEngine().getEagerlyAssumeTags();
588
589 const ProgramPointTag *tag = PS->getTag();
590 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000591 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000592 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000593 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000594 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000595 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000596
Ted Kremenek993124e2011-08-06 06:54:45 +0000597 return 0;
598 }
599
600 return 0;
601}
602
603PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000604ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000605 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000606 const CFGBlock *srcBlk,
607 const CFGBlock *dstBlk,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000608 BugReport &R,
Anna Zaks50bbc162011-08-19 22:33:38 +0000609 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000610 const Expr *Cond = 0;
611
612 switch (Term->getStmtClass()) {
613 default:
614 return 0;
615 case Stmt::IfStmtClass:
616 Cond = cast<IfStmt>(Term)->getCond();
617 break;
618 case Stmt::ConditionalOperatorClass:
619 Cond = cast<ConditionalOperator>(Term)->getCond();
620 break;
621 }
622
623 assert(Cond);
624 assert(srcBlk->succ_size() == 2);
625 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
626 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
Ted Kremenek76aadc32012-03-09 01:13:14 +0000627 tookTrue, BRC, R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000628}
629
630PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000631ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
632 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000633 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000634 BugReport &R,
635 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000636
637 const Expr *Ex = Cond;
638
Ted Kremenek681bc112011-08-16 01:53:41 +0000639 while (true) {
640 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000641 switch (Ex->getStmtClass()) {
642 default:
643 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000644 case Stmt::BinaryOperatorClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000645 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
646 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000647 case Stmt::DeclRefExprClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000648 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
649 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000650 case Stmt::UnaryOperatorClass: {
651 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
652 if (UO->getOpcode() == UO_LNot) {
653 tookTrue = !tookTrue;
654 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
655 continue;
656 }
657 return 0;
658 }
659 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000660 }
661}
662
Anna Zaks50bbc162011-08-19 22:33:38 +0000663bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000664 BugReporterContext &BRC,
665 BugReport &report,
666 const ExplodedNode *N,
667 llvm::Optional<bool> &prunable) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000668 const Expr *OriginalExpr = Ex;
669 Ex = Ex->IgnoreParenCasts();
670
671 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000672 const bool quotes = isa<VarDecl>(DR->getDecl());
Ted Kremenek76aadc32012-03-09 01:13:14 +0000673 if (quotes) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000674 Out << '\'';
Ted Kremenek76aadc32012-03-09 01:13:14 +0000675 const LocationContext *LCtx = N->getLocationContext();
676 const ProgramState *state = N->getState().getPtr();
677 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
678 LCtx).getAsRegion()) {
679 if (report.isInteresting(R))
680 prunable = false;
681 else {
682 const ProgramState *state = N->getState().getPtr();
683 SVal V = state->getSVal(R);
684 if (report.isInteresting(V))
685 prunable = false;
686 }
687 }
688 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000689 Out << DR->getDecl()->getDeclName().getAsString();
690 if (quotes)
691 Out << '\'';
692 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000693 }
694
695 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
696 QualType OriginalTy = OriginalExpr->getType();
697 if (OriginalTy->isPointerType()) {
698 if (IL->getValue() == 0) {
699 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000700 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000701 }
702 }
703 else if (OriginalTy->isObjCObjectPointerType()) {
704 if (IL->getValue() == 0) {
705 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000706 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000707 }
708 }
709
710 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000711 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000712 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000713
714 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000715}
716
717PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000718ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
719 const BinaryOperator *BExpr,
720 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000721 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000722 BugReport &R,
723 const ExplodedNode *N) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000724
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000725 bool shouldInvert = false;
Ted Kremenek76aadc32012-03-09 01:13:14 +0000726 llvm::Optional<bool> shouldPrune;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000727
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000728 SmallString<128> LhsString, RhsString;
Ted Kremenek681bc112011-08-16 01:53:41 +0000729 {
Ted Kremenek76aadc32012-03-09 01:13:14 +0000730 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
731 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
732 shouldPrune);
733 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
734 shouldPrune);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000735
736 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000737 }
738
Ted Kremenekd1247c52012-01-04 08:18:09 +0000739 BinaryOperator::Opcode Op = BExpr->getOpcode();
740
741 if (BinaryOperator::isAssignmentOp(Op)) {
742 // For assignment operators, all that we care about is that the LHS
743 // evaluates to "true" or "false".
744 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000745 BRC, R, N);
Ted Kremenekd1247c52012-01-04 08:18:09 +0000746 }
747
748 // For non-assignment operations, we require that we can understand
749 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000750 if (LhsString.empty() || RhsString.empty())
751 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000752
Ted Kremenekd1247c52012-01-04 08:18:09 +0000753 // Should we invert the strings if the LHS is not a variable name?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000754 SmallString<256> buf;
Ted Kremenek681bc112011-08-16 01:53:41 +0000755 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000756 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000757
758 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000759 if (shouldInvert)
760 switch (Op) {
761 default: break;
762 case BO_LT: Op = BO_GT; break;
763 case BO_GT: Op = BO_LT; break;
764 case BO_LE: Op = BO_GE; break;
765 case BO_GE: Op = BO_LE; break;
766 }
767
Ted Kremenek681bc112011-08-16 01:53:41 +0000768 if (!tookTrue)
769 switch (Op) {
770 case BO_EQ: Op = BO_NE; break;
771 case BO_NE: Op = BO_EQ; break;
772 case BO_LT: Op = BO_GE; break;
773 case BO_GT: Op = BO_LE; break;
774 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000775 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000776 default:
777 return 0;
778 }
779
Ted Kremenek6ae32572011-12-20 22:00:25 +0000780 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000781 case BO_EQ:
782 Out << "equal to ";
783 break;
784 case BO_NE:
785 Out << "not equal to ";
786 break;
787 default:
788 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
789 break;
790 }
791
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000792 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek76aadc32012-03-09 01:13:14 +0000793 const LocationContext *LCtx = N->getLocationContext();
794 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
795 PathDiagnosticEventPiece *event =
796 new PathDiagnosticEventPiece(Loc, Out.str());
797 if (shouldPrune.hasValue())
798 event->setPrunable(shouldPrune.getValue());
799 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000800}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000801
802PathDiagnosticPiece *
803ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
804 const Expr *CondVarExpr,
805 const bool tookTrue,
806 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000807 BugReport &report,
808 const ExplodedNode *N) {
Jordan Roseb0e1bad2012-08-03 23:08:54 +0000809 // FIXME: If there's already a constraint tracker for this variable,
810 // we shouldn't emit anything here (c.f. the double note in
811 // test/Analysis/inlining/path-notes.c)
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000812 SmallString<256> buf;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000813 llvm::raw_svector_ostream Out(buf);
814 Out << "Assuming " << LhsString << " is ";
815
816 QualType Ty = CondVarExpr->getType();
817
818 if (Ty->isPointerType())
819 Out << (tookTrue ? "not null" : "null");
820 else if (Ty->isObjCObjectPointerType())
821 Out << (tookTrue ? "not nil" : "nil");
822 else if (Ty->isBooleanType())
823 Out << (tookTrue ? "true" : "false");
824 else if (Ty->isIntegerType())
825 Out << (tookTrue ? "non-zero" : "zero");
826 else
827 return 0;
828
Ted Kremenek76aadc32012-03-09 01:13:14 +0000829 const LocationContext *LCtx = N->getLocationContext();
830 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
831 PathDiagnosticEventPiece *event =
832 new PathDiagnosticEventPiece(Loc, Out.str());
833
834 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
835 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
836 const ProgramState *state = N->getState().getPtr();
837 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
838 if (report.isInteresting(R))
839 event->setPrunable(false);
840 }
841 }
842 }
843
844 return event;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000845}
Ted Kremenek993124e2011-08-06 06:54:45 +0000846
847PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000848ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
849 const DeclRefExpr *DR,
850 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000851 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000852 BugReport &report,
853 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000854
855 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
856 if (!VD)
857 return 0;
858
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000859 SmallString<256> Buf;
Ted Kremenek993124e2011-08-06 06:54:45 +0000860 llvm::raw_svector_ostream Out(Buf);
861
862 Out << "Assuming '";
863 VD->getDeclName().printName(Out);
864 Out << "' is ";
865
866 QualType VDTy = VD->getType();
867
868 if (VDTy->isPointerType())
869 Out << (tookTrue ? "non-null" : "null");
870 else if (VDTy->isObjCObjectPointerType())
871 Out << (tookTrue ? "non-nil" : "nil");
872 else if (VDTy->isScalarType())
873 Out << (tookTrue ? "not equal to 0" : "0");
874 else
875 return 0;
876
Ted Kremenek76aadc32012-03-09 01:13:14 +0000877 const LocationContext *LCtx = N->getLocationContext();
878 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
879 PathDiagnosticEventPiece *event =
880 new PathDiagnosticEventPiece(Loc, Out.str());
881
882 const ProgramState *state = N->getState().getPtr();
883 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
884 if (report.isInteresting(R))
885 event->setPrunable(false);
886 else {
887 SVal V = state->getSVal(R);
888 if (report.isInteresting(V))
889 event->setPrunable(false);
890 }
891 }
892 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000893}
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000894