blob: 9fbccf8f8136cd88bcef8cdd116e872f56227576 [file] [log] [blame]
Ted Kremenek53500662009-07-22 17:55:28 +00001// BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a set of BugReporter "visitors" which can be used to
11// enhance the diagnostics reported for a bug.
12//
13//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +000014#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
Ted Kremenek53500662009-07-22 17:55:28 +000015
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprObjC.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenek681bc112011-08-16 01:53:41 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek53500662009-07-22 17:55:28 +000023
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek53500662009-07-22 17:55:28 +000026
27//===----------------------------------------------------------------------===//
28// Utility functions.
29//===----------------------------------------------------------------------===//
30
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000031const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000032 // Pattern match for a few useful cases (do something smarter later):
33 // a[0], p->f, *p
34 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000035
Ted Kremenek53500662009-07-22 17:55:28 +000036 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +000037 if (U->getOpcode() == UO_Deref)
Ted Kremenek53500662009-07-22 17:55:28 +000038 return U->getSubExpr()->IgnoreParenCasts();
39 }
40 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
41 return ME->getBase()->IgnoreParenCasts();
42 }
43 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
Ted Kremenek53500662009-07-22 17:55:28 +000044 return AE->getBase();
45 }
Mike Stump1eb44332009-09-09 15:08:12 +000046
47 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000048}
49
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000050const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000051 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000052 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
53 return BE->getRHS();
54 return NULL;
55}
56
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000057const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000058 // Callee is checked as a PreVisit to the CallExpr.
59 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000060 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
61 return CE->getCallee();
62 return NULL;
63}
64
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000065const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000066 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
67 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
68 return RS->getRetValue();
69 return NULL;
70}
71
72//===----------------------------------------------------------------------===//
73// Definitions for bug reporter visitors.
74//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +000075void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
76 static int tag = 0;
77 ID.AddPointer(&tag);
78 ID.AddPointer(R);
79 ID.Add(V);
80}
Ted Kremenek53500662009-07-22 17:55:28 +000081
Anna Zaks50bbc162011-08-19 22:33:38 +000082PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
83 const ExplodedNode *PrevN,
84 BugReporterContext &BRC,
85 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +000086
Anna Zaks50bbc162011-08-19 22:33:38 +000087 if (satisfied)
88 return NULL;
89
90 if (!StoreSite) {
91 const ExplodedNode *Node = N, *Last = NULL;
92
93 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
94
95 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
96 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
97 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
98 if (DS->getSingleDecl() == VR->getDecl()) {
99 Last = Node;
100 break;
101 }
102 }
103
104 if (Node->getState()->getSVal(R) != V)
105 break;
106 }
107
108 if (!Node || !Last) {
109 satisfied = true;
110 return NULL;
111 }
112
113 StoreSite = Last;
Ted Kremenek1b431022010-03-20 18:01:57 +0000114 }
115
Anna Zaks50bbc162011-08-19 22:33:38 +0000116 if (StoreSite != N)
117 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Anna Zaks50bbc162011-08-19 22:33:38 +0000119 satisfied = true;
120 llvm::SmallString<256> sbuf;
121 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Anna Zaks50bbc162011-08-19 22:33:38 +0000123 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
124 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Anna Zaks50bbc162011-08-19 22:33:38 +0000126 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
127 os << "Variable '" << VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000128 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000129 else
Ted Kremenek53500662009-07-22 17:55:28 +0000130 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek53500662009-07-22 17:55:28 +0000132 if (isa<loc::ConcreteInt>(V)) {
133 bool b = false;
Ted Kremenek53500662009-07-22 17:55:28 +0000134 if (R->isBoundable()) {
Ted Kremenek96979342011-08-12 20:02:48 +0000135 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000136 if (TR->getValueType()->isObjCObjectPointerType()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000137 os << "initialized to nil";
Ted Kremenek53500662009-07-22 17:55:28 +0000138 b = true;
139 }
140 }
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek53500662009-07-22 17:55:28 +0000143 if (!b)
Anna Zaks50bbc162011-08-19 22:33:38 +0000144 os << "initialized to a null pointer value";
Ted Kremenek53500662009-07-22 17:55:28 +0000145 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000146 else if (isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000147 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
Ted Kremenek592362b2009-08-18 01:05:30 +0000148 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000149 else if (V.isUndef()) {
150 if (isa<VarRegion>(R)) {
151 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
152 if (VD->getInit())
153 os << "initialized to a garbage value";
154 else
155 os << "declared without an initial value";
156 }
Ted Kremenek53500662009-07-22 17:55:28 +0000157 }
Ted Kremenek53500662009-07-22 17:55:28 +0000158 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000159 }
160
161 if (os.str().empty()) {
162 if (isa<loc::ConcreteInt>(V)) {
163 bool b = false;
164 if (R->isBoundable()) {
165 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
166 if (TR->getValueType()->isObjCObjectPointerType()) {
167 os << "nil object reference stored to ";
168 b = true;
169 }
170 }
171 }
172
173 if (!b)
174 os << "Null pointer value stored to ";
175 }
176 else if (V.isUndef()) {
177 os << "Uninitialized value stored to ";
178 }
179 else if (isa<nonloc::ConcreteInt>(V)) {
180 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
181 << " is assigned to ";
182 }
183 else
184 return NULL;
185
186 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
187 os << '\'' << VR->getDecl() << '\'';
188 }
189 else
190 return NULL;
191 }
192
193 // FIXME: Refactor this into BugReporterContext.
194 const Stmt *S = 0;
195 ProgramPoint P = N->getLocation();
196
197 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
198 const CFGBlock *BSrc = BE->getSrc();
199 S = BSrc->getTerminatorCondition();
200 }
201 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
202 S = PS->getStmt();
203 }
204
205 if (!S)
206 return NULL;
207
208 // Construct a new PathDiagnosticPiece.
209 PathDiagnosticLocation L(S, BRC.getSourceManager());
210 return new PathDiagnosticEventPiece(L, os.str());
211}
212
213void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
214 static int tag = 0;
215 ID.AddPointer(&tag);
216 ID.AddBoolean(Assumption);
217 ID.Add(Constraint);
218}
219
220PathDiagnosticPiece *
221TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
222 const ExplodedNode *PrevN,
223 BugReporterContext &BRC,
224 BugReport &BR) {
225 if (isSatisfied)
226 return NULL;
227
228 // Check if in the previous state it was feasible for this constraint
229 // to *not* be true.
230 if (PrevN->getState()->assume(Constraint, !Assumption)) {
231
232 isSatisfied = true;
233
234 // As a sanity check, make sure that the negation of the constraint
235 // was infeasible in the current state. If it is feasible, we somehow
236 // missed the transition point.
237 if (N->getState()->assume(Constraint, !Assumption))
238 return NULL;
239
240 // We found the transition point for the constraint. We now need to
241 // pretty-print the constraint. (work-in-progress)
242 std::string sbuf;
243 llvm::raw_string_ostream os(sbuf);
244
245 if (isa<Loc>(Constraint)) {
246 os << "Assuming pointer value is ";
247 os << (Assumption ? "non-null" : "null");
248 }
249
250 if (os.str().empty())
251 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek53500662009-07-22 17:55:28 +0000253 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000254 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000255 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek53500662009-07-22 17:55:28 +0000257 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000258 const CFGBlock *BSrc = BE->getSrc();
Ted Kremenek53500662009-07-22 17:55:28 +0000259 S = BSrc->getTerminatorCondition();
260 }
261 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
262 S = PS->getStmt();
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek53500662009-07-22 17:55:28 +0000265 if (!S)
266 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek53500662009-07-22 17:55:28 +0000268 // Construct a new PathDiagnosticPiece.
269 PathDiagnosticLocation L(S, BRC.getSourceManager());
270 return new PathDiagnosticEventPiece(L, os.str());
271 }
Ted Kremenek53500662009-07-22 17:55:28 +0000272
Anna Zaks50bbc162011-08-19 22:33:38 +0000273 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000274}
275
Anna Zaks50bbc162011-08-19 22:33:38 +0000276BugReporterVisitor *
277bugreporter::getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
278 const Stmt *S) {
279 if (!S || !N)
280 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Anna Zaks8e6431a2011-08-18 22:37:56 +0000282 ProgramStateManager &StateMgr = N->getState()->getStateManager();
283
Ted Kremenek88299892011-07-28 23:07:59 +0000284 // Walk through nodes until we get one that matches the statement
285 // exactly.
286 while (N) {
287 const ProgramPoint &pp = N->getLocation();
288 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
289 if (ps->getStmt() == S)
290 break;
291 }
292 N = *N->pred_begin();
293 }
294
295 if (!N)
Anna Zaks50bbc162011-08-19 22:33:38 +0000296 return 0;
Ted Kremenek88299892011-07-28 23:07:59 +0000297
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000298 const ProgramState *state = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek892697d2010-12-16 07:46:53 +0000300 // Walk through lvalue-to-rvalue conversions.
Mike Stump1eb44332009-09-09 15:08:12 +0000301 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
302 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000303 const VarRegion *R =
Ted Kremenek892697d2010-12-16 07:46:53 +0000304 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek53500662009-07-22 17:55:28 +0000306 // What did we load?
Ted Kremenek892697d2010-12-16 07:46:53 +0000307 SVal V = state->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000308
309 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000310 || V.isUndef()) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000311 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek53500662009-07-22 17:55:28 +0000312 }
313 }
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek13976632010-02-08 16:18:51 +0000316 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek53500662009-07-22 17:55:28 +0000318 // Uncomment this to find cases where we aren't properly getting the
319 // base value that was dereferenced.
320 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenek53500662009-07-22 17:55:28 +0000322 // Is it a symbolic value?
323 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
324 const SubRegion *R = cast<SubRegion>(L->getRegion());
325 while (R && !isa<SymbolicRegion>(R)) {
326 R = dyn_cast<SubRegion>(R->getSuperRegion());
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek53500662009-07-22 17:55:28 +0000329 if (R) {
330 assert(isa<SymbolicRegion>(R));
Anna Zaks50bbc162011-08-19 22:33:38 +0000331 return new TrackConstraintBRVisitor(loc::MemRegionVal(R), false);
Ted Kremenek53500662009-07-22 17:55:28 +0000332 }
333 }
Anna Zaks50bbc162011-08-19 22:33:38 +0000334
335 return 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000336}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000337
Anna Zaks50bbc162011-08-19 22:33:38 +0000338BugReporterVisitor *
339FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
340 const MemRegion *R) {
341 assert(R && "The memory region is null.");
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000342
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000343 const ProgramState *state = N->getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000344 SVal V = state->getSVal(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000345 if (V.isUnknown())
Anna Zaks50bbc162011-08-19 22:33:38 +0000346 return 0;
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000347
Anna Zaks50bbc162011-08-19 22:33:38 +0000348 return new FindLastStoreBRVisitor(V, R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000349}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000350
351
Anna Zaks50bbc162011-08-19 22:33:38 +0000352PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
353 const ExplodedNode *PrevN,
354 BugReporterContext &BRC,
355 BugReport &BR) {
356 const PostStmt *P = N->getLocationAs<PostStmt>();
357 if (!P)
358 return 0;
359 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
360 if (!ME)
361 return 0;
362 const Expr *Receiver = ME->getInstanceReceiver();
363 if (!Receiver)
364 return 0;
365 const ProgramState *state = N->getState();
366 const SVal &V = state->getSVal(Receiver);
367 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
368 if (!DV)
369 return 0;
370 state = state->assume(*DV, true);
371 if (state)
372 return 0;
Ted Kremenekff7f7362010-03-20 18:02:01 +0000373
Anna Zaks50bbc162011-08-19 22:33:38 +0000374 // The receiver was nil, and hence the method was skipped.
375 // Register a BugReporterVisitor to issue a message telling us how
376 // the receiver was null.
377 BR.addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Receiver));
378 // Issue a message saying that the method was skipped.
379 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
380 return new PathDiagnosticEventPiece(L, "No method actually called "
381 "because the receiver is nil");
Ted Kremenekff7f7362010-03-20 18:02:01 +0000382}
Tom Care2bbbe502010-09-02 23:30:22 +0000383
Anna Zaks8e6431a2011-08-18 22:37:56 +0000384// Registers every VarDecl inside a Stmt with a last store visitor.
Anna Zaks50bbc162011-08-19 22:33:38 +0000385void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
386 const Stmt *S) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000387 const ExplodedNode *N = BR.getErrorNode();
Tom Care2bbbe502010-09-02 23:30:22 +0000388 std::deque<const Stmt *> WorkList;
Tom Care2bbbe502010-09-02 23:30:22 +0000389 WorkList.push_back(S);
390
391 while (!WorkList.empty()) {
392 const Stmt *Head = WorkList.front();
393 WorkList.pop_front();
394
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000395 const ProgramState *state = N->getState();
Anna Zaks8e6431a2011-08-18 22:37:56 +0000396 ProgramStateManager &StateMgr = state->getStateManager();
Tom Care2bbbe502010-09-02 23:30:22 +0000397
398 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
399 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
400 const VarRegion *R =
401 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
402
403 // What did we load?
404 SVal V = state->getSVal(S);
405
406 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
Anna Zaks50bbc162011-08-19 22:33:38 +0000407 // Register a new visitor with the BugReport.
408 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
Tom Care2bbbe502010-09-02 23:30:22 +0000409 }
410 }
411 }
412
413 for (Stmt::const_child_iterator I = Head->child_begin();
414 I != Head->child_end(); ++I)
415 WorkList.push_back(*I);
416 }
417}
Ted Kremenek993124e2011-08-06 06:54:45 +0000418
419//===----------------------------------------------------------------------===//
420// Visitor that tries to report interesting diagnostics from conditions.
421//===----------------------------------------------------------------------===//
Anna Zaks50bbc162011-08-19 22:33:38 +0000422PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
423 const ExplodedNode *Prev,
424 BugReporterContext &BRC,
425 BugReport &BR) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000426
427 const ProgramPoint &progPoint = N->getLocation();
Ted Kremenek681bc112011-08-16 01:53:41 +0000428
429 const ProgramState *CurrentState = N->getState();
430 const ProgramState *PrevState = Prev->getState();
431
432 // Compare the GDMs of the state, because that is where constraints
433 // are managed. Note that ensure that we only look at nodes that
434 // were generated by the analyzer engine proper, not checkers.
435 if (CurrentState->getGDM().getRoot() ==
436 PrevState->getGDM().getRoot())
437 return 0;
Ted Kremenek993124e2011-08-06 06:54:45 +0000438
439 // If an assumption was made on a branch, it should be caught
440 // here by looking at the state transition.
441 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000442 const CFGBlock *srcBlk = BE->getSrc();
443 if (const Stmt *term = srcBlk->getTerminator())
444 return VisitTerminator(term, CurrentState, PrevState,
445 srcBlk, BE->getDst(), BRC);
446 return 0;
447 }
448
449 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
450 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
451 // violation.
452 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
453 cast<GRBugReporter>(BRC.getBugReporter()).
454 getEngine().getEagerlyAssumeTags();
455
456 const ProgramPointTag *tag = PS->getTag();
457 if (tag == tags.first)
458 return VisitTrueTest(cast<Expr>(PS->getStmt()), true, BRC);
459 if (tag == tags.second)
460 return VisitTrueTest(cast<Expr>(PS->getStmt()), false, BRC);
461
Ted Kremenek993124e2011-08-06 06:54:45 +0000462 return 0;
463 }
464
465 return 0;
466}
467
468PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000469ConditionBRVisitor::VisitTerminator(const Stmt *Term,
470 const ProgramState *CurrentState,
471 const ProgramState *PrevState,
472 const CFGBlock *srcBlk,
473 const CFGBlock *dstBlk,
474 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000475 const Expr *Cond = 0;
476
477 switch (Term->getStmtClass()) {
478 default:
479 return 0;
480 case Stmt::IfStmtClass:
481 Cond = cast<IfStmt>(Term)->getCond();
482 break;
483 case Stmt::ConditionalOperatorClass:
484 Cond = cast<ConditionalOperator>(Term)->getCond();
485 break;
486 }
487
488 assert(Cond);
489 assert(srcBlk->succ_size() == 2);
490 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
491 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
492 tookTrue, BRC);
493}
494
495PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000496ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
497 bool tookTrue,
498 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000499
500 const Expr *Ex = Cond;
501
Ted Kremenek681bc112011-08-16 01:53:41 +0000502 while (true) {
503 Ex = Ex->IgnoreParens();
Ted Kremenek993124e2011-08-06 06:54:45 +0000504 switch (Ex->getStmtClass()) {
505 default:
506 return 0;
Ted Kremenek681bc112011-08-16 01:53:41 +0000507 case Stmt::BinaryOperatorClass:
508 return VisitTrueTest(Cond, cast<BinaryOperator>(Cond), tookTrue, BRC);
Ted Kremenek993124e2011-08-06 06:54:45 +0000509 case Stmt::DeclRefExprClass:
510 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC);
511 case Stmt::UnaryOperatorClass: {
512 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
513 if (UO->getOpcode() == UO_LNot) {
514 tookTrue = !tookTrue;
515 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
516 continue;
517 }
518 return 0;
519 }
520 }
Ted Kremenek681bc112011-08-16 01:53:41 +0000521 }
522}
523
Anna Zaks50bbc162011-08-19 22:33:38 +0000524bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
525 BugReporterContext &BRC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000526 const Expr *OriginalExpr = Ex;
527 Ex = Ex->IgnoreParenCasts();
528
529 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000530 const bool quotes = isa<VarDecl>(DR->getDecl());
531 if (quotes)
532 Out << '\'';
533 Out << DR->getDecl()->getDeclName().getAsString();
534 if (quotes)
535 Out << '\'';
536 return quotes;
Ted Kremenek681bc112011-08-16 01:53:41 +0000537 }
538
539 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
540 QualType OriginalTy = OriginalExpr->getType();
541 if (OriginalTy->isPointerType()) {
542 if (IL->getValue() == 0) {
543 Out << "null";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000544 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000545 }
546 }
547 else if (OriginalTy->isObjCObjectPointerType()) {
548 if (IL->getValue() == 0) {
549 Out << "nil";
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000550 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000551 }
552 }
553
554 Out << IL->getValue();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000555 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000556 }
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000557
558 return false;
Ted Kremenek681bc112011-08-16 01:53:41 +0000559}
560
561PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000562ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
563 const BinaryOperator *BExpr,
564 const bool tookTrue,
565 BugReporterContext &BRC) {
Ted Kremenek681bc112011-08-16 01:53:41 +0000566
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000567 bool shouldInvert = false;
568
Ted Kremenek681bc112011-08-16 01:53:41 +0000569 llvm::SmallString<128> LhsString, RhsString;
570 {
571 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000572 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
573 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
574
575 shouldInvert = !isVarLHS && isVarRHS;
Ted Kremenek681bc112011-08-16 01:53:41 +0000576 }
577
578 if (LhsString.empty() || RhsString.empty())
579 return 0;
580
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000581 // Should we invert the strings if the LHS is not a variable name?
582
Ted Kremenek681bc112011-08-16 01:53:41 +0000583 llvm::SmallString<256> buf;
584 llvm::raw_svector_ostream Out(buf);
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000585 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
Ted Kremenek681bc112011-08-16 01:53:41 +0000586
587 // Do we need to invert the opcode?
588 BinaryOperator::Opcode Op = BExpr->getOpcode();
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000589
590 if (shouldInvert)
591 switch (Op) {
592 default: break;
593 case BO_LT: Op = BO_GT; break;
594 case BO_GT: Op = BO_LT; break;
595 case BO_LE: Op = BO_GE; break;
596 case BO_GE: Op = BO_LE; break;
597 }
598
Ted Kremenek681bc112011-08-16 01:53:41 +0000599 if (!tookTrue)
600 switch (Op) {
601 case BO_EQ: Op = BO_NE; break;
602 case BO_NE: Op = BO_EQ; break;
603 case BO_LT: Op = BO_GE; break;
604 case BO_GT: Op = BO_LE; break;
605 case BO_LE: Op = BO_GT; break;
Ted Kremenek4ee7c9c2011-08-16 03:44:38 +0000606 case BO_GE: Op = BO_LT; break;
Ted Kremenek681bc112011-08-16 01:53:41 +0000607 default:
608 return 0;
609 }
610
611 switch (BExpr->getOpcode()) {
612 case BO_EQ:
613 Out << "equal to ";
614 break;
615 case BO_NE:
616 Out << "not equal to ";
617 break;
618 default:
619 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
620 break;
621 }
622
Ted Kremenek3b9e8e42011-08-16 10:57:37 +0000623 Out << (shouldInvert ? LhsString : RhsString);
Ted Kremenek681bc112011-08-16 01:53:41 +0000624
625 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
626 return new PathDiagnosticEventPiece(Loc, Out.str());
Ted Kremenek993124e2011-08-06 06:54:45 +0000627}
628
629PathDiagnosticPiece *
Anna Zaks50bbc162011-08-19 22:33:38 +0000630ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
631 const DeclRefExpr *DR,
632 const bool tookTrue,
633 BugReporterContext &BRC) {
Ted Kremenek993124e2011-08-06 06:54:45 +0000634
635 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
636 if (!VD)
637 return 0;
638
639 llvm::SmallString<256> Buf;
640 llvm::raw_svector_ostream Out(Buf);
641
642 Out << "Assuming '";
643 VD->getDeclName().printName(Out);
644 Out << "' is ";
645
646 QualType VDTy = VD->getType();
647
648 if (VDTy->isPointerType())
649 Out << (tookTrue ? "non-null" : "null");
650 else if (VDTy->isObjCObjectPointerType())
651 Out << (tookTrue ? "non-nil" : "nil");
652 else if (VDTy->isScalarType())
653 Out << (tookTrue ? "not equal to 0" : "0");
654 else
655 return 0;
656
657 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
658 return new PathDiagnosticEventPiece(Loc, Out.str());
659}