blob: 66f5cae65476baf701a06a84bccdb6e264119f4c [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek50df4f42008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000017#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
18
19#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000020
21using namespace clang;
22using llvm::dyn_cast;
23using llvm::cast;
24using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000025
Ted Kremenek30fa28b2008-02-13 17:41:41 +000026GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000027GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000028
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000029 if (!StateCleaned) {
30 St = RemoveDeadBindings(CurrentStmt, St);
31 StateCleaned = true;
32 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000033
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000034 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000035
Ted Kremenek07baa252008-02-21 18:02:17 +000036 if (Ex == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000038
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000042
Ted Kremenek07baa252008-02-21 18:02:17 +000043 return StateMgr.SetRVal(St, Ex, isBlkExpr, V);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000044}
45
Ted Kremenek30fa28b2008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
Ted Kremenek07baa252008-02-21 18:02:17 +000047GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
Ted Kremenekf20c2012008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
Ted Kremenek07baa252008-02-21 18:02:17 +000052 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
53 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekf20c2012008-02-05 19:35:18 +000054
55 return RetBuf;
56}
57
Ted Kremenek30fa28b2008-02-13 17:41:41 +000058GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000059GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060
61 if (!StateCleaned) {
62 St = RemoveDeadBindings(CurrentStmt, St);
63 StateCleaned = true;
64 }
65
Ted Kremenek07baa252008-02-21 18:02:17 +000066 return StateMgr.SetRVal(St, LV, RV);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000067}
68
Ted Kremenek30fa28b2008-02-13 17:41:41 +000069void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +000070 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +000071
Ted Kremenek17c5f112008-02-11 19:21:59 +000072 // Remove old bindings for subexpressions.
73 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +000074
Ted Kremenek022b6052008-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 Kremenek07baa252008-02-21 18:02:17 +000090 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +000091
92 switch (V.getBaseKind()) {
93 default:
94 break;
95
Ted Kremenek07baa252008-02-21 18:02:17 +000096 case RVal::UnknownKind:
Ted Kremenek90960972008-01-30 23:03:39 +000097 builder.generateNode(PrevState, true);
98 builder.generateNode(PrevState, false);
99 return;
100
Ted Kremenek07baa252008-02-21 18:02:17 +0000101 case RVal::UninitializedKind: {
Ted Kremenek90960972008-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 Kremenek022b6052008-02-15 22:29:00 +0000113
Ted Kremenek4b170e52008-02-12 18:08:17 +0000114 // Get the current block counter.
115 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekfd85f292008-02-12 19:49:57 +0000116 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
117 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000118
Ted Kremenek4b170e52008-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 Kremenek90960972008-01-30 23:03:39 +0000123
Ted Kremenek4b170e52008-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 Kremenek90960972008-01-30 23:03:39 +0000132 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000133 else
134 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000135
Ted Kremenekfd85f292008-02-12 19:49:57 +0000136 BlockID = builder.getTargetBlock(false)->getBlockID();
137 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000138
Ted Kremenek4b170e52008-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 Kremenek1f0eb992008-02-05 00:26:40 +0000153 else
154 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000155}
156
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000157/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000158/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000159void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000160
161 StateTy St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000162 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-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 Kremenek79f63f52008-02-13 17:27:37 +0000177 if (I.getLabel() == L) {
178 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-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 Kremenek79f63f52008-02-13 17:27:37 +0000189 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-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 Kremenek07baa252008-02-21 18:02:17 +0000196 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000197
198 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000199 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000200}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000201
Ted Kremenekaee121c2008-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 Kremenekbc965a62008-02-18 22:57:02 +0000209 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000210 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000211
Ted Kremenek07baa252008-02-21 18:02:17 +0000212 if (CondV.isUninit()) {
Ted Kremenekaee121c2008-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 Kremenekbc965a62008-02-18 22:57:02 +0000222
223 unsigned bits = getContext().getTypeSize(CondE->getType(),
224 CondE->getExprLoc());
225
Ted Kremenekaee121c2008-02-13 23:08:21 +0000226 APSInt V1(bits, false);
227 APSInt V2 = V1;
228
Ted Kremenek07baa252008-02-21 18:02:17 +0000229 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-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 Kremenek07baa252008-02-21 18:02:17 +0000254 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000255
256 do {
257 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
258
Ted Kremenek07baa252008-02-21 18:02:17 +0000259 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000260
261 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000262 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000263
Ted Kremenekb1934132008-02-14 19:37:24 +0000264 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-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 Kremenekb1934132008-02-14 19:37:24 +0000279 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-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 Kremenek30fa28b2008-02-13 17:41:41 +0000296void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000297 NodeSet& Dst) {
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000298
299 bool hasR2;
300 StateTy PrevState = Pred->getState();
301
Ted Kremenek07baa252008-02-21 18:02:17 +0000302 RVal R1 = GetRVal(PrevState, B->getLHS());
303 RVal R2 = GetRVal(PrevState, B->getRHS(), hasR2);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000304
305 if (hasR2) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000306 if (R2.isUnknownOrUninit()) {
307 Nodify(Dst, B, Pred, SetRVal(PrevState, B, R2));
Ted Kremenekbf988d02008-02-19 00:22:37 +0000308 return;
309 }
310 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000311 else if (R1.isUnknownOrUninit()) {
312 Nodify(Dst, B, Pred, SetRVal(PrevState, B, R1));
Ted Kremenek1f0eb992008-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 Kremenek07baa252008-02-21 18:02:17 +0000321 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-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 Kremenek07baa252008-02-21 18:02:17 +0000330 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B)));
Ted Kremenek1f0eb992008-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 Kremenek07baa252008-02-21 18:02:17 +0000342 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000343
344 St = Assume(PrevState, R2, false, isFeasible);
345
346 if (isFeasible)
Ted Kremenek07baa252008-02-21 18:02:17 +0000347 Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000348}
349
350
351
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000352void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000353
Ted Kremenek07baa252008-02-21 18:02:17 +0000354 Builder = &builder;
Ted Kremenekf031b872008-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 Kremenek07baa252008-02-21 18:02:17 +0000364
Ted Kremenekf031b872008-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 Kremeneka57214f2008-01-18 00:41:32 +0000369
Ted Kremenek07baa252008-02-21 18:02:17 +0000370 // For safety, NULL out these variables.
371
Ted Kremenekf031b872008-01-23 19:59:44 +0000372 CurrentStmt = NULL;
373 StmtEntryNode = NULL;
374 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000375}
376
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000377GRExprEngine::NodeTy*
378GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000379
380 // If the state hasn't changed, don't generate a new node.
Ted Kremenek02b5b402008-02-07 15:20:13 +0000381 if (St == Pred->getState())
Ted Kremenek80d52d02008-02-07 05:48:01 +0000382 return NULL;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000383
Ted Kremenek80d52d02008-02-07 05:48:01 +0000384 NodeTy* N = Builder->generateNode(S, St, Pred);
385 Dst.Add(N);
Ted Kremenek07baa252008-02-21 18:02:17 +0000386
Ted Kremenek80d52d02008-02-07 05:48:01 +0000387 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000388}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000389
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000390void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-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 Kremenek6d409922008-02-13 18:06:44 +0000397void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000398
Ted Kremenek9b32cd02008-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 Kremenek07baa252008-02-21 18:02:17 +0000407 StateTy St = Pred->getState();
408 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000409}
410
Ted Kremenekd9268e32008-02-19 01:44:53 +0000411void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000412 CallExpr::arg_iterator AI,
413 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000414 NodeSet& Dst) {
415
Ted Kremenek07baa252008-02-21 18:02:17 +0000416 // Process the arguments.
417
418 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000419
Ted Kremenek07baa252008-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 Kremenekd9268e32008-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 Kremenek07baa252008-02-21 18:02:17 +0000432 NodeSet DstTmp;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000433 Visit(CE->getCallee(), Pred, DstTmp);
434
435 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000436 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
437
Ted Kremenekd9268e32008-02-19 01:44:53 +0000438 StateTy St = (*DI)->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000439 RVal L = GetLVal(St, CE->getCallee());
Ted Kremenekd9268e32008-02-19 01:44:53 +0000440
441 // Check for uninitialized control-flow.
Ted Kremenek07baa252008-02-21 18:02:17 +0000442
443 if (L.isUninit()) {
444
Ted Kremenekd9268e32008-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 Kremenek07baa252008-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 Kremenekd9268e32008-02-19 01:44:53 +0000455 }
456}
457
Ted Kremenek07baa252008-02-21 18:02:17 +0000458void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000459
Ted Kremenek5f585b02008-02-19 18:52:54 +0000460 NodeSet S1;
Ted Kremenek07baa252008-02-21 18:02:17 +0000461 Visit(Ex, Pred, S1);
Ted Kremenek54eddae2008-01-24 02:02:54 +0000462
Ted Kremenek5f585b02008-02-19 18:52:54 +0000463 QualType T = CastE->getType();
464
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000465 // Check for redundant casts or casting to "void"
466 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000467 Ex->getType() == T ||
468 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000469
Ted Kremenek07baa252008-02-21 18:02:17 +0000470 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000471 Dst.Add(*I1);
472
Ted Kremenek54eddae2008-01-24 02:02:54 +0000473 return;
474 }
475
Ted Kremenek07baa252008-02-21 18:02:17 +0000476 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000477 NodeTy* N = *I1;
478 StateTy St = N->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000479 RVal V = GetRVal(St, Ex);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000480 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000481 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000482}
483
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000484void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000485 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000486
487 StateTy St = Pred->getState();
488
489 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000490 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000491
492 // FIXME: Add support for local arrays.
493 if (VD->getType()->isArrayType())
494 continue;
495
Ted Kremenek07baa252008-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 Kremenek8c9bc342008-01-28 22:51:57 +0000500 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000501
502 Nodify(Dst, DS, Pred, St);
503
Ted Kremenek07baa252008-02-21 18:02:17 +0000504 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000505}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000506
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000507
Ted Kremenek07baa252008-02-21 18:02:17 +0000508void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000509 NodeTy* Pred, NodeSet& Dst) {
510
511 StateTy St = Pred->getState();
512
Ted Kremenek07baa252008-02-21 18:02:17 +0000513 RVal V = GetRVal(St, L);
514 if (isa<UnknownVal>(V)) V = GetRVal(St, R);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000515
Ted Kremenek07baa252008-02-21 18:02:17 +0000516 Nodify(Dst, Ex, Pred, SetRVal(St, Ex, V));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000517}
518
Ted Kremenekfd85f292008-02-12 19:49:57 +0000519/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000520void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
521 NodeTy* Pred,
522 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000523
524 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000525
526 // 6.5.3.4 sizeof: "The result type is an integer."
527
Ted Kremenek07baa252008-02-21 18:02:17 +0000528 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000529
Ted Kremenek07baa252008-02-21 18:02:17 +0000530
Ted Kremenek571f5192008-02-21 18:15:29 +0000531 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000532 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000533 return;
534
Ted Kremenek571f5192008-02-21 18:15:29 +0000535
536 uint64_t size = 1; // Handle sizeof(void)
537
538 if (T != getContext().VoidTy) {
539 SourceLocation Loc = Ex->getExprLoc();
540 size = getContext().getTypeSize(T, Loc) / 8;
541 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000542
Ted Kremenek07baa252008-02-21 18:02:17 +0000543 Nodify(Dst, Ex, Pred,
544 SetRVal(Pred->getState(), Ex,
Ted Kremenek571f5192008-02-21 18:15:29 +0000545 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000546
547}
548
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000549void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
550
Ted Kremenek07baa252008-02-21 18:02:17 +0000551 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000552
553 NodeSet DstTmp;
554
Ted Kremenek07baa252008-02-21 18:02:17 +0000555 if (!isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000556 DstTmp.Add(Pred);
557 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000558 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000559
Ted Kremenek07baa252008-02-21 18:02:17 +0000560 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000561
562 NodeTy* N = *I;
563 StateTy St = N->getState();
564
565 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
566
Ted Kremenek07baa252008-02-21 18:02:17 +0000567 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000568
Ted Kremenek07baa252008-02-21 18:02:17 +0000569 // Check for dereferences of uninitialized values.
570
571 if (V.isUninit()) {
572
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000573 NodeTy* Succ = Builder->generateNode(U, St, N);
574
575 if (Succ) {
576 Succ->markAsSink();
577 UninitDeref.insert(Succ);
578 }
579
580 continue;
581 }
582
Ted Kremenek07baa252008-02-21 18:02:17 +0000583 // Check for dereferences of unknown values. Treat as No-Ops.
584
585 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000586 Dst.Add(N);
587 continue;
588 }
589
590 // After a dereference, one of two possible situations arise:
591 // (1) A crash, because the pointer was NULL.
592 // (2) The pointer is not NULL, and the dereference works.
593 //
594 // We add these assumptions.
595
Ted Kremenek07baa252008-02-21 18:02:17 +0000596 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000597 bool isFeasibleNotNull;
598
599 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000600
601 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000602
603 if (isFeasibleNotNull) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000604
605 // FIXME: Currently symbolic analysis "generates" new symbols
606 // for the contents of values. We need a better approach.
607
Ted Kremenek07baa252008-02-21 18:02:17 +0000608 Nodify(Dst, U, N, SetRVal(StNotNull, U,
609 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000610 }
611
612 bool isFeasibleNull;
613
Ted Kremenek07baa252008-02-21 18:02:17 +0000614 // Now "assume" that the pointer is NULL.
615
616 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000617
618 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000619
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000620 // We don't use "Nodify" here because the node will be a sink
621 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000622
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000623 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
624
625 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000626
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000627 NullNode->markAsSink();
628
Ted Kremenek07baa252008-02-21 18:02:17 +0000629 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
630 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000631 }
632 }
633 }
634}
635
Ted Kremenek07baa252008-02-21 18:02:17 +0000636void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
637 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000638
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000639 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000640
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000641 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000642 assert (U->getOpcode() != UnaryOperator::SizeOf);
643 assert (U->getOpcode() != UnaryOperator::AlignOf);
644
645 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000646
647 switch (U->getOpcode()) {
648 case UnaryOperator::PostInc:
649 case UnaryOperator::PostDec:
650 case UnaryOperator::PreInc:
651 case UnaryOperator::PreDec:
652 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000653 // Evalue subexpression as an LVal.
654 use_GetLVal = true;
655 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000656 break;
657
658 default:
659 Visit(U->getSubExpr(), Pred, S1);
660 break;
661 }
662
Ted Kremenek07baa252008-02-21 18:02:17 +0000663 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000664
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000665 NodeTy* N1 = *I1;
666 StateTy St = N1->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000667
668 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
669 GetRVal(St, U->getSubExpr());
670
671 if (SubV.isUnknown()) {
672 Dst.Add(N1);
673 continue;
674 }
675
676 if (SubV.isUninit()) {
677 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
678 continue;
679 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000680
Ted Kremenek22640ce2008-02-15 22:09:30 +0000681 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000682
683 // Handle ++ and -- (both pre- and post-increment).
684
Ted Kremenek07baa252008-02-21 18:02:17 +0000685 LVal SubLV = cast<LVal>(SubV);
686 RVal V = GetRVal(St, SubLV, U->getType());
687
Ted Kremenekb8782e12008-02-21 19:15:37 +0000688 if (V.isUnknown()) {
689 Dst.Add(N1);
690 continue;
691 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000692
693 // Propagate uninitialized values.
694 if (V.isUninit()) {
695 Nodify(Dst, U, N1, SetRVal(St, U, V));
696 continue;
697 }
698
Ted Kremenekb1669d42008-02-21 19:29:23 +0000699 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000700
701 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
702 : BinaryOperator::Sub;
703
Ted Kremenekb1669d42008-02-21 19:29:23 +0000704 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000705
706 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000707 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000708 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000709 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000710
Ted Kremenek07baa252008-02-21 18:02:17 +0000711 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000712 continue;
713 }
714
715 // Handle all other unary operators.
716
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000717 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000718
Ted Kremenek07baa252008-02-21 18:02:17 +0000719 case UnaryOperator::Minus:
720 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000721 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000722
Ted Kremenek07baa252008-02-21 18:02:17 +0000723 case UnaryOperator::Not:
724 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000725 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000726
Ted Kremenek07baa252008-02-21 18:02:17 +0000727 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000728
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000729 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
730 //
731 // Note: technically we do "E == 0", but this is the same in the
732 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000733
734 if (isa<LVal>(SubV)) {
735 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
736 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
737 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000738 }
739 else {
Ted Kremenek07baa252008-02-21 18:02:17 +0000740 nonlval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
741 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
742 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000743 }
744
745 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000746
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000747 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000748 assert (isa<LVal>(SubV));
749 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000750 break;
751 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000752
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000753 default: ;
754 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000755 }
756
757 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000758 }
759}
760
Ted Kremenek07baa252008-02-21 18:02:17 +0000761void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
762 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000763
Ted Kremenek07baa252008-02-21 18:02:17 +0000764 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000765
Ted Kremenek07baa252008-02-21 18:02:17 +0000766 // FIXME: Add support for VLAs.
767 if (!T.getTypePtr()->isConstantSizeType())
768 return;
769
770 SourceLocation Loc = U->getExprLoc();
771 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
772 StateTy St = Pred->getState();
773 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
774
775 Nodify(Dst, U, Pred, St);
776}
777
778void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
779
780 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
781
782 Ex = Ex->IgnoreParens();
783
784 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000785 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000786 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000787 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000788
Ted Kremenek07baa252008-02-21 18:02:17 +0000789 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000790 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000791 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000792
Ted Kremenek07baa252008-02-21 18:02:17 +0000793 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000794 Dst.Add(Pred);
795 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000796 Visit(Ex, Pred, Dst);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000797
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000798 return;
799 }
800 }
801
Ted Kremenek07baa252008-02-21 18:02:17 +0000802 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000803}
804
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000805void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000806 GRExprEngine::NodeTy* Pred,
807 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000808 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000809
810 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +0000811 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000812 else
813 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000814
Ted Kremenekf031b872008-01-23 19:59:44 +0000815 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000816
Ted Kremenekf031b872008-01-23 19:59:44 +0000817 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000818
Ted Kremenekf031b872008-01-23 19:59:44 +0000819 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +0000820 // In such cases, we want to (initially) treat the LHS as an LVal,
821 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
822 // evaluated to LValDecl's instead of to an NonLVal.
823
824 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
825 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000826
Ted Kremenek07baa252008-02-21 18:02:17 +0000827 // Visit the RHS...
828
829 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +0000830 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +0000831
832 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +0000833
Ted Kremenek07baa252008-02-21 18:02:17 +0000834 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000835
Ted Kremenekf031b872008-01-23 19:59:44 +0000836 NodeTy* N2 = *I2;
Ted Kremenek07baa252008-02-21 18:02:17 +0000837 StateTy St = N2->getState();
838 RVal RightV = GetRVal(St, B->getRHS());
Ted Kremenekf031b872008-01-23 19:59:44 +0000839
Ted Kremenek15cb0782008-02-06 22:50:25 +0000840 BinaryOperator::Opcode Op = B->getOpcode();
841
842 if (Op <= BinaryOperator::Or) {
843
Ted Kremenek07baa252008-02-21 18:02:17 +0000844 // Process non-assignements except commas or short-circuited
845 // logical expressions (LAnd and LOr).
846
847 RVal Result = EvalBinOp(Op, LeftV, RightV);
848
849 if (Result.isUnknown()) {
850 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000851 continue;
852 }
853
Ted Kremenek07baa252008-02-21 18:02:17 +0000854 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000855 continue;
856 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000857
858 // Process assignments.
859
860 switch (Op) {
861
Ted Kremenekf031b872008-01-23 19:59:44 +0000862 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000863
864 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +0000865
Ted Kremenek07baa252008-02-21 18:02:17 +0000866 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000867 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +0000868 continue;
869 }
870
871 if (LeftV.isUnknown()) {
872 St = SetRVal(St, B, RightV);
873 break;
874 }
Ted Kremenekbf988d02008-02-19 00:22:37 +0000875
Ted Kremenek07baa252008-02-21 18:02:17 +0000876 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +0000877 break;
878 }
879
Ted Kremenek07baa252008-02-21 18:02:17 +0000880 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +0000881
Ted Kremenek07baa252008-02-21 18:02:17 +0000882 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000883
Ted Kremenek07baa252008-02-21 18:02:17 +0000884 assert (B->isCompoundAssignmentOp());
885
886 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000887 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +0000888 continue;
889 }
890
891 if (LeftV.isUnknown()) {
892
893 // While we do not know the location to store RightV,
894 // the entire expression does evaluate to RightV.
895
896 if (RightV.isUnknown()) {
897 Dst.Add(N2);
898 continue;
899 }
900
901 St = SetRVal(St, B, RightV);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000902 break;
903 }
904
Ted Kremenek07baa252008-02-21 18:02:17 +0000905 // At this pointer we know that the LHS evaluates to an LVal
906 // that is neither "Unknown" or "Unintialized."
907
908 LVal LeftLV = cast<LVal>(LeftV);
909
910 // Propagate uninitialized values (right-side).
911
912 if (RightV.isUninit()) {
913 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
Ted Kremenek9d4895c2008-02-19 20:53:06 +0000914 break;
915 }
916
Ted Kremenek07baa252008-02-21 18:02:17 +0000917 // Fetch the value of the LHS (the value of the variable, etc.).
918
919 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
920
921 // Propagate uninitialized value (left-side).
922
923 if (V.isUninit()) {
924 St = SetRVal(St, B, V);
925 break;
926 }
927
928 // Propagate unknown values.
929
Ted Kremenekb8782e12008-02-21 19:15:37 +0000930 if (V.isUnknown()) {
931 Dst.Add(N2);
932 continue;
933 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000934
935 if (RightV.isUnknown()) {
936 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
937 break;
938 }
939
940 // Neither the LHS or the RHS have Unknown/Uninit values. Process
941 // the operation and store the result.
Ted Kremenek15cb0782008-02-06 22:50:25 +0000942
Ted Kremenek106f37c2008-02-08 07:05:39 +0000943 if (Op >= BinaryOperator::AndAssign)
944 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
945 else
Ted Kremenek22640ce2008-02-15 22:09:30 +0000946 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000947
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000948 // Get the computation type.
949 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
950
951 // Perform promotions.
952 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +0000953 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000954
955 // Evaluate operands and promote to result type.
956 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
957
Ted Kremenek07baa252008-02-21 18:02:17 +0000958 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +0000959 }
Ted Kremenekf031b872008-01-23 19:59:44 +0000960 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000961
962 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000963 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000964 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000965}
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000966
Ted Kremenek07baa252008-02-21 18:02:17 +0000967void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000968 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
969 N->markAsSink();
970 UninitStores.insert(N);
971}
Ted Kremenekbe962452008-01-16 19:42:59 +0000972
Ted Kremenekbf988d02008-02-19 00:22:37 +0000973void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000974
975 // FIXME: add metadata to the CFG so that we can disable
976 // this check when we KNOW that there is no block-level subexpression.
977 // The motivation is that this check requires a hashtable lookup.
978
979 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
980 Dst.Add(Pred);
981 return;
982 }
983
984 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000985
986 default:
987 // Cases we intentionally have "default" handle:
Ted Kremenek18044ff2008-02-19 02:01:16 +0000988 // AddrLabelExpr
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000989
990 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
991 break;
992
Ted Kremenek744a7862008-02-08 20:29:23 +0000993 case Stmt::BinaryOperatorClass: {
994 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000995
Ted Kremenek744a7862008-02-08 20:29:23 +0000996 if (B->isLogicalOp()) {
997 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000998 break;
999 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001000 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +00001001 StateTy St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001002 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001003 break;
1004 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001005
1006 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1007 break;
1008 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001009
1010 case Stmt::CallExprClass: {
1011 CallExpr* C = cast<CallExpr>(S);
1012 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1013 break;
1014 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001015
1016 case Stmt::CastExprClass: {
1017 CastExpr* C = cast<CastExpr>(S);
1018 VisitCast(C, C->getSubExpr(), Pred, Dst);
1019 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001020 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001021
Ted Kremenek18044ff2008-02-19 02:01:16 +00001022 // While explicitly creating a node+state for visiting a CharacterLiteral
1023 // seems wasteful, it also solves a bunch of problems when handling
1024 // the ?, &&, and ||.
1025
1026 case Stmt::CharacterLiteralClass: {
1027 CharacterLiteral* C = cast<CharacterLiteral>(S);
1028 StateTy St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001029 NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(),
Ted Kremenek18044ff2008-02-19 02:01:16 +00001030 C->getLoc());
Ted Kremenek07baa252008-02-21 18:02:17 +00001031 Nodify(Dst, C, Pred, SetRVal(St, C, X));
Ted Kremenek18044ff2008-02-19 02:01:16 +00001032 break;
1033 }
1034
Ted Kremenek07baa252008-02-21 18:02:17 +00001035 // FIXME: ChooseExpr is really a constant. We need to fix
1036 // the CFG do not model them as explicit control-flow.
1037
Ted Kremenekfd85f292008-02-12 19:49:57 +00001038 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1039 ChooseExpr* C = cast<ChooseExpr>(S);
1040 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1041 break;
1042 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001043
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001044 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001045 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1046 break;
1047
Ted Kremenekfd85f292008-02-12 19:49:57 +00001048 case Stmt::ConditionalOperatorClass: { // '?' operator
1049 ConditionalOperator* C = cast<ConditionalOperator>(S);
1050 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1051 break;
1052 }
1053
1054 case Stmt::DeclRefExprClass:
1055 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1056 break;
1057
1058 case Stmt::DeclStmtClass:
1059 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1060 break;
1061
Ted Kremenek18044ff2008-02-19 02:01:16 +00001062 // While explicitly creating a node+state for visiting an IntegerLiteral
1063 // seems wasteful, it also solves a bunch of problems when handling
1064 // the ?, &&, and ||.
1065
1066 case Stmt::IntegerLiteralClass: {
1067 StateTy St = Pred->getState();
1068 IntegerLiteral* I = cast<IntegerLiteral>(S);
Ted Kremenek07baa252008-02-21 18:02:17 +00001069 NonLVal X = NonLVal::MakeVal(ValMgr, I);
1070 Nodify(Dst, I, Pred, SetRVal(St, I, X));
Ted Kremenek18044ff2008-02-19 02:01:16 +00001071 break;
1072 }
1073
Ted Kremenekfd85f292008-02-12 19:49:57 +00001074 case Stmt::ImplicitCastExprClass: {
1075 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1076 VisitCast(C, C->getSubExpr(), Pred, Dst);
1077 break;
1078 }
1079
1080 case Stmt::ParenExprClass:
1081 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1082 break;
1083
1084 case Stmt::SizeOfAlignOfTypeExprClass:
1085 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1086 break;
1087
Ted Kremenek106f37c2008-02-08 07:05:39 +00001088 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001089 StmtExpr* SE = cast<StmtExpr>(S);
1090
Ted Kremenek106f37c2008-02-08 07:05:39 +00001091 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +00001092 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001093 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001094 break;
1095 }
1096
Ted Kremenek07baa252008-02-21 18:02:17 +00001097 // FIXME: We may wish to always bind state to ReturnStmts so
1098 // that users can quickly query what was the state at the
1099 // exit points of a function.
1100
Ted Kremenekfd85f292008-02-12 19:49:57 +00001101 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001102 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1103 Visit(R, Pred, Dst);
1104 else
1105 Dst.Add(Pred);
1106
1107 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001108 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001109
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001110 case Stmt::UnaryOperatorClass: {
1111 UnaryOperator* U = cast<UnaryOperator>(S);
1112
Ted Kremenek07baa252008-02-21 18:02:17 +00001113 switch (U->getOpcode()) {
1114 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1115 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1116 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1117 default: VisitUnaryOperator(U, Pred, Dst); break;
1118 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001119
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001120 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001121 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001122 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001123}
1124
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001125//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001126// "Assume" logic.
1127//===----------------------------------------------------------------------===//
1128
Ted Kremenek07baa252008-02-21 18:02:17 +00001129GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001130 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001131 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001132 switch (Cond.getSubKind()) {
1133 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001134 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001135 return St;
1136
Ted Kremenek13f31562008-02-06 00:54:14 +00001137 case lval::SymbolValKind:
1138 if (Assumption)
1139 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1140 ValMgr.getZeroWithPtrWidth(), isFeasible);
1141 else
1142 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1143 ValMgr.getZeroWithPtrWidth(), isFeasible);
1144
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001145
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001146 case lval::DeclValKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001147 isFeasible = Assumption;
1148 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001149
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001150 case lval::ConcreteIntKind: {
1151 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001152 isFeasible = b ? Assumption : !Assumption;
1153 return St;
1154 }
1155 }
Ted Kremenek90960972008-01-30 23:03:39 +00001156}
1157
Ted Kremenek07baa252008-02-21 18:02:17 +00001158GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001159 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001160 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001161 switch (Cond.getSubKind()) {
1162 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001163 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001164 return St;
1165
Ted Kremenekab359c12008-02-06 17:32:17 +00001166
1167 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001168 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001169 SymbolID sym = SV.getSymbol();
1170
1171 if (Assumption)
1172 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1173 isFeasible);
1174 else
1175 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1176 isFeasible);
1177 }
1178
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001179 case nonlval::SymIntConstraintValKind:
1180 return
1181 AssumeSymInt(St, Assumption,
1182 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1183 isFeasible);
1184
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001185 case nonlval::ConcreteIntKind: {
1186 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001187 isFeasible = b ? Assumption : !Assumption;
1188 return St;
1189 }
1190 }
1191}
1192
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001193GRExprEngine::StateTy
1194GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001195 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001196
Ted Kremenek13f31562008-02-06 00:54:14 +00001197 // First, determine if sym == X, where X != V.
1198 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1199 isFeasible = *X != V;
1200 return St;
1201 }
1202
1203 // Second, determine if sym != V.
1204 if (St.isNotEqual(sym, V)) {
1205 isFeasible = true;
1206 return St;
1207 }
1208
1209 // If we reach here, sym is not a constant and we don't know if it is != V.
1210 // Make that assumption.
1211
1212 isFeasible = true;
1213 return StateMgr.AddNE(St, sym, V);
1214}
1215
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001216GRExprEngine::StateTy
1217GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001218 const llvm::APSInt& V, bool& isFeasible) {
1219
1220 // First, determine if sym == X, where X != V.
1221 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1222 isFeasible = *X == V;
1223 return St;
1224 }
1225
1226 // Second, determine if sym != V.
1227 if (St.isNotEqual(sym, V)) {
1228 isFeasible = false;
1229 return St;
1230 }
1231
1232 // If we reach here, sym is not a constant and we don't know if it is == V.
1233 // Make that assumption.
1234
1235 isFeasible = true;
1236 return StateMgr.AddEQ(St, sym, V);
1237}
Ted Kremenek90960972008-01-30 23:03:39 +00001238
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001239GRExprEngine::StateTy
1240GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001241 const SymIntConstraint& C, bool& isFeasible) {
1242
1243 switch (C.getOpcode()) {
1244 default:
1245 // No logic yet for other operators.
1246 return St;
1247
1248 case BinaryOperator::EQ:
1249 if (Assumption)
1250 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1251 else
1252 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1253
1254 case BinaryOperator::NE:
1255 if (Assumption)
1256 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1257 else
1258 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1259 }
1260}
1261
Ted Kremenek90960972008-01-30 23:03:39 +00001262//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001263// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001264//===----------------------------------------------------------------------===//
1265
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001266#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001267static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001268
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001269namespace llvm {
1270template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001271struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001272 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001273
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001274 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001275
1276 Out << "Variables:\\l";
1277
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001278 bool isFirst = true;
1279
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001280 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +00001281 E=St.vb_end(); I!=E;++I) {
1282
1283 if (isFirst)
1284 isFirst = false;
1285 else
1286 Out << "\\l";
1287
1288 Out << ' ' << I.getKey()->getName() << " : ";
1289 I.getData().print(Out);
1290 }
1291
1292 }
1293
Ted Kremenek17c5f112008-02-11 19:21:59 +00001294
Ted Kremenek6d409922008-02-13 18:06:44 +00001295 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001296
1297 bool isFirst = true;
1298
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001299 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001300 I != E;++I) {
1301
1302 if (isFirst) {
1303 Out << "\\l\\lSub-Expressions:\\l";
1304 isFirst = false;
1305 }
1306 else
1307 Out << "\\l";
1308
1309 Out << " (" << (void*) I.getKey() << ") ";
1310 I.getKey()->printPretty(Out);
1311 Out << " : ";
1312 I.getData().print(Out);
1313 }
1314 }
1315
Ted Kremenek6d409922008-02-13 18:06:44 +00001316 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001317
Ted Kremenek08cfd832008-02-08 21:10:02 +00001318 bool isFirst = true;
1319
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001320 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001321 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001322 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001323 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001324 isFirst = false;
1325 }
1326 else
1327 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001328
Ted Kremenek17c5f112008-02-11 19:21:59 +00001329 Out << " (" << (void*) I.getKey() << ") ";
1330 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001331 Out << " : ";
1332 I.getData().print(Out);
1333 }
1334 }
1335
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001336 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001337 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001338
1339 if (CE.isEmpty())
1340 return;
1341
1342 Out << "\\l\\|'==' constraints:";
1343
Ted Kremenek07baa252008-02-21 18:02:17 +00001344 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001345 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1346 }
1347
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001348 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001349 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001350
1351 if (NE.isEmpty())
1352 return;
1353
1354 Out << "\\l\\|'!=' constraints:";
1355
Ted Kremenek07baa252008-02-21 18:02:17 +00001356 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001357 I != EI; ++I){
1358
1359 Out << "\\l $" << I.getKey() << " : ";
1360 bool isFirst = true;
1361
1362 ValueState::IntSetTy::iterator J=I.getData().begin(),
1363 EJ=I.getData().end();
1364 for ( ; J != EJ; ++J) {
1365 if (isFirst) isFirst = false;
1366 else Out << ", ";
1367
1368 Out << (*J)->toString();
1369 }
1370 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001371 }
1372
1373 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1374
1375 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001376 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001377 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001378 GraphPrintCheckerState->isUninitStore(N) ||
1379 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001380 return "color=\"red\",style=\"filled\"";
1381
1382 return "";
1383 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001384
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001385 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001386 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001387
1388 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001389 ProgramPoint Loc = N->getLocation();
1390
1391 switch (Loc.getKind()) {
1392 case ProgramPoint::BlockEntranceKind:
1393 Out << "Block Entrance: B"
1394 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1395 break;
1396
1397 case ProgramPoint::BlockExitKind:
1398 assert (false);
1399 break;
1400
1401 case ProgramPoint::PostStmtKind: {
1402 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001403 Out << L.getStmt()->getStmtClassName() << ':'
1404 << (void*) L.getStmt() << ' ';
1405
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001406 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001407
1408 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1409 Out << "\\|Implicit-Null Dereference.\\l";
1410 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001411 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1412 Out << "\\|Explicit-Null Dereference.\\l";
1413 }
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001414 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1415 Out << "\\|Dereference of uninitialied value.\\l";
1416 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001417 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001418 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenekbf988d02008-02-19 00:22:37 +00001419 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001420
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001421 break;
1422 }
1423
1424 default: {
1425 const BlockEdge& E = cast<BlockEdge>(Loc);
1426 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1427 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001428
1429 if (Stmt* T = E.getSrc()->getTerminator()) {
1430 Out << "\\|Terminator: ";
1431 E.getSrc()->printTerminator(Out);
1432
Ted Kremenekaee121c2008-02-13 23:08:21 +00001433 if (isa<SwitchStmt>(T)) {
1434 Stmt* Label = E.getDst()->getLabel();
1435
1436 if (Label) {
1437 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1438 Out << "\\lcase ";
1439 C->getLHS()->printPretty(Out);
1440
1441 if (Stmt* RHS = C->getRHS()) {
1442 Out << " .. ";
1443 RHS->printPretty(Out);
1444 }
1445
1446 Out << ":";
1447 }
1448 else {
1449 assert (isa<DefaultStmt>(Label));
1450 Out << "\\ldefault:";
1451 }
1452 }
1453 else
1454 Out << "\\l(implicit) default:";
1455 }
1456 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001457 // FIXME
1458 }
1459 else {
1460 Out << "\\lCondition: ";
1461 if (*E.getSrc()->succ_begin() == E.getDst())
1462 Out << "true";
1463 else
1464 Out << "false";
1465 }
1466
1467 Out << "\\l";
1468 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001469
1470 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1471 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1472 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001473 }
1474 }
1475
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001476 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001477
Ted Kremenek17c5f112008-02-11 19:21:59 +00001478 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001479
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001480 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001481 return Out.str();
1482 }
1483};
1484} // end llvm namespace
1485#endif
1486
Ted Kremenek3862eb12008-02-14 22:36:46 +00001487void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001488#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001489 GraphPrintCheckerState = this;
1490 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001491 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001492#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001493}