blob: 33f7f5150439b09bf5f6c75d9bfd7e90379f73d1 [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();
212 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
213
214 if (isa<UninitializedVal>(CondV)) {
215 NodeTy* N = builder.generateDefaultCaseNode(St, true);
216 UninitBranches.insert(N);
217 return;
218 }
219
220 StateTy DefaultSt = St;
221
222 // While most of this can be assumed (such as the signedness), having it
223 // just computed makes sure everything makes the same assumptions end-to-end.
224 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
225 APSInt V1(bits, false);
226 APSInt V2 = V1;
227
228 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
229
230 CaseStmt* Case = cast<CaseStmt>(I.getCase());
231
232 // Evaluate the case.
233 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
234 assert (false && "Case condition must evaluate to an integer constant.");
235 return;
236 }
237
238 // Get the RHS of the case, if it exists.
239
240 if (Expr* E = Case->getRHS()) {
241 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
242 assert (false &&
243 "Case condition (RHS) must evaluate to an integer constant.");
244 return ;
245 }
246
247 assert (V1 <= V2);
248 }
249 else V2 = V1;
250
251 // FIXME: Eventually we should replace the logic below with a range
252 // comparison, rather than concretize the values within the range.
253 // This should be easy once we have "ranges" for NonLValues.
254
255 do {
256 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
257
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000258 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000259
260 // Now "assume" that the case matches.
261 bool isFeasible;
262
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000263 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000264
265 if (isFeasible) {
266 builder.generateCaseStmtNode(I, StNew);
267
268 // If CondV evaluates to a constant, then we know that this
269 // is the *only* case that we can take, so stop evaluating the
270 // others.
271 if (isa<nonlval::ConcreteInt>(CondV))
272 return;
273 }
274
275 // Now "assume" that the case doesn't match. Add this state
276 // to the default state (if it is feasible).
277
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000278 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000279
280 if (isFeasible)
281 DefaultSt = StNew;
282
283 // Concretize the next value in the range.
284 ++V1;
285
286 } while (V1 < V2);
287 }
288
289 // If we reach here, than we know that the default branch is
290 // possible.
291 builder.generateDefaultCaseNode(DefaultSt);
292}
293
294
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000295void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000296 NodeSet& Dst) {
297
298 bool hasR2;
299 StateTy PrevState = Pred->getState();
300
301 RValue R1 = GetValue(PrevState, B->getLHS());
302 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
303
Ted Kremenek22031182008-02-08 02:57:34 +0000304 if (isa<UnknownVal>(R1) &&
305 (isa<UnknownVal>(R2) ||
306 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000307
308 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
309 return;
310 }
Ted Kremenek22031182008-02-08 02:57:34 +0000311 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000312 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
313 return;
314 }
315
316 // R1 is an expression that can evaluate to either 'true' or 'false'.
317 if (B->getOpcode() == BinaryOperator::LAnd) {
318 // hasR2 == 'false' means that LHS evaluated to 'false' and that
319 // we short-circuited, leading to a value of '0' for the '&&' expression.
320 if (hasR2 == false) {
321 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
322 return;
323 }
324 }
325 else {
326 assert (B->getOpcode() == BinaryOperator::LOr);
327 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
328 // we short-circuited, leading to a value of '1' for the '||' expression.
329 if (hasR2 == false) {
330 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
331 return;
332 }
333 }
334
335 // If we reach here we did not short-circuit. Assume R2 == true and
336 // R2 == false.
337
338 bool isFeasible;
339 StateTy St = Assume(PrevState, R2, true, isFeasible);
340
341 if (isFeasible)
342 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
343
344 St = Assume(PrevState, R2, false, isFeasible);
345
346 if (isFeasible)
347 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
348}
349
350
351
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000352void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000353 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000354
355 StmtEntryNode = builder.getLastNode();
356 CurrentStmt = S;
357 NodeSet Dst;
358 StateCleaned = false;
359
360 Visit(S, StmtEntryNode, Dst);
361
362 // If no nodes were generated, generate a new node that has all the
363 // dead mappings removed.
364 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
365 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
366 builder.generateNode(S, St, StmtEntryNode);
367 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000368
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000369 CurrentStmt = NULL;
370 StmtEntryNode = NULL;
371 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000372}
373
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000374GRExprEngine::NodeTy*
375GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000376
377 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000378 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000379 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000380
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000381 NodeTy* N = Builder->generateNode(S, St, Pred);
382 Dst.Add(N);
383 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000384}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000385
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000386void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000387 const StateTy::BufferTy& SB) {
388
389 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
390 Nodify(Dst, S, Pred, *I);
391}
392
Ted Kremenek44842c22008-02-13 18:06:44 +0000393void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000394 if (D != CurrentStmt) {
395 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
396 return;
397 }
398
399 // If we are here, we are loading the value of the decl and binding
400 // it to the block-level expression.
401
402 StateTy St = Pred->getState();
403
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000404 Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000405}
406
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000407void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000408
409 QualType T = CastE->getType();
410
411 // Check for redundant casts.
412 if (E->getType() == T) {
413 Dst.Add(Pred);
414 return;
415 }
416
417 NodeSet S1;
418 Visit(E, Pred, S1);
419
420 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
421 NodeTy* N = *I1;
422 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000423 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000424 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000425 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000426}
427
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000428void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
429 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000430
431 StateTy St = Pred->getState();
432
433 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000434 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
435 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000436 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000437 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000438 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000439
440 Nodify(Dst, DS, Pred, St);
441
442 if (Dst.empty())
443 Dst.Add(Pred);
444}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000445
Ted Kremenekf233d482008-02-05 00:26:40 +0000446
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000447void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000448 NodeTy* Pred, NodeSet& Dst) {
449
450 StateTy St = Pred->getState();
451
452 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000453 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000454
455 Nodify(Dst, S, Pred, SetValue(St, S, R));
456}
457
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000458/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000459void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000460 NodeTy* Pred,
461 NodeSet& Dst) {
462
463 // 6.5.3.4 sizeof: "The result type is an integer."
464
465 QualType T = S->getArgumentType();
466
467 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000468 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000469 return;
470
471 SourceLocation L = S->getExprLoc();
472 uint64_t size = getContext().getTypeSize(T, L) / 8;
473
474 Nodify(Dst, S, Pred,
475 SetValue(Pred->getState(), S,
476 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
477
478}
479
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000480void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
481 GRExprEngine::NodeTy* Pred,
482 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000483
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000484 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000485 UnaryOperator::Opcode Op = U->getOpcode();
486
487 // FIXME: This is a hack so that for '*' and '&' we don't recurse
488 // on visiting the subexpression if it is a DeclRefExpr. We should
489 // probably just handle AddrOf and Deref in their own methods to make
490 // this cleaner.
491 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
492 isa<DeclRefExpr>(U->getSubExpr()))
493 S1.Add(Pred);
494 else
495 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000496
497 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
498 NodeTy* N1 = *I1;
499 StateTy St = N1->getState();
500
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000501 // Handle ++ and -- (both pre- and post-increment).
502
503 if (U->isIncrementDecrementOp()) {
504 const LValue& L1 = GetLValue(St, U->getSubExpr());
505 RValue R1 = GetValue(St, L1);
506
507 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
508 : BinaryOperator::Sub;
509
510 RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U));
511
512 if (U->isPostfix())
513 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
514 else
515 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
516
517 continue;
518 }
519
520 // Handle all other unary operators.
521
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000522 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000523
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000524 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000525 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000526 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000527 break;
528 }
529
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000530 case UnaryOperator::Not: {
531 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000532 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000533 break;
534 }
535
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000536 case UnaryOperator::LNot: {
537 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
538 //
539 // Note: technically we do "E == 0", but this is the same in the
540 // transfer functions as "0 == E".
541
542 RValue V1 = GetValue(St, U->getSubExpr());
543
544 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000545 const LValue& L1 = cast<LValue>(V1);
546 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
547 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000548 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
549 L1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000550 }
551 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000552 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000553 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000554 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000555 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
556 R1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000557 }
558
559 break;
560 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000561
562 case UnaryOperator::SizeOf: {
563 // 6.5.3.4 sizeof: "The result type is an integer."
564
565 QualType T = U->getSubExpr()->getType();
566
567 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000568 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000569 return;
570
571 SourceLocation L = U->getExprLoc();
572 uint64_t size = getContext().getTypeSize(T, L) / 8;
573
574 Nodify(Dst, U, N1,
575 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
576 getContext().IntTy, L)));
577
578 break;
579 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000580
Ted Kremenek64924852008-01-31 02:35:41 +0000581 case UnaryOperator::AddrOf: {
582 const LValue& L1 = GetLValue(St, U->getSubExpr());
583 Nodify(Dst, U, N1, SetValue(St, U, L1));
584 break;
585 }
586
587 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000588 // FIXME: Stop when dereferencing an uninitialized value.
589 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
590
591 const RValue& V = GetValue(St, U->getSubExpr());
592 const LValue& L1 = cast<LValue>(V);
593
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000594 // After a dereference, one of two possible situations arise:
595 // (1) A crash, because the pointer was NULL.
596 // (2) The pointer is not NULL, and the dereference works.
597 //
598 // We add these assumptions.
599
Ted Kremenek63a4f692008-02-07 06:04:18 +0000600 bool isFeasibleNotNull;
601
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000602 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000603 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
604
605 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000606 QualType T = U->getType();
607 Nodify(Dst, U, N1, SetValue(StNotNull, U,
608 GetValue(StNotNull, L1, &T)));
609 }
610
Ted Kremenek63a4f692008-02-07 06:04:18 +0000611 bool isFeasibleNull;
612
613 // "Assume" that the pointer is NULL.
614 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
615
616 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000617 // We don't use "Nodify" here because the node will be a sink
618 // and we have no intention of processing it later.
619 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
620
Ted Kremenek63a4f692008-02-07 06:04:18 +0000621 if (NullNode) {
622 NullNode->markAsSink();
623
624 if (isFeasibleNotNull)
625 ImplicitNullDeref.insert(NullNode);
626 else
627 ExplicitNullDeref.insert(NullNode);
628 }
629 }
630
Ted Kremenek64924852008-01-31 02:35:41 +0000631 break;
632 }
633
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000634 default: ;
635 assert (false && "Not implemented.");
636 }
637 }
638}
639
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000640void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
641 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000642
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000643 if (isa<DeclRefExpr>(E)) {
644 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000645 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000646 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000647
648 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
649 if (U->getOpcode() == UnaryOperator::Deref) {
650 Visit(U->getSubExpr(), Pred, Dst);
651 return;
652 }
653 }
654
655 Visit(E, Pred, Dst);
656}
657
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000658void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000659 GRExprEngine::NodeTy* Pred,
660 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000661 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000662
663 if (B->isAssignmentOp())
664 VisitAssignmentLHS(B->getLHS(), Pred, S1);
665 else
666 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000667
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000668 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
669 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000670
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000671 // When getting the value for the LHS, check if we are in an assignment.
672 // In such cases, we want to (initially) treat the LHS as an LValue,
673 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000674 // evaluated to LValueDecl's instead of to an NonLValue.
675 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000676 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
677 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000678
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000679 NodeSet S2;
680 Visit(B->getRHS(), N1, S2);
681
682 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000683
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000684 NodeTy* N2 = *I2;
685 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000686 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000687
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000688 BinaryOperator::Opcode Op = B->getOpcode();
689
690 if (Op <= BinaryOperator::Or) {
691
Ted Kremenek22031182008-02-08 02:57:34 +0000692 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000693 Nodify(Dst, B, N2, SetValue(St, B, V1));
694 continue;
695 }
696
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000697 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2)));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000698 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000699
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000700 }
701
702 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000703 case BinaryOperator::Assign: {
704 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000705 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000706 break;
707 }
708
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000709 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000710
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000711 assert (B->isCompoundAssignmentOp());
712
713 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000714 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000715
Ted Kremenekda9bd092008-02-08 07:05:39 +0000716 if (Op >= BinaryOperator::AndAssign)
717 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
718 else
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000719 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000720
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000721 if (B->getType()->isPointerType()) { // Perform pointer arithmetic.
722 const NonLValue& R2 = cast<NonLValue>(V2);
723 Result = EvalBinaryOp(ValMgr, Op, L1, R2);
724 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000725 else if (isa<LValue>(V2)) {
Ted Kremenek687af802008-01-29 19:43:15 +0000726 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekb2331832008-02-15 22:29:00 +0000727
728 if (B->getRHS()->getType()->isPointerType()) {
729 // LValue comparison.
730 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
731 }
732 else {
733 // An operation between two variables of a non-lvalue type.
734 Result =
735 EvalBinaryOp(ValMgr, Op,
736 cast<NonLValue>(GetValue(N1->getState(), L1)),
737 cast<NonLValue>(GetValue(N2->getState(), L2)));
738 }
Ted Kremenek687af802008-01-29 19:43:15 +0000739 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000740 else { // Any other operation between two Non-LValues.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000741 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000742 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000743 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000744 }
745
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000746 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000747 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000748 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000749 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000750 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000751 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000752}
Ted Kremenekee985462008-01-16 18:18:48 +0000753
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000754
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000755void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
756 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000757
758 // FIXME: add metadata to the CFG so that we can disable
759 // this check when we KNOW that there is no block-level subexpression.
760 // The motivation is that this check requires a hashtable lookup.
761
762 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
763 Dst.Add(Pred);
764 return;
765 }
766
767 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000768
769 default:
770 // Cases we intentionally have "default" handle:
771 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
772
773 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
774 break;
775
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000776 case Stmt::BinaryOperatorClass: {
777 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000778
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000779 if (B->isLogicalOp()) {
780 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000781 break;
782 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000783 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000784 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000785 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000786 break;
787 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000788
789 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
790 break;
791 }
792
793 case Stmt::CastExprClass: {
794 CastExpr* C = cast<CastExpr>(S);
795 VisitCast(C, C->getSubExpr(), Pred, Dst);
796 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000797 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000798
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000799 case Stmt::ChooseExprClass: { // __builtin_choose_expr
800 ChooseExpr* C = cast<ChooseExpr>(S);
801 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
802 break;
803 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000804
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000805 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000806 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
807 break;
808
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000809 case Stmt::ConditionalOperatorClass: { // '?' operator
810 ConditionalOperator* C = cast<ConditionalOperator>(S);
811 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
812 break;
813 }
814
815 case Stmt::DeclRefExprClass:
816 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
817 break;
818
819 case Stmt::DeclStmtClass:
820 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
821 break;
822
823 case Stmt::ImplicitCastExprClass: {
824 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
825 VisitCast(C, C->getSubExpr(), Pred, Dst);
826 break;
827 }
828
829 case Stmt::ParenExprClass:
830 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
831 break;
832
833 case Stmt::SizeOfAlignOfTypeExprClass:
834 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
835 break;
836
Ted Kremenekda9bd092008-02-08 07:05:39 +0000837 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000838 StmtExpr* SE = cast<StmtExpr>(S);
839
Ted Kremenekda9bd092008-02-08 07:05:39 +0000840 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000841 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
842 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000843 break;
844 }
845
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000846 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000847 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
848 Visit(R, Pred, Dst);
849 else
850 Dst.Add(Pred);
851
852 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000853 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000854
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000855 case Stmt::UnaryOperatorClass:
856 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000857 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000858 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000859}
860
Ted Kremenekee985462008-01-16 18:18:48 +0000861//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000862// "Assume" logic.
863//===----------------------------------------------------------------------===//
864
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000865GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000866 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000867 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000868
869 switch (Cond.getSubKind()) {
870 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000871 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000872 return St;
873
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000874 case lval::SymbolValKind:
875 if (Assumption)
876 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
877 ValMgr.getZeroWithPtrWidth(), isFeasible);
878 else
879 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
880 ValMgr.getZeroWithPtrWidth(), isFeasible);
881
Ted Kremenek08b66252008-02-06 04:31:33 +0000882
Ted Kremenek329f8542008-02-05 21:52:21 +0000883 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000884 isFeasible = Assumption;
885 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000886
Ted Kremenek329f8542008-02-05 21:52:21 +0000887 case lval::ConcreteIntKind: {
888 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000889 isFeasible = b ? Assumption : !Assumption;
890 return St;
891 }
892 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000893}
894
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000895GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000896 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000897 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000898
899 switch (Cond.getSubKind()) {
900 default:
901 assert (false && "'Assume' not implemented for this NonLValue.");
902 return St;
903
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000904
905 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000906 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000907 SymbolID sym = SV.getSymbol();
908
909 if (Assumption)
910 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
911 isFeasible);
912 else
913 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
914 isFeasible);
915 }
916
Ted Kremenek08b66252008-02-06 04:31:33 +0000917 case nonlval::SymIntConstraintValKind:
918 return
919 AssumeSymInt(St, Assumption,
920 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
921 isFeasible);
922
Ted Kremenek329f8542008-02-05 21:52:21 +0000923 case nonlval::ConcreteIntKind: {
924 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000925 isFeasible = b ? Assumption : !Assumption;
926 return St;
927 }
928 }
929}
930
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000931GRExprEngine::StateTy
932GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000933 const llvm::APSInt& V, bool& isFeasible) {
934
935 // First, determine if sym == X, where X != V.
936 if (const llvm::APSInt* X = St.getSymVal(sym)) {
937 isFeasible = *X != V;
938 return St;
939 }
940
941 // Second, determine if sym != V.
942 if (St.isNotEqual(sym, V)) {
943 isFeasible = true;
944 return St;
945 }
946
947 // If we reach here, sym is not a constant and we don't know if it is != V.
948 // Make that assumption.
949
950 isFeasible = true;
951 return StateMgr.AddNE(St, sym, V);
952}
953
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000954GRExprEngine::StateTy
955GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000956 const llvm::APSInt& V, bool& isFeasible) {
957
958 // First, determine if sym == X, where X != V.
959 if (const llvm::APSInt* X = St.getSymVal(sym)) {
960 isFeasible = *X == V;
961 return St;
962 }
963
964 // Second, determine if sym != V.
965 if (St.isNotEqual(sym, V)) {
966 isFeasible = false;
967 return St;
968 }
969
970 // If we reach here, sym is not a constant and we don't know if it is == V.
971 // Make that assumption.
972
973 isFeasible = true;
974 return StateMgr.AddEQ(St, sym, V);
975}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000976
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000977GRExprEngine::StateTy
978GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +0000979 const SymIntConstraint& C, bool& isFeasible) {
980
981 switch (C.getOpcode()) {
982 default:
983 // No logic yet for other operators.
984 return St;
985
986 case BinaryOperator::EQ:
987 if (Assumption)
988 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
989 else
990 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
991
992 case BinaryOperator::NE:
993 if (Assumption)
994 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
995 else
996 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
997 }
998}
999
Ted Kremenekb38911f2008-01-30 23:03:39 +00001000//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001001// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001002//===----------------------------------------------------------------------===//
1003
Ted Kremenekaa66a322008-01-16 21:46:15 +00001004#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001005static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001006
Ted Kremenekaa66a322008-01-16 21:46:15 +00001007namespace llvm {
1008template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001009struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001010 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001011
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001012 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001013
1014 Out << "Variables:\\l";
1015
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001016 bool isFirst = true;
1017
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001018 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001019 E=St.vb_end(); I!=E;++I) {
1020
1021 if (isFirst)
1022 isFirst = false;
1023 else
1024 Out << "\\l";
1025
1026 Out << ' ' << I.getKey()->getName() << " : ";
1027 I.getData().print(Out);
1028 }
1029
1030 }
1031
Ted Kremeneke7d22112008-02-11 19:21:59 +00001032
Ted Kremenek44842c22008-02-13 18:06:44 +00001033 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001034
1035 bool isFirst = true;
1036
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001037 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001038 I != E;++I) {
1039
1040 if (isFirst) {
1041 Out << "\\l\\lSub-Expressions:\\l";
1042 isFirst = false;
1043 }
1044 else
1045 Out << "\\l";
1046
1047 Out << " (" << (void*) I.getKey() << ") ";
1048 I.getKey()->printPretty(Out);
1049 Out << " : ";
1050 I.getData().print(Out);
1051 }
1052 }
1053
Ted Kremenek44842c22008-02-13 18:06:44 +00001054 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001055
Ted Kremenek016f52f2008-02-08 21:10:02 +00001056 bool isFirst = true;
1057
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001058 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001059 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001060 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001061 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001062 isFirst = false;
1063 }
1064 else
1065 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001066
Ted Kremeneke7d22112008-02-11 19:21:59 +00001067 Out << " (" << (void*) I.getKey() << ") ";
1068 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001069 Out << " : ";
1070 I.getData().print(Out);
1071 }
1072 }
1073
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001074 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001075 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1076
1077 if (CE.isEmpty())
1078 return;
1079
1080 Out << "\\l\\|'==' constraints:";
1081
1082 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1083 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1084 }
1085
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001086 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001087 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1088
1089 if (NE.isEmpty())
1090 return;
1091
1092 Out << "\\l\\|'!=' constraints:";
1093
1094 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1095 I != EI; ++I){
1096
1097 Out << "\\l $" << I.getKey() << " : ";
1098 bool isFirst = true;
1099
1100 ValueState::IntSetTy::iterator J=I.getData().begin(),
1101 EJ=I.getData().end();
1102 for ( ; J != EJ; ++J) {
1103 if (isFirst) isFirst = false;
1104 else Out << ", ";
1105
1106 Out << (*J)->toString();
1107 }
1108 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001109 }
1110
1111 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1112
1113 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
1114 GraphPrintCheckerState->isExplicitNullDeref(N))
1115 return "color=\"red\",style=\"filled\"";
1116
1117 return "";
1118 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001119
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001120 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001121 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001122
1123 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001124 ProgramPoint Loc = N->getLocation();
1125
1126 switch (Loc.getKind()) {
1127 case ProgramPoint::BlockEntranceKind:
1128 Out << "Block Entrance: B"
1129 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1130 break;
1131
1132 case ProgramPoint::BlockExitKind:
1133 assert (false);
1134 break;
1135
1136 case ProgramPoint::PostStmtKind: {
1137 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001138 Out << L.getStmt()->getStmtClassName() << ':'
1139 << (void*) L.getStmt() << ' ';
1140
Ted Kremenekaa66a322008-01-16 21:46:15 +00001141 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001142
1143 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1144 Out << "\\|Implicit-Null Dereference.\\l";
1145 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001146 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1147 Out << "\\|Explicit-Null Dereference.\\l";
1148 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001149
Ted Kremenekaa66a322008-01-16 21:46:15 +00001150 break;
1151 }
1152
1153 default: {
1154 const BlockEdge& E = cast<BlockEdge>(Loc);
1155 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1156 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001157
1158 if (Stmt* T = E.getSrc()->getTerminator()) {
1159 Out << "\\|Terminator: ";
1160 E.getSrc()->printTerminator(Out);
1161
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001162 if (isa<SwitchStmt>(T)) {
1163 Stmt* Label = E.getDst()->getLabel();
1164
1165 if (Label) {
1166 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1167 Out << "\\lcase ";
1168 C->getLHS()->printPretty(Out);
1169
1170 if (Stmt* RHS = C->getRHS()) {
1171 Out << " .. ";
1172 RHS->printPretty(Out);
1173 }
1174
1175 Out << ":";
1176 }
1177 else {
1178 assert (isa<DefaultStmt>(Label));
1179 Out << "\\ldefault:";
1180 }
1181 }
1182 else
1183 Out << "\\l(implicit) default:";
1184 }
1185 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001186 // FIXME
1187 }
1188 else {
1189 Out << "\\lCondition: ";
1190 if (*E.getSrc()->succ_begin() == E.getDst())
1191 Out << "true";
1192 else
1193 Out << "false";
1194 }
1195
1196 Out << "\\l";
1197 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001198
1199 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1200 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1201 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001202 }
1203 }
1204
Ted Kremenek9153f732008-02-05 07:17:49 +00001205 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001206
Ted Kremeneke7d22112008-02-11 19:21:59 +00001207 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001208
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001209 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001210 return Out.str();
1211 }
1212};
1213} // end llvm namespace
1214#endif
1215
Ted Kremeneke01c9872008-02-14 22:36:46 +00001216void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001217#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001218 GraphPrintCheckerState = this;
1219 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001220 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001221#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001222}