blob: 80de395c585dd25e61ade3bfde64df3dabd57ca3 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-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 Kremenek77349cb2008-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 Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000017#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
18
19#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000020
21using namespace clang;
22using llvm::dyn_cast;
23using llvm::cast;
24using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000025
Ted Kremenek4d4dd852008-02-13 17:41:41 +000026GRExprEngine::StateTy
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000027GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000028
Ted Kremeneke070a1d2008-02-04 21:59:01 +000029 if (!StateCleaned) {
30 St = RemoveDeadBindings(CurrentStmt, St);
31 StateCleaned = true;
32 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000033
Ted Kremeneke070a1d2008-02-04 21:59:01 +000034 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000035
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000036 if (Ex == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000038
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000042
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000043 return StateMgr.SetRVal(St, Ex, isBlkExpr, V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000044}
45
Ted Kremenek4d4dd852008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000047GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
Ted Kremenekaa1c4e52008-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 Kremenekcba2e432008-02-05 19:35:18 +000054
55 return RetBuf;
56}
57
Ted Kremenek4d4dd852008-02-13 17:41:41 +000058GRExprEngine::StateTy
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000059GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000060
61 if (!StateCleaned) {
62 St = RemoveDeadBindings(CurrentStmt, St);
63 StateCleaned = true;
64 }
65
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000066 return StateMgr.SetRVal(St, LV, RV);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000067}
68
Ted Kremenek4d4dd852008-02-13 17:41:41 +000069void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000070 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +000071
Ted Kremeneke7d22112008-02-11 19:21:59 +000072 // Remove old bindings for subexpressions.
73 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +000074
Ted Kremenekb2331832008-02-15 22:29:00 +000075 // Check for NULL conditions; e.g. "for(;;)"
76 if (!Condition) {
77 builder.markInfeasible(false);
78
79 // Get the current block counter.
80 GRBlockCounter BC = builder.getBlockCounter();
81 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
82 unsigned NumVisited = BC.getNumVisited(BlockID);
83
84 if (NumVisited < 1) builder.generateNode(PrevState, true);
85 else builder.markInfeasible(true);
86
87 return;
88 }
89
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000090 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +000091
92 switch (V.getBaseKind()) {
93 default:
94 break;
95
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000096 case RVal::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +000097 builder.generateNode(PrevState, true);
98 builder.generateNode(PrevState, false);
99 return;
100
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000101 case RVal::UninitializedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000102 NodeTy* N = builder.generateNode(PrevState, true);
103
104 if (N) {
105 N->markAsSink();
106 UninitBranches.insert(N);
107 }
108
109 builder.markInfeasible(false);
110 return;
111 }
112 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000113
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000114 // Get the current block counter.
115 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000116 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
117 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000118
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000119 if (isa<nonlval::ConcreteInt>(V) ||
120 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
121
122 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000123
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000124 bool isFeasible = true;
125
126 StateTy St = Assume(PrevState, V, true, isFeasible);
127
128 if (isFeasible)
129 builder.generateNode(St, true);
130 else
131 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000132 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000133 else
134 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000135
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000136 BlockID = builder.getTargetBlock(false)->getBlockID();
137 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000138
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000139 if (isa<nonlval::ConcreteInt>(V) ||
140 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
141
142 // Process the false branch.
143
144 bool isFeasible = false;
145
146 StateTy St = Assume(PrevState, V, false, isFeasible);
147
148 if (isFeasible)
149 builder.generateNode(St, false);
150 else
151 builder.markInfeasible(false);
152 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000153 else
154 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000155}
156
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000157/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000158/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000159void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000160
161 StateTy St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000162 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000163
164 // Three possibilities:
165 //
166 // (1) We know the computed label.
167 // (2) The label is NULL (or some other constant), or Uninitialized.
168 // (3) We have no clue about the label. Dispatch to all targets.
169 //
170
171 typedef IndirectGotoNodeBuilder::iterator iterator;
172
173 if (isa<lval::GotoLabel>(V)) {
174 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
175
176 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000177 if (I.getLabel() == L) {
178 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000179 return;
180 }
181 }
182
183 assert (false && "No block with label.");
184 return;
185 }
186
187 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
188 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000189 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000190 UninitBranches.insert(N);
191 return;
192 }
193
194 // This is really a catch-all. We don't support symbolics yet.
195
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000196 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000197
198 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000199 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000200}
Ted Kremenekf233d482008-02-05 00:26:40 +0000201
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000202/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
203/// nodes by processing the 'effects' of a switch statement.
204void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
205
206 typedef SwitchNodeBuilder::iterator iterator;
207
208 StateTy St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000209 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000210 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000211
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000212 if (CondV.isUninit()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000213 NodeTy* N = builder.generateDefaultCaseNode(St, true);
214 UninitBranches.insert(N);
215 return;
216 }
217
218 StateTy DefaultSt = St;
219
220 // While most of this can be assumed (such as the signedness), having it
221 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000222
223 unsigned bits = getContext().getTypeSize(CondE->getType(),
224 CondE->getExprLoc());
225
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000226 APSInt V1(bits, false);
227 APSInt V2 = V1;
228
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000229 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000230
231 CaseStmt* Case = cast<CaseStmt>(I.getCase());
232
233 // Evaluate the case.
234 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
235 assert (false && "Case condition must evaluate to an integer constant.");
236 return;
237 }
238
239 // Get the RHS of the case, if it exists.
240
241 if (Expr* E = Case->getRHS()) {
242 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
243 assert (false &&
244 "Case condition (RHS) must evaluate to an integer constant.");
245 return ;
246 }
247
248 assert (V1 <= V2);
249 }
250 else V2 = V1;
251
252 // FIXME: Eventually we should replace the logic below with a range
253 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000254 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000255
256 do {
257 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
258
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000259 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000260
261 // Now "assume" that the case matches.
Ted Kremenek692416c2008-02-18 22:57:02 +0000262 bool isFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000263
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000264 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000265
266 if (isFeasible) {
267 builder.generateCaseStmtNode(I, StNew);
268
269 // If CondV evaluates to a constant, then we know that this
270 // is the *only* case that we can take, so stop evaluating the
271 // others.
272 if (isa<nonlval::ConcreteInt>(CondV))
273 return;
274 }
275
276 // Now "assume" that the case doesn't match. Add this state
277 // to the default state (if it is feasible).
278
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000279 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000280
281 if (isFeasible)
282 DefaultSt = StNew;
283
284 // Concretize the next value in the range.
285 ++V1;
286
287 } while (V1 < V2);
288 }
289
290 // If we reach here, than we know that the default branch is
291 // possible.
292 builder.generateDefaultCaseNode(DefaultSt);
293}
294
295
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000296void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000297 NodeSet& Dst) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000298
299 bool hasR2;
300 StateTy PrevState = Pred->getState();
301
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000302 RVal R1 = GetRVal(PrevState, B->getLHS());
303 RVal R2 = GetRVal(PrevState, B->getRHS(), hasR2);
Ted Kremenek9dca0622008-02-19 00:22:37 +0000304
305 if (hasR2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000306 if (R2.isUnknownOrUninit()) {
307 Nodify(Dst, B, Pred, SetRVal(PrevState, B, R2));
Ted Kremenek9dca0622008-02-19 00:22:37 +0000308 return;
309 }
310 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000311 else if (R1.isUnknownOrUninit()) {
312 Nodify(Dst, B, Pred, SetRVal(PrevState, B, R1));
Ted Kremenekf233d482008-02-05 00:26:40 +0000313 return;
314 }
315
316 // R1 is an expression that can evaluate to either 'true' or 'false'.
317 if (B->getOpcode() == BinaryOperator::LAnd) {
318 // hasR2 == 'false' means that LHS evaluated to 'false' and that
319 // we short-circuited, leading to a value of '0' for the '&&' expression.
320 if (hasR2 == false) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000321 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000322 return;
323 }
324 }
325 else {
326 assert (B->getOpcode() == BinaryOperator::LOr);
327 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
328 // we short-circuited, leading to a value of '1' for the '||' expression.
329 if (hasR2 == false) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000330 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000331 return;
332 }
333 }
334
335 // If we reach here we did not short-circuit. Assume R2 == true and
336 // R2 == false.
337
338 bool isFeasible;
339 StateTy St = Assume(PrevState, R2, true, isFeasible);
340
341 if (isFeasible)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000342 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000343
344 St = Assume(PrevState, R2, false, isFeasible);
345
346 if (isFeasible)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000347 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000348}
349
350
351
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000352void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000353
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000354 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000355 StmtEntryNode = builder.getLastNode();
356 CurrentStmt = S;
357 NodeSet Dst;
358 StateCleaned = false;
359
360 Visit(S, StmtEntryNode, Dst);
361
362 // If no nodes were generated, generate a new node that has all the
363 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000364
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000365 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
366 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
367 builder.generateNode(S, St, StmtEntryNode);
368 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000369
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000370 // For safety, NULL out these variables.
371
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000372 CurrentStmt = NULL;
373 StmtEntryNode = NULL;
374 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000375}
376
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000377GRExprEngine::NodeTy*
378GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000379
380 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000381 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000382 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000383
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000384 NodeTy* N = Builder->generateNode(S, St, Pred);
385 Dst.Add(N);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000386
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000387 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000388}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000389
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000390void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000391 const StateTy::BufferTy& SB) {
392
393 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
394 Nodify(Dst, S, Pred, *I);
395}
396
Ted Kremenek44842c22008-02-13 18:06:44 +0000397void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000398
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000399 if (D != CurrentStmt) {
400 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
401 return;
402 }
403
404 // If we are here, we are loading the value of the decl and binding
405 // it to the block-level expression.
406
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000407 StateTy St = Pred->getState();
408 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000409}
410
Ted Kremenekde434242008-02-19 01:44:53 +0000411void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000412 CallExpr::arg_iterator AI,
413 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000414 NodeSet& Dst) {
415
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000416 // Process the arguments.
417
418 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000419
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000420 NodeSet DstTmp;
421 Visit(*AI, Pred, DstTmp);
422 ++AI;
423
424 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
425 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000426
427 return;
428 }
429
430 // If we reach here we have processed all of the arguments. Evaluate
431 // the callee expression.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000432 NodeSet DstTmp;
Ted Kremenekde434242008-02-19 01:44:53 +0000433 Visit(CE->getCallee(), Pred, DstTmp);
434
435 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000436 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
437
Ted Kremenekde434242008-02-19 01:44:53 +0000438 StateTy St = (*DI)->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000439 RVal L = GetLVal(St, CE->getCallee());
Ted Kremenekde434242008-02-19 01:44:53 +0000440
441 // Check for uninitialized control-flow.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000442
443 if (L.isUninit()) {
444
Ted Kremenekde434242008-02-19 01:44:53 +0000445 NodeTy* N = Builder->generateNode(CE, St, *DI);
446 N->markAsSink();
447 UninitBranches.insert(N);
448 continue;
449 }
450
Ted Kremenek03da0d72008-02-21 19:46:04 +0000451 if (L.isUnknown()) {
452 // Invalidate all arguments passed in by reference (LVals).
453 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
454 I != E; ++I) {
455 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000456
Ted Kremenek03da0d72008-02-21 19:46:04 +0000457 if (isa<LVal>(V))
458 St = SetRVal(St, cast<LVal>(V), UnknownVal());
459 }
460 }
461 else
462 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
463
464 Nodify(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +0000465 }
466}
467
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000468void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000469
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000470 NodeSet S1;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000471 Visit(Ex, Pred, S1);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000472
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000473 QualType T = CastE->getType();
474
Ted Kremenek402563b2008-02-19 18:47:04 +0000475 // Check for redundant casts or casting to "void"
476 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000477 Ex->getType() == T ||
478 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000479
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000480 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000481 Dst.Add(*I1);
482
Ted Kremenek874d63f2008-01-24 02:02:54 +0000483 return;
484 }
485
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000486 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000487 NodeTy* N = *I1;
488 StateTy St = N->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000489 RVal V = GetRVal(St, Ex);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000490 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000491 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000492}
493
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000494void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000495 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000496
497 StateTy St = Pred->getState();
498
499 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000500 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000501
502 // FIXME: Add support for local arrays.
503 if (VD->getType()->isArrayType())
504 continue;
505
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000506 const Expr* Ex = VD->getInit();
507
508 St = SetRVal(St, lval::DeclVal(VD),
509 Ex ? GetRVal(St, Ex) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000510 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000511
512 Nodify(Dst, DS, Pred, St);
513
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000514 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000515}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000516
Ted Kremenekf233d482008-02-05 00:26:40 +0000517
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000518void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000519 NodeTy* Pred, NodeSet& Dst) {
520
521 StateTy St = Pred->getState();
522
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000523 RVal V = GetRVal(St, L);
524 if (isa<UnknownVal>(V)) V = GetRVal(St, R);
Ted Kremenekf233d482008-02-05 00:26:40 +0000525
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000526 Nodify(Dst, Ex, Pred, SetRVal(St, Ex, V));
Ted Kremenekf233d482008-02-05 00:26:40 +0000527}
528
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000529/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000530void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
531 NodeTy* Pred,
532 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000533
534 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000535
536 // 6.5.3.4 sizeof: "The result type is an integer."
537
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000538 QualType T = Ex->getArgumentType();
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000539
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000540
Ted Kremenek297d0d72008-02-21 18:15:29 +0000541 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000542 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000543 return;
544
Ted Kremenek297d0d72008-02-21 18:15:29 +0000545
546 uint64_t size = 1; // Handle sizeof(void)
547
548 if (T != getContext().VoidTy) {
549 SourceLocation Loc = Ex->getExprLoc();
550 size = getContext().getTypeSize(T, Loc) / 8;
551 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000552
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000553 Nodify(Dst, Ex, Pred,
554 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000555 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000556
557}
558
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000559void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
560
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000561 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000562
563 NodeSet DstTmp;
564
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000565 if (!isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000566 DstTmp.Add(Pred);
567 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000568 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000569
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000570 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000571
572 NodeTy* N = *I;
573 StateTy St = N->getState();
574
575 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
576
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000577 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000578
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000579 // Check for dereferences of uninitialized values.
580
581 if (V.isUninit()) {
582
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000583 NodeTy* Succ = Builder->generateNode(U, St, N);
584
585 if (Succ) {
586 Succ->markAsSink();
587 UninitDeref.insert(Succ);
588 }
589
590 continue;
591 }
592
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000593 // Check for dereferences of unknown values. Treat as No-Ops.
594
595 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000596 Dst.Add(N);
597 continue;
598 }
599
600 // After a dereference, one of two possible situations arise:
601 // (1) A crash, because the pointer was NULL.
602 // (2) The pointer is not NULL, and the dereference works.
603 //
604 // We add these assumptions.
605
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000606 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000607 bool isFeasibleNotNull;
608
609 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000610
611 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000612
613 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000614
615 // FIXME: Currently symbolic analysis "generates" new symbols
616 // for the contents of values. We need a better approach.
617
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000618 Nodify(Dst, U, N, SetRVal(StNotNull, U,
619 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000620 }
621
622 bool isFeasibleNull;
623
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000624 // Now "assume" that the pointer is NULL.
625
626 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000627
628 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000629
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000630 // We don't use "Nodify" here because the node will be a sink
631 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000632
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000633 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
634
635 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000636
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000637 NullNode->markAsSink();
638
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000639 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
640 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000641 }
642 }
643 }
644}
645
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000646void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
647 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000648
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000649 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000650
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000651 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000652 assert (U->getOpcode() != UnaryOperator::SizeOf);
653 assert (U->getOpcode() != UnaryOperator::AlignOf);
654
655 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000656
657 switch (U->getOpcode()) {
658 case UnaryOperator::PostInc:
659 case UnaryOperator::PostDec:
660 case UnaryOperator::PreInc:
661 case UnaryOperator::PreDec:
662 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000663 // Evalue subexpression as an LVal.
664 use_GetLVal = true;
665 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000666 break;
667
668 default:
669 Visit(U->getSubExpr(), Pred, S1);
670 break;
671 }
672
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000673 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000674
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000675 NodeTy* N1 = *I1;
676 StateTy St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000677
678 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
679 GetRVal(St, U->getSubExpr());
680
681 if (SubV.isUnknown()) {
682 Dst.Add(N1);
683 continue;
684 }
685
686 if (SubV.isUninit()) {
687 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
688 continue;
689 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000690
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000691 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000692
693 // Handle ++ and -- (both pre- and post-increment).
694
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000695 LVal SubLV = cast<LVal>(SubV);
696 RVal V = GetRVal(St, SubLV, U->getType());
697
Ted Kremenek89063af2008-02-21 19:15:37 +0000698 if (V.isUnknown()) {
699 Dst.Add(N1);
700 continue;
701 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000702
703 // Propagate uninitialized values.
704 if (V.isUninit()) {
705 Nodify(Dst, U, N1, SetRVal(St, U, V));
706 continue;
707 }
708
Ted Kremenek443003b2008-02-21 19:29:23 +0000709 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000710
711 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
712 : BinaryOperator::Sub;
713
Ted Kremenek443003b2008-02-21 19:29:23 +0000714 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000715
716 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000717 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000718 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000719 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000720
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000721 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000722 continue;
723 }
724
725 // Handle all other unary operators.
726
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000727 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000728
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000729 case UnaryOperator::Minus:
730 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000731 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000732
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000733 case UnaryOperator::Not:
734 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000735 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000736
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000737 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000738
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000739 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
740 //
741 // Note: technically we do "E == 0", but this is the same in the
742 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000743
744 if (isa<LVal>(SubV)) {
745 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
746 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
747 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000748 }
749 else {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000750 nonlval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
751 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
752 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000753 }
754
755 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000756
Ted Kremenek64924852008-01-31 02:35:41 +0000757 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000758 assert (isa<LVal>(SubV));
759 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000760 break;
761 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000762
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000763 default: ;
764 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000765 }
766
767 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000768 }
769}
770
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000771void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
772 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000773
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000774 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000775
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000776 // FIXME: Add support for VLAs.
777 if (!T.getTypePtr()->isConstantSizeType())
778 return;
779
780 SourceLocation Loc = U->getExprLoc();
781 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
782 StateTy St = Pred->getState();
783 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
784
785 Nodify(Dst, U, Pred, St);
786}
787
788void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
789
790 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
791
792 Ex = Ex->IgnoreParens();
793
794 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000795 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000796 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000797 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000798
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000799 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000800 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000801 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000802
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000803 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000804 Dst.Add(Pred);
805 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000806 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000807
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000808 return;
809 }
810 }
811
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000812 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000813}
814
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000815void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000816 GRExprEngine::NodeTy* Pred,
817 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000819
820 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000821 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000822 else
823 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000824
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000825 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000826
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000827 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000828
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000829 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000830 // In such cases, we want to (initially) treat the LHS as an LVal,
831 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
832 // evaluated to LValDecl's instead of to an NonLVal.
833
834 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
835 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000836
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000837 // Visit the RHS...
838
839 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000840 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000841
842 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000843
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000844 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000845
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000846 NodeTy* N2 = *I2;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000847 StateTy St = N2->getState();
848 RVal RightV = GetRVal(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000849
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000850 BinaryOperator::Opcode Op = B->getOpcode();
851
852 if (Op <= BinaryOperator::Or) {
853
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000854 // Process non-assignements except commas or short-circuited
855 // logical expressions (LAnd and LOr).
856
857 RVal Result = EvalBinOp(Op, LeftV, RightV);
858
859 if (Result.isUnknown()) {
860 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000861 continue;
862 }
863
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000864 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000865 continue;
866 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000867
868 // Process assignments.
869
870 switch (Op) {
871
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000872 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000873
874 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +0000875
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000876 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000877 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000878 continue;
879 }
880
881 if (LeftV.isUnknown()) {
882 St = SetRVal(St, B, RightV);
883 break;
884 }
Ted Kremenek9dca0622008-02-19 00:22:37 +0000885
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000886 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000887 break;
888 }
889
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000890 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000891
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000892 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000893
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000894 assert (B->isCompoundAssignmentOp());
895
896 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000897 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000898 continue;
899 }
900
901 if (LeftV.isUnknown()) {
902
903 // While we do not know the location to store RightV,
904 // the entire expression does evaluate to RightV.
905
906 if (RightV.isUnknown()) {
907 Dst.Add(N2);
908 continue;
909 }
910
911 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +0000912 break;
913 }
914
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000915 // At this pointer we know that the LHS evaluates to an LVal
916 // that is neither "Unknown" or "Unintialized."
917
918 LVal LeftLV = cast<LVal>(LeftV);
919
920 // Propagate uninitialized values (right-side).
921
922 if (RightV.isUninit()) {
923 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
Ted Kremenekb5339122008-02-19 20:53:06 +0000924 break;
925 }
926
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000927 // Fetch the value of the LHS (the value of the variable, etc.).
928
929 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
930
931 // Propagate uninitialized value (left-side).
932
933 if (V.isUninit()) {
934 St = SetRVal(St, B, V);
935 break;
936 }
937
938 // Propagate unknown values.
939
Ted Kremenek89063af2008-02-21 19:15:37 +0000940 if (V.isUnknown()) {
941 Dst.Add(N2);
942 continue;
943 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000944
945 if (RightV.isUnknown()) {
946 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
947 break;
948 }
949
950 // Neither the LHS or the RHS have Unknown/Uninit values. Process
951 // the operation and store the result.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000952
Ted Kremenekda9bd092008-02-08 07:05:39 +0000953 if (Op >= BinaryOperator::AndAssign)
954 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
955 else
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000956 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000957
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000958 // Get the computation type.
959 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
960
961 // Perform promotions.
962 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +0000963 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000964
965 // Evaluate operands and promote to result type.
966 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
967
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000968 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000969 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000970 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000971
972 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000973 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000974 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000975}
Ted Kremenekee985462008-01-16 18:18:48 +0000976
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000977void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000978 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
979 N->markAsSink();
980 UninitStores.insert(N);
981}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000982
Ted Kremenek9dca0622008-02-19 00:22:37 +0000983void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000984
985 // FIXME: add metadata to the CFG so that we can disable
986 // this check when we KNOW that there is no block-level subexpression.
987 // The motivation is that this check requires a hashtable lookup.
988
989 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
990 Dst.Add(Pred);
991 return;
992 }
993
994 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000995
996 default:
997 // Cases we intentionally have "default" handle:
Ted Kremenek72639102008-02-19 02:01:16 +0000998 // AddrLabelExpr
Ted Kremenek230aaab2008-02-12 21:37:25 +0000999
1000 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1001 break;
1002
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001003 case Stmt::BinaryOperatorClass: {
1004 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001005
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001006 if (B->isLogicalOp()) {
1007 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001008 break;
1009 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001010 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001011 StateTy St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001012 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001013 break;
1014 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001015
1016 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1017 break;
1018 }
Ted Kremenekde434242008-02-19 01:44:53 +00001019
1020 case Stmt::CallExprClass: {
1021 CallExpr* C = cast<CallExpr>(S);
1022 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1023 break;
1024 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001025
1026 case Stmt::CastExprClass: {
1027 CastExpr* C = cast<CastExpr>(S);
1028 VisitCast(C, C->getSubExpr(), Pred, Dst);
1029 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001030 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001031
Ted Kremenek72639102008-02-19 02:01:16 +00001032 // While explicitly creating a node+state for visiting a CharacterLiteral
1033 // seems wasteful, it also solves a bunch of problems when handling
1034 // the ?, &&, and ||.
1035
1036 case Stmt::CharacterLiteralClass: {
1037 CharacterLiteral* C = cast<CharacterLiteral>(S);
1038 StateTy St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001039 NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(),
Ted Kremenek72639102008-02-19 02:01:16 +00001040 C->getLoc());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001041 Nodify(Dst, C, Pred, SetRVal(St, C, X));
Ted Kremenek72639102008-02-19 02:01:16 +00001042 break;
1043 }
1044
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001045 // FIXME: ChooseExpr is really a constant. We need to fix
1046 // the CFG do not model them as explicit control-flow.
1047
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001048 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1049 ChooseExpr* C = cast<ChooseExpr>(S);
1050 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1051 break;
1052 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001053
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001054 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001055 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1056 break;
1057
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001058 case Stmt::ConditionalOperatorClass: { // '?' operator
1059 ConditionalOperator* C = cast<ConditionalOperator>(S);
1060 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1061 break;
1062 }
1063
1064 case Stmt::DeclRefExprClass:
1065 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1066 break;
1067
1068 case Stmt::DeclStmtClass:
1069 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1070 break;
1071
Ted Kremenek72639102008-02-19 02:01:16 +00001072 // While explicitly creating a node+state for visiting an IntegerLiteral
1073 // seems wasteful, it also solves a bunch of problems when handling
1074 // the ?, &&, and ||.
1075
1076 case Stmt::IntegerLiteralClass: {
1077 StateTy St = Pred->getState();
1078 IntegerLiteral* I = cast<IntegerLiteral>(S);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001079 NonLVal X = NonLVal::MakeVal(ValMgr, I);
1080 Nodify(Dst, I, Pred, SetRVal(St, I, X));
Ted Kremenek72639102008-02-19 02:01:16 +00001081 break;
1082 }
1083
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001084 case Stmt::ImplicitCastExprClass: {
1085 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1086 VisitCast(C, C->getSubExpr(), Pred, Dst);
1087 break;
1088 }
1089
1090 case Stmt::ParenExprClass:
1091 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1092 break;
1093
1094 case Stmt::SizeOfAlignOfTypeExprClass:
1095 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1096 break;
1097
Ted Kremenekda9bd092008-02-08 07:05:39 +00001098 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001099 StmtExpr* SE = cast<StmtExpr>(S);
1100
Ted Kremenekda9bd092008-02-08 07:05:39 +00001101 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001102 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001103 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001104 break;
1105 }
1106
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001107 // FIXME: We may wish to always bind state to ReturnStmts so
1108 // that users can quickly query what was the state at the
1109 // exit points of a function.
1110
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001111 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001112 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1113 Visit(R, Pred, Dst);
1114 else
1115 Dst.Add(Pred);
1116
1117 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001118 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001119
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001120 case Stmt::UnaryOperatorClass: {
1121 UnaryOperator* U = cast<UnaryOperator>(S);
1122
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001123 switch (U->getOpcode()) {
1124 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1125 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1126 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1127 default: VisitUnaryOperator(U, Pred, Dst); break;
1128 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001129
Ted Kremenek9de04c42008-01-24 20:55:43 +00001130 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001131 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001132 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001133}
1134
Ted Kremenekee985462008-01-16 18:18:48 +00001135//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001136// "Assume" logic.
1137//===----------------------------------------------------------------------===//
1138
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001139GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001140 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001141 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001142 switch (Cond.getSubKind()) {
1143 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001144 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001145 return St;
1146
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001147 case lval::SymbolValKind:
1148 if (Assumption)
1149 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1150 ValMgr.getZeroWithPtrWidth(), isFeasible);
1151 else
1152 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1153 ValMgr.getZeroWithPtrWidth(), isFeasible);
1154
Ted Kremenek08b66252008-02-06 04:31:33 +00001155
Ted Kremenek329f8542008-02-05 21:52:21 +00001156 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001157 isFeasible = Assumption;
1158 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001159
Ted Kremenek329f8542008-02-05 21:52:21 +00001160 case lval::ConcreteIntKind: {
1161 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001162 isFeasible = b ? Assumption : !Assumption;
1163 return St;
1164 }
1165 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001166}
1167
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001168GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001169 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001170 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001171 switch (Cond.getSubKind()) {
1172 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001173 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001174 return St;
1175
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001176
1177 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001178 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001179 SymbolID sym = SV.getSymbol();
1180
1181 if (Assumption)
1182 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1183 isFeasible);
1184 else
1185 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1186 isFeasible);
1187 }
1188
Ted Kremenek08b66252008-02-06 04:31:33 +00001189 case nonlval::SymIntConstraintValKind:
1190 return
1191 AssumeSymInt(St, Assumption,
1192 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1193 isFeasible);
1194
Ted Kremenek329f8542008-02-05 21:52:21 +00001195 case nonlval::ConcreteIntKind: {
1196 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001197 isFeasible = b ? Assumption : !Assumption;
1198 return St;
1199 }
1200 }
1201}
1202
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001203GRExprEngine::StateTy
1204GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001205 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001206
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001207 // First, determine if sym == X, where X != V.
1208 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1209 isFeasible = *X != V;
1210 return St;
1211 }
1212
1213 // Second, determine if sym != V.
1214 if (St.isNotEqual(sym, V)) {
1215 isFeasible = true;
1216 return St;
1217 }
1218
1219 // If we reach here, sym is not a constant and we don't know if it is != V.
1220 // Make that assumption.
1221
1222 isFeasible = true;
1223 return StateMgr.AddNE(St, sym, V);
1224}
1225
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001226GRExprEngine::StateTy
1227GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001228 const llvm::APSInt& V, bool& isFeasible) {
1229
1230 // First, determine if sym == X, where X != V.
1231 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1232 isFeasible = *X == V;
1233 return St;
1234 }
1235
1236 // Second, determine if sym != V.
1237 if (St.isNotEqual(sym, V)) {
1238 isFeasible = false;
1239 return St;
1240 }
1241
1242 // If we reach here, sym is not a constant and we don't know if it is == V.
1243 // Make that assumption.
1244
1245 isFeasible = true;
1246 return StateMgr.AddEQ(St, sym, V);
1247}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001248
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001249GRExprEngine::StateTy
1250GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001251 const SymIntConstraint& C, bool& isFeasible) {
1252
1253 switch (C.getOpcode()) {
1254 default:
1255 // No logic yet for other operators.
1256 return St;
1257
1258 case BinaryOperator::EQ:
1259 if (Assumption)
1260 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1261 else
1262 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1263
1264 case BinaryOperator::NE:
1265 if (Assumption)
1266 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1267 else
1268 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1269 }
1270}
1271
Ted Kremenekb38911f2008-01-30 23:03:39 +00001272//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001273// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001274//===----------------------------------------------------------------------===//
1275
Ted Kremenekaa66a322008-01-16 21:46:15 +00001276#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001277static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001278
Ted Kremenekaa66a322008-01-16 21:46:15 +00001279namespace llvm {
1280template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001281struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001282 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001283
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001284 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001285
1286 Out << "Variables:\\l";
1287
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001288 bool isFirst = true;
1289
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001290 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001291 E=St.vb_end(); I!=E;++I) {
1292
1293 if (isFirst)
1294 isFirst = false;
1295 else
1296 Out << "\\l";
1297
1298 Out << ' ' << I.getKey()->getName() << " : ";
1299 I.getData().print(Out);
1300 }
1301
1302 }
1303
Ted Kremeneke7d22112008-02-11 19:21:59 +00001304
Ted Kremenek44842c22008-02-13 18:06:44 +00001305 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001306
1307 bool isFirst = true;
1308
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001309 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001310 I != E;++I) {
1311
1312 if (isFirst) {
1313 Out << "\\l\\lSub-Expressions:\\l";
1314 isFirst = false;
1315 }
1316 else
1317 Out << "\\l";
1318
1319 Out << " (" << (void*) I.getKey() << ") ";
1320 I.getKey()->printPretty(Out);
1321 Out << " : ";
1322 I.getData().print(Out);
1323 }
1324 }
1325
Ted Kremenek44842c22008-02-13 18:06:44 +00001326 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001327
Ted Kremenek016f52f2008-02-08 21:10:02 +00001328 bool isFirst = true;
1329
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001330 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001331 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001332 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001333 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001334 isFirst = false;
1335 }
1336 else
1337 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001338
Ted Kremeneke7d22112008-02-11 19:21:59 +00001339 Out << " (" << (void*) I.getKey() << ") ";
1340 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001341 Out << " : ";
1342 I.getData().print(Out);
1343 }
1344 }
1345
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001346 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001347 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001348
1349 if (CE.isEmpty())
1350 return;
1351
1352 Out << "\\l\\|'==' constraints:";
1353
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001354 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001355 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1356 }
1357
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001358 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001359 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001360
1361 if (NE.isEmpty())
1362 return;
1363
1364 Out << "\\l\\|'!=' constraints:";
1365
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001366 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001367 I != EI; ++I){
1368
1369 Out << "\\l $" << I.getKey() << " : ";
1370 bool isFirst = true;
1371
1372 ValueState::IntSetTy::iterator J=I.getData().begin(),
1373 EJ=I.getData().end();
1374 for ( ; J != EJ; ++J) {
1375 if (isFirst) isFirst = false;
1376 else Out << ", ";
1377
1378 Out << (*J)->toString();
1379 }
1380 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001381 }
1382
1383 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1384
1385 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001386 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb5339122008-02-19 20:53:06 +00001387 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001388 GraphPrintCheckerState->isUninitStore(N) ||
1389 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001390 return "color=\"red\",style=\"filled\"";
1391
1392 return "";
1393 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001394
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001395 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001396 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001397
1398 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001399 ProgramPoint Loc = N->getLocation();
1400
1401 switch (Loc.getKind()) {
1402 case ProgramPoint::BlockEntranceKind:
1403 Out << "Block Entrance: B"
1404 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1405 break;
1406
1407 case ProgramPoint::BlockExitKind:
1408 assert (false);
1409 break;
1410
1411 case ProgramPoint::PostStmtKind: {
1412 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001413 Out << L.getStmt()->getStmtClassName() << ':'
1414 << (void*) L.getStmt() << ' ';
1415
Ted Kremenekaa66a322008-01-16 21:46:15 +00001416 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001417
1418 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1419 Out << "\\|Implicit-Null Dereference.\\l";
1420 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001421 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1422 Out << "\\|Explicit-Null Dereference.\\l";
1423 }
Ted Kremenekb5339122008-02-19 20:53:06 +00001424 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1425 Out << "\\|Dereference of uninitialied value.\\l";
1426 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001427 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001428 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenek9dca0622008-02-19 00:22:37 +00001429 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001430
Ted Kremenekaa66a322008-01-16 21:46:15 +00001431 break;
1432 }
1433
1434 default: {
1435 const BlockEdge& E = cast<BlockEdge>(Loc);
1436 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1437 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001438
1439 if (Stmt* T = E.getSrc()->getTerminator()) {
1440 Out << "\\|Terminator: ";
1441 E.getSrc()->printTerminator(Out);
1442
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001443 if (isa<SwitchStmt>(T)) {
1444 Stmt* Label = E.getDst()->getLabel();
1445
1446 if (Label) {
1447 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1448 Out << "\\lcase ";
1449 C->getLHS()->printPretty(Out);
1450
1451 if (Stmt* RHS = C->getRHS()) {
1452 Out << " .. ";
1453 RHS->printPretty(Out);
1454 }
1455
1456 Out << ":";
1457 }
1458 else {
1459 assert (isa<DefaultStmt>(Label));
1460 Out << "\\ldefault:";
1461 }
1462 }
1463 else
1464 Out << "\\l(implicit) default:";
1465 }
1466 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001467 // FIXME
1468 }
1469 else {
1470 Out << "\\lCondition: ";
1471 if (*E.getSrc()->succ_begin() == E.getDst())
1472 Out << "true";
1473 else
1474 Out << "false";
1475 }
1476
1477 Out << "\\l";
1478 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001479
1480 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1481 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1482 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001483 }
1484 }
1485
Ted Kremenek9153f732008-02-05 07:17:49 +00001486 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001487
Ted Kremeneke7d22112008-02-11 19:21:59 +00001488 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001489
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001490 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001491 return Out.str();
1492 }
1493};
1494} // end llvm namespace
1495#endif
1496
Ted Kremeneke01c9872008-02-14 22:36:46 +00001497void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001498#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001499 GraphPrintCheckerState = this;
1500 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001501 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001502#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001503}