blob: 3cd490e330b78ee9699b1cb6ae874a2eafbf4e50 [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 Kremenek90960972008-01-30 23:03:39 +000078 RValue V = GetValue(PrevState, Condition);
79
80 switch (V.getBaseKind()) {
81 default:
82 break;
83
Ted Kremenek0428e022008-02-08 03:02:48 +000084 case RValue::UnknownKind:
Ted Kremenek90960972008-01-30 23:03:39 +000085 builder.generateNode(PrevState, true);
86 builder.generateNode(PrevState, false);
87 return;
88
89 case RValue::UninitializedKind: {
90 NodeTy* N = builder.generateNode(PrevState, true);
91
92 if (N) {
93 N->markAsSink();
94 UninitBranches.insert(N);
95 }
96
97 builder.markInfeasible(false);
98 return;
99 }
100 }
101
Ted Kremenek4b170e52008-02-12 18:08:17 +0000102 // Get the current block counter.
103 GRBlockCounter BC = builder.getBlockCounter();
104
Ted Kremenekfd85f292008-02-12 19:49:57 +0000105 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
106 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000107
Ted Kremenek4b170e52008-02-12 18:08:17 +0000108 if (isa<nonlval::ConcreteInt>(V) ||
109 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
110
111 // Process the true branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000112
Ted Kremenek4b170e52008-02-12 18:08:17 +0000113 bool isFeasible = true;
114
115 StateTy St = Assume(PrevState, V, true, isFeasible);
116
117 if (isFeasible)
118 builder.generateNode(St, true);
119 else
120 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000121 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000122 else
123 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000124
Ted Kremenekfd85f292008-02-12 19:49:57 +0000125 BlockID = builder.getTargetBlock(false)->getBlockID();
126 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000127
Ted Kremenek4b170e52008-02-12 18:08:17 +0000128 if (isa<nonlval::ConcreteInt>(V) ||
129 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
130
131 // Process the false branch.
132
133 bool isFeasible = false;
134
135 StateTy St = Assume(PrevState, V, false, isFeasible);
136
137 if (isFeasible)
138 builder.generateNode(St, false);
139 else
140 builder.markInfeasible(false);
141 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000142 else
143 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000144}
145
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000146/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000147/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000148void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000149
150 StateTy St = builder.getState();
151 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
152
153 // Three possibilities:
154 //
155 // (1) We know the computed label.
156 // (2) The label is NULL (or some other constant), or Uninitialized.
157 // (3) We have no clue about the label. Dispatch to all targets.
158 //
159
160 typedef IndirectGotoNodeBuilder::iterator iterator;
161
162 if (isa<lval::GotoLabel>(V)) {
163 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
164
165 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000166 if (I.getLabel() == L) {
167 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000168 return;
169 }
170 }
171
172 assert (false && "No block with label.");
173 return;
174 }
175
176 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
177 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000178 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000179 UninitBranches.insert(N);
180 return;
181 }
182
183 // This is really a catch-all. We don't support symbolics yet.
184
185 assert (isa<UnknownVal>(V));
186
187 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000188 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000189}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000190
Ted Kremenekaee121c2008-02-13 23:08:21 +0000191/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
192/// nodes by processing the 'effects' of a switch statement.
193void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
194
195 typedef SwitchNodeBuilder::iterator iterator;
196
197 StateTy St = builder.getState();
198 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
199
200 if (isa<UninitializedVal>(CondV)) {
201 NodeTy* N = builder.generateDefaultCaseNode(St, true);
202 UninitBranches.insert(N);
203 return;
204 }
205
206 StateTy DefaultSt = St;
207
208 // While most of this can be assumed (such as the signedness), having it
209 // just computed makes sure everything makes the same assumptions end-to-end.
210 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
211 APSInt V1(bits, false);
212 APSInt V2 = V1;
213
214 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
215
216 CaseStmt* Case = cast<CaseStmt>(I.getCase());
217
218 // Evaluate the case.
219 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
220 assert (false && "Case condition must evaluate to an integer constant.");
221 return;
222 }
223
224 // Get the RHS of the case, if it exists.
225
226 if (Expr* E = Case->getRHS()) {
227 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
228 assert (false &&
229 "Case condition (RHS) must evaluate to an integer constant.");
230 return ;
231 }
232
233 assert (V1 <= V2);
234 }
235 else V2 = V1;
236
237 // FIXME: Eventually we should replace the logic below with a range
238 // comparison, rather than concretize the values within the range.
239 // This should be easy once we have "ranges" for NonLValues.
240
241 do {
242 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
243
Ted Kremenekb1934132008-02-14 19:37:24 +0000244 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000245
246 // Now "assume" that the case matches.
247 bool isFeasible;
248
Ted Kremenekb1934132008-02-14 19:37:24 +0000249 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000250
251 if (isFeasible) {
252 builder.generateCaseStmtNode(I, StNew);
253
254 // If CondV evaluates to a constant, then we know that this
255 // is the *only* case that we can take, so stop evaluating the
256 // others.
257 if (isa<nonlval::ConcreteInt>(CondV))
258 return;
259 }
260
261 // Now "assume" that the case doesn't match. Add this state
262 // to the default state (if it is feasible).
263
Ted Kremenekb1934132008-02-14 19:37:24 +0000264 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000265
266 if (isFeasible)
267 DefaultSt = StNew;
268
269 // Concretize the next value in the range.
270 ++V1;
271
272 } while (V1 < V2);
273 }
274
275 // If we reach here, than we know that the default branch is
276 // possible.
277 builder.generateDefaultCaseNode(DefaultSt);
278}
279
280
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000281void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000282 NodeSet& Dst) {
283
284 bool hasR2;
285 StateTy PrevState = Pred->getState();
286
287 RValue R1 = GetValue(PrevState, B->getLHS());
288 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
289
Ted Kremenekadec14b2008-02-08 02:57:34 +0000290 if (isa<UnknownVal>(R1) &&
291 (isa<UnknownVal>(R2) ||
292 isa<UninitializedVal>(R2))) {
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000293
294 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
295 return;
296 }
Ted Kremenekadec14b2008-02-08 02:57:34 +0000297 else if (isa<UninitializedVal>(R1)) {
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000298 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
299 return;
300 }
301
302 // R1 is an expression that can evaluate to either 'true' or 'false'.
303 if (B->getOpcode() == BinaryOperator::LAnd) {
304 // hasR2 == 'false' means that LHS evaluated to 'false' and that
305 // we short-circuited, leading to a value of '0' for the '&&' expression.
306 if (hasR2 == false) {
307 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
308 return;
309 }
310 }
311 else {
312 assert (B->getOpcode() == BinaryOperator::LOr);
313 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
314 // we short-circuited, leading to a value of '1' for the '||' expression.
315 if (hasR2 == false) {
316 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
317 return;
318 }
319 }
320
321 // If we reach here we did not short-circuit. Assume R2 == true and
322 // R2 == false.
323
324 bool isFeasible;
325 StateTy St = Assume(PrevState, R2, true, isFeasible);
326
327 if (isFeasible)
328 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
329
330 St = Assume(PrevState, R2, false, isFeasible);
331
332 if (isFeasible)
333 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
334}
335
336
337
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000338void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenek68d70a82008-01-15 23:55:06 +0000339 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000340
341 StmtEntryNode = builder.getLastNode();
342 CurrentStmt = S;
343 NodeSet Dst;
344 StateCleaned = false;
345
346 Visit(S, StmtEntryNode, Dst);
347
348 // If no nodes were generated, generate a new node that has all the
349 // dead mappings removed.
350 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
351 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
352 builder.generateNode(S, St, StmtEntryNode);
353 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000354
Ted Kremenekf031b872008-01-23 19:59:44 +0000355 CurrentStmt = NULL;
356 StmtEntryNode = NULL;
357 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000358}
359
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000360GRExprEngine::NodeTy*
361GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000362
363 // If the state hasn't changed, don't generate a new node.
Ted Kremenek02b5b402008-02-07 15:20:13 +0000364 if (St == Pred->getState())
Ted Kremenek80d52d02008-02-07 05:48:01 +0000365 return NULL;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000366
Ted Kremenek80d52d02008-02-07 05:48:01 +0000367 NodeTy* N = Builder->generateNode(S, St, Pred);
368 Dst.Add(N);
369 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000370}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000371
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000372void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-02-05 19:35:18 +0000373 const StateTy::BufferTy& SB) {
374
375 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
376 Nodify(Dst, S, Pred, *I);
377}
378
Ted Kremenek6d409922008-02-13 18:06:44 +0000379void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000380 if (D != CurrentStmt) {
381 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
382 return;
383 }
384
385 // If we are here, we are loading the value of the decl and binding
386 // it to the block-level expression.
387
388 StateTy St = Pred->getState();
389
Ted Kremenek22640ce2008-02-15 22:09:30 +0000390 Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000391}
392
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000393void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000394
395 QualType T = CastE->getType();
396
397 // Check for redundant casts.
398 if (E->getType() == T) {
399 Dst.Add(Pred);
400 return;
401 }
402
403 NodeSet S1;
404 Visit(E, Pred, S1);
405
406 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
407 NodeTy* N = *I1;
408 StateTy St = N->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000409 const RValue& V = GetValue(St, E);
Ted Kremenek25a484d2008-02-14 18:28:23 +0000410 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000411 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000412}
413
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000414void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
415 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000416
417 StateTy St = Pred->getState();
418
419 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000420 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
421 const Expr* E = VD->getInit();
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000422 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenekadec14b2008-02-08 02:57:34 +0000423 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000424 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000425
426 Nodify(Dst, DS, Pred, St);
427
428 if (Dst.empty())
429 Dst.Add(Pred);
430}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000431
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000432
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000433void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000434 NodeTy* Pred, NodeSet& Dst) {
435
436 StateTy St = Pred->getState();
437
438 RValue R = GetValue(St, LHS);
Ted Kremenekadec14b2008-02-08 02:57:34 +0000439 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000440
441 Nodify(Dst, S, Pred, SetValue(St, S, R));
442}
443
Ted Kremenekfd85f292008-02-12 19:49:57 +0000444/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000445void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekfd85f292008-02-12 19:49:57 +0000446 NodeTy* Pred,
447 NodeSet& Dst) {
448
449 // 6.5.3.4 sizeof: "The result type is an integer."
450
451 QualType T = S->getArgumentType();
452
453 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000454 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000455 return;
456
457 SourceLocation L = S->getExprLoc();
458 uint64_t size = getContext().getTypeSize(T, L) / 8;
459
460 Nodify(Dst, S, Pred,
461 SetValue(Pred->getState(), S,
462 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
463
464}
465
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000466void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
467 GRExprEngine::NodeTy* Pred,
468 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000469
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000470 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000471 UnaryOperator::Opcode Op = U->getOpcode();
472
473 // FIXME: This is a hack so that for '*' and '&' we don't recurse
474 // on visiting the subexpression if it is a DeclRefExpr. We should
475 // probably just handle AddrOf and Deref in their own methods to make
476 // this cleaner.
477 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
478 isa<DeclRefExpr>(U->getSubExpr()))
479 S1.Add(Pred);
480 else
481 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000482
483 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
484 NodeTy* N1 = *I1;
485 StateTy St = N1->getState();
486
Ted Kremenek22640ce2008-02-15 22:09:30 +0000487 // Handle ++ and -- (both pre- and post-increment).
488
489 if (U->isIncrementDecrementOp()) {
490 const LValue& L1 = GetLValue(St, U->getSubExpr());
491 RValue R1 = GetValue(St, L1);
492
493 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
494 : BinaryOperator::Sub;
495
496 RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U));
497
498 if (U->isPostfix())
499 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
500 else
501 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
502
503 continue;
504 }
505
506 // Handle all other unary operators.
507
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000508 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000509
Ted Kremenekcacd7362008-01-24 08:20:02 +0000510 case UnaryOperator::Minus: {
Ted Kremenek0922ec82008-01-28 22:09:13 +0000511 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000512 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000513 break;
514 }
515
Ted Kremenek2cb46642008-02-04 16:58:30 +0000516 case UnaryOperator::Not: {
517 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000518 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenek2cb46642008-02-04 16:58:30 +0000519 break;
520 }
521
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000522 case UnaryOperator::LNot: {
523 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
524 //
525 // Note: technically we do "E == 0", but this is the same in the
526 // transfer functions as "0 == E".
527
528 RValue V1 = GetValue(St, U->getSubExpr());
529
530 if (isa<LValue>(V1)) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000531 const LValue& L1 = cast<LValue>(V1);
532 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
533 Nodify(Dst, U, N1,
Ted Kremenekb1934132008-02-14 19:37:24 +0000534 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
535 L1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000536 }
537 else {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000538 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000539 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000540 Nodify(Dst, U, N1,
Ted Kremenekb1934132008-02-14 19:37:24 +0000541 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
542 R1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000543 }
544
545 break;
546 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000547
548 case UnaryOperator::SizeOf: {
549 // 6.5.3.4 sizeof: "The result type is an integer."
550
551 QualType T = U->getSubExpr()->getType();
552
553 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000554 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000555 return;
556
557 SourceLocation L = U->getExprLoc();
558 uint64_t size = getContext().getTypeSize(T, L) / 8;
559
560 Nodify(Dst, U, N1,
561 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
562 getContext().IntTy, L)));
563
564 break;
565 }
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000566
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000567 case UnaryOperator::AddrOf: {
568 const LValue& L1 = GetLValue(St, U->getSubExpr());
569 Nodify(Dst, U, N1, SetValue(St, U, L1));
570 break;
571 }
572
573 case UnaryOperator::Deref: {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000574 // FIXME: Stop when dereferencing an uninitialized value.
575 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
576
577 const RValue& V = GetValue(St, U->getSubExpr());
578 const LValue& L1 = cast<LValue>(V);
579
Ted Kremenek80d52d02008-02-07 05:48:01 +0000580 // After a dereference, one of two possible situations arise:
581 // (1) A crash, because the pointer was NULL.
582 // (2) The pointer is not NULL, and the dereference works.
583 //
584 // We add these assumptions.
585
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000586 bool isFeasibleNotNull;
587
Ted Kremenek80d52d02008-02-07 05:48:01 +0000588 // "Assume" that the pointer is Not-NULL.
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000589 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
590
591 if (isFeasibleNotNull) {
Ted Kremenek80d52d02008-02-07 05:48:01 +0000592 QualType T = U->getType();
593 Nodify(Dst, U, N1, SetValue(StNotNull, U,
594 GetValue(StNotNull, L1, &T)));
595 }
596
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000597 bool isFeasibleNull;
598
599 // "Assume" that the pointer is NULL.
600 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
601
602 if (isFeasibleNull) {
Ted Kremenek02b5b402008-02-07 15:20:13 +0000603 // We don't use "Nodify" here because the node will be a sink
604 // and we have no intention of processing it later.
605 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
606
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000607 if (NullNode) {
608 NullNode->markAsSink();
609
610 if (isFeasibleNotNull)
611 ImplicitNullDeref.insert(NullNode);
612 else
613 ExplicitNullDeref.insert(NullNode);
614 }
615 }
616
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000617 break;
618 }
619
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000620 default: ;
621 assert (false && "Not implemented.");
622 }
623 }
624}
625
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000626void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
627 GRExprEngine::NodeSet& Dst) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000628
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000629 if (isa<DeclRefExpr>(E)) {
630 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000631 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000632 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000633
634 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
635 if (U->getOpcode() == UnaryOperator::Deref) {
636 Visit(U->getSubExpr(), Pred, Dst);
637 return;
638 }
639 }
640
641 Visit(E, Pred, Dst);
642}
643
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000644void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000645 GRExprEngine::NodeTy* Pred,
646 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000647 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000648
649 if (B->isAssignmentOp())
650 VisitAssignmentLHS(B->getLHS(), Pred, S1);
651 else
652 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000653
Ted Kremenekf031b872008-01-23 19:59:44 +0000654 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
655 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000656
Ted Kremenekf031b872008-01-23 19:59:44 +0000657 // When getting the value for the LHS, check if we are in an assignment.
658 // In such cases, we want to (initially) treat the LHS as an LValue,
659 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenek0922ec82008-01-28 22:09:13 +0000660 // evaluated to LValueDecl's instead of to an NonLValue.
661 const RValue& V1 =
Ted Kremenekf031b872008-01-23 19:59:44 +0000662 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
663 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000664
Ted Kremenekf031b872008-01-23 19:59:44 +0000665 NodeSet S2;
666 Visit(B->getRHS(), N1, S2);
667
668 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000669
Ted Kremenekf031b872008-01-23 19:59:44 +0000670 NodeTy* N2 = *I2;
671 StateTy St = N2->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000672 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekf031b872008-01-23 19:59:44 +0000673
Ted Kremenek15cb0782008-02-06 22:50:25 +0000674 BinaryOperator::Opcode Op = B->getOpcode();
675
676 if (Op <= BinaryOperator::Or) {
677
Ted Kremenekadec14b2008-02-08 02:57:34 +0000678 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000679 Nodify(Dst, B, N2, SetValue(St, B, V1));
680 continue;
681 }
682
Ted Kremenek22640ce2008-02-15 22:09:30 +0000683 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2)));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000684 continue;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000685
Ted Kremenek15cb0782008-02-06 22:50:25 +0000686 }
687
688 switch (Op) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000689 case BinaryOperator::Assign: {
690 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek46a19772008-02-06 04:41:14 +0000691 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekf031b872008-01-23 19:59:44 +0000692 break;
693 }
694
Ted Kremenek15cb0782008-02-06 22:50:25 +0000695 default: { // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +0000696
Ted Kremenek15cb0782008-02-06 22:50:25 +0000697 assert (B->isCompoundAssignmentOp());
698
699 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekadec14b2008-02-08 02:57:34 +0000700 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000701
Ted Kremenek106f37c2008-02-08 07:05:39 +0000702 if (Op >= BinaryOperator::AndAssign)
703 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
704 else
Ted Kremenek22640ce2008-02-15 22:09:30 +0000705 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000706
Ted Kremenek22640ce2008-02-15 22:09:30 +0000707 if (B->getType()->isPointerType()) { // Perform pointer arithmetic.
708 const NonLValue& R2 = cast<NonLValue>(V2);
709 Result = EvalBinaryOp(ValMgr, Op, L1, R2);
710 }
711 else if (isa<LValue>(V2)) { // LValue comparison.
Ted Kremenek19072e02008-01-29 19:43:15 +0000712 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekb1934132008-02-14 19:37:24 +0000713 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
Ted Kremenek19072e02008-01-29 19:43:15 +0000714 }
Ted Kremenek22640ce2008-02-15 22:09:30 +0000715 else { // Any operation between two Non-LValues.
Ted Kremenek15cb0782008-02-06 22:50:25 +0000716 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek19072e02008-01-29 19:43:15 +0000717 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekb1934132008-02-14 19:37:24 +0000718 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek19072e02008-01-29 19:43:15 +0000719 }
720
Ted Kremenek15cb0782008-02-06 22:50:25 +0000721 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekf031b872008-01-23 19:59:44 +0000722 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000723 }
Ted Kremenekf031b872008-01-23 19:59:44 +0000724 }
Ted Kremenekafba4b22008-01-16 00:53:15 +0000725 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000726 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000727}
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000728
Ted Kremenekbe962452008-01-16 19:42:59 +0000729
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000730void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
731 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000732
733 // FIXME: add metadata to the CFG so that we can disable
734 // this check when we KNOW that there is no block-level subexpression.
735 // The motivation is that this check requires a hashtable lookup.
736
737 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
738 Dst.Add(Pred);
739 return;
740 }
741
742 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000743
744 default:
745 // Cases we intentionally have "default" handle:
746 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
747
748 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
749 break;
750
Ted Kremenek744a7862008-02-08 20:29:23 +0000751 case Stmt::BinaryOperatorClass: {
752 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000753
Ted Kremenek744a7862008-02-08 20:29:23 +0000754 if (B->isLogicalOp()) {
755 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000756 break;
757 }
Ted Kremenek744a7862008-02-08 20:29:23 +0000758 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +0000759 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000760 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000761 break;
762 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000763
764 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
765 break;
766 }
767
768 case Stmt::CastExprClass: {
769 CastExpr* C = cast<CastExpr>(S);
770 VisitCast(C, C->getSubExpr(), Pred, Dst);
771 break;
Ted Kremenek744a7862008-02-08 20:29:23 +0000772 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000773
Ted Kremenekfd85f292008-02-12 19:49:57 +0000774 case Stmt::ChooseExprClass: { // __builtin_choose_expr
775 ChooseExpr* C = cast<ChooseExpr>(S);
776 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
777 break;
778 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000779
Ted Kremenek9914e9c2008-01-23 23:38:00 +0000780 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +0000781 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
782 break;
783
Ted Kremenekfd85f292008-02-12 19:49:57 +0000784 case Stmt::ConditionalOperatorClass: { // '?' operator
785 ConditionalOperator* C = cast<ConditionalOperator>(S);
786 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
787 break;
788 }
789
790 case Stmt::DeclRefExprClass:
791 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
792 break;
793
794 case Stmt::DeclStmtClass:
795 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
796 break;
797
798 case Stmt::ImplicitCastExprClass: {
799 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
800 VisitCast(C, C->getSubExpr(), Pred, Dst);
801 break;
802 }
803
804 case Stmt::ParenExprClass:
805 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
806 break;
807
808 case Stmt::SizeOfAlignOfTypeExprClass:
809 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
810 break;
811
Ted Kremenek106f37c2008-02-08 07:05:39 +0000812 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +0000813 StmtExpr* SE = cast<StmtExpr>(S);
814
Ted Kremenek106f37c2008-02-08 07:05:39 +0000815 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000816 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
817 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000818 break;
819 }
820
Ted Kremenekfd85f292008-02-12 19:49:57 +0000821 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000822 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
823 Visit(R, Pred, Dst);
824 else
825 Dst.Add(Pred);
826
827 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000828 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000829
Ted Kremenekfd85f292008-02-12 19:49:57 +0000830 case Stmt::UnaryOperatorClass:
831 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000832 break;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000833 }
Ted Kremenekbe962452008-01-16 19:42:59 +0000834}
835
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000836//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +0000837// "Assume" logic.
838//===----------------------------------------------------------------------===//
839
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000840GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +0000841 bool Assumption,
Ted Kremenek72197902008-01-31 19:34:24 +0000842 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +0000843
844 switch (Cond.getSubKind()) {
845 default:
Ted Kremenek13f31562008-02-06 00:54:14 +0000846 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremenek6e24a802008-02-01 06:36:40 +0000847 return St;
848
Ted Kremenek13f31562008-02-06 00:54:14 +0000849 case lval::SymbolValKind:
850 if (Assumption)
851 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
852 ValMgr.getZeroWithPtrWidth(), isFeasible);
853 else
854 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
855 ValMgr.getZeroWithPtrWidth(), isFeasible);
856
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000857
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000858 case lval::DeclValKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +0000859 isFeasible = Assumption;
860 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +0000861
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000862 case lval::ConcreteIntKind: {
863 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +0000864 isFeasible = b ? Assumption : !Assumption;
865 return St;
866 }
867 }
Ted Kremenek90960972008-01-30 23:03:39 +0000868}
869
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000870GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +0000871 bool Assumption,
Ted Kremenek72197902008-01-31 19:34:24 +0000872 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +0000873
874 switch (Cond.getSubKind()) {
875 default:
876 assert (false && "'Assume' not implemented for this NonLValue.");
877 return St;
878
Ted Kremenekab359c12008-02-06 17:32:17 +0000879
880 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000881 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +0000882 SymbolID sym = SV.getSymbol();
883
884 if (Assumption)
885 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
886 isFeasible);
887 else
888 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
889 isFeasible);
890 }
891
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000892 case nonlval::SymIntConstraintValKind:
893 return
894 AssumeSymInt(St, Assumption,
895 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
896 isFeasible);
897
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000898 case nonlval::ConcreteIntKind: {
899 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +0000900 isFeasible = b ? Assumption : !Assumption;
901 return St;
902 }
903 }
904}
905
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000906GRExprEngine::StateTy
907GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +0000908 const llvm::APSInt& V, bool& isFeasible) {
909
910 // First, determine if sym == X, where X != V.
911 if (const llvm::APSInt* X = St.getSymVal(sym)) {
912 isFeasible = *X != V;
913 return St;
914 }
915
916 // Second, determine if sym != V.
917 if (St.isNotEqual(sym, V)) {
918 isFeasible = true;
919 return St;
920 }
921
922 // If we reach here, sym is not a constant and we don't know if it is != V.
923 // Make that assumption.
924
925 isFeasible = true;
926 return StateMgr.AddNE(St, sym, V);
927}
928
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000929GRExprEngine::StateTy
930GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +0000931 const llvm::APSInt& V, bool& isFeasible) {
932
933 // First, determine if sym == X, where X != V.
934 if (const llvm::APSInt* X = St.getSymVal(sym)) {
935 isFeasible = *X == V;
936 return St;
937 }
938
939 // Second, determine if sym != V.
940 if (St.isNotEqual(sym, V)) {
941 isFeasible = false;
942 return St;
943 }
944
945 // If we reach here, sym is not a constant and we don't know if it is == V.
946 // Make that assumption.
947
948 isFeasible = true;
949 return StateMgr.AddEQ(St, sym, V);
950}
Ted Kremenek90960972008-01-30 23:03:39 +0000951
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000952GRExprEngine::StateTy
953GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000954 const SymIntConstraint& C, bool& isFeasible) {
955
956 switch (C.getOpcode()) {
957 default:
958 // No logic yet for other operators.
959 return St;
960
961 case BinaryOperator::EQ:
962 if (Assumption)
963 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
964 else
965 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
966
967 case BinaryOperator::NE:
968 if (Assumption)
969 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
970 else
971 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
972 }
973}
974
Ted Kremenek90960972008-01-30 23:03:39 +0000975//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +0000976// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000977//===----------------------------------------------------------------------===//
978
Ted Kremenekdd9e97d2008-01-16 21:46:15 +0000979#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000980static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +0000981
Ted Kremenekdd9e97d2008-01-16 21:46:15 +0000982namespace llvm {
983template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000984struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +0000985 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +0000986
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000987 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +0000988
989 Out << "Variables:\\l";
990
Ted Kremenek3ec7be92008-01-24 22:27:20 +0000991 bool isFirst = true;
992
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000993 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +0000994 E=St.vb_end(); I!=E;++I) {
995
996 if (isFirst)
997 isFirst = false;
998 else
999 Out << "\\l";
1000
1001 Out << ' ' << I.getKey()->getName() << " : ";
1002 I.getData().print(Out);
1003 }
1004
1005 }
1006
Ted Kremenek17c5f112008-02-11 19:21:59 +00001007
Ted Kremenek6d409922008-02-13 18:06:44 +00001008 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001009
1010 bool isFirst = true;
1011
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001012 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001013 I != E;++I) {
1014
1015 if (isFirst) {
1016 Out << "\\l\\lSub-Expressions:\\l";
1017 isFirst = false;
1018 }
1019 else
1020 Out << "\\l";
1021
1022 Out << " (" << (void*) I.getKey() << ") ";
1023 I.getKey()->printPretty(Out);
1024 Out << " : ";
1025 I.getData().print(Out);
1026 }
1027 }
1028
Ted Kremenek6d409922008-02-13 18:06:44 +00001029 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001030
Ted Kremenek08cfd832008-02-08 21:10:02 +00001031 bool isFirst = true;
1032
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001033 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001034 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001035 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001036 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001037 isFirst = false;
1038 }
1039 else
1040 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001041
Ted Kremenek17c5f112008-02-11 19:21:59 +00001042 Out << " (" << (void*) I.getKey() << ") ";
1043 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001044 Out << " : ";
1045 I.getData().print(Out);
1046 }
1047 }
1048
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001049 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001050 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1051
1052 if (CE.isEmpty())
1053 return;
1054
1055 Out << "\\l\\|'==' constraints:";
1056
1057 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1058 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1059 }
1060
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001061 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001062 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1063
1064 if (NE.isEmpty())
1065 return;
1066
1067 Out << "\\l\\|'!=' constraints:";
1068
1069 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1070 I != EI; ++I){
1071
1072 Out << "\\l $" << I.getKey() << " : ";
1073 bool isFirst = true;
1074
1075 ValueState::IntSetTy::iterator J=I.getData().begin(),
1076 EJ=I.getData().end();
1077 for ( ; J != EJ; ++J) {
1078 if (isFirst) isFirst = false;
1079 else Out << ", ";
1080
1081 Out << (*J)->toString();
1082 }
1083 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001084 }
1085
1086 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1087
1088 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
1089 GraphPrintCheckerState->isExplicitNullDeref(N))
1090 return "color=\"red\",style=\"filled\"";
1091
1092 return "";
1093 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001094
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001095 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001096 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001097
1098 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001099 ProgramPoint Loc = N->getLocation();
1100
1101 switch (Loc.getKind()) {
1102 case ProgramPoint::BlockEntranceKind:
1103 Out << "Block Entrance: B"
1104 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1105 break;
1106
1107 case ProgramPoint::BlockExitKind:
1108 assert (false);
1109 break;
1110
1111 case ProgramPoint::PostStmtKind: {
1112 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001113 Out << L.getStmt()->getStmtClassName() << ':'
1114 << (void*) L.getStmt() << ' ';
1115
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001116 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001117
1118 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1119 Out << "\\|Implicit-Null Dereference.\\l";
1120 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001121 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1122 Out << "\\|Explicit-Null Dereference.\\l";
1123 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001124
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001125 break;
1126 }
1127
1128 default: {
1129 const BlockEdge& E = cast<BlockEdge>(Loc);
1130 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1131 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001132
1133 if (Stmt* T = E.getSrc()->getTerminator()) {
1134 Out << "\\|Terminator: ";
1135 E.getSrc()->printTerminator(Out);
1136
Ted Kremenekaee121c2008-02-13 23:08:21 +00001137 if (isa<SwitchStmt>(T)) {
1138 Stmt* Label = E.getDst()->getLabel();
1139
1140 if (Label) {
1141 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1142 Out << "\\lcase ";
1143 C->getLHS()->printPretty(Out);
1144
1145 if (Stmt* RHS = C->getRHS()) {
1146 Out << " .. ";
1147 RHS->printPretty(Out);
1148 }
1149
1150 Out << ":";
1151 }
1152 else {
1153 assert (isa<DefaultStmt>(Label));
1154 Out << "\\ldefault:";
1155 }
1156 }
1157 else
1158 Out << "\\l(implicit) default:";
1159 }
1160 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001161 // FIXME
1162 }
1163 else {
1164 Out << "\\lCondition: ";
1165 if (*E.getSrc()->succ_begin() == E.getDst())
1166 Out << "true";
1167 else
1168 Out << "false";
1169 }
1170
1171 Out << "\\l";
1172 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001173
1174 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1175 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1176 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001177 }
1178 }
1179
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001180 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001181
Ted Kremenek17c5f112008-02-11 19:21:59 +00001182 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001183
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001184 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001185 return Out.str();
1186 }
1187};
1188} // end llvm namespace
1189#endif
1190
Ted Kremenek3862eb12008-02-14 22:36:46 +00001191void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001192#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001193 GraphPrintCheckerState = this;
1194 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001195 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001196#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001197}