blob: b6e726fd0be5968b61bc9cd22708dc9838a798f9 [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,
Anna Zaks8e6431a2011-08-18 22:37:56 +000094 BugReporterContext &BRC,
95 BugReport &BR) {
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()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000145 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(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()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000175 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(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
Anna Zaks8e6431a2011-08-18 22:37:56 +0000225static void registerFindLastStore(BugReport &BR, const MemRegion *R,
Ted Kremenek53500662009-07-22 17:55:28 +0000226 SVal V) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000227 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Ted Kremenek53500662009-07-22 17:55:28 +0000228}
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
Ted Kremenek9c378f72011-08-12 23:37:29 +0000245 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000246 const ExplodedNode *PrevN,
Anna Zaks8e6431a2011-08-18 22:37:56 +0000247 BugReporterContext &BRC,
248 BugReport &BR) {
Ted Kremenek53500662009-07-22 17:55:28 +0000249 if (isSatisfied)
250 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek53500662009-07-22 17:55:28 +0000252 // Check if in the previous state it was feasible for this constraint
253 // to *not* be true.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000254 if (PrevN->getState()->assume(Constraint, !Assumption)) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000255
Ted Kremenek53500662009-07-22 17:55:28 +0000256 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenek53500662009-07-22 17:55:28 +0000258 // As a sanity check, make sure that the negation of the constraint
259 // was infeasible in the current state. If it is feasible, we somehow
260 // missed the transition point.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000261 if (N->getState()->assume(Constraint, !Assumption))
Ted Kremenek53500662009-07-22 17:55:28 +0000262 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenek53500662009-07-22 17:55:28 +0000264 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000265 // pretty-print the constraint. (work-in-progress)
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000266 std::string sbuf;
267 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek53500662009-07-22 17:55:28 +0000269 if (isa<Loc>(Constraint)) {
270 os << "Assuming pointer value is ";
271 os << (Assumption ? "non-null" : "null");
272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenek53500662009-07-22 17:55:28 +0000274 if (os.str().empty())
275 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek53500662009-07-22 17:55:28 +0000277 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000278 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000279 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenek53500662009-07-22 17:55:28 +0000281 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000282 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000283 S = BSrc->getTerminatorCondition();
284 }
285 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
286 S = PS->getStmt();
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenek53500662009-07-22 17:55:28 +0000289 if (!S)
290 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenek53500662009-07-22 17:55:28 +0000292 // Construct a new PathDiagnosticPiece.
293 PathDiagnosticLocation L(S, BRC.getSourceManager());
294 return new PathDiagnosticEventPiece(L, os.str());
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenek53500662009-07-22 17:55:28 +0000297 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000298 }
Ted Kremenek53500662009-07-22 17:55:28 +0000299};
300} // end anonymous namespace
301
Anna Zaks8e6431a2011-08-18 22:37:56 +0000302static void registerTrackConstraint(BugReport &BR,
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000303 DefinedSVal Constraint,
Ted Kremenek53500662009-07-22 17:55:28 +0000304 bool Assumption) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000305 BR.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000306}
307
Anna Zaks8e6431a2011-08-18 22:37:56 +0000308void bugreporter::registerTrackNullOrUndefValue(BugReport &BR,
309 const void *data) {
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek592362b2009-08-18 01:05:30 +0000311 const Stmt *S = static_cast<const Stmt*>(data);
Anna Zaks8e6431a2011-08-18 22:37:56 +0000312 const ExplodedNode *N = BR.getErrorNode();
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek53500662009-07-22 17:55:28 +0000314 if (!S)
315 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000316
Anna Zaks8e6431a2011-08-18 22:37:56 +0000317 ProgramStateManager &StateMgr = N->getState()->getStateManager();
318
Ted Kremenek88299892011-07-28 23:07:59 +0000319 // Walk through nodes until we get one that matches the statement
320 // exactly.
321 while (N) {
322 const ProgramPoint &pp = N->getLocation();
323 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
324 if (ps->getStmt() == S)
325 break;
326 }
327 N = *N->pred_begin();
328 }
329
330 if (!N)
331 return;
332
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000333 const ProgramState *state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek892697d2010-12-16 07:46:53 +0000335 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000336 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
337 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000338 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000339 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek53500662009-07-22 17:55:28 +0000341 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000342 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000343
344 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000345 || V.isUndef()) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000346 ::registerFindLastStore(BR, R, V);
Ted Kremenek53500662009-07-22 17:55:28 +0000347 }
348 }
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek13976632010-02-08 16:18:51 +0000351 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek53500662009-07-22 17:55:28 +0000353 // Uncomment this to find cases where we aren't properly getting the
354 // base value that was dereferenced.
355 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek53500662009-07-22 17:55:28 +0000357 // Is it a symbolic value?
358 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
359 const SubRegion *R = cast<SubRegion>(L->getRegion());
360 while (R && !isa<SymbolicRegion>(R)) {
361 R = dyn_cast<SubRegion>(R->getSuperRegion());
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek53500662009-07-22 17:55:28 +0000364 if (R) {
365 assert(isa<SymbolicRegion>(R));
Anna Zaks8e6431a2011-08-18 22:37:56 +0000366 registerTrackConstraint(BR, loc::MemRegionVal(R), false);
Ted Kremenek53500662009-07-22 17:55:28 +0000367 }
368 }
369}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000370
Anna Zaks8e6431a2011-08-18 22:37:56 +0000371void bugreporter::registerFindLastStore(BugReport &BR,
372 const void *data) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000373
374 const MemRegion *R = static_cast<const MemRegion*>(data);
Anna Zaks8e6431a2011-08-18 22:37:56 +0000375 const ExplodedNode *N = BR.getErrorNode();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000376
377 if (!R)
378 return;
379
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000380 const ProgramState *state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000381 SVal V = state->getSVal(R);
382
383 if (V.isUnknown())
384 return;
385
Anna Zaks8e6431a2011-08-18 22:37:56 +0000386 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000387}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000388
389
390namespace {
391class NilReceiverVisitor : public BugReporterVisitor {
392public:
393 NilReceiverVisitor() {}
394
395 void Profile(llvm::FoldingSetNodeID &ID) const {
396 static int x = 0;
397 ID.AddPointer(&x);
398 }
399
Ted Kremenek9c378f72011-08-12 23:37:29 +0000400 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
Ted Kremenekff7f7362010-03-20 18:02:01 +0000401 const ExplodedNode *PrevN,
Anna Zaks8e6431a2011-08-18 22:37:56 +0000402 BugReporterContext &BRC,
403 BugReport &BR) {
Ted Kremenekff7f7362010-03-20 18:02:01 +0000404
405 const PostStmt *P = N->getLocationAs<PostStmt>();
406 if (!P)
407 return 0;
408 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
409 if (!ME)
410 return 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +0000411 const Expr *Receiver = ME->getInstanceReceiver();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000412 if (!Receiver)
413 return 0;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000414 const ProgramState *state = N->getState();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000415 const SVal &V = state->getSVal(Receiver);
416 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
417 if (!DV)
418 return 0;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000419 state = state->assume(*DV, true);
Ted Kremenekff7f7362010-03-20 18:02:01 +0000420 if (state)
421 return 0;
422
423 // The receiver was nil, and hence the method was skipped.
424 // Register a BugReporterVisitor to issue a message telling us how
425 // the receiver was null.
Anna Zaks8e6431a2011-08-18 22:37:56 +0000426 bugreporter::registerTrackNullOrUndefValue(BR, Receiver);
Ted Kremenekff7f7362010-03-20 18:02:01 +0000427 // Issue a message saying that the method was skipped.
428 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
429 return new PathDiagnosticEventPiece(L, "No method actually called "
430 "because the receiver is nil");
431 }
432};
433} // end anonymous namespace
434
Anna Zaks8e6431a2011-08-18 22:37:56 +0000435void bugreporter::registerNilReceiverVisitor(BugReport &BR) {
436 BR.addVisitor(new NilReceiverVisitor());
Ted Kremenekff7f7362010-03-20 18:02:01 +0000437}
Tom Care2bbbe502010-09-02 23:30:22 +0000438
Anna Zaks8e6431a2011-08-18 22:37:56 +0000439// Registers every VarDecl inside a Stmt with a last store visitor.
440void bugreporter::registerVarDeclsLastStore(BugReport &BR,
441 const void *stmt) {
Tom Care2bbbe502010-09-02 23:30:22 +0000442 const Stmt *S = static_cast<const Stmt *>(stmt);
Anna Zaks8e6431a2011-08-18 22:37:56 +0000443 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000444
445 std::deque<const Stmt *> WorkList;
446
447 WorkList.push_back(S);
448
449 while (!WorkList.empty()) {
450 const Stmt *Head = WorkList.front();
451 WorkList.pop_front();
452
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000453 const ProgramState *state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000454 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000455
456 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
457 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
458 const VarRegion *R =
459 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
460
461 // What did we load?
462 SVal V = state->getSVal(S);
463
464 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000465 ::registerFindLastStore(BR, R, V);
Tom Care2bbbe502010-09-02 23:30:22 +0000466 }
467 }
468 }
469
470 for (Stmt::const_child_iterator I = Head->child_begin();
471 I != Head->child_end(); ++I)
472 WorkList.push_back(*I);
473 }
474}
Ted Kremenek993124e2011-08-06 06:54:45 +0000475
476//===----------------------------------------------------------------------===//
477// Visitor that tries to report interesting diagnostics from conditions.
478//===----------------------------------------------------------------------===//
479
480namespace {
481class ConditionVisitor : public BugReporterVisitor {
482public:
483 void Profile(llvm::FoldingSetNodeID &ID) const {
484 static int x = 0;
485 ID.AddPointer(&x);
486 }
487
488 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
489 const ExplodedNode *Prev,
Anna Zaks8e6431a2011-08-18 22:37:56 +0000490 BugReporterContext &BRC,
491 BugReport &BR);
Ted Kremenek993124e2011-08-06 06:54:45 +0000492
493 PathDiagnosticPiece *VisitTerminator(const Stmt *Term,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000494 const ProgramState *CurrentState,
495 const ProgramState *PrevState,
Ted Kremenek993124e2011-08-06 06:54:45 +0000496 const CFGBlock *srcBlk,
497 const CFGBlock *dstBlk,
498 BugReporterContext &BRC);
499
500 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
501 bool tookTrue,
502 BugReporterContext &BRC);
503
504 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
505 const DeclRefExpr *DR,
506 const bool tookTrue,
507 BugReporterContext &BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000508
509 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
510 const BinaryOperator *BExpr,
511 const bool tookTrue,
512 BugReporterContext &BRC);
513
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000514 bool patternMatch(const Expr *Ex,
Ted Kremenek681bc112011-08-16 01:53:41 +0000515 llvm::raw_ostream &Out,
516 BugReporterContext &BRC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000517};
518}
519
520PathDiagnosticPiece *ConditionVisitor::VisitNode(const ExplodedNode *N,
521 const ExplodedNode *Prev,
Anna Zaks8e6431a2011-08-18 22:37:56 +0000522 BugReporterContext &BRC,
523 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000524
525 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000526
527 const ProgramState *CurrentState = N->getState();
528 const ProgramState *PrevState = Prev->getState();
529
530 // Compare the GDMs of the state, because that is where constraints
531 // are managed. Note that ensure that we only look at nodes that
532 // were generated by the analyzer engine proper, not checkers.
533 if (CurrentState->getGDM().getRoot() ==
534 PrevState->getGDM().getRoot())
535 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000536
537 // If an assumption was made on a branch, it should be caught
538 // here by looking at the state transition.
539 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000540 const CFGBlock *srcBlk = BE->getSrc();
541 if (const Stmt *term = srcBlk->getTerminator())
542 return VisitTerminator(term, CurrentState, PrevState,
543 srcBlk, BE->getDst(), BRC);
544 return 0;
545 }
546
547 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
548 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
549 // violation.
550 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
551 cast<GRBugReporter>(BRC.getBugReporter()).
552 getEngine().getEagerlyAssumeTags();
553
554 const ProgramPointTag *tag = PS->getTag();
555 if (tag == tags.first)
556 return VisitTrueTest(cast<Expr>(PS->getStmt()), true, BRC);
557 if (tag == tags.second)
558 return VisitTrueTest(cast<Expr>(PS->getStmt()), false, BRC);
559
Ted Kremenek993124e2011-08-06 06:54:45 +0000560 return 0;
561 }
562
563 return 0;
564}
565
566PathDiagnosticPiece *
567ConditionVisitor::VisitTerminator(const Stmt *Term,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000568 const ProgramState *CurrentState,
569 const ProgramState *PrevState,
Ted Kremenek993124e2011-08-06 06:54:45 +0000570 const CFGBlock *srcBlk,
571 const CFGBlock *dstBlk,
572 BugReporterContext &BRC) {
573
Ted Kremenek993124e2011-08-06 06:54:45 +0000574 const Expr *Cond = 0;
575
576 switch (Term->getStmtClass()) {
577 default:
578 return 0;
579 case Stmt::IfStmtClass:
580 Cond = cast<IfStmt>(Term)->getCond();
581 break;
582 case Stmt::ConditionalOperatorClass:
583 Cond = cast<ConditionalOperator>(Term)->getCond();
584 break;
585 }
586
587 assert(Cond);
588 assert(srcBlk->succ_size() == 2);
589 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
590 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
591 tookTrue, BRC);
592}
593
594PathDiagnosticPiece *
595ConditionVisitor::VisitTrueTest(const Expr *Cond,
596 bool tookTrue,
597 BugReporterContext &BRC) {
598
599 const Expr *Ex = Cond;
600
Ted Kremenek681bc112011-08-16 01:53:41 +0000601 while (true) {
602 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000603 switch (Ex->getStmtClass()) {
604 default:
605 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000606 case Stmt::BinaryOperatorClass:
607 return VisitTrueTest(Cond, cast<BinaryOperator>(Cond), tookTrue, BRC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000608 case Stmt::DeclRefExprClass:
609 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC);
610 case Stmt::UnaryOperatorClass: {
611 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
612 if (UO->getOpcode() == UO_LNot) {
613 tookTrue = !tookTrue;
614 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
615 continue;
616 }
617 return 0;
618 }
619 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000620 }
621}
622
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000623bool ConditionVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
Ted Kremenek681bc112011-08-16 01:53:41 +0000624 BugReporterContext &BRC) {
625 const Expr *OriginalExpr = Ex;
626 Ex = Ex->IgnoreParenCasts();
627
628 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000629 const bool quotes = isa<VarDecl>(DR->getDecl());
630 if (quotes)
631 Out << '\'';
632 Out << DR->getDecl()->getDeclName().getAsString();
633 if (quotes)
634 Out << '\'';
635 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000636 }
637
638 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
639 QualType OriginalTy = OriginalExpr->getType();
640 if (OriginalTy->isPointerType()) {
641 if (IL->getValue() == 0) {
642 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000643 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000644 }
645 }
646 else if (OriginalTy->isObjCObjectPointerType()) {
647 if (IL->getValue() == 0) {
648 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000649 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000650 }
651 }
652
653 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000654 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000655 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000656
657 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000658}
659
660PathDiagnosticPiece *
661ConditionVisitor::VisitTrueTest(const Expr *Cond,
662 const BinaryOperator *BExpr,
663 const bool tookTrue,
664 BugReporterContext &BRC) {
665
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000666 bool shouldInvert = false;
667
Ted Kremenek681bc112011-08-16 01:53:41 +0000668 llvm::SmallString<128> LhsString, RhsString;
669 {
670 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000671 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
672 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
673
674 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000675 }
676
677 if (LhsString.empty() || RhsString.empty())
678 return 0;
679
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000680 // Should we invert the strings if the LHS is not a variable name?
681
Ted Kremenek681bc112011-08-16 01:53:41 +0000682 llvm::SmallString<256> buf;
683 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000684 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000685
686 // Do we need to invert the opcode?
687 BinaryOperator::Opcode Op = BExpr->getOpcode();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000688
689 if (shouldInvert)
690 switch (Op) {
691 default: break;
692 case BO_LT: Op = BO_GT; break;
693 case BO_GT: Op = BO_LT; break;
694 case BO_LE: Op = BO_GE; break;
695 case BO_GE: Op = BO_LE; break;
696 }
697
Ted Kremenek681bc112011-08-16 01:53:41 +0000698 if (!tookTrue)
699 switch (Op) {
700 case BO_EQ: Op = BO_NE; break;
701 case BO_NE: Op = BO_EQ; break;
702 case BO_LT: Op = BO_GE; break;
703 case BO_GT: Op = BO_LE; break;
704 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000705 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000706 default:
707 return 0;
708 }
709
710 switch (BExpr->getOpcode()) {
711 case BO_EQ:
712 Out << "equal to ";
713 break;
714 case BO_NE:
715 Out << "not equal to ";
716 break;
717 default:
718 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
719 break;
720 }
721
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000722 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek681bc112011-08-16 01:53:41 +0000723
724 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
725 return new PathDiagnosticEventPiece(Loc, Out.str());
Ted Kremenek993124e2011-08-06 06:54:45 +0000726}
727
728PathDiagnosticPiece *
729ConditionVisitor::VisitTrueTest(const Expr *Cond,
730 const DeclRefExpr *DR,
731 const bool tookTrue,
732 BugReporterContext &BRC) {
733
734 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
735 if (!VD)
736 return 0;
737
738 llvm::SmallString<256> Buf;
739 llvm::raw_svector_ostream Out(Buf);
740
741 Out << "Assuming '";
742 VD->getDeclName().printName(Out);
743 Out << "' is ";
744
745 QualType VDTy = VD->getType();
746
747 if (VDTy->isPointerType())
748 Out << (tookTrue ? "non-null" : "null");
749 else if (VDTy->isObjCObjectPointerType())
750 Out << (tookTrue ? "non-nil" : "nil");
751 else if (VDTy->isScalarType())
752 Out << (tookTrue ? "not equal to 0" : "0");
753 else
754 return 0;
755
756 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
757 return new PathDiagnosticEventPiece(Loc, Out.str());
758}
759
Anna Zaks8e6431a2011-08-18 22:37:56 +0000760void bugreporter::registerConditionVisitor(BugReport &BR) {
761 BR.addVisitor(new ConditionVisitor());
Ted Kremenek993124e2011-08-06 06:54:45 +0000762}
763