blob: 5702198b381e53f840838134572c990096907468 [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 Kremenekb38911f2008-01-30 23:03:39 +000078 RValue V = GetValue(PrevState, Condition);
79
80 switch (V.getBaseKind()) {
81 default:
82 break;
83
Ted Kremenek53c641a2008-02-08 03:02:48 +000084 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +000085 builder.generateNode(PrevState, true);
86 builder.generateNode(PrevState, false);
87 return;
88
89 case RValue::UninitializedKind: {
90 NodeTy* N = builder.generateNode(PrevState, true);
91
92 if (N) {
93 N->markAsSink();
94 UninitBranches.insert(N);
95 }
96
97 builder.markInfeasible(false);
98 return;
99 }
100 }
101
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000102 // Get the current block counter.
103 GRBlockCounter BC = builder.getBlockCounter();
104
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000105 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
106 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000107
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000108 if (isa<nonlval::ConcreteInt>(V) ||
109 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
110
111 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000112
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000113 bool isFeasible = true;
114
115 StateTy St = Assume(PrevState, V, true, isFeasible);
116
117 if (isFeasible)
118 builder.generateNode(St, true);
119 else
120 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000121 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000122 else
123 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000124
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000125 BlockID = builder.getTargetBlock(false)->getBlockID();
126 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000127
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000128 if (isa<nonlval::ConcreteInt>(V) ||
129 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
130
131 // Process the false branch.
132
133 bool isFeasible = false;
134
135 StateTy St = Assume(PrevState, V, false, isFeasible);
136
137 if (isFeasible)
138 builder.generateNode(St, false);
139 else
140 builder.markInfeasible(false);
141 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000142 else
143 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000144}
145
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000146/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000147/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000148void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000149
150 StateTy St = builder.getState();
151 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
152
153 // Three possibilities:
154 //
155 // (1) We know the computed label.
156 // (2) The label is NULL (or some other constant), or Uninitialized.
157 // (3) We have no clue about the label. Dispatch to all targets.
158 //
159
160 typedef IndirectGotoNodeBuilder::iterator iterator;
161
162 if (isa<lval::GotoLabel>(V)) {
163 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
164
165 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000166 if (I.getLabel() == L) {
167 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000168 return;
169 }
170 }
171
172 assert (false && "No block with label.");
173 return;
174 }
175
176 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
177 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000178 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000179 UninitBranches.insert(N);
180 return;
181 }
182
183 // This is really a catch-all. We don't support symbolics yet.
184
185 assert (isa<UnknownVal>(V));
186
187 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000188 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000189}
Ted Kremenekf233d482008-02-05 00:26:40 +0000190
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000191/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
192/// nodes by processing the 'effects' of a switch statement.
193void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
194
195 typedef SwitchNodeBuilder::iterator iterator;
196
197 StateTy St = builder.getState();
198 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
199
200 if (isa<UninitializedVal>(CondV)) {
201 NodeTy* N = builder.generateDefaultCaseNode(St, true);
202 UninitBranches.insert(N);
203 return;
204 }
205
206 StateTy DefaultSt = St;
207
208 // While most of this can be assumed (such as the signedness), having it
209 // just computed makes sure everything makes the same assumptions end-to-end.
210 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
211 APSInt V1(bits, false);
212 APSInt V2 = V1;
213
214 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
215
216 CaseStmt* Case = cast<CaseStmt>(I.getCase());
217
218 // Evaluate the case.
219 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
220 assert (false && "Case condition must evaluate to an integer constant.");
221 return;
222 }
223
224 // Get the RHS of the case, if it exists.
225
226 if (Expr* E = Case->getRHS()) {
227 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
228 assert (false &&
229 "Case condition (RHS) must evaluate to an integer constant.");
230 return ;
231 }
232
233 assert (V1 <= V2);
234 }
235 else V2 = V1;
236
237 // FIXME: Eventually we should replace the logic below with a range
238 // comparison, rather than concretize the values within the range.
239 // This should be easy once we have "ranges" for NonLValues.
240
241 do {
242 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
243
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000244 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000245
246 // Now "assume" that the case matches.
247 bool isFeasible;
248
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000249 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000250
251 if (isFeasible) {
252 builder.generateCaseStmtNode(I, StNew);
253
254 // If CondV evaluates to a constant, then we know that this
255 // is the *only* case that we can take, so stop evaluating the
256 // others.
257 if (isa<nonlval::ConcreteInt>(CondV))
258 return;
259 }
260
261 // Now "assume" that the case doesn't match. Add this state
262 // to the default state (if it is feasible).
263
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000264 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000265
266 if (isFeasible)
267 DefaultSt = StNew;
268
269 // Concretize the next value in the range.
270 ++V1;
271
272 } while (V1 < V2);
273 }
274
275 // If we reach here, than we know that the default branch is
276 // possible.
277 builder.generateDefaultCaseNode(DefaultSt);
278}
279
280
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000281void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000282 NodeSet& Dst) {
283
284 bool hasR2;
285 StateTy PrevState = Pred->getState();
286
287 RValue R1 = GetValue(PrevState, B->getLHS());
288 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
289
Ted Kremenek22031182008-02-08 02:57:34 +0000290 if (isa<UnknownVal>(R1) &&
291 (isa<UnknownVal>(R2) ||
292 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000293
294 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
295 return;
296 }
Ted Kremenek22031182008-02-08 02:57:34 +0000297 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000298 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
299 return;
300 }
301
302 // R1 is an expression that can evaluate to either 'true' or 'false'.
303 if (B->getOpcode() == BinaryOperator::LAnd) {
304 // hasR2 == 'false' means that LHS evaluated to 'false' and that
305 // we short-circuited, leading to a value of '0' for the '&&' expression.
306 if (hasR2 == false) {
307 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
308 return;
309 }
310 }
311 else {
312 assert (B->getOpcode() == BinaryOperator::LOr);
313 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
314 // we short-circuited, leading to a value of '1' for the '||' expression.
315 if (hasR2 == false) {
316 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
317 return;
318 }
319 }
320
321 // If we reach here we did not short-circuit. Assume R2 == true and
322 // R2 == false.
323
324 bool isFeasible;
325 StateTy St = Assume(PrevState, R2, true, isFeasible);
326
327 if (isFeasible)
328 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
329
330 St = Assume(PrevState, R2, false, isFeasible);
331
332 if (isFeasible)
333 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
334}
335
336
337
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000338void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000339 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000340
341 StmtEntryNode = builder.getLastNode();
342 CurrentStmt = S;
343 NodeSet Dst;
344 StateCleaned = false;
345
346 Visit(S, StmtEntryNode, Dst);
347
348 // If no nodes were generated, generate a new node that has all the
349 // dead mappings removed.
350 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
351 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
352 builder.generateNode(S, St, StmtEntryNode);
353 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000354
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000355 CurrentStmt = NULL;
356 StmtEntryNode = NULL;
357 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000358}
359
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000360GRExprEngine::NodeTy*
361GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000362
363 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000364 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000365 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000366
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000367 NodeTy* N = Builder->generateNode(S, St, Pred);
368 Dst.Add(N);
369 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000370}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000371
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000372void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000373 const StateTy::BufferTy& SB) {
374
375 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
376 Nodify(Dst, S, Pred, *I);
377}
378
Ted Kremenek44842c22008-02-13 18:06:44 +0000379void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000380 if (D != CurrentStmt) {
381 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
382 return;
383 }
384
385 // If we are here, we are loading the value of the decl and binding
386 // it to the block-level expression.
387
388 StateTy St = Pred->getState();
389
390 Nodify(Dst, D, Pred,
391 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
392}
393
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000394void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000395
396 QualType T = CastE->getType();
397
398 // Check for redundant casts.
399 if (E->getType() == T) {
400 Dst.Add(Pred);
401 return;
402 }
403
404 NodeSet S1;
405 Visit(E, Pred, S1);
406
407 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
408 NodeTy* N = *I1;
409 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000410 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000411 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000412 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000413}
414
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000415void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
416 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000417
418 StateTy St = Pred->getState();
419
420 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000421 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
422 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000423 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000424 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000425 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000426
427 Nodify(Dst, DS, Pred, St);
428
429 if (Dst.empty())
430 Dst.Add(Pred);
431}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000432
Ted Kremenekf233d482008-02-05 00:26:40 +0000433
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000434void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000435 NodeTy* Pred, NodeSet& Dst) {
436
437 StateTy St = Pred->getState();
438
439 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000440 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000441
442 Nodify(Dst, S, Pred, SetValue(St, S, R));
443}
444
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000445/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000446void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000447 NodeTy* Pred,
448 NodeSet& Dst) {
449
450 // 6.5.3.4 sizeof: "The result type is an integer."
451
452 QualType T = S->getArgumentType();
453
454 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000455 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000456 return;
457
458 SourceLocation L = S->getExprLoc();
459 uint64_t size = getContext().getTypeSize(T, L) / 8;
460
461 Nodify(Dst, S, Pred,
462 SetValue(Pred->getState(), S,
463 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
464
465}
466
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000467void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
468 GRExprEngine::NodeTy* Pred,
469 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000470
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000471 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000472 UnaryOperator::Opcode Op = U->getOpcode();
473
474 // FIXME: This is a hack so that for '*' and '&' we don't recurse
475 // on visiting the subexpression if it is a DeclRefExpr. We should
476 // probably just handle AddrOf and Deref in their own methods to make
477 // this cleaner.
478 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
479 isa<DeclRefExpr>(U->getSubExpr()))
480 S1.Add(Pred);
481 else
482 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000483
484 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
485 NodeTy* N1 = *I1;
486 StateTy St = N1->getState();
487
488 switch (U->getOpcode()) {
489 case UnaryOperator::PostInc: {
490 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000491 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000492
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000493 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
494 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000495
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000496 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
497 break;
498 }
499
500 case UnaryOperator::PostDec: {
501 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000502 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000503
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000504 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
505 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000506
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000507 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
508 break;
509 }
510
511 case UnaryOperator::PreInc: {
512 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000513 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000514
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000515 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
516 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000517
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000518 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
519 break;
520 }
521
522 case UnaryOperator::PreDec: {
523 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000524 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000525
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000526 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
527 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000528
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000529 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
530 break;
531 }
532
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000533 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000534 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000535 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000536 break;
537 }
538
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000539 case UnaryOperator::Not: {
540 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000541 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000542 break;
543 }
544
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000545 case UnaryOperator::LNot: {
546 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
547 //
548 // Note: technically we do "E == 0", but this is the same in the
549 // transfer functions as "0 == E".
550
551 RValue V1 = GetValue(St, U->getSubExpr());
552
553 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000554 const LValue& L1 = cast<LValue>(V1);
555 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
556 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000557 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
558 L1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000559 }
560 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000561 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000562 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000563 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000564 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
565 R1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000566 }
567
568 break;
569 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000570
571 case UnaryOperator::SizeOf: {
572 // 6.5.3.4 sizeof: "The result type is an integer."
573
574 QualType T = U->getSubExpr()->getType();
575
576 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000577 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000578 return;
579
580 SourceLocation L = U->getExprLoc();
581 uint64_t size = getContext().getTypeSize(T, L) / 8;
582
583 Nodify(Dst, U, N1,
584 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
585 getContext().IntTy, L)));
586
587 break;
588 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000589
Ted Kremenek64924852008-01-31 02:35:41 +0000590 case UnaryOperator::AddrOf: {
591 const LValue& L1 = GetLValue(St, U->getSubExpr());
592 Nodify(Dst, U, N1, SetValue(St, U, L1));
593 break;
594 }
595
596 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000597 // FIXME: Stop when dereferencing an uninitialized value.
598 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
599
600 const RValue& V = GetValue(St, U->getSubExpr());
601 const LValue& L1 = cast<LValue>(V);
602
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000603 // After a dereference, one of two possible situations arise:
604 // (1) A crash, because the pointer was NULL.
605 // (2) The pointer is not NULL, and the dereference works.
606 //
607 // We add these assumptions.
608
Ted Kremenek63a4f692008-02-07 06:04:18 +0000609 bool isFeasibleNotNull;
610
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000611 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000612 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
613
614 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000615 QualType T = U->getType();
616 Nodify(Dst, U, N1, SetValue(StNotNull, U,
617 GetValue(StNotNull, L1, &T)));
618 }
619
Ted Kremenek63a4f692008-02-07 06:04:18 +0000620 bool isFeasibleNull;
621
622 // "Assume" that the pointer is NULL.
623 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
624
625 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000626 // We don't use "Nodify" here because the node will be a sink
627 // and we have no intention of processing it later.
628 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
629
Ted Kremenek63a4f692008-02-07 06:04:18 +0000630 if (NullNode) {
631 NullNode->markAsSink();
632
633 if (isFeasibleNotNull)
634 ImplicitNullDeref.insert(NullNode);
635 else
636 ExplicitNullDeref.insert(NullNode);
637 }
638 }
639
Ted Kremenek64924852008-01-31 02:35:41 +0000640 break;
641 }
642
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000643 default: ;
644 assert (false && "Not implemented.");
645 }
646 }
647}
648
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000649void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
650 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000651
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000652 if (isa<DeclRefExpr>(E)) {
653 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000654 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000655 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000656
657 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
658 if (U->getOpcode() == UnaryOperator::Deref) {
659 Visit(U->getSubExpr(), Pred, Dst);
660 return;
661 }
662 }
663
664 Visit(E, Pred, Dst);
665}
666
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000667void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000668 GRExprEngine::NodeTy* Pred,
669 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000670 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000671
672 if (B->isAssignmentOp())
673 VisitAssignmentLHS(B->getLHS(), Pred, S1);
674 else
675 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000676
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000677 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
678 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000679
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000680 // When getting the value for the LHS, check if we are in an assignment.
681 // In such cases, we want to (initially) treat the LHS as an LValue,
682 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000683 // evaluated to LValueDecl's instead of to an NonLValue.
684 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000685 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
686 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000687
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000688 NodeSet S2;
689 Visit(B->getRHS(), N1, S2);
690
691 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000692
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000693 NodeTy* N2 = *I2;
694 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000695 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000696
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000697 BinaryOperator::Opcode Op = B->getOpcode();
698
699 if (Op <= BinaryOperator::Or) {
700
Ted Kremenek22031182008-02-08 02:57:34 +0000701 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000702 Nodify(Dst, B, N2, SetValue(St, B, V1));
703 continue;
704 }
705
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000706 if (isa<LValue>(V1)) {
707 // FIXME: Add support for RHS being a non-lvalue.
708 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek687af802008-01-29 19:43:15 +0000709
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000710 if (isa<LValue>(V2)) {
711 const LValue& L2 = cast<LValue>(V2);
712 Nodify(Dst, B, N2, SetValue(St, B,
713 EvalBinaryOp(ValMgr, Op, L1, L2)));
714 }
715 else {
716 const NonLValue& R2 = cast<NonLValue>(V2);
717 Nodify(Dst, B, N2, SetValue(St, B,
718 EvalBinaryOp(ValMgr, Op, L1, R2)));
719 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000720 }
721 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000722 const NonLValue& R1 = cast<NonLValue>(V1);
723 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000724
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000725 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, R1, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000726 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000727
728 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000729
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000730 }
731
732 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000733 case BinaryOperator::Assign: {
734 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000735 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000736 break;
737 }
738
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000739 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000740
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000741 assert (B->isCompoundAssignmentOp());
742
743 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000744 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000745
Ted Kremenekda9bd092008-02-08 07:05:39 +0000746 if (Op >= BinaryOperator::AndAssign)
747 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
748 else
749 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000750
751 if (isa<LValue>(V2)) {
752 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000753 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000754 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000755 }
756 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000757 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000758 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000759 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000760 }
761
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000762 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000763 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000764 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000765 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000766 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000767 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000768}
Ted Kremenekee985462008-01-16 18:18:48 +0000769
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000770
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000771void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
772 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000773
774 // FIXME: add metadata to the CFG so that we can disable
775 // this check when we KNOW that there is no block-level subexpression.
776 // The motivation is that this check requires a hashtable lookup.
777
778 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
779 Dst.Add(Pred);
780 return;
781 }
782
783 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000784
785 default:
786 // Cases we intentionally have "default" handle:
787 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
788
789 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
790 break;
791
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000792 case Stmt::BinaryOperatorClass: {
793 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000794
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000795 if (B->isLogicalOp()) {
796 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000797 break;
798 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000799 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000800 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000801 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000802 break;
803 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000804
805 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
806 break;
807 }
808
809 case Stmt::CastExprClass: {
810 CastExpr* C = cast<CastExpr>(S);
811 VisitCast(C, C->getSubExpr(), Pred, Dst);
812 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000813 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000814
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000815 case Stmt::ChooseExprClass: { // __builtin_choose_expr
816 ChooseExpr* C = cast<ChooseExpr>(S);
817 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
818 break;
819 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000820
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000821 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000822 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
823 break;
824
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000825 case Stmt::ConditionalOperatorClass: { // '?' operator
826 ConditionalOperator* C = cast<ConditionalOperator>(S);
827 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
828 break;
829 }
830
831 case Stmt::DeclRefExprClass:
832 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
833 break;
834
835 case Stmt::DeclStmtClass:
836 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
837 break;
838
839 case Stmt::ImplicitCastExprClass: {
840 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
841 VisitCast(C, C->getSubExpr(), Pred, Dst);
842 break;
843 }
844
845 case Stmt::ParenExprClass:
846 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
847 break;
848
849 case Stmt::SizeOfAlignOfTypeExprClass:
850 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
851 break;
852
Ted Kremenekda9bd092008-02-08 07:05:39 +0000853 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000854 StmtExpr* SE = cast<StmtExpr>(S);
855
Ted Kremenekda9bd092008-02-08 07:05:39 +0000856 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000857 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
858 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000859 break;
860 }
861
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000862 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000863 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
864 Visit(R, Pred, Dst);
865 else
866 Dst.Add(Pred);
867
868 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000869 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000870
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000871 case Stmt::UnaryOperatorClass:
872 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000873 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000874 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000875}
876
Ted Kremenekee985462008-01-16 18:18:48 +0000877//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000878// "Assume" logic.
879//===----------------------------------------------------------------------===//
880
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000881GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000882 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000883 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000884
885 switch (Cond.getSubKind()) {
886 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000887 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000888 return St;
889
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000890 case lval::SymbolValKind:
891 if (Assumption)
892 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
893 ValMgr.getZeroWithPtrWidth(), isFeasible);
894 else
895 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
896 ValMgr.getZeroWithPtrWidth(), isFeasible);
897
Ted Kremenek08b66252008-02-06 04:31:33 +0000898
Ted Kremenek329f8542008-02-05 21:52:21 +0000899 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000900 isFeasible = Assumption;
901 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000902
Ted Kremenek329f8542008-02-05 21:52:21 +0000903 case lval::ConcreteIntKind: {
904 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000905 isFeasible = b ? Assumption : !Assumption;
906 return St;
907 }
908 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000909}
910
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000911GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000912 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000913 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000914
915 switch (Cond.getSubKind()) {
916 default:
917 assert (false && "'Assume' not implemented for this NonLValue.");
918 return St;
919
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000920
921 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000922 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000923 SymbolID sym = SV.getSymbol();
924
925 if (Assumption)
926 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
927 isFeasible);
928 else
929 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
930 isFeasible);
931 }
932
Ted Kremenek08b66252008-02-06 04:31:33 +0000933 case nonlval::SymIntConstraintValKind:
934 return
935 AssumeSymInt(St, Assumption,
936 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
937 isFeasible);
938
Ted Kremenek329f8542008-02-05 21:52:21 +0000939 case nonlval::ConcreteIntKind: {
940 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000941 isFeasible = b ? Assumption : !Assumption;
942 return St;
943 }
944 }
945}
946
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000947GRExprEngine::StateTy
948GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000949 const llvm::APSInt& V, bool& isFeasible) {
950
951 // First, determine if sym == X, where X != V.
952 if (const llvm::APSInt* X = St.getSymVal(sym)) {
953 isFeasible = *X != V;
954 return St;
955 }
956
957 // Second, determine if sym != V.
958 if (St.isNotEqual(sym, V)) {
959 isFeasible = true;
960 return St;
961 }
962
963 // If we reach here, sym is not a constant and we don't know if it is != V.
964 // Make that assumption.
965
966 isFeasible = true;
967 return StateMgr.AddNE(St, sym, V);
968}
969
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000970GRExprEngine::StateTy
971GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000972 const llvm::APSInt& V, bool& isFeasible) {
973
974 // First, determine if sym == X, where X != V.
975 if (const llvm::APSInt* X = St.getSymVal(sym)) {
976 isFeasible = *X == V;
977 return St;
978 }
979
980 // Second, determine if sym != V.
981 if (St.isNotEqual(sym, V)) {
982 isFeasible = false;
983 return St;
984 }
985
986 // If we reach here, sym is not a constant and we don't know if it is == V.
987 // Make that assumption.
988
989 isFeasible = true;
990 return StateMgr.AddEQ(St, sym, V);
991}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000992
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000993GRExprEngine::StateTy
994GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +0000995 const SymIntConstraint& C, bool& isFeasible) {
996
997 switch (C.getOpcode()) {
998 default:
999 // No logic yet for other operators.
1000 return St;
1001
1002 case BinaryOperator::EQ:
1003 if (Assumption)
1004 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1005 else
1006 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1007
1008 case BinaryOperator::NE:
1009 if (Assumption)
1010 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1011 else
1012 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1013 }
1014}
1015
Ted Kremenekb38911f2008-01-30 23:03:39 +00001016//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001017// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001018//===----------------------------------------------------------------------===//
1019
Ted Kremenekaa66a322008-01-16 21:46:15 +00001020#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001021static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001022
Ted Kremenekaa66a322008-01-16 21:46:15 +00001023namespace llvm {
1024template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001025struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001026 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001027
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001028 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001029
1030 Out << "Variables:\\l";
1031
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001032 bool isFirst = true;
1033
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001034 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001035 E=St.vb_end(); I!=E;++I) {
1036
1037 if (isFirst)
1038 isFirst = false;
1039 else
1040 Out << "\\l";
1041
1042 Out << ' ' << I.getKey()->getName() << " : ";
1043 I.getData().print(Out);
1044 }
1045
1046 }
1047
Ted Kremeneke7d22112008-02-11 19:21:59 +00001048
Ted Kremenek44842c22008-02-13 18:06:44 +00001049 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001050
1051 bool isFirst = true;
1052
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001053 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001054 I != E;++I) {
1055
1056 if (isFirst) {
1057 Out << "\\l\\lSub-Expressions:\\l";
1058 isFirst = false;
1059 }
1060 else
1061 Out << "\\l";
1062
1063 Out << " (" << (void*) I.getKey() << ") ";
1064 I.getKey()->printPretty(Out);
1065 Out << " : ";
1066 I.getData().print(Out);
1067 }
1068 }
1069
Ted Kremenek44842c22008-02-13 18:06:44 +00001070 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001071
Ted Kremenek016f52f2008-02-08 21:10:02 +00001072 bool isFirst = true;
1073
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001074 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001075 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001076 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001077 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001078 isFirst = false;
1079 }
1080 else
1081 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001082
Ted Kremeneke7d22112008-02-11 19:21:59 +00001083 Out << " (" << (void*) I.getKey() << ") ";
1084 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001085 Out << " : ";
1086 I.getData().print(Out);
1087 }
1088 }
1089
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001090 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001091 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1092
1093 if (CE.isEmpty())
1094 return;
1095
1096 Out << "\\l\\|'==' constraints:";
1097
1098 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1099 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1100 }
1101
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001102 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001103 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1104
1105 if (NE.isEmpty())
1106 return;
1107
1108 Out << "\\l\\|'!=' constraints:";
1109
1110 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1111 I != EI; ++I){
1112
1113 Out << "\\l $" << I.getKey() << " : ";
1114 bool isFirst = true;
1115
1116 ValueState::IntSetTy::iterator J=I.getData().begin(),
1117 EJ=I.getData().end();
1118 for ( ; J != EJ; ++J) {
1119 if (isFirst) isFirst = false;
1120 else Out << ", ";
1121
1122 Out << (*J)->toString();
1123 }
1124 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001125 }
1126
1127 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1128
1129 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
1130 GraphPrintCheckerState->isExplicitNullDeref(N))
1131 return "color=\"red\",style=\"filled\"";
1132
1133 return "";
1134 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001135
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001136 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001137 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001138
1139 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001140 ProgramPoint Loc = N->getLocation();
1141
1142 switch (Loc.getKind()) {
1143 case ProgramPoint::BlockEntranceKind:
1144 Out << "Block Entrance: B"
1145 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1146 break;
1147
1148 case ProgramPoint::BlockExitKind:
1149 assert (false);
1150 break;
1151
1152 case ProgramPoint::PostStmtKind: {
1153 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001154 Out << L.getStmt()->getStmtClassName() << ':'
1155 << (void*) L.getStmt() << ' ';
1156
Ted Kremenekaa66a322008-01-16 21:46:15 +00001157 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001158
1159 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1160 Out << "\\|Implicit-Null Dereference.\\l";
1161 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001162 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1163 Out << "\\|Explicit-Null Dereference.\\l";
1164 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001165
Ted Kremenekaa66a322008-01-16 21:46:15 +00001166 break;
1167 }
1168
1169 default: {
1170 const BlockEdge& E = cast<BlockEdge>(Loc);
1171 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1172 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001173
1174 if (Stmt* T = E.getSrc()->getTerminator()) {
1175 Out << "\\|Terminator: ";
1176 E.getSrc()->printTerminator(Out);
1177
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001178 if (isa<SwitchStmt>(T)) {
1179 Stmt* Label = E.getDst()->getLabel();
1180
1181 if (Label) {
1182 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1183 Out << "\\lcase ";
1184 C->getLHS()->printPretty(Out);
1185
1186 if (Stmt* RHS = C->getRHS()) {
1187 Out << " .. ";
1188 RHS->printPretty(Out);
1189 }
1190
1191 Out << ":";
1192 }
1193 else {
1194 assert (isa<DefaultStmt>(Label));
1195 Out << "\\ldefault:";
1196 }
1197 }
1198 else
1199 Out << "\\l(implicit) default:";
1200 }
1201 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001202 // FIXME
1203 }
1204 else {
1205 Out << "\\lCondition: ";
1206 if (*E.getSrc()->succ_begin() == E.getDst())
1207 Out << "true";
1208 else
1209 Out << "false";
1210 }
1211
1212 Out << "\\l";
1213 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001214
1215 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1216 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1217 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001218 }
1219 }
1220
Ted Kremenek9153f732008-02-05 07:17:49 +00001221 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001222
Ted Kremeneke7d22112008-02-11 19:21:59 +00001223 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001224
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001225 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001226 return Out.str();
1227 }
1228};
1229} // end llvm namespace
1230#endif
1231
Ted Kremeneke01c9872008-02-14 22:36:46 +00001232void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001233#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001234 GraphPrintCheckerState = this;
1235 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001236 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001237#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001238}