blob: 4479ecf30b2a8949eb10f2cf219b4c8640c1fd5f [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 Kremenek3862eb12008-02-14 22:36:46 +000017#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
18
19#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000020
21using namespace clang;
22using llvm::dyn_cast;
23using llvm::cast;
24using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000025
Ted Kremenek30fa28b2008-02-13 17:41:41 +000026GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000027GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000028
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000029 if (!StateCleaned) {
30 St = RemoveDeadBindings(CurrentStmt, St);
31 StateCleaned = true;
32 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000033
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000034 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000035
Ted Kremenek07baa252008-02-21 18:02:17 +000036 if (Ex == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000038
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000042
Ted Kremenek07baa252008-02-21 18:02:17 +000043 return StateMgr.SetRVal(St, Ex, isBlkExpr, V);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000044}
45
Ted Kremenek30fa28b2008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
Ted Kremenek07baa252008-02-21 18:02:17 +000047GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
Ted Kremenekf20c2012008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
Ted Kremenek07baa252008-02-21 18:02:17 +000052 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
53 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekf20c2012008-02-05 19:35:18 +000054
55 return RetBuf;
56}
57
Ted Kremenek30fa28b2008-02-13 17:41:41 +000058GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000059GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060
61 if (!StateCleaned) {
62 St = RemoveDeadBindings(CurrentStmt, St);
63 StateCleaned = true;
64 }
65
Ted Kremenek07baa252008-02-21 18:02:17 +000066 return StateMgr.SetRVal(St, LV, RV);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000067}
68
Ted Kremenek99ecce72008-02-26 19:05:15 +000069GRExprEngine::StateTy
70GRExprEngine::MarkBranch(StateTy St, Stmt* Terminator, bool branchTaken) {
71
72 switch (Terminator->getStmtClass()) {
73 default:
74 return St;
75
76 case Stmt::BinaryOperatorClass: { // '&&' and '||'
77
78 BinaryOperator* B = cast<BinaryOperator>(Terminator);
79 BinaryOperator::Opcode Op = B->getOpcode();
80
81 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
82
83 // For &&, if we take the true branch, then the value of the whole
84 // expression is that of the RHS expression.
85 //
86 // For ||, if we take the false branch, then the value of the whole
87 // expression is that of the RHS expression.
88
89 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
90 (Op == BinaryOperator::LOr && !branchTaken)
91 ? B->getRHS() : B->getLHS();
92
93 return SetBlkExprRVal(St, B, UninitializedVal(Ex));
94 }
95
96 case Stmt::ConditionalOperatorClass: { // ?:
97
98 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
99
100 // For ?, if branchTaken == true then the value is either the LHS or
101 // the condition itself. (GNU extension).
102
103 Expr* Ex;
104
105 if (branchTaken)
106 Ex = C->getLHS() ? C->getLHS() : C->getCond();
107 else
108 Ex = C->getRHS();
109
110 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
111 }
112
113 case Stmt::ChooseExprClass: { // ?:
114
115 ChooseExpr* C = cast<ChooseExpr>(Terminator);
116
117 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
118 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
119 }
120 }
121}
122
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000123void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000124 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000125
Ted Kremenek17c5f112008-02-11 19:21:59 +0000126 // Remove old bindings for subexpressions.
127 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000128
Ted Kremenek022b6052008-02-15 22:29:00 +0000129 // Check for NULL conditions; e.g. "for(;;)"
130 if (!Condition) {
131 builder.markInfeasible(false);
132
133 // Get the current block counter.
134 GRBlockCounter BC = builder.getBlockCounter();
135 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
136 unsigned NumVisited = BC.getNumVisited(BlockID);
137
138 if (NumVisited < 1) builder.generateNode(PrevState, true);
139 else builder.markInfeasible(true);
140
141 return;
142 }
143
Ted Kremenek07baa252008-02-21 18:02:17 +0000144 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000145
146 switch (V.getBaseKind()) {
147 default:
148 break;
149
Ted Kremenek07baa252008-02-21 18:02:17 +0000150 case RVal::UnknownKind:
Ted Kremenek90960972008-01-30 23:03:39 +0000151 builder.generateNode(PrevState, true);
152 builder.generateNode(PrevState, false);
153 return;
154
Ted Kremenek07baa252008-02-21 18:02:17 +0000155 case RVal::UninitializedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000156 NodeTy* N = builder.generateNode(PrevState, true);
157
158 if (N) {
159 N->markAsSink();
160 UninitBranches.insert(N);
161 }
162
163 builder.markInfeasible(false);
164 return;
165 }
166 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000167
Ted Kremenek4b170e52008-02-12 18:08:17 +0000168 // Get the current block counter.
169 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekfd85f292008-02-12 19:49:57 +0000170 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
171 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000172
Ted Kremenek4b170e52008-02-12 18:08:17 +0000173 if (isa<nonlval::ConcreteInt>(V) ||
174 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
175
176 // Process the true branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000177
Ted Kremenek4b170e52008-02-12 18:08:17 +0000178 bool isFeasible = true;
179
180 StateTy St = Assume(PrevState, V, true, isFeasible);
181
182 if (isFeasible)
Ted Kremenek99ecce72008-02-26 19:05:15 +0000183 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000184 else
185 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000186 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000187 else
188 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000189
Ted Kremenekfd85f292008-02-12 19:49:57 +0000190 BlockID = builder.getTargetBlock(false)->getBlockID();
191 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000192
Ted Kremenek4b170e52008-02-12 18:08:17 +0000193 if (isa<nonlval::ConcreteInt>(V) ||
194 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
195
196 // Process the false branch.
197
198 bool isFeasible = false;
199
200 StateTy St = Assume(PrevState, V, false, isFeasible);
201
202 if (isFeasible)
Ted Kremenek99ecce72008-02-26 19:05:15 +0000203 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000204 else
205 builder.markInfeasible(false);
206 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000207 else
208 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000209}
210
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000211/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000212/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000213void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000214
215 StateTy St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000216 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000217
218 // Three possibilities:
219 //
220 // (1) We know the computed label.
221 // (2) The label is NULL (or some other constant), or Uninitialized.
222 // (3) We have no clue about the label. Dispatch to all targets.
223 //
224
225 typedef IndirectGotoNodeBuilder::iterator iterator;
226
227 if (isa<lval::GotoLabel>(V)) {
228 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
229
230 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000231 if (I.getLabel() == L) {
232 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000233 return;
234 }
235 }
236
237 assert (false && "No block with label.");
238 return;
239 }
240
241 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
242 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000243 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000244 UninitBranches.insert(N);
245 return;
246 }
247
248 // This is really a catch-all. We don't support symbolics yet.
249
Ted Kremenek07baa252008-02-21 18:02:17 +0000250 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000251
252 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000253 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000254}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000255
Ted Kremenekaee121c2008-02-13 23:08:21 +0000256/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
257/// nodes by processing the 'effects' of a switch statement.
258void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
259
260 typedef SwitchNodeBuilder::iterator iterator;
261
262 StateTy St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000263 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000264 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000265
Ted Kremenek07baa252008-02-21 18:02:17 +0000266 if (CondV.isUninit()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000267 NodeTy* N = builder.generateDefaultCaseNode(St, true);
268 UninitBranches.insert(N);
269 return;
270 }
271
272 StateTy DefaultSt = St;
273
274 // While most of this can be assumed (such as the signedness), having it
275 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000276
277 unsigned bits = getContext().getTypeSize(CondE->getType(),
278 CondE->getExprLoc());
279
Ted Kremenekaee121c2008-02-13 23:08:21 +0000280 APSInt V1(bits, false);
281 APSInt V2 = V1;
282
Ted Kremenek07baa252008-02-21 18:02:17 +0000283 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000284
285 CaseStmt* Case = cast<CaseStmt>(I.getCase());
286
287 // Evaluate the case.
288 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
289 assert (false && "Case condition must evaluate to an integer constant.");
290 return;
291 }
292
293 // Get the RHS of the case, if it exists.
294
295 if (Expr* E = Case->getRHS()) {
296 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
297 assert (false &&
298 "Case condition (RHS) must evaluate to an integer constant.");
299 return ;
300 }
301
302 assert (V1 <= V2);
303 }
304 else V2 = V1;
305
306 // FIXME: Eventually we should replace the logic below with a range
307 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000308 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000309
310 do {
311 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
312
Ted Kremenek07baa252008-02-21 18:02:17 +0000313 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000314
315 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000316 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000317
Ted Kremenekb1934132008-02-14 19:37:24 +0000318 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000319
320 if (isFeasible) {
321 builder.generateCaseStmtNode(I, StNew);
322
323 // If CondV evaluates to a constant, then we know that this
324 // is the *only* case that we can take, so stop evaluating the
325 // others.
326 if (isa<nonlval::ConcreteInt>(CondV))
327 return;
328 }
329
330 // Now "assume" that the case doesn't match. Add this state
331 // to the default state (if it is feasible).
332
Ted Kremenekb1934132008-02-14 19:37:24 +0000333 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000334
335 if (isFeasible)
336 DefaultSt = StNew;
337
338 // Concretize the next value in the range.
339 ++V1;
340
341 } while (V1 < V2);
342 }
343
344 // If we reach here, than we know that the default branch is
345 // possible.
346 builder.generateDefaultCaseNode(DefaultSt);
347}
348
349
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000350void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000351 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000352
Ted Kremenek99ecce72008-02-26 19:05:15 +0000353 assert (B->getOpcode() == BinaryOperator::LAnd ||
354 B->getOpcode() == BinaryOperator::LOr);
355
356 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
357
358 StateTy St = Pred->getState();
359 RVal X = GetBlkExprRVal(St, B);
360
361 assert (X.isUninit());
362
363 Expr* Ex = (Expr*) cast<UninitializedVal>(X).getData();
364
365 assert (Ex);
366
367 if (Ex == B->getRHS()) {
368
369 X = GetBlkExprRVal(St, Ex);
370
371 // 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;
379 StateTy NewState = Assume(St, X, true, isFeasible);
380
381 if (isFeasible)
382 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
383
384 isFeasible = false;
385 NewState = Assume(St, X, false, isFeasible);
386
387 if (isFeasible)
388 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000389 }
390 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000391 // We took the LHS expression. Depending on whether we are '&&' or
392 // '||' we know what the value of the expression is via properties of
393 // the short-circuiting.
394
395 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
396 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000397 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000398}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000399
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000400
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000401void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000402
Ted Kremenek07baa252008-02-21 18:02:17 +0000403 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000404 StmtEntryNode = builder.getLastNode();
405 CurrentStmt = S;
406 NodeSet Dst;
407 StateCleaned = false;
408
409 Visit(S, StmtEntryNode, Dst);
410
411 // If no nodes were generated, generate a new node that has all the
412 // dead mappings removed.
Ted Kremenek07baa252008-02-21 18:02:17 +0000413
Ted Kremenekf031b872008-01-23 19:59:44 +0000414 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
415 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
416 builder.generateNode(S, St, StmtEntryNode);
417 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000418
Ted Kremenek07baa252008-02-21 18:02:17 +0000419 // For safety, NULL out these variables.
420
Ted Kremenekf031b872008-01-23 19:59:44 +0000421 CurrentStmt = NULL;
422 StmtEntryNode = NULL;
423 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000424}
425
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000426GRExprEngine::NodeTy*
427GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000428
429 // If the state hasn't changed, don't generate a new node.
Ted Kremenek02b5b402008-02-07 15:20:13 +0000430 if (St == Pred->getState())
Ted Kremenek80d52d02008-02-07 05:48:01 +0000431 return NULL;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000432
Ted Kremenek80d52d02008-02-07 05:48:01 +0000433 NodeTy* N = Builder->generateNode(S, St, Pred);
434 Dst.Add(N);
Ted Kremenek07baa252008-02-21 18:02:17 +0000435
Ted Kremenek80d52d02008-02-07 05:48:01 +0000436 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000437}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000438
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000439void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-02-05 19:35:18 +0000440 const StateTy::BufferTy& SB) {
441
442 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
443 Nodify(Dst, S, Pred, *I);
444}
445
Ted Kremenek6d409922008-02-13 18:06:44 +0000446void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000447
Ted Kremenekef0007f2008-02-26 02:15:56 +0000448 if (VarDecl* VD = dyn_cast<VarDecl>(D->getDecl()))
449 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
450
451 StateTy StOld = Pred->getState();
452 StateTy St = Symbolicate(StOld, VD);
453
454 if (!(St == StOld)) {
455 if (D != CurrentStmt)
456 Nodify(Dst, D, Pred, St);
457 else
458 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
459
460 return;
461 }
462 }
463
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000464 if (D != CurrentStmt) {
465 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
466 return;
467 }
468
469 // If we are here, we are loading the value of the decl and binding
470 // it to the block-level expression.
471
Ted Kremenek07baa252008-02-21 18:02:17 +0000472 StateTy St = Pred->getState();
473 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000474}
475
Ted Kremenekd9268e32008-02-19 01:44:53 +0000476void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000477 CallExpr::arg_iterator AI,
478 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000479 NodeSet& Dst) {
480
Ted Kremenek07baa252008-02-21 18:02:17 +0000481 // Process the arguments.
482
483 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000484
Ted Kremenek07baa252008-02-21 18:02:17 +0000485 NodeSet DstTmp;
486 Visit(*AI, Pred, DstTmp);
487 ++AI;
488
489 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
490 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000491
492 return;
493 }
494
495 // If we reach here we have processed all of the arguments. Evaluate
496 // the callee expression.
Ted Kremenekc71901d2008-02-25 21:16:03 +0000497 NodeSet DstTmp;
498 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
499
500 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000501
502 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000503 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
504
Ted Kremenekd9268e32008-02-19 01:44:53 +0000505 StateTy St = (*DI)->getState();
Ted Kremenekc71901d2008-02-25 21:16:03 +0000506 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000507
508 // Check for uninitialized control-flow.
Ted Kremenek07baa252008-02-21 18:02:17 +0000509
510 if (L.isUninit()) {
511
Ted Kremenekd9268e32008-02-19 01:44:53 +0000512 NodeTy* N = Builder->generateNode(CE, St, *DI);
513 N->markAsSink();
514 UninitBranches.insert(N);
515 continue;
516 }
517
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000518 if (L.isUnknown()) {
519 // Invalidate all arguments passed in by reference (LVals).
520 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
521 I != E; ++I) {
522 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000523
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000524 if (isa<LVal>(V))
525 St = SetRVal(St, cast<LVal>(V), UnknownVal());
526 }
527 }
528 else
529 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
530
531 Nodify(Dst, CE, *DI, St);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000532 }
533}
534
Ted Kremenek07baa252008-02-21 18:02:17 +0000535void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000536
Ted Kremenek5f585b02008-02-19 18:52:54 +0000537 NodeSet S1;
Ted Kremenek07baa252008-02-21 18:02:17 +0000538 Visit(Ex, Pred, S1);
Ted Kremenek54eddae2008-01-24 02:02:54 +0000539
Ted Kremenek5f585b02008-02-19 18:52:54 +0000540 QualType T = CastE->getType();
541
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000542 // Check for redundant casts or casting to "void"
543 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000544 Ex->getType() == T ||
545 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000546
Ted Kremenek07baa252008-02-21 18:02:17 +0000547 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000548 Dst.Add(*I1);
549
Ted Kremenek54eddae2008-01-24 02:02:54 +0000550 return;
551 }
552
Ted Kremenek07baa252008-02-21 18:02:17 +0000553 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000554 NodeTy* N = *I1;
555 StateTy St = N->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000556 RVal V = GetRVal(St, Ex);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000557 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000558 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000559}
560
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000561void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000562 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000563
564 StateTy St = Pred->getState();
565
566 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000567 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000568
569 // FIXME: Add support for local arrays.
570 if (VD->getType()->isArrayType())
571 continue;
572
Ted Kremenek1fc70b32008-02-26 00:20:52 +0000573 // FIXME: static variables have an initializer, but the second
574 // time a function is called those values may not be current.
Ted Kremenek07baa252008-02-21 18:02:17 +0000575 const Expr* Ex = VD->getInit();
576
577 St = SetRVal(St, lval::DeclVal(VD),
578 Ex ? GetRVal(St, Ex) : UninitializedVal());
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000579 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000580
581 Nodify(Dst, DS, Pred, St);
582
Ted Kremenek07baa252008-02-21 18:02:17 +0000583 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000584}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000585
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000586
Ted Kremenek07baa252008-02-21 18:02:17 +0000587void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000588 NodeTy* Pred, NodeSet& Dst) {
589
Ted Kremenek99ecce72008-02-26 19:05:15 +0000590 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
591
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000592 StateTy St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000593 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000594
Ted Kremenek99ecce72008-02-26 19:05:15 +0000595 assert (X.isUninit());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000596
Ted Kremenek99ecce72008-02-26 19:05:15 +0000597 Expr* SE = (Expr*) cast<UninitializedVal>(X).getData();
598
599 assert (SE);
600
601 X = GetBlkExprRVal(St, SE);
602 Nodify(Dst, Ex, Pred, SetBlkExprRVal(St, Ex, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000603}
604
Ted Kremenekfd85f292008-02-12 19:49:57 +0000605/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000606void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
607 NodeTy* Pred,
608 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000609
610 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000611
612 // 6.5.3.4 sizeof: "The result type is an integer."
613
Ted Kremenek07baa252008-02-21 18:02:17 +0000614 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000615
Ted Kremenek07baa252008-02-21 18:02:17 +0000616
Ted Kremenek571f5192008-02-21 18:15:29 +0000617 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000618 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000619 return;
620
Ted Kremenek571f5192008-02-21 18:15:29 +0000621
622 uint64_t size = 1; // Handle sizeof(void)
623
624 if (T != getContext().VoidTy) {
625 SourceLocation Loc = Ex->getExprLoc();
626 size = getContext().getTypeSize(T, Loc) / 8;
627 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000628
Ted Kremenek07baa252008-02-21 18:02:17 +0000629 Nodify(Dst, Ex, Pred,
630 SetRVal(Pred->getState(), Ex,
Ted Kremenek571f5192008-02-21 18:15:29 +0000631 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000632
633}
634
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000635void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
636
Ted Kremenek07baa252008-02-21 18:02:17 +0000637 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000638
639 NodeSet DstTmp;
640
Ted Kremenek56a89992008-02-26 03:44:25 +0000641 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000642 DstTmp.Add(Pred);
643 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000644 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000645
Ted Kremenek07baa252008-02-21 18:02:17 +0000646 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000647
648 NodeTy* N = *I;
649 StateTy St = N->getState();
650
651 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
652
Ted Kremenek07baa252008-02-21 18:02:17 +0000653 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000654
Ted Kremenek07baa252008-02-21 18:02:17 +0000655 // Check for dereferences of uninitialized values.
656
657 if (V.isUninit()) {
658
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000659 NodeTy* Succ = Builder->generateNode(U, St, N);
660
661 if (Succ) {
662 Succ->markAsSink();
663 UninitDeref.insert(Succ);
664 }
665
666 continue;
667 }
668
Ted Kremenek07baa252008-02-21 18:02:17 +0000669 // Check for dereferences of unknown values. Treat as No-Ops.
670
671 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000672 Dst.Add(N);
673 continue;
674 }
675
676 // After a dereference, one of two possible situations arise:
677 // (1) A crash, because the pointer was NULL.
678 // (2) The pointer is not NULL, and the dereference works.
679 //
680 // We add these assumptions.
681
Ted Kremenek07baa252008-02-21 18:02:17 +0000682 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000683 bool isFeasibleNotNull;
684
685 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000686
687 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000688
689 if (isFeasibleNotNull) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000690
691 // FIXME: Currently symbolic analysis "generates" new symbols
692 // for the contents of values. We need a better approach.
693
Ted Kremenek07baa252008-02-21 18:02:17 +0000694 Nodify(Dst, U, N, SetRVal(StNotNull, U,
695 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000696 }
697
698 bool isFeasibleNull;
699
Ted Kremenek07baa252008-02-21 18:02:17 +0000700 // Now "assume" that the pointer is NULL.
701
702 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000703
704 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000705
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000706 // We don't use "Nodify" here because the node will be a sink
707 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000708
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000709 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
710
711 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000712
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000713 NullNode->markAsSink();
714
Ted Kremenek07baa252008-02-21 18:02:17 +0000715 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
716 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000717 }
718 }
719 }
720}
721
Ted Kremenek07baa252008-02-21 18:02:17 +0000722void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
723 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000724
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000725 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000726
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000727 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000728 assert (U->getOpcode() != UnaryOperator::SizeOf);
729 assert (U->getOpcode() != UnaryOperator::AlignOf);
730
731 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000732
733 switch (U->getOpcode()) {
734 case UnaryOperator::PostInc:
735 case UnaryOperator::PostDec:
736 case UnaryOperator::PreInc:
737 case UnaryOperator::PreDec:
738 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000739 // Evalue subexpression as an LVal.
740 use_GetLVal = true;
741 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000742 break;
743
744 default:
745 Visit(U->getSubExpr(), Pred, S1);
746 break;
747 }
748
Ted Kremenek07baa252008-02-21 18:02:17 +0000749 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000750
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000751 NodeTy* N1 = *I1;
752 StateTy St = N1->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000753
754 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
755 GetRVal(St, U->getSubExpr());
756
757 if (SubV.isUnknown()) {
758 Dst.Add(N1);
759 continue;
760 }
761
762 if (SubV.isUninit()) {
763 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
764 continue;
765 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000766
Ted Kremenek22640ce2008-02-15 22:09:30 +0000767 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000768
769 // Handle ++ and -- (both pre- and post-increment).
770
Ted Kremenek07baa252008-02-21 18:02:17 +0000771 LVal SubLV = cast<LVal>(SubV);
772 RVal V = GetRVal(St, SubLV, U->getType());
773
Ted Kremenekb8782e12008-02-21 19:15:37 +0000774 if (V.isUnknown()) {
775 Dst.Add(N1);
776 continue;
777 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000778
779 // Propagate uninitialized values.
780 if (V.isUninit()) {
781 Nodify(Dst, U, N1, SetRVal(St, U, V));
782 continue;
783 }
784
Ted Kremenekb1669d42008-02-21 19:29:23 +0000785 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000786
787 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
788 : BinaryOperator::Sub;
789
Ted Kremenekb1669d42008-02-21 19:29:23 +0000790 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000791
792 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000793 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000794 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000795 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000796
Ted Kremenek07baa252008-02-21 18:02:17 +0000797 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000798 continue;
799 }
800
801 // Handle all other unary operators.
802
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000803 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000804
Ted Kremenek07baa252008-02-21 18:02:17 +0000805 case UnaryOperator::Minus:
806 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000807 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000808
Ted Kremenek07baa252008-02-21 18:02:17 +0000809 case UnaryOperator::Not:
810 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000811 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000812
Ted Kremenek07baa252008-02-21 18:02:17 +0000813 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000814
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000815 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
816 //
817 // Note: technically we do "E == 0", but this is the same in the
818 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000819
820 if (isa<LVal>(SubV)) {
821 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
822 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
823 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000824 }
825 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000826 Expr* Ex = U->getSubExpr();
827 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000828 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
829 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000830 }
831
832 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000833
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000834 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000835 assert (isa<LVal>(SubV));
836 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000837 break;
838 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000839
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000840 default: ;
841 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000842 }
843
844 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000845 }
846}
847
Ted Kremenek07baa252008-02-21 18:02:17 +0000848void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
849 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000850
Ted Kremenek07baa252008-02-21 18:02:17 +0000851 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000852
Ted Kremenek07baa252008-02-21 18:02:17 +0000853 // FIXME: Add support for VLAs.
854 if (!T.getTypePtr()->isConstantSizeType())
855 return;
856
857 SourceLocation Loc = U->getExprLoc();
858 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
859 StateTy St = Pred->getState();
860 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
861
862 Nodify(Dst, U, Pred, St);
863}
864
865void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
866
867 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
868
869 Ex = Ex->IgnoreParens();
870
Ted Kremenekef0007f2008-02-26 02:15:56 +0000871 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) {
872
873 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
874 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
875
876 StateTy StOld = Pred->getState();
877 StateTy St = Symbolicate(StOld, VD);
878
879 if (!(St == StOld)) {
880 Nodify(Dst, Ex, Pred, St);
881 return;
882 }
883 }
884
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000885 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000886 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000887 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000888
Ted Kremenek07baa252008-02-21 18:02:17 +0000889 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000890 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000891 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000892
Ted Kremenek07baa252008-02-21 18:02:17 +0000893 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000894 Dst.Add(Pred);
895 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000896 Visit(Ex, Pred, Dst);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000897
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000898 return;
899 }
900 }
901
Ted Kremenek07baa252008-02-21 18:02:17 +0000902 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000903}
904
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000905void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000906 GRExprEngine::NodeTy* Pred,
907 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000908 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000909
910 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +0000911 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000912 else
913 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000914
Ted Kremenekf031b872008-01-23 19:59:44 +0000915 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000916
Ted Kremenekf031b872008-01-23 19:59:44 +0000917 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000918
Ted Kremenekf031b872008-01-23 19:59:44 +0000919 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +0000920 // In such cases, we want to (initially) treat the LHS as an LVal,
921 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
922 // evaluated to LValDecl's instead of to an NonLVal.
923
924 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
925 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000926
Ted Kremenek07baa252008-02-21 18:02:17 +0000927 // Visit the RHS...
928
929 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +0000930 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +0000931
932 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +0000933
Ted Kremenek07baa252008-02-21 18:02:17 +0000934 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000935
Ted Kremenekf031b872008-01-23 19:59:44 +0000936 NodeTy* N2 = *I2;
Ted Kremenek07baa252008-02-21 18:02:17 +0000937 StateTy St = N2->getState();
Ted Kremenek2c369792008-02-25 18:42:54 +0000938 Expr* RHS = B->getRHS();
939 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +0000940
Ted Kremenek15cb0782008-02-06 22:50:25 +0000941 BinaryOperator::Opcode Op = B->getOpcode();
942
Ted Kremenek2c369792008-02-25 18:42:54 +0000943 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
944 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +0000945
946 // Check if the denominator is uninitialized.
Ted Kremenek2c369792008-02-25 18:42:54 +0000947
Ted Kremenekef0007f2008-02-26 02:15:56 +0000948 if (RightV.isUninit()) {
949 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
950
951 if (DivUninit) {
952 DivUninit->markAsSink();
953 BadDivides.insert(DivUninit);
954 }
955
956 continue;
957 }
958
959 // Check for divide/remainder-by-zero.
960 //
961 // First, "assume" that the denominator is 0 or uninitialized.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000962
963 bool isFeasible = false;
Ted Kremenekef0007f2008-02-26 02:15:56 +0000964 StateTy ZeroSt = Assume(St, RightV, false,isFeasible);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000965
966 if (isFeasible) {
967 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
968
969 if (DivZeroNode) {
970 DivZeroNode->markAsSink();
Ted Kremenekef0007f2008-02-26 02:15:56 +0000971 BadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000972 }
973 }
974
975 // Second, "assume" that the denominator cannot be 0.
976
977 isFeasible = false;
978 St = Assume(St, RightV, true, isFeasible);
979
980 if (!isFeasible)
981 continue;
982
983 // Fall-through. The logic below processes the divide.
984 }
985
Ted Kremenek15cb0782008-02-06 22:50:25 +0000986 if (Op <= BinaryOperator::Or) {
987
Ted Kremenek07baa252008-02-21 18:02:17 +0000988 // Process non-assignements except commas or short-circuited
989 // logical expressions (LAnd and LOr).
990
991 RVal Result = EvalBinOp(Op, LeftV, RightV);
992
993 if (Result.isUnknown()) {
994 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000995 continue;
996 }
997
Ted Kremenek07baa252008-02-21 18:02:17 +0000998 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000999 continue;
1000 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001001
1002 // Process assignments.
1003
1004 switch (Op) {
1005
Ted Kremenekf031b872008-01-23 19:59:44 +00001006 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001007
1008 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001009
Ted Kremenek07baa252008-02-21 18:02:17 +00001010 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001011 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001012 continue;
1013 }
1014
1015 if (LeftV.isUnknown()) {
1016 St = SetRVal(St, B, RightV);
1017 break;
1018 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001019
Ted Kremenek07baa252008-02-21 18:02:17 +00001020 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001021 break;
1022 }
1023
Ted Kremenek07baa252008-02-21 18:02:17 +00001024 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001025
Ted Kremenek07baa252008-02-21 18:02:17 +00001026 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001027
Ted Kremenek07baa252008-02-21 18:02:17 +00001028 assert (B->isCompoundAssignmentOp());
1029
1030 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001031 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001032 continue;
1033 }
1034
1035 if (LeftV.isUnknown()) {
1036
1037 // While we do not know the location to store RightV,
1038 // the entire expression does evaluate to RightV.
1039
1040 if (RightV.isUnknown()) {
1041 Dst.Add(N2);
1042 continue;
1043 }
1044
1045 St = SetRVal(St, B, RightV);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001046 break;
1047 }
1048
Ted Kremenek07baa252008-02-21 18:02:17 +00001049 // At this pointer we know that the LHS evaluates to an LVal
1050 // that is neither "Unknown" or "Unintialized."
1051
1052 LVal LeftLV = cast<LVal>(LeftV);
1053
1054 // Propagate uninitialized values (right-side).
1055
1056 if (RightV.isUninit()) {
1057 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001058 break;
1059 }
1060
Ted Kremenek07baa252008-02-21 18:02:17 +00001061 // Fetch the value of the LHS (the value of the variable, etc.).
1062
1063 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1064
1065 // Propagate uninitialized value (left-side).
1066
1067 if (V.isUninit()) {
1068 St = SetRVal(St, B, V);
1069 break;
1070 }
1071
1072 // Propagate unknown values.
1073
Ted Kremenekb8782e12008-02-21 19:15:37 +00001074 if (V.isUnknown()) {
1075 Dst.Add(N2);
1076 continue;
1077 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001078
1079 if (RightV.isUnknown()) {
1080 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1081 break;
1082 }
1083
1084 // Neither the LHS or the RHS have Unknown/Uninit values. Process
1085 // the operation and store the result.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001086
Ted Kremenek106f37c2008-02-08 07:05:39 +00001087 if (Op >= BinaryOperator::AndAssign)
1088 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1089 else
Ted Kremenek22640ce2008-02-15 22:09:30 +00001090 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenek15cb0782008-02-06 22:50:25 +00001091
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001092 // Get the computation type.
1093 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1094
1095 // Perform promotions.
1096 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001097 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001098
1099 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001100
Ted Kremenek2c369792008-02-25 18:42:54 +00001101 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1102 && RHS->getType()->isIntegerType()) {
1103
Ted Kremenekef0007f2008-02-26 02:15:56 +00001104 // Check if the denominator is uninitialized.
1105
1106 if (RightV.isUninit()) {
1107 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
1108
1109 if (DivUninit) {
1110 DivUninit->markAsSink();
1111 BadDivides.insert(DivUninit);
1112 }
1113
1114 continue;
1115 }
1116
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001117 // First, "assume" that the denominator is 0.
1118
1119 bool isFeasible = false;
1120 StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
1121
1122 if (isFeasible) {
1123 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1124
1125 if (DivZeroNode) {
1126 DivZeroNode->markAsSink();
Ted Kremenekef0007f2008-02-26 02:15:56 +00001127 BadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001128 }
1129 }
1130
1131 // Second, "assume" that the denominator cannot be 0.
1132
1133 isFeasible = false;
1134 St = Assume(St, RightV, true, isFeasible);
1135
1136 if (!isFeasible)
1137 continue;
1138
1139 // Fall-through. The logic below processes the divide.
1140 }
1141
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001142 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek07baa252008-02-21 18:02:17 +00001143 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001144 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001145 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001146
1147 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001148 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001149 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001150}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001151
Ted Kremenek07baa252008-02-21 18:02:17 +00001152void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001153 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1154 N->markAsSink();
1155 UninitStores.insert(N);
1156}
Ted Kremenekbe962452008-01-16 19:42:59 +00001157
Ted Kremenekbf988d02008-02-19 00:22:37 +00001158void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001159
1160 // FIXME: add metadata to the CFG so that we can disable
1161 // this check when we KNOW that there is no block-level subexpression.
1162 // The motivation is that this check requires a hashtable lookup.
1163
1164 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1165 Dst.Add(Pred);
1166 return;
1167 }
1168
1169 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001170
1171 default:
1172 // Cases we intentionally have "default" handle:
Ted Kremenek18044ff2008-02-19 02:01:16 +00001173 // AddrLabelExpr
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001174
1175 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1176 break;
1177
Ted Kremenek744a7862008-02-08 20:29:23 +00001178 case Stmt::BinaryOperatorClass: {
1179 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001180
Ted Kremenek744a7862008-02-08 20:29:23 +00001181 if (B->isLogicalOp()) {
1182 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001183 break;
1184 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001185 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +00001186 StateTy St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001187 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001188 break;
1189 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001190
1191 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1192 break;
1193 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001194
1195 case Stmt::CallExprClass: {
1196 CallExpr* C = cast<CallExpr>(S);
1197 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1198 break;
1199 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001200
1201 case Stmt::CastExprClass: {
1202 CastExpr* C = cast<CastExpr>(S);
1203 VisitCast(C, C->getSubExpr(), Pred, Dst);
1204 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001205 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001206
Ted Kremenek18044ff2008-02-19 02:01:16 +00001207 // While explicitly creating a node+state for visiting a CharacterLiteral
1208 // seems wasteful, it also solves a bunch of problems when handling
1209 // the ?, &&, and ||.
1210
1211 case Stmt::CharacterLiteralClass: {
1212 CharacterLiteral* C = cast<CharacterLiteral>(S);
1213 StateTy St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001214 NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(),
Ted Kremenek18044ff2008-02-19 02:01:16 +00001215 C->getLoc());
Ted Kremenek07baa252008-02-21 18:02:17 +00001216 Nodify(Dst, C, Pred, SetRVal(St, C, X));
Ted Kremenek18044ff2008-02-19 02:01:16 +00001217 break;
1218 }
1219
Ted Kremenek07baa252008-02-21 18:02:17 +00001220 // FIXME: ChooseExpr is really a constant. We need to fix
1221 // the CFG do not model them as explicit control-flow.
1222
Ted Kremenekfd85f292008-02-12 19:49:57 +00001223 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1224 ChooseExpr* C = cast<ChooseExpr>(S);
1225 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1226 break;
1227 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001228
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001229 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001230 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1231 break;
1232
Ted Kremenekfd85f292008-02-12 19:49:57 +00001233 case Stmt::ConditionalOperatorClass: { // '?' operator
1234 ConditionalOperator* C = cast<ConditionalOperator>(S);
1235 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1236 break;
1237 }
1238
1239 case Stmt::DeclRefExprClass:
1240 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1241 break;
1242
1243 case Stmt::DeclStmtClass:
1244 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1245 break;
1246
Ted Kremenek18044ff2008-02-19 02:01:16 +00001247 // While explicitly creating a node+state for visiting an IntegerLiteral
1248 // seems wasteful, it also solves a bunch of problems when handling
1249 // the ?, &&, and ||.
1250
1251 case Stmt::IntegerLiteralClass: {
1252 StateTy St = Pred->getState();
1253 IntegerLiteral* I = cast<IntegerLiteral>(S);
Ted Kremenek07baa252008-02-21 18:02:17 +00001254 NonLVal X = NonLVal::MakeVal(ValMgr, I);
1255 Nodify(Dst, I, Pred, SetRVal(St, I, X));
Ted Kremenek18044ff2008-02-19 02:01:16 +00001256 break;
1257 }
1258
Ted Kremenekfd85f292008-02-12 19:49:57 +00001259 case Stmt::ImplicitCastExprClass: {
1260 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1261 VisitCast(C, C->getSubExpr(), Pred, Dst);
1262 break;
1263 }
1264
1265 case Stmt::ParenExprClass:
1266 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1267 break;
1268
1269 case Stmt::SizeOfAlignOfTypeExprClass:
1270 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1271 break;
1272
Ted Kremenek106f37c2008-02-08 07:05:39 +00001273 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001274 StmtExpr* SE = cast<StmtExpr>(S);
1275
Ted Kremenek106f37c2008-02-08 07:05:39 +00001276 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +00001277 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001278 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001279 break;
1280 }
1281
Ted Kremenek07baa252008-02-21 18:02:17 +00001282 // FIXME: We may wish to always bind state to ReturnStmts so
1283 // that users can quickly query what was the state at the
1284 // exit points of a function.
1285
Ted Kremenekfd85f292008-02-12 19:49:57 +00001286 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001287 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1288 Visit(R, Pred, Dst);
1289 else
1290 Dst.Add(Pred);
1291
1292 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001293 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001294
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001295 case Stmt::UnaryOperatorClass: {
1296 UnaryOperator* U = cast<UnaryOperator>(S);
1297
Ted Kremenek07baa252008-02-21 18:02:17 +00001298 switch (U->getOpcode()) {
1299 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1300 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1301 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1302 default: VisitUnaryOperator(U, Pred, Dst); break;
1303 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001304
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001305 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001306 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001307 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001308}
1309
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001310//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001311// "Assume" logic.
1312//===----------------------------------------------------------------------===//
1313
Ted Kremenek07baa252008-02-21 18:02:17 +00001314GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001315 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001316 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001317 switch (Cond.getSubKind()) {
1318 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001319 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001320 return St;
1321
Ted Kremenek13f31562008-02-06 00:54:14 +00001322 case lval::SymbolValKind:
1323 if (Assumption)
1324 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1325 ValMgr.getZeroWithPtrWidth(), isFeasible);
1326 else
1327 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1328 ValMgr.getZeroWithPtrWidth(), isFeasible);
1329
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001330
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001331 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001332 case lval::FuncValKind:
1333 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001334 isFeasible = Assumption;
1335 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001336
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001337 case lval::ConcreteIntKind: {
1338 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001339 isFeasible = b ? Assumption : !Assumption;
1340 return St;
1341 }
1342 }
Ted Kremenek90960972008-01-30 23:03:39 +00001343}
1344
Ted Kremenek07baa252008-02-21 18:02:17 +00001345GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001346 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001347 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001348 switch (Cond.getSubKind()) {
1349 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001350 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001351 return St;
1352
Ted Kremenekab359c12008-02-06 17:32:17 +00001353
1354 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001355 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001356 SymbolID sym = SV.getSymbol();
1357
1358 if (Assumption)
1359 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1360 isFeasible);
1361 else
1362 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1363 isFeasible);
1364 }
1365
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001366 case nonlval::SymIntConstraintValKind:
1367 return
1368 AssumeSymInt(St, Assumption,
1369 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1370 isFeasible);
1371
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001372 case nonlval::ConcreteIntKind: {
1373 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001374 isFeasible = b ? Assumption : !Assumption;
1375 return St;
1376 }
1377 }
1378}
1379
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001380GRExprEngine::StateTy
1381GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001382 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001383
Ted Kremenek13f31562008-02-06 00:54:14 +00001384 // First, determine if sym == X, where X != V.
1385 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1386 isFeasible = *X != V;
1387 return St;
1388 }
1389
1390 // Second, determine if sym != V.
1391 if (St.isNotEqual(sym, V)) {
1392 isFeasible = true;
1393 return St;
1394 }
1395
1396 // If we reach here, sym is not a constant and we don't know if it is != V.
1397 // Make that assumption.
1398
1399 isFeasible = true;
1400 return StateMgr.AddNE(St, sym, V);
1401}
1402
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001403GRExprEngine::StateTy
1404GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001405 const llvm::APSInt& V, bool& isFeasible) {
1406
1407 // First, determine if sym == X, where X != V.
1408 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1409 isFeasible = *X == V;
1410 return St;
1411 }
1412
1413 // Second, determine if sym != V.
1414 if (St.isNotEqual(sym, V)) {
1415 isFeasible = false;
1416 return St;
1417 }
1418
1419 // If we reach here, sym is not a constant and we don't know if it is == V.
1420 // Make that assumption.
1421
1422 isFeasible = true;
1423 return StateMgr.AddEQ(St, sym, V);
1424}
Ted Kremenek90960972008-01-30 23:03:39 +00001425
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001426GRExprEngine::StateTy
1427GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001428 const SymIntConstraint& C, bool& isFeasible) {
1429
1430 switch (C.getOpcode()) {
1431 default:
1432 // No logic yet for other operators.
1433 return St;
1434
1435 case BinaryOperator::EQ:
1436 if (Assumption)
1437 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1438 else
1439 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1440
1441 case BinaryOperator::NE:
1442 if (Assumption)
1443 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1444 else
1445 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1446 }
1447}
1448
Ted Kremenek90960972008-01-30 23:03:39 +00001449//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001450// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001451//===----------------------------------------------------------------------===//
1452
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001453#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001454static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001455
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001456namespace llvm {
1457template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001458struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001459 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001460
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001461 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001462
1463 Out << "Variables:\\l";
1464
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001465 bool isFirst = true;
1466
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001467 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +00001468 E=St.vb_end(); I!=E;++I) {
1469
1470 if (isFirst)
1471 isFirst = false;
1472 else
1473 Out << "\\l";
1474
1475 Out << ' ' << I.getKey()->getName() << " : ";
1476 I.getData().print(Out);
1477 }
1478
1479 }
1480
Ted Kremenek17c5f112008-02-11 19:21:59 +00001481
Ted Kremenek6d409922008-02-13 18:06:44 +00001482 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001483
1484 bool isFirst = true;
1485
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001486 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001487 I != E;++I) {
1488
1489 if (isFirst) {
1490 Out << "\\l\\lSub-Expressions:\\l";
1491 isFirst = false;
1492 }
1493 else
1494 Out << "\\l";
1495
1496 Out << " (" << (void*) I.getKey() << ") ";
1497 I.getKey()->printPretty(Out);
1498 Out << " : ";
1499 I.getData().print(Out);
1500 }
1501 }
1502
Ted Kremenek6d409922008-02-13 18:06:44 +00001503 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001504
Ted Kremenek08cfd832008-02-08 21:10:02 +00001505 bool isFirst = true;
1506
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001507 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001508 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001509 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001510 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001511 isFirst = false;
1512 }
1513 else
1514 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001515
Ted Kremenek17c5f112008-02-11 19:21:59 +00001516 Out << " (" << (void*) I.getKey() << ") ";
1517 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001518 Out << " : ";
1519 I.getData().print(Out);
1520 }
1521 }
1522
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001523 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001524 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001525
1526 if (CE.isEmpty())
1527 return;
1528
1529 Out << "\\l\\|'==' constraints:";
1530
Ted Kremenek07baa252008-02-21 18:02:17 +00001531 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001532 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1533 }
1534
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001535 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001536 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001537
1538 if (NE.isEmpty())
1539 return;
1540
1541 Out << "\\l\\|'!=' constraints:";
1542
Ted Kremenek07baa252008-02-21 18:02:17 +00001543 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001544 I != EI; ++I){
1545
1546 Out << "\\l $" << I.getKey() << " : ";
1547 bool isFirst = true;
1548
1549 ValueState::IntSetTy::iterator J=I.getData().begin(),
1550 EJ=I.getData().end();
1551 for ( ; J != EJ; ++J) {
1552 if (isFirst) isFirst = false;
1553 else Out << ", ";
1554
1555 Out << (*J)->toString();
1556 }
1557 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001558 }
1559
1560 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1561
1562 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001563 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001564 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001565 GraphPrintCheckerState->isUninitStore(N) ||
1566 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001567 return "color=\"red\",style=\"filled\"";
1568
1569 return "";
1570 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001571
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001572 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001573 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001574
1575 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001576 ProgramPoint Loc = N->getLocation();
1577
1578 switch (Loc.getKind()) {
1579 case ProgramPoint::BlockEntranceKind:
1580 Out << "Block Entrance: B"
1581 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1582 break;
1583
1584 case ProgramPoint::BlockExitKind:
1585 assert (false);
1586 break;
1587
1588 case ProgramPoint::PostStmtKind: {
1589 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001590 Out << L.getStmt()->getStmtClassName() << ':'
1591 << (void*) L.getStmt() << ' ';
1592
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001593 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001594
1595 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1596 Out << "\\|Implicit-Null Dereference.\\l";
1597 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001598 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1599 Out << "\\|Explicit-Null Dereference.\\l";
1600 }
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001601 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1602 Out << "\\|Dereference of uninitialied value.\\l";
1603 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001604 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001605 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenekbf988d02008-02-19 00:22:37 +00001606 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001607
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001608 break;
1609 }
1610
1611 default: {
1612 const BlockEdge& E = cast<BlockEdge>(Loc);
1613 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1614 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001615
1616 if (Stmt* T = E.getSrc()->getTerminator()) {
1617 Out << "\\|Terminator: ";
1618 E.getSrc()->printTerminator(Out);
1619
Ted Kremenekaee121c2008-02-13 23:08:21 +00001620 if (isa<SwitchStmt>(T)) {
1621 Stmt* Label = E.getDst()->getLabel();
1622
1623 if (Label) {
1624 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1625 Out << "\\lcase ";
1626 C->getLHS()->printPretty(Out);
1627
1628 if (Stmt* RHS = C->getRHS()) {
1629 Out << " .. ";
1630 RHS->printPretty(Out);
1631 }
1632
1633 Out << ":";
1634 }
1635 else {
1636 assert (isa<DefaultStmt>(Label));
1637 Out << "\\ldefault:";
1638 }
1639 }
1640 else
1641 Out << "\\l(implicit) default:";
1642 }
1643 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001644 // FIXME
1645 }
1646 else {
1647 Out << "\\lCondition: ";
1648 if (*E.getSrc()->succ_begin() == E.getDst())
1649 Out << "true";
1650 else
1651 Out << "false";
1652 }
1653
1654 Out << "\\l";
1655 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001656
1657 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1658 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1659 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001660 }
1661 }
1662
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001663 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001664
Ted Kremenek17c5f112008-02-11 19:21:59 +00001665 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001666
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001667 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001668 return Out.str();
1669 }
1670};
1671} // end llvm namespace
1672#endif
1673
Ted Kremenek3862eb12008-02-14 22:36:46 +00001674void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001675#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001676 GraphPrintCheckerState = this;
1677 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001678 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001679#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001680}