blob: e13d9c9a0a00d364b2f64f6914903cc91472742e [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"
Ted Kremenek9b663712011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenek681bc112011-08-16 01:53:41 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek53500662009-07-22 17:55:28 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000025
26//===----------------------------------------------------------------------===//
27// Utility functions.
28//===----------------------------------------------------------------------===//
29
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000030const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000031 // Pattern match for a few useful cases (do something smarter later):
32 // a[0], p->f, *p
33 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Ted Kremenek53500662009-07-22 17:55:28 +000035 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +000036 if (U->getOpcode() == UO_Deref)
Ted Kremenek53500662009-07-22 17:55:28 +000037 return U->getSubExpr()->IgnoreParenCasts();
38 }
39 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
40 return ME->getBase()->IgnoreParenCasts();
41 }
42 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
Ted Kremenek53500662009-07-22 17:55:28 +000043 return AE->getBase();
44 }
Mike Stump1eb44332009-09-09 15:08:12 +000045
46 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000047}
48
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000049const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000050 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000051 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
52 return BE->getRHS();
53 return NULL;
54}
55
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000056const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000057 // Callee is checked as a PreVisit to the CallExpr.
58 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000059 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
60 return CE->getCallee();
61 return NULL;
62}
63
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000064const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000065 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
66 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
67 return RS->getRetValue();
68 return NULL;
69}
70
71//===----------------------------------------------------------------------===//
72// Definitions for bug reporter visitors.
73//===----------------------------------------------------------------------===//
74
75namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000076class FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek53500662009-07-22 17:55:28 +000077 const MemRegion *R;
78 SVal V;
79 bool satisfied;
Zhongxing Xuc5619d92009-08-06 01:32:16 +000080 const ExplodedNode *StoreSite;
Ted Kremenek53500662009-07-22 17:55:28 +000081public:
82 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
83 : R(r), V(v), satisfied(false), StoreSite(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000084
Ted Kremenek1b431022010-03-20 18:01:57 +000085 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
86 static int tag = 0;
87 ID.AddPointer(&tag);
88 ID.AddPointer(R);
89 ID.Add(V);
90 }
91
Ted Kremenek9c378f72011-08-12 23:37:29 +000092 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000093 const ExplodedNode *PrevN,
Ted Kremenek9c378f72011-08-12 23:37:29 +000094 BugReporterContext &BRC) {
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenek53500662009-07-22 17:55:28 +000096 if (satisfied)
97 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +000098
99 if (!StoreSite) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000100 const ExplodedNode *Node = N, *Last = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek53500662009-07-22 17:55:28 +0000102 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000103
Ted Kremenek53500662009-07-22 17:55:28 +0000104 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
105 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
106 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
107 if (DS->getSingleDecl() == VR->getDecl()) {
108 Last = Node;
109 break;
110 }
111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek13976632010-02-08 16:18:51 +0000113 if (Node->getState()->getSVal(R) != V)
Ted Kremenek53500662009-07-22 17:55:28 +0000114 break;
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenek53500662009-07-22 17:55:28 +0000117 if (!Node || !Last) {
118 satisfied = true;
119 return NULL;
120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek53500662009-07-22 17:55:28 +0000122 StoreSite = Last;
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek53500662009-07-22 17:55:28 +0000125 if (StoreSite != N)
126 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek53500662009-07-22 17:55:28 +0000128 satisfied = true;
Ted Kremenek1b431022010-03-20 18:01:57 +0000129 llvm::SmallString<256> sbuf;
130 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek53500662009-07-22 17:55:28 +0000132 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
133 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek53500662009-07-22 17:55:28 +0000135 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000136 os << "Variable '" << VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000137 }
138 else
139 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek53500662009-07-22 17:55:28 +0000141 if (isa<loc::ConcreteInt>(V)) {
142 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000143 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000144 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000145 if (TR->getValueType()->isObjCObjectPointerType()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000146 os << "initialized to nil";
147 b = true;
148 }
149 }
150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Ted Kremenek53500662009-07-22 17:55:28 +0000152 if (!b)
153 os << "initialized to a null pointer value";
154 }
155 else if (isa<nonloc::ConcreteInt>(V)) {
156 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
157 }
158 else if (V.isUndef()) {
159 if (isa<VarRegion>(R)) {
160 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
161 if (VD->getInit())
162 os << "initialized to a garbage value";
163 else
Mike Stump1eb44332009-09-09 15:08:12 +0000164 os << "declared without an initial value";
165 }
Ted Kremenek53500662009-07-22 17:55:28 +0000166 }
167 }
168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169
170 if (os.str().empty()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000171 if (isa<loc::ConcreteInt>(V)) {
172 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000173 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000174 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000175 if (TR->getValueType()->isObjCObjectPointerType()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000176 os << "nil object reference stored to ";
177 b = true;
178 }
179 }
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek53500662009-07-22 17:55:28 +0000182 if (!b)
183 os << "Null pointer value stored to ";
184 }
185 else if (V.isUndef()) {
186 os << "Uninitialized value stored to ";
187 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000188 else if (isa<nonloc::ConcreteInt>(V)) {
189 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
190 << " is assigned to ";
191 }
Ted Kremenek53500662009-07-22 17:55:28 +0000192 else
193 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek53500662009-07-22 17:55:28 +0000195 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000196 os << '\'' << VR->getDecl() << '\'';
Ted Kremenek53500662009-07-22 17:55:28 +0000197 }
198 else
199 return NULL;
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek53500662009-07-22 17:55:28 +0000202 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000203 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000204 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek53500662009-07-22 17:55:28 +0000206 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000207 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000208 S = BSrc->getTerminatorCondition();
209 }
210 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
211 S = PS->getStmt();
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek53500662009-07-22 17:55:28 +0000214 if (!S)
215 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek53500662009-07-22 17:55:28 +0000217 // Construct a new PathDiagnosticPiece.
218 PathDiagnosticLocation L(S, BRC.getSourceManager());
219 return new PathDiagnosticEventPiece(L, os.str());
220 }
221};
222
223
Ted Kremenek9c378f72011-08-12 23:37:29 +0000224static void registerFindLastStore(BugReporterContext &BRC, const MemRegion *R,
Ted Kremenek53500662009-07-22 17:55:28 +0000225 SVal V) {
226 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
227}
228
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000229class TrackConstraintBRVisitor : public BugReporterVisitor {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000230 DefinedSVal Constraint;
Ted Kremenek53500662009-07-22 17:55:28 +0000231 const bool Assumption;
232 bool isSatisfied;
233public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000234 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
Ted Kremenek53500662009-07-22 17:55:28 +0000235 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenek1b431022010-03-20 18:01:57 +0000237 void Profile(llvm::FoldingSetNodeID &ID) const {
238 static int tag = 0;
239 ID.AddPointer(&tag);
240 ID.AddBoolean(Assumption);
241 ID.Add(Constraint);
242 }
243
Ted Kremenek9c378f72011-08-12 23:37:29 +0000244 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000245 const ExplodedNode *PrevN,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000246 BugReporterContext &BRC) {
Ted Kremenek53500662009-07-22 17:55:28 +0000247 if (isSatisfied)
248 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenek53500662009-07-22 17:55:28 +0000250 // Check if in the previous state it was feasible for this constraint
251 // to *not* be true.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000252 if (PrevN->getState()->assume(Constraint, !Assumption)) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000253
Ted Kremenek53500662009-07-22 17:55:28 +0000254 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremenek53500662009-07-22 17:55:28 +0000256 // As a sanity check, make sure that the negation of the constraint
257 // was infeasible in the current state. If it is feasible, we somehow
258 // missed the transition point.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000259 if (N->getState()->assume(Constraint, !Assumption))
Ted Kremenek53500662009-07-22 17:55:28 +0000260 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek53500662009-07-22 17:55:28 +0000262 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000263 // pretty-print the constraint. (work-in-progress)
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000264 std::string sbuf;
265 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek53500662009-07-22 17:55:28 +0000267 if (isa<Loc>(Constraint)) {
268 os << "Assuming pointer value is ";
269 os << (Assumption ? "non-null" : "null");
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek53500662009-07-22 17:55:28 +0000272 if (os.str().empty())
273 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek53500662009-07-22 17:55:28 +0000275 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000276 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000277 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek53500662009-07-22 17:55:28 +0000279 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000280 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000281 S = BSrc->getTerminatorCondition();
282 }
283 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
284 S = PS->getStmt();
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremenek53500662009-07-22 17:55:28 +0000287 if (!S)
288 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek53500662009-07-22 17:55:28 +0000290 // Construct a new PathDiagnosticPiece.
291 PathDiagnosticLocation L(S, BRC.getSourceManager());
292 return new PathDiagnosticEventPiece(L, os.str());
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek53500662009-07-22 17:55:28 +0000295 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000296 }
Ted Kremenek53500662009-07-22 17:55:28 +0000297};
298} // end anonymous namespace
299
Ted Kremenek9c378f72011-08-12 23:37:29 +0000300static void registerTrackConstraint(BugReporterContext &BRC,
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000301 DefinedSVal Constraint,
Ted Kremenek53500662009-07-22 17:55:28 +0000302 bool Assumption) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000304}
305
Ted Kremenek9c378f72011-08-12 23:37:29 +0000306void bugreporter::registerTrackNullOrUndefValue(BugReporterContext &BRC,
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000307 const void *data,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000308 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Ted Kremenek592362b2009-08-18 01:05:30 +0000310 const Stmt *S = static_cast<const Stmt*>(data);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek53500662009-07-22 17:55:28 +0000312 if (!S)
313 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000315 ProgramStateManager &StateMgr = BRC.getStateManager();
Ted Kremenek88299892011-07-28 23:07:59 +0000316
317 // Walk through nodes until we get one that matches the statement
318 // exactly.
319 while (N) {
320 const ProgramPoint &pp = N->getLocation();
321 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
322 if (ps->getStmt() == S)
323 break;
324 }
325 N = *N->pred_begin();
326 }
327
328 if (!N)
329 return;
330
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000331 const ProgramState *state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek892697d2010-12-16 07:46:53 +0000333 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000334 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
335 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000336 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000337 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek53500662009-07-22 17:55:28 +0000339 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000340 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000341
342 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000343 || V.isUndef()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000344 ::registerFindLastStore(BRC, R, V);
Ted Kremenek53500662009-07-22 17:55:28 +0000345 }
346 }
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek13976632010-02-08 16:18:51 +0000349 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek53500662009-07-22 17:55:28 +0000351 // Uncomment this to find cases where we aren't properly getting the
352 // base value that was dereferenced.
353 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek53500662009-07-22 17:55:28 +0000355 // Is it a symbolic value?
356 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
357 const SubRegion *R = cast<SubRegion>(L->getRegion());
358 while (R && !isa<SymbolicRegion>(R)) {
359 R = dyn_cast<SubRegion>(R->getSuperRegion());
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek53500662009-07-22 17:55:28 +0000362 if (R) {
363 assert(isa<SymbolicRegion>(R));
364 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
365 }
366 }
367}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000368
Ted Kremenek9c378f72011-08-12 23:37:29 +0000369void bugreporter::registerFindLastStore(BugReporterContext &BRC,
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000370 const void *data,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000371 const ExplodedNode *N) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000372
373 const MemRegion *R = static_cast<const MemRegion*>(data);
374
375 if (!R)
376 return;
377
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000378 const ProgramState *state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000379 SVal V = state->getSVal(R);
380
381 if (V.isUnknown())
382 return;
383
384 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
385}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000386
387
388namespace {
389class NilReceiverVisitor : public BugReporterVisitor {
390public:
391 NilReceiverVisitor() {}
392
393 void Profile(llvm::FoldingSetNodeID &ID) const {
394 static int x = 0;
395 ID.AddPointer(&x);
396 }
397
Ted Kremenek9c378f72011-08-12 23:37:29 +0000398 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
Ted Kremenekff7f7362010-03-20 18:02:01 +0000399 const ExplodedNode *PrevN,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000400 BugReporterContext &BRC) {
Ted Kremenekff7f7362010-03-20 18:02:01 +0000401
402 const PostStmt *P = N->getLocationAs<PostStmt>();
403 if (!P)
404 return 0;
405 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
406 if (!ME)
407 return 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +0000408 const Expr *Receiver = ME->getInstanceReceiver();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000409 if (!Receiver)
410 return 0;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000411 const ProgramState *state = N->getState();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000412 const SVal &V = state->getSVal(Receiver);
413 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
414 if (!DV)
415 return 0;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000416 state = state->assume(*DV, true);
Ted Kremenekff7f7362010-03-20 18:02:01 +0000417 if (state)
418 return 0;
419
420 // The receiver was nil, and hence the method was skipped.
421 // Register a BugReporterVisitor to issue a message telling us how
422 // the receiver was null.
423 bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N);
424 // Issue a message saying that the method was skipped.
425 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
426 return new PathDiagnosticEventPiece(L, "No method actually called "
427 "because the receiver is nil");
428 }
429};
430} // end anonymous namespace
431
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000432void bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) {
Ted Kremenekff7f7362010-03-20 18:02:01 +0000433 BRC.addVisitor(new NilReceiverVisitor());
434}
Tom Care2bbbe502010-09-02 23:30:22 +0000435
436// Registers every VarDecl inside a Stmt with a last store vistor.
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000437void bugreporter::registerVarDeclsLastStore(BugReporterContext &BRC,
Tom Care2bbbe502010-09-02 23:30:22 +0000438 const void *stmt,
439 const ExplodedNode *N) {
440 const Stmt *S = static_cast<const Stmt *>(stmt);
441
442 std::deque<const Stmt *> WorkList;
443
444 WorkList.push_back(S);
445
446 while (!WorkList.empty()) {
447 const Stmt *Head = WorkList.front();
448 WorkList.pop_front();
449
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000450 ProgramStateManager &StateMgr = BRC.getStateManager();
451 const ProgramState *state = N->getState();
Tom Care2bbbe502010-09-02 23:30:22 +0000452
453 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
454 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
455 const VarRegion *R =
456 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
457
458 // What did we load?
459 SVal V = state->getSVal(S);
460
461 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
462 ::registerFindLastStore(BRC, R, V);
463 }
464 }
465 }
466
467 for (Stmt::const_child_iterator I = Head->child_begin();
468 I != Head->child_end(); ++I)
469 WorkList.push_back(*I);
470 }
471}
Ted Kremenek993124e2011-08-06 06:54:45 +0000472
473//===----------------------------------------------------------------------===//
474// Visitor that tries to report interesting diagnostics from conditions.
475//===----------------------------------------------------------------------===//
476
477namespace {
478class ConditionVisitor : public BugReporterVisitor {
479public:
480 void Profile(llvm::FoldingSetNodeID &ID) const {
481 static int x = 0;
482 ID.AddPointer(&x);
483 }
484
485 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
486 const ExplodedNode *Prev,
487 BugReporterContext &BRC);
488
489 PathDiagnosticPiece *VisitTerminator(const Stmt *Term,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000490 const ProgramState *CurrentState,
491 const ProgramState *PrevState,
Ted Kremenek993124e2011-08-06 06:54:45 +0000492 const CFGBlock *srcBlk,
493 const CFGBlock *dstBlk,
494 BugReporterContext &BRC);
495
496 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
497 bool tookTrue,
498 BugReporterContext &BRC);
499
500 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
501 const DeclRefExpr *DR,
502 const bool tookTrue,
503 BugReporterContext &BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000504
505 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
506 const BinaryOperator *BExpr,
507 const bool tookTrue,
508 BugReporterContext &BRC);
509
510 void patternMatch(const Expr *Ex,
511 llvm::raw_ostream &Out,
512 BugReporterContext &BRC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000513};
514}
515
516PathDiagnosticPiece *ConditionVisitor::VisitNode(const ExplodedNode *N,
517 const ExplodedNode *Prev,
518 BugReporterContext &BRC) {
519
520 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000521
522 const ProgramState *CurrentState = N->getState();
523 const ProgramState *PrevState = Prev->getState();
524
525 // Compare the GDMs of the state, because that is where constraints
526 // are managed. Note that ensure that we only look at nodes that
527 // were generated by the analyzer engine proper, not checkers.
528 if (CurrentState->getGDM().getRoot() ==
529 PrevState->getGDM().getRoot())
530 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000531
532 // If an assumption was made on a branch, it should be caught
533 // here by looking at the state transition.
534 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000535 const CFGBlock *srcBlk = BE->getSrc();
536 if (const Stmt *term = srcBlk->getTerminator())
537 return VisitTerminator(term, CurrentState, PrevState,
538 srcBlk, BE->getDst(), BRC);
539 return 0;
540 }
541
542 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
543 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
544 // violation.
545 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
546 cast<GRBugReporter>(BRC.getBugReporter()).
547 getEngine().getEagerlyAssumeTags();
548
549 const ProgramPointTag *tag = PS->getTag();
550 if (tag == tags.first)
551 return VisitTrueTest(cast<Expr>(PS->getStmt()), true, BRC);
552 if (tag == tags.second)
553 return VisitTrueTest(cast<Expr>(PS->getStmt()), false, BRC);
554
Ted Kremenek993124e2011-08-06 06:54:45 +0000555 return 0;
556 }
557
558 return 0;
559}
560
561PathDiagnosticPiece *
562ConditionVisitor::VisitTerminator(const Stmt *Term,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000563 const ProgramState *CurrentState,
564 const ProgramState *PrevState,
Ted Kremenek993124e2011-08-06 06:54:45 +0000565 const CFGBlock *srcBlk,
566 const CFGBlock *dstBlk,
567 BugReporterContext &BRC) {
568
Ted Kremenek993124e2011-08-06 06:54:45 +0000569 const Expr *Cond = 0;
570
571 switch (Term->getStmtClass()) {
572 default:
573 return 0;
574 case Stmt::IfStmtClass:
575 Cond = cast<IfStmt>(Term)->getCond();
576 break;
577 case Stmt::ConditionalOperatorClass:
578 Cond = cast<ConditionalOperator>(Term)->getCond();
579 break;
580 }
581
582 assert(Cond);
583 assert(srcBlk->succ_size() == 2);
584 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
585 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
586 tookTrue, BRC);
587}
588
589PathDiagnosticPiece *
590ConditionVisitor::VisitTrueTest(const Expr *Cond,
591 bool tookTrue,
592 BugReporterContext &BRC) {
593
594 const Expr *Ex = Cond;
595
Ted Kremenek681bc112011-08-16 01:53:41 +0000596 while (true) {
597 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000598 switch (Ex->getStmtClass()) {
599 default:
600 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000601 case Stmt::BinaryOperatorClass:
602 return VisitTrueTest(Cond, cast<BinaryOperator>(Cond), tookTrue, BRC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000603 case Stmt::DeclRefExprClass:
604 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC);
605 case Stmt::UnaryOperatorClass: {
606 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
607 if (UO->getOpcode() == UO_LNot) {
608 tookTrue = !tookTrue;
609 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
610 continue;
611 }
612 return 0;
613 }
614 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000615 }
616}
617
618void ConditionVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
619 BugReporterContext &BRC) {
620 const Expr *OriginalExpr = Ex;
621 Ex = Ex->IgnoreParenCasts();
622
623 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
624 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
625 Out << VD->getDeclName().getAsString();
626 return;
627 }
628
629 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
630 QualType OriginalTy = OriginalExpr->getType();
631 if (OriginalTy->isPointerType()) {
632 if (IL->getValue() == 0) {
633 Out << "null";
634 return;
635 }
636 }
637 else if (OriginalTy->isObjCObjectPointerType()) {
638 if (IL->getValue() == 0) {
639 Out << "nil";
640 return;
641 }
642 }
643
644 Out << IL->getValue();
645 return;
646 }
647}
648
649PathDiagnosticPiece *
650ConditionVisitor::VisitTrueTest(const Expr *Cond,
651 const BinaryOperator *BExpr,
652 const bool tookTrue,
653 BugReporterContext &BRC) {
654
655 llvm::SmallString<128> LhsString, RhsString;
656 {
657 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
658 patternMatch(BExpr->getLHS(), OutLHS, BRC);
659 patternMatch(BExpr->getRHS(), OutRHS, BRC);
660 }
661
662 if (LhsString.empty() || RhsString.empty())
663 return 0;
664
665 llvm::SmallString<256> buf;
666 llvm::raw_svector_ostream Out(buf);
667 Out << "Assuming " << LhsString << " is ";
668
669 // Do we need to invert the opcode?
670 BinaryOperator::Opcode Op = BExpr->getOpcode();
671
672 if (!tookTrue)
673 switch (Op) {
674 case BO_EQ: Op = BO_NE; break;
675 case BO_NE: Op = BO_EQ; break;
676 case BO_LT: Op = BO_GE; break;
677 case BO_GT: Op = BO_LE; break;
678 case BO_LE: Op = BO_GT; break;
679 case BO_GE: Op = BO_GE; break;
680 default:
681 return 0;
682 }
683
684 switch (BExpr->getOpcode()) {
685 case BO_EQ:
686 Out << "equal to ";
687 break;
688 case BO_NE:
689 Out << "not equal to ";
690 break;
691 default:
692 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
693 break;
694 }
695
696 Out << RhsString;
697
698 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
699 return new PathDiagnosticEventPiece(Loc, Out.str());
Ted Kremenek993124e2011-08-06 06:54:45 +0000700}
701
702PathDiagnosticPiece *
703ConditionVisitor::VisitTrueTest(const Expr *Cond,
704 const DeclRefExpr *DR,
705 const bool tookTrue,
706 BugReporterContext &BRC) {
707
708 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
709 if (!VD)
710 return 0;
711
712 llvm::SmallString<256> Buf;
713 llvm::raw_svector_ostream Out(Buf);
714
715 Out << "Assuming '";
716 VD->getDeclName().printName(Out);
717 Out << "' is ";
718
719 QualType VDTy = VD->getType();
720
721 if (VDTy->isPointerType())
722 Out << (tookTrue ? "non-null" : "null");
723 else if (VDTy->isObjCObjectPointerType())
724 Out << (tookTrue ? "non-nil" : "nil");
725 else if (VDTy->isScalarType())
726 Out << (tookTrue ? "not equal to 0" : "0");
727 else
728 return 0;
729
730 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
731 return new PathDiagnosticEventPiece(Loc, Out.str());
732}
733
734void bugreporter::registerConditionVisitor(BugReporterContext &BRC) {
735 BRC.addVisitor(new ConditionVisitor());
736}
737