blob: b6f164a57f1035bf38443f3857557314ffa37655 [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek50df4f42008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000018#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000019
Ted Kremenek9f6b1612008-02-27 06:07:00 +000020#ifndef NDEBUG
21#include "llvm/Support/GraphWriter.h"
22#include <sstream>
23#endif
24
Ted Kremenekb451dd32008-03-05 21:15:02 +000025// SaveAndRestore - A utility class that uses RIIA to save and restore
26// the value of a variable.
27template<typename T>
28struct VISIBILITY_HIDDEN SaveAndRestore {
29 SaveAndRestore(T& x) : X(x), old_value(x) {}
30 ~SaveAndRestore() { X = old_value; }
31 T get() { return old_value; }
32
33 T& X;
34 T old_value;
35};
36
Ted Kremenekd4467432008-02-14 22:16:04 +000037using namespace clang;
38using llvm::dyn_cast;
39using llvm::cast;
40using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000041
Ted Kremenekf973eb02008-03-09 18:05:48 +000042
Ted Kremenekf4b49df2008-02-28 10:21:43 +000043ValueState* GRExprEngine::getInitialState() {
Ted Kremenekb5175bf2008-02-27 06:47:26 +000044
45 // The LiveVariables information already has a compilation of all VarDecls
46 // used in the function. Iterate through this set, and "symbolicate"
47 // any VarDecl whose value originally comes from outside the function.
48
49 typedef LiveVariables::AnalysisDataTy LVDataTy;
50 LVDataTy& D = Liveness.getAnalysisData();
51
Ted Kremenekf4b49df2008-02-28 10:21:43 +000052 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenekb5175bf2008-02-27 06:47:26 +000053
54 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
55
56 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
57
58 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
59 RVal X = RVal::GetSymbolValue(SymMgr, VD);
60 StateMgr.BindVar(StateImpl, VD, X);
61 }
62 }
63
64 return StateMgr.getPersistentState(StateImpl);
65}
66
Ted Kremenekf4b49df2008-02-28 10:21:43 +000067ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000068
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000069 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000070
Ted Kremenek07baa252008-02-21 18:02:17 +000071 if (Ex == CurrentStmt) {
72 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000073
74 if (!isBlkExpr)
75 return St;
76 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000077
Ted Kremenekad5d5c52008-02-26 23:37:01 +000078 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000079}
80
Ted Kremenekf4b49df2008-02-28 10:21:43 +000081ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
82 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +000083
84 switch (Terminator->getStmtClass()) {
85 default:
86 return St;
87
88 case Stmt::BinaryOperatorClass: { // '&&' and '||'
89
90 BinaryOperator* B = cast<BinaryOperator>(Terminator);
91 BinaryOperator::Opcode Op = B->getOpcode();
92
93 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
94
95 // For &&, if we take the true branch, then the value of the whole
96 // expression is that of the RHS expression.
97 //
98 // For ||, if we take the false branch, then the value of the whole
99 // expression is that of the RHS expression.
100
101 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
102 (Op == BinaryOperator::LOr && !branchTaken)
103 ? B->getRHS() : B->getLHS();
104
Ted Kremenekb31af242008-02-28 09:25:22 +0000105 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000106 }
107
108 case Stmt::ConditionalOperatorClass: { // ?:
109
110 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
111
112 // For ?, if branchTaken == true then the value is either the LHS or
113 // the condition itself. (GNU extension).
114
115 Expr* Ex;
116
117 if (branchTaken)
118 Ex = C->getLHS() ? C->getLHS() : C->getCond();
119 else
120 Ex = C->getRHS();
121
Ted Kremenekb31af242008-02-28 09:25:22 +0000122 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000123 }
124
125 case Stmt::ChooseExprClass: { // ?:
126
127 ChooseExpr* C = cast<ChooseExpr>(Terminator);
128
129 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenekb31af242008-02-28 09:25:22 +0000130 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000131 }
132 }
133}
134
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000135bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
136 GRBlockCounter BC) {
137
138 return BC.getNumVisited(B->getBlockID()) < 3;
139}
140
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000141void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000142 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000143
Ted Kremenek17c5f112008-02-11 19:21:59 +0000144 // Remove old bindings for subexpressions.
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000145 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000146
Ted Kremenek022b6052008-02-15 22:29:00 +0000147 // Check for NULL conditions; e.g. "for(;;)"
148 if (!Condition) {
149 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000150 return;
151 }
152
Ted Kremenek07baa252008-02-21 18:02:17 +0000153 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000154
155 switch (V.getBaseKind()) {
156 default:
157 break;
158
Ted Kremenek07baa252008-02-21 18:02:17 +0000159 case RVal::UnknownKind:
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000160 builder.generateNode(MarkBranch(PrevState, Term, true), true);
161 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000162 return;
163
Ted Kremenekb31af242008-02-28 09:25:22 +0000164 case RVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000165 NodeTy* N = builder.generateNode(PrevState, true);
166
167 if (N) {
168 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000169 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000170 }
171
172 builder.markInfeasible(false);
173 return;
174 }
175 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000176
Ted Kremenek90960972008-01-30 23:03:39 +0000177
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000178 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000179
Ted Kremenekd4676512008-03-12 21:45:47 +0000180 bool isFeasible = false;
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000181 ValueState* St = Assume(PrevState, V, true, isFeasible);
182
183 if (isFeasible)
184 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000185 else
186 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000187
188 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000189
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000190 isFeasible = false;
191 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000192
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000193 if (isFeasible)
194 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000195 else
196 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000197}
198
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000199/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000200/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000201void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000202
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000203 ValueState* St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000204 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000205
206 // Three possibilities:
207 //
208 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000209 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000210 // (3) We have no clue about the label. Dispatch to all targets.
211 //
212
213 typedef IndirectGotoNodeBuilder::iterator iterator;
214
215 if (isa<lval::GotoLabel>(V)) {
216 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
217
218 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000219 if (I.getLabel() == L) {
220 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000221 return;
222 }
223 }
224
225 assert (false && "No block with label.");
226 return;
227 }
228
Ted Kremenekb31af242008-02-28 09:25:22 +0000229 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000230 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000231 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000232 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000233 return;
234 }
235
236 // This is really a catch-all. We don't support symbolics yet.
237
Ted Kremenek07baa252008-02-21 18:02:17 +0000238 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000239
240 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000241 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000242}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000243
Ted Kremenekaee121c2008-02-13 23:08:21 +0000244/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
245/// nodes by processing the 'effects' of a switch statement.
246void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
247
248 typedef SwitchNodeBuilder::iterator iterator;
249
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000250 ValueState* St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000251 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000252 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000253
Ted Kremenekb31af242008-02-28 09:25:22 +0000254 if (CondV.isUndef()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000255 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000256 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000257 return;
258 }
259
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000260 ValueState* DefaultSt = St;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000261
262 // While most of this can be assumed (such as the signedness), having it
263 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000264
Chris Lattner8cd0e932008-03-05 18:54:05 +0000265 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenekbc965a62008-02-18 22:57:02 +0000266
Ted Kremenekaee121c2008-02-13 23:08:21 +0000267 APSInt V1(bits, false);
268 APSInt V2 = V1;
269
Ted Kremenek07baa252008-02-21 18:02:17 +0000270 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000271
272 CaseStmt* Case = cast<CaseStmt>(I.getCase());
273
274 // Evaluate the case.
275 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
276 assert (false && "Case condition must evaluate to an integer constant.");
277 return;
278 }
279
280 // Get the RHS of the case, if it exists.
281
282 if (Expr* E = Case->getRHS()) {
283 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
284 assert (false &&
285 "Case condition (RHS) must evaluate to an integer constant.");
286 return ;
287 }
288
289 assert (V1 <= V2);
290 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000291 else
292 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000293
294 // FIXME: Eventually we should replace the logic below with a range
295 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000296 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000297
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000298 do {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000299 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000300
Ted Kremenek07baa252008-02-21 18:02:17 +0000301 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000302
303 // Now "assume" that the case matches.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000304
Ted Kremenekd4676512008-03-12 21:45:47 +0000305 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000306 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000307
308 if (isFeasible) {
309 builder.generateCaseStmtNode(I, StNew);
310
311 // If CondV evaluates to a constant, then we know that this
312 // is the *only* case that we can take, so stop evaluating the
313 // others.
314 if (isa<nonlval::ConcreteInt>(CondV))
315 return;
316 }
317
318 // Now "assume" that the case doesn't match. Add this state
319 // to the default state (if it is feasible).
320
Ted Kremenekd4676512008-03-12 21:45:47 +0000321 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000322 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000323
324 if (isFeasible)
325 DefaultSt = StNew;
326
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000327 // Concretize the next value in the range.
328 if (V1 == V2)
329 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000330
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000331 ++V1;
Ted Kremenek539269c2008-03-17 22:18:22 +0000332 assert (V1 <= V2);
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000333
334 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000335 }
336
337 // If we reach here, than we know that the default branch is
338 // possible.
339 builder.generateDefaultCaseNode(DefaultSt);
340}
341
342
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000343void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000344 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000345
Ted Kremenek99ecce72008-02-26 19:05:15 +0000346 assert (B->getOpcode() == BinaryOperator::LAnd ||
347 B->getOpcode() == BinaryOperator::LOr);
348
349 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
350
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000351 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000352 RVal X = GetBlkExprRVal(St, B);
353
Ted Kremenekb31af242008-02-28 09:25:22 +0000354 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000355
Ted Kremenekb31af242008-02-28 09:25:22 +0000356 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000357
358 assert (Ex);
359
360 if (Ex == B->getRHS()) {
361
362 X = GetBlkExprRVal(St, Ex);
363
Ted Kremenekb31af242008-02-28 09:25:22 +0000364 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000365
Ted Kremenekb31af242008-02-28 09:25:22 +0000366 if (X.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000367 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000368 return;
369 }
370
Ted Kremenek99ecce72008-02-26 19:05:15 +0000371 // We took the RHS. Because the value of the '&&' or '||' expression must
372 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
373 // or 1. Alternatively, we could take a lazy approach, and calculate this
374 // value later when necessary. We don't have the machinery in place for
375 // this right now, and since most logical expressions are used for branches,
376 // the payoff is not likely to be large. Instead, we do eager evaluation.
377
378 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000379 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000380
381 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000382 MakeNode(Dst, B, Pred,
383 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000384
385 isFeasible = false;
386 NewState = Assume(St, X, false, isFeasible);
387
388 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000389 MakeNode(Dst, B, Pred,
390 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000391 }
392 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000393 // We took the LHS expression. Depending on whether we are '&&' or
394 // '||' we know what the value of the expression is via properties of
395 // the short-circuiting.
396
397 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenekf10f2882008-03-21 21:30:14 +0000398 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000399 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000400}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000401
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000402
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000403void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000404
Ted Kremenek07baa252008-02-21 18:02:17 +0000405 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000406 StmtEntryNode = builder.getLastNode();
407 CurrentStmt = S;
408 NodeSet Dst;
Ted Kremenek02331462008-03-09 18:28:41 +0000409
Ted Kremenekb0a2e472008-03-27 07:25:52 +0000410 // Set up our simple checks.
411
412 if (!MsgExprChecks.empty())
413 Builder->setObjCMsgExprAuditors(
414 (GRNodeAuditor<ValueState>**) &MsgExprChecks[0],
415 (GRNodeAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size()));
416
417
418 if (!CallChecks.empty())
419 Builder->setCallExprAuditors(
420 (GRNodeAuditor<ValueState>**) &CallChecks[0],
421 (GRNodeAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size()));
422
Ted Kremenek02331462008-03-09 18:28:41 +0000423 // Create the cleaned state.
424
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000425 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
426 CurrentStmt, Liveness);
Ted Kremeneka1d070b2008-03-10 04:45:00 +0000427
428 Builder->SetCleanedState(CleanedState);
Ted Kremenek02331462008-03-09 18:28:41 +0000429
430 // Visit the statement.
Ted Kremenekf031b872008-01-23 19:59:44 +0000431
432 Visit(S, StmtEntryNode, Dst);
433
434 // If no nodes were generated, generate a new node that has all the
435 // dead mappings removed.
Ted Kremenek07baa252008-02-21 18:02:17 +0000436
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000437 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode)
438 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
Ted Kremeneka57214f2008-01-18 00:41:32 +0000439
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000440 // NULL out these variables to cleanup.
Ted Kremenek07baa252008-02-21 18:02:17 +0000441
Ted Kremenekf031b872008-01-23 19:59:44 +0000442 CurrentStmt = NULL;
443 StmtEntryNode = NULL;
444 Builder = NULL;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000445 CleanedState = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000446}
447
Ted Kremenek6d409922008-02-13 18:06:44 +0000448void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000449
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000450 if (D != CurrentStmt) {
451 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
452 return;
453 }
454
455 // If we are here, we are loading the value of the decl and binding
456 // it to the block-level expression.
457
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000458 ValueState* St = GetState(Pred);
Ted Kremenekf97c6682008-03-09 03:30:59 +0000459 RVal X = RVal::MakeVal(BasicVals, D);
460 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenekf10f2882008-03-21 21:30:14 +0000461 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000462}
463
Ted Kremenekd9268e32008-02-19 01:44:53 +0000464void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000465 CallExpr::arg_iterator AI,
466 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000467 NodeSet& Dst) {
468
Ted Kremenek07baa252008-02-21 18:02:17 +0000469 // Process the arguments.
470
471 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000472
Ted Kremenekef7ea072008-03-04 00:56:45 +0000473 NodeSet DstTmp;
Ted Kremenek769f3482008-03-04 22:01:56 +0000474 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +0000475 ++AI;
476
Ted Kremenek769f3482008-03-04 22:01:56 +0000477 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenek07baa252008-02-21 18:02:17 +0000478 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000479
480 return;
481 }
482
483 // If we reach here we have processed all of the arguments. Evaluate
484 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000485
Ted Kremenekc71901d2008-02-25 21:16:03 +0000486 NodeSet DstTmp;
487 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000488
Ted Kremenekc71901d2008-02-25 21:16:03 +0000489 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000490
491 if (DstTmp.empty())
492 DstTmp.Add(Pred);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000493
494 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000495 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
496
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000497 ValueState* St = GetState(*DI);
Ted Kremenekc71901d2008-02-25 21:16:03 +0000498 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000499
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000500 // FIXME: Add support for symbolic function calls (calls involving
501 // function pointer values that are symbolic).
502
503 // Check for undefined control-flow or calls to NULL.
504
Ted Kremenek43863eb2008-02-29 23:14:48 +0000505 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000506 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000507
Ted Kremenek9b31f5b2008-02-29 23:53:11 +0000508 if (N) {
509 N->markAsSink();
510 BadCalls.insert(N);
511 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000512
Ted Kremenekd9268e32008-02-19 01:44:53 +0000513 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +0000514 }
515
516 // Check for the "noreturn" attribute.
517
518 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
519
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000520 if (isa<lval::FuncVal>(L)) {
521
522 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
523
524 if (FD->getAttr<NoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +0000525 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000526 else {
527 // HACK: Some functions are not marked noreturn, and don't return.
528 // Here are a few hardwired ones. If this takes too long, we can
529 // potentially cache these results.
530 const char* s = FD->getIdentifier()->getName();
531 unsigned n = strlen(s);
532
533 switch (n) {
534 default:
535 break;
Ted Kremenek550025b2008-03-14 23:25:49 +0000536
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000537 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +0000538 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
539 break;
540
541 case 5:
542 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
543 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000544 }
545 }
546 }
Ted Kremenekb451dd32008-03-05 21:15:02 +0000547
548 // Evaluate the call.
549
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000550
Ted Kremenek769f3482008-03-04 22:01:56 +0000551 bool invalidateArgs = false;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000552
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000553 if (L.isUnknown()) {
Ted Kremenek769f3482008-03-04 22:01:56 +0000554 // Check for an "unknown" callee.
555 invalidateArgs = true;
556 }
557 else if (isa<lval::FuncVal>(L)) {
558
559 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
560
Ted Kremenek21581c62008-03-05 22:59:42 +0000561 if (unsigned id = Info->getBuiltinID()) {
562 switch (id) {
563 case Builtin::BI__builtin_expect: {
564 // For __builtin_expect, just return the value of the subexpression.
565 assert (CE->arg_begin() != CE->arg_end());
566 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenekf10f2882008-03-21 21:30:14 +0000567 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +0000568 continue;
569 }
570
571 default:
572 invalidateArgs = true;
573 break;
574 }
575 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000576 }
577
578 if (invalidateArgs) {
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000579 // Invalidate all arguments passed in by reference (LVals).
580 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
581 I != E; ++I) {
582 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000583
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000584 if (isa<LVal>(V))
585 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenekb451dd32008-03-05 21:15:02 +0000586 }
587
Ted Kremenekf10f2882008-03-21 21:30:14 +0000588 MakeNode(Dst, CE, *DI, St);
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000589 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000590 else {
591
592 // Check any arguments passed-by-value against being undefined.
593
594 bool badArg = false;
595
596 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
597 I != E; ++I) {
598
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000599 if (GetRVal(GetState(*DI), *I).isUndef()) {
600 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000601
602 if (N) {
603 N->markAsSink();
604 UndefArgs[N] = *I;
605 }
606
607 badArg = true;
608 break;
609 }
610 }
611
612 if (badArg)
613 continue;
614
615 // Dispatch to the plug-in transfer function.
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000616
617 unsigned size = Dst.size();
618
619 EvalCall(Dst, CE, cast<LVal>(L), *DI);
620
Ted Kremeneka2822eb2008-03-05 22:49:16 +0000621 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000622 MakeNode(Dst, CE, *DI, St);
Ted Kremenek769f3482008-03-04 22:01:56 +0000623 }
Ted Kremenekd9268e32008-02-19 01:44:53 +0000624 }
625}
626
Ted Kremenek07baa252008-02-21 18:02:17 +0000627void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000628
Ted Kremenek5f585b02008-02-19 18:52:54 +0000629 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +0000630 QualType T = CastE->getType();
631
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000632 if (T->isReferenceType())
633 VisitLVal(Ex, Pred, S1);
634 else
635 Visit(Ex, Pred, S1);
636
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000637 // Check for redundant casts or casting to "void"
638 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000639 Ex->getType() == T ||
640 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000641
Ted Kremenek07baa252008-02-21 18:02:17 +0000642 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000643 Dst.Add(*I1);
644
Ted Kremenek54eddae2008-01-24 02:02:54 +0000645 return;
646 }
647
Ted Kremenek07baa252008-02-21 18:02:17 +0000648 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000649 NodeTy* N = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000650 ValueState* St = GetState(N);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000651
652 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
653
Ted Kremenekf10f2882008-03-21 21:30:14 +0000654 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000655 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000656}
657
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000658void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000659 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000660
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000661 ValueState* St = GetState(Pred);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000662
663 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000664 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000665
666 // FIXME: Add support for local arrays.
667 if (VD->getType()->isArrayType())
668 continue;
669
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000670 const Expr* Ex = VD->getInit();
Ted Kremenek07baa252008-02-21 18:02:17 +0000671
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000672 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
673
674 // In this context, Static => Local variable.
675
676 assert (!VD->getStorageClass() == VarDecl::Static ||
677 !isa<FileVarDecl>(VD));
678
679 // If there is no initializer, set the value of the
Ted Kremenekb31af242008-02-28 09:25:22 +0000680 // variable to "Undefined".
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000681 //
682 // FIXME: static variables may have an initializer, but the second
683 // time a function is called those values may not be current.
Ted Kremenek98d093b2008-03-04 20:40:11 +0000684
685 QualType T = VD->getType();
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000686
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000687 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000688
689 // C99: 6.7.8 Initialization
690 // If an object that has static storage duration is not initialized
691 // explicitly, then:
692 // —if it has pointer type, it is initialized to a null pointer;
693 // —if it has arithmetic type, it is initialized to (positive or
694 // unsigned) zero;
695
Ted Kremenek98d093b2008-03-04 20:40:11 +0000696 // FIXME: Handle structs. Now we treat their values as unknown.
697
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000698 if (T->isPointerType()) {
699
700 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000701 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000702 }
703 else if (T->isIntegerType()) {
704
705 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000706 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000707 }
708
Ted Kremenek98d093b2008-03-04 20:40:11 +0000709
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000710 }
Ted Kremenek98d093b2008-03-04 20:40:11 +0000711 else {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000712
Ted Kremenek98d093b2008-03-04 20:40:11 +0000713 // FIXME: Handle structs. Now we treat them as unknown. What
714 // we need to do is treat their members as unknown.
715
716 if (T->isPointerType() || T->isIntegerType())
717 St = SetRVal(St, lval::DeclVal(VD),
718 Ex ? GetRVal(St, Ex) : UndefinedVal());
719 }
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000720 }
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000721 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000722
Ted Kremenekf10f2882008-03-21 21:30:14 +0000723 MakeNode(Dst, DS, Pred, St);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000724}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000725
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000726
Ted Kremenek07baa252008-02-21 18:02:17 +0000727void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000728 NodeTy* Pred, NodeSet& Dst) {
729
Ted Kremenek99ecce72008-02-26 19:05:15 +0000730 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
731
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000732 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000733 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000734
Ted Kremenekb31af242008-02-28 09:25:22 +0000735 assert (X.isUndef());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000736
Ted Kremenekb31af242008-02-28 09:25:22 +0000737 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000738
739 assert (SE);
740
741 X = GetBlkExprRVal(St, SE);
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000742
743 // Make sure that we invalidate the previous binding.
Ted Kremenekf10f2882008-03-21 21:30:14 +0000744 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000745}
746
Ted Kremenekfd85f292008-02-12 19:49:57 +0000747/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000748void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
749 NodeTy* Pred,
750 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000751
Ted Kremenek07baa252008-02-21 18:02:17 +0000752 QualType T = Ex->getArgumentType();
Ted Kremenekc3b12832008-03-15 03:13:20 +0000753 uint64_t amt;
754
755 if (Ex->isSizeOf()) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000756
Ted Kremenekc3b12832008-03-15 03:13:20 +0000757 // FIXME: Add support for VLAs.
758 if (!T.getTypePtr()->isConstantSizeType())
759 return;
760
761 amt = 1; // Handle sizeof(void)
762
763 if (T != getContext().VoidTy)
764 amt = getContext().getTypeSize(T) / 8;
765
766 }
767 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +0000768 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000769
Ted Kremenekf10f2882008-03-21 21:30:14 +0000770 MakeNode(Dst, Ex, Pred,
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000771 SetRVal(GetState(Pred), Ex,
Ted Kremenekc3b12832008-03-15 03:13:20 +0000772 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000773}
774
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000775void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
776 NodeSet& Dst, bool GetLVal) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000777
Ted Kremenek07baa252008-02-21 18:02:17 +0000778 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000779
780 NodeSet DstTmp;
781
Ted Kremenek56a89992008-02-26 03:44:25 +0000782 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000783 DstTmp.Add(Pred);
784 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000785 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000786
Ted Kremenek07baa252008-02-21 18:02:17 +0000787 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000788
789 NodeTy* N = *I;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000790 ValueState* St = GetState(N);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000791
792 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
793
Ted Kremenek07baa252008-02-21 18:02:17 +0000794 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000795
Ted Kremenekb31af242008-02-28 09:25:22 +0000796 // Check for dereferences of undefined values.
Ted Kremenek07baa252008-02-21 18:02:17 +0000797
Ted Kremenekb31af242008-02-28 09:25:22 +0000798 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000799
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000800 NodeTy* Succ = Builder->generateNode(U, St, N);
801
802 if (Succ) {
803 Succ->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000804 UndefDeref.insert(Succ);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000805 }
806
807 continue;
808 }
809
Ted Kremenek07baa252008-02-21 18:02:17 +0000810 // Check for dereferences of unknown values. Treat as No-Ops.
811
812 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000813 Dst.Add(N);
814 continue;
815 }
816
817 // After a dereference, one of two possible situations arise:
818 // (1) A crash, because the pointer was NULL.
819 // (2) The pointer is not NULL, and the dereference works.
820 //
821 // We add these assumptions.
822
Ted Kremenek07baa252008-02-21 18:02:17 +0000823 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000824 bool isFeasibleNotNull;
825
826 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000827
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000828 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000829
830 if (isFeasibleNotNull) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000831
Ted Kremenekf10f2882008-03-21 21:30:14 +0000832 if (GetLVal) MakeNode(Dst, U, N, SetRVal(StNotNull, U, LV));
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000833 else {
834
835 // FIXME: Currently symbolic analysis "generates" new symbols
836 // for the contents of values. We need a better approach.
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000837
Ted Kremenekf10f2882008-03-21 21:30:14 +0000838 MakeNode(Dst, U, N, SetRVal(StNotNull, U,
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000839 GetRVal(StNotNull, LV, U->getType())));
840 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000841 }
842
843 bool isFeasibleNull;
844
Ted Kremenek07baa252008-02-21 18:02:17 +0000845 // Now "assume" that the pointer is NULL.
846
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000847 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000848
849 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000850
Ted Kremenekf10f2882008-03-21 21:30:14 +0000851 // We don't use "MakeNode" here because the node will be a sink
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000852 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000853
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000854 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
855
856 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000857
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000858 NullNode->markAsSink();
859
Ted Kremenek07baa252008-02-21 18:02:17 +0000860 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
861 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000862 }
863 }
864 }
865}
866
Ted Kremenek07baa252008-02-21 18:02:17 +0000867void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
868 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000869
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000870 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000871
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000872 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000873 assert (U->getOpcode() != UnaryOperator::SizeOf);
874 assert (U->getOpcode() != UnaryOperator::AlignOf);
875
876 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000877
878 switch (U->getOpcode()) {
879 case UnaryOperator::PostInc:
880 case UnaryOperator::PostDec:
881 case UnaryOperator::PreInc:
882 case UnaryOperator::PreDec:
883 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000884 // Evalue subexpression as an LVal.
885 use_GetLVal = true;
886 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000887 break;
888
889 default:
890 Visit(U->getSubExpr(), Pred, S1);
891 break;
892 }
893
Ted Kremenek07baa252008-02-21 18:02:17 +0000894 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000895
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000896 NodeTy* N1 = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000897 ValueState* St = GetState(N1);
Ted Kremenek07baa252008-02-21 18:02:17 +0000898
899 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
900 GetRVal(St, U->getSubExpr());
901
902 if (SubV.isUnknown()) {
903 Dst.Add(N1);
904 continue;
905 }
906
Ted Kremenekb31af242008-02-28 09:25:22 +0000907 if (SubV.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000908 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenek07baa252008-02-21 18:02:17 +0000909 continue;
910 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000911
Ted Kremenek22640ce2008-02-15 22:09:30 +0000912 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000913
914 // Handle ++ and -- (both pre- and post-increment).
915
Ted Kremenek07baa252008-02-21 18:02:17 +0000916 LVal SubLV = cast<LVal>(SubV);
917 RVal V = GetRVal(St, SubLV, U->getType());
918
Ted Kremenekb8782e12008-02-21 19:15:37 +0000919 if (V.isUnknown()) {
920 Dst.Add(N1);
921 continue;
922 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000923
Ted Kremenekb31af242008-02-28 09:25:22 +0000924 // Propagate undefined values.
925 if (V.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000926 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenek07baa252008-02-21 18:02:17 +0000927 continue;
928 }
929
Ted Kremenekb1669d42008-02-21 19:29:23 +0000930 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000931
932 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
933 : BinaryOperator::Sub;
934
Ted Kremenekb1669d42008-02-21 19:29:23 +0000935 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000936
937 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000938 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000939 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000940 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000941
Ted Kremenekf10f2882008-03-21 21:30:14 +0000942 MakeNode(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000943 continue;
944 }
945
946 // Handle all other unary operators.
947
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000948 switch (U->getOpcode()) {
Ted Kremenek3c536072008-03-15 03:05:30 +0000949
950 case UnaryOperator::Extension:
951 St = SetRVal(St, U, SubV);
952 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000953
Ted Kremenek07baa252008-02-21 18:02:17 +0000954 case UnaryOperator::Minus:
955 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000956 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000957
Ted Kremenek07baa252008-02-21 18:02:17 +0000958 case UnaryOperator::Not:
959 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000960 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000961
Ted Kremenek07baa252008-02-21 18:02:17 +0000962 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000963
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000964 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
965 //
966 // Note: technically we do "E == 0", but this is the same in the
967 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000968
969 if (isa<LVal>(SubV)) {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000970 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenek07baa252008-02-21 18:02:17 +0000971 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
972 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000973 }
974 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000975 Expr* Ex = U->getSubExpr();
Ted Kremenek8ad19872008-03-07 20:13:31 +0000976 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000977 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
978 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000979 }
980
981 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000982
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000983 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000984 assert (isa<LVal>(SubV));
985 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000986 break;
987 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000988
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000989 default: ;
990 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000991 }
992
Ted Kremenekf10f2882008-03-21 21:30:14 +0000993 MakeNode(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000994 }
995}
996
Ted Kremenek07baa252008-02-21 18:02:17 +0000997void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
998 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000999
Ted Kremenek07baa252008-02-21 18:02:17 +00001000 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001001
Ted Kremenek07baa252008-02-21 18:02:17 +00001002 // FIXME: Add support for VLAs.
1003 if (!T.getTypePtr()->isConstantSizeType())
1004 return;
1005
Chris Lattner8cd0e932008-03-05 18:54:05 +00001006 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001007 ValueState* St = GetState(Pred);
Ted Kremenek8ad19872008-03-07 20:13:31 +00001008 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +00001009
Ted Kremenekf10f2882008-03-21 21:30:14 +00001010 MakeNode(Dst, U, Pred, St);
Ted Kremenek07baa252008-02-21 18:02:17 +00001011}
1012
1013void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneka129a2e2008-02-27 07:04:16 +00001014
1015 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1016 Dst.Add(Pred);
1017 return;
1018 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001019
1020 Ex = Ex->IgnoreParens();
1021
Ted Kremenekb5175bf2008-02-27 06:47:26 +00001022 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001023 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001024 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001025 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001026
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001027 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex))
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001028 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001029 VisitDeref(U, Pred, Dst, true);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001030 return;
1031 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001032
Ted Kremenek07baa252008-02-21 18:02:17 +00001033 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001034}
1035
Ted Kremenek31803c32008-03-17 21:11:24 +00001036void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1037 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1038}
1039
1040void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1041 AsmStmt::outputs_iterator I,
1042 AsmStmt::outputs_iterator E,
1043 NodeTy* Pred, NodeSet& Dst) {
1044 if (I == E) {
1045 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1046 return;
1047 }
1048
1049 NodeSet Tmp;
1050 VisitLVal(*I, Pred, Tmp);
1051
1052 ++I;
1053
1054 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1055 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1056}
1057
1058void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1059 AsmStmt::inputs_iterator I,
1060 AsmStmt::inputs_iterator E,
1061 NodeTy* Pred, NodeSet& Dst) {
1062 if (I == E) {
1063
1064 // We have processed both the inputs and the outputs. All of the outputs
1065 // should evaluate to LVals. Nuke all of their values.
1066
1067 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1068 // which interprets the inline asm and stores proper results in the
1069 // outputs.
1070
1071 ValueState* St = GetState(Pred);
1072
1073 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1074 OE = A->end_outputs(); OI != OE; ++OI) {
1075
1076 RVal X = GetLVal(St, *OI);
1077
1078 assert (!isa<NonLVal>(X));
1079
1080 if (isa<LVal>(X))
1081 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1082 }
1083
Ted Kremenekf10f2882008-03-21 21:30:14 +00001084 MakeNode(Dst, A, Pred, St);
Ted Kremenek31803c32008-03-17 21:11:24 +00001085 return;
1086 }
1087
1088 NodeSet Tmp;
1089 Visit(*I, Pred, Tmp);
1090
1091 ++I;
1092
1093 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1094 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1095}
1096
1097
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001098void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1099 NodeSet& Dst){
Ted Kremenek71507472008-03-25 16:07:41 +00001100 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), Pred, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001101}
1102
Ted Kremenek71507472008-03-25 16:07:41 +00001103void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001104 ObjCMessageExpr::arg_iterator AI,
1105 ObjCMessageExpr::arg_iterator AE,
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001106 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001107 if (AI == AE) {
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001108
1109 // Process the receiver.
1110
Ted Kremenek71507472008-03-25 16:07:41 +00001111 if (Expr* Receiver = ME->getReceiver()) {
1112 NodeSet Tmp;
Ted Kremenek2380f372008-03-26 21:36:08 +00001113 Visit(Receiver, Pred, Tmp);
Ted Kremenek71507472008-03-25 16:07:41 +00001114
1115 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1116 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001117
Ted Kremenek71507472008-03-25 16:07:41 +00001118 return;
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001119 }
Ted Kremenek71507472008-03-25 16:07:41 +00001120
1121 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001122 return;
1123 }
1124
1125 NodeSet Tmp;
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001126 Visit(*AI, Pred, Tmp);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001127
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001128 ++AI;
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001129
1130 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenek71507472008-03-25 16:07:41 +00001131 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001132}
1133
Ted Kremenek71507472008-03-25 16:07:41 +00001134void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1135 NodeTy* Pred, NodeSet& Dst) {
1136
1137
1138 // FIXME: More logic for the processing the method call.
1139
1140 ValueState* St = GetState(Pred);
1141
1142 if (Expr* Receiver = ME->getReceiver()) {
1143
Ted Kremenek9ba5a1f2008-03-26 22:21:58 +00001144 RVal L = GetRVal(St, Receiver);
Ted Kremenek71507472008-03-25 16:07:41 +00001145
1146 // Check for undefined control-flow or calls to NULL.
1147
1148 if (L.isUndef()) {
1149 NodeTy* N = Builder->generateNode(ME, St, Pred);
1150
1151 if (N) {
1152 N->markAsSink();
1153 UndefReceivers.insert(N);
1154 }
1155
1156 return;
1157 }
1158 }
1159
1160 // Check for any arguments that are uninitialized/undefined.
1161
1162 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1163 I != E; ++I) {
1164
1165 if (GetRVal(St, *I).isUndef()) {
1166
1167 NodeTy* N = Builder->generateNode(ME, St, Pred);
1168
1169 if (N) {
1170 N->markAsSink();
1171 MsgExprUndefArgs[N] = *I;
1172 }
1173
1174 return;
1175 }
1176 }
1177
1178 // FIXME: Eventually we will properly handle the effects of a message
1179 // expr. For now invalidate all arguments passed in by references.
1180
1181 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1182 I != E; ++I) {
1183
1184 RVal V = GetRVal(St, *I);
1185
1186 if (isa<LVal>(V))
1187 St = SetRVal(St, cast<LVal>(V), UnknownVal());
1188 }
1189
1190 MakeNode(Dst, ME, Pred, St);
1191}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001192
1193
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001194void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00001195 GRExprEngine::NodeTy* Pred,
1196 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001197 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001198
1199 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00001200 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001201 else
1202 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001203
Ted Kremenekf031b872008-01-23 19:59:44 +00001204 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001205
Ted Kremenekf031b872008-01-23 19:59:44 +00001206 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +00001207
Ted Kremenekf031b872008-01-23 19:59:44 +00001208 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +00001209 // In such cases, we want to (initially) treat the LHS as an LVal,
1210 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1211 // evaluated to LValDecl's instead of to an NonLVal.
1212
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001213 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1214 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +00001215
Ted Kremenek07baa252008-02-21 18:02:17 +00001216 // Visit the RHS...
1217
1218 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +00001219 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001220
1221 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +00001222
Ted Kremenek07baa252008-02-21 18:02:17 +00001223 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +00001224
Ted Kremenekf031b872008-01-23 19:59:44 +00001225 NodeTy* N2 = *I2;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001226 ValueState* St = GetState(N2);
Ted Kremenek2c369792008-02-25 18:42:54 +00001227 Expr* RHS = B->getRHS();
1228 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +00001229
Ted Kremenek15cb0782008-02-06 22:50:25 +00001230 BinaryOperator::Opcode Op = B->getOpcode();
1231
Ted Kremenek2c369792008-02-25 18:42:54 +00001232 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1233 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +00001234
Ted Kremenekb31af242008-02-28 09:25:22 +00001235 // Check if the denominator is undefined.
Ted Kremenek2c369792008-02-25 18:42:54 +00001236
Ted Kremenek5a600862008-02-26 22:27:51 +00001237 if (!RightV.isUnknown()) {
1238
Ted Kremenekb31af242008-02-28 09:25:22 +00001239 if (RightV.isUndef()) {
1240 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek5a600862008-02-26 22:27:51 +00001241
Ted Kremenekb31af242008-02-28 09:25:22 +00001242 if (DivUndef) {
1243 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001244 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek5a600862008-02-26 22:27:51 +00001245 }
1246
1247 continue;
1248 }
1249
1250 // Check for divide/remainder-by-zero.
1251 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001252 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001253
Ted Kremenek75f32c62008-03-07 19:04:53 +00001254 bool isFeasibleZero = false;
1255 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001256
Ted Kremenek5a600862008-02-26 22:27:51 +00001257 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001258
Ted Kremenek75f32c62008-03-07 19:04:53 +00001259 bool isFeasibleNotZero = false;
1260 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001261
Ted Kremenek75f32c62008-03-07 19:04:53 +00001262 // Create the node for the divide-by-zero (if it occurred).
1263
1264 if (isFeasibleZero)
1265 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1266 DivZeroNode->markAsSink();
1267
1268 if (isFeasibleNotZero)
1269 ImplicitBadDivides.insert(DivZeroNode);
1270 else
1271 ExplicitBadDivides.insert(DivZeroNode);
1272
1273 }
1274
1275 if (!isFeasibleNotZero)
Ted Kremenek5a600862008-02-26 22:27:51 +00001276 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001277 }
1278
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001279 // Fall-through. The logic below processes the divide.
1280 }
1281
Ted Kremenekc2d07202008-02-28 20:32:03 +00001282
Ted Kremenek15cb0782008-02-06 22:50:25 +00001283 if (Op <= BinaryOperator::Or) {
1284
Ted Kremenek07baa252008-02-21 18:02:17 +00001285 // Process non-assignements except commas or short-circuited
1286 // logical expressions (LAnd and LOr).
1287
1288 RVal Result = EvalBinOp(Op, LeftV, RightV);
1289
1290 if (Result.isUnknown()) {
1291 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001292 continue;
1293 }
1294
Ted Kremenekc2d07202008-02-28 20:32:03 +00001295 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1296
1297 // The operands were not undefined, but the result is undefined.
1298
1299 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1300 UndefNode->markAsSink();
1301 UndefResults.insert(UndefNode);
1302 }
1303
1304 continue;
1305 }
1306
Ted Kremenekf10f2882008-03-21 21:30:14 +00001307 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001308 continue;
1309 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001310
1311 // Process assignments.
1312
1313 switch (Op) {
1314
Ted Kremenekf031b872008-01-23 19:59:44 +00001315 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001316
1317 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001318
Ted Kremenekb31af242008-02-28 09:25:22 +00001319 if (LeftV.isUndef()) {
1320 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001321 continue;
1322 }
1323
Ted Kremenekd4676512008-03-12 21:45:47 +00001324 // EXPERIMENTAL: "Conjured" symbols.
1325
1326 if (RightV.isUnknown()) {
1327 unsigned Count = Builder->getCurrentBlockCount();
1328 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1329
1330 RightV = B->getRHS()->getType()->isPointerType()
1331 ? cast<RVal>(lval::SymbolVal(Sym))
1332 : cast<RVal>(nonlval::SymbolVal(Sym));
1333 }
1334
1335 // Even if the LHS evaluates to an unknown L-Value, the entire
1336 // expression still evaluates to the RHS.
1337
Ted Kremenek07baa252008-02-21 18:02:17 +00001338 if (LeftV.isUnknown()) {
1339 St = SetRVal(St, B, RightV);
1340 break;
1341 }
Ted Kremenekd4676512008-03-12 21:45:47 +00001342
1343 // Simulate the effects of a "store": bind the value of the RHS
1344 // to the L-Value represented by the LHS.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001345
Ted Kremenek07baa252008-02-21 18:02:17 +00001346 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001347 break;
1348 }
1349
Ted Kremenek07baa252008-02-21 18:02:17 +00001350 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001351
Ted Kremenek07baa252008-02-21 18:02:17 +00001352 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001353
Ted Kremenek5a600862008-02-26 22:27:51 +00001354 assert (B->isCompoundAssignmentOp());
1355
1356 if (Op >= BinaryOperator::AndAssign)
1357 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1358 else
1359 ((int&) Op) -= BinaryOperator::MulAssign;
1360
Ted Kremenekb31af242008-02-28 09:25:22 +00001361 // Check if the LHS is undefined.
Ted Kremenek07baa252008-02-21 18:02:17 +00001362
Ted Kremenekb31af242008-02-28 09:25:22 +00001363 if (LeftV.isUndef()) {
1364 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001365 continue;
1366 }
1367
1368 if (LeftV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001369 assert (isa<UnknownVal>(GetRVal(St, B)));
1370 Dst.Add(N2);
1371 continue;
Ted Kremenekbf988d02008-02-19 00:22:37 +00001372 }
1373
Ted Kremenek07baa252008-02-21 18:02:17 +00001374 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001375 // that is neither "Unknown" or "Undefined."
Ted Kremenek07baa252008-02-21 18:02:17 +00001376
1377 LVal LeftLV = cast<LVal>(LeftV);
1378
Ted Kremenek07baa252008-02-21 18:02:17 +00001379 // Fetch the value of the LHS (the value of the variable, etc.).
1380
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001381 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenek07baa252008-02-21 18:02:17 +00001382
Ted Kremenekb31af242008-02-28 09:25:22 +00001383 // Propagate undefined value (left-side). We
1384 // propogate undefined values for the RHS below when
Ted Kremenek5a600862008-02-26 22:27:51 +00001385 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001386
Ted Kremenekb31af242008-02-28 09:25:22 +00001387 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001388 St = SetRVal(St, B, V);
1389 break;
1390 }
1391
1392 // Propagate unknown values.
1393
Ted Kremenekb8782e12008-02-21 19:15:37 +00001394 if (V.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001395 // The value bound to LeftV is unknown. Thus we just
1396 // propagate the current node (as "B" is already bound to nothing).
1397 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenekb8782e12008-02-21 19:15:37 +00001398 Dst.Add(N2);
1399 continue;
1400 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001401
1402 if (RightV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001403 assert (isa<UnknownVal>(GetRVal(St, B)));
1404 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenek07baa252008-02-21 18:02:17 +00001405 break;
1406 }
1407
Ted Kremenek5a600862008-02-26 22:27:51 +00001408 // At this point:
1409 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001410 // The LHS is not Undef/Unknown.
Ted Kremenek5a600862008-02-26 22:27:51 +00001411 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001412
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001413 // Get the computation type.
1414 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1415
1416 // Perform promotions.
1417 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001418 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001419
1420 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001421
Ted Kremenek2c369792008-02-25 18:42:54 +00001422 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1423 && RHS->getType()->isIntegerType()) {
1424
Ted Kremenekb31af242008-02-28 09:25:22 +00001425 // Check if the denominator is undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001426
Ted Kremenekb31af242008-02-28 09:25:22 +00001427 if (RightV.isUndef()) {
1428 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001429
Ted Kremenekb31af242008-02-28 09:25:22 +00001430 if (DivUndef) {
1431 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001432 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001433 }
1434
1435 continue;
1436 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001437
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001438 // First, "assume" that the denominator is 0.
1439
Ted Kremenek75f32c62008-03-07 19:04:53 +00001440 bool isFeasibleZero = false;
1441 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001442
Ted Kremenek75f32c62008-03-07 19:04:53 +00001443 // Second, "assume" that the denominator cannot be 0.
1444
1445 bool isFeasibleNotZero = false;
1446 St = Assume(St, RightV, true, isFeasibleNotZero);
1447
1448 // Create the node for the divide-by-zero error (if it occurred).
1449
1450 if (isFeasibleZero) {
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001451 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1452
1453 if (DivZeroNode) {
1454 DivZeroNode->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001455
1456 if (isFeasibleNotZero)
1457 ImplicitBadDivides.insert(DivZeroNode);
1458 else
1459 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001460 }
1461 }
1462
Ted Kremenek75f32c62008-03-07 19:04:53 +00001463 if (!isFeasibleNotZero)
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001464 continue;
1465
1466 // Fall-through. The logic below processes the divide.
1467 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001468 else {
1469
Ted Kremenekb31af242008-02-28 09:25:22 +00001470 // Propagate undefined values (right-side).
Ted Kremenek5a600862008-02-26 22:27:51 +00001471
Ted Kremenekb31af242008-02-28 09:25:22 +00001472 if (RightV.isUndef()) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001473 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1474 break;
1475 }
1476
1477 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001478
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001479 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekc2d07202008-02-28 20:32:03 +00001480
1481 if (Result.isUndef()) {
1482
1483 // The operands were not undefined, but the result is undefined.
1484
1485 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1486 UndefNode->markAsSink();
1487 UndefResults.insert(UndefNode);
1488 }
1489
1490 continue;
1491 }
1492
Ted Kremenek07baa252008-02-21 18:02:17 +00001493 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001494 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001495 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001496
Ted Kremenekf10f2882008-03-21 21:30:14 +00001497 MakeNode(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001498 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001499 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001500}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001501
Ted Kremenekb31af242008-02-28 09:25:22 +00001502void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001503 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001504 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +00001505 UndefStores.insert(N);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001506}
Ted Kremenekbe962452008-01-16 19:42:59 +00001507
Ted Kremenekbf988d02008-02-19 00:22:37 +00001508void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001509
1510 // FIXME: add metadata to the CFG so that we can disable
1511 // this check when we KNOW that there is no block-level subexpression.
1512 // The motivation is that this check requires a hashtable lookup.
1513
1514 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1515 Dst.Add(Pred);
1516 return;
1517 }
1518
1519 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001520
1521 default:
1522 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001523 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001524
1525 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1526 break;
Ted Kremenek52b8d0e2008-03-17 21:31:48 +00001527
1528 case Stmt::AsmStmtClass:
1529 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
1530 break;
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001531
Ted Kremenek744a7862008-02-08 20:29:23 +00001532 case Stmt::BinaryOperatorClass: {
1533 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001534
Ted Kremenek744a7862008-02-08 20:29:23 +00001535 if (B->isLogicalOp()) {
1536 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001537 break;
1538 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001539 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001540 ValueState* St = GetState(Pred);
Ted Kremenekf10f2882008-03-21 21:30:14 +00001541 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001542 break;
1543 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001544
1545 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1546 break;
1547 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001548
1549 case Stmt::CallExprClass: {
1550 CallExpr* C = cast<CallExpr>(S);
1551 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1552 break;
1553 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001554
1555 case Stmt::CastExprClass: {
1556 CastExpr* C = cast<CastExpr>(S);
1557 VisitCast(C, C->getSubExpr(), Pred, Dst);
1558 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001559 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001560
Ted Kremenek07baa252008-02-21 18:02:17 +00001561 // FIXME: ChooseExpr is really a constant. We need to fix
1562 // the CFG do not model them as explicit control-flow.
1563
Ted Kremenekfd85f292008-02-12 19:49:57 +00001564 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1565 ChooseExpr* C = cast<ChooseExpr>(S);
1566 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1567 break;
1568 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001569
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001570 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001571 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1572 break;
1573
Ted Kremenekfd85f292008-02-12 19:49:57 +00001574 case Stmt::ConditionalOperatorClass: { // '?' operator
1575 ConditionalOperator* C = cast<ConditionalOperator>(S);
1576 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1577 break;
1578 }
1579
1580 case Stmt::DeclRefExprClass:
1581 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1582 break;
1583
1584 case Stmt::DeclStmtClass:
1585 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1586 break;
1587
1588 case Stmt::ImplicitCastExprClass: {
1589 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1590 VisitCast(C, C->getSubExpr(), Pred, Dst);
1591 break;
1592 }
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001593
1594 case Stmt::ObjCMessageExprClass: {
1595 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
1596 break;
1597 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001598
1599 case Stmt::ParenExprClass:
1600 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1601 break;
1602
1603 case Stmt::SizeOfAlignOfTypeExprClass:
1604 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1605 break;
1606
Ted Kremenek106f37c2008-02-08 07:05:39 +00001607 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001608 StmtExpr* SE = cast<StmtExpr>(S);
1609
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001610 ValueState* St = GetState(Pred);
Ted Kremeneka0a7c412008-03-15 03:27:30 +00001611
1612 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
1613 // probably just remove these from the CFG.
1614 assert (!SE->getSubStmt()->body_empty());
1615
1616 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Ted Kremenekf10f2882008-03-21 21:30:14 +00001617 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremeneka0a7c412008-03-15 03:27:30 +00001618 else
1619 Dst.Add(Pred);
1620
1621 break;
Ted Kremenek106f37c2008-02-08 07:05:39 +00001622 }
1623
Ted Kremenek07baa252008-02-21 18:02:17 +00001624 // FIXME: We may wish to always bind state to ReturnStmts so
1625 // that users can quickly query what was the state at the
1626 // exit points of a function.
1627
Ted Kremenekfd85f292008-02-12 19:49:57 +00001628 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001629 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1630 Visit(R, Pred, Dst);
1631 else
1632 Dst.Add(Pred);
1633
1634 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001635 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001636
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001637 case Stmt::UnaryOperatorClass: {
1638 UnaryOperator* U = cast<UnaryOperator>(S);
1639
Ted Kremenek07baa252008-02-21 18:02:17 +00001640 switch (U->getOpcode()) {
1641 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1642 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1643 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1644 default: VisitUnaryOperator(U, Pred, Dst); break;
1645 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001646
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001647 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001648 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001649 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001650}
1651
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001652//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001653// "Assume" logic.
1654//===----------------------------------------------------------------------===//
1655
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001656ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001657 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001658 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001659 switch (Cond.getSubKind()) {
1660 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001661 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001662 return St;
1663
Ted Kremenek13f31562008-02-06 00:54:14 +00001664 case lval::SymbolValKind:
1665 if (Assumption)
1666 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001667 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001668 else
1669 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001670 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001671
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001672
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001673 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001674 case lval::FuncValKind:
1675 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001676 isFeasible = Assumption;
1677 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001678
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001679 case lval::ConcreteIntKind: {
1680 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001681 isFeasible = b ? Assumption : !Assumption;
1682 return St;
1683 }
1684 }
Ted Kremenek90960972008-01-30 23:03:39 +00001685}
1686
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001687ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001688 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001689 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001690 switch (Cond.getSubKind()) {
1691 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001692 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001693 return St;
1694
Ted Kremenekab359c12008-02-06 17:32:17 +00001695
1696 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001697 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001698 SymbolID sym = SV.getSymbol();
1699
1700 if (Assumption)
Ted Kremenek8ad19872008-03-07 20:13:31 +00001701 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001702 isFeasible);
1703 else
Ted Kremenek8ad19872008-03-07 20:13:31 +00001704 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001705 isFeasible);
1706 }
1707
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001708 case nonlval::SymIntConstraintValKind:
1709 return
1710 AssumeSymInt(St, Assumption,
1711 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1712 isFeasible);
1713
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001714 case nonlval::ConcreteIntKind: {
1715 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001716 isFeasible = b ? Assumption : !Assumption;
1717 return St;
1718 }
1719 }
1720}
1721
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001722ValueState*
1723GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001724 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001725
Ted Kremenek13f31562008-02-06 00:54:14 +00001726 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001727 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001728 isFeasible = *X != V;
1729 return St;
1730 }
1731
1732 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001733 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001734 isFeasible = true;
1735 return St;
1736 }
1737
1738 // If we reach here, sym is not a constant and we don't know if it is != V.
1739 // Make that assumption.
1740
1741 isFeasible = true;
1742 return StateMgr.AddNE(St, sym, V);
1743}
1744
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001745ValueState*
1746GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001747 const llvm::APSInt& V, bool& isFeasible) {
1748
1749 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001750 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001751 isFeasible = *X == V;
1752 return St;
1753 }
1754
1755 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001756 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001757 isFeasible = false;
1758 return St;
1759 }
1760
1761 // If we reach here, sym is not a constant and we don't know if it is == V.
1762 // Make that assumption.
1763
1764 isFeasible = true;
1765 return StateMgr.AddEQ(St, sym, V);
1766}
Ted Kremenek90960972008-01-30 23:03:39 +00001767
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001768ValueState*
1769GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001770 const SymIntConstraint& C, bool& isFeasible) {
1771
1772 switch (C.getOpcode()) {
1773 default:
1774 // No logic yet for other operators.
Ted Kremenekd4676512008-03-12 21:45:47 +00001775 isFeasible = true;
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001776 return St;
1777
1778 case BinaryOperator::EQ:
1779 if (Assumption)
1780 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1781 else
1782 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1783
1784 case BinaryOperator::NE:
1785 if (Assumption)
1786 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1787 else
1788 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1789 }
1790}
1791
Ted Kremenek90960972008-01-30 23:03:39 +00001792//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001793// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001794//===----------------------------------------------------------------------===//
1795
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001796#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001797static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001798static SourceManager* GraphPrintSourceManager;
Ted Kremenek9f597922008-03-11 19:02:40 +00001799static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001800
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001801namespace llvm {
1802template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001803struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001804 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001805
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001806 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001807
1808 Out << "Variables:\\l";
1809
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001810 bool isFirst = true;
1811
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001812 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001813
1814 if (isFirst)
1815 isFirst = false;
1816 else
1817 Out << "\\l";
1818
1819 Out << ' ' << I.getKey()->getName() << " : ";
1820 I.getData().print(Out);
1821 }
1822
1823 }
1824
Ted Kremenek17c5f112008-02-11 19:21:59 +00001825
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001826 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001827
1828 bool isFirst = true;
1829
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001830 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001831
1832 if (isFirst) {
1833 Out << "\\l\\lSub-Expressions:\\l";
1834 isFirst = false;
1835 }
1836 else
1837 Out << "\\l";
1838
1839 Out << " (" << (void*) I.getKey() << ") ";
1840 I.getKey()->printPretty(Out);
1841 Out << " : ";
1842 I.getData().print(Out);
1843 }
1844 }
1845
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001846 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001847
Ted Kremenek08cfd832008-02-08 21:10:02 +00001848 bool isFirst = true;
1849
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001850 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001851 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001852 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001853 isFirst = false;
1854 }
1855 else
1856 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001857
Ted Kremenek17c5f112008-02-11 19:21:59 +00001858 Out << " (" << (void*) I.getKey() << ") ";
1859 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001860 Out << " : ";
1861 I.getData().print(Out);
1862 }
1863 }
1864
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001865 static void PrintEQ(std::ostream& Out, ValueState* St) {
1866 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001867
1868 if (CE.isEmpty())
1869 return;
1870
1871 Out << "\\l\\|'==' constraints:";
1872
Ted Kremenek07baa252008-02-21 18:02:17 +00001873 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001874 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1875 }
1876
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001877 static void PrintNE(std::ostream& Out, ValueState* St) {
1878 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001879
1880 if (NE.isEmpty())
1881 return;
1882
1883 Out << "\\l\\|'!=' constraints:";
1884
Ted Kremenek07baa252008-02-21 18:02:17 +00001885 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001886 I != EI; ++I){
1887
1888 Out << "\\l $" << I.getKey() << " : ";
1889 bool isFirst = true;
1890
1891 ValueState::IntSetTy::iterator J=I.getData().begin(),
1892 EJ=I.getData().end();
1893 for ( ; J != EJ; ++J) {
1894 if (isFirst) isFirst = false;
1895 else Out << ", ";
1896
1897 Out << (*J)->toString();
1898 }
1899 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001900 }
1901
1902 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1903
1904 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001905 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00001906 GraphPrintCheckerState->isUndefDeref(N) ||
1907 GraphPrintCheckerState->isUndefStore(N) ||
1908 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00001909 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1910 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00001911 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001912 GraphPrintCheckerState->isBadCall(N) ||
1913 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001914 return "color=\"red\",style=\"filled\"";
1915
Ted Kremenekc2d07202008-02-28 20:32:03 +00001916 if (GraphPrintCheckerState->isNoReturnCall(N))
1917 return "color=\"blue\",style=\"filled\"";
1918
Ted Kremeneka853de62008-02-14 22:54:53 +00001919 return "";
1920 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001921
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001922 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001923 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001924
1925 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001926 ProgramPoint Loc = N->getLocation();
1927
1928 switch (Loc.getKind()) {
1929 case ProgramPoint::BlockEntranceKind:
1930 Out << "Block Entrance: B"
1931 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1932 break;
1933
1934 case ProgramPoint::BlockExitKind:
1935 assert (false);
1936 break;
1937
1938 case ProgramPoint::PostStmtKind: {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001939 const PostStmt& L = cast<PostStmt>(Loc);
1940 Stmt* S = L.getStmt();
1941 SourceLocation SLoc = S->getLocStart();
1942
1943 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
1944 S->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001945
Ted Kremenekf97c6682008-03-09 03:30:59 +00001946 if (SLoc.isFileID()) {
1947 Out << "\\lline="
1948 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1949 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
1950 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001951
Ted Kremenek43863eb2008-02-29 23:14:48 +00001952 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenek80d52d02008-02-07 05:48:01 +00001953 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001954 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001955 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001956 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001957 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001958 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001959 Out << "\\|Store to Undefined LVal.";
Ted Kremenek75f32c62008-03-07 19:04:53 +00001960 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1961 Out << "\\|Explicit divide-by zero or undefined value.";
1962 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
1963 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001964 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenekc2d07202008-02-28 20:32:03 +00001965 Out << "\\|Result of operation is undefined.";
Ted Kremenekc2d07202008-02-28 20:32:03 +00001966 else if (GraphPrintCheckerState->isNoReturnCall(N))
1967 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001968 else if (GraphPrintCheckerState->isBadCall(N))
1969 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001970 else if (GraphPrintCheckerState->isUndefArg(N))
1971 Out << "\\|Argument in call is undefined";
Ted Kremenek80d52d02008-02-07 05:48:01 +00001972
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001973 break;
1974 }
1975
1976 default: {
1977 const BlockEdge& E = cast<BlockEdge>(Loc);
1978 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1979 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001980
1981 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001982
1983 SourceLocation SLoc = T->getLocStart();
1984
Ted Kremenek90960972008-01-30 23:03:39 +00001985 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001986
Ted Kremenek90960972008-01-30 23:03:39 +00001987 E.getSrc()->printTerminator(Out);
1988
Ted Kremenekf97c6682008-03-09 03:30:59 +00001989 if (SLoc.isFileID()) {
1990 Out << "\\lline="
1991 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1992 << GraphPrintSourceManager->getColumnNumber(SLoc);
1993 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001994
Ted Kremenekaee121c2008-02-13 23:08:21 +00001995 if (isa<SwitchStmt>(T)) {
1996 Stmt* Label = E.getDst()->getLabel();
1997
1998 if (Label) {
1999 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2000 Out << "\\lcase ";
2001 C->getLHS()->printPretty(Out);
2002
2003 if (Stmt* RHS = C->getRHS()) {
2004 Out << " .. ";
2005 RHS->printPretty(Out);
2006 }
2007
2008 Out << ":";
2009 }
2010 else {
2011 assert (isa<DefaultStmt>(Label));
2012 Out << "\\ldefault:";
2013 }
2014 }
2015 else
2016 Out << "\\l(implicit) default:";
2017 }
2018 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00002019 // FIXME
2020 }
2021 else {
2022 Out << "\\lCondition: ";
2023 if (*E.getSrc()->succ_begin() == E.getDst())
2024 Out << "true";
2025 else
2026 Out << "false";
2027 }
2028
2029 Out << "\\l";
2030 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00002031
Ted Kremenekb31af242008-02-28 09:25:22 +00002032 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2033 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00002034 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002035 }
2036 }
2037
Ted Kremenekf4b49df2008-02-28 10:21:43 +00002038 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00002039
Ted Kremenek9f597922008-03-11 19:02:40 +00002040 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00002041
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00002042 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002043 return Out.str();
2044 }
2045};
2046} // end llvm namespace
2047#endif
2048
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002049#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002050
2051template <typename ITERATOR>
2052GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2053
2054template <>
2055GRExprEngine::NodeTy*
2056GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2057 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2058 return I->first;
2059}
2060
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002061template <typename ITERATOR>
2062static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources,
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002063 ITERATOR I, ITERATOR E) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002064
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002065 llvm::SmallPtrSet<void*,10> CachedSources;
2066
2067 for ( ; I != E; ++I ) {
2068 GRExprEngine::NodeTy* N = GetGraphNode(I);
2069 void* p = N->getLocation().getRawData();
2070
2071 if (CachedSources.count(p))
2072 continue;
2073
2074 CachedSources.insert(p);
2075
2076 Sources.push_back(N);
2077 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002078}
2079#endif
2080
2081void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00002082#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002083 if (trim) {
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002084 llvm::SmallVector<NodeTy*, 10> Src;
2085 AddSources(Src, null_derefs_begin(), null_derefs_end());
2086 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2087 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2088 AddSources(Src, undef_results_begin(), undef_results_end());
2089 AddSources(Src, bad_calls_begin(), bad_calls_end());
2090 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek2f0c0e12008-03-14 18:14:50 +00002091 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002092
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002093 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002094 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00002095 else {
2096 GraphPrintCheckerState = this;
2097 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00002098 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremeneke44a8302008-03-11 18:25:33 +00002099
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002100 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00002101
2102 GraphPrintCheckerState = NULL;
2103 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00002104 GraphCheckerStatePrinter = NULL;
Ted Kremeneke44a8302008-03-11 18:25:33 +00002105 }
2106#endif
2107}
2108
2109void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2110#ifndef NDEBUG
2111 GraphPrintCheckerState = this;
2112 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00002113 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002114
Ted Kremeneke44a8302008-03-11 18:25:33 +00002115 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2116
2117 if (!TrimmedG)
2118 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2119 else {
2120 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2121 delete TrimmedG;
2122 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002123
Ted Kremenek428d39e2008-01-30 23:24:39 +00002124 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00002125 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00002126 GraphCheckerStatePrinter = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00002127#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002128}