blob: d709eff99fd3cac3d07c8f9ede065d914599bdbc [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000017#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
18
19#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000020
21using namespace clang;
22using llvm::dyn_cast;
23using llvm::cast;
24using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000025
Ted Kremenek4d4dd852008-02-13 17:41:41 +000026GRExprEngine::StateTy
27GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000028
Ted Kremeneke070a1d2008-02-04 21:59:01 +000029 if (!StateCleaned) {
30 St = RemoveDeadBindings(CurrentStmt, St);
31 StateCleaned = true;
32 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000033
Ted Kremeneke070a1d2008-02-04 21:59:01 +000034 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000035
Ted Kremeneke070a1d2008-02-04 21:59:01 +000036 if (S == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(S);
38
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000042
Ted Kremeneke070a1d2008-02-04 21:59:01 +000043 return StateMgr.SetValue(St, S, isBlkExpr, V);
44}
45
Ted Kremenek4d4dd852008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
47GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-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 Kremenek4d4dd852008-02-13 17:41:41 +000058GRExprEngine::StateTy
59GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000060
Ted Kremenek53c641a2008-02-08 03:02:48 +000061 if (LV.isUnknown())
Ted Kremeneke070a1d2008-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 Kremenek4d4dd852008-02-13 17:41:41 +000072void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +000073 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +000074
Ted Kremeneke7d22112008-02-11 19:21:59 +000075 // Remove old bindings for subexpressions.
76 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +000077
Ted Kremenekb2331832008-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 Kremenekb38911f2008-01-30 23:03:39 +000093 RValue V = GetValue(PrevState, Condition);
94
95 switch (V.getBaseKind()) {
96 default:
97 break;
98
Ted Kremenek53c641a2008-02-08 03:02:48 +000099 case RValue::UnknownKind:
Ted Kremenekb38911f2008-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 Kremenekb2331832008-02-15 22:29:00 +0000116
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000117 // Get the current block counter.
118 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000119 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
120 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000121
Ted Kremenek8e49dd62008-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 Kremenekb38911f2008-01-30 23:03:39 +0000126
Ted Kremenek8e49dd62008-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 Kremenekb38911f2008-01-30 23:03:39 +0000135 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000136 else
137 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000138
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000139 BlockID = builder.getTargetBlock(false)->getBlockID();
140 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000141
Ted Kremenek8e49dd62008-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 Kremenekf233d482008-02-05 00:26:40 +0000156 else
157 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000158}
159
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000160/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000161/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000162void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-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 Kremenek24f1a962008-02-13 17:27:37 +0000180 if (I.getLabel() == L) {
181 builder.generateNode(I, St);
Ted Kremenek754607e2008-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 Kremenek24f1a962008-02-13 17:27:37 +0000192 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-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 Kremenek24f1a962008-02-13 17:27:37 +0000202 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000203}
Ted Kremenekf233d482008-02-05 00:26:40 +0000204
Ted Kremenekdaeb9a72008-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 Kremenek692416c2008-02-18 22:57:02 +0000212 Expr* CondE = builder.getCondition();
213 NonLValue CondV = cast<NonLValue>(GetValue(St, CondE));
Ted Kremenekdaeb9a72008-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 Kremenek692416c2008-02-18 22:57:02 +0000225
226 unsigned bits = getContext().getTypeSize(CondE->getType(),
227 CondE->getExprLoc());
228
Ted Kremenekdaeb9a72008-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 Kremenek6cb0b542008-02-14 19:37:24 +0000262 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000263
264 // Now "assume" that the case matches.
Ted Kremenek692416c2008-02-18 22:57:02 +0000265 bool isFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000266
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000267 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-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 Kremenek6cb0b542008-02-14 19:37:24 +0000282 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-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 Kremenek4d4dd852008-02-13 17:41:41 +0000299void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-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);
307
Ted Kremenek22031182008-02-08 02:57:34 +0000308 if (isa<UnknownVal>(R1) &&
309 (isa<UnknownVal>(R2) ||
310 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000311
312 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
313 return;
314 }
Ted Kremenek22031182008-02-08 02:57:34 +0000315 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000316 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
317 return;
318 }
319
320 // R1 is an expression that can evaluate to either 'true' or 'false'.
321 if (B->getOpcode() == BinaryOperator::LAnd) {
322 // hasR2 == 'false' means that LHS evaluated to 'false' and that
323 // we short-circuited, leading to a value of '0' for the '&&' expression.
324 if (hasR2 == false) {
325 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
326 return;
327 }
328 }
329 else {
330 assert (B->getOpcode() == BinaryOperator::LOr);
331 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
332 // we short-circuited, leading to a value of '1' for the '||' expression.
333 if (hasR2 == false) {
334 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
335 return;
336 }
337 }
338
339 // If we reach here we did not short-circuit. Assume R2 == true and
340 // R2 == false.
341
342 bool isFeasible;
343 StateTy St = Assume(PrevState, R2, true, isFeasible);
344
345 if (isFeasible)
346 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
347
348 St = Assume(PrevState, R2, false, isFeasible);
349
350 if (isFeasible)
351 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
352}
353
354
355
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000356void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000357 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000358
359 StmtEntryNode = builder.getLastNode();
360 CurrentStmt = S;
361 NodeSet Dst;
362 StateCleaned = false;
363
364 Visit(S, StmtEntryNode, Dst);
365
366 // If no nodes were generated, generate a new node that has all the
367 // dead mappings removed.
368 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
369 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
370 builder.generateNode(S, St, StmtEntryNode);
371 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000372
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000373 CurrentStmt = NULL;
374 StmtEntryNode = NULL;
375 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000376}
377
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000378GRExprEngine::NodeTy*
379GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000380
381 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000382 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000383 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000384
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000385 NodeTy* N = Builder->generateNode(S, St, Pred);
386 Dst.Add(N);
387 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000388}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000389
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000390void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000391 const StateTy::BufferTy& SB) {
392
393 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
394 Nodify(Dst, S, Pred, *I);
395}
396
Ted Kremenek44842c22008-02-13 18:06:44 +0000397void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000398 if (D != CurrentStmt) {
399 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
400 return;
401 }
402
403 // If we are here, we are loading the value of the decl and binding
404 // it to the block-level expression.
405
406 StateTy St = Pred->getState();
407
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000408 Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000409}
410
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000411void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000412
413 QualType T = CastE->getType();
414
415 // Check for redundant casts.
416 if (E->getType() == T) {
417 Dst.Add(Pred);
418 return;
419 }
420
421 NodeSet S1;
422 Visit(E, Pred, S1);
423
424 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
425 NodeTy* N = *I1;
426 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000427 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000428 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000429 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000430}
431
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000432void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
433 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000434
435 StateTy St = Pred->getState();
436
437 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000438 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
439 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000440 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000441 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000442 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000443
444 Nodify(Dst, DS, Pred, St);
445
446 if (Dst.empty())
447 Dst.Add(Pred);
448}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000449
Ted Kremenekf233d482008-02-05 00:26:40 +0000450
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000451void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000452 NodeTy* Pred, NodeSet& Dst) {
453
454 StateTy St = Pred->getState();
455
456 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000457 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000458
459 Nodify(Dst, S, Pred, SetValue(St, S, R));
460}
461
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000462/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000463void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000464 NodeTy* Pred,
465 NodeSet& Dst) {
466
467 // 6.5.3.4 sizeof: "The result type is an integer."
468
469 QualType T = S->getArgumentType();
470
471 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000472 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000473 return;
474
475 SourceLocation L = S->getExprLoc();
476 uint64_t size = getContext().getTypeSize(T, L) / 8;
477
478 Nodify(Dst, S, Pred,
479 SetValue(Pred->getState(), S,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000480 NonLValue::GetValue(ValMgr, size, S->getType(), L)));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000481
482}
483
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000484void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
485 GRExprEngine::NodeTy* Pred,
486 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000487
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000488 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000489 UnaryOperator::Opcode Op = U->getOpcode();
490
491 // FIXME: This is a hack so that for '*' and '&' we don't recurse
492 // on visiting the subexpression if it is a DeclRefExpr. We should
493 // probably just handle AddrOf and Deref in their own methods to make
494 // this cleaner.
495 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
496 isa<DeclRefExpr>(U->getSubExpr()))
497 S1.Add(Pred);
498 else
499 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000500
501 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
502 NodeTy* N1 = *I1;
503 StateTy St = N1->getState();
504
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000505 // Handle ++ and -- (both pre- and post-increment).
506
507 if (U->isIncrementDecrementOp()) {
508 const LValue& L1 = GetLValue(St, U->getSubExpr());
509 RValue R1 = GetValue(St, L1);
510
511 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
512 : BinaryOperator::Sub;
513
514 RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U));
515
516 if (U->isPostfix())
517 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
518 else
519 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
520
521 continue;
522 }
523
524 // Handle all other unary operators.
525
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000526 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000527
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000528 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000529 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000530 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000531 break;
532 }
533
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000534 case UnaryOperator::Not: {
535 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000536 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000537 break;
538 }
539
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000540 case UnaryOperator::LNot: {
541 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
542 //
543 // Note: technically we do "E == 0", but this is the same in the
544 // transfer functions as "0 == E".
545
546 RValue V1 = GetValue(St, U->getSubExpr());
547
548 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000549 const LValue& L1 = cast<LValue>(V1);
550 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
551 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000552 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
553 L1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000554 }
555 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000556 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000557 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000558 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000559 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
560 R1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000561 }
562
563 break;
564 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000565
566 case UnaryOperator::SizeOf: {
567 // 6.5.3.4 sizeof: "The result type is an integer."
568
569 QualType T = U->getSubExpr()->getType();
570
571 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000572 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000573 return;
574
575 SourceLocation L = U->getExprLoc();
576 uint64_t size = getContext().getTypeSize(T, L) / 8;
577
578 Nodify(Dst, U, N1,
579 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000580 U->getType(), L)));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000581
582 break;
583 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000584
Ted Kremenek64924852008-01-31 02:35:41 +0000585 case UnaryOperator::AddrOf: {
586 const LValue& L1 = GetLValue(St, U->getSubExpr());
587 Nodify(Dst, U, N1, SetValue(St, U, L1));
588 break;
589 }
590
591 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000592 // FIXME: Stop when dereferencing an uninitialized value.
593 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
594
Ted Kremenek692416c2008-02-18 22:57:02 +0000595 const RValue& V = GetValue(St, U->getSubExpr());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000596 const LValue& L1 = cast<LValue>(V);
597
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000598 // After a dereference, one of two possible situations arise:
599 // (1) A crash, because the pointer was NULL.
600 // (2) The pointer is not NULL, and the dereference works.
601 //
602 // We add these assumptions.
603
Ted Kremenek63a4f692008-02-07 06:04:18 +0000604 bool isFeasibleNotNull;
605
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000606 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000607 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
608
609 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000610 QualType T = U->getType();
611 Nodify(Dst, U, N1, SetValue(StNotNull, U,
612 GetValue(StNotNull, L1, &T)));
613 }
614
Ted Kremenek692416c2008-02-18 22:57:02 +0000615 if (V.isUnknown())
616 return;
617
Ted Kremenek63a4f692008-02-07 06:04:18 +0000618 bool isFeasibleNull;
619
620 // "Assume" that the pointer is NULL.
621 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
622
623 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000624 // We don't use "Nodify" here because the node will be a sink
625 // and we have no intention of processing it later.
626 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
627
Ted Kremenek63a4f692008-02-07 06:04:18 +0000628 if (NullNode) {
629 NullNode->markAsSink();
630
631 if (isFeasibleNotNull)
632 ImplicitNullDeref.insert(NullNode);
633 else
634 ExplicitNullDeref.insert(NullNode);
635 }
636 }
637
Ted Kremenek64924852008-01-31 02:35:41 +0000638 break;
639 }
640
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000641 default: ;
642 assert (false && "Not implemented.");
643 }
644 }
645}
646
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000647void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
648 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000649
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000650 if (isa<DeclRefExpr>(E)) {
651 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000652 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000653 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000654
655 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
656 if (U->getOpcode() == UnaryOperator::Deref) {
657 Visit(U->getSubExpr(), Pred, Dst);
658 return;
659 }
660 }
661
662 Visit(E, Pred, Dst);
663}
664
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000665void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666 GRExprEngine::NodeTy* Pred,
667 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000668 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000669
670 if (B->isAssignmentOp())
671 VisitAssignmentLHS(B->getLHS(), Pred, S1);
672 else
673 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000674
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000675 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
676 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000677
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000678 // When getting the value for the LHS, check if we are in an assignment.
679 // In such cases, we want to (initially) treat the LHS as an LValue,
680 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000681 // evaluated to LValueDecl's instead of to an NonLValue.
682 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000683 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
684 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000685
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000686 NodeSet S2;
687 Visit(B->getRHS(), N1, S2);
688
689 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000690
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000691 NodeTy* N2 = *I2;
692 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000693 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000694
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000695 BinaryOperator::Opcode Op = B->getOpcode();
696
697 if (Op <= BinaryOperator::Or) {
698
Ted Kremenek22031182008-02-08 02:57:34 +0000699 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000700 Nodify(Dst, B, N2, SetValue(St, B, V1));
701 continue;
702 }
703
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000704 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2)));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000705 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000706
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000707 }
708
709 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000710 case BinaryOperator::Assign: {
711 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000712 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000713 break;
714 }
715
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000716 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000717
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000718 assert (B->isCompoundAssignmentOp());
719
720 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000721 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000722
Ted Kremenekda9bd092008-02-08 07:05:39 +0000723 if (Op >= BinaryOperator::AndAssign)
724 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
725 else
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000726 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000727
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000728 if (B->getType()->isPointerType()) { // Perform pointer arithmetic.
729 const NonLValue& R2 = cast<NonLValue>(V2);
730 Result = EvalBinaryOp(ValMgr, Op, L1, R2);
731 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000732 else if (isa<LValue>(V2)) {
Ted Kremenek687af802008-01-29 19:43:15 +0000733 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekb2331832008-02-15 22:29:00 +0000734
735 if (B->getRHS()->getType()->isPointerType()) {
736 // LValue comparison.
737 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
738 }
739 else {
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000740 QualType T1 = B->getLHS()->getType();
741 QualType T2 = B->getRHS()->getType();
742
Ted Kremenekb2331832008-02-15 22:29:00 +0000743 // An operation between two variables of a non-lvalue type.
744 Result =
745 EvalBinaryOp(ValMgr, Op,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000746 cast<NonLValue>(GetValue(N1->getState(), L1, &T1)),
747 cast<NonLValue>(GetValue(N2->getState(), L2, &T2)));
Ted Kremenekb2331832008-02-15 22:29:00 +0000748 }
Ted Kremenek687af802008-01-29 19:43:15 +0000749 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000750 else { // Any other operation between two Non-LValues.
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000751 QualType T = B->getLHS()->getType();
752 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(),
753 L1, &T));
Ted Kremenek687af802008-01-29 19:43:15 +0000754 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000755 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000756 }
757
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000758 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000759 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000760 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000761 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000762 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000763 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000764}
Ted Kremenekee985462008-01-16 18:18:48 +0000765
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000766
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000767void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
768 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000769
770 // FIXME: add metadata to the CFG so that we can disable
771 // this check when we KNOW that there is no block-level subexpression.
772 // The motivation is that this check requires a hashtable lookup.
773
774 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
775 Dst.Add(Pred);
776 return;
777 }
778
779 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000780
781 default:
782 // Cases we intentionally have "default" handle:
783 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
784
785 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
786 break;
787
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000788 case Stmt::BinaryOperatorClass: {
789 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000790
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000791 if (B->isLogicalOp()) {
792 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000793 break;
794 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000795 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000796 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000797 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000798 break;
799 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000800
801 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
802 break;
803 }
804
805 case Stmt::CastExprClass: {
806 CastExpr* C = cast<CastExpr>(S);
807 VisitCast(C, C->getSubExpr(), Pred, Dst);
808 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000809 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000810
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000811 case Stmt::ChooseExprClass: { // __builtin_choose_expr
812 ChooseExpr* C = cast<ChooseExpr>(S);
813 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
814 break;
815 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000816
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000817 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000818 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
819 break;
820
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000821 case Stmt::ConditionalOperatorClass: { // '?' operator
822 ConditionalOperator* C = cast<ConditionalOperator>(S);
823 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
824 break;
825 }
826
827 case Stmt::DeclRefExprClass:
828 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
829 break;
830
831 case Stmt::DeclStmtClass:
832 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
833 break;
834
835 case Stmt::ImplicitCastExprClass: {
836 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
837 VisitCast(C, C->getSubExpr(), Pred, Dst);
838 break;
839 }
840
841 case Stmt::ParenExprClass:
842 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
843 break;
844
845 case Stmt::SizeOfAlignOfTypeExprClass:
846 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
847 break;
848
Ted Kremenekda9bd092008-02-08 07:05:39 +0000849 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000850 StmtExpr* SE = cast<StmtExpr>(S);
851
Ted Kremenekda9bd092008-02-08 07:05:39 +0000852 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000853 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
854 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000855 break;
856 }
857
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000858 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000859 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
860 Visit(R, Pred, Dst);
861 else
862 Dst.Add(Pred);
863
864 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000865 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000866
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000867 case Stmt::UnaryOperatorClass:
868 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000869 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000870 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000871}
872
Ted Kremenekee985462008-01-16 18:18:48 +0000873//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000874// "Assume" logic.
875//===----------------------------------------------------------------------===//
876
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000877GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +0000878 bool Assumption,
879 bool& isFeasible) {
880
881 assert (!isa<UninitializedVal>(Cond));
882
883 if (isa<UnknownVal>(Cond)) {
884 isFeasible = true;
885 return St;
886 }
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000887
888 switch (Cond.getSubKind()) {
889 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000890 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000891 return St;
892
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000893 case lval::SymbolValKind:
894 if (Assumption)
895 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
896 ValMgr.getZeroWithPtrWidth(), isFeasible);
897 else
898 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
899 ValMgr.getZeroWithPtrWidth(), isFeasible);
900
Ted Kremenek08b66252008-02-06 04:31:33 +0000901
Ted Kremenek329f8542008-02-05 21:52:21 +0000902 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000903 isFeasible = Assumption;
904 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000905
Ted Kremenek329f8542008-02-05 21:52:21 +0000906 case lval::ConcreteIntKind: {
907 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000908 isFeasible = b ? Assumption : !Assumption;
909 return St;
910 }
911 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000912}
913
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000914GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000915 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000916 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000917
Ted Kremenek692416c2008-02-18 22:57:02 +0000918 assert (!isa<UninitializedVal>(Cond));
919
920 if (isa<UnknownVal>(Cond)) {
921 isFeasible = true;
922 return St;
923 }
924
Ted Kremenekb38911f2008-01-30 23:03:39 +0000925 switch (Cond.getSubKind()) {
926 default:
927 assert (false && "'Assume' not implemented for this NonLValue.");
928 return St;
929
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000930
931 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000932 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000933 SymbolID sym = SV.getSymbol();
934
935 if (Assumption)
936 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
937 isFeasible);
938 else
939 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
940 isFeasible);
941 }
942
Ted Kremenek08b66252008-02-06 04:31:33 +0000943 case nonlval::SymIntConstraintValKind:
944 return
945 AssumeSymInt(St, Assumption,
946 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
947 isFeasible);
948
Ted Kremenek329f8542008-02-05 21:52:21 +0000949 case nonlval::ConcreteIntKind: {
950 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000951 isFeasible = b ? Assumption : !Assumption;
952 return St;
953 }
954 }
955}
956
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000957GRExprEngine::StateTy
958GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000959 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000960
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000961 // First, determine if sym == X, where X != V.
962 if (const llvm::APSInt* X = St.getSymVal(sym)) {
963 isFeasible = *X != V;
964 return St;
965 }
966
967 // Second, determine if sym != V.
968 if (St.isNotEqual(sym, V)) {
969 isFeasible = true;
970 return St;
971 }
972
973 // If we reach here, sym is not a constant and we don't know if it is != V.
974 // Make that assumption.
975
976 isFeasible = true;
977 return StateMgr.AddNE(St, sym, V);
978}
979
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000980GRExprEngine::StateTy
981GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000982 const llvm::APSInt& V, bool& isFeasible) {
983
984 // First, determine if sym == X, where X != V.
985 if (const llvm::APSInt* X = St.getSymVal(sym)) {
986 isFeasible = *X == V;
987 return St;
988 }
989
990 // Second, determine if sym != V.
991 if (St.isNotEqual(sym, V)) {
992 isFeasible = false;
993 return St;
994 }
995
996 // If we reach here, sym is not a constant and we don't know if it is == V.
997 // Make that assumption.
998
999 isFeasible = true;
1000 return StateMgr.AddEQ(St, sym, V);
1001}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001002
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001003GRExprEngine::StateTy
1004GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001005 const SymIntConstraint& C, bool& isFeasible) {
1006
1007 switch (C.getOpcode()) {
1008 default:
1009 // No logic yet for other operators.
1010 return St;
1011
1012 case BinaryOperator::EQ:
1013 if (Assumption)
1014 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1015 else
1016 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1017
1018 case BinaryOperator::NE:
1019 if (Assumption)
1020 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1021 else
1022 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1023 }
1024}
1025
Ted Kremenekb38911f2008-01-30 23:03:39 +00001026//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001027// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001028//===----------------------------------------------------------------------===//
1029
Ted Kremenekaa66a322008-01-16 21:46:15 +00001030#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001031static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001032
Ted Kremenekaa66a322008-01-16 21:46:15 +00001033namespace llvm {
1034template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001035struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001036 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001037
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001038 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001039
1040 Out << "Variables:\\l";
1041
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001042 bool isFirst = true;
1043
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001044 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001045 E=St.vb_end(); I!=E;++I) {
1046
1047 if (isFirst)
1048 isFirst = false;
1049 else
1050 Out << "\\l";
1051
1052 Out << ' ' << I.getKey()->getName() << " : ";
1053 I.getData().print(Out);
1054 }
1055
1056 }
1057
Ted Kremeneke7d22112008-02-11 19:21:59 +00001058
Ted Kremenek44842c22008-02-13 18:06:44 +00001059 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001060
1061 bool isFirst = true;
1062
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001063 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001064 I != E;++I) {
1065
1066 if (isFirst) {
1067 Out << "\\l\\lSub-Expressions:\\l";
1068 isFirst = false;
1069 }
1070 else
1071 Out << "\\l";
1072
1073 Out << " (" << (void*) I.getKey() << ") ";
1074 I.getKey()->printPretty(Out);
1075 Out << " : ";
1076 I.getData().print(Out);
1077 }
1078 }
1079
Ted Kremenek44842c22008-02-13 18:06:44 +00001080 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001081
Ted Kremenek016f52f2008-02-08 21:10:02 +00001082 bool isFirst = true;
1083
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001084 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001085 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001086 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001087 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001088 isFirst = false;
1089 }
1090 else
1091 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001092
Ted Kremeneke7d22112008-02-11 19:21:59 +00001093 Out << " (" << (void*) I.getKey() << ") ";
1094 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001095 Out << " : ";
1096 I.getData().print(Out);
1097 }
1098 }
1099
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001100 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001101 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1102
1103 if (CE.isEmpty())
1104 return;
1105
1106 Out << "\\l\\|'==' constraints:";
1107
1108 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1109 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1110 }
1111
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001112 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001113 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1114
1115 if (NE.isEmpty())
1116 return;
1117
1118 Out << "\\l\\|'!=' constraints:";
1119
1120 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1121 I != EI; ++I){
1122
1123 Out << "\\l $" << I.getKey() << " : ";
1124 bool isFirst = true;
1125
1126 ValueState::IntSetTy::iterator J=I.getData().begin(),
1127 EJ=I.getData().end();
1128 for ( ; J != EJ; ++J) {
1129 if (isFirst) isFirst = false;
1130 else Out << ", ";
1131
1132 Out << (*J)->toString();
1133 }
1134 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001135 }
1136
1137 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1138
1139 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
1140 GraphPrintCheckerState->isExplicitNullDeref(N))
1141 return "color=\"red\",style=\"filled\"";
1142
1143 return "";
1144 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001145
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001146 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001147 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001148
1149 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001150 ProgramPoint Loc = N->getLocation();
1151
1152 switch (Loc.getKind()) {
1153 case ProgramPoint::BlockEntranceKind:
1154 Out << "Block Entrance: B"
1155 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1156 break;
1157
1158 case ProgramPoint::BlockExitKind:
1159 assert (false);
1160 break;
1161
1162 case ProgramPoint::PostStmtKind: {
1163 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001164 Out << L.getStmt()->getStmtClassName() << ':'
1165 << (void*) L.getStmt() << ' ';
1166
Ted Kremenekaa66a322008-01-16 21:46:15 +00001167 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001168
1169 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1170 Out << "\\|Implicit-Null Dereference.\\l";
1171 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001172 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1173 Out << "\\|Explicit-Null Dereference.\\l";
1174 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001175
Ted Kremenekaa66a322008-01-16 21:46:15 +00001176 break;
1177 }
1178
1179 default: {
1180 const BlockEdge& E = cast<BlockEdge>(Loc);
1181 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1182 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001183
1184 if (Stmt* T = E.getSrc()->getTerminator()) {
1185 Out << "\\|Terminator: ";
1186 E.getSrc()->printTerminator(Out);
1187
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001188 if (isa<SwitchStmt>(T)) {
1189 Stmt* Label = E.getDst()->getLabel();
1190
1191 if (Label) {
1192 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1193 Out << "\\lcase ";
1194 C->getLHS()->printPretty(Out);
1195
1196 if (Stmt* RHS = C->getRHS()) {
1197 Out << " .. ";
1198 RHS->printPretty(Out);
1199 }
1200
1201 Out << ":";
1202 }
1203 else {
1204 assert (isa<DefaultStmt>(Label));
1205 Out << "\\ldefault:";
1206 }
1207 }
1208 else
1209 Out << "\\l(implicit) default:";
1210 }
1211 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001212 // FIXME
1213 }
1214 else {
1215 Out << "\\lCondition: ";
1216 if (*E.getSrc()->succ_begin() == E.getDst())
1217 Out << "true";
1218 else
1219 Out << "false";
1220 }
1221
1222 Out << "\\l";
1223 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001224
1225 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1226 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1227 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001228 }
1229 }
1230
Ted Kremenek9153f732008-02-05 07:17:49 +00001231 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001232
Ted Kremeneke7d22112008-02-11 19:21:59 +00001233 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001234
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001235 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001236 return Out.str();
1237 }
1238};
1239} // end llvm namespace
1240#endif
1241
Ted Kremeneke01c9872008-02-14 22:36:46 +00001242void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001243#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001244 GraphPrintCheckerState = this;
1245 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001246 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001247#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001248}