blob: ae8ed00783637a95d6ff237c8342b087ce4cc837 [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
Anna Zaks50bbc162011-08-19 22:33:38 +0000248PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
249 const ExplodedNode *PrevN,
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
256 if (!StoreSite) {
Jordan Rose20165e72012-08-03 23:08:42 +0000257 // Make sure the region is actually bound to value V here.
258 // This is necessary because the region may not actually be live at the
259 // report's error node.
260 if (N->getState()->getSVal(R) != V)
261 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000262
Jordan Rose20165e72012-08-03 23:08:42 +0000263 const ExplodedNode *Node = N, *Last = N;
Jordan Rose7aba1172012-08-28 00:50:42 +0000264 const Expr *InitE = 0;
Jordan Rose20165e72012-08-03 23:08:42 +0000265
266 // Now look for the store of V.
Ted Kremenekd2e70902012-01-25 22:18:04 +0000267 for ( ; Node ; Node = Node->getFirstPred()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000268 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
269 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
270 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
271 if (DS->getSingleDecl() == VR->getDecl()) {
Ted Kremenekd2e70902012-01-25 22:18:04 +0000272 // Record the last seen initialization point.
Anna Zaks50bbc162011-08-19 22:33:38 +0000273 Last = Node;
Jordan Rose7aba1172012-08-28 00:50:42 +0000274 InitE = VR->getDecl()->getInit();
Anna Zaks50bbc162011-08-19 22:33:38 +0000275 break;
276 }
277 }
278
Ted Kremenekd2e70902012-01-25 22:18:04 +0000279 // Does the region still bind to value V? If not, we are done
280 // looking for store sites.
Jordan Rose7aba1172012-08-28 00:50:42 +0000281 SVal Current = Node->getState()->getSVal(R);
282 if (Current != V) {
283 // If this is an assignment expression, we can track the value
284 // being assigned.
285 if (const StmtPoint *SP = Last->getLocationAs<StmtPoint>()) {
286 const Stmt *S = SP->getStmt();
287 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
288 if (BO->isAssignmentOp())
289 InitE = BO->getRHS();
290 }
291
Anna Zaks50bbc162011-08-19 22:33:38 +0000292 break;
Jordan Rose7aba1172012-08-28 00:50:42 +0000293 }
Jordan Rose20165e72012-08-03 23:08:42 +0000294
295 Last = Node;
Anna Zaks50bbc162011-08-19 22:33:38 +0000296 }
297
Jordan Rose20165e72012-08-03 23:08:42 +0000298 if (!Node) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000299 satisfied = true;
300 return NULL;
301 }
302
303 StoreSite = Last;
Jordan Rose7aba1172012-08-28 00:50:42 +0000304
305 // If the value that was stored came from an inlined call, make sure we
306 // step into the call.
307 if (InitE) {
308 InitE = InitE->IgnoreParenCasts();
309 ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE, BR);
310 }
Ted Kremenek1b431022010-03-20 18:01:57 +0000311 }
312
Anna Zaks50bbc162011-08-19 22:33:38 +0000313 if (StoreSite != N)
314 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Anna Zaks50bbc162011-08-19 22:33:38 +0000316 satisfied = true;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000317 SmallString<256> sbuf;
Anna Zaks50bbc162011-08-19 22:33:38 +0000318 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Anna Zaks50bbc162011-08-19 22:33:38 +0000320 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
321 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Anna Zaks50bbc162011-08-19 22:33:38 +0000323 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000324 os << "Variable '" << *VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000325 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000326 else
Ted Kremenek53500662009-07-22 17:55:28 +0000327 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek53500662009-07-22 17:55:28 +0000329 if (isa<loc::ConcreteInt>(V)) {
330 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000331 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000332 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000333 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000334 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000335 b = true;
336 }
337 }
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek53500662009-07-22 17:55:28 +0000340 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000341 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000342 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000343 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000344 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000345 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000346 else if (V.isUndef()) {
347 if (isa<VarRegion>(R)) {
348 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
349 if (VD->getInit())
350 os << "initialized to a garbage value";
351 else
352 os << "declared without an initial value";
353 }
Ted Kremenek53500662009-07-22 17:55:28 +0000354 }
Jordan Rose68537992012-08-03 23:09:01 +0000355 else {
356 os << "initialized here";
357 }
Ted Kremenek53500662009-07-22 17:55:28 +0000358 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000359 }
360
361 if (os.str().empty()) {
362 if (isa<loc::ConcreteInt>(V)) {
363 bool b = false;
364 if (R->isBoundable()) {
365 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
366 if (TR->getValueType()->isObjCObjectPointerType()) {
367 os << "nil object reference stored to ";
368 b = true;
369 }
370 }
371 }
372
373 if (!b)
374 os << "Null pointer value stored to ";
375 }
376 else if (V.isUndef()) {
377 os << "Uninitialized value stored to ";
378 }
379 else if (isa<nonloc::ConcreteInt>(V)) {
380 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
381 << " is assigned to ";
382 }
383 else
Jordan Rose68537992012-08-03 23:09:01 +0000384 os << "Value assigned to ";
Anna Zaks50bbc162011-08-19 22:33:38 +0000385
386 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000387 os << '\'' << *VR->getDecl() << '\'';
Anna Zaks50bbc162011-08-19 22:33:38 +0000388 }
389 else
390 return NULL;
391 }
392
Anna Zaks50bbc162011-08-19 22:33:38 +0000393 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000394 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000395 PathDiagnosticLocation L =
396 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000397 if (!L.isValid())
398 return NULL;
Anna Zaks50bbc162011-08-19 22:33:38 +0000399 return new PathDiagnosticEventPiece(L, os.str());
400}
401
402void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
403 static int tag = 0;
404 ID.AddPointer(&tag);
405 ID.AddBoolean(Assumption);
406 ID.Add(Constraint);
407}
408
409PathDiagnosticPiece *
410TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
411 const ExplodedNode *PrevN,
412 BugReporterContext &BRC,
413 BugReport &BR) {
414 if (isSatisfied)
415 return NULL;
416
417 // Check if in the previous state it was feasible for this constraint
418 // to *not* be true.
419 if (PrevN->getState()->assume(Constraint, !Assumption)) {
420
421 isSatisfied = true;
422
423 // As a sanity check, make sure that the negation of the constraint
424 // was infeasible in the current state. If it is feasible, we somehow
425 // missed the transition point.
426 if (N->getState()->assume(Constraint, !Assumption))
427 return NULL;
428
429 // We found the transition point for the constraint. We now need to
430 // pretty-print the constraint. (work-in-progress)
431 std::string sbuf;
432 llvm::raw_string_ostream os(sbuf);
433
434 if (isa<Loc>(Constraint)) {
435 os << "Assuming pointer value is ";
436 os << (Assumption ? "non-null" : "null");
437 }
438
439 if (os.str().empty())
440 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek53500662009-07-22 17:55:28 +0000442 // Construct a new PathDiagnosticPiece.
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000443 ProgramPoint P = N->getLocation();
Anna Zaks1531bb02011-09-20 01:38:47 +0000444 PathDiagnosticLocation L =
445 PathDiagnosticLocation::create(P, BRC.getSourceManager());
Anna Zaks4fdf97b2011-09-15 18:56:07 +0000446 if (!L.isValid())
447 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000448 return new PathDiagnosticEventPiece(L, os.str());
449 }
Ted Kremenek53500662009-07-22 17:55:28 +0000450
Anna Zaks50bbc162011-08-19 22:33:38 +0000451 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000452}
453
Jordan Rose68537992012-08-03 23:09:01 +0000454void bugreporter::addTrackNullOrUndefValueVisitor(const ExplodedNode *N,
455 const Stmt *S,
456 BugReport *report) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000457 if (!S || !N)
Jordan Rose68537992012-08-03 23:09:01 +0000458 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Anna Zaks8e6431a2011-08-18 22:37:56 +0000460 ProgramStateManager &StateMgr = N->getState()->getStateManager();
461
Jordan Rose364b9f92012-08-27 20:18:30 +0000462 // Walk through nodes until we get one that matches the statement exactly.
Ted Kremenek88299892011-07-28 23:07:59 +0000463 while (N) {
464 const ProgramPoint &pp = N->getLocation();
465 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
466 if (ps->getStmt() == S)
467 break;
Jordan Rose23df2432012-08-24 16:34:31 +0000468 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) {
469 if (CEE->getCalleeContext()->getCallSite() == S)
470 break;
Ted Kremenek88299892011-07-28 23:07:59 +0000471 }
Anna Zaksb459cf32011-10-01 06:35:19 +0000472 N = N->getFirstPred();
Ted Kremenek88299892011-07-28 23:07:59 +0000473 }
474
475 if (!N)
Jordan Rose68537992012-08-03 23:09:01 +0000476 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000477
Ted Kremenek8bef8232012-01-26 21:29:00 +0000478 ProgramStateRef state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Jordan Rose364b9f92012-08-27 20:18:30 +0000480 // See if the expression we're interested refers to a variable.
481 // If so, we can track both its contents and constraints on its value.
482 if (const Expr *Ex = dyn_cast<Expr>(S)) {
483 // Strip off parens and casts. Note that this will never have issues with
484 // C++ user-defined implicit conversions, because those have a constructor
485 // or function call inside.
Jordan Rosee6cd0542012-08-16 00:03:33 +0000486 Ex = Ex->IgnoreParenCasts();
Ted Kremenek76aadc32012-03-09 01:13:14 +0000487 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000488 // FIXME: Right now we only track VarDecls because it's non-trivial to
489 // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812>
Ted Kremenek76aadc32012-03-09 01:13:14 +0000490 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
491 const VarRegion *R =
492 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Jordan Rose364b9f92012-08-27 20:18:30 +0000494 // Mark both the variable region and its contents as interesting.
Ted Kremenek11abcec2012-05-02 00:31:29 +0000495 SVal V = state->getRawSVal(loc::MemRegionVal(R));
Ted Kremenek76aadc32012-03-09 01:13:14 +0000496 report->markInteresting(R);
497 report->markInteresting(V);
Jordan Rose68537992012-08-03 23:09:01 +0000498
Jordan Rose364b9f92012-08-27 20:18:30 +0000499 // If the contents are symbolic, find out when they became null.
Jordan Rose68537992012-08-03 23:09:01 +0000500 if (V.getAsLocSymbol()) {
501 BugReporterVisitor *ConstraintTracker
502 = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false);
503 report->addVisitor(ConstraintTracker);
504 }
505
506 report->addVisitor(new FindLastStoreBRVisitor(V, R));
507 return;
Ted Kremenek53500662009-07-22 17:55:28 +0000508 }
509 }
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Jordan Rose364b9f92012-08-27 20:18:30 +0000512 // If the expression does NOT refer to a variable, we can still track
513 // constraints on its contents.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000514 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek53500662009-07-22 17:55:28 +0000516 // Uncomment this to find cases where we aren't properly getting the
517 // base value that was dereferenced.
518 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek53500662009-07-22 17:55:28 +0000520 // Is it a symbolic value?
521 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
Jordan Rose364b9f92012-08-27 20:18:30 +0000522 const MemRegion *Base = L->getRegion()->getBaseRegion();
523 if (isa<SymbolicRegion>(Base)) {
524 report->markInteresting(Base);
525 report->addVisitor(new TrackConstraintBRVisitor(loc::MemRegionVal(Base),
Jordan Rose68537992012-08-03 23:09:01 +0000526 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.
531 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 Rose68537992012-08-03 23:09:01 +0000574 bugreporter::addTrackNullOrUndefValueVisitor(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
636 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000637
Ted Kremenek8bef8232012-01-26 21:29:00 +0000638 ProgramStateRef CurrentState = N->getState();
639 ProgramStateRef PrevState = Prev->getState();
Ted Kremenek681bc112011-08-16 01:53:41 +0000640
641 // Compare the GDMs of the state, because that is where constraints
642 // are managed. Note that ensure that we only look at nodes that
643 // were generated by the analyzer engine proper, not checkers.
644 if (CurrentState->getGDM().getRoot() ==
645 PrevState->getGDM().getRoot())
646 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000647
648 // If an assumption was made on a branch, it should be caught
649 // here by looking at the state transition.
650 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000651 const CFGBlock *srcBlk = BE->getSrc();
652 if (const Stmt *term = srcBlk->getTerminator())
Ted Kremenek76aadc32012-03-09 01:13:14 +0000653 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
Ted Kremenek681bc112011-08-16 01:53:41 +0000654 return 0;
655 }
656
657 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
658 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
659 // violation.
660 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
661 cast<GRBugReporter>(BRC.getBugReporter()).
662 getEngine().getEagerlyAssumeTags();
663
664 const ProgramPointTag *tag = PS->getTag();
665 if (tag == tags.first)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000666 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000667 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000668 if (tag == tags.second)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000669 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000670 BRC, BR, N);
Ted Kremenek681bc112011-08-16 01:53:41 +0000671
Ted Kremenek993124e2011-08-06 06:54:45 +0000672 return 0;
673 }
674
675 return 0;
676}
677
678PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000679ConditionBRVisitor::VisitTerminator(const Stmt *Term,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000680 const ExplodedNode *N,
Anna Zaks50bbc162011-08-19 22:33:38 +0000681 const CFGBlock *srcBlk,
682 const CFGBlock *dstBlk,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000683 BugReport &R,
Anna Zaks50bbc162011-08-19 22:33:38 +0000684 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000685 const Expr *Cond = 0;
686
687 switch (Term->getStmtClass()) {
688 default:
689 return 0;
690 case Stmt::IfStmtClass:
691 Cond = cast<IfStmt>(Term)->getCond();
692 break;
693 case Stmt::ConditionalOperatorClass:
694 Cond = cast<ConditionalOperator>(Term)->getCond();
695 break;
696 }
697
698 assert(Cond);
699 assert(srcBlk->succ_size() == 2);
700 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
701 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
Ted Kremenek76aadc32012-03-09 01:13:14 +0000702 tookTrue, BRC, R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000703}
704
705PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000706ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
707 bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000708 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000709 BugReport &R,
710 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000711
712 const Expr *Ex = Cond;
713
Ted Kremenek681bc112011-08-16 01:53:41 +0000714 while (true) {
715 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000716 switch (Ex->getStmtClass()) {
717 default:
718 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000719 case Stmt::BinaryOperatorClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000720 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
721 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000722 case Stmt::DeclRefExprClass:
Ted Kremenek76aadc32012-03-09 01:13:14 +0000723 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
724 R, N);
Ted Kremenek993124e2011-08-06 06:54:45 +0000725 case Stmt::UnaryOperatorClass: {
726 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
727 if (UO->getOpcode() == UO_LNot) {
728 tookTrue = !tookTrue;
729 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
730 continue;
731 }
732 return 0;
733 }
734 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000735 }
736}
737
Anna Zaks50bbc162011-08-19 22:33:38 +0000738bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000739 BugReporterContext &BRC,
740 BugReport &report,
741 const ExplodedNode *N,
742 llvm::Optional<bool> &prunable) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000743 const Expr *OriginalExpr = Ex;
744 Ex = Ex->IgnoreParenCasts();
745
746 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000747 const bool quotes = isa<VarDecl>(DR->getDecl());
Ted Kremenek76aadc32012-03-09 01:13:14 +0000748 if (quotes) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000749 Out << '\'';
Ted Kremenek76aadc32012-03-09 01:13:14 +0000750 const LocationContext *LCtx = N->getLocationContext();
751 const ProgramState *state = N->getState().getPtr();
752 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
753 LCtx).getAsRegion()) {
754 if (report.isInteresting(R))
755 prunable = false;
756 else {
757 const ProgramState *state = N->getState().getPtr();
758 SVal V = state->getSVal(R);
759 if (report.isInteresting(V))
760 prunable = false;
761 }
762 }
763 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000764 Out << DR->getDecl()->getDeclName().getAsString();
765 if (quotes)
766 Out << '\'';
767 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000768 }
769
770 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
771 QualType OriginalTy = OriginalExpr->getType();
772 if (OriginalTy->isPointerType()) {
773 if (IL->getValue() == 0) {
774 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000775 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000776 }
777 }
778 else if (OriginalTy->isObjCObjectPointerType()) {
779 if (IL->getValue() == 0) {
780 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000781 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000782 }
783 }
784
785 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000786 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000787 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000788
789 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000790}
791
792PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000793ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
794 const BinaryOperator *BExpr,
795 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000796 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000797 BugReport &R,
798 const ExplodedNode *N) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000799
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000800 bool shouldInvert = false;
Ted Kremenek76aadc32012-03-09 01:13:14 +0000801 llvm::Optional<bool> shouldPrune;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000802
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000803 SmallString<128> LhsString, RhsString;
Ted Kremenek681bc112011-08-16 01:53:41 +0000804 {
Ted Kremenek76aadc32012-03-09 01:13:14 +0000805 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
806 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
807 shouldPrune);
808 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
809 shouldPrune);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000810
811 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000812 }
813
Ted Kremenekd1247c52012-01-04 08:18:09 +0000814 BinaryOperator::Opcode Op = BExpr->getOpcode();
815
816 if (BinaryOperator::isAssignmentOp(Op)) {
817 // For assignment operators, all that we care about is that the LHS
818 // evaluates to "true" or "false".
819 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000820 BRC, R, N);
Ted Kremenekd1247c52012-01-04 08:18:09 +0000821 }
822
823 // For non-assignment operations, we require that we can understand
824 // both the LHS and RHS.
Ted Kremenek681bc112011-08-16 01:53:41 +0000825 if (LhsString.empty() || RhsString.empty())
826 return 0;
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000827
Ted Kremenekd1247c52012-01-04 08:18:09 +0000828 // Should we invert the strings if the LHS is not a variable name?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000829 SmallString<256> buf;
Ted Kremenek681bc112011-08-16 01:53:41 +0000830 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000831 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000832
833 // Do we need to invert the opcode?
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000834 if (shouldInvert)
835 switch (Op) {
836 default: break;
837 case BO_LT: Op = BO_GT; break;
838 case BO_GT: Op = BO_LT; break;
839 case BO_LE: Op = BO_GE; break;
840 case BO_GE: Op = BO_LE; break;
841 }
842
Ted Kremenek681bc112011-08-16 01:53:41 +0000843 if (!tookTrue)
844 switch (Op) {
845 case BO_EQ: Op = BO_NE; break;
846 case BO_NE: Op = BO_EQ; break;
847 case BO_LT: Op = BO_GE; break;
848 case BO_GT: Op = BO_LE; break;
849 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000850 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000851 default:
852 return 0;
853 }
854
Ted Kremenek6ae32572011-12-20 22:00:25 +0000855 switch (Op) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000856 case BO_EQ:
857 Out << "equal to ";
858 break;
859 case BO_NE:
860 Out << "not equal to ";
861 break;
862 default:
863 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
864 break;
865 }
866
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000867 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek76aadc32012-03-09 01:13:14 +0000868 const LocationContext *LCtx = N->getLocationContext();
869 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
870 PathDiagnosticEventPiece *event =
871 new PathDiagnosticEventPiece(Loc, Out.str());
872 if (shouldPrune.hasValue())
873 event->setPrunable(shouldPrune.getValue());
874 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000875}
Ted Kremenekd1247c52012-01-04 08:18:09 +0000876
877PathDiagnosticPiece *
878ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
879 const Expr *CondVarExpr,
880 const bool tookTrue,
881 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000882 BugReport &report,
883 const ExplodedNode *N) {
Jordan Roseb0e1bad2012-08-03 23:08:54 +0000884 // FIXME: If there's already a constraint tracker for this variable,
885 // we shouldn't emit anything here (c.f. the double note in
886 // test/Analysis/inlining/path-notes.c)
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000887 SmallString<256> buf;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000888 llvm::raw_svector_ostream Out(buf);
889 Out << "Assuming " << LhsString << " is ";
890
891 QualType Ty = CondVarExpr->getType();
892
893 if (Ty->isPointerType())
894 Out << (tookTrue ? "not null" : "null");
895 else if (Ty->isObjCObjectPointerType())
896 Out << (tookTrue ? "not nil" : "nil");
897 else if (Ty->isBooleanType())
898 Out << (tookTrue ? "true" : "false");
899 else if (Ty->isIntegerType())
900 Out << (tookTrue ? "non-zero" : "zero");
901 else
902 return 0;
903
Ted Kremenek76aadc32012-03-09 01:13:14 +0000904 const LocationContext *LCtx = N->getLocationContext();
905 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
906 PathDiagnosticEventPiece *event =
907 new PathDiagnosticEventPiece(Loc, Out.str());
908
909 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
910 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
911 const ProgramState *state = N->getState().getPtr();
912 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
913 if (report.isInteresting(R))
914 event->setPrunable(false);
915 }
916 }
917 }
918
919 return event;
Ted Kremenekd1247c52012-01-04 08:18:09 +0000920}
Ted Kremenek993124e2011-08-06 06:54:45 +0000921
922PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000923ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
924 const DeclRefExpr *DR,
925 const bool tookTrue,
Anna Zaks220ac8c2011-09-15 01:08:34 +0000926 BugReporterContext &BRC,
Ted Kremenek76aadc32012-03-09 01:13:14 +0000927 BugReport &report,
928 const ExplodedNode *N) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000929
930 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
931 if (!VD)
932 return 0;
933
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000934 SmallString<256> Buf;
Ted Kremenek993124e2011-08-06 06:54:45 +0000935 llvm::raw_svector_ostream Out(Buf);
936
937 Out << "Assuming '";
938 VD->getDeclName().printName(Out);
939 Out << "' is ";
940
941 QualType VDTy = VD->getType();
942
943 if (VDTy->isPointerType())
944 Out << (tookTrue ? "non-null" : "null");
945 else if (VDTy->isObjCObjectPointerType())
946 Out << (tookTrue ? "non-nil" : "nil");
947 else if (VDTy->isScalarType())
948 Out << (tookTrue ? "not equal to 0" : "0");
949 else
950 return 0;
951
Ted Kremenek76aadc32012-03-09 01:13:14 +0000952 const LocationContext *LCtx = N->getLocationContext();
953 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
954 PathDiagnosticEventPiece *event =
955 new PathDiagnosticEventPiece(Loc, Out.str());
956
957 const ProgramState *state = N->getState().getPtr();
958 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
959 if (report.isInteresting(R))
960 event->setPrunable(false);
961 else {
962 SVal V = state->getSVal(R);
963 if (report.isInteresting(V))
964 event->setPrunable(false);
965 }
966 }
967 return event;
Ted Kremenek993124e2011-08-06 06:54:45 +0000968}
Ted Kremenek0cf3d472012-02-07 00:24:33 +0000969