blob: 1ca37615d81bf08ae98f59a82a5cbd61e050595c [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 Kremenekaa1c4e52008-02-21 18:02:17 +0000451 // FIXME: EvalCall must handle the case where the callee is Unknown.
452 assert (!L.isUnknown());
453
454 Nodify(Dst, CE, *DI, EvalCall(CE, cast<LVal>(L), (*DI)->getState()));
Ted Kremenekde434242008-02-19 01:44:53 +0000455 }
456}
457
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000458void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000459
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000460 NodeSet S1;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000461 Visit(Ex, Pred, S1);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000462
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000463 QualType T = CastE->getType();
464
Ted Kremenek402563b2008-02-19 18:47:04 +0000465 // Check for redundant casts or casting to "void"
466 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000467 Ex->getType() == T ||
468 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000469
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000470 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000471 Dst.Add(*I1);
472
Ted Kremenek874d63f2008-01-24 02:02:54 +0000473 return;
474 }
475
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000476 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000477 NodeTy* N = *I1;
478 StateTy St = N->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000479 RVal V = GetRVal(St, Ex);
480 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000481 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000482}
483
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000484void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000485 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000486
487 StateTy St = Pred->getState();
488
489 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000490 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000491
492 // FIXME: Add support for local arrays.
493 if (VD->getType()->isArrayType())
494 continue;
495
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000496 const Expr* Ex = VD->getInit();
497
498 St = SetRVal(St, lval::DeclVal(VD),
499 Ex ? GetRVal(St, Ex) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000500 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000501
502 Nodify(Dst, DS, Pred, St);
503
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000504 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000505}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000506
Ted Kremenekf233d482008-02-05 00:26:40 +0000507
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000508void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000509 NodeTy* Pred, NodeSet& Dst) {
510
511 StateTy St = Pred->getState();
512
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000513 RVal V = GetRVal(St, L);
514 if (isa<UnknownVal>(V)) V = GetRVal(St, R);
Ted Kremenekf233d482008-02-05 00:26:40 +0000515
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000516 Nodify(Dst, Ex, Pred, SetRVal(St, Ex, V));
Ted Kremenekf233d482008-02-05 00:26:40 +0000517}
518
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000519/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000520void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
521 NodeTy* Pred,
522 NodeSet& Dst) {
523
524 assert (Ex->isSizeOf() && "AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000525
526 // 6.5.3.4 sizeof: "The result type is an integer."
527
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000528 QualType T = Ex->getArgumentType();
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000529
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000530 // FIXME: Implement alignof
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000531
Ted Kremenek297d0d72008-02-21 18:15:29 +0000532 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000533 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000534 return;
535
Ted Kremenek297d0d72008-02-21 18:15:29 +0000536
537 uint64_t size = 1; // Handle sizeof(void)
538
539 if (T != getContext().VoidTy) {
540 SourceLocation Loc = Ex->getExprLoc();
541 size = getContext().getTypeSize(T, Loc) / 8;
542 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000543
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000544 Nodify(Dst, Ex, Pred,
545 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000546 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000547
548}
549
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000550void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
551
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000552 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000553
554 NodeSet DstTmp;
555
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000556 if (!isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000557 DstTmp.Add(Pred);
558 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000559 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000560
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000561 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000562
563 NodeTy* N = *I;
564 StateTy St = N->getState();
565
566 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
567
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000568 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000569
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000570 // Check for dereferences of uninitialized values.
571
572 if (V.isUninit()) {
573
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000574 NodeTy* Succ = Builder->generateNode(U, St, N);
575
576 if (Succ) {
577 Succ->markAsSink();
578 UninitDeref.insert(Succ);
579 }
580
581 continue;
582 }
583
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000584 // Check for dereferences of unknown values. Treat as No-Ops.
585
586 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000587 Dst.Add(N);
588 continue;
589 }
590
591 // After a dereference, one of two possible situations arise:
592 // (1) A crash, because the pointer was NULL.
593 // (2) The pointer is not NULL, and the dereference works.
594 //
595 // We add these assumptions.
596
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000597 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000598 bool isFeasibleNotNull;
599
600 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000601
602 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000603
604 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000605
606 // FIXME: Currently symbolic analysis "generates" new symbols
607 // for the contents of values. We need a better approach.
608
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000609 Nodify(Dst, U, N, SetRVal(StNotNull, U,
610 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000611 }
612
613 bool isFeasibleNull;
614
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000615 // Now "assume" that the pointer is NULL.
616
617 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000618
619 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000620
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000621 // We don't use "Nodify" here because the node will be a sink
622 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000623
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000624 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
625
626 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000627
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000628 NullNode->markAsSink();
629
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000630 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
631 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000632 }
633 }
634 }
635}
636
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000637void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
638 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000639
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000640 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000641
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000642 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000643 assert (U->getOpcode() != UnaryOperator::SizeOf);
644 assert (U->getOpcode() != UnaryOperator::AlignOf);
645
646 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000647
648 switch (U->getOpcode()) {
649 case UnaryOperator::PostInc:
650 case UnaryOperator::PostDec:
651 case UnaryOperator::PreInc:
652 case UnaryOperator::PreDec:
653 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000654 // Evalue subexpression as an LVal.
655 use_GetLVal = true;
656 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000657 break;
658
659 default:
660 Visit(U->getSubExpr(), Pred, S1);
661 break;
662 }
663
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000664 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000665
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000666 NodeTy* N1 = *I1;
667 StateTy St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000668
669 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
670 GetRVal(St, U->getSubExpr());
671
672 if (SubV.isUnknown()) {
673 Dst.Add(N1);
674 continue;
675 }
676
677 if (SubV.isUninit()) {
678 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
679 continue;
680 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000681
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000682 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000683
684 // Handle ++ and -- (both pre- and post-increment).
685
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000686 LVal SubLV = cast<LVal>(SubV);
687 RVal V = GetRVal(St, SubLV, U->getType());
688
689 // An LVal should never bind to UnknownVal.
690 assert (!V.isUnknown());
691
692 // Propagate uninitialized values.
693 if (V.isUninit()) {
694 Nodify(Dst, U, N1, SetRVal(St, U, V));
695 continue;
696 }
697
698 // Handle all NonLVals.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000699
700 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
701 : BinaryOperator::Sub;
702
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000703 RVal Result = EvalBinOp(Op, cast<NonLVal>(V), MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000704
705 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000707 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000708 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000709
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000710 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000711 continue;
712 }
713
714 // Handle all other unary operators.
715
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000716 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000717
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000718 case UnaryOperator::Minus:
719 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000720 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000721
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000722 case UnaryOperator::Not:
723 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000724 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000725
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000726 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000727
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000728 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
729 //
730 // Note: technically we do "E == 0", but this is the same in the
731 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732
733 if (isa<LVal>(SubV)) {
734 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
735 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
736 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000737 }
738 else {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000739 nonlval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
740 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
741 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000742 }
743
744 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000745
Ted Kremenek64924852008-01-31 02:35:41 +0000746 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000747 assert (isa<LVal>(SubV));
748 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000749 break;
750 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000751
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000752 default: ;
753 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000754 }
755
756 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000757 }
758}
759
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000760void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
761 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000762
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000763 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000764
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000765 // FIXME: Add support for VLAs.
766 if (!T.getTypePtr()->isConstantSizeType())
767 return;
768
769 SourceLocation Loc = U->getExprLoc();
770 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
771 StateTy St = Pred->getState();
772 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
773
774 Nodify(Dst, U, Pred, St);
775}
776
777void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
778
779 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
780
781 Ex = Ex->IgnoreParens();
782
783 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000784 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000785 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000786 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000787
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000788 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000789 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000790 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000791
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000792 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000793 Dst.Add(Pred);
794 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000795 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000796
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000797 return;
798 }
799 }
800
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000801 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000802}
803
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000804void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000805 GRExprEngine::NodeTy* Pred,
806 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000807 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000808
809 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000810 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000811 else
812 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000813
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000814 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000815
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000816 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000817
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000819 // In such cases, we want to (initially) treat the LHS as an LVal,
820 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
821 // evaluated to LValDecl's instead of to an NonLVal.
822
823 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
824 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000825
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000826 // Visit the RHS...
827
828 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000829 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000830
831 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000832
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000833 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000834
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000835 NodeTy* N2 = *I2;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000836 StateTy St = N2->getState();
837 RVal RightV = GetRVal(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000838
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000839 BinaryOperator::Opcode Op = B->getOpcode();
840
841 if (Op <= BinaryOperator::Or) {
842
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000843 // Process non-assignements except commas or short-circuited
844 // logical expressions (LAnd and LOr).
845
846 RVal Result = EvalBinOp(Op, LeftV, RightV);
847
848 if (Result.isUnknown()) {
849 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000850 continue;
851 }
852
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000853 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000854 continue;
855 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000856
857 // Process assignments.
858
859 switch (Op) {
860
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000861 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000862
863 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +0000864
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000865 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000866 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000867 continue;
868 }
869
870 if (LeftV.isUnknown()) {
871 St = SetRVal(St, B, RightV);
872 break;
873 }
Ted Kremenek9dca0622008-02-19 00:22:37 +0000874
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000875 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000876 break;
877 }
878
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000879 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000880
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000881 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000882
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000883 assert (B->isCompoundAssignmentOp());
884
885 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000886 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000887 continue;
888 }
889
890 if (LeftV.isUnknown()) {
891
892 // While we do not know the location to store RightV,
893 // the entire expression does evaluate to RightV.
894
895 if (RightV.isUnknown()) {
896 Dst.Add(N2);
897 continue;
898 }
899
900 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +0000901 break;
902 }
903
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000904 // At this pointer we know that the LHS evaluates to an LVal
905 // that is neither "Unknown" or "Unintialized."
906
907 LVal LeftLV = cast<LVal>(LeftV);
908
909 // Propagate uninitialized values (right-side).
910
911 if (RightV.isUninit()) {
912 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
Ted Kremenekb5339122008-02-19 20:53:06 +0000913 break;
914 }
915
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000916 // Fetch the value of the LHS (the value of the variable, etc.).
917
918 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
919
920 // Propagate uninitialized value (left-side).
921
922 if (V.isUninit()) {
923 St = SetRVal(St, B, V);
924 break;
925 }
926
927 // Propagate unknown values.
928
929 assert (!V.isUnknown() &&
930 "An LVal should never bind to UnknownVal");
931
932 if (RightV.isUnknown()) {
933 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
934 break;
935 }
936
937 // Neither the LHS or the RHS have Unknown/Uninit values. Process
938 // the operation and store the result.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000939
Ted Kremenekda9bd092008-02-08 07:05:39 +0000940 if (Op >= BinaryOperator::AndAssign)
941 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
942 else
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000943 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000944
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000945 RVal Result = EvalBinOp(Op, V, RightV);
946 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000947 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000948 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000949
950 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000951 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000952 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000953}
Ted Kremenekee985462008-01-16 18:18:48 +0000954
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000955void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000956 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
957 N->markAsSink();
958 UninitStores.insert(N);
959}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000960
Ted Kremenek9dca0622008-02-19 00:22:37 +0000961void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000962
963 // FIXME: add metadata to the CFG so that we can disable
964 // this check when we KNOW that there is no block-level subexpression.
965 // The motivation is that this check requires a hashtable lookup.
966
967 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
968 Dst.Add(Pred);
969 return;
970 }
971
972 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000973
974 default:
975 // Cases we intentionally have "default" handle:
Ted Kremenek72639102008-02-19 02:01:16 +0000976 // AddrLabelExpr
Ted Kremenek230aaab2008-02-12 21:37:25 +0000977
978 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
979 break;
980
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000981 case Stmt::BinaryOperatorClass: {
982 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000983
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000984 if (B->isLogicalOp()) {
985 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000986 break;
987 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000988 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000989 StateTy St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000990 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000991 break;
992 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000993
994 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
995 break;
996 }
Ted Kremenekde434242008-02-19 01:44:53 +0000997
998 case Stmt::CallExprClass: {
999 CallExpr* C = cast<CallExpr>(S);
1000 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1001 break;
1002 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001003
1004 case Stmt::CastExprClass: {
1005 CastExpr* C = cast<CastExpr>(S);
1006 VisitCast(C, C->getSubExpr(), Pred, Dst);
1007 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001008 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001009
Ted Kremenek72639102008-02-19 02:01:16 +00001010 // While explicitly creating a node+state for visiting a CharacterLiteral
1011 // seems wasteful, it also solves a bunch of problems when handling
1012 // the ?, &&, and ||.
1013
1014 case Stmt::CharacterLiteralClass: {
1015 CharacterLiteral* C = cast<CharacterLiteral>(S);
1016 StateTy St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001017 NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(),
Ted Kremenek72639102008-02-19 02:01:16 +00001018 C->getLoc());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001019 Nodify(Dst, C, Pred, SetRVal(St, C, X));
Ted Kremenek72639102008-02-19 02:01:16 +00001020 break;
1021 }
1022
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001023 // FIXME: ChooseExpr is really a constant. We need to fix
1024 // the CFG do not model them as explicit control-flow.
1025
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001026 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1027 ChooseExpr* C = cast<ChooseExpr>(S);
1028 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1029 break;
1030 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001031
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001032 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001033 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1034 break;
1035
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001036 case Stmt::ConditionalOperatorClass: { // '?' operator
1037 ConditionalOperator* C = cast<ConditionalOperator>(S);
1038 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1039 break;
1040 }
1041
1042 case Stmt::DeclRefExprClass:
1043 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1044 break;
1045
1046 case Stmt::DeclStmtClass:
1047 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1048 break;
1049
Ted Kremenek72639102008-02-19 02:01:16 +00001050 // While explicitly creating a node+state for visiting an IntegerLiteral
1051 // seems wasteful, it also solves a bunch of problems when handling
1052 // the ?, &&, and ||.
1053
1054 case Stmt::IntegerLiteralClass: {
1055 StateTy St = Pred->getState();
1056 IntegerLiteral* I = cast<IntegerLiteral>(S);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001057 NonLVal X = NonLVal::MakeVal(ValMgr, I);
1058 Nodify(Dst, I, Pred, SetRVal(St, I, X));
Ted Kremenek72639102008-02-19 02:01:16 +00001059 break;
1060 }
1061
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001062 case Stmt::ImplicitCastExprClass: {
1063 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1064 VisitCast(C, C->getSubExpr(), Pred, Dst);
1065 break;
1066 }
1067
1068 case Stmt::ParenExprClass:
1069 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1070 break;
1071
1072 case Stmt::SizeOfAlignOfTypeExprClass:
1073 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1074 break;
1075
Ted Kremenekda9bd092008-02-08 07:05:39 +00001076 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001077 StmtExpr* SE = cast<StmtExpr>(S);
1078
Ted Kremenekda9bd092008-02-08 07:05:39 +00001079 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001080 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001081 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001082 break;
1083 }
1084
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001085 // FIXME: We may wish to always bind state to ReturnStmts so
1086 // that users can quickly query what was the state at the
1087 // exit points of a function.
1088
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001089 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001090 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1091 Visit(R, Pred, Dst);
1092 else
1093 Dst.Add(Pred);
1094
1095 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001096 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001097
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001098 case Stmt::UnaryOperatorClass: {
1099 UnaryOperator* U = cast<UnaryOperator>(S);
1100
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001101 switch (U->getOpcode()) {
1102 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1103 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1104 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1105 default: VisitUnaryOperator(U, Pred, Dst); break;
1106 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001107
Ted Kremenek9de04c42008-01-24 20:55:43 +00001108 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001109 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001110 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001111}
1112
Ted Kremenekee985462008-01-16 18:18:48 +00001113//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001114// "Assume" logic.
1115//===----------------------------------------------------------------------===//
1116
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001117GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001118 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001119 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001120 switch (Cond.getSubKind()) {
1121 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001122 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001123 return St;
1124
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001125 case lval::SymbolValKind:
1126 if (Assumption)
1127 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1128 ValMgr.getZeroWithPtrWidth(), isFeasible);
1129 else
1130 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1131 ValMgr.getZeroWithPtrWidth(), isFeasible);
1132
Ted Kremenek08b66252008-02-06 04:31:33 +00001133
Ted Kremenek329f8542008-02-05 21:52:21 +00001134 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001135 isFeasible = Assumption;
1136 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001137
Ted Kremenek329f8542008-02-05 21:52:21 +00001138 case lval::ConcreteIntKind: {
1139 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001140 isFeasible = b ? Assumption : !Assumption;
1141 return St;
1142 }
1143 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001144}
1145
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001146GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001147 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001148 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001149 switch (Cond.getSubKind()) {
1150 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001151 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001152 return St;
1153
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001154
1155 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001156 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001157 SymbolID sym = SV.getSymbol();
1158
1159 if (Assumption)
1160 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1161 isFeasible);
1162 else
1163 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1164 isFeasible);
1165 }
1166
Ted Kremenek08b66252008-02-06 04:31:33 +00001167 case nonlval::SymIntConstraintValKind:
1168 return
1169 AssumeSymInt(St, Assumption,
1170 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1171 isFeasible);
1172
Ted Kremenek329f8542008-02-05 21:52:21 +00001173 case nonlval::ConcreteIntKind: {
1174 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001175 isFeasible = b ? Assumption : !Assumption;
1176 return St;
1177 }
1178 }
1179}
1180
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001181GRExprEngine::StateTy
1182GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001183 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001184
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001185 // First, determine if sym == X, where X != V.
1186 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1187 isFeasible = *X != V;
1188 return St;
1189 }
1190
1191 // Second, determine if sym != V.
1192 if (St.isNotEqual(sym, V)) {
1193 isFeasible = true;
1194 return St;
1195 }
1196
1197 // If we reach here, sym is not a constant and we don't know if it is != V.
1198 // Make that assumption.
1199
1200 isFeasible = true;
1201 return StateMgr.AddNE(St, sym, V);
1202}
1203
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001204GRExprEngine::StateTy
1205GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001206 const llvm::APSInt& V, bool& isFeasible) {
1207
1208 // First, determine if sym == X, where X != V.
1209 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1210 isFeasible = *X == V;
1211 return St;
1212 }
1213
1214 // Second, determine if sym != V.
1215 if (St.isNotEqual(sym, V)) {
1216 isFeasible = false;
1217 return St;
1218 }
1219
1220 // If we reach here, sym is not a constant and we don't know if it is == V.
1221 // Make that assumption.
1222
1223 isFeasible = true;
1224 return StateMgr.AddEQ(St, sym, V);
1225}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001226
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001227GRExprEngine::StateTy
1228GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001229 const SymIntConstraint& C, bool& isFeasible) {
1230
1231 switch (C.getOpcode()) {
1232 default:
1233 // No logic yet for other operators.
1234 return St;
1235
1236 case BinaryOperator::EQ:
1237 if (Assumption)
1238 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1239 else
1240 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1241
1242 case BinaryOperator::NE:
1243 if (Assumption)
1244 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1245 else
1246 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1247 }
1248}
1249
Ted Kremenekb38911f2008-01-30 23:03:39 +00001250//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001251// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001252//===----------------------------------------------------------------------===//
1253
Ted Kremenekaa66a322008-01-16 21:46:15 +00001254#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001255static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001256
Ted Kremenekaa66a322008-01-16 21:46:15 +00001257namespace llvm {
1258template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001259struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001260 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001261
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001262 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001263
1264 Out << "Variables:\\l";
1265
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001266 bool isFirst = true;
1267
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001268 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001269 E=St.vb_end(); I!=E;++I) {
1270
1271 if (isFirst)
1272 isFirst = false;
1273 else
1274 Out << "\\l";
1275
1276 Out << ' ' << I.getKey()->getName() << " : ";
1277 I.getData().print(Out);
1278 }
1279
1280 }
1281
Ted Kremeneke7d22112008-02-11 19:21:59 +00001282
Ted Kremenek44842c22008-02-13 18:06:44 +00001283 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001284
1285 bool isFirst = true;
1286
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001287 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001288 I != E;++I) {
1289
1290 if (isFirst) {
1291 Out << "\\l\\lSub-Expressions:\\l";
1292 isFirst = false;
1293 }
1294 else
1295 Out << "\\l";
1296
1297 Out << " (" << (void*) I.getKey() << ") ";
1298 I.getKey()->printPretty(Out);
1299 Out << " : ";
1300 I.getData().print(Out);
1301 }
1302 }
1303
Ted Kremenek44842c22008-02-13 18:06:44 +00001304 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001305
Ted Kremenek016f52f2008-02-08 21:10:02 +00001306 bool isFirst = true;
1307
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001308 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001309 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001310 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001311 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001312 isFirst = false;
1313 }
1314 else
1315 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001316
Ted Kremeneke7d22112008-02-11 19:21:59 +00001317 Out << " (" << (void*) I.getKey() << ") ";
1318 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001319 Out << " : ";
1320 I.getData().print(Out);
1321 }
1322 }
1323
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001324 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001325 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001326
1327 if (CE.isEmpty())
1328 return;
1329
1330 Out << "\\l\\|'==' constraints:";
1331
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001332 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001333 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1334 }
1335
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001336 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001337 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001338
1339 if (NE.isEmpty())
1340 return;
1341
1342 Out << "\\l\\|'!=' constraints:";
1343
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001344 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001345 I != EI; ++I){
1346
1347 Out << "\\l $" << I.getKey() << " : ";
1348 bool isFirst = true;
1349
1350 ValueState::IntSetTy::iterator J=I.getData().begin(),
1351 EJ=I.getData().end();
1352 for ( ; J != EJ; ++J) {
1353 if (isFirst) isFirst = false;
1354 else Out << ", ";
1355
1356 Out << (*J)->toString();
1357 }
1358 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001359 }
1360
1361 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1362
1363 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001364 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb5339122008-02-19 20:53:06 +00001365 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001366 GraphPrintCheckerState->isUninitStore(N) ||
1367 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001368 return "color=\"red\",style=\"filled\"";
1369
1370 return "";
1371 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001372
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001373 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001374 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001375
1376 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001377 ProgramPoint Loc = N->getLocation();
1378
1379 switch (Loc.getKind()) {
1380 case ProgramPoint::BlockEntranceKind:
1381 Out << "Block Entrance: B"
1382 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1383 break;
1384
1385 case ProgramPoint::BlockExitKind:
1386 assert (false);
1387 break;
1388
1389 case ProgramPoint::PostStmtKind: {
1390 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001391 Out << L.getStmt()->getStmtClassName() << ':'
1392 << (void*) L.getStmt() << ' ';
1393
Ted Kremenekaa66a322008-01-16 21:46:15 +00001394 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001395
1396 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1397 Out << "\\|Implicit-Null Dereference.\\l";
1398 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001399 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1400 Out << "\\|Explicit-Null Dereference.\\l";
1401 }
Ted Kremenekb5339122008-02-19 20:53:06 +00001402 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1403 Out << "\\|Dereference of uninitialied value.\\l";
1404 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001405 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001406 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenek9dca0622008-02-19 00:22:37 +00001407 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001408
Ted Kremenekaa66a322008-01-16 21:46:15 +00001409 break;
1410 }
1411
1412 default: {
1413 const BlockEdge& E = cast<BlockEdge>(Loc);
1414 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1415 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001416
1417 if (Stmt* T = E.getSrc()->getTerminator()) {
1418 Out << "\\|Terminator: ";
1419 E.getSrc()->printTerminator(Out);
1420
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001421 if (isa<SwitchStmt>(T)) {
1422 Stmt* Label = E.getDst()->getLabel();
1423
1424 if (Label) {
1425 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1426 Out << "\\lcase ";
1427 C->getLHS()->printPretty(Out);
1428
1429 if (Stmt* RHS = C->getRHS()) {
1430 Out << " .. ";
1431 RHS->printPretty(Out);
1432 }
1433
1434 Out << ":";
1435 }
1436 else {
1437 assert (isa<DefaultStmt>(Label));
1438 Out << "\\ldefault:";
1439 }
1440 }
1441 else
1442 Out << "\\l(implicit) default:";
1443 }
1444 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001445 // FIXME
1446 }
1447 else {
1448 Out << "\\lCondition: ";
1449 if (*E.getSrc()->succ_begin() == E.getDst())
1450 Out << "true";
1451 else
1452 Out << "false";
1453 }
1454
1455 Out << "\\l";
1456 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001457
1458 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1459 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1460 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001461 }
1462 }
1463
Ted Kremenek9153f732008-02-05 07:17:49 +00001464 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001465
Ted Kremeneke7d22112008-02-11 19:21:59 +00001466 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001467
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001468 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001469 return Out.str();
1470 }
1471};
1472} // end llvm namespace
1473#endif
1474
Ted Kremeneke01c9872008-02-14 22:36:46 +00001475void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001476#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001477 GraphPrintCheckerState = this;
1478 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001479 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001480#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001481}