blob: be946842fc35b4ee15784445fd607b4194425646 [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"
Anna Zaks80de4872012-08-29 21:22:37 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenek681bc112011-08-16 01:53:41 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Jordan Rose7aba1172012-08-28 00:50:42 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek53500662009-07-22 17:55:28 +000026
27using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000028using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000029
30//===----------------------------------------------------------------------===//
31// Utility functions.
32//===----------------------------------------------------------------------===//
33
Anna Zaks9b925ac2012-09-05 23:41:54 +000034bool bugreporter::isDeclRefExprToReference(const Expr *E) {
35 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
36 return DRE->getDecl()->getType()->isReferenceType();
37 }
38 return false;
39}
40
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000041const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000042 // Pattern match for a few useful cases (do something smarter later):
43 // a[0], p->f, *p
Jordan Rose3682f1e2012-08-25 01:06:23 +000044 const PostStmt *Loc = N->getLocationAs<PostStmt>();
45 if (!Loc)
46 return 0;
47
Jordan Rose364b9f92012-08-27 20:18:30 +000048 const Expr *S = dyn_cast<Expr>(Loc->getStmt());
49 if (!S)
50 return 0;
51 S = S->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek11abcec2012-05-02 00:31:29 +000053 while (true) {
54 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
55 assert(B->isAssignmentOp());
56 S = B->getLHS()->IgnoreParenCasts();
57 continue;
58 }
59 else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
60 if (U->getOpcode() == UO_Deref)
61 return U->getSubExpr()->IgnoreParenCasts();
62 }
63 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
Anna Zaks9b925ac2012-09-05 23:41:54 +000064 if (ME->isArrow() || isDeclRefExprToReference(ME->getBase())) {
Anna Zaksd91696e2012-09-05 22:31:55 +000065 return ME->getBase()->IgnoreParenCasts();
66 }
Ted Kremenek11abcec2012-05-02 00:31:29 +000067 }
68 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
69 return AE->getBase();
70 }
71 break;
Ted Kremenek53500662009-07-22 17:55:28 +000072 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
74 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000075}
76
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000077const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000078 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000079 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
80 return BE->getRHS();
81 return NULL;
82}
83
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000084const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000085 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
86 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
87 return RS->getRetValue();
88 return NULL;
89}
90
91//===----------------------------------------------------------------------===//
92// Definitions for bug reporter visitors.
93//===----------------------------------------------------------------------===//
Anna Zaks23f395e2011-08-20 01:27:22 +000094
95PathDiagnosticPiece*
96BugReporterVisitor::getEndPath(BugReporterContext &BRC,
97 const ExplodedNode *EndPathNode,
98 BugReport &BR) {
99 return 0;
100}
101
102PathDiagnosticPiece*
103BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
104 const ExplodedNode *EndPathNode,
105 BugReport &BR) {
Anna Zaks5a0917d2012-02-16 03:41:01 +0000106 PathDiagnosticLocation L =
107 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
Anna Zaks23f395e2011-08-20 01:27:22 +0000108
109 BugReport::ranges_iterator Beg, End;
110 llvm::tie(Beg, End) = BR.getRanges();
111
112 // Only add the statement itself as a range if we didn't specify any
113 // special ranges for this report.
114 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
115 BR.getDescription(),
116 Beg == End);
117 for (; Beg != End; ++Beg)
118 P->addRange(*Beg);
119
120 return P;
121}
122
123
Jordan Rose7aba1172012-08-28 00:50:42 +0000124namespace {
125/// Emits an extra note at the return statement of an interesting stack frame.
126///
127/// The returned value is marked as an interesting value, and if it's null,
128/// adds a visitor to track where it became null.
129///
130/// This visitor is intended to be used when another visitor discovers that an
131/// interesting value comes from an inlined function call.
132class ReturnVisitor : public BugReporterVisitorImpl<ReturnVisitor> {
133 const StackFrameContext *StackFrame;
134 bool Satisfied;
135public:
136 ReturnVisitor(const StackFrameContext *Frame)
137 : StackFrame(Frame), Satisfied(false) {}
138
139 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
140 static int Tag = 0;
141 ID.AddPointer(&Tag);
142 ID.AddPointer(StackFrame);
143 }
144
145 /// Adds a ReturnVisitor if the given statement represents a call that was
146 /// inlined.
147 ///
148 /// This will search back through the ExplodedGraph, starting from the given
149 /// node, looking for when the given statement was processed. If it turns out
150 /// the statement is a call that was inlined, we add the visitor to the
151 /// bug report, so it can print a note later.
152 static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S,
153 BugReport &BR) {
154 if (!CallEvent::isCallStmt(S))
155 return;
156
157 // First, find when we processed the statement.
158 do {
159 if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>())
160 if (CEE->getCalleeContext()->getCallSite() == S)
161 break;
162 if (const StmtPoint *SP = Node->getLocationAs<StmtPoint>())
163 if (SP->getStmt() == S)
164 break;
165
166 Node = Node->getFirstPred();
167 } while (Node);
168
169 // Next, step over any post-statement checks.
170 while (Node && isa<PostStmt>(Node->getLocation()))
171 Node = Node->getFirstPred();
172
173 // Finally, see if we inlined the call.
174 if (Node)
175 if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>())
176 if (CEE->getCalleeContext()->getCallSite() == S)
177 BR.addVisitor(new ReturnVisitor(CEE->getCalleeContext()));
178
179 }
180
181 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
182 const ExplodedNode *PrevN,
183 BugReporterContext &BRC,
184 BugReport &BR) {
185 if (Satisfied)
186 return 0;
187
188 // Only print a message at the interesting return statement.
189 if (N->getLocationContext() != StackFrame)
190 return 0;
191
192 const StmtPoint *SP = N->getLocationAs<StmtPoint>();
193 if (!SP)
194 return 0;
195
196 const ReturnStmt *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
197 if (!Ret)
198 return 0;
199
200 // Okay, we're at the right return statement, but do we have the return
201 // value available?
202 ProgramStateRef State = N->getState();
203 SVal V = State->getSVal(Ret, StackFrame);
204 if (V.isUnknownOrUndef())
205 return 0;
206
207 // Don't print any more notes after this one.
208 Satisfied = true;
209
210 // Build an appropriate message based on the return value.
211 SmallString<64> Msg;
212 llvm::raw_svector_ostream Out(Msg);
213
214 const Expr *RetE = Ret->getRetValue();
215 assert(RetE && "Tracking a return value for a void function");
216 RetE = RetE->IgnoreParenCasts();
217
218 // See if we know that the return value is 0.
219 ProgramStateRef StNonZero, StZero;
220 llvm::tie(StNonZero, StZero) = State->assume(cast<DefinedSVal>(V));
221 if (StZero && !StNonZero) {
222 // If we're returning 0, we should track where that 0 came from.
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000223 bugreporter::trackNullOrUndefValue(N, RetE, BR);
Jordan Rose7aba1172012-08-28 00:50:42 +0000224
225 if (isa<Loc>(V)) {
226 if (RetE->getType()->isObjCObjectPointerType())
227 Out << "Returning nil";
228 else
229 Out << "Returning null pointer";
230 } else {
231 Out << "Returning zero";
232 }
233 } else {
234 // FIXME: We can probably do better than this.
235 BR.markInteresting(V);
236 Out << "Value returned here";
237 }
238
239 // FIXME: We should have a more generalized location printing mechanism.
240 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetE))
241 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
242 Out << " (loaded from '" << *DD << "')";
243
244 PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame);
245 return new PathDiagnosticEventPiece(L, Out.str());
246 }
247};
248} // end anonymous namespace
249
250
Anna Zaks50bbc162011-08-19 22:33:38 +0000251void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
252 static int tag = 0;
253 ID.AddPointer(&tag);
254 ID.AddPointer(R);
255 ID.Add(V);
256}
Ted Kremenek53500662009-07-22 17:55:28 +0000257
Jordan Rose166b7bd2012-08-28 00:50:45 +0000258PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ,
259 const ExplodedNode *Pred,
260 BugReporterContext &BRC,
261 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anna Zaks50bbc162011-08-19 22:33:38 +0000263 if (satisfied)
264 return NULL;
265
Jordan Rose166b7bd2012-08-28 00:50:45 +0000266 const ExplodedNode *StoreSite = 0;
267 const Expr *InitE = 0;
Anna Zaks50bbc162011-08-19 22:33:38 +0000268
Jordan Rose166b7bd2012-08-28 00:50:45 +0000269 // First see if we reached the declaration of the region.
270 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
271 if (const PostStmt *P = Pred->getLocationAs<PostStmt>()) {
272 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) {
273 if (DS->getSingleDecl() == VR->getDecl()) {
274 StoreSite = Pred;
275 InitE = VR->getDecl()->getInit();
Jordan Rose7aba1172012-08-28 00:50:42 +0000276 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000277 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000278 }
Ted Kremenek1b431022010-03-20 18:01:57 +0000279 }
280
Jordan Rose166b7bd2012-08-28 00:50:45 +0000281 // Otherwise, check that Succ has this binding and Pred does not, i.e. this is
282 // where the binding first occurred.
283 if (!StoreSite) {
284 if (Succ->getState()->getSVal(R) != V)
285 return NULL;
286 if (Pred->getState()->getSVal(R) == V)
287 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Jordan Rose166b7bd2012-08-28 00:50:45 +0000289 StoreSite = Succ;
290
291 // If this is an assignment expression, we can track the value
292 // being assigned.
293 if (const PostStmt *P = Succ->getLocationAs<PostStmt>())
294 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
295 if (BO->isAssignmentOp())
296 InitE = BO->getRHS();
297 }
298
299 if (!StoreSite)
300 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000301 satisfied = true;
Jordan Rose166b7bd2012-08-28 00:50:45 +0000302
303 // If the value that was stored came from an inlined call, make sure we
304 // step into the call.
305 if (InitE) {
306 InitE = InitE->IgnoreParenCasts();
307 ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE, BR);
308 }
309
310 // Okay, we've found the binding. Emit an appropriate message.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000311 SmallString<256> sbuf;
Anna Zaks50bbc162011-08-19 22:33:38 +0000312 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Jordan Rose166b7bd2012-08-28 00:50:45 +0000314 if (const PostStmt *PS = StoreSite->getLocationAs<PostStmt>()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000315 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Anna Zaks50bbc162011-08-19 22:33:38 +0000317 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000318 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000319 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000320 else
Ted Kremenek53500662009-07-22 17:55:28 +0000321 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek53500662009-07-22 17:55:28 +0000323 if (isa<loc::ConcreteInt>(V)) {
324 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000325 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000326 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000327 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000328 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000329 b = true;
330 }
331 }
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek53500662009-07-22 17:55:28 +0000334 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000335 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000336 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000337 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000338 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000339 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000340 else if (V.isUndef()) {
341 if (isa<VarRegion>(R)) {
342 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
343 if (VD->getInit())
344 os << "initialized to a garbage value";
345 else
346 os << "declared without an initial value";
347 }
Ted Kremenek53500662009-07-22 17:55:28 +0000348 }
Jordan Rose68537992012-08-03 23:09:01 +0000349 else {
350 os << "initialized here";
351 }
Ted Kremenek53500662009-07-22 17:55:28 +0000352 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000353 }
354
355 if (os.str().empty()) {
356 if (isa<loc::ConcreteInt>(V)) {
357 bool b = false;
358 if (R->isBoundable()) {
359 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
360 if (TR->getValueType()->isObjCObjectPointerType()) {
361 os << "nil object reference stored to ";
362 b = true;
363 }
364 }
365 }
366
367 if (!b)
368 os << "Null pointer value stored to ";
369 }
370 else if (V.isUndef()) {
371 os << "Uninitialized value stored to ";
372 }
373 else if (isa<nonloc::ConcreteInt>(V)) {
374 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
375 << " is assigned to ";
376 }
377 else
Jordan Rose68537992012-08-03 23:09:01 +0000378 os << "Value assigned to ";
Anna Zaks50bbc162011-08-19 22:33:38 +0000379
380 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000381 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000382 }
383 else
384 return NULL;
385 }
386
Anna Zaks50bbc162011-08-19 22:33:38 +0000387 // Construct a new PathDiagnosticPiece.
Jordan Rose166b7bd2012-08-28 00:50:45 +0000388 ProgramPoint P = StoreSite->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000389 PathDiagnosticLocation L =
390 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000391 if (!L.isValid())
392 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000393 return new PathDiagnosticEventPiece(L, os.str());
394}
395
396void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
397 static int tag = 0;
398 ID.AddPointer(&tag);
399 ID.AddBoolean(Assumption);
400 ID.Add(Constraint);
401}
402
403PathDiagnosticPiece *
404TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
405 const ExplodedNode *PrevN,
406 BugReporterContext &BRC,
407 BugReport &BR) {
408 if (isSatisfied)
409 return NULL;
410
411 // Check if in the previous state it was feasible for this constraint
412 // to *not* be true.
413 if (PrevN->getState()->assume(Constraint, !Assumption)) {
414
415 isSatisfied = true;
416
417 // As a sanity check, make sure that the negation of the constraint
418 // was infeasible in the current state. If it is feasible, we somehow
419 // missed the transition point.
420 if (N->getState()->assume(Constraint, !Assumption))
421 return NULL;
422
423 // We found the transition point for the constraint. We now need to
424 // pretty-print the constraint. (work-in-progress)
425 std::string sbuf;
426 llvm::raw_string_ostream os(sbuf);
427
428 if (isa<Loc>(Constraint)) {
429 os << "Assuming pointer value is ";
430 os << (Assumption ? "non-null" : "null");
431 }
432
433 if (os.str().empty())
434 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek53500662009-07-22 17:55:28 +0000436 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000437 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000438 PathDiagnosticLocation L =
439 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000440 if (!L.isValid())
441 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000442 return new PathDiagnosticEventPiece(L, os.str());
443 }
Ted Kremenek53500662009-07-22 17:55:28 +0000444
Anna Zaks50bbc162011-08-19 22:33:38 +0000445 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000446}
447
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000448void bugreporter::trackNullOrUndefValue(const ExplodedNode *N, const Stmt *S,
449 BugReport &report) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000450 if (!S || !N)
Jordan Rose68537992012-08-03 23:09:01 +0000451 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Anna Zaks8e6431a2011-08-18 22:37:56 +0000453 ProgramStateManager &StateMgr = N->getState()->getStateManager();
454
Jordan Rose364b9f92012-08-27 20:18:30 +0000455 // Walk through nodes until we get one that matches the statement exactly.
Ted Kremenek88299892011-07-28 23:07:59 +0000456 while (N) {
457 const ProgramPoint &pp = N->getLocation();
458 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
459 if (ps->getStmt() == S)
460 break;
Jordan Rose23df2432012-08-24 16:34:31 +0000461 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) {
462 if (CEE->getCalleeContext()->getCallSite() == S)
463 break;
Ted Kremenek88299892011-07-28 23:07:59 +0000464 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000465 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000466 }
467
468 if (!N)
Jordan Rose68537992012-08-03 23:09:01 +0000469 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000470
Ted Kremenek8bef8232012-01-26 21:29:00 +0000471 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Jordan Rose364b9f92012-08-27 20:18:30 +0000473 // See if the expression we're interested refers to a variable.
474 // If so, we can track both its contents and constraints on its value.
475 if (const Expr *Ex = dyn_cast<Expr>(S)) {
476 // Strip off parens and casts. Note that this will never have issues with
477 // C++ user-defined implicit conversions, because those have a constructor
478 // or function call inside.
Jordan Rosee6cd0542012-08-16 00:03:33 +0000479 Ex = Ex->IgnoreParenCasts();
Ted Kremenek76aadc32012-03-09 01:13:14 +0000480 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000481 // FIXME: Right now we only track VarDecls because it's non-trivial to
482 // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812>
Ted Kremenek76aadc32012-03-09 01:13:14 +0000483 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
484 const VarRegion *R =
485 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Jordan Rose364b9f92012-08-27 20:18:30 +0000487 // Mark both the variable region and its contents as interesting.
Ted Kremenek11abcec2012-05-02 00:31:29 +0000488 SVal V = state->getRawSVal(loc::MemRegionVal(R));
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000489 report.markInteresting(R);
490 report.markInteresting(V);
Anna Zaks80de4872012-08-29 21:22:37 +0000491 report.addVisitor(new UndefOrNullArgVisitor(R));
Jordan Rose68537992012-08-03 23:09:01 +0000492
Jordan Rose364b9f92012-08-27 20:18:30 +0000493 // If the contents are symbolic, find out when they became null.
Jordan Rose68537992012-08-03 23:09:01 +0000494 if (V.getAsLocSymbol()) {
495 BugReporterVisitor *ConstraintTracker
496 = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false);
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000497 report.addVisitor(ConstraintTracker);
Jordan Rose68537992012-08-03 23:09:01 +0000498 }
499
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000500 report.addVisitor(new FindLastStoreBRVisitor(V, R));
Jordan Rose68537992012-08-03 23:09:01 +0000501 return;
Ted Kremenek53500662009-07-22 17:55:28 +0000502 }
503 }
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Jordan Rose364b9f92012-08-27 20:18:30 +0000506 // If the expression does NOT refer to a variable, we can still track
507 // constraints on its contents.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000508 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek53500662009-07-22 17:55:28 +0000510 // Uncomment this to find cases where we aren't properly getting the
511 // base value that was dereferenced.
512 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek53500662009-07-22 17:55:28 +0000514 // Is it a symbolic value?
515 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
Anna Zaksd91696e2012-09-05 22:31:55 +0000516 // At this point we are dealing with the region's LValue.
517 // However, if the rvalue is a symbolic region, we should track it as well.
518 SVal RVal = state->getSVal(L->getRegion());
519 const MemRegion *RegionRVal = RVal.getAsRegion();
Anna Zaks522fc212012-09-12 22:57:30 +0000520 report.addVisitor(new UndefOrNullArgVisitor(L->getRegion()));
521
Anna Zaksd91696e2012-09-05 22:31:55 +0000522
523 if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) {
524 report.markInteresting(RegionRVal);
525 report.addVisitor(new TrackConstraintBRVisitor(
526 loc::MemRegionVal(RegionRVal), false));
Ted Kremenek53500662009-07-22 17:55:28 +0000527 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000528 } else {
529 // Otherwise, if the value came from an inlined function call,
530 // we should at least make sure that function isn't pruned in our output.
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000531 ReturnVisitor::addVisitorIfNecessary(N, S, report);
Ted Kremenek53500662009-07-22 17:55:28 +0000532 }
533}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000534
Anna Zaks50bbc162011-08-19 22:33:38 +0000535BugReporterVisitor *
536FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
537 const MemRegion *R) {
538 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000539
Ted Kremenek8bef8232012-01-26 21:29:00 +0000540 ProgramStateRef state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000541 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000542 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000543 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000544
Anna Zaks50bbc162011-08-19 22:33:38 +0000545 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000546}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000547
548
Anna Zaks50bbc162011-08-19 22:33:38 +0000549PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
550 const ExplodedNode *PrevN,
551 BugReporterContext &BRC,
552 BugReport &BR) {
553 const PostStmt *P = N->getLocationAs<PostStmt>();
554 if (!P)
555 return 0;
556 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
557 if (!ME)
558 return 0;
559 const Expr *Receiver = ME->getInstanceReceiver();
560 if (!Receiver)
561 return 0;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000562 ProgramStateRef state = N->getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000563 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000564 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
565 if (!DV)
566 return 0;
567 state = state->assume(*DV, true);
568 if (state)
569 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000570
Anna Zaks50bbc162011-08-19 22:33:38 +0000571 // The receiver was nil, and hence the method was skipped.
572 // Register a BugReporterVisitor to issue a message telling us how
573 // the receiver was null.
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000574 bugreporter::trackNullOrUndefValue(N, Receiver, BR);
Anna Zaks50bbc162011-08-19 22:33:38 +0000575 // Issue a message saying that the method was skipped.
Anna Zaks220ac8c2011-09-15 01:08:34 +0000576 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
577 N->getLocationContext());
Anna Zaks907344e2012-04-05 02:10:19 +0000578 return new PathDiagnosticEventPiece(L, "No method is called "
Anna Zaks50bbc162011-08-19 22:33:38 +0000579 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000580}
Tom Care2bbbe502010-09-02 23:30:22 +0000581
Anna Zaks8e6431a2011-08-18 22:37:56 +0000582// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000583void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
584 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000585 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000586 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000587 WorkList.push_back(S);
588
589 while (!WorkList.empty()) {
590 const Stmt *Head = WorkList.front();
591 WorkList.pop_front();
592
Ted Kremenek8bef8232012-01-26 21:29:00 +0000593 ProgramStateRef state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000594 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000595
596 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
597 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
598 const VarRegion *R =
599 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
600
601 // What did we load?
Ted Kremenek5eca4822012-01-06 22:09:28 +0000602 SVal V = state->getSVal(S, N->getLocationContext());
Tom Care2bbbe502010-09-02 23:30:22 +0000603
604 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000605 // Register a new visitor with the BugReport.
606 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000607 }
608 }
609 }
610
611 for (Stmt::const_child_iterator I = Head->child_begin();
612 I != Head->child_end(); ++I)
613 WorkList.push_back(*I);
614 }
615}
Ted Kremenek993124e2011-08-06 06:54:45 +0000616
617//===----------------------------------------------------------------------===//
618// Visitor that tries to report interesting diagnostics from conditions.
619//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000620PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
621 const ExplodedNode *Prev,
622 BugReporterContext &BRC,
623 BugReport &BR) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000624 PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR);
625 if (PathDiagnosticEventPiece *ev =
626 dyn_cast_or_null<PathDiagnosticEventPiece>(piece))
Ted Kremenek76aadc32012-03-09 01:13:14 +0000627 ev->setPrunable(true, /* override */ false);
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000628 return piece;
629}
630
631PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
632 const ExplodedNode *Prev,
633 BugReporterContext &BRC,
634 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000635
Ted Kremenek22505ef2012-09-08 07:18:18 +0000636 ProgramPoint progPoint = N->getLocation();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000637 ProgramStateRef CurrentState = N->getState();
638 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000639
640 // Compare the GDMs of the state, because that is where constraints
641 // are managed. Note that ensure that we only look at nodes that
642 // were generated by the analyzer engine proper, not checkers.
643 if (CurrentState->getGDM().getRoot() ==
644 PrevState->getGDM().getRoot())
645 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000646
647 // If an assumption was made on a branch, it should be caught
648 // here by looking at the state transition.
649 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000650 const CFGBlock *srcBlk = BE->getSrc();
651 if (const Stmt *term = srcBlk->getTerminator())
Ted Kremenek76aadc32012-03-09 01:13:14 +0000652 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000653 return 0;
654 }
655
656 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
657 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
658 // violation.
659 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
660 cast<GRBugReporter>(BRC.getBugReporter()).
Ted Kremenek0caa2d42012-08-30 19:26:48 +0000661 getEngine().geteagerlyAssumeBinOpBifurcationTags();
Ted Kremenek681bc112011-08-16 01:53:41 +0000662
663 const ProgramPointTag *tag = PS->getTag();
664 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000665 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000666 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000667 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000668 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000669 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000670
Ted Kremenek993124e2011-08-06 06:54:45 +0000671 return 0;
672 }
673
674 return 0;
675}
676
677PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000678ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000679 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000680 const CFGBlock *srcBlk,
681 const CFGBlock *dstBlk,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000682 BugReport &R,
Anna Zaks50bbc162011-08-19 22:33:38 +0000683 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000684 const Expr *Cond = 0;
685
686 switch (Term->getStmtClass()) {
687 default:
688 return 0;
689 case Stmt::IfStmtClass:
690 Cond = cast<IfStmt>(Term)->getCond();
691 break;
692 case Stmt::ConditionalOperatorClass:
693 Cond = cast<ConditionalOperator>(Term)->getCond();
694 break;
695 }
696
697 assert(Cond);
698 assert(srcBlk->succ_size() == 2);
699 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
Ted Kremenekc47dc1b2012-09-07 06:51:37 +0000700 return VisitTrueTest(Cond, tookTrue, BRC, R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000701}
702
703PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000704ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
705 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000706 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000707 BugReport &R,
708 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000709
710 const Expr *Ex = Cond;
711
Ted Kremenek681bc112011-08-16 01:53:41 +0000712 while (true) {
Ted Kremenekc47dc1b2012-09-07 06:51:37 +0000713 Ex = Ex->IgnoreParenCasts();
Ted Kremenek993124e2011-08-06 06:54:45 +0000714 switch (Ex->getStmtClass()) {
715 default:
716 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000717 case Stmt::BinaryOperatorClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000718 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
719 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000720 case Stmt::DeclRefExprClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000721 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
722 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000723 case Stmt::UnaryOperatorClass: {
724 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
725 if (UO->getOpcode() == UO_LNot) {
726 tookTrue = !tookTrue;
Ted Kremenekc47dc1b2012-09-07 06:51:37 +0000727 Ex = UO->getSubExpr();
Ted Kremenek993124e2011-08-06 06:54:45 +0000728 continue;
729 }
730 return 0;
731 }
732 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000733 }
734}
735
Anna Zaks50bbc162011-08-19 22:33:38 +0000736bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000737 BugReporterContext &BRC,
738 BugReport &report,
739 const ExplodedNode *N,
740 llvm::Optional<bool> &prunable) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000741 const Expr *OriginalExpr = Ex;
742 Ex = Ex->IgnoreParenCasts();
743
744 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000745 const bool quotes = isa<VarDecl>(DR->getDecl());
Ted Kremenek76aadc32012-03-09 01:13:14 +0000746 if (quotes) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000747 Out << '\'';
Ted Kremenek76aadc32012-03-09 01:13:14 +0000748 const LocationContext *LCtx = N->getLocationContext();
749 const ProgramState *state = N->getState().getPtr();
750 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
751 LCtx).getAsRegion()) {
752 if (report.isInteresting(R))
753 prunable = false;
754 else {
755 const ProgramState *state = N->getState().getPtr();
756 SVal V = state->getSVal(R);
757 if (report.isInteresting(V))
758 prunable = false;
759 }
760 }
761 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000762 Out << DR->getDecl()->getDeclName().getAsString();
763 if (quotes)
764 Out << '\'';
765 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000766 }
767
768 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
769 QualType OriginalTy = OriginalExpr->getType();
770 if (OriginalTy->isPointerType()) {
771 if (IL->getValue() == 0) {
772 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000773 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000774 }
775 }
776 else if (OriginalTy->isObjCObjectPointerType()) {
777 if (IL->getValue() == 0) {
778 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000779 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000780 }
781 }
782
783 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000784 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000785 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000786
787 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000788}
789
790PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000791ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
792 const BinaryOperator *BExpr,
793 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000794 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000795 BugReport &R,
796 const ExplodedNode *N) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000797
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000798 bool shouldInvert = false;
Ted Kremenek76aadc32012-03-09 01:13:14 +0000799 llvm::Optional<bool> shouldPrune;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000800
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000801 SmallString<128> LhsString, RhsString;
Ted Kremenek681bc112011-08-16 01:53:41 +0000802 {
Ted Kremenek76aadc32012-03-09 01:13:14 +0000803 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
804 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
805 shouldPrune);
806 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
807 shouldPrune);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000808
809 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000810 }
811
Ted Kremenekd1247c52012-01-04 08:18:09 +0000812 BinaryOperator::Opcode Op = BExpr->getOpcode();
813
814 if (BinaryOperator::isAssignmentOp(Op)) {
815 // For assignment operators, all that we care about is that the LHS
816 // evaluates to "true" or "false".
817 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000818 BRC, R, N);
Ted Kremenekd1247c52012-01-04 08:18:09 +0000819 }
820
821 // For non-assignment operations, we require that we can understand
822 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000823 if (LhsString.empty() || RhsString.empty())
824 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000825
Ted Kremenekd1247c52012-01-04 08:18:09 +0000826 // Should we invert the strings if the LHS is not a variable name?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000827 SmallString<256> buf;
Ted Kremenek681bc112011-08-16 01:53:41 +0000828 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000829 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000830
831 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000832 if (shouldInvert)
833 switch (Op) {
834 default: break;
835 case BO_LT: Op = BO_GT; break;
836 case BO_GT: Op = BO_LT; break;
837 case BO_LE: Op = BO_GE; break;
838 case BO_GE: Op = BO_LE; break;
839 }
840
Ted Kremenek681bc112011-08-16 01:53:41 +0000841 if (!tookTrue)
842 switch (Op) {
843 case BO_EQ: Op = BO_NE; break;
844 case BO_NE: Op = BO_EQ; break;
845 case BO_LT: Op = BO_GE; break;
846 case BO_GT: Op = BO_LE; break;
847 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000848 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000849 default:
850 return 0;
851 }
852
Ted Kremenek6ae32572011-12-20 22:00:25 +0000853 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000854 case BO_EQ:
855 Out << "equal to ";
856 break;
857 case BO_NE:
858 Out << "not equal to ";
859 break;
860 default:
861 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
862 break;
863 }
864
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000865 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek76aadc32012-03-09 01:13:14 +0000866 const LocationContext *LCtx = N->getLocationContext();
867 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
868 PathDiagnosticEventPiece *event =
869 new PathDiagnosticEventPiece(Loc, Out.str());
870 if (shouldPrune.hasValue())
871 event->setPrunable(shouldPrune.getValue());
872 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000873}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000874
875PathDiagnosticPiece *
876ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
877 const Expr *CondVarExpr,
878 const bool tookTrue,
879 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000880 BugReport &report,
881 const ExplodedNode *N) {
Jordan Roseb0e1bad2012-08-03 23:08:54 +0000882 // FIXME: If there's already a constraint tracker for this variable,
883 // we shouldn't emit anything here (c.f. the double note in
884 // test/Analysis/inlining/path-notes.c)
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000885 SmallString<256> buf;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000886 llvm::raw_svector_ostream Out(buf);
887 Out << "Assuming " << LhsString << " is ";
888
889 QualType Ty = CondVarExpr->getType();
890
891 if (Ty->isPointerType())
892 Out << (tookTrue ? "not null" : "null");
893 else if (Ty->isObjCObjectPointerType())
894 Out << (tookTrue ? "not nil" : "nil");
895 else if (Ty->isBooleanType())
896 Out << (tookTrue ? "true" : "false");
897 else if (Ty->isIntegerType())
898 Out << (tookTrue ? "non-zero" : "zero");
899 else
900 return 0;
901
Ted Kremenek76aadc32012-03-09 01:13:14 +0000902 const LocationContext *LCtx = N->getLocationContext();
903 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
904 PathDiagnosticEventPiece *event =
905 new PathDiagnosticEventPiece(Loc, Out.str());
906
907 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
908 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
909 const ProgramState *state = N->getState().getPtr();
910 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
911 if (report.isInteresting(R))
912 event->setPrunable(false);
913 }
914 }
915 }
916
917 return event;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000918}
Ted Kremenek993124e2011-08-06 06:54:45 +0000919
920PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000921ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
922 const DeclRefExpr *DR,
923 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000924 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000925 BugReport &report,
926 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000927
928 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
929 if (!VD)
930 return 0;
931
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000932 SmallString<256> Buf;
Ted Kremenek993124e2011-08-06 06:54:45 +0000933 llvm::raw_svector_ostream Out(Buf);
934
935 Out << "Assuming '";
936 VD->getDeclName().printName(Out);
937 Out << "' is ";
938
939 QualType VDTy = VD->getType();
940
941 if (VDTy->isPointerType())
942 Out << (tookTrue ? "non-null" : "null");
943 else if (VDTy->isObjCObjectPointerType())
944 Out << (tookTrue ? "non-nil" : "nil");
945 else if (VDTy->isScalarType())
946 Out << (tookTrue ? "not equal to 0" : "0");
947 else
948 return 0;
949
Ted Kremenek76aadc32012-03-09 01:13:14 +0000950 const LocationContext *LCtx = N->getLocationContext();
951 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
952 PathDiagnosticEventPiece *event =
953 new PathDiagnosticEventPiece(Loc, Out.str());
954
955 const ProgramState *state = N->getState().getPtr();
956 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
957 if (report.isInteresting(R))
958 event->setPrunable(false);
959 else {
960 SVal V = state->getSVal(R);
961 if (report.isInteresting(V))
962 event->setPrunable(false);
963 }
964 }
965 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000966}
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000967
Anna Zaks80de4872012-08-29 21:22:37 +0000968PathDiagnosticPiece *
969UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N,
970 const ExplodedNode *PrevN,
971 BugReporterContext &BRC,
972 BugReport &BR) {
973
974 ProgramStateRef State = N->getState();
975 ProgramPoint ProgLoc = N->getLocation();
976
977 // We are only interested in visiting CallEnter nodes.
978 CallEnter *CEnter = dyn_cast<CallEnter>(&ProgLoc);
979 if (!CEnter)
980 return 0;
981
982 // Check if one of the arguments is the region the visitor is tracking.
983 CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager();
984 CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
985 unsigned Idx = 0;
986 for (CallEvent::param_iterator I = Call->param_begin(),
987 E = Call->param_end(); I != E; ++I, ++Idx) {
988 const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
989
Anna Zaks522fc212012-09-12 22:57:30 +0000990 // Are we tracking the argument or its subregion?
991 if ( !ArgReg || (ArgReg != R && !R->isSubRegionOf(ArgReg->StripCasts())))
Anna Zaks28694c12012-08-29 23:23:39 +0000992 continue;
Anna Zaks80de4872012-08-29 21:22:37 +0000993
994 // Check the function parameter type.
995 const ParmVarDecl *ParamDecl = *I;
996 assert(ParamDecl && "Formal parameter has no decl?");
997 QualType T = ParamDecl->getType();
998
999 if (!(T->isAnyPointerType() || T->isReferenceType())) {
1000 // Function can only change the value passed in by address.
Anna Zaks28694c12012-08-29 23:23:39 +00001001 continue;
Anna Zaks80de4872012-08-29 21:22:37 +00001002 }
1003
1004 // If it is a const pointer value, the function does not intend to
1005 // change the value.
1006 if (T->getPointeeType().isConstQualified())
Anna Zaks28694c12012-08-29 23:23:39 +00001007 continue;
Anna Zaks80de4872012-08-29 21:22:37 +00001008
1009 // Mark the call site (LocationContext) as interesting if the value of the
1010 // argument is undefined or '0'/'NULL'.
Anna Zaks522fc212012-09-12 22:57:30 +00001011 SVal BoundVal = State->getSVal(R);
Anna Zaks80de4872012-08-29 21:22:37 +00001012 if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
1013 BR.markInteresting(CEnter->getCalleeContext());
1014 return 0;
1015 }
1016 }
1017 return 0;
1018}