blob: 4b290626c0814ea5b9b19439d8586b12b9616bf1 [file] [log] [blame]
Ted Kremenek53500662009-07-22 17:55:28 +00001// BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a set of BugReporter "visitors" which can be used to
11// enhance the diagnostics reported for a bug.
12//
13//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +000014#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
Ted Kremenek53500662009-07-22 17:55:28 +000015
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprObjC.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenek681bc112011-08-16 01:53:41 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Ted Kremenek53500662009-07-22 17:55:28 +000024
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000027
28//===----------------------------------------------------------------------===//
29// Utility functions.
30//===----------------------------------------------------------------------===//
31
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000032const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000033 // Pattern match for a few useful cases (do something smarter later):
34 // a[0], p->f, *p
35 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek53500662009-07-22 17:55:28 +000037 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +000038 if (U->getOpcode() == UO_Deref)
Ted Kremenek53500662009-07-22 17:55:28 +000039 return U->getSubExpr()->IgnoreParenCasts();
40 }
41 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
42 return ME->getBase()->IgnoreParenCasts();
43 }
44 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
Ted Kremenek53500662009-07-22 17:55:28 +000045 return AE->getBase();
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
48 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000049}
50
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000051const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000052 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000053 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
54 return BE->getRHS();
55 return NULL;
56}
57
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000058const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000059 // Callee is checked as a PreVisit to the CallExpr.
60 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000061 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
62 return CE->getCallee();
63 return NULL;
64}
65
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000066const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000067 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
68 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
69 return RS->getRetValue();
70 return NULL;
71}
72
73//===----------------------------------------------------------------------===//
74// Definitions for bug reporter visitors.
75//===----------------------------------------------------------------------===//
Anna Zaks23f395e2011-08-20 01:27:22 +000076
77PathDiagnosticPiece*
78BugReporterVisitor::getEndPath(BugReporterContext &BRC,
79 const ExplodedNode *EndPathNode,
80 BugReport &BR) {
81 return 0;
82}
83
84PathDiagnosticPiece*
85BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
86 const ExplodedNode *EndPathNode,
87 BugReport &BR) {
Anna Zaks5a0917d2012-02-16 03:41:01 +000088 PathDiagnosticLocation L =
89 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
Anna Zaks23f395e2011-08-20 01:27:22 +000090
91 BugReport::ranges_iterator Beg, End;
92 llvm::tie(Beg, End) = BR.getRanges();
93
94 // Only add the statement itself as a range if we didn't specify any
95 // special ranges for this report.
96 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
97 BR.getDescription(),
98 Beg == End);
99 for (; Beg != End; ++Beg)
100 P->addRange(*Beg);
101
102 return P;
103}
104
105
Anna Zaks50bbc162011-08-19 22:33:38 +0000106void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
107 static int tag = 0;
108 ID.AddPointer(&tag);
109 ID.AddPointer(R);
110 ID.Add(V);
111}
Ted Kremenek53500662009-07-22 17:55:28 +0000112
Anna Zaks50bbc162011-08-19 22:33:38 +0000113PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
114 const ExplodedNode *PrevN,
115 BugReporterContext &BRC,
116 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anna Zaks50bbc162011-08-19 22:33:38 +0000118 if (satisfied)
119 return NULL;
120
121 if (!StoreSite) {
122 const ExplodedNode *Node = N, *Last = NULL;
123
Ted Kremenekd2e70902012-01-25 22:18:04 +0000124 for ( ; Node ; Node = Node->getFirstPred()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000125
126 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
127 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
128 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
129 if (DS->getSingleDecl() == VR->getDecl()) {
Ted Kremenekd2e70902012-01-25 22:18:04 +0000130 // Record the last seen initialization point.
Anna Zaks50bbc162011-08-19 22:33:38 +0000131 Last = Node;
132 break;
133 }
134 }
135
Ted Kremenekd2e70902012-01-25 22:18:04 +0000136 // Does the region still bind to value V? If not, we are done
137 // looking for store sites.
Anna Zaks50bbc162011-08-19 22:33:38 +0000138 if (Node->getState()->getSVal(R) != V)
139 break;
140 }
141
142 if (!Node || !Last) {
143 satisfied = true;
144 return NULL;
145 }
146
147 StoreSite = Last;
Ted Kremenek1b431022010-03-20 18:01:57 +0000148 }
149
Anna Zaks50bbc162011-08-19 22:33:38 +0000150 if (StoreSite != N)
151 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anna Zaks50bbc162011-08-19 22:33:38 +0000153 satisfied = true;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000154 SmallString<256> sbuf;
Anna Zaks50bbc162011-08-19 22:33:38 +0000155 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anna Zaks50bbc162011-08-19 22:33:38 +0000157 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
158 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Anna Zaks50bbc162011-08-19 22:33:38 +0000160 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000161 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000162 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000163 else
Ted Kremenek53500662009-07-22 17:55:28 +0000164 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek53500662009-07-22 17:55:28 +0000166 if (isa<loc::ConcreteInt>(V)) {
167 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000168 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000169 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000170 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000171 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000172 b = true;
173 }
174 }
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek53500662009-07-22 17:55:28 +0000177 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000178 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000179 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000180 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000181 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000182 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000183 else if (V.isUndef()) {
184 if (isa<VarRegion>(R)) {
185 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
186 if (VD->getInit())
187 os << "initialized to a garbage value";
188 else
189 os << "declared without an initial value";
190 }
Ted Kremenek53500662009-07-22 17:55:28 +0000191 }
Ted Kremenek53500662009-07-22 17:55:28 +0000192 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000193 }
194
195 if (os.str().empty()) {
196 if (isa<loc::ConcreteInt>(V)) {
197 bool b = false;
198 if (R->isBoundable()) {
199 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
200 if (TR->getValueType()->isObjCObjectPointerType()) {
201 os << "nil object reference stored to ";
202 b = true;
203 }
204 }
205 }
206
207 if (!b)
208 os << "Null pointer value stored to ";
209 }
210 else if (V.isUndef()) {
211 os << "Uninitialized value stored to ";
212 }
213 else if (isa<nonloc::ConcreteInt>(V)) {
214 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
215 << " is assigned to ";
216 }
217 else
218 return NULL;
219
220 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000221 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000222 }
223 else
224 return NULL;
225 }
226
Anna Zaks50bbc162011-08-19 22:33:38 +0000227 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000228 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000229 PathDiagnosticLocation L =
230 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000231 if (!L.isValid())
232 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000233 return new PathDiagnosticEventPiece(L, os.str());
234}
235
236void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
237 static int tag = 0;
238 ID.AddPointer(&tag);
239 ID.AddBoolean(Assumption);
240 ID.Add(Constraint);
241}
242
243PathDiagnosticPiece *
244TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
245 const ExplodedNode *PrevN,
246 BugReporterContext &BRC,
247 BugReport &BR) {
248 if (isSatisfied)
249 return NULL;
250
251 // Check if in the previous state it was feasible for this constraint
252 // to *not* be true.
253 if (PrevN->getState()->assume(Constraint, !Assumption)) {
254
255 isSatisfied = true;
256
257 // As a sanity check, make sure that the negation of the constraint
258 // was infeasible in the current state. If it is feasible, we somehow
259 // missed the transition point.
260 if (N->getState()->assume(Constraint, !Assumption))
261 return NULL;
262
263 // We found the transition point for the constraint. We now need to
264 // pretty-print the constraint. (work-in-progress)
265 std::string sbuf;
266 llvm::raw_string_ostream os(sbuf);
267
268 if (isa<Loc>(Constraint)) {
269 os << "Assuming pointer value is ";
270 os << (Assumption ? "non-null" : "null");
271 }
272
273 if (os.str().empty())
274 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek53500662009-07-22 17:55:28 +0000276 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000277 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000278 PathDiagnosticLocation L =
279 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000280 if (!L.isValid())
281 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000282 return new PathDiagnosticEventPiece(L, os.str());
283 }
Ted Kremenek53500662009-07-22 17:55:28 +0000284
Anna Zaks50bbc162011-08-19 22:33:38 +0000285 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000286}
287
Anna Zaks50bbc162011-08-19 22:33:38 +0000288BugReporterVisitor *
289bugreporter::getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
290 const Stmt *S) {
291 if (!S || !N)
292 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Anna Zaks8e6431a2011-08-18 22:37:56 +0000294 ProgramStateManager &StateMgr = N->getState()->getStateManager();
295
Ted Kremenek88299892011-07-28 23:07:59 +0000296 // Walk through nodes until we get one that matches the statement
297 // exactly.
298 while (N) {
299 const ProgramPoint &pp = N->getLocation();
300 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
301 if (ps->getStmt() == S)
302 break;
303 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000304 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000305 }
306
307 if (!N)
Anna Zaks50bbc162011-08-19 22:33:38 +0000308 return 0;
Ted Kremenek88299892011-07-28 23:07:59 +0000309
Ted Kremenek8bef8232012-01-26 21:29:00 +0000310 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek892697d2010-12-16 07:46:53 +0000312 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000313 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
314 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000315 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000316 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek53500662009-07-22 17:55:28 +0000318 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000319 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000320
321 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000322 || V.isUndef()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000323 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek53500662009-07-22 17:55:28 +0000324 }
325 }
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek5eca4822012-01-06 22:09:28 +0000328 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek53500662009-07-22 17:55:28 +0000330 // Uncomment this to find cases where we aren't properly getting the
331 // base value that was dereferenced.
332 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek53500662009-07-22 17:55:28 +0000334 // Is it a symbolic value?
335 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
336 const SubRegion *R = cast<SubRegion>(L->getRegion());
337 while (R && !isa<SymbolicRegion>(R)) {
338 R = dyn_cast<SubRegion>(R->getSuperRegion());
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek53500662009-07-22 17:55:28 +0000341 if (R) {
342 assert(isa<SymbolicRegion>(R));
Anna Zaks50bbc162011-08-19 22:33:38 +0000343 return new TrackConstraintBRVisitor(loc::MemRegionVal(R), false);
Ted Kremenek53500662009-07-22 17:55:28 +0000344 }
345 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000346
347 return 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000348}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000349
Anna Zaks50bbc162011-08-19 22:33:38 +0000350BugReporterVisitor *
351FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
352 const MemRegion *R) {
353 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000354
Ted Kremenek8bef8232012-01-26 21:29:00 +0000355 ProgramStateRef state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000356 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000357 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000358 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000359
Anna Zaks50bbc162011-08-19 22:33:38 +0000360 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000361}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000362
363
Anna Zaks50bbc162011-08-19 22:33:38 +0000364PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
365 const ExplodedNode *PrevN,
366 BugReporterContext &BRC,
367 BugReport &BR) {
368 const PostStmt *P = N->getLocationAs<PostStmt>();
369 if (!P)
370 return 0;
371 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
372 if (!ME)
373 return 0;
374 const Expr *Receiver = ME->getInstanceReceiver();
375 if (!Receiver)
376 return 0;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000377 ProgramStateRef state = N->getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000378 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000379 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
380 if (!DV)
381 return 0;
382 state = state->assume(*DV, true);
383 if (state)
384 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000385
Anna Zaks50bbc162011-08-19 22:33:38 +0000386 // The receiver was nil, and hence the method was skipped.
387 // Register a BugReporterVisitor to issue a message telling us how
388 // the receiver was null.
389 BR.addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Receiver));
390 // Issue a message saying that the method was skipped.
Anna Zaks220ac8c2011-09-15 01:08:34 +0000391 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
392 N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000393 return new PathDiagnosticEventPiece(L, "No method actually called "
394 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000395}
Tom Care2bbbe502010-09-02 23:30:22 +0000396
Anna Zaks8e6431a2011-08-18 22:37:56 +0000397// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000398void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
399 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000400 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000401 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000402 WorkList.push_back(S);
403
404 while (!WorkList.empty()) {
405 const Stmt *Head = WorkList.front();
406 WorkList.pop_front();
407
Ted Kremenek8bef8232012-01-26 21:29:00 +0000408 ProgramStateRef state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000409 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000410
411 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
412 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
413 const VarRegion *R =
414 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
415
416 // What did we load?
Ted Kremenek5eca4822012-01-06 22:09:28 +0000417 SVal V = state->getSVal(S, N->getLocationContext());
Tom Care2bbbe502010-09-02 23:30:22 +0000418
419 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000420 // Register a new visitor with the BugReport.
421 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000422 }
423 }
424 }
425
426 for (Stmt::const_child_iterator I = Head->child_begin();
427 I != Head->child_end(); ++I)
428 WorkList.push_back(*I);
429 }
430}
Ted Kremenek993124e2011-08-06 06:54:45 +0000431
432//===----------------------------------------------------------------------===//
433// Visitor that tries to report interesting diagnostics from conditions.
434//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000435PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
436 const ExplodedNode *Prev,
437 BugReporterContext &BRC,
438 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000439
440 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000441
Ted Kremenek8bef8232012-01-26 21:29:00 +0000442 ProgramStateRef CurrentState = N->getState();
443 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000444
445 // Compare the GDMs of the state, because that is where constraints
446 // are managed. Note that ensure that we only look at nodes that
447 // were generated by the analyzer engine proper, not checkers.
448 if (CurrentState->getGDM().getRoot() ==
449 PrevState->getGDM().getRoot())
450 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000451
452 // If an assumption was made on a branch, it should be caught
453 // here by looking at the state transition.
454 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000455 const CFGBlock *srcBlk = BE->getSrc();
456 if (const Stmt *term = srcBlk->getTerminator())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000457 return VisitTerminator(term, N, srcBlk, BE->getDst(), BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000458 return 0;
459 }
460
461 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
462 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
463 // violation.
464 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
465 cast<GRBugReporter>(BRC.getBugReporter()).
466 getEngine().getEagerlyAssumeTags();
467
468 const ProgramPointTag *tag = PS->getTag();
469 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000470 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
471 BRC, N->getLocationContext());
Ted Kremenek681bc112011-08-16 01:53:41 +0000472 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000473 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
474 BRC, N->getLocationContext());
Ted Kremenek681bc112011-08-16 01:53:41 +0000475
Ted Kremenek993124e2011-08-06 06:54:45 +0000476 return 0;
477 }
478
479 return 0;
480}
481
482PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000483ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000484 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000485 const CFGBlock *srcBlk,
486 const CFGBlock *dstBlk,
487 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000488 const Expr *Cond = 0;
489
490 switch (Term->getStmtClass()) {
491 default:
492 return 0;
493 case Stmt::IfStmtClass:
494 Cond = cast<IfStmt>(Term)->getCond();
495 break;
496 case Stmt::ConditionalOperatorClass:
497 Cond = cast<ConditionalOperator>(Term)->getCond();
498 break;
499 }
500
501 assert(Cond);
502 assert(srcBlk->succ_size() == 2);
503 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
504 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
Anna Zaks220ac8c2011-09-15 01:08:34 +0000505 tookTrue, BRC, N->getLocationContext());
Ted Kremenek993124e2011-08-06 06:54:45 +0000506}
507
508PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000509ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
510 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000511 BugReporterContext &BRC,
512 const LocationContext *LC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000513
514 const Expr *Ex = Cond;
515
Ted Kremenek681bc112011-08-16 01:53:41 +0000516 while (true) {
517 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000518 switch (Ex->getStmtClass()) {
519 default:
520 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000521 case Stmt::BinaryOperatorClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000522 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC, LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000523 case Stmt::DeclRefExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000524 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC, LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000525 case Stmt::UnaryOperatorClass: {
526 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
527 if (UO->getOpcode() == UO_LNot) {
528 tookTrue = !tookTrue;
529 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
530 continue;
531 }
532 return 0;
533 }
534 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000535 }
536}
537
Anna Zaks50bbc162011-08-19 22:33:38 +0000538bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
539 BugReporterContext &BRC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000540 const Expr *OriginalExpr = Ex;
541 Ex = Ex->IgnoreParenCasts();
542
543 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000544 const bool quotes = isa<VarDecl>(DR->getDecl());
545 if (quotes)
546 Out << '\'';
547 Out << DR->getDecl()->getDeclName().getAsString();
548 if (quotes)
549 Out << '\'';
550 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000551 }
552
553 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
554 QualType OriginalTy = OriginalExpr->getType();
555 if (OriginalTy->isPointerType()) {
556 if (IL->getValue() == 0) {
557 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000558 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000559 }
560 }
561 else if (OriginalTy->isObjCObjectPointerType()) {
562 if (IL->getValue() == 0) {
563 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000564 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000565 }
566 }
567
568 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000569 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000570 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000571
572 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000573}
574
575PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000576ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
577 const BinaryOperator *BExpr,
578 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000579 BugReporterContext &BRC,
580 const LocationContext *LC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000581
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000582 bool shouldInvert = false;
583
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000584 SmallString<128> LhsString, RhsString;
Ted Kremenek681bc112011-08-16 01:53:41 +0000585 {
586 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000587 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
588 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
589
590 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000591 }
592
Ted Kremenekd1247c52012-01-04 08:18:09 +0000593 BinaryOperator::Opcode Op = BExpr->getOpcode();
594
595 if (BinaryOperator::isAssignmentOp(Op)) {
596 // For assignment operators, all that we care about is that the LHS
597 // evaluates to "true" or "false".
598 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
599 BRC, LC);
600 }
601
602 // For non-assignment operations, we require that we can understand
603 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000604 if (LhsString.empty() || RhsString.empty())
605 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000606
Ted Kremenekd1247c52012-01-04 08:18:09 +0000607 // Should we invert the strings if the LHS is not a variable name?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000608 SmallString<256> buf;
Ted Kremenek681bc112011-08-16 01:53:41 +0000609 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000610 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000611
612 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000613 if (shouldInvert)
614 switch (Op) {
615 default: break;
616 case BO_LT: Op = BO_GT; break;
617 case BO_GT: Op = BO_LT; break;
618 case BO_LE: Op = BO_GE; break;
619 case BO_GE: Op = BO_LE; break;
620 }
621
Ted Kremenek681bc112011-08-16 01:53:41 +0000622 if (!tookTrue)
623 switch (Op) {
624 case BO_EQ: Op = BO_NE; break;
625 case BO_NE: Op = BO_EQ; break;
626 case BO_LT: Op = BO_GE; break;
627 case BO_GT: Op = BO_LE; break;
628 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000629 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000630 default:
631 return 0;
632 }
633
Ted Kremenek6ae32572011-12-20 22:00:25 +0000634 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000635 case BO_EQ:
636 Out << "equal to ";
637 break;
638 case BO_NE:
639 Out << "not equal to ";
640 break;
641 default:
642 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
643 break;
644 }
645
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000646 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek681bc112011-08-16 01:53:41 +0000647
Anna Zaks220ac8c2011-09-15 01:08:34 +0000648 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000649 return new PathDiagnosticEventPiece(Loc, Out.str());
Ted Kremenek993124e2011-08-06 06:54:45 +0000650}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000651
652PathDiagnosticPiece *
653ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
654 const Expr *CondVarExpr,
655 const bool tookTrue,
656 BugReporterContext &BRC,
657 const LocationContext *LC) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000658 SmallString<256> buf;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000659 llvm::raw_svector_ostream Out(buf);
660 Out << "Assuming " << LhsString << " is ";
661
662 QualType Ty = CondVarExpr->getType();
663
664 if (Ty->isPointerType())
665 Out << (tookTrue ? "not null" : "null");
666 else if (Ty->isObjCObjectPointerType())
667 Out << (tookTrue ? "not nil" : "nil");
668 else if (Ty->isBooleanType())
669 Out << (tookTrue ? "true" : "false");
670 else if (Ty->isIntegerType())
671 Out << (tookTrue ? "non-zero" : "zero");
672 else
673 return 0;
674
675 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LC);
676 return new PathDiagnosticEventPiece(Loc, Out.str());
677}
Ted Kremenek993124e2011-08-06 06:54:45 +0000678
679PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000680ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
681 const DeclRefExpr *DR,
682 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000683 BugReporterContext &BRC,
684 const LocationContext *LC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000685
686 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
687 if (!VD)
688 return 0;
689
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000690 SmallString<256> Buf;
Ted Kremenek993124e2011-08-06 06:54:45 +0000691 llvm::raw_svector_ostream Out(Buf);
692
693 Out << "Assuming '";
694 VD->getDeclName().printName(Out);
695 Out << "' is ";
696
697 QualType VDTy = VD->getType();
698
699 if (VDTy->isPointerType())
700 Out << (tookTrue ? "non-null" : "null");
701 else if (VDTy->isObjCObjectPointerType())
702 Out << (tookTrue ? "non-nil" : "nil");
703 else if (VDTy->isScalarType())
704 Out << (tookTrue ? "not equal to 0" : "0");
705 else
706 return 0;
707
Anna Zaks220ac8c2011-09-15 01:08:34 +0000708 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000709 return new PathDiagnosticEventPiece(Loc, Out.str());
710}
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000711
712static PathDiagnosticLocation getLastStmtLoc(const ExplodedNode *N,
713 const SourceManager &SM) {
714 while (N) {
715 ProgramPoint PP = N->getLocation();
716 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP))
717 return PathDiagnosticLocation(SP->getStmt(), SM, PP.getLocationContext());
718 if (N->pred_empty())
719 break;
720 N = *N->pred_begin();
721 }
722 return PathDiagnosticLocation();
723}
724
725PathDiagnosticPiece *
726CallEnterExitBRVisitor::VisitNode(const ExplodedNode *N,
727 const ExplodedNode *PrevN,
728 BugReporterContext &BRC,
729 BugReport &BR) {
730 ProgramPoint PP = N->getLocation();
731 SmallString<256> buf;
732 llvm::raw_svector_ostream Out(buf);
733 PathDiagnosticLocation pos;
734
735 if (const CallEnter *CEnter = dyn_cast<CallEnter>(&PP)) {
736 const Decl *callee = CEnter->getCalleeContext()->getDecl();
737 pos = PathDiagnosticLocation(CEnter->getCallExpr(), BRC.getSourceManager(),
738 PP.getLocationContext());
739 if (isa<BlockDecl>(callee))
740 Out << "Entering call to block";
741 else if (const NamedDecl *ND = dyn_cast<NamedDecl>(callee))
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000742 Out << "Entering call to '" << *ND << "'";
Ted Kremeneka6215b92012-02-07 02:27:37 +0000743 StringRef msg = Out.str();
744 if (msg.empty())
745 return 0;
746 return new PathDiagnosticCallEnterPiece(pos, msg);
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000747 }
748 else if (const CallExit *CExit = dyn_cast<CallExit>(&PP)) {
749 const Decl *caller = CExit->getLocationContext()->getParent()->getDecl();
750 pos = getLastStmtLoc(PrevN, BRC.getSourceManager());
751 if (const NamedDecl *ND = dyn_cast<NamedDecl>(caller))
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000752 Out << "Returning to '" << *ND << "'";
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000753 else
Ted Kremeneka6215b92012-02-07 02:27:37 +0000754 Out << "Returning to caller";
755 return new PathDiagnosticCallExitPiece(pos, Out.str());
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000756 }
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000757
Ted Kremeneka6215b92012-02-07 02:27:37 +0000758 return 0;
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000759}