blob: 6d45beab32771275612be5749f609a68ed6bf903 [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
27GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& 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 Kremenek7f5ebc72008-02-04 21:59:01 +000036 if (S == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(S);
38
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000042
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000043 return StateMgr.SetValue(St, S, isBlkExpr, V);
44}
45
Ted Kremenek30fa28b2008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
47GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekf20c2012008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
52 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
53 RetBuf.push_back(SetValue(St, S, *I));
54
55 return RetBuf;
56}
57
Ted Kremenek30fa28b2008-02-13 17:41:41 +000058GRExprEngine::StateTy
59GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060
Ted Kremenek0428e022008-02-08 03:02:48 +000061 if (LV.isUnknown())
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000062 return St;
63
64 if (!StateCleaned) {
65 St = RemoveDeadBindings(CurrentStmt, St);
66 StateCleaned = true;
67 }
68
69 return StateMgr.SetValue(St, LV, V);
70}
71
Ted Kremenek30fa28b2008-02-13 17:41:41 +000072void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek6ff3cea2008-01-29 23:32:35 +000073 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +000074
Ted Kremenek17c5f112008-02-11 19:21:59 +000075 // Remove old bindings for subexpressions.
76 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +000077
Ted Kremenek022b6052008-02-15 22:29:00 +000078 // Check for NULL conditions; e.g. "for(;;)"
79 if (!Condition) {
80 builder.markInfeasible(false);
81
82 // Get the current block counter.
83 GRBlockCounter BC = builder.getBlockCounter();
84 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
85 unsigned NumVisited = BC.getNumVisited(BlockID);
86
87 if (NumVisited < 1) builder.generateNode(PrevState, true);
88 else builder.markInfeasible(true);
89
90 return;
91 }
92
Ted Kremenek90960972008-01-30 23:03:39 +000093 RValue V = GetValue(PrevState, Condition);
94
95 switch (V.getBaseKind()) {
96 default:
97 break;
98
Ted Kremenek0428e022008-02-08 03:02:48 +000099 case RValue::UnknownKind:
Ted Kremenek90960972008-01-30 23:03:39 +0000100 builder.generateNode(PrevState, true);
101 builder.generateNode(PrevState, false);
102 return;
103
104 case RValue::UninitializedKind: {
105 NodeTy* N = builder.generateNode(PrevState, true);
106
107 if (N) {
108 N->markAsSink();
109 UninitBranches.insert(N);
110 }
111
112 builder.markInfeasible(false);
113 return;
114 }
115 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000116
Ted Kremenek4b170e52008-02-12 18:08:17 +0000117 // Get the current block counter.
118 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekfd85f292008-02-12 19:49:57 +0000119 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
120 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000121
Ted Kremenek4b170e52008-02-12 18:08:17 +0000122 if (isa<nonlval::ConcreteInt>(V) ||
123 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
124
125 // Process the true branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000126
Ted Kremenek4b170e52008-02-12 18:08:17 +0000127 bool isFeasible = true;
128
129 StateTy St = Assume(PrevState, V, true, isFeasible);
130
131 if (isFeasible)
132 builder.generateNode(St, true);
133 else
134 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000135 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000136 else
137 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000138
Ted Kremenekfd85f292008-02-12 19:49:57 +0000139 BlockID = builder.getTargetBlock(false)->getBlockID();
140 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000141
Ted Kremenek4b170e52008-02-12 18:08:17 +0000142 if (isa<nonlval::ConcreteInt>(V) ||
143 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
144
145 // Process the false branch.
146
147 bool isFeasible = false;
148
149 StateTy St = Assume(PrevState, V, false, isFeasible);
150
151 if (isFeasible)
152 builder.generateNode(St, false);
153 else
154 builder.markInfeasible(false);
155 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000156 else
157 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000158}
159
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000160/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000161/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000162void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000163
164 StateTy St = builder.getState();
165 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
166
167 // Three possibilities:
168 //
169 // (1) We know the computed label.
170 // (2) The label is NULL (or some other constant), or Uninitialized.
171 // (3) We have no clue about the label. Dispatch to all targets.
172 //
173
174 typedef IndirectGotoNodeBuilder::iterator iterator;
175
176 if (isa<lval::GotoLabel>(V)) {
177 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
178
179 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000180 if (I.getLabel() == L) {
181 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000182 return;
183 }
184 }
185
186 assert (false && "No block with label.");
187 return;
188 }
189
190 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
191 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000192 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000193 UninitBranches.insert(N);
194 return;
195 }
196
197 // This is really a catch-all. We don't support symbolics yet.
198
199 assert (isa<UnknownVal>(V));
200
201 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000202 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000203}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000204
Ted Kremenekaee121c2008-02-13 23:08:21 +0000205/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
206/// nodes by processing the 'effects' of a switch statement.
207void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
208
209 typedef SwitchNodeBuilder::iterator iterator;
210
211 StateTy St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000212 Expr* CondE = builder.getCondition();
213 NonLValue CondV = cast<NonLValue>(GetValue(St, CondE));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000214
215 if (isa<UninitializedVal>(CondV)) {
216 NodeTy* N = builder.generateDefaultCaseNode(St, true);
217 UninitBranches.insert(N);
218 return;
219 }
220
221 StateTy DefaultSt = St;
222
223 // While most of this can be assumed (such as the signedness), having it
224 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000225
226 unsigned bits = getContext().getTypeSize(CondE->getType(),
227 CondE->getExprLoc());
228
Ted Kremenekaee121c2008-02-13 23:08:21 +0000229 APSInt V1(bits, false);
230 APSInt V2 = V1;
231
232 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
233
234 CaseStmt* Case = cast<CaseStmt>(I.getCase());
235
236 // Evaluate the case.
237 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
238 assert (false && "Case condition must evaluate to an integer constant.");
239 return;
240 }
241
242 // Get the RHS of the case, if it exists.
243
244 if (Expr* E = Case->getRHS()) {
245 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
246 assert (false &&
247 "Case condition (RHS) must evaluate to an integer constant.");
248 return ;
249 }
250
251 assert (V1 <= V2);
252 }
253 else V2 = V1;
254
255 // FIXME: Eventually we should replace the logic below with a range
256 // comparison, rather than concretize the values within the range.
257 // This should be easy once we have "ranges" for NonLValues.
258
259 do {
260 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
261
Ted Kremenekd9268e32008-02-19 01:44:53 +0000262 NonLValue Res = EvalBinaryOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000263
264 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000265 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000266
Ted Kremenekb1934132008-02-14 19:37:24 +0000267 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000268
269 if (isFeasible) {
270 builder.generateCaseStmtNode(I, StNew);
271
272 // If CondV evaluates to a constant, then we know that this
273 // is the *only* case that we can take, so stop evaluating the
274 // others.
275 if (isa<nonlval::ConcreteInt>(CondV))
276 return;
277 }
278
279 // Now "assume" that the case doesn't match. Add this state
280 // to the default state (if it is feasible).
281
Ted Kremenekb1934132008-02-14 19:37:24 +0000282 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000283
284 if (isFeasible)
285 DefaultSt = StNew;
286
287 // Concretize the next value in the range.
288 ++V1;
289
290 } while (V1 < V2);
291 }
292
293 // If we reach here, than we know that the default branch is
294 // possible.
295 builder.generateDefaultCaseNode(DefaultSt);
296}
297
298
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000299void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000300 NodeSet& Dst) {
301
302 bool hasR2;
303 StateTy PrevState = Pred->getState();
304
305 RValue R1 = GetValue(PrevState, B->getLHS());
306 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000307
308 if (hasR2) {
309 if (isa<UninitializedVal>(R2) || isa<UnknownVal>(R2)) {
310 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
311 return;
312 }
313 }
314 else if (isa<UninitializedVal>(R1) || isa<UnknownVal>(R1)) {
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000315 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
316 return;
317 }
318
319 // R1 is an expression that can evaluate to either 'true' or 'false'.
320 if (B->getOpcode() == BinaryOperator::LAnd) {
321 // hasR2 == 'false' means that LHS evaluated to 'false' and that
322 // we short-circuited, leading to a value of '0' for the '&&' expression.
323 if (hasR2 == false) {
324 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
325 return;
326 }
327 }
328 else {
329 assert (B->getOpcode() == BinaryOperator::LOr);
330 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
331 // we short-circuited, leading to a value of '1' for the '||' expression.
332 if (hasR2 == false) {
333 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
334 return;
335 }
336 }
337
338 // If we reach here we did not short-circuit. Assume R2 == true and
339 // R2 == false.
340
341 bool isFeasible;
342 StateTy St = Assume(PrevState, R2, true, isFeasible);
343
344 if (isFeasible)
345 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
346
347 St = Assume(PrevState, R2, false, isFeasible);
348
349 if (isFeasible)
350 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
351}
352
353
354
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000355void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenek68d70a82008-01-15 23:55:06 +0000356 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000357
358 StmtEntryNode = builder.getLastNode();
359 CurrentStmt = S;
360 NodeSet Dst;
361 StateCleaned = false;
362
363 Visit(S, StmtEntryNode, Dst);
364
365 // If no nodes were generated, generate a new node that has all the
366 // dead mappings removed.
367 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
368 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
369 builder.generateNode(S, St, StmtEntryNode);
370 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000371
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);
386 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000387}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000388
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000389void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-02-05 19:35:18 +0000390 const StateTy::BufferTy& SB) {
391
392 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
393 Nodify(Dst, S, Pred, *I);
394}
395
Ted Kremenek6d409922008-02-13 18:06:44 +0000396void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000397 if (D != CurrentStmt) {
398 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
399 return;
400 }
401
402 // If we are here, we are loading the value of the decl and binding
403 // it to the block-level expression.
404
405 StateTy St = Pred->getState();
406
Ted Kremenek22640ce2008-02-15 22:09:30 +0000407 Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000408}
409
Ted Kremenekd9268e32008-02-19 01:44:53 +0000410void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
411 CallExpr::arg_iterator I, CallExpr::arg_iterator E,
412 NodeSet& Dst) {
413
414 if (I != E) {
415 NodeSet DstTmp;
416 Visit(*I, Pred, DstTmp);
417 ++I;
418
419 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI!=DE; ++DI)
420 VisitCall(CE, *DI, I, E, Dst);
421
422 return;
423 }
424
425 // If we reach here we have processed all of the arguments. Evaluate
426 // the callee expression.
427 NodeSet DstTmp;
428 Visit(CE->getCallee(), Pred, DstTmp);
429
430 // Finally, evaluate the function call.
431 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI!=DE; ++DI) {
432 StateTy St = (*DI)->getState();
433 LValue L = GetLValue(St, CE->getCallee());
434
435 // Check for uninitialized control-flow.
436 if (isa<UninitializedVal>(L)) {
437 NodeTy* N = Builder->generateNode(CE, St, *DI);
438 N->markAsSink();
439 UninitBranches.insert(N);
440 continue;
441 }
442
443 // Note: EvalCall must handle the case where the callee is "UnknownVal."
444 Nodify(Dst, CE, *DI, EvalCall(CE, (*DI)->getState()));
445 }
446}
447
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000448void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000449
450 QualType T = CastE->getType();
451
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000452 // Check for redundant casts or casting to "void"
453 if (T->isVoidType() ||
454 E->getType() == T ||
Ted Kremenekd9268e32008-02-19 01:44:53 +0000455 (T->isPointerType() && E->getType()->isFunctionType())) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000456 Dst.Add(Pred);
457 return;
458 }
459
460 NodeSet S1;
461 Visit(E, Pred, S1);
462
463 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
464 NodeTy* N = *I1;
465 StateTy St = N->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000466 const RValue& V = GetValue(St, E);
Ted Kremenek25a484d2008-02-14 18:28:23 +0000467 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000468 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000469}
470
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000471void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
472 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000473
474 StateTy St = Pred->getState();
475
476 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000477 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000478
479 // FIXME: Add support for local arrays.
480 if (VD->getType()->isArrayType())
481 continue;
482
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000483 const Expr* E = VD->getInit();
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000484 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenekadec14b2008-02-08 02:57:34 +0000485 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000486 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000487
488 Nodify(Dst, DS, Pred, St);
489
490 if (Dst.empty())
491 Dst.Add(Pred);
492}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000493
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000494
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000495void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000496 NodeTy* Pred, NodeSet& Dst) {
497
498 StateTy St = Pred->getState();
499
500 RValue R = GetValue(St, LHS);
Ted Kremenekadec14b2008-02-08 02:57:34 +0000501 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000502
503 Nodify(Dst, S, Pred, SetValue(St, S, R));
504}
505
Ted Kremenekfd85f292008-02-12 19:49:57 +0000506/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000507void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekfd85f292008-02-12 19:49:57 +0000508 NodeTy* Pred,
509 NodeSet& Dst) {
510
511 // 6.5.3.4 sizeof: "The result type is an integer."
512
513 QualType T = S->getArgumentType();
514
515 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000516 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000517 return;
518
519 SourceLocation L = S->getExprLoc();
520 uint64_t size = getContext().getTypeSize(T, L) / 8;
521
522 Nodify(Dst, S, Pred,
523 SetValue(Pred->getState(), S,
Ted Kremenek521d9722008-02-15 23:15:23 +0000524 NonLValue::GetValue(ValMgr, size, S->getType(), L)));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000525
526}
527
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000528void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
529 GRExprEngine::NodeTy* Pred,
530 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000531
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000532 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000533 UnaryOperator::Opcode Op = U->getOpcode();
534
535 // FIXME: This is a hack so that for '*' and '&' we don't recurse
536 // on visiting the subexpression if it is a DeclRefExpr. We should
537 // probably just handle AddrOf and Deref in their own methods to make
538 // this cleaner.
539 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
540 isa<DeclRefExpr>(U->getSubExpr()))
541 S1.Add(Pred);
542 else
543 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000544
545 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
546 NodeTy* N1 = *I1;
547 StateTy St = N1->getState();
548
Ted Kremenek22640ce2008-02-15 22:09:30 +0000549 // Handle ++ and -- (both pre- and post-increment).
550
551 if (U->isIncrementDecrementOp()) {
552 const LValue& L1 = GetLValue(St, U->getSubExpr());
553 RValue R1 = GetValue(St, L1);
554
555 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
556 : BinaryOperator::Sub;
557
Ted Kremenekd9268e32008-02-19 01:44:53 +0000558 RValue Result = EvalBinaryOp(Op, R1, GetRValueConstant(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000559
560 if (U->isPostfix())
561 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
562 else
563 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
564
565 continue;
566 }
567
568 // Handle all other unary operators.
569
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000570 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000571
Ted Kremenekcacd7362008-01-24 08:20:02 +0000572 case UnaryOperator::Minus: {
Ted Kremenek0922ec82008-01-28 22:09:13 +0000573 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000574 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000575 break;
576 }
577
Ted Kremenek2cb46642008-02-04 16:58:30 +0000578 case UnaryOperator::Not: {
579 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000580 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenek2cb46642008-02-04 16:58:30 +0000581 break;
582 }
583
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000584 case UnaryOperator::LNot: {
585 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
586 //
587 // Note: technically we do "E == 0", but this is the same in the
588 // transfer functions as "0 == E".
589
590 RValue V1 = GetValue(St, U->getSubExpr());
591
592 if (isa<LValue>(V1)) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000593 const LValue& L1 = cast<LValue>(V1);
594 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
595 Nodify(Dst, U, N1,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000596 SetValue(St, U, EvalBinaryOp(BinaryOperator::EQ,
Ted Kremenekb1934132008-02-14 19:37:24 +0000597 L1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000598 }
599 else {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000600 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000601 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000602 Nodify(Dst, U, N1,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000603 SetValue(St, U, EvalBinaryOp(BinaryOperator::EQ,
Ted Kremenekb1934132008-02-14 19:37:24 +0000604 R1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000605 }
606
607 break;
608 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000609
610 case UnaryOperator::SizeOf: {
611 // 6.5.3.4 sizeof: "The result type is an integer."
612
613 QualType T = U->getSubExpr()->getType();
614
615 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000616 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000617 return;
618
619 SourceLocation L = U->getExprLoc();
620 uint64_t size = getContext().getTypeSize(T, L) / 8;
621
622 Nodify(Dst, U, N1,
623 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
Ted Kremenek521d9722008-02-15 23:15:23 +0000624 U->getType(), L)));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000625
626 break;
627 }
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000628
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000629 case UnaryOperator::AddrOf: {
630 const LValue& L1 = GetLValue(St, U->getSubExpr());
631 Nodify(Dst, U, N1, SetValue(St, U, L1));
632 break;
633 }
634
635 case UnaryOperator::Deref: {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000636 // FIXME: Stop when dereferencing an uninitialized value.
637 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
638
Ted Kremenekbc965a62008-02-18 22:57:02 +0000639 const RValue& V = GetValue(St, U->getSubExpr());
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000640 const LValue& L1 = cast<LValue>(V);
641
Ted Kremenek80d52d02008-02-07 05:48:01 +0000642 // After a dereference, one of two possible situations arise:
643 // (1) A crash, because the pointer was NULL.
644 // (2) The pointer is not NULL, and the dereference works.
645 //
646 // We add these assumptions.
647
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000648 bool isFeasibleNotNull;
649
Ted Kremenek80d52d02008-02-07 05:48:01 +0000650 // "Assume" that the pointer is Not-NULL.
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000651 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
652
653 if (isFeasibleNotNull) {
Ted Kremenek80d52d02008-02-07 05:48:01 +0000654 QualType T = U->getType();
655 Nodify(Dst, U, N1, SetValue(StNotNull, U,
656 GetValue(StNotNull, L1, &T)));
657 }
658
Ted Kremenekbc965a62008-02-18 22:57:02 +0000659 if (V.isUnknown())
660 return;
661
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000662 bool isFeasibleNull;
663
664 // "Assume" that the pointer is NULL.
665 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
666
667 if (isFeasibleNull) {
Ted Kremenek02b5b402008-02-07 15:20:13 +0000668 // We don't use "Nodify" here because the node will be a sink
669 // and we have no intention of processing it later.
670 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
671
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000672 if (NullNode) {
673 NullNode->markAsSink();
674
675 if (isFeasibleNotNull)
676 ImplicitNullDeref.insert(NullNode);
677 else
678 ExplicitNullDeref.insert(NullNode);
679 }
680 }
681
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000682 break;
683 }
684
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000685 default: ;
686 assert (false && "Not implemented.");
687 }
688 }
689}
690
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000691void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
692 GRExprEngine::NodeSet& Dst) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000693
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000694 if (isa<DeclRefExpr>(E)) {
695 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000696 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000697 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000698
699 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
700 if (U->getOpcode() == UnaryOperator::Deref) {
701 Visit(U->getSubExpr(), Pred, Dst);
702 return;
703 }
704 }
705
706 Visit(E, Pred, Dst);
707}
708
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000709void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000710 GRExprEngine::NodeTy* Pred,
711 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000712 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000713
714 if (B->isAssignmentOp())
715 VisitAssignmentLHS(B->getLHS(), Pred, S1);
716 else
717 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000718
Ted Kremenekf031b872008-01-23 19:59:44 +0000719 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
720 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000721
Ted Kremenekf031b872008-01-23 19:59:44 +0000722 // When getting the value for the LHS, check if we are in an assignment.
723 // In such cases, we want to (initially) treat the LHS as an LValue,
724 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenek0922ec82008-01-28 22:09:13 +0000725 // evaluated to LValueDecl's instead of to an NonLValue.
726 const RValue& V1 =
Ted Kremenekf031b872008-01-23 19:59:44 +0000727 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
728 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000729
Ted Kremenekf031b872008-01-23 19:59:44 +0000730 NodeSet S2;
731 Visit(B->getRHS(), N1, S2);
732
733 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000734
Ted Kremenekf031b872008-01-23 19:59:44 +0000735 NodeTy* N2 = *I2;
736 StateTy St = N2->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000737 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekf031b872008-01-23 19:59:44 +0000738
Ted Kremenek15cb0782008-02-06 22:50:25 +0000739 BinaryOperator::Opcode Op = B->getOpcode();
740
741 if (Op <= BinaryOperator::Or) {
742
Ted Kremenekadec14b2008-02-08 02:57:34 +0000743 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000744 Nodify(Dst, B, N2, SetValue(St, B, V1));
745 continue;
746 }
747
Ted Kremenekd9268e32008-02-19 01:44:53 +0000748 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(Op, V1, V2)));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000749 continue;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000750
Ted Kremenek15cb0782008-02-06 22:50:25 +0000751 }
752
753 switch (Op) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000754 case BinaryOperator::Assign: {
755 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000756
757 if (isa<UninitializedVal>(L1))
758 HandleUninitializedStore(B, N2);
759 else
760 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
761
Ted Kremenekf031b872008-01-23 19:59:44 +0000762 break;
763 }
764
Ted Kremenek15cb0782008-02-06 22:50:25 +0000765 default: { // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +0000766
Ted Kremenek15cb0782008-02-06 22:50:25 +0000767 assert (B->isCompoundAssignmentOp());
768
769 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000770
771 if (isa<UninitializedVal>(L1)) {
772 HandleUninitializedStore(B, N2);
773 break;
774 }
775
Ted Kremenekadec14b2008-02-08 02:57:34 +0000776 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000777
Ted Kremenek106f37c2008-02-08 07:05:39 +0000778 if (Op >= BinaryOperator::AndAssign)
779 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
780 else
Ted Kremenek22640ce2008-02-15 22:09:30 +0000781 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000782
Ted Kremenek22640ce2008-02-15 22:09:30 +0000783 if (B->getType()->isPointerType()) { // Perform pointer arithmetic.
784 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000785 Result = EvalBinaryOp(Op, L1, R2);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000786 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000787 else if (isa<LValue>(V2)) {
Ted Kremenek19072e02008-01-29 19:43:15 +0000788 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek022b6052008-02-15 22:29:00 +0000789
790 if (B->getRHS()->getType()->isPointerType()) {
791 // LValue comparison.
Ted Kremenekd9268e32008-02-19 01:44:53 +0000792 Result = EvalBinaryOp(Op, L1, L2);
Ted Kremenek022b6052008-02-15 22:29:00 +0000793 }
794 else {
Ted Kremenek521d9722008-02-15 23:15:23 +0000795 QualType T1 = B->getLHS()->getType();
796 QualType T2 = B->getRHS()->getType();
797
Ted Kremenek022b6052008-02-15 22:29:00 +0000798 // An operation between two variables of a non-lvalue type.
799 Result =
Ted Kremenekd9268e32008-02-19 01:44:53 +0000800 EvalBinaryOp(Op,
Ted Kremenek521d9722008-02-15 23:15:23 +0000801 cast<NonLValue>(GetValue(N1->getState(), L1, &T1)),
802 cast<NonLValue>(GetValue(N2->getState(), L2, &T2)));
Ted Kremenek022b6052008-02-15 22:29:00 +0000803 }
Ted Kremenek19072e02008-01-29 19:43:15 +0000804 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000805 else { // Any other operation between two Non-LValues.
Ted Kremenek521d9722008-02-15 23:15:23 +0000806 QualType T = B->getLHS()->getType();
807 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(),
808 L1, &T));
Ted Kremenek19072e02008-01-29 19:43:15 +0000809 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000810 Result = EvalBinaryOp(Op, R1, R2);
Ted Kremenek19072e02008-01-29 19:43:15 +0000811 }
812
Ted Kremenek15cb0782008-02-06 22:50:25 +0000813 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekf031b872008-01-23 19:59:44 +0000814 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000815 }
Ted Kremenekf031b872008-01-23 19:59:44 +0000816 }
Ted Kremenekafba4b22008-01-16 00:53:15 +0000817 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000818 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000819}
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000820
Ted Kremenekbf988d02008-02-19 00:22:37 +0000821void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
822
823 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
824 N->markAsSink();
825 UninitStores.insert(N);
826}
Ted Kremenekbe962452008-01-16 19:42:59 +0000827
Ted Kremenekbf988d02008-02-19 00:22:37 +0000828void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000829
830 // FIXME: add metadata to the CFG so that we can disable
831 // this check when we KNOW that there is no block-level subexpression.
832 // The motivation is that this check requires a hashtable lookup.
833
834 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
835 Dst.Add(Pred);
836 return;
837 }
838
839 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000840
841 default:
842 // Cases we intentionally have "default" handle:
Ted Kremenek18044ff2008-02-19 02:01:16 +0000843 // AddrLabelExpr
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000844
845 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
846 break;
847
Ted Kremenek744a7862008-02-08 20:29:23 +0000848 case Stmt::BinaryOperatorClass: {
849 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000850
Ted Kremenek744a7862008-02-08 20:29:23 +0000851 if (B->isLogicalOp()) {
852 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000853 break;
854 }
Ted Kremenek744a7862008-02-08 20:29:23 +0000855 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +0000856 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000857 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000858 break;
859 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000860
861 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
862 break;
863 }
Ted Kremenekd9268e32008-02-19 01:44:53 +0000864
865 case Stmt::CallExprClass: {
866 CallExpr* C = cast<CallExpr>(S);
867 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
868 break;
869 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000870
871 case Stmt::CastExprClass: {
872 CastExpr* C = cast<CastExpr>(S);
873 VisitCast(C, C->getSubExpr(), Pred, Dst);
874 break;
Ted Kremenek744a7862008-02-08 20:29:23 +0000875 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000876
Ted Kremenek18044ff2008-02-19 02:01:16 +0000877 // While explicitly creating a node+state for visiting a CharacterLiteral
878 // seems wasteful, it also solves a bunch of problems when handling
879 // the ?, &&, and ||.
880
881 case Stmt::CharacterLiteralClass: {
882 CharacterLiteral* C = cast<CharacterLiteral>(S);
883 StateTy St = Pred->getState();
884 NonLValue X = NonLValue::GetValue(ValMgr, C->getValue(), C->getType(),
885 C->getLoc());
886 Nodify(Dst, C, Pred, SetValue(St, C, X));
887 break;
888 }
889
Ted Kremenekfd85f292008-02-12 19:49:57 +0000890 case Stmt::ChooseExprClass: { // __builtin_choose_expr
891 ChooseExpr* C = cast<ChooseExpr>(S);
892 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
893 break;
894 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000895
Ted Kremenek9914e9c2008-01-23 23:38:00 +0000896 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +0000897 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
898 break;
899
Ted Kremenekfd85f292008-02-12 19:49:57 +0000900 case Stmt::ConditionalOperatorClass: { // '?' operator
901 ConditionalOperator* C = cast<ConditionalOperator>(S);
902 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
903 break;
904 }
905
906 case Stmt::DeclRefExprClass:
907 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
908 break;
909
910 case Stmt::DeclStmtClass:
911 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
912 break;
913
Ted Kremenek18044ff2008-02-19 02:01:16 +0000914 // While explicitly creating a node+state for visiting an IntegerLiteral
915 // seems wasteful, it also solves a bunch of problems when handling
916 // the ?, &&, and ||.
917
918 case Stmt::IntegerLiteralClass: {
919 StateTy St = Pred->getState();
920 IntegerLiteral* I = cast<IntegerLiteral>(S);
921 NonLValue X = NonLValue::GetValue(ValMgr, I);
922 Nodify(Dst, I, Pred, SetValue(St, I, X));
923 break;
924 }
925
Ted Kremenekfd85f292008-02-12 19:49:57 +0000926 case Stmt::ImplicitCastExprClass: {
927 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
928 VisitCast(C, C->getSubExpr(), Pred, Dst);
929 break;
930 }
931
932 case Stmt::ParenExprClass:
933 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
934 break;
935
936 case Stmt::SizeOfAlignOfTypeExprClass:
937 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
938 break;
939
Ted Kremenek106f37c2008-02-08 07:05:39 +0000940 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +0000941 StmtExpr* SE = cast<StmtExpr>(S);
942
Ted Kremenek106f37c2008-02-08 07:05:39 +0000943 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000944 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
945 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000946 break;
947 }
948
Ted Kremenekfd85f292008-02-12 19:49:57 +0000949 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000950 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
951 Visit(R, Pred, Dst);
952 else
953 Dst.Add(Pred);
954
955 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000956 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000957
Ted Kremenekfd85f292008-02-12 19:49:57 +0000958 case Stmt::UnaryOperatorClass:
959 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000960 break;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000961 }
Ted Kremenekbe962452008-01-16 19:42:59 +0000962}
963
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000964//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +0000965// "Assume" logic.
966//===----------------------------------------------------------------------===//
967
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000968GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +0000969 bool Assumption,
970 bool& isFeasible) {
971
972 assert (!isa<UninitializedVal>(Cond));
973
974 if (isa<UnknownVal>(Cond)) {
975 isFeasible = true;
976 return St;
977 }
Ted Kremenek6e24a802008-02-01 06:36:40 +0000978
979 switch (Cond.getSubKind()) {
980 default:
Ted Kremenek13f31562008-02-06 00:54:14 +0000981 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremenek6e24a802008-02-01 06:36:40 +0000982 return St;
983
Ted Kremenek13f31562008-02-06 00:54:14 +0000984 case lval::SymbolValKind:
985 if (Assumption)
986 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
987 ValMgr.getZeroWithPtrWidth(), isFeasible);
988 else
989 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
990 ValMgr.getZeroWithPtrWidth(), isFeasible);
991
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000992
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000993 case lval::DeclValKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +0000994 isFeasible = Assumption;
995 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +0000996
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000997 case lval::ConcreteIntKind: {
998 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +0000999 isFeasible = b ? Assumption : !Assumption;
1000 return St;
1001 }
1002 }
Ted Kremenek90960972008-01-30 23:03:39 +00001003}
1004
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001005GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001006 bool Assumption,
Ted Kremenek72197902008-01-31 19:34:24 +00001007 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001008
Ted Kremenekbc965a62008-02-18 22:57:02 +00001009 assert (!isa<UninitializedVal>(Cond));
1010
1011 if (isa<UnknownVal>(Cond)) {
1012 isFeasible = true;
1013 return St;
1014 }
1015
Ted Kremenek90960972008-01-30 23:03:39 +00001016 switch (Cond.getSubKind()) {
1017 default:
1018 assert (false && "'Assume' not implemented for this NonLValue.");
1019 return St;
1020
Ted Kremenekab359c12008-02-06 17:32:17 +00001021
1022 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001023 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001024 SymbolID sym = SV.getSymbol();
1025
1026 if (Assumption)
1027 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1028 isFeasible);
1029 else
1030 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1031 isFeasible);
1032 }
1033
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001034 case nonlval::SymIntConstraintValKind:
1035 return
1036 AssumeSymInt(St, Assumption,
1037 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1038 isFeasible);
1039
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001040 case nonlval::ConcreteIntKind: {
1041 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001042 isFeasible = b ? Assumption : !Assumption;
1043 return St;
1044 }
1045 }
1046}
1047
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001048GRExprEngine::StateTy
1049GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001050 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001051
Ted Kremenek13f31562008-02-06 00:54:14 +00001052 // First, determine if sym == X, where X != V.
1053 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1054 isFeasible = *X != V;
1055 return St;
1056 }
1057
1058 // Second, determine if sym != V.
1059 if (St.isNotEqual(sym, V)) {
1060 isFeasible = true;
1061 return St;
1062 }
1063
1064 // If we reach here, sym is not a constant and we don't know if it is != V.
1065 // Make that assumption.
1066
1067 isFeasible = true;
1068 return StateMgr.AddNE(St, sym, V);
1069}
1070
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001071GRExprEngine::StateTy
1072GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001073 const llvm::APSInt& V, bool& isFeasible) {
1074
1075 // First, determine if sym == X, where X != V.
1076 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1077 isFeasible = *X == V;
1078 return St;
1079 }
1080
1081 // Second, determine if sym != V.
1082 if (St.isNotEqual(sym, V)) {
1083 isFeasible = false;
1084 return St;
1085 }
1086
1087 // If we reach here, sym is not a constant and we don't know if it is == V.
1088 // Make that assumption.
1089
1090 isFeasible = true;
1091 return StateMgr.AddEQ(St, sym, V);
1092}
Ted Kremenek90960972008-01-30 23:03:39 +00001093
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001094GRExprEngine::StateTy
1095GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001096 const SymIntConstraint& C, bool& isFeasible) {
1097
1098 switch (C.getOpcode()) {
1099 default:
1100 // No logic yet for other operators.
1101 return St;
1102
1103 case BinaryOperator::EQ:
1104 if (Assumption)
1105 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1106 else
1107 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1108
1109 case BinaryOperator::NE:
1110 if (Assumption)
1111 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1112 else
1113 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1114 }
1115}
1116
Ted Kremenek90960972008-01-30 23:03:39 +00001117//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001118// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001119//===----------------------------------------------------------------------===//
1120
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001121#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001122static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001123
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001124namespace llvm {
1125template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001126struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001127 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001128
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001129 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001130
1131 Out << "Variables:\\l";
1132
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001133 bool isFirst = true;
1134
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001135 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +00001136 E=St.vb_end(); I!=E;++I) {
1137
1138 if (isFirst)
1139 isFirst = false;
1140 else
1141 Out << "\\l";
1142
1143 Out << ' ' << I.getKey()->getName() << " : ";
1144 I.getData().print(Out);
1145 }
1146
1147 }
1148
Ted Kremenek17c5f112008-02-11 19:21:59 +00001149
Ted Kremenek6d409922008-02-13 18:06:44 +00001150 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001151
1152 bool isFirst = true;
1153
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001154 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001155 I != E;++I) {
1156
1157 if (isFirst) {
1158 Out << "\\l\\lSub-Expressions:\\l";
1159 isFirst = false;
1160 }
1161 else
1162 Out << "\\l";
1163
1164 Out << " (" << (void*) I.getKey() << ") ";
1165 I.getKey()->printPretty(Out);
1166 Out << " : ";
1167 I.getData().print(Out);
1168 }
1169 }
1170
Ted Kremenek6d409922008-02-13 18:06:44 +00001171 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001172
Ted Kremenek08cfd832008-02-08 21:10:02 +00001173 bool isFirst = true;
1174
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001175 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001176 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001177 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001178 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001179 isFirst = false;
1180 }
1181 else
1182 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001183
Ted Kremenek17c5f112008-02-11 19:21:59 +00001184 Out << " (" << (void*) I.getKey() << ") ";
1185 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001186 Out << " : ";
1187 I.getData().print(Out);
1188 }
1189 }
1190
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001191 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001192 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1193
1194 if (CE.isEmpty())
1195 return;
1196
1197 Out << "\\l\\|'==' constraints:";
1198
1199 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1200 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1201 }
1202
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001203 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001204 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1205
1206 if (NE.isEmpty())
1207 return;
1208
1209 Out << "\\l\\|'!=' constraints:";
1210
1211 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1212 I != EI; ++I){
1213
1214 Out << "\\l $" << I.getKey() << " : ";
1215 bool isFirst = true;
1216
1217 ValueState::IntSetTy::iterator J=I.getData().begin(),
1218 EJ=I.getData().end();
1219 for ( ; J != EJ; ++J) {
1220 if (isFirst) isFirst = false;
1221 else Out << ", ";
1222
1223 Out << (*J)->toString();
1224 }
1225 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001226 }
1227
1228 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1229
1230 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001231 GraphPrintCheckerState->isExplicitNullDeref(N) ||
1232 GraphPrintCheckerState->isUninitStore(N) ||
1233 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001234 return "color=\"red\",style=\"filled\"";
1235
1236 return "";
1237 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001238
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001239 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001240 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001241
1242 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001243 ProgramPoint Loc = N->getLocation();
1244
1245 switch (Loc.getKind()) {
1246 case ProgramPoint::BlockEntranceKind:
1247 Out << "Block Entrance: B"
1248 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1249 break;
1250
1251 case ProgramPoint::BlockExitKind:
1252 assert (false);
1253 break;
1254
1255 case ProgramPoint::PostStmtKind: {
1256 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001257 Out << L.getStmt()->getStmtClassName() << ':'
1258 << (void*) L.getStmt() << ' ';
1259
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001260 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001261
1262 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1263 Out << "\\|Implicit-Null Dereference.\\l";
1264 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001265 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1266 Out << "\\|Explicit-Null Dereference.\\l";
1267 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001268 else if (GraphPrintCheckerState->isUninitStore(N)) {
1269 Out << "\\|Store to Uninitialized LValue.";
1270 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001271
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001272 break;
1273 }
1274
1275 default: {
1276 const BlockEdge& E = cast<BlockEdge>(Loc);
1277 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1278 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001279
1280 if (Stmt* T = E.getSrc()->getTerminator()) {
1281 Out << "\\|Terminator: ";
1282 E.getSrc()->printTerminator(Out);
1283
Ted Kremenekaee121c2008-02-13 23:08:21 +00001284 if (isa<SwitchStmt>(T)) {
1285 Stmt* Label = E.getDst()->getLabel();
1286
1287 if (Label) {
1288 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1289 Out << "\\lcase ";
1290 C->getLHS()->printPretty(Out);
1291
1292 if (Stmt* RHS = C->getRHS()) {
1293 Out << " .. ";
1294 RHS->printPretty(Out);
1295 }
1296
1297 Out << ":";
1298 }
1299 else {
1300 assert (isa<DefaultStmt>(Label));
1301 Out << "\\ldefault:";
1302 }
1303 }
1304 else
1305 Out << "\\l(implicit) default:";
1306 }
1307 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001308 // FIXME
1309 }
1310 else {
1311 Out << "\\lCondition: ";
1312 if (*E.getSrc()->succ_begin() == E.getDst())
1313 Out << "true";
1314 else
1315 Out << "false";
1316 }
1317
1318 Out << "\\l";
1319 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001320
1321 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1322 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1323 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001324 }
1325 }
1326
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001327 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001328
Ted Kremenek17c5f112008-02-11 19:21:59 +00001329 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001330
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001331 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001332 return Out.str();
1333 }
1334};
1335} // end llvm namespace
1336#endif
1337
Ted Kremenek3862eb12008-02-14 22:36:46 +00001338void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001339#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001340 GraphPrintCheckerState = this;
1341 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001342 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001343#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001344}