blob: f56d0538325c3d4f66dc8a8e7e85ed65de1f8ed9 [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//===----------------------------------------------------------------------===//
14
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprObjC.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000017#include "clang/GR/BugReporter/BugReporter.h"
18#include "clang/GR/BugReporter/PathDiagnostic.h"
19#include "clang/GR/PathSensitive/ExplodedGraph.h"
20#include "clang/GR/PathSensitive/GRState.h"
Ted Kremenek53500662009-07-22 17:55:28 +000021
22using namespace clang;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000023using namespace GR;
Ted Kremenek53500662009-07-22 17:55:28 +000024
25//===----------------------------------------------------------------------===//
26// Utility functions.
27//===----------------------------------------------------------------------===//
28
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000029const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000030 // Pattern match for a few useful cases (do something smarter later):
31 // a[0], p->f, *p
32 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000033
Ted Kremenek53500662009-07-22 17:55:28 +000034 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +000035 if (U->getOpcode() == UO_Deref)
Ted Kremenek53500662009-07-22 17:55:28 +000036 return U->getSubExpr()->IgnoreParenCasts();
37 }
38 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
39 return ME->getBase()->IgnoreParenCasts();
40 }
41 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
42 // Retrieve the base for arrays since BasicStoreManager doesn't know how
43 // to reason about them.
44 return AE->getBase();
45 }
Mike Stump1eb44332009-09-09 15:08:12 +000046
47 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000048}
49
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000050const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000051 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000052 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
53 return BE->getRHS();
54 return NULL;
55}
56
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000057const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000058 // Callee is checked as a PreVisit to the CallExpr.
59 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000060 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
61 return CE->getCallee();
62 return NULL;
63}
64
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000065const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000066 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
76namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000077class FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek53500662009-07-22 17:55:28 +000078 const MemRegion *R;
79 SVal V;
80 bool satisfied;
Zhongxing Xuc5619d92009-08-06 01:32:16 +000081 const ExplodedNode *StoreSite;
Ted Kremenek53500662009-07-22 17:55:28 +000082public:
83 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
84 : R(r), V(v), satisfied(false), StoreSite(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremenek1b431022010-03-20 18:01:57 +000086 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
87 static int tag = 0;
88 ID.AddPointer(&tag);
89 ID.AddPointer(R);
90 ID.Add(V);
91 }
92
Zhongxing Xuc5619d92009-08-06 01:32:16 +000093 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
94 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +000095 BugReporterContext& BRC) {
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremenek53500662009-07-22 17:55:28 +000097 if (satisfied)
98 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +000099
100 if (!StoreSite) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000101 const ExplodedNode *Node = N, *Last = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek53500662009-07-22 17:55:28 +0000103 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000104
Ted Kremenek53500662009-07-22 17:55:28 +0000105 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
106 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
107 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
108 if (DS->getSingleDecl() == VR->getDecl()) {
109 Last = Node;
110 break;
111 }
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek13976632010-02-08 16:18:51 +0000114 if (Node->getState()->getSVal(R) != V)
Ted Kremenek53500662009-07-22 17:55:28 +0000115 break;
116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremenek53500662009-07-22 17:55:28 +0000118 if (!Node || !Last) {
119 satisfied = true;
120 return NULL;
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenek53500662009-07-22 17:55:28 +0000123 StoreSite = Last;
124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek53500662009-07-22 17:55:28 +0000126 if (StoreSite != N)
127 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek53500662009-07-22 17:55:28 +0000129 satisfied = true;
Ted Kremenek1b431022010-03-20 18:01:57 +0000130 llvm::SmallString<256> sbuf;
131 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek53500662009-07-22 17:55:28 +0000133 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
134 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek53500662009-07-22 17:55:28 +0000136 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000137 os << "Variable '" << VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000138 }
139 else
140 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek53500662009-07-22 17:55:28 +0000142 if (isa<loc::ConcreteInt>(V)) {
143 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000144 if (R->isBoundable()) {
145 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000146 if (TR->getValueType()->isObjCObjectPointerType()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000147 os << "initialized to nil";
148 b = true;
149 }
150 }
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek53500662009-07-22 17:55:28 +0000153 if (!b)
154 os << "initialized to a null pointer value";
155 }
156 else if (isa<nonloc::ConcreteInt>(V)) {
157 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
158 }
159 else if (V.isUndef()) {
160 if (isa<VarRegion>(R)) {
161 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
162 if (VD->getInit())
163 os << "initialized to a garbage value";
164 else
Mike Stump1eb44332009-09-09 15:08:12 +0000165 os << "declared without an initial value";
166 }
Ted Kremenek53500662009-07-22 17:55:28 +0000167 }
168 }
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
171 if (os.str().empty()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000172 if (isa<loc::ConcreteInt>(V)) {
173 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000174 if (R->isBoundable()) {
175 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000176 if (TR->getValueType()->isObjCObjectPointerType()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000177 os << "nil object reference stored to ";
178 b = true;
179 }
180 }
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek53500662009-07-22 17:55:28 +0000183 if (!b)
184 os << "Null pointer value stored to ";
185 }
186 else if (V.isUndef()) {
187 os << "Uninitialized value stored to ";
188 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000189 else if (isa<nonloc::ConcreteInt>(V)) {
190 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
191 << " is assigned to ";
192 }
Ted Kremenek53500662009-07-22 17:55:28 +0000193 else
194 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek53500662009-07-22 17:55:28 +0000196 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000197 os << '\'' << VR->getDecl() << '\'';
Ted Kremenek53500662009-07-22 17:55:28 +0000198 }
199 else
200 return NULL;
201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek53500662009-07-22 17:55:28 +0000203 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000204 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000205 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek53500662009-07-22 17:55:28 +0000207 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000208 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000209 S = BSrc->getTerminatorCondition();
210 }
211 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
212 S = PS->getStmt();
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek53500662009-07-22 17:55:28 +0000215 if (!S)
216 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek53500662009-07-22 17:55:28 +0000218 // Construct a new PathDiagnosticPiece.
219 PathDiagnosticLocation L(S, BRC.getSourceManager());
220 return new PathDiagnosticEventPiece(L, os.str());
221 }
222};
223
224
225static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
226 SVal V) {
227 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
228}
229
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000230class TrackConstraintBRVisitor : public BugReporterVisitor {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000231 DefinedSVal Constraint;
Ted Kremenek53500662009-07-22 17:55:28 +0000232 const bool Assumption;
233 bool isSatisfied;
234public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000235 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
Ted Kremenek53500662009-07-22 17:55:28 +0000236 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek1b431022010-03-20 18:01:57 +0000238 void Profile(llvm::FoldingSetNodeID &ID) const {
239 static int tag = 0;
240 ID.AddPointer(&tag);
241 ID.AddBoolean(Assumption);
242 ID.Add(Constraint);
243 }
244
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000245 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
246 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +0000247 BugReporterContext& BRC) {
248 if (isSatisfied)
249 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek53500662009-07-22 17:55:28 +0000251 // Check if in the previous state it was feasible for this constraint
252 // to *not* be true.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000253 if (PrevN->getState()->assume(Constraint, !Assumption)) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000254
Ted Kremenek53500662009-07-22 17:55:28 +0000255 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek53500662009-07-22 17:55:28 +0000257 // As a sanity check, make sure that the negation of the constraint
258 // was infeasible in the current state. If it is feasible, we somehow
259 // missed the transition point.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000260 if (N->getState()->assume(Constraint, !Assumption))
Ted Kremenek53500662009-07-22 17:55:28 +0000261 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek53500662009-07-22 17:55:28 +0000263 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000264 // pretty-print the constraint. (work-in-progress)
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000265 std::string sbuf;
266 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek53500662009-07-22 17:55:28 +0000268 if (isa<Loc>(Constraint)) {
269 os << "Assuming pointer value is ";
270 os << (Assumption ? "non-null" : "null");
271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenek53500662009-07-22 17:55:28 +0000273 if (os.str().empty())
274 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek53500662009-07-22 17:55:28 +0000276 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000277 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000278 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek53500662009-07-22 17:55:28 +0000280 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000281 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000282 S = BSrc->getTerminatorCondition();
283 }
284 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
285 S = PS->getStmt();
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek53500662009-07-22 17:55:28 +0000288 if (!S)
289 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek53500662009-07-22 17:55:28 +0000291 // Construct a new PathDiagnosticPiece.
292 PathDiagnosticLocation L(S, BRC.getSourceManager());
293 return new PathDiagnosticEventPiece(L, os.str());
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenek53500662009-07-22 17:55:28 +0000296 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000297 }
Ted Kremenek53500662009-07-22 17:55:28 +0000298};
299} // end anonymous namespace
300
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000301static void registerTrackConstraint(BugReporterContext& BRC,
302 DefinedSVal Constraint,
Ted Kremenek53500662009-07-22 17:55:28 +0000303 bool Assumption) {
Mike Stump1eb44332009-09-09 15:08:12 +0000304 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000305}
306
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000307void bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
308 const void *data,
309 const ExplodedNode* N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek592362b2009-08-18 01:05:30 +0000311 const Stmt *S = static_cast<const Stmt*>(data);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenek53500662009-07-22 17:55:28 +0000313 if (!S)
314 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek53500662009-07-22 17:55:28 +0000316 GRStateManager &StateMgr = BRC.getStateManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000317 const GRState *state = N->getState();
318
Ted Kremenek892697d2010-12-16 07:46:53 +0000319 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000320 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
321 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000322 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000323 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek53500662009-07-22 17:55:28 +0000325 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000326 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000327
328 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000329 || V.isUndef()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000330 ::registerFindLastStore(BRC, R, V);
Ted Kremenek53500662009-07-22 17:55:28 +0000331 }
332 }
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek13976632010-02-08 16:18:51 +0000335 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek53500662009-07-22 17:55:28 +0000337 // Uncomment this to find cases where we aren't properly getting the
338 // base value that was dereferenced.
339 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek53500662009-07-22 17:55:28 +0000341 // Is it a symbolic value?
342 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
343 const SubRegion *R = cast<SubRegion>(L->getRegion());
344 while (R && !isa<SymbolicRegion>(R)) {
345 R = dyn_cast<SubRegion>(R->getSuperRegion());
346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek53500662009-07-22 17:55:28 +0000348 if (R) {
349 assert(isa<SymbolicRegion>(R));
350 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
351 }
352 }
353}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000354
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000355void bugreporter::registerFindLastStore(BugReporterContext& BRC,
356 const void *data,
357 const ExplodedNode* N) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000358
359 const MemRegion *R = static_cast<const MemRegion*>(data);
360
361 if (!R)
362 return;
363
364 const GRState *state = N->getState();
365 SVal V = state->getSVal(R);
366
367 if (V.isUnknown())
368 return;
369
370 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
371}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000372
373
374namespace {
375class NilReceiverVisitor : public BugReporterVisitor {
376public:
377 NilReceiverVisitor() {}
378
379 void Profile(llvm::FoldingSetNodeID &ID) const {
380 static int x = 0;
381 ID.AddPointer(&x);
382 }
383
384 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
385 const ExplodedNode *PrevN,
386 BugReporterContext& BRC) {
387
388 const PostStmt *P = N->getLocationAs<PostStmt>();
389 if (!P)
390 return 0;
391 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
392 if (!ME)
393 return 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +0000394 const Expr *Receiver = ME->getInstanceReceiver();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000395 if (!Receiver)
396 return 0;
397 const GRState *state = N->getState();
398 const SVal &V = state->getSVal(Receiver);
399 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
400 if (!DV)
401 return 0;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000402 state = state->assume(*DV, true);
Ted Kremenekff7f7362010-03-20 18:02:01 +0000403 if (state)
404 return 0;
405
406 // The receiver was nil, and hence the method was skipped.
407 // Register a BugReporterVisitor to issue a message telling us how
408 // the receiver was null.
409 bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N);
410 // Issue a message saying that the method was skipped.
411 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
412 return new PathDiagnosticEventPiece(L, "No method actually called "
413 "because the receiver is nil");
414 }
415};
416} // end anonymous namespace
417
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000418void bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) {
Ted Kremenekff7f7362010-03-20 18:02:01 +0000419 BRC.addVisitor(new NilReceiverVisitor());
420}
Tom Care2bbbe502010-09-02 23:30:22 +0000421
422// Registers every VarDecl inside a Stmt with a last store vistor.
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000423void bugreporter::registerVarDeclsLastStore(BugReporterContext &BRC,
Tom Care2bbbe502010-09-02 23:30:22 +0000424 const void *stmt,
425 const ExplodedNode *N) {
426 const Stmt *S = static_cast<const Stmt *>(stmt);
427
428 std::deque<const Stmt *> WorkList;
429
430 WorkList.push_back(S);
431
432 while (!WorkList.empty()) {
433 const Stmt *Head = WorkList.front();
434 WorkList.pop_front();
435
436 GRStateManager &StateMgr = BRC.getStateManager();
437 const GRState *state = N->getState();
438
439 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
440 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
441 const VarRegion *R =
442 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
443
444 // What did we load?
445 SVal V = state->getSVal(S);
446
447 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
448 ::registerFindLastStore(BRC, R, V);
449 }
450 }
451 }
452
453 for (Stmt::const_child_iterator I = Head->child_begin();
454 I != Head->child_end(); ++I)
455 WorkList.push_back(*I);
456 }
457}