blob: 4bcdd5fb70fd696487a45beafa39222b42557100 [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,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000476 NonLValue::GetValue(ValMgr, size, S->getType(), L)));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000477
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,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000576 U->getType(), L)));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000577
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 {
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000733 QualType T1 = B->getLHS()->getType();
734 QualType T2 = B->getRHS()->getType();
735
Ted Kremenekb2331832008-02-15 22:29:00 +0000736 // An operation between two variables of a non-lvalue type.
737 Result =
738 EvalBinaryOp(ValMgr, Op,
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000739 cast<NonLValue>(GetValue(N1->getState(), L1, &T1)),
740 cast<NonLValue>(GetValue(N2->getState(), L2, &T2)));
Ted Kremenekb2331832008-02-15 22:29:00 +0000741 }
Ted Kremenek687af802008-01-29 19:43:15 +0000742 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000743 else { // Any other operation between two Non-LValues.
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000744 QualType T = B->getLHS()->getType();
745 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(),
746 L1, &T));
Ted Kremenek687af802008-01-29 19:43:15 +0000747 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000748 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000749 }
750
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000751 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000752 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000753 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000754 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000755 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000756 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000757}
Ted Kremenekee985462008-01-16 18:18:48 +0000758
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000759
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000760void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
761 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000762
763 // FIXME: add metadata to the CFG so that we can disable
764 // this check when we KNOW that there is no block-level subexpression.
765 // The motivation is that this check requires a hashtable lookup.
766
767 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
768 Dst.Add(Pred);
769 return;
770 }
771
772 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000773
774 default:
775 // Cases we intentionally have "default" handle:
776 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
777
778 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
779 break;
780
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000781 case Stmt::BinaryOperatorClass: {
782 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000783
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000784 if (B->isLogicalOp()) {
785 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000786 break;
787 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000788 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000789 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000790 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000791 break;
792 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000793
794 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
795 break;
796 }
797
798 case Stmt::CastExprClass: {
799 CastExpr* C = cast<CastExpr>(S);
800 VisitCast(C, C->getSubExpr(), Pred, Dst);
801 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000802 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000803
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000804 case Stmt::ChooseExprClass: { // __builtin_choose_expr
805 ChooseExpr* C = cast<ChooseExpr>(S);
806 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
807 break;
808 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000809
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000810 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000811 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
812 break;
813
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000814 case Stmt::ConditionalOperatorClass: { // '?' operator
815 ConditionalOperator* C = cast<ConditionalOperator>(S);
816 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
817 break;
818 }
819
820 case Stmt::DeclRefExprClass:
821 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
822 break;
823
824 case Stmt::DeclStmtClass:
825 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
826 break;
827
828 case Stmt::ImplicitCastExprClass: {
829 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
830 VisitCast(C, C->getSubExpr(), Pred, Dst);
831 break;
832 }
833
834 case Stmt::ParenExprClass:
835 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
836 break;
837
838 case Stmt::SizeOfAlignOfTypeExprClass:
839 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
840 break;
841
Ted Kremenekda9bd092008-02-08 07:05:39 +0000842 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000843 StmtExpr* SE = cast<StmtExpr>(S);
844
Ted Kremenekda9bd092008-02-08 07:05:39 +0000845 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000846 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
847 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000848 break;
849 }
850
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000851 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000852 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
853 Visit(R, Pred, Dst);
854 else
855 Dst.Add(Pred);
856
857 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000858 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000859
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000860 case Stmt::UnaryOperatorClass:
861 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000862 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000863 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000864}
865
Ted Kremenekee985462008-01-16 18:18:48 +0000866//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000867// "Assume" logic.
868//===----------------------------------------------------------------------===//
869
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000870GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000871 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000872 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000873
874 switch (Cond.getSubKind()) {
875 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000876 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000877 return St;
878
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000879 case lval::SymbolValKind:
880 if (Assumption)
881 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
882 ValMgr.getZeroWithPtrWidth(), isFeasible);
883 else
884 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
885 ValMgr.getZeroWithPtrWidth(), isFeasible);
886
Ted Kremenek08b66252008-02-06 04:31:33 +0000887
Ted Kremenek329f8542008-02-05 21:52:21 +0000888 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000889 isFeasible = Assumption;
890 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000891
Ted Kremenek329f8542008-02-05 21:52:21 +0000892 case lval::ConcreteIntKind: {
893 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000894 isFeasible = b ? Assumption : !Assumption;
895 return St;
896 }
897 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000898}
899
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000900GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000901 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000902 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000903
904 switch (Cond.getSubKind()) {
905 default:
906 assert (false && "'Assume' not implemented for this NonLValue.");
907 return St;
908
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000909
910 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000911 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000912 SymbolID sym = SV.getSymbol();
913
914 if (Assumption)
915 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
916 isFeasible);
917 else
918 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
919 isFeasible);
920 }
921
Ted Kremenek08b66252008-02-06 04:31:33 +0000922 case nonlval::SymIntConstraintValKind:
923 return
924 AssumeSymInt(St, Assumption,
925 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
926 isFeasible);
927
Ted Kremenek329f8542008-02-05 21:52:21 +0000928 case nonlval::ConcreteIntKind: {
929 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000930 isFeasible = b ? Assumption : !Assumption;
931 return St;
932 }
933 }
934}
935
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000936GRExprEngine::StateTy
937GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000938 const llvm::APSInt& V, bool& isFeasible) {
939
940 // First, determine if sym == X, where X != V.
941 if (const llvm::APSInt* X = St.getSymVal(sym)) {
942 isFeasible = *X != V;
943 return St;
944 }
945
946 // Second, determine if sym != V.
947 if (St.isNotEqual(sym, V)) {
948 isFeasible = true;
949 return St;
950 }
951
952 // If we reach here, sym is not a constant and we don't know if it is != V.
953 // Make that assumption.
954
955 isFeasible = true;
956 return StateMgr.AddNE(St, sym, V);
957}
958
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000959GRExprEngine::StateTy
960GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000961 const llvm::APSInt& V, bool& isFeasible) {
962
963 // First, determine if sym == X, where X != V.
964 if (const llvm::APSInt* X = St.getSymVal(sym)) {
965 isFeasible = *X == V;
966 return St;
967 }
968
969 // Second, determine if sym != V.
970 if (St.isNotEqual(sym, V)) {
971 isFeasible = false;
972 return St;
973 }
974
975 // If we reach here, sym is not a constant and we don't know if it is == V.
976 // Make that assumption.
977
978 isFeasible = true;
979 return StateMgr.AddEQ(St, sym, V);
980}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000981
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000982GRExprEngine::StateTy
983GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +0000984 const SymIntConstraint& C, bool& isFeasible) {
985
986 switch (C.getOpcode()) {
987 default:
988 // No logic yet for other operators.
989 return St;
990
991 case BinaryOperator::EQ:
992 if (Assumption)
993 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
994 else
995 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
996
997 case BinaryOperator::NE:
998 if (Assumption)
999 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1000 else
1001 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1002 }
1003}
1004
Ted Kremenekb38911f2008-01-30 23:03:39 +00001005//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001006// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001007//===----------------------------------------------------------------------===//
1008
Ted Kremenekaa66a322008-01-16 21:46:15 +00001009#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001010static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001011
Ted Kremenekaa66a322008-01-16 21:46:15 +00001012namespace llvm {
1013template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001014struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001015 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001016
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001017 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001018
1019 Out << "Variables:\\l";
1020
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001021 bool isFirst = true;
1022
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001023 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001024 E=St.vb_end(); I!=E;++I) {
1025
1026 if (isFirst)
1027 isFirst = false;
1028 else
1029 Out << "\\l";
1030
1031 Out << ' ' << I.getKey()->getName() << " : ";
1032 I.getData().print(Out);
1033 }
1034
1035 }
1036
Ted Kremeneke7d22112008-02-11 19:21:59 +00001037
Ted Kremenek44842c22008-02-13 18:06:44 +00001038 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001039
1040 bool isFirst = true;
1041
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001042 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001043 I != E;++I) {
1044
1045 if (isFirst) {
1046 Out << "\\l\\lSub-Expressions:\\l";
1047 isFirst = false;
1048 }
1049 else
1050 Out << "\\l";
1051
1052 Out << " (" << (void*) I.getKey() << ") ";
1053 I.getKey()->printPretty(Out);
1054 Out << " : ";
1055 I.getData().print(Out);
1056 }
1057 }
1058
Ted Kremenek44842c22008-02-13 18:06:44 +00001059 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001060
Ted Kremenek016f52f2008-02-08 21:10:02 +00001061 bool isFirst = true;
1062
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001063 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001064 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001065 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001066 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001067 isFirst = false;
1068 }
1069 else
1070 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001071
Ted Kremeneke7d22112008-02-11 19:21:59 +00001072 Out << " (" << (void*) I.getKey() << ") ";
1073 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001074 Out << " : ";
1075 I.getData().print(Out);
1076 }
1077 }
1078
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001079 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001080 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1081
1082 if (CE.isEmpty())
1083 return;
1084
1085 Out << "\\l\\|'==' constraints:";
1086
1087 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1088 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1089 }
1090
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001091 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001092 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1093
1094 if (NE.isEmpty())
1095 return;
1096
1097 Out << "\\l\\|'!=' constraints:";
1098
1099 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1100 I != EI; ++I){
1101
1102 Out << "\\l $" << I.getKey() << " : ";
1103 bool isFirst = true;
1104
1105 ValueState::IntSetTy::iterator J=I.getData().begin(),
1106 EJ=I.getData().end();
1107 for ( ; J != EJ; ++J) {
1108 if (isFirst) isFirst = false;
1109 else Out << ", ";
1110
1111 Out << (*J)->toString();
1112 }
1113 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001114 }
1115
1116 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1117
1118 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
1119 GraphPrintCheckerState->isExplicitNullDeref(N))
1120 return "color=\"red\",style=\"filled\"";
1121
1122 return "";
1123 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001124
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001125 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001126 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001127
1128 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001129 ProgramPoint Loc = N->getLocation();
1130
1131 switch (Loc.getKind()) {
1132 case ProgramPoint::BlockEntranceKind:
1133 Out << "Block Entrance: B"
1134 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1135 break;
1136
1137 case ProgramPoint::BlockExitKind:
1138 assert (false);
1139 break;
1140
1141 case ProgramPoint::PostStmtKind: {
1142 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001143 Out << L.getStmt()->getStmtClassName() << ':'
1144 << (void*) L.getStmt() << ' ';
1145
Ted Kremenekaa66a322008-01-16 21:46:15 +00001146 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001147
1148 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1149 Out << "\\|Implicit-Null Dereference.\\l";
1150 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001151 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1152 Out << "\\|Explicit-Null Dereference.\\l";
1153 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001154
Ted Kremenekaa66a322008-01-16 21:46:15 +00001155 break;
1156 }
1157
1158 default: {
1159 const BlockEdge& E = cast<BlockEdge>(Loc);
1160 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1161 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001162
1163 if (Stmt* T = E.getSrc()->getTerminator()) {
1164 Out << "\\|Terminator: ";
1165 E.getSrc()->printTerminator(Out);
1166
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001167 if (isa<SwitchStmt>(T)) {
1168 Stmt* Label = E.getDst()->getLabel();
1169
1170 if (Label) {
1171 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1172 Out << "\\lcase ";
1173 C->getLHS()->printPretty(Out);
1174
1175 if (Stmt* RHS = C->getRHS()) {
1176 Out << " .. ";
1177 RHS->printPretty(Out);
1178 }
1179
1180 Out << ":";
1181 }
1182 else {
1183 assert (isa<DefaultStmt>(Label));
1184 Out << "\\ldefault:";
1185 }
1186 }
1187 else
1188 Out << "\\l(implicit) default:";
1189 }
1190 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001191 // FIXME
1192 }
1193 else {
1194 Out << "\\lCondition: ";
1195 if (*E.getSrc()->succ_begin() == E.getDst())
1196 Out << "true";
1197 else
1198 Out << "false";
1199 }
1200
1201 Out << "\\l";
1202 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001203
1204 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1205 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1206 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001207 }
1208 }
1209
Ted Kremenek9153f732008-02-05 07:17:49 +00001210 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001211
Ted Kremeneke7d22112008-02-11 19:21:59 +00001212 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001213
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001214 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001215 return Out.str();
1216 }
1217};
1218} // end llvm namespace
1219#endif
1220
Ted Kremeneke01c9872008-02-14 22:36:46 +00001221void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001222#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001223 GraphPrintCheckerState = this;
1224 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001225 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001226#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001227}