blob: 31e68211ba522043d141fca7c361486cbf629821 [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"
Ted Kremenek53500662009-07-22 17:55:28 +000023
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000026
27//===----------------------------------------------------------------------===//
28// Utility functions.
29//===----------------------------------------------------------------------===//
30
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000031const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000032 // Pattern match for a few useful cases (do something smarter later):
33 // a[0], p->f, *p
34 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000035
Ted Kremenek53500662009-07-22 17:55:28 +000036 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +000037 if (U->getOpcode() == UO_Deref)
Ted Kremenek53500662009-07-22 17:55:28 +000038 return U->getSubExpr()->IgnoreParenCasts();
39 }
40 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
41 return ME->getBase()->IgnoreParenCasts();
42 }
43 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
Ted Kremenek53500662009-07-22 17:55:28 +000044 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//===----------------------------------------------------------------------===//
Anna Zaks23f395e2011-08-20 01:27:22 +000075
76PathDiagnosticPiece*
77BugReporterVisitor::getEndPath(BugReporterContext &BRC,
78 const ExplodedNode *EndPathNode,
79 BugReport &BR) {
80 return 0;
81}
82
83PathDiagnosticPiece*
84BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
85 const ExplodedNode *EndPathNode,
86 BugReport &BR) {
87 const ProgramPoint &PP = EndPathNode->getLocation();
88 PathDiagnosticLocation L;
89
90 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
91 const CFGBlock *block = BE->getBlock();
92 if (block->getBlockID() == 0) {
Anna Zaks0cd59482011-09-16 19:18:30 +000093 L = PathDiagnosticLocation::createDeclEnd(PP.getLocationContext(),
94 BRC.getSourceManager());
Anna Zaks23f395e2011-08-20 01:27:22 +000095 }
96 }
97
98 if (!L.isValid()) {
99 const Stmt *S = BR.getStmt();
100
101 if (!S)
102 return NULL;
103
Anna Zaks220ac8c2011-09-15 01:08:34 +0000104 L = PathDiagnosticLocation(S, BRC.getSourceManager(),
105 PP.getLocationContext());
Anna Zaks23f395e2011-08-20 01:27:22 +0000106 }
107
108 BugReport::ranges_iterator Beg, End;
109 llvm::tie(Beg, End) = BR.getRanges();
110
111 // Only add the statement itself as a range if we didn't specify any
112 // special ranges for this report.
113 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
114 BR.getDescription(),
115 Beg == End);
116 for (; Beg != End; ++Beg)
117 P->addRange(*Beg);
118
119 return P;
120}
121
122
Anna Zaks50bbc162011-08-19 22:33:38 +0000123void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
124 static int tag = 0;
125 ID.AddPointer(&tag);
126 ID.AddPointer(R);
127 ID.Add(V);
128}
Ted Kremenek53500662009-07-22 17:55:28 +0000129
Anna Zaks50bbc162011-08-19 22:33:38 +0000130PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
131 const ExplodedNode *PrevN,
132 BugReporterContext &BRC,
133 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Anna Zaks50bbc162011-08-19 22:33:38 +0000135 if (satisfied)
136 return NULL;
137
138 if (!StoreSite) {
139 const ExplodedNode *Node = N, *Last = NULL;
140
Ted Kremenekd2e70902012-01-25 22:18:04 +0000141 for ( ; Node ; Node = Node->getFirstPred()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000142
143 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
144 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
145 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
146 if (DS->getSingleDecl() == VR->getDecl()) {
Ted Kremenekd2e70902012-01-25 22:18:04 +0000147 // Record the last seen initialization point.
Anna Zaks50bbc162011-08-19 22:33:38 +0000148 Last = Node;
149 break;
150 }
151 }
152
Ted Kremenekd2e70902012-01-25 22:18:04 +0000153 // Does the region still bind to value V? If not, we are done
154 // looking for store sites.
Anna Zaks50bbc162011-08-19 22:33:38 +0000155 if (Node->getState()->getSVal(R) != V)
156 break;
157 }
158
159 if (!Node || !Last) {
160 satisfied = true;
161 return NULL;
162 }
163
164 StoreSite = Last;
Ted Kremenek1b431022010-03-20 18:01:57 +0000165 }
166
Anna Zaks50bbc162011-08-19 22:33:38 +0000167 if (StoreSite != N)
168 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anna Zaks50bbc162011-08-19 22:33:38 +0000170 satisfied = true;
171 llvm::SmallString<256> sbuf;
172 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Anna Zaks50bbc162011-08-19 22:33:38 +0000174 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
175 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Anna Zaks50bbc162011-08-19 22:33:38 +0000177 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000178 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000179 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000180 else
Ted Kremenek53500662009-07-22 17:55:28 +0000181 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek53500662009-07-22 17:55:28 +0000183 if (isa<loc::ConcreteInt>(V)) {
184 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000185 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000186 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000187 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000188 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000189 b = true;
190 }
191 }
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenek53500662009-07-22 17:55:28 +0000194 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000195 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000196 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000197 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000198 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000199 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000200 else if (V.isUndef()) {
201 if (isa<VarRegion>(R)) {
202 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
203 if (VD->getInit())
204 os << "initialized to a garbage value";
205 else
206 os << "declared without an initial value";
207 }
Ted Kremenek53500662009-07-22 17:55:28 +0000208 }
Ted Kremenek53500662009-07-22 17:55:28 +0000209 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000210 }
211
212 if (os.str().empty()) {
213 if (isa<loc::ConcreteInt>(V)) {
214 bool b = false;
215 if (R->isBoundable()) {
216 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
217 if (TR->getValueType()->isObjCObjectPointerType()) {
218 os << "nil object reference stored to ";
219 b = true;
220 }
221 }
222 }
223
224 if (!b)
225 os << "Null pointer value stored to ";
226 }
227 else if (V.isUndef()) {
228 os << "Uninitialized value stored to ";
229 }
230 else if (isa<nonloc::ConcreteInt>(V)) {
231 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
232 << " is assigned to ";
233 }
234 else
235 return NULL;
236
237 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000238 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000239 }
240 else
241 return NULL;
242 }
243
Anna Zaks50bbc162011-08-19 22:33:38 +0000244 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000245 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000246 PathDiagnosticLocation L =
247 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000248 if (!L.isValid())
249 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000250 return new PathDiagnosticEventPiece(L, os.str());
251}
252
253void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
254 static int tag = 0;
255 ID.AddPointer(&tag);
256 ID.AddBoolean(Assumption);
257 ID.Add(Constraint);
258}
259
260PathDiagnosticPiece *
261TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
262 const ExplodedNode *PrevN,
263 BugReporterContext &BRC,
264 BugReport &BR) {
265 if (isSatisfied)
266 return NULL;
267
268 // Check if in the previous state it was feasible for this constraint
269 // to *not* be true.
270 if (PrevN->getState()->assume(Constraint, !Assumption)) {
271
272 isSatisfied = true;
273
274 // As a sanity check, make sure that the negation of the constraint
275 // was infeasible in the current state. If it is feasible, we somehow
276 // missed the transition point.
277 if (N->getState()->assume(Constraint, !Assumption))
278 return NULL;
279
280 // We found the transition point for the constraint. We now need to
281 // pretty-print the constraint. (work-in-progress)
282 std::string sbuf;
283 llvm::raw_string_ostream os(sbuf);
284
285 if (isa<Loc>(Constraint)) {
286 os << "Assuming pointer value is ";
287 os << (Assumption ? "non-null" : "null");
288 }
289
290 if (os.str().empty())
291 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek53500662009-07-22 17:55:28 +0000293 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000294 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000295 PathDiagnosticLocation L =
296 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000297 if (!L.isValid())
298 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000299 return new PathDiagnosticEventPiece(L, os.str());
300 }
Ted Kremenek53500662009-07-22 17:55:28 +0000301
Anna Zaks50bbc162011-08-19 22:33:38 +0000302 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000303}
304
Anna Zaks50bbc162011-08-19 22:33:38 +0000305BugReporterVisitor *
306bugreporter::getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
307 const Stmt *S) {
308 if (!S || !N)
309 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Anna Zaks8e6431a2011-08-18 22:37:56 +0000311 ProgramStateManager &StateMgr = N->getState()->getStateManager();
312
Ted Kremenek88299892011-07-28 23:07:59 +0000313 // Walk through nodes until we get one that matches the statement
314 // exactly.
315 while (N) {
316 const ProgramPoint &pp = N->getLocation();
317 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
318 if (ps->getStmt() == S)
319 break;
320 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000321 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000322 }
323
324 if (!N)
Anna Zaks50bbc162011-08-19 22:33:38 +0000325 return 0;
Ted Kremenek88299892011-07-28 23:07:59 +0000326
Ted Kremenek8bef8232012-01-26 21:29:00 +0000327 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek892697d2010-12-16 07:46:53 +0000329 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000330 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
331 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000332 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000333 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek53500662009-07-22 17:55:28 +0000335 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000336 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000337
338 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000339 || V.isUndef()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000340 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek53500662009-07-22 17:55:28 +0000341 }
342 }
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek5eca4822012-01-06 22:09:28 +0000345 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek53500662009-07-22 17:55:28 +0000347 // Uncomment this to find cases where we aren't properly getting the
348 // base value that was dereferenced.
349 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek53500662009-07-22 17:55:28 +0000351 // Is it a symbolic value?
352 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
353 const SubRegion *R = cast<SubRegion>(L->getRegion());
354 while (R && !isa<SymbolicRegion>(R)) {
355 R = dyn_cast<SubRegion>(R->getSuperRegion());
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek53500662009-07-22 17:55:28 +0000358 if (R) {
359 assert(isa<SymbolicRegion>(R));
Anna Zaks50bbc162011-08-19 22:33:38 +0000360 return new TrackConstraintBRVisitor(loc::MemRegionVal(R), false);
Ted Kremenek53500662009-07-22 17:55:28 +0000361 }
362 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000363
364 return 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000365}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000366
Anna Zaks50bbc162011-08-19 22:33:38 +0000367BugReporterVisitor *
368FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
369 const MemRegion *R) {
370 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000371
Ted Kremenek8bef8232012-01-26 21:29:00 +0000372 ProgramStateRef state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000373 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000374 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000375 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000376
Anna Zaks50bbc162011-08-19 22:33:38 +0000377 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000378}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000379
380
Anna Zaks50bbc162011-08-19 22:33:38 +0000381PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
382 const ExplodedNode *PrevN,
383 BugReporterContext &BRC,
384 BugReport &BR) {
385 const PostStmt *P = N->getLocationAs<PostStmt>();
386 if (!P)
387 return 0;
388 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
389 if (!ME)
390 return 0;
391 const Expr *Receiver = ME->getInstanceReceiver();
392 if (!Receiver)
393 return 0;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000394 ProgramStateRef state = N->getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000395 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000396 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
397 if (!DV)
398 return 0;
399 state = state->assume(*DV, true);
400 if (state)
401 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000402
Anna Zaks50bbc162011-08-19 22:33:38 +0000403 // The receiver was nil, and hence the method was skipped.
404 // Register a BugReporterVisitor to issue a message telling us how
405 // the receiver was null.
406 BR.addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Receiver));
407 // Issue a message saying that the method was skipped.
Anna Zaks220ac8c2011-09-15 01:08:34 +0000408 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
409 N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000410 return new PathDiagnosticEventPiece(L, "No method actually called "
411 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000412}
Tom Care2bbbe502010-09-02 23:30:22 +0000413
Anna Zaks8e6431a2011-08-18 22:37:56 +0000414// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000415void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
416 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000417 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000418 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000419 WorkList.push_back(S);
420
421 while (!WorkList.empty()) {
422 const Stmt *Head = WorkList.front();
423 WorkList.pop_front();
424
Ted Kremenek8bef8232012-01-26 21:29:00 +0000425 ProgramStateRef state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000426 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000427
428 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
429 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
430 const VarRegion *R =
431 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
432
433 // What did we load?
Ted Kremenek5eca4822012-01-06 22:09:28 +0000434 SVal V = state->getSVal(S, N->getLocationContext());
Tom Care2bbbe502010-09-02 23:30:22 +0000435
436 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000437 // Register a new visitor with the BugReport.
438 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000439 }
440 }
441 }
442
443 for (Stmt::const_child_iterator I = Head->child_begin();
444 I != Head->child_end(); ++I)
445 WorkList.push_back(*I);
446 }
447}
Ted Kremenek993124e2011-08-06 06:54:45 +0000448
449//===----------------------------------------------------------------------===//
450// Visitor that tries to report interesting diagnostics from conditions.
451//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000452PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
453 const ExplodedNode *Prev,
454 BugReporterContext &BRC,
455 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000456
457 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000458
Ted Kremenek8bef8232012-01-26 21:29:00 +0000459 ProgramStateRef CurrentState = N->getState();
460 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000461
462 // Compare the GDMs of the state, because that is where constraints
463 // are managed. Note that ensure that we only look at nodes that
464 // were generated by the analyzer engine proper, not checkers.
465 if (CurrentState->getGDM().getRoot() ==
466 PrevState->getGDM().getRoot())
467 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000468
469 // If an assumption was made on a branch, it should be caught
470 // here by looking at the state transition.
471 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000472 const CFGBlock *srcBlk = BE->getSrc();
473 if (const Stmt *term = srcBlk->getTerminator())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000474 return VisitTerminator(term, N, srcBlk, BE->getDst(), BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000475 return 0;
476 }
477
478 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
479 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
480 // violation.
481 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
482 cast<GRBugReporter>(BRC.getBugReporter()).
483 getEngine().getEagerlyAssumeTags();
484
485 const ProgramPointTag *tag = PS->getTag();
486 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000487 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
488 BRC, N->getLocationContext());
Ted Kremenek681bc112011-08-16 01:53:41 +0000489 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000490 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
491 BRC, N->getLocationContext());
Ted Kremenek681bc112011-08-16 01:53:41 +0000492
Ted Kremenek993124e2011-08-06 06:54:45 +0000493 return 0;
494 }
495
496 return 0;
497}
498
499PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000500ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000501 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000502 const CFGBlock *srcBlk,
503 const CFGBlock *dstBlk,
504 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000505 const Expr *Cond = 0;
506
507 switch (Term->getStmtClass()) {
508 default:
509 return 0;
510 case Stmt::IfStmtClass:
511 Cond = cast<IfStmt>(Term)->getCond();
512 break;
513 case Stmt::ConditionalOperatorClass:
514 Cond = cast<ConditionalOperator>(Term)->getCond();
515 break;
516 }
517
518 assert(Cond);
519 assert(srcBlk->succ_size() == 2);
520 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
521 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
Anna Zaks220ac8c2011-09-15 01:08:34 +0000522 tookTrue, BRC, N->getLocationContext());
Ted Kremenek993124e2011-08-06 06:54:45 +0000523}
524
525PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000526ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
527 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000528 BugReporterContext &BRC,
529 const LocationContext *LC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000530
531 const Expr *Ex = Cond;
532
Ted Kremenek681bc112011-08-16 01:53:41 +0000533 while (true) {
534 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000535 switch (Ex->getStmtClass()) {
536 default:
537 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000538 case Stmt::BinaryOperatorClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000539 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC, LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000540 case Stmt::DeclRefExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000541 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC, LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000542 case Stmt::UnaryOperatorClass: {
543 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
544 if (UO->getOpcode() == UO_LNot) {
545 tookTrue = !tookTrue;
546 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
547 continue;
548 }
549 return 0;
550 }
551 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000552 }
553}
554
Anna Zaks50bbc162011-08-19 22:33:38 +0000555bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
556 BugReporterContext &BRC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000557 const Expr *OriginalExpr = Ex;
558 Ex = Ex->IgnoreParenCasts();
559
560 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000561 const bool quotes = isa<VarDecl>(DR->getDecl());
562 if (quotes)
563 Out << '\'';
564 Out << DR->getDecl()->getDeclName().getAsString();
565 if (quotes)
566 Out << '\'';
567 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000568 }
569
570 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
571 QualType OriginalTy = OriginalExpr->getType();
572 if (OriginalTy->isPointerType()) {
573 if (IL->getValue() == 0) {
574 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000575 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000576 }
577 }
578 else if (OriginalTy->isObjCObjectPointerType()) {
579 if (IL->getValue() == 0) {
580 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000581 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000582 }
583 }
584
585 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000586 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000587 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000588
589 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000590}
591
592PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000593ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
594 const BinaryOperator *BExpr,
595 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000596 BugReporterContext &BRC,
597 const LocationContext *LC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000598
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000599 bool shouldInvert = false;
600
Ted Kremenek681bc112011-08-16 01:53:41 +0000601 llvm::SmallString<128> LhsString, RhsString;
602 {
603 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000604 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
605 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
606
607 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000608 }
609
Ted Kremenekd1247c52012-01-04 08:18:09 +0000610 BinaryOperator::Opcode Op = BExpr->getOpcode();
611
612 if (BinaryOperator::isAssignmentOp(Op)) {
613 // For assignment operators, all that we care about is that the LHS
614 // evaluates to "true" or "false".
615 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
616 BRC, LC);
617 }
618
619 // For non-assignment operations, we require that we can understand
620 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000621 if (LhsString.empty() || RhsString.empty())
622 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000623
Ted Kremenekd1247c52012-01-04 08:18:09 +0000624 // Should we invert the strings if the LHS is not a variable name?
Ted Kremenek681bc112011-08-16 01:53:41 +0000625 llvm::SmallString<256> buf;
626 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000627 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000628
629 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000630 if (shouldInvert)
631 switch (Op) {
632 default: break;
633 case BO_LT: Op = BO_GT; break;
634 case BO_GT: Op = BO_LT; break;
635 case BO_LE: Op = BO_GE; break;
636 case BO_GE: Op = BO_LE; break;
637 }
638
Ted Kremenek681bc112011-08-16 01:53:41 +0000639 if (!tookTrue)
640 switch (Op) {
641 case BO_EQ: Op = BO_NE; break;
642 case BO_NE: Op = BO_EQ; break;
643 case BO_LT: Op = BO_GE; break;
644 case BO_GT: Op = BO_LE; break;
645 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000646 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000647 default:
648 return 0;
649 }
650
Ted Kremenek6ae32572011-12-20 22:00:25 +0000651 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000652 case BO_EQ:
653 Out << "equal to ";
654 break;
655 case BO_NE:
656 Out << "not equal to ";
657 break;
658 default:
659 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
660 break;
661 }
662
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000663 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek681bc112011-08-16 01:53:41 +0000664
Anna Zaks220ac8c2011-09-15 01:08:34 +0000665 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000666 return new PathDiagnosticEventPiece(Loc, Out.str());
Ted Kremenek993124e2011-08-06 06:54:45 +0000667}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000668
669PathDiagnosticPiece *
670ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
671 const Expr *CondVarExpr,
672 const bool tookTrue,
673 BugReporterContext &BRC,
674 const LocationContext *LC) {
675 llvm::SmallString<256> buf;
676 llvm::raw_svector_ostream Out(buf);
677 Out << "Assuming " << LhsString << " is ";
678
679 QualType Ty = CondVarExpr->getType();
680
681 if (Ty->isPointerType())
682 Out << (tookTrue ? "not null" : "null");
683 else if (Ty->isObjCObjectPointerType())
684 Out << (tookTrue ? "not nil" : "nil");
685 else if (Ty->isBooleanType())
686 Out << (tookTrue ? "true" : "false");
687 else if (Ty->isIntegerType())
688 Out << (tookTrue ? "non-zero" : "zero");
689 else
690 return 0;
691
692 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LC);
693 return new PathDiagnosticEventPiece(Loc, Out.str());
694}
Ted Kremenek993124e2011-08-06 06:54:45 +0000695
696PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000697ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
698 const DeclRefExpr *DR,
699 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000700 BugReporterContext &BRC,
701 const LocationContext *LC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000702
703 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
704 if (!VD)
705 return 0;
706
707 llvm::SmallString<256> Buf;
708 llvm::raw_svector_ostream Out(Buf);
709
710 Out << "Assuming '";
711 VD->getDeclName().printName(Out);
712 Out << "' is ";
713
714 QualType VDTy = VD->getType();
715
716 if (VDTy->isPointerType())
717 Out << (tookTrue ? "non-null" : "null");
718 else if (VDTy->isObjCObjectPointerType())
719 Out << (tookTrue ? "non-nil" : "nil");
720 else if (VDTy->isScalarType())
721 Out << (tookTrue ? "not equal to 0" : "0");
722 else
723 return 0;
724
Anna Zaks220ac8c2011-09-15 01:08:34 +0000725 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000726 return new PathDiagnosticEventPiece(Loc, Out.str());
727}