blob: d9f1826bd7d1276464870688e6c73a1ccc7285b3 [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 Kremenekb387a3f2008-02-14 22:16:04 +000017#include "GRSimpleVals.h"
18
19using namespace clang;
20using llvm::dyn_cast;
21using llvm::cast;
22using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000023
Ted Kremenek4d4dd852008-02-13 17:41:41 +000024GRExprEngine::StateTy
25GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000026
Ted Kremeneke070a1d2008-02-04 21:59:01 +000027 if (!StateCleaned) {
28 St = RemoveDeadBindings(CurrentStmt, St);
29 StateCleaned = true;
30 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000031
Ted Kremeneke070a1d2008-02-04 21:59:01 +000032 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000033
Ted Kremeneke070a1d2008-02-04 21:59:01 +000034 if (S == CurrentStmt) {
35 isBlkExpr = getCFG().isBlkExpr(S);
36
37 if (!isBlkExpr)
38 return St;
39 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000040
Ted Kremeneke070a1d2008-02-04 21:59:01 +000041 return StateMgr.SetValue(St, S, isBlkExpr, V);
42}
43
Ted Kremenek4d4dd852008-02-13 17:41:41 +000044const GRExprEngine::StateTy::BufferTy&
45GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +000046 StateTy::BufferTy& RetBuf) {
47
48 assert (RetBuf.empty());
49
50 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
51 RetBuf.push_back(SetValue(St, S, *I));
52
53 return RetBuf;
54}
55
Ted Kremenek4d4dd852008-02-13 17:41:41 +000056GRExprEngine::StateTy
57GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000058
Ted Kremenek53c641a2008-02-08 03:02:48 +000059 if (LV.isUnknown())
Ted Kremeneke070a1d2008-02-04 21:59:01 +000060 return St;
61
62 if (!StateCleaned) {
63 St = RemoveDeadBindings(CurrentStmt, St);
64 StateCleaned = true;
65 }
66
67 return StateMgr.SetValue(St, LV, V);
68}
69
Ted Kremenek4d4dd852008-02-13 17:41:41 +000070void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +000071 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +000072
Ted Kremeneke7d22112008-02-11 19:21:59 +000073 // Remove old bindings for subexpressions.
74 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +000075
Ted Kremenekb38911f2008-01-30 23:03:39 +000076 RValue V = GetValue(PrevState, Condition);
77
78 switch (V.getBaseKind()) {
79 default:
80 break;
81
Ted Kremenek53c641a2008-02-08 03:02:48 +000082 case RValue::UnknownKind:
Ted Kremenekb38911f2008-01-30 23:03:39 +000083 builder.generateNode(PrevState, true);
84 builder.generateNode(PrevState, false);
85 return;
86
87 case RValue::UninitializedKind: {
88 NodeTy* N = builder.generateNode(PrevState, true);
89
90 if (N) {
91 N->markAsSink();
92 UninitBranches.insert(N);
93 }
94
95 builder.markInfeasible(false);
96 return;
97 }
98 }
99
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000100 // Get the current block counter.
101 GRBlockCounter BC = builder.getBlockCounter();
102
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000103 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
104 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000105
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000106 if (isa<nonlval::ConcreteInt>(V) ||
107 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
108
109 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000110
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000111 bool isFeasible = true;
112
113 StateTy St = Assume(PrevState, V, true, isFeasible);
114
115 if (isFeasible)
116 builder.generateNode(St, true);
117 else
118 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000119 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000120 else
121 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000122
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000123 BlockID = builder.getTargetBlock(false)->getBlockID();
124 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000125
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000126 if (isa<nonlval::ConcreteInt>(V) ||
127 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
128
129 // Process the false branch.
130
131 bool isFeasible = false;
132
133 StateTy St = Assume(PrevState, V, false, isFeasible);
134
135 if (isFeasible)
136 builder.generateNode(St, false);
137 else
138 builder.markInfeasible(false);
139 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000140 else
141 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000142}
143
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000144/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000145/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000146void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000147
148 StateTy St = builder.getState();
149 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
150
151 // Three possibilities:
152 //
153 // (1) We know the computed label.
154 // (2) The label is NULL (or some other constant), or Uninitialized.
155 // (3) We have no clue about the label. Dispatch to all targets.
156 //
157
158 typedef IndirectGotoNodeBuilder::iterator iterator;
159
160 if (isa<lval::GotoLabel>(V)) {
161 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
162
163 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000164 if (I.getLabel() == L) {
165 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000166 return;
167 }
168 }
169
170 assert (false && "No block with label.");
171 return;
172 }
173
174 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
175 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000176 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000177 UninitBranches.insert(N);
178 return;
179 }
180
181 // This is really a catch-all. We don't support symbolics yet.
182
183 assert (isa<UnknownVal>(V));
184
185 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000186 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000187}
Ted Kremenekf233d482008-02-05 00:26:40 +0000188
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000189/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
190/// nodes by processing the 'effects' of a switch statement.
191void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
192
193 typedef SwitchNodeBuilder::iterator iterator;
194
195 StateTy St = builder.getState();
196 NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition()));
197
198 if (isa<UninitializedVal>(CondV)) {
199 NodeTy* N = builder.generateDefaultCaseNode(St, true);
200 UninitBranches.insert(N);
201 return;
202 }
203
204 StateTy DefaultSt = St;
205
206 // While most of this can be assumed (such as the signedness), having it
207 // just computed makes sure everything makes the same assumptions end-to-end.
208 unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation());
209 APSInt V1(bits, false);
210 APSInt V2 = V1;
211
212 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
213
214 CaseStmt* Case = cast<CaseStmt>(I.getCase());
215
216 // Evaluate the case.
217 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
218 assert (false && "Case condition must evaluate to an integer constant.");
219 return;
220 }
221
222 // Get the RHS of the case, if it exists.
223
224 if (Expr* E = Case->getRHS()) {
225 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
226 assert (false &&
227 "Case condition (RHS) must evaluate to an integer constant.");
228 return ;
229 }
230
231 assert (V1 <= V2);
232 }
233 else V2 = V1;
234
235 // FIXME: Eventually we should replace the logic below with a range
236 // comparison, rather than concretize the values within the range.
237 // This should be easy once we have "ranges" for NonLValues.
238
239 do {
240 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
241
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000242 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000243
244 // Now "assume" that the case matches.
245 bool isFeasible;
246
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000247 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000248
249 if (isFeasible) {
250 builder.generateCaseStmtNode(I, StNew);
251
252 // If CondV evaluates to a constant, then we know that this
253 // is the *only* case that we can take, so stop evaluating the
254 // others.
255 if (isa<nonlval::ConcreteInt>(CondV))
256 return;
257 }
258
259 // Now "assume" that the case doesn't match. Add this state
260 // to the default state (if it is feasible).
261
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000262 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000263
264 if (isFeasible)
265 DefaultSt = StNew;
266
267 // Concretize the next value in the range.
268 ++V1;
269
270 } while (V1 < V2);
271 }
272
273 // If we reach here, than we know that the default branch is
274 // possible.
275 builder.generateDefaultCaseNode(DefaultSt);
276}
277
278
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000279void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekf233d482008-02-05 00:26:40 +0000280 NodeSet& Dst) {
281
282 bool hasR2;
283 StateTy PrevState = Pred->getState();
284
285 RValue R1 = GetValue(PrevState, B->getLHS());
286 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
287
Ted Kremenek22031182008-02-08 02:57:34 +0000288 if (isa<UnknownVal>(R1) &&
289 (isa<UnknownVal>(R2) ||
290 isa<UninitializedVal>(R2))) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000291
292 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
293 return;
294 }
Ted Kremenek22031182008-02-08 02:57:34 +0000295 else if (isa<UninitializedVal>(R1)) {
Ted Kremenekf233d482008-02-05 00:26:40 +0000296 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
297 return;
298 }
299
300 // R1 is an expression that can evaluate to either 'true' or 'false'.
301 if (B->getOpcode() == BinaryOperator::LAnd) {
302 // hasR2 == 'false' means that LHS evaluated to 'false' and that
303 // we short-circuited, leading to a value of '0' for the '&&' expression.
304 if (hasR2 == false) {
305 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
306 return;
307 }
308 }
309 else {
310 assert (B->getOpcode() == BinaryOperator::LOr);
311 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
312 // we short-circuited, leading to a value of '1' for the '||' expression.
313 if (hasR2 == false) {
314 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
315 return;
316 }
317 }
318
319 // If we reach here we did not short-circuit. Assume R2 == true and
320 // R2 == false.
321
322 bool isFeasible;
323 StateTy St = Assume(PrevState, R2, true, isFeasible);
324
325 if (isFeasible)
326 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
327
328 St = Assume(PrevState, R2, false, isFeasible);
329
330 if (isFeasible)
331 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
332}
333
334
335
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000336void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000337 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000338
339 StmtEntryNode = builder.getLastNode();
340 CurrentStmt = S;
341 NodeSet Dst;
342 StateCleaned = false;
343
344 Visit(S, StmtEntryNode, Dst);
345
346 // If no nodes were generated, generate a new node that has all the
347 // dead mappings removed.
348 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
349 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
350 builder.generateNode(S, St, StmtEntryNode);
351 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000352
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000353 CurrentStmt = NULL;
354 StmtEntryNode = NULL;
355 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000356}
357
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000358GRExprEngine::NodeTy*
359GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000360
361 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000362 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000363 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000364
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000365 NodeTy* N = Builder->generateNode(S, St, Pred);
366 Dst.Add(N);
367 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000368}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000369
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000370void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000371 const StateTy::BufferTy& SB) {
372
373 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
374 Nodify(Dst, S, Pred, *I);
375}
376
Ted Kremenek44842c22008-02-13 18:06:44 +0000377void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000378 if (D != CurrentStmt) {
379 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
380 return;
381 }
382
383 // If we are here, we are loading the value of the decl and binding
384 // it to the block-level expression.
385
386 StateTy St = Pred->getState();
387
388 Nodify(Dst, D, Pred,
389 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
390}
391
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000392void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000393
394 QualType T = CastE->getType();
395
396 // Check for redundant casts.
397 if (E->getType() == T) {
398 Dst.Add(Pred);
399 return;
400 }
401
402 NodeSet S1;
403 Visit(E, Pred, S1);
404
405 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
406 NodeTy* N = *I1;
407 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000408 const RValue& V = GetValue(St, E);
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000409 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000410 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000411}
412
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000413void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
414 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000415
416 StateTy St = Pred->getState();
417
418 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000419 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
420 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000421 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek22031182008-02-08 02:57:34 +0000422 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000423 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000424
425 Nodify(Dst, DS, Pred, St);
426
427 if (Dst.empty())
428 Dst.Add(Pred);
429}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000430
Ted Kremenekf233d482008-02-05 00:26:40 +0000431
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000432void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenekf233d482008-02-05 00:26:40 +0000433 NodeTy* Pred, NodeSet& Dst) {
434
435 StateTy St = Pred->getState();
436
437 RValue R = GetValue(St, LHS);
Ted Kremenek22031182008-02-08 02:57:34 +0000438 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenekf233d482008-02-05 00:26:40 +0000439
440 Nodify(Dst, S, Pred, SetValue(St, S, R));
441}
442
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000443/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000444void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000445 NodeTy* Pred,
446 NodeSet& Dst) {
447
448 // 6.5.3.4 sizeof: "The result type is an integer."
449
450 QualType T = S->getArgumentType();
451
452 // FIXME: Add support for VLAs.
453 if (isa<VariableArrayType>(T.getTypePtr()))
454 return;
455
456 SourceLocation L = S->getExprLoc();
457 uint64_t size = getContext().getTypeSize(T, L) / 8;
458
459 Nodify(Dst, S, Pred,
460 SetValue(Pred->getState(), S,
461 NonLValue::GetValue(ValMgr, size, getContext().IntTy, L)));
462
463}
464
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000465void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
466 GRExprEngine::NodeTy* Pred,
467 GRExprEngine::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000468
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000469 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000470 UnaryOperator::Opcode Op = U->getOpcode();
471
472 // FIXME: This is a hack so that for '*' and '&' we don't recurse
473 // on visiting the subexpression if it is a DeclRefExpr. We should
474 // probably just handle AddrOf and Deref in their own methods to make
475 // this cleaner.
476 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
477 isa<DeclRefExpr>(U->getSubExpr()))
478 S1.Add(Pred);
479 else
480 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000481
482 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
483 NodeTy* N1 = *I1;
484 StateTy St = N1->getState();
485
486 switch (U->getOpcode()) {
487 case UnaryOperator::PostInc: {
488 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000489 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000490
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000491 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
492 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000493
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000494 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
495 break;
496 }
497
498 case UnaryOperator::PostDec: {
499 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000500 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000501
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000502 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
503 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000504
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000505 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
506 break;
507 }
508
509 case UnaryOperator::PreInc: {
510 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000511 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000512
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000513 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add,
514 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000515
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000516 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
517 break;
518 }
519
520 case UnaryOperator::PreDec: {
521 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000522 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000523
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000524 NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub,
525 R1, GetRValueConstant(1U, U));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000526
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000527 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
528 break;
529 }
530
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000531 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000532 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000533 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000534 break;
535 }
536
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000537 case UnaryOperator::Not: {
538 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000539 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000540 break;
541 }
542
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000543 case UnaryOperator::LNot: {
544 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
545 //
546 // Note: technically we do "E == 0", but this is the same in the
547 // transfer functions as "0 == E".
548
549 RValue V1 = GetValue(St, U->getSubExpr());
550
551 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000552 const LValue& L1 = cast<LValue>(V1);
553 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
554 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000555 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
556 L1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000557 }
558 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000559 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000560 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000561 Nodify(Dst, U, N1,
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000562 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
563 R1, V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000564 }
565
566 break;
567 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000568
569 case UnaryOperator::SizeOf: {
570 // 6.5.3.4 sizeof: "The result type is an integer."
571
572 QualType T = U->getSubExpr()->getType();
573
574 // FIXME: Add support for VLAs.
575 if (isa<VariableArrayType>(T.getTypePtr()))
576 return;
577
578 SourceLocation L = U->getExprLoc();
579 uint64_t size = getContext().getTypeSize(T, L) / 8;
580
581 Nodify(Dst, U, N1,
582 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
583 getContext().IntTy, L)));
584
585 break;
586 }
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000587
Ted Kremenek64924852008-01-31 02:35:41 +0000588 case UnaryOperator::AddrOf: {
589 const LValue& L1 = GetLValue(St, U->getSubExpr());
590 Nodify(Dst, U, N1, SetValue(St, U, L1));
591 break;
592 }
593
594 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000595 // FIXME: Stop when dereferencing an uninitialized value.
596 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
597
598 const RValue& V = GetValue(St, U->getSubExpr());
599 const LValue& L1 = cast<LValue>(V);
600
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000601 // After a dereference, one of two possible situations arise:
602 // (1) A crash, because the pointer was NULL.
603 // (2) The pointer is not NULL, and the dereference works.
604 //
605 // We add these assumptions.
606
Ted Kremenek63a4f692008-02-07 06:04:18 +0000607 bool isFeasibleNotNull;
608
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000609 // "Assume" that the pointer is Not-NULL.
Ted Kremenek63a4f692008-02-07 06:04:18 +0000610 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
611
612 if (isFeasibleNotNull) {
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000613 QualType T = U->getType();
614 Nodify(Dst, U, N1, SetValue(StNotNull, U,
615 GetValue(StNotNull, L1, &T)));
616 }
617
Ted Kremenek63a4f692008-02-07 06:04:18 +0000618 bool isFeasibleNull;
619
620 // "Assume" that the pointer is NULL.
621 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
622
623 if (isFeasibleNull) {
Ted Kremenek7e593362008-02-07 15:20:13 +0000624 // We don't use "Nodify" here because the node will be a sink
625 // and we have no intention of processing it later.
626 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
627
Ted Kremenek63a4f692008-02-07 06:04:18 +0000628 if (NullNode) {
629 NullNode->markAsSink();
630
631 if (isFeasibleNotNull)
632 ImplicitNullDeref.insert(NullNode);
633 else
634 ExplicitNullDeref.insert(NullNode);
635 }
636 }
637
Ted Kremenek64924852008-01-31 02:35:41 +0000638 break;
639 }
640
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000641 default: ;
642 assert (false && "Not implemented.");
643 }
644 }
645}
646
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000647void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
648 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000649
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000650 if (isa<DeclRefExpr>(E)) {
651 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000652 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000653 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000654
655 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
656 if (U->getOpcode() == UnaryOperator::Deref) {
657 Visit(U->getSubExpr(), Pred, Dst);
658 return;
659 }
660 }
661
662 Visit(E, Pred, Dst);
663}
664
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000665void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666 GRExprEngine::NodeTy* Pred,
667 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000668 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000669
670 if (B->isAssignmentOp())
671 VisitAssignmentLHS(B->getLHS(), Pred, S1);
672 else
673 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000674
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000675 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
676 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000677
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000678 // When getting the value for the LHS, check if we are in an assignment.
679 // In such cases, we want to (initially) treat the LHS as an LValue,
680 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000681 // evaluated to LValueDecl's instead of to an NonLValue.
682 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000683 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
684 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000685
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000686 NodeSet S2;
687 Visit(B->getRHS(), N1, S2);
688
689 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000690
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000691 NodeTy* N2 = *I2;
692 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000693 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000694
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000695 BinaryOperator::Opcode Op = B->getOpcode();
696
697 if (Op <= BinaryOperator::Or) {
698
Ted Kremenek22031182008-02-08 02:57:34 +0000699 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000700 Nodify(Dst, B, N2, SetValue(St, B, V1));
701 continue;
702 }
703
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000704 if (isa<LValue>(V1)) {
705 // FIXME: Add support for RHS being a non-lvalue.
706 const LValue& L1 = cast<LValue>(V1);
707 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000708
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000709 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, L1, L2)));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000710 }
711 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000712 const NonLValue& R1 = cast<NonLValue>(V1);
713 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000714
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000715 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, R1, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000716 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000717
718 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000719
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000720 }
721
722 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000723 case BinaryOperator::Assign: {
724 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000725 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000726 break;
727 }
728
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000729 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000730
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000731 assert (B->isCompoundAssignmentOp());
732
733 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek22031182008-02-08 02:57:34 +0000734 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000735
Ted Kremenekda9bd092008-02-08 07:05:39 +0000736 if (Op >= BinaryOperator::AndAssign)
737 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
738 else
739 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000740
741 if (isa<LValue>(V2)) {
742 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000743 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000744 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000745 }
746 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000747 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000748 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000749 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000750 }
751
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000752 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000753 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000754 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000755 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000756 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000757 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000758}
Ted Kremenekee985462008-01-16 18:18:48 +0000759
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000760
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000761void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred,
762 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000763
764 // FIXME: add metadata to the CFG so that we can disable
765 // this check when we KNOW that there is no block-level subexpression.
766 // The motivation is that this check requires a hashtable lookup.
767
768 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
769 Dst.Add(Pred);
770 return;
771 }
772
773 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000774
775 default:
776 // Cases we intentionally have "default" handle:
777 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
778
779 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
780 break;
781
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000782 case Stmt::BinaryOperatorClass: {
783 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +0000784
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000785 if (B->isLogicalOp()) {
786 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +0000787 break;
788 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000789 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +0000790 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000791 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000792 break;
793 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000794
795 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
796 break;
797 }
798
799 case Stmt::CastExprClass: {
800 CastExpr* C = cast<CastExpr>(S);
801 VisitCast(C, C->getSubExpr(), Pred, Dst);
802 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000803 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000804
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000805 case Stmt::ChooseExprClass: { // __builtin_choose_expr
806 ChooseExpr* C = cast<ChooseExpr>(S);
807 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
808 break;
809 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000810
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000811 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000812 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
813 break;
814
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000815 case Stmt::ConditionalOperatorClass: { // '?' operator
816 ConditionalOperator* C = cast<ConditionalOperator>(S);
817 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
818 break;
819 }
820
821 case Stmt::DeclRefExprClass:
822 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
823 break;
824
825 case Stmt::DeclStmtClass:
826 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
827 break;
828
829 case Stmt::ImplicitCastExprClass: {
830 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
831 VisitCast(C, C->getSubExpr(), Pred, Dst);
832 break;
833 }
834
835 case Stmt::ParenExprClass:
836 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
837 break;
838
839 case Stmt::SizeOfAlignOfTypeExprClass:
840 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
841 break;
842
Ted Kremenekda9bd092008-02-08 07:05:39 +0000843 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000844 StmtExpr* SE = cast<StmtExpr>(S);
845
Ted Kremenekda9bd092008-02-08 07:05:39 +0000846 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +0000847 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
848 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +0000849 break;
850 }
851
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000852 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000853 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
854 Visit(R, Pred, Dst);
855 else
856 Dst.Add(Pred);
857
858 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000859 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000860
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000861 case Stmt::UnaryOperatorClass:
862 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000863 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000864 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000865}
866
Ted Kremenekee985462008-01-16 18:18:48 +0000867//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000868// "Assume" logic.
869//===----------------------------------------------------------------------===//
870
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000871GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000872 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000873 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000874
875 switch (Cond.getSubKind()) {
876 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000877 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000878 return St;
879
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000880 case lval::SymbolValKind:
881 if (Assumption)
882 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
883 ValMgr.getZeroWithPtrWidth(), isFeasible);
884 else
885 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
886 ValMgr.getZeroWithPtrWidth(), isFeasible);
887
Ted Kremenek08b66252008-02-06 04:31:33 +0000888
Ted Kremenek329f8542008-02-05 21:52:21 +0000889 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000890 isFeasible = Assumption;
891 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000892
Ted Kremenek329f8542008-02-05 21:52:21 +0000893 case lval::ConcreteIntKind: {
894 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000895 isFeasible = b ? Assumption : !Assumption;
896 return St;
897 }
898 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000899}
900
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000901GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000902 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000903 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000904
905 switch (Cond.getSubKind()) {
906 default:
907 assert (false && "'Assume' not implemented for this NonLValue.");
908 return St;
909
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000910
911 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +0000912 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +0000913 SymbolID sym = SV.getSymbol();
914
915 if (Assumption)
916 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
917 isFeasible);
918 else
919 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
920 isFeasible);
921 }
922
Ted Kremenek08b66252008-02-06 04:31:33 +0000923 case nonlval::SymIntConstraintValKind:
924 return
925 AssumeSymInt(St, Assumption,
926 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
927 isFeasible);
928
Ted Kremenek329f8542008-02-05 21:52:21 +0000929 case nonlval::ConcreteIntKind: {
930 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +0000931 isFeasible = b ? Assumption : !Assumption;
932 return St;
933 }
934 }
935}
936
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000937GRExprEngine::StateTy
938GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000939 const llvm::APSInt& V, bool& isFeasible) {
940
941 // First, determine if sym == X, where X != V.
942 if (const llvm::APSInt* X = St.getSymVal(sym)) {
943 isFeasible = *X != V;
944 return St;
945 }
946
947 // Second, determine if sym != V.
948 if (St.isNotEqual(sym, V)) {
949 isFeasible = true;
950 return St;
951 }
952
953 // If we reach here, sym is not a constant and we don't know if it is != V.
954 // Make that assumption.
955
956 isFeasible = true;
957 return StateMgr.AddNE(St, sym, V);
958}
959
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000960GRExprEngine::StateTy
961GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000962 const llvm::APSInt& V, bool& isFeasible) {
963
964 // First, determine if sym == X, where X != V.
965 if (const llvm::APSInt* X = St.getSymVal(sym)) {
966 isFeasible = *X == V;
967 return St;
968 }
969
970 // Second, determine if sym != V.
971 if (St.isNotEqual(sym, V)) {
972 isFeasible = false;
973 return St;
974 }
975
976 // If we reach here, sym is not a constant and we don't know if it is == V.
977 // Make that assumption.
978
979 isFeasible = true;
980 return StateMgr.AddEQ(St, sym, V);
981}
Ted Kremenekb38911f2008-01-30 23:03:39 +0000982
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000983GRExprEngine::StateTy
984GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +0000985 const SymIntConstraint& C, bool& isFeasible) {
986
987 switch (C.getOpcode()) {
988 default:
989 // No logic yet for other operators.
990 return St;
991
992 case BinaryOperator::EQ:
993 if (Assumption)
994 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
995 else
996 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
997
998 case BinaryOperator::NE:
999 if (Assumption)
1000 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1001 else
1002 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1003 }
1004}
1005
Ted Kremenekb38911f2008-01-30 23:03:39 +00001006//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001007// Driver.
1008//===----------------------------------------------------------------------===//
1009
Ted Kremenekaa66a322008-01-16 21:46:15 +00001010#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001011static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001012
Ted Kremenekaa66a322008-01-16 21:46:15 +00001013namespace llvm {
1014template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001015struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001016 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001017
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001018 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001019
1020 Out << "Variables:\\l";
1021
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001022 bool isFirst = true;
1023
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001024 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001025 E=St.vb_end(); I!=E;++I) {
1026
1027 if (isFirst)
1028 isFirst = false;
1029 else
1030 Out << "\\l";
1031
1032 Out << ' ' << I.getKey()->getName() << " : ";
1033 I.getData().print(Out);
1034 }
1035
1036 }
1037
Ted Kremeneke7d22112008-02-11 19:21:59 +00001038
Ted Kremenek44842c22008-02-13 18:06:44 +00001039 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001040
1041 bool isFirst = true;
1042
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001043 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001044 I != E;++I) {
1045
1046 if (isFirst) {
1047 Out << "\\l\\lSub-Expressions:\\l";
1048 isFirst = false;
1049 }
1050 else
1051 Out << "\\l";
1052
1053 Out << " (" << (void*) I.getKey() << ") ";
1054 I.getKey()->printPretty(Out);
1055 Out << " : ";
1056 I.getData().print(Out);
1057 }
1058 }
1059
Ted Kremenek44842c22008-02-13 18:06:44 +00001060 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001061
Ted Kremenek016f52f2008-02-08 21:10:02 +00001062 bool isFirst = true;
1063
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001064 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001065 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001066 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001067 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001068 isFirst = false;
1069 }
1070 else
1071 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001072
Ted Kremeneke7d22112008-02-11 19:21:59 +00001073 Out << " (" << (void*) I.getKey() << ") ";
1074 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001075 Out << " : ";
1076 I.getData().print(Out);
1077 }
1078 }
1079
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001080 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001081 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1082
1083 if (CE.isEmpty())
1084 return;
1085
1086 Out << "\\l\\|'==' constraints:";
1087
1088 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1089 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1090 }
1091
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001092 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneked4de312008-02-06 03:56:15 +00001093 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1094
1095 if (NE.isEmpty())
1096 return;
1097
1098 Out << "\\l\\|'!=' constraints:";
1099
1100 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1101 I != EI; ++I){
1102
1103 Out << "\\l $" << I.getKey() << " : ";
1104 bool isFirst = true;
1105
1106 ValueState::IntSetTy::iterator J=I.getData().begin(),
1107 EJ=I.getData().end();
1108 for ( ; J != EJ; ++J) {
1109 if (isFirst) isFirst = false;
1110 else Out << ", ";
1111
1112 Out << (*J)->toString();
1113 }
1114 }
1115 }
1116
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001117 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001118 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001119
1120 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001121 ProgramPoint Loc = N->getLocation();
1122
1123 switch (Loc.getKind()) {
1124 case ProgramPoint::BlockEntranceKind:
1125 Out << "Block Entrance: B"
1126 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1127 break;
1128
1129 case ProgramPoint::BlockExitKind:
1130 assert (false);
1131 break;
1132
1133 case ProgramPoint::PostStmtKind: {
1134 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001135 Out << L.getStmt()->getStmtClassName() << ':'
1136 << (void*) L.getStmt() << ' ';
1137
Ted Kremenekaa66a322008-01-16 21:46:15 +00001138 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001139
1140 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1141 Out << "\\|Implicit-Null Dereference.\\l";
1142 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001143 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1144 Out << "\\|Explicit-Null Dereference.\\l";
1145 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001146
Ted Kremenekaa66a322008-01-16 21:46:15 +00001147 break;
1148 }
1149
1150 default: {
1151 const BlockEdge& E = cast<BlockEdge>(Loc);
1152 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1153 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001154
1155 if (Stmt* T = E.getSrc()->getTerminator()) {
1156 Out << "\\|Terminator: ";
1157 E.getSrc()->printTerminator(Out);
1158
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001159 if (isa<SwitchStmt>(T)) {
1160 Stmt* Label = E.getDst()->getLabel();
1161
1162 if (Label) {
1163 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1164 Out << "\\lcase ";
1165 C->getLHS()->printPretty(Out);
1166
1167 if (Stmt* RHS = C->getRHS()) {
1168 Out << " .. ";
1169 RHS->printPretty(Out);
1170 }
1171
1172 Out << ":";
1173 }
1174 else {
1175 assert (isa<DefaultStmt>(Label));
1176 Out << "\\ldefault:";
1177 }
1178 }
1179 else
1180 Out << "\\l(implicit) default:";
1181 }
1182 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001183 // FIXME
1184 }
1185 else {
1186 Out << "\\lCondition: ";
1187 if (*E.getSrc()->succ_begin() == E.getDst())
1188 Out << "true";
1189 else
1190 Out << "false";
1191 }
1192
1193 Out << "\\l";
1194 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001195
1196 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1197 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1198 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001199 }
1200 }
1201
Ted Kremenek9153f732008-02-05 07:17:49 +00001202 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001203
Ted Kremeneke7d22112008-02-11 19:21:59 +00001204 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001205
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001206 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001207 return Out.str();
1208 }
1209};
1210} // end llvm namespace
1211#endif
1212
Ted Kremenekee985462008-01-16 18:18:48 +00001213namespace clang {
Ted Kremenek0ee25712008-02-13 17:45:18 +00001214void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
Ted Kremenek19227e32008-02-07 06:33:19 +00001215 Diagnostic& Diag) {
1216
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001217 GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001218 GRExprEngine* CheckerState = &Engine.getCheckerState();
Ted Kremenek77349cb2008-02-14 22:13:12 +00001219 GRSimpleVals GRSV;
1220 CheckerState->setTransferFunctions(GRSV);
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001221
1222 // Execute the worklist algorithm.
Ted Kremenek19227e32008-02-07 06:33:19 +00001223 Engine.ExecuteWorkList();
1224
1225 // Look for explicit-Null dereferences and warn about them.
Ted Kremenekd59cccc2008-02-14 18:28:23 +00001226
Ted Kremenek19227e32008-02-07 06:33:19 +00001227
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001228 for (GRExprEngine::null_iterator I=CheckerState->null_begin(),
Ted Kremenek19227e32008-02-07 06:33:19 +00001229 E=CheckerState->null_end(); I!=E; ++I) {
1230
1231 const PostStmt& L = cast<PostStmt>((*I)->getLocation());
1232 Expr* E = cast<Expr>(L.getStmt());
1233
1234 Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()),
1235 diag::chkr_null_deref_after_check);
1236 }
1237
1238
Ted Kremenekaa66a322008-01-16 21:46:15 +00001239#ifndef NDEBUG
Ted Kremenek19227e32008-02-07 06:33:19 +00001240 GraphPrintCheckerState = CheckerState;
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001241 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001242 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001243#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001244}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001245} // end clang namespace