blob: a2d05852e8144c3bcf8d5ff9ef2d4fe9279462b0 [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"
Jordan Rose7aba1172012-08-28 00:50:42 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek53500662009-07-22 17:55:28 +000025
26using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000028
29//===----------------------------------------------------------------------===//
30// Utility functions.
31//===----------------------------------------------------------------------===//
32
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000033const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000034 // Pattern match for a few useful cases (do something smarter later):
35 // a[0], p->f, *p
Jordan Rose3682f1e2012-08-25 01:06:23 +000036 const PostStmt *Loc = N->getLocationAs<PostStmt>();
37 if (!Loc)
38 return 0;
39
Jordan Rose364b9f92012-08-27 20:18:30 +000040 const Expr *S = dyn_cast<Expr>(Loc->getStmt());
41 if (!S)
42 return 0;
43 S = S->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenek11abcec2012-05-02 00:31:29 +000045 while (true) {
46 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
47 assert(B->isAssignmentOp());
48 S = B->getLHS()->IgnoreParenCasts();
49 continue;
50 }
51 else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
52 if (U->getOpcode() == UO_Deref)
53 return U->getSubExpr()->IgnoreParenCasts();
54 }
55 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
56 return ME->getBase()->IgnoreParenCasts();
57 }
58 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
59 return AE->getBase();
60 }
61 break;
Ted Kremenek53500662009-07-22 17:55:28 +000062 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
64 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000065}
66
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000067const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000068 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000069 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
70 return BE->getRHS();
71 return NULL;
72}
73
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000074const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000075 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
76 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
77 return RS->getRetValue();
78 return NULL;
79}
80
81//===----------------------------------------------------------------------===//
82// Definitions for bug reporter visitors.
83//===----------------------------------------------------------------------===//
Anna Zaks23f395e2011-08-20 01:27:22 +000084
85PathDiagnosticPiece*
86BugReporterVisitor::getEndPath(BugReporterContext &BRC,
87 const ExplodedNode *EndPathNode,
88 BugReport &BR) {
89 return 0;
90}
91
92PathDiagnosticPiece*
93BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
94 const ExplodedNode *EndPathNode,
95 BugReport &BR) {
Anna Zaks5a0917d2012-02-16 03:41:01 +000096 PathDiagnosticLocation L =
97 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
Anna Zaks23f395e2011-08-20 01:27:22 +000098
99 BugReport::ranges_iterator Beg, End;
100 llvm::tie(Beg, End) = BR.getRanges();
101
102 // Only add the statement itself as a range if we didn't specify any
103 // special ranges for this report.
104 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
105 BR.getDescription(),
106 Beg == End);
107 for (; Beg != End; ++Beg)
108 P->addRange(*Beg);
109
110 return P;
111}
112
113
Jordan Rose7aba1172012-08-28 00:50:42 +0000114namespace {
115/// Emits an extra note at the return statement of an interesting stack frame.
116///
117/// The returned value is marked as an interesting value, and if it's null,
118/// adds a visitor to track where it became null.
119///
120/// This visitor is intended to be used when another visitor discovers that an
121/// interesting value comes from an inlined function call.
122class ReturnVisitor : public BugReporterVisitorImpl<ReturnVisitor> {
123 const StackFrameContext *StackFrame;
124 bool Satisfied;
125public:
126 ReturnVisitor(const StackFrameContext *Frame)
127 : StackFrame(Frame), Satisfied(false) {}
128
129 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
130 static int Tag = 0;
131 ID.AddPointer(&Tag);
132 ID.AddPointer(StackFrame);
133 }
134
135 /// Adds a ReturnVisitor if the given statement represents a call that was
136 /// inlined.
137 ///
138 /// This will search back through the ExplodedGraph, starting from the given
139 /// node, looking for when the given statement was processed. If it turns out
140 /// the statement is a call that was inlined, we add the visitor to the
141 /// bug report, so it can print a note later.
142 static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S,
143 BugReport &BR) {
144 if (!CallEvent::isCallStmt(S))
145 return;
146
147 // First, find when we processed the statement.
148 do {
149 if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>())
150 if (CEE->getCalleeContext()->getCallSite() == S)
151 break;
152 if (const StmtPoint *SP = Node->getLocationAs<StmtPoint>())
153 if (SP->getStmt() == S)
154 break;
155
156 Node = Node->getFirstPred();
157 } while (Node);
158
159 // Next, step over any post-statement checks.
160 while (Node && isa<PostStmt>(Node->getLocation()))
161 Node = Node->getFirstPred();
162
163 // Finally, see if we inlined the call.
164 if (Node)
165 if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>())
166 if (CEE->getCalleeContext()->getCallSite() == S)
167 BR.addVisitor(new ReturnVisitor(CEE->getCalleeContext()));
168
169 }
170
171 PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
172 const ExplodedNode *PrevN,
173 BugReporterContext &BRC,
174 BugReport &BR) {
175 if (Satisfied)
176 return 0;
177
178 // Only print a message at the interesting return statement.
179 if (N->getLocationContext() != StackFrame)
180 return 0;
181
182 const StmtPoint *SP = N->getLocationAs<StmtPoint>();
183 if (!SP)
184 return 0;
185
186 const ReturnStmt *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
187 if (!Ret)
188 return 0;
189
190 // Okay, we're at the right return statement, but do we have the return
191 // value available?
192 ProgramStateRef State = N->getState();
193 SVal V = State->getSVal(Ret, StackFrame);
194 if (V.isUnknownOrUndef())
195 return 0;
196
197 // Don't print any more notes after this one.
198 Satisfied = true;
199
200 // Build an appropriate message based on the return value.
201 SmallString<64> Msg;
202 llvm::raw_svector_ostream Out(Msg);
203
204 const Expr *RetE = Ret->getRetValue();
205 assert(RetE && "Tracking a return value for a void function");
206 RetE = RetE->IgnoreParenCasts();
207
208 // See if we know that the return value is 0.
209 ProgramStateRef StNonZero, StZero;
210 llvm::tie(StNonZero, StZero) = State->assume(cast<DefinedSVal>(V));
211 if (StZero && !StNonZero) {
212 // If we're returning 0, we should track where that 0 came from.
213 bugreporter::addTrackNullOrUndefValueVisitor(N, RetE, &BR);
214
215 if (isa<Loc>(V)) {
216 if (RetE->getType()->isObjCObjectPointerType())
217 Out << "Returning nil";
218 else
219 Out << "Returning null pointer";
220 } else {
221 Out << "Returning zero";
222 }
223 } else {
224 // FIXME: We can probably do better than this.
225 BR.markInteresting(V);
226 Out << "Value returned here";
227 }
228
229 // FIXME: We should have a more generalized location printing mechanism.
230 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetE))
231 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
232 Out << " (loaded from '" << *DD << "')";
233
234 PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame);
235 return new PathDiagnosticEventPiece(L, Out.str());
236 }
237};
238} // end anonymous namespace
239
240
Anna Zaks50bbc162011-08-19 22:33:38 +0000241void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
242 static int tag = 0;
243 ID.AddPointer(&tag);
244 ID.AddPointer(R);
245 ID.Add(V);
246}
Ted Kremenek53500662009-07-22 17:55:28 +0000247
Jordan Rose166b7bd2012-08-28 00:50:45 +0000248PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ,
249 const ExplodedNode *Pred,
250 BugReporterContext &BRC,
251 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Anna Zaks50bbc162011-08-19 22:33:38 +0000253 if (satisfied)
254 return NULL;
255
Jordan Rose166b7bd2012-08-28 00:50:45 +0000256 const ExplodedNode *StoreSite = 0;
257 const Expr *InitE = 0;
Anna Zaks50bbc162011-08-19 22:33:38 +0000258
Jordan Rose166b7bd2012-08-28 00:50:45 +0000259 // First see if we reached the declaration of the region.
260 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
261 if (const PostStmt *P = Pred->getLocationAs<PostStmt>()) {
262 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) {
263 if (DS->getSingleDecl() == VR->getDecl()) {
264 StoreSite = Pred;
265 InitE = VR->getDecl()->getInit();
Jordan Rose7aba1172012-08-28 00:50:42 +0000266 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000267 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000268 }
Ted Kremenek1b431022010-03-20 18:01:57 +0000269 }
270
Jordan Rose166b7bd2012-08-28 00:50:45 +0000271 // Otherwise, check that Succ has this binding and Pred does not, i.e. this is
272 // where the binding first occurred.
273 if (!StoreSite) {
274 if (Succ->getState()->getSVal(R) != V)
275 return NULL;
276 if (Pred->getState()->getSVal(R) == V)
277 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Jordan Rose166b7bd2012-08-28 00:50:45 +0000279 StoreSite = Succ;
280
281 // If this is an assignment expression, we can track the value
282 // being assigned.
283 if (const PostStmt *P = Succ->getLocationAs<PostStmt>())
284 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
285 if (BO->isAssignmentOp())
286 InitE = BO->getRHS();
287 }
288
289 if (!StoreSite)
290 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000291 satisfied = true;
Jordan Rose166b7bd2012-08-28 00:50:45 +0000292
293 // If the value that was stored came from an inlined call, make sure we
294 // step into the call.
295 if (InitE) {
296 InitE = InitE->IgnoreParenCasts();
297 ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE, BR);
298 }
299
300 // Okay, we've found the binding. Emit an appropriate message.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000301 SmallString<256> sbuf;
Anna Zaks50bbc162011-08-19 22:33:38 +0000302 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Jordan Rose166b7bd2012-08-28 00:50:45 +0000304 if (const PostStmt *PS = StoreSite->getLocationAs<PostStmt>()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000305 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Anna Zaks50bbc162011-08-19 22:33:38 +0000307 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000308 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000309 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000310 else
Ted Kremenek53500662009-07-22 17:55:28 +0000311 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenek53500662009-07-22 17:55:28 +0000313 if (isa<loc::ConcreteInt>(V)) {
314 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000315 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000316 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000317 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000318 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000319 b = true;
320 }
321 }
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek53500662009-07-22 17:55:28 +0000324 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000325 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000326 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000327 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000328 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000329 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000330 else if (V.isUndef()) {
331 if (isa<VarRegion>(R)) {
332 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
333 if (VD->getInit())
334 os << "initialized to a garbage value";
335 else
336 os << "declared without an initial value";
337 }
Ted Kremenek53500662009-07-22 17:55:28 +0000338 }
Jordan Rose68537992012-08-03 23:09:01 +0000339 else {
340 os << "initialized here";
341 }
Ted Kremenek53500662009-07-22 17:55:28 +0000342 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000343 }
344
345 if (os.str().empty()) {
346 if (isa<loc::ConcreteInt>(V)) {
347 bool b = false;
348 if (R->isBoundable()) {
349 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
350 if (TR->getValueType()->isObjCObjectPointerType()) {
351 os << "nil object reference stored to ";
352 b = true;
353 }
354 }
355 }
356
357 if (!b)
358 os << "Null pointer value stored to ";
359 }
360 else if (V.isUndef()) {
361 os << "Uninitialized value stored to ";
362 }
363 else if (isa<nonloc::ConcreteInt>(V)) {
364 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
365 << " is assigned to ";
366 }
367 else
Jordan Rose68537992012-08-03 23:09:01 +0000368 os << "Value assigned to ";
Anna Zaks50bbc162011-08-19 22:33:38 +0000369
370 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000371 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000372 }
373 else
374 return NULL;
375 }
376
Anna Zaks50bbc162011-08-19 22:33:38 +0000377 // Construct a new PathDiagnosticPiece.
Jordan Rose166b7bd2012-08-28 00:50:45 +0000378 ProgramPoint P = StoreSite->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000379 PathDiagnosticLocation L =
380 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000381 if (!L.isValid())
382 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000383 return new PathDiagnosticEventPiece(L, os.str());
384}
385
386void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
387 static int tag = 0;
388 ID.AddPointer(&tag);
389 ID.AddBoolean(Assumption);
390 ID.Add(Constraint);
391}
392
393PathDiagnosticPiece *
394TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
395 const ExplodedNode *PrevN,
396 BugReporterContext &BRC,
397 BugReport &BR) {
398 if (isSatisfied)
399 return NULL;
400
401 // Check if in the previous state it was feasible for this constraint
402 // to *not* be true.
403 if (PrevN->getState()->assume(Constraint, !Assumption)) {
404
405 isSatisfied = true;
406
407 // As a sanity check, make sure that the negation of the constraint
408 // was infeasible in the current state. If it is feasible, we somehow
409 // missed the transition point.
410 if (N->getState()->assume(Constraint, !Assumption))
411 return NULL;
412
413 // We found the transition point for the constraint. We now need to
414 // pretty-print the constraint. (work-in-progress)
415 std::string sbuf;
416 llvm::raw_string_ostream os(sbuf);
417
418 if (isa<Loc>(Constraint)) {
419 os << "Assuming pointer value is ";
420 os << (Assumption ? "non-null" : "null");
421 }
422
423 if (os.str().empty())
424 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek53500662009-07-22 17:55:28 +0000426 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000427 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000428 PathDiagnosticLocation L =
429 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000430 if (!L.isValid())
431 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000432 return new PathDiagnosticEventPiece(L, os.str());
433 }
Ted Kremenek53500662009-07-22 17:55:28 +0000434
Anna Zaks50bbc162011-08-19 22:33:38 +0000435 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000436}
437
Jordan Rose68537992012-08-03 23:09:01 +0000438void bugreporter::addTrackNullOrUndefValueVisitor(const ExplodedNode *N,
439 const Stmt *S,
440 BugReport *report) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000441 if (!S || !N)
Jordan Rose68537992012-08-03 23:09:01 +0000442 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Anna Zaks8e6431a2011-08-18 22:37:56 +0000444 ProgramStateManager &StateMgr = N->getState()->getStateManager();
445
Jordan Rose364b9f92012-08-27 20:18:30 +0000446 // Walk through nodes until we get one that matches the statement exactly.
Ted Kremenek88299892011-07-28 23:07:59 +0000447 while (N) {
448 const ProgramPoint &pp = N->getLocation();
449 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
450 if (ps->getStmt() == S)
451 break;
Jordan Rose23df2432012-08-24 16:34:31 +0000452 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) {
453 if (CEE->getCalleeContext()->getCallSite() == S)
454 break;
Ted Kremenek88299892011-07-28 23:07:59 +0000455 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000456 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000457 }
458
459 if (!N)
Jordan Rose68537992012-08-03 23:09:01 +0000460 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000461
Ted Kremenek8bef8232012-01-26 21:29:00 +0000462 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Jordan Rose364b9f92012-08-27 20:18:30 +0000464 // See if the expression we're interested refers to a variable.
465 // If so, we can track both its contents and constraints on its value.
466 if (const Expr *Ex = dyn_cast<Expr>(S)) {
467 // Strip off parens and casts. Note that this will never have issues with
468 // C++ user-defined implicit conversions, because those have a constructor
469 // or function call inside.
Jordan Rosee6cd0542012-08-16 00:03:33 +0000470 Ex = Ex->IgnoreParenCasts();
Ted Kremenek76aadc32012-03-09 01:13:14 +0000471 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000472 // FIXME: Right now we only track VarDecls because it's non-trivial to
473 // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812>
Ted Kremenek76aadc32012-03-09 01:13:14 +0000474 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
475 const VarRegion *R =
476 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Jordan Rose364b9f92012-08-27 20:18:30 +0000478 // Mark both the variable region and its contents as interesting.
Ted Kremenek11abcec2012-05-02 00:31:29 +0000479 SVal V = state->getRawSVal(loc::MemRegionVal(R));
Ted Kremenek76aadc32012-03-09 01:13:14 +0000480 report->markInteresting(R);
481 report->markInteresting(V);
Jordan Rose68537992012-08-03 23:09:01 +0000482
Jordan Rose364b9f92012-08-27 20:18:30 +0000483 // If the contents are symbolic, find out when they became null.
Jordan Rose68537992012-08-03 23:09:01 +0000484 if (V.getAsLocSymbol()) {
485 BugReporterVisitor *ConstraintTracker
486 = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false);
487 report->addVisitor(ConstraintTracker);
488 }
489
490 report->addVisitor(new FindLastStoreBRVisitor(V, R));
491 return;
Ted Kremenek53500662009-07-22 17:55:28 +0000492 }
493 }
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Jordan Rose364b9f92012-08-27 20:18:30 +0000496 // If the expression does NOT refer to a variable, we can still track
497 // constraints on its contents.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000498 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek53500662009-07-22 17:55:28 +0000500 // Uncomment this to find cases where we aren't properly getting the
501 // base value that was dereferenced.
502 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek53500662009-07-22 17:55:28 +0000504 // Is it a symbolic value?
505 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000506 const MemRegion *Base = L->getRegion()->getBaseRegion();
507 if (isa<SymbolicRegion>(Base)) {
508 report->markInteresting(Base);
509 report->addVisitor(new TrackConstraintBRVisitor(loc::MemRegionVal(Base),
Jordan Rose68537992012-08-03 23:09:01 +0000510 false));
Ted Kremenek53500662009-07-22 17:55:28 +0000511 }
Jordan Rose7aba1172012-08-28 00:50:42 +0000512 } else {
513 // Otherwise, if the value came from an inlined function call,
514 // we should at least make sure that function isn't pruned in our output.
515 ReturnVisitor::addVisitorIfNecessary(N, S, *report);
Ted Kremenek53500662009-07-22 17:55:28 +0000516 }
517}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000518
Anna Zaks50bbc162011-08-19 22:33:38 +0000519BugReporterVisitor *
520FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
521 const MemRegion *R) {
522 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000523
Ted Kremenek8bef8232012-01-26 21:29:00 +0000524 ProgramStateRef state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000525 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000526 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000527 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000528
Anna Zaks50bbc162011-08-19 22:33:38 +0000529 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000530}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000531
532
Anna Zaks50bbc162011-08-19 22:33:38 +0000533PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
534 const ExplodedNode *PrevN,
535 BugReporterContext &BRC,
536 BugReport &BR) {
537 const PostStmt *P = N->getLocationAs<PostStmt>();
538 if (!P)
539 return 0;
540 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
541 if (!ME)
542 return 0;
543 const Expr *Receiver = ME->getInstanceReceiver();
544 if (!Receiver)
545 return 0;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000546 ProgramStateRef state = N->getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000547 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
Anna Zaks50bbc162011-08-19 22:33:38 +0000548 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
549 if (!DV)
550 return 0;
551 state = state->assume(*DV, true);
552 if (state)
553 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000554
Anna Zaks50bbc162011-08-19 22:33:38 +0000555 // The receiver was nil, and hence the method was skipped.
556 // Register a BugReporterVisitor to issue a message telling us how
557 // the receiver was null.
Jordan Rose68537992012-08-03 23:09:01 +0000558 bugreporter::addTrackNullOrUndefValueVisitor(N, Receiver, &BR);
Anna Zaks50bbc162011-08-19 22:33:38 +0000559 // Issue a message saying that the method was skipped.
Anna Zaks220ac8c2011-09-15 01:08:34 +0000560 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
561 N->getLocationContext());
Anna Zaks907344e2012-04-05 02:10:19 +0000562 return new PathDiagnosticEventPiece(L, "No method is called "
Anna Zaks50bbc162011-08-19 22:33:38 +0000563 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000564}
Tom Care2bbbe502010-09-02 23:30:22 +0000565
Anna Zaks8e6431a2011-08-18 22:37:56 +0000566// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000567void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
568 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000569 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000570 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000571 WorkList.push_back(S);
572
573 while (!WorkList.empty()) {
574 const Stmt *Head = WorkList.front();
575 WorkList.pop_front();
576
Ted Kremenek8bef8232012-01-26 21:29:00 +0000577 ProgramStateRef state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000578 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000579
580 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
581 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
582 const VarRegion *R =
583 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
584
585 // What did we load?
Ted Kremenek5eca4822012-01-06 22:09:28 +0000586 SVal V = state->getSVal(S, N->getLocationContext());
Tom Care2bbbe502010-09-02 23:30:22 +0000587
588 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000589 // Register a new visitor with the BugReport.
590 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000591 }
592 }
593 }
594
595 for (Stmt::const_child_iterator I = Head->child_begin();
596 I != Head->child_end(); ++I)
597 WorkList.push_back(*I);
598 }
599}
Ted Kremenek993124e2011-08-06 06:54:45 +0000600
601//===----------------------------------------------------------------------===//
602// Visitor that tries to report interesting diagnostics from conditions.
603//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000604PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
605 const ExplodedNode *Prev,
606 BugReporterContext &BRC,
607 BugReport &BR) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000608 PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR);
609 if (PathDiagnosticEventPiece *ev =
610 dyn_cast_or_null<PathDiagnosticEventPiece>(piece))
Ted Kremenek76aadc32012-03-09 01:13:14 +0000611 ev->setPrunable(true, /* override */ false);
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000612 return piece;
613}
614
615PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
616 const ExplodedNode *Prev,
617 BugReporterContext &BRC,
618 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000619
620 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000621
Ted Kremenek8bef8232012-01-26 21:29:00 +0000622 ProgramStateRef CurrentState = N->getState();
623 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000624
625 // Compare the GDMs of the state, because that is where constraints
626 // are managed. Note that ensure that we only look at nodes that
627 // were generated by the analyzer engine proper, not checkers.
628 if (CurrentState->getGDM().getRoot() ==
629 PrevState->getGDM().getRoot())
630 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000631
632 // If an assumption was made on a branch, it should be caught
633 // here by looking at the state transition.
634 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000635 const CFGBlock *srcBlk = BE->getSrc();
636 if (const Stmt *term = srcBlk->getTerminator())
Ted Kremenek76aadc32012-03-09 01:13:14 +0000637 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000638 return 0;
639 }
640
641 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
642 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
643 // violation.
644 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
645 cast<GRBugReporter>(BRC.getBugReporter()).
646 getEngine().getEagerlyAssumeTags();
647
648 const ProgramPointTag *tag = PS->getTag();
649 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000650 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000651 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000652 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000653 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000654 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000655
Ted Kremenek993124e2011-08-06 06:54:45 +0000656 return 0;
657 }
658
659 return 0;
660}
661
662PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000663ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000664 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000665 const CFGBlock *srcBlk,
666 const CFGBlock *dstBlk,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000667 BugReport &R,
Anna Zaks50bbc162011-08-19 22:33:38 +0000668 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000669 const Expr *Cond = 0;
670
671 switch (Term->getStmtClass()) {
672 default:
673 return 0;
674 case Stmt::IfStmtClass:
675 Cond = cast<IfStmt>(Term)->getCond();
676 break;
677 case Stmt::ConditionalOperatorClass:
678 Cond = cast<ConditionalOperator>(Term)->getCond();
679 break;
680 }
681
682 assert(Cond);
683 assert(srcBlk->succ_size() == 2);
684 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
685 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
Ted Kremenek76aadc32012-03-09 01:13:14 +0000686 tookTrue, BRC, R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000687}
688
689PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000690ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
691 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000692 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000693 BugReport &R,
694 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000695
696 const Expr *Ex = Cond;
697
Ted Kremenek681bc112011-08-16 01:53:41 +0000698 while (true) {
699 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000700 switch (Ex->getStmtClass()) {
701 default:
702 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000703 case Stmt::BinaryOperatorClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000704 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
705 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000706 case Stmt::DeclRefExprClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000707 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
708 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000709 case Stmt::UnaryOperatorClass: {
710 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
711 if (UO->getOpcode() == UO_LNot) {
712 tookTrue = !tookTrue;
713 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
714 continue;
715 }
716 return 0;
717 }
718 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000719 }
720}
721
Anna Zaks50bbc162011-08-19 22:33:38 +0000722bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000723 BugReporterContext &BRC,
724 BugReport &report,
725 const ExplodedNode *N,
726 llvm::Optional<bool> &prunable) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000727 const Expr *OriginalExpr = Ex;
728 Ex = Ex->IgnoreParenCasts();
729
730 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000731 const bool quotes = isa<VarDecl>(DR->getDecl());
Ted Kremenek76aadc32012-03-09 01:13:14 +0000732 if (quotes) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000733 Out << '\'';
Ted Kremenek76aadc32012-03-09 01:13:14 +0000734 const LocationContext *LCtx = N->getLocationContext();
735 const ProgramState *state = N->getState().getPtr();
736 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
737 LCtx).getAsRegion()) {
738 if (report.isInteresting(R))
739 prunable = false;
740 else {
741 const ProgramState *state = N->getState().getPtr();
742 SVal V = state->getSVal(R);
743 if (report.isInteresting(V))
744 prunable = false;
745 }
746 }
747 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000748 Out << DR->getDecl()->getDeclName().getAsString();
749 if (quotes)
750 Out << '\'';
751 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000752 }
753
754 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
755 QualType OriginalTy = OriginalExpr->getType();
756 if (OriginalTy->isPointerType()) {
757 if (IL->getValue() == 0) {
758 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000759 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000760 }
761 }
762 else if (OriginalTy->isObjCObjectPointerType()) {
763 if (IL->getValue() == 0) {
764 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000765 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000766 }
767 }
768
769 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000770 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000771 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000772
773 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000774}
775
776PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000777ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
778 const BinaryOperator *BExpr,
779 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000780 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000781 BugReport &R,
782 const ExplodedNode *N) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000783
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000784 bool shouldInvert = false;
Ted Kremenek76aadc32012-03-09 01:13:14 +0000785 llvm::Optional<bool> shouldPrune;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000786
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000787 SmallString<128> LhsString, RhsString;
Ted Kremenek681bc112011-08-16 01:53:41 +0000788 {
Ted Kremenek76aadc32012-03-09 01:13:14 +0000789 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
790 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
791 shouldPrune);
792 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
793 shouldPrune);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000794
795 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000796 }
797
Ted Kremenekd1247c52012-01-04 08:18:09 +0000798 BinaryOperator::Opcode Op = BExpr->getOpcode();
799
800 if (BinaryOperator::isAssignmentOp(Op)) {
801 // For assignment operators, all that we care about is that the LHS
802 // evaluates to "true" or "false".
803 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000804 BRC, R, N);
Ted Kremenekd1247c52012-01-04 08:18:09 +0000805 }
806
807 // For non-assignment operations, we require that we can understand
808 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000809 if (LhsString.empty() || RhsString.empty())
810 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000811
Ted Kremenekd1247c52012-01-04 08:18:09 +0000812 // Should we invert the strings if the LHS is not a variable name?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000813 SmallString<256> buf;
Ted Kremenek681bc112011-08-16 01:53:41 +0000814 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000815 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000816
817 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000818 if (shouldInvert)
819 switch (Op) {
820 default: break;
821 case BO_LT: Op = BO_GT; break;
822 case BO_GT: Op = BO_LT; break;
823 case BO_LE: Op = BO_GE; break;
824 case BO_GE: Op = BO_LE; break;
825 }
826
Ted Kremenek681bc112011-08-16 01:53:41 +0000827 if (!tookTrue)
828 switch (Op) {
829 case BO_EQ: Op = BO_NE; break;
830 case BO_NE: Op = BO_EQ; break;
831 case BO_LT: Op = BO_GE; break;
832 case BO_GT: Op = BO_LE; break;
833 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000834 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000835 default:
836 return 0;
837 }
838
Ted Kremenek6ae32572011-12-20 22:00:25 +0000839 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000840 case BO_EQ:
841 Out << "equal to ";
842 break;
843 case BO_NE:
844 Out << "not equal to ";
845 break;
846 default:
847 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
848 break;
849 }
850
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000851 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek76aadc32012-03-09 01:13:14 +0000852 const LocationContext *LCtx = N->getLocationContext();
853 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
854 PathDiagnosticEventPiece *event =
855 new PathDiagnosticEventPiece(Loc, Out.str());
856 if (shouldPrune.hasValue())
857 event->setPrunable(shouldPrune.getValue());
858 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000859}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000860
861PathDiagnosticPiece *
862ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
863 const Expr *CondVarExpr,
864 const bool tookTrue,
865 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000866 BugReport &report,
867 const ExplodedNode *N) {
Jordan Roseb0e1bad2012-08-03 23:08:54 +0000868 // FIXME: If there's already a constraint tracker for this variable,
869 // we shouldn't emit anything here (c.f. the double note in
870 // test/Analysis/inlining/path-notes.c)
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000871 SmallString<256> buf;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000872 llvm::raw_svector_ostream Out(buf);
873 Out << "Assuming " << LhsString << " is ";
874
875 QualType Ty = CondVarExpr->getType();
876
877 if (Ty->isPointerType())
878 Out << (tookTrue ? "not null" : "null");
879 else if (Ty->isObjCObjectPointerType())
880 Out << (tookTrue ? "not nil" : "nil");
881 else if (Ty->isBooleanType())
882 Out << (tookTrue ? "true" : "false");
883 else if (Ty->isIntegerType())
884 Out << (tookTrue ? "non-zero" : "zero");
885 else
886 return 0;
887
Ted Kremenek76aadc32012-03-09 01:13:14 +0000888 const LocationContext *LCtx = N->getLocationContext();
889 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
890 PathDiagnosticEventPiece *event =
891 new PathDiagnosticEventPiece(Loc, Out.str());
892
893 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
894 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
895 const ProgramState *state = N->getState().getPtr();
896 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
897 if (report.isInteresting(R))
898 event->setPrunable(false);
899 }
900 }
901 }
902
903 return event;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000904}
Ted Kremenek993124e2011-08-06 06:54:45 +0000905
906PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000907ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
908 const DeclRefExpr *DR,
909 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000910 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000911 BugReport &report,
912 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000913
914 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
915 if (!VD)
916 return 0;
917
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000918 SmallString<256> Buf;
Ted Kremenek993124e2011-08-06 06:54:45 +0000919 llvm::raw_svector_ostream Out(Buf);
920
921 Out << "Assuming '";
922 VD->getDeclName().printName(Out);
923 Out << "' is ";
924
925 QualType VDTy = VD->getType();
926
927 if (VDTy->isPointerType())
928 Out << (tookTrue ? "non-null" : "null");
929 else if (VDTy->isObjCObjectPointerType())
930 Out << (tookTrue ? "non-nil" : "nil");
931 else if (VDTy->isScalarType())
932 Out << (tookTrue ? "not equal to 0" : "0");
933 else
934 return 0;
935
Ted Kremenek76aadc32012-03-09 01:13:14 +0000936 const LocationContext *LCtx = N->getLocationContext();
937 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
938 PathDiagnosticEventPiece *event =
939 new PathDiagnosticEventPiece(Loc, Out.str());
940
941 const ProgramState *state = N->getState().getPtr();
942 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
943 if (report.isInteresting(R))
944 event->setPrunable(false);
945 else {
946 SVal V = state->getSVal(R);
947 if (report.isInteresting(V))
948 event->setPrunable(false);
949 }
950 }
951 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000952}
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000953