blob: cc0923e7968ca90105c8d392ce95ece0e34e3850 [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 Kremeneke97ca062008-03-07 20:57:30 +000017#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000018#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000019
Ted Kremenek0f5f0592008-02-27 06:07:00 +000020#ifndef NDEBUG
21#include "llvm/Support/GraphWriter.h"
22#include <sstream>
23#endif
24
Ted Kremenek4bf38da2008-03-05 21:15:02 +000025// SaveAndRestore - A utility class that uses RIIA to save and restore
26// the value of a variable.
27template<typename T>
28struct VISIBILITY_HIDDEN SaveAndRestore {
29 SaveAndRestore(T& x) : X(x), old_value(x) {}
30 ~SaveAndRestore() { X = old_value; }
31 T get() { return old_value; }
32
33 T& X;
34 T old_value;
35};
36
Ted Kremenekb387a3f2008-02-14 22:16:04 +000037using namespace clang;
38using llvm::dyn_cast;
39using llvm::cast;
40using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000041
Ted Kremenekdaa497e2008-03-09 18:05:48 +000042
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000043ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +000044
45 // The LiveVariables information already has a compilation of all VarDecls
46 // used in the function. Iterate through this set, and "symbolicate"
47 // any VarDecl whose value originally comes from outside the function.
48
49 typedef LiveVariables::AnalysisDataTy LVDataTy;
50 LVDataTy& D = Liveness.getAnalysisData();
51
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000052 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +000053
54 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
55
56 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
57
58 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
59 RVal X = RVal::GetSymbolValue(SymMgr, VD);
60 StateMgr.BindVar(StateImpl, VD, X);
61 }
62 }
63
64 return StateMgr.getPersistentState(StateImpl);
65}
66
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000067ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000068
Ted Kremeneke070a1d2008-02-04 21:59:01 +000069 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000070
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000071 if (Ex == CurrentStmt) {
72 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000073
74 if (!isBlkExpr)
75 return St;
76 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000077
Ted Kremenek5a7b3822008-02-26 23:37:01 +000078 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000079}
80
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000081ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
82 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +000083
84 switch (Terminator->getStmtClass()) {
85 default:
86 return St;
87
88 case Stmt::BinaryOperatorClass: { // '&&' and '||'
89
90 BinaryOperator* B = cast<BinaryOperator>(Terminator);
91 BinaryOperator::Opcode Op = B->getOpcode();
92
93 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
94
95 // For &&, if we take the true branch, then the value of the whole
96 // expression is that of the RHS expression.
97 //
98 // For ||, if we take the false branch, then the value of the whole
99 // expression is that of the RHS expression.
100
101 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
102 (Op == BinaryOperator::LOr && !branchTaken)
103 ? B->getRHS() : B->getLHS();
104
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000105 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000106 }
107
108 case Stmt::ConditionalOperatorClass: { // ?:
109
110 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
111
112 // For ?, if branchTaken == true then the value is either the LHS or
113 // the condition itself. (GNU extension).
114
115 Expr* Ex;
116
117 if (branchTaken)
118 Ex = C->getLHS() ? C->getLHS() : C->getCond();
119 else
120 Ex = C->getRHS();
121
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000122 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000123 }
124
125 case Stmt::ChooseExprClass: { // ?:
126
127 ChooseExpr* C = cast<ChooseExpr>(Terminator);
128
129 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000130 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000131 }
132 }
133}
134
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000135bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
136 GRBlockCounter BC) {
137
138 return BC.getNumVisited(B->getBlockID()) < 3;
139}
140
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000141void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000142 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000143
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000145 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000146
Ted Kremenekb2331832008-02-15 22:29:00 +0000147 // Check for NULL conditions; e.g. "for(;;)"
148 if (!Condition) {
149 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000150 return;
151 }
152
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000153 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000154
155 switch (V.getBaseKind()) {
156 default:
157 break;
158
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000159 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000160 builder.generateNode(MarkBranch(PrevState, Term, true), true);
161 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000162 return;
163
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000164 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000165 NodeTy* N = builder.generateNode(PrevState, true);
166
167 if (N) {
168 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000169 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000170 }
171
172 builder.markInfeasible(false);
173 return;
174 }
175 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000176
Ted Kremenekb38911f2008-01-30 23:03:39 +0000177
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000178 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000179
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000180 bool isFeasible = false;
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000181 ValueState* St = Assume(PrevState, V, true, isFeasible);
182
183 if (isFeasible)
184 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000185 else
186 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000187
188 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000189
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000190 isFeasible = false;
191 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000192
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000193 if (isFeasible)
194 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000195 else
196 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000197}
198
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000199/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000200/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000201void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000202
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000203 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000204 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000205
206 // Three possibilities:
207 //
208 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000209 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000210 // (3) We have no clue about the label. Dispatch to all targets.
211 //
212
213 typedef IndirectGotoNodeBuilder::iterator iterator;
214
215 if (isa<lval::GotoLabel>(V)) {
216 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
217
218 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000219 if (I.getLabel() == L) {
220 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000221 return;
222 }
223 }
224
225 assert (false && "No block with label.");
226 return;
227 }
228
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000229 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000230 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000231 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000232 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000233 return;
234 }
235
236 // This is really a catch-all. We don't support symbolics yet.
237
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000238 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000239
240 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000241 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000242}
Ted Kremenekf233d482008-02-05 00:26:40 +0000243
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000244/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
245/// nodes by processing the 'effects' of a switch statement.
246void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
247
248 typedef SwitchNodeBuilder::iterator iterator;
249
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000250 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000251 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000252 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000253
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000254 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000255 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000256 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000257 return;
258 }
259
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000260 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000261
262 // While most of this can be assumed (such as the signedness), having it
263 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000264
Chris Lattner98be4942008-03-05 18:54:05 +0000265 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000266
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000267 APSInt V1(bits, false);
268 APSInt V2 = V1;
269
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000270 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000271
272 CaseStmt* Case = cast<CaseStmt>(I.getCase());
273
274 // Evaluate the case.
275 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
276 assert (false && "Case condition must evaluate to an integer constant.");
277 return;
278 }
279
280 // Get the RHS of the case, if it exists.
281
282 if (Expr* E = Case->getRHS()) {
283 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
284 assert (false &&
285 "Case condition (RHS) must evaluate to an integer constant.");
286 return ;
287 }
288
289 assert (V1 <= V2);
290 }
291 else V2 = V1;
292
293 // FIXME: Eventually we should replace the logic below with a range
294 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000295 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000296
297 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000298 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000299
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000300 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000301
302 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000303
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000304 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000305 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000306
307 if (isFeasible) {
308 builder.generateCaseStmtNode(I, StNew);
309
310 // If CondV evaluates to a constant, then we know that this
311 // is the *only* case that we can take, so stop evaluating the
312 // others.
313 if (isa<nonlval::ConcreteInt>(CondV))
314 return;
315 }
316
317 // Now "assume" that the case doesn't match. Add this state
318 // to the default state (if it is feasible).
319
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000320 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000321 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000322
323 if (isFeasible)
324 DefaultSt = StNew;
325
326 // Concretize the next value in the range.
327 ++V1;
328
329 } while (V1 < V2);
330 }
331
332 // If we reach here, than we know that the default branch is
333 // possible.
334 builder.generateDefaultCaseNode(DefaultSt);
335}
336
337
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000338void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000339 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000340
Ted Kremenek05a23782008-02-26 19:05:15 +0000341 assert (B->getOpcode() == BinaryOperator::LAnd ||
342 B->getOpcode() == BinaryOperator::LOr);
343
344 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
345
Ted Kremenek0d093d32008-03-10 04:11:42 +0000346 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000347 RVal X = GetBlkExprRVal(St, B);
348
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000349 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000350
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000351 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000352
353 assert (Ex);
354
355 if (Ex == B->getRHS()) {
356
357 X = GetBlkExprRVal(St, Ex);
358
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000359 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000360
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000361 if (X.isUndef()) {
Ted Kremenek58b33212008-02-26 19:40:44 +0000362 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
363 return;
364 }
365
Ted Kremenek05a23782008-02-26 19:05:15 +0000366 // We took the RHS. Because the value of the '&&' or '||' expression must
367 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
368 // or 1. Alternatively, we could take a lazy approach, and calculate this
369 // value later when necessary. We don't have the machinery in place for
370 // this right now, and since most logical expressions are used for branches,
371 // the payoff is not likely to be large. Instead, we do eager evaluation.
372
373 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000374 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000375
376 if (isFeasible)
377 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
378
379 isFeasible = false;
380 NewState = Assume(St, X, false, isFeasible);
381
382 if (isFeasible)
383 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000384 }
385 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000386 // We took the LHS expression. Depending on whether we are '&&' or
387 // '||' we know what the value of the expression is via properties of
388 // the short-circuiting.
389
390 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
391 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000392 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000393}
Ted Kremenek05a23782008-02-26 19:05:15 +0000394
Ted Kremenekf233d482008-02-05 00:26:40 +0000395
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000396void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000397
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000398 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000399 StmtEntryNode = builder.getLastNode();
400 CurrentStmt = S;
401 NodeSet Dst;
Ted Kremenek512c9132008-03-09 18:28:41 +0000402
403 // Create the cleaned state.
404
Ted Kremenek0d093d32008-03-10 04:11:42 +0000405 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
406 CurrentStmt, Liveness);
Ted Kremenek3bbad552008-03-10 04:45:00 +0000407
408 Builder->SetCleanedState(CleanedState);
Ted Kremenek512c9132008-03-09 18:28:41 +0000409
410 // Visit the statement.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000411
412 Visit(S, StmtEntryNode, Dst);
413
414 // If no nodes were generated, generate a new node that has all the
415 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000416
Ted Kremenek0d093d32008-03-10 04:11:42 +0000417 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode)
418 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
Ted Kremenekf84469b2008-01-18 00:41:32 +0000419
Ted Kremenek0d093d32008-03-10 04:11:42 +0000420 // NULL out these variables to cleanup.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000421
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000422 CurrentStmt = NULL;
423 StmtEntryNode = NULL;
424 Builder = NULL;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000425 CleanedState = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000426}
427
Ted Kremenek44842c22008-02-13 18:06:44 +0000428void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000429
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000430 if (D != CurrentStmt) {
431 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
432 return;
433 }
434
435 // If we are here, we are loading the value of the decl and binding
436 // it to the block-level expression.
437
Ted Kremenek0d093d32008-03-10 04:11:42 +0000438 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000439 RVal X = RVal::MakeVal(BasicVals, D);
440 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
441 Nodify(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000442}
443
Ted Kremenekde434242008-02-19 01:44:53 +0000444void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000445 CallExpr::arg_iterator AI,
446 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000447 NodeSet& Dst) {
448
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000449 // Process the arguments.
450
451 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000452
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000453 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000454 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000455 ++AI;
456
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000457 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000458 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000459
460 return;
461 }
462
463 // If we reach here we have processed all of the arguments. Evaluate
464 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000465
Ted Kremenek994a09b2008-02-25 21:16:03 +0000466 NodeSet DstTmp;
467 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000468
Ted Kremenek994a09b2008-02-25 21:16:03 +0000469 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000470
471 if (DstTmp.empty())
472 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000473
474 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000475 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
476
Ted Kremenek0d093d32008-03-10 04:11:42 +0000477 ValueState* St = GetState(*DI);
Ted Kremenek994a09b2008-02-25 21:16:03 +0000478 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000479
Ted Kremeneka1354a52008-03-03 16:47:31 +0000480 // FIXME: Add support for symbolic function calls (calls involving
481 // function pointer values that are symbolic).
482
483 // Check for undefined control-flow or calls to NULL.
484
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000485 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000486 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000487
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000488 if (N) {
489 N->markAsSink();
490 BadCalls.insert(N);
491 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000492
Ted Kremenekde434242008-02-19 01:44:53 +0000493 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000494 }
495
496 // Check for the "noreturn" attribute.
497
498 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
499
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000500 if (isa<lval::FuncVal>(L)) {
501
502 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
503
504 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000505 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000506 else {
507 // HACK: Some functions are not marked noreturn, and don't return.
508 // Here are a few hardwired ones. If this takes too long, we can
509 // potentially cache these results.
510 const char* s = FD->getIdentifier()->getName();
511 unsigned n = strlen(s);
512
513 switch (n) {
514 default:
515 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000516
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000517 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000518 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
519 break;
520
521 case 5:
522 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
523 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000524 }
525 }
526 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000527
528 // Evaluate the call.
529
Ted Kremeneka1354a52008-03-03 16:47:31 +0000530
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000531 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000532
Ted Kremenek03da0d72008-02-21 19:46:04 +0000533 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000534 // Check for an "unknown" callee.
535 invalidateArgs = true;
536 }
537 else if (isa<lval::FuncVal>(L)) {
538
539 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
540
Ted Kremenek55aea312008-03-05 22:59:42 +0000541 if (unsigned id = Info->getBuiltinID()) {
542 switch (id) {
543 case Builtin::BI__builtin_expect: {
544 // For __builtin_expect, just return the value of the subexpression.
545 assert (CE->arg_begin() != CE->arg_end());
546 RVal X = GetRVal(St, *(CE->arg_begin()));
547 Nodify(Dst, CE, *DI, SetRVal(St, CE, X));
548 continue;
549 }
550
551 default:
552 invalidateArgs = true;
553 break;
554 }
555 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000556 }
557
558 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000559 // Invalidate all arguments passed in by reference (LVals).
560 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
561 I != E; ++I) {
562 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000563
Ted Kremenek03da0d72008-02-21 19:46:04 +0000564 if (isa<LVal>(V))
565 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000566 }
567
568 Nodify(Dst, CE, *DI, St);
Ted Kremenek03da0d72008-02-21 19:46:04 +0000569 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000570 else {
571
572 // Check any arguments passed-by-value against being undefined.
573
574 bool badArg = false;
575
576 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
577 I != E; ++I) {
578
Ted Kremenek0d093d32008-03-10 04:11:42 +0000579 if (GetRVal(GetState(*DI), *I).isUndef()) {
580 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000581
582 if (N) {
583 N->markAsSink();
584 UndefArgs[N] = *I;
585 }
586
587 badArg = true;
588 break;
589 }
590 }
591
592 if (badArg)
593 continue;
594
595 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000596
597 unsigned size = Dst.size();
598
599 EvalCall(Dst, CE, cast<LVal>(L), *DI);
600
Ted Kremenek5dc7f8b2008-03-05 22:49:16 +0000601 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenek330dddd2008-03-05 00:33:14 +0000602 Nodify(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000603 }
Ted Kremenekde434242008-02-19 01:44:53 +0000604 }
605}
606
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000607void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000608
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000609 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000610 QualType T = CastE->getType();
611
Ted Kremenek65cfb732008-03-04 22:16:08 +0000612 if (T->isReferenceType())
613 VisitLVal(Ex, Pred, S1);
614 else
615 Visit(Ex, Pred, S1);
616
Ted Kremenek402563b2008-02-19 18:47:04 +0000617 // Check for redundant casts or casting to "void"
618 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000619 Ex->getType() == T ||
620 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000621
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000622 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000623 Dst.Add(*I1);
624
Ted Kremenek874d63f2008-01-24 02:02:54 +0000625 return;
626 }
627
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000628 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000629 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000630 ValueState* St = GetState(N);
Ted Kremenek65cfb732008-03-04 22:16:08 +0000631
632 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
633
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000634 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000635 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000636}
637
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000638void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000639 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000640
Ted Kremenek0d093d32008-03-10 04:11:42 +0000641 ValueState* St = GetState(Pred);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000642
643 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000644 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000645
646 // FIXME: Add support for local arrays.
647 if (VD->getType()->isArrayType())
648 continue;
649
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000650 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000651
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000652 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
653
654 // In this context, Static => Local variable.
655
656 assert (!VD->getStorageClass() == VarDecl::Static ||
657 !isa<FileVarDecl>(VD));
658
659 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000660 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000661 //
662 // FIXME: static variables may have an initializer, but the second
663 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000664
665 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000666
Ted Kremenek16d81562008-03-04 04:18:04 +0000667 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +0000668
669 // C99: 6.7.8 Initialization
670 // If an object that has static storage duration is not initialized
671 // explicitly, then:
672 // —if it has pointer type, it is initialized to a null pointer;
673 // —if it has arithmetic type, it is initialized to (positive or
674 // unsigned) zero;
675
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000676 // FIXME: Handle structs. Now we treat their values as unknown.
677
Ted Kremenek16d81562008-03-04 04:18:04 +0000678 if (T->isPointerType()) {
679
680 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +0000681 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +0000682 }
683 else if (T->isIntegerType()) {
684
685 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +0000686 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +0000687 }
688
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000689
Ted Kremenek16d81562008-03-04 04:18:04 +0000690 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000691 else {
Ted Kremenek16d81562008-03-04 04:18:04 +0000692
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000693 // FIXME: Handle structs. Now we treat them as unknown. What
694 // we need to do is treat their members as unknown.
695
696 if (T->isPointerType() || T->isIntegerType())
697 St = SetRVal(St, lval::DeclVal(VD),
698 Ex ? GetRVal(St, Ex) : UndefinedVal());
699 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000700 }
Ted Kremenek403c1812008-01-28 22:51:57 +0000701 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000702
703 Nodify(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000704}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000705
Ted Kremenekf233d482008-02-05 00:26:40 +0000706
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000707void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000708 NodeTy* Pred, NodeSet& Dst) {
709
Ted Kremenek05a23782008-02-26 19:05:15 +0000710 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
711
Ted Kremenek0d093d32008-03-10 04:11:42 +0000712 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000713 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000714
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000715 assert (X.isUndef());
Ted Kremenekf233d482008-02-05 00:26:40 +0000716
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000717 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000718
719 assert (SE);
720
721 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000722
723 // Make sure that we invalidate the previous binding.
724 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000725}
726
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000727/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000728void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
729 NodeTy* Pred,
730 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000731
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +0000733 uint64_t amt;
734
735 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000736
Ted Kremenek87e80342008-03-15 03:13:20 +0000737 // FIXME: Add support for VLAs.
738 if (!T.getTypePtr()->isConstantSizeType())
739 return;
740
741 amt = 1; // Handle sizeof(void)
742
743 if (T != getContext().VoidTy)
744 amt = getContext().getTypeSize(T) / 8;
745
746 }
747 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +0000748 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000749
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000750 Nodify(Dst, Ex, Pred,
Ted Kremenek0d093d32008-03-10 04:11:42 +0000751 SetRVal(GetState(Pred), Ex,
Ted Kremenek87e80342008-03-15 03:13:20 +0000752 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000753}
754
Ted Kremenekffe0f432008-03-07 22:58:01 +0000755void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
756 NodeSet& Dst, bool GetLVal) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000757
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000758 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000759
760 NodeSet DstTmp;
761
Ted Kremenek018c15f2008-02-26 03:44:25 +0000762 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000763 DstTmp.Add(Pred);
764 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000765 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000766
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000767 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000768
769 NodeTy* N = *I;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000770 ValueState* St = GetState(N);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000771
772 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
773
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000774 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000775
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000776 // Check for dereferences of undefined values.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000777
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000778 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000779
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000780 NodeTy* Succ = Builder->generateNode(U, St, N);
781
782 if (Succ) {
783 Succ->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000784 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000785 }
786
787 continue;
788 }
789
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000790 // Check for dereferences of unknown values. Treat as No-Ops.
791
792 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000793 Dst.Add(N);
794 continue;
795 }
796
797 // After a dereference, one of two possible situations arise:
798 // (1) A crash, because the pointer was NULL.
799 // (2) The pointer is not NULL, and the dereference works.
800 //
801 // We add these assumptions.
802
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000803 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000804 bool isFeasibleNotNull;
805
806 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000807
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000808 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000809
810 if (isFeasibleNotNull) {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000811
812 if (GetLVal) Nodify(Dst, U, N, SetRVal(StNotNull, U, LV));
813 else {
814
815 // FIXME: Currently symbolic analysis "generates" new symbols
816 // for the contents of values. We need a better approach.
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000817
Ted Kremenekffe0f432008-03-07 22:58:01 +0000818 Nodify(Dst, U, N, SetRVal(StNotNull, U,
819 GetRVal(StNotNull, LV, U->getType())));
820 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000821 }
822
823 bool isFeasibleNull;
824
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000825 // Now "assume" that the pointer is NULL.
826
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000827 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000828
829 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000830
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000831 // We don't use "Nodify" here because the node will be a sink
832 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000833
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000834 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
835
836 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000837
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000838 NullNode->markAsSink();
839
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000840 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
841 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000842 }
843 }
844 }
845}
846
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000847void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
848 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000849
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000850 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000851
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000852 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000853 assert (U->getOpcode() != UnaryOperator::SizeOf);
854 assert (U->getOpcode() != UnaryOperator::AlignOf);
855
856 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000857
858 switch (U->getOpcode()) {
859 case UnaryOperator::PostInc:
860 case UnaryOperator::PostDec:
861 case UnaryOperator::PreInc:
862 case UnaryOperator::PreDec:
863 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000864 // Evalue subexpression as an LVal.
865 use_GetLVal = true;
866 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000867 break;
868
869 default:
870 Visit(U->getSubExpr(), Pred, S1);
871 break;
872 }
873
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000874 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000875
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000876 NodeTy* N1 = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000877 ValueState* St = GetState(N1);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000878
879 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
880 GetRVal(St, U->getSubExpr());
881
882 if (SubV.isUnknown()) {
883 Dst.Add(N1);
884 continue;
885 }
886
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000887 if (SubV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000888 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
889 continue;
890 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000891
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000892 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000893
894 // Handle ++ and -- (both pre- and post-increment).
895
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000896 LVal SubLV = cast<LVal>(SubV);
897 RVal V = GetRVal(St, SubLV, U->getType());
898
Ted Kremenek89063af2008-02-21 19:15:37 +0000899 if (V.isUnknown()) {
900 Dst.Add(N1);
901 continue;
902 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000903
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000904 // Propagate undefined values.
905 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000906 Nodify(Dst, U, N1, SetRVal(St, U, V));
907 continue;
908 }
909
Ted Kremenek443003b2008-02-21 19:29:23 +0000910 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000911
912 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
913 : BinaryOperator::Sub;
914
Ted Kremenek443003b2008-02-21 19:29:23 +0000915 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000916
917 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000918 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000919 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000920 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000921
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000922 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000923 continue;
924 }
925
926 // Handle all other unary operators.
927
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000928 switch (U->getOpcode()) {
Ted Kremenek5139c782008-03-15 03:05:30 +0000929
930 case UnaryOperator::Extension:
931 St = SetRVal(St, U, SubV);
932 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000933
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000934 case UnaryOperator::Minus:
935 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000936 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000937
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000938 case UnaryOperator::Not:
939 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000940 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000941
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000942 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000943
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000944 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
945 //
946 // Note: technically we do "E == 0", but this is the same in the
947 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000948
949 if (isa<LVal>(SubV)) {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000950 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000951 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
952 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000953 }
954 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000955 Expr* Ex = U->getSubExpr();
Ted Kremenek240f1f02008-03-07 20:13:31 +0000956 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000957 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
958 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000959 }
960
961 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000962
Ted Kremenek64924852008-01-31 02:35:41 +0000963 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000964 assert (isa<LVal>(SubV));
965 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000966 break;
967 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000968
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000969 default: ;
970 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000971 }
972
973 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000974 }
975}
976
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000977void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
978 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000979
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000980 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000981
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000982 // FIXME: Add support for VLAs.
983 if (!T.getTypePtr()->isConstantSizeType())
984 return;
985
Chris Lattner98be4942008-03-05 18:54:05 +0000986 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000987 ValueState* St = GetState(Pred);
Ted Kremenek240f1f02008-03-07 20:13:31 +0000988 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000989
990 Nodify(Dst, U, Pred, St);
991}
992
993void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +0000994
995 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
996 Dst.Add(Pred);
997 return;
998 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000999
1000 Ex = Ex->IgnoreParens();
1001
Ted Kremenek07932632008-02-27 06:47:26 +00001002 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001003 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001004 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001005 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001006
Ted Kremenekffe0f432008-03-07 22:58:01 +00001007 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex))
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001008 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00001009 VisitDeref(U, Pred, Dst, true);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001010 return;
1011 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001012
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001013 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001014}
1015
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001016void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1017 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1018}
1019
1020void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1021 AsmStmt::outputs_iterator I,
1022 AsmStmt::outputs_iterator E,
1023 NodeTy* Pred, NodeSet& Dst) {
1024 if (I == E) {
1025 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1026 return;
1027 }
1028
1029 NodeSet Tmp;
1030 VisitLVal(*I, Pred, Tmp);
1031
1032 ++I;
1033
1034 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1035 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1036}
1037
1038void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1039 AsmStmt::inputs_iterator I,
1040 AsmStmt::inputs_iterator E,
1041 NodeTy* Pred, NodeSet& Dst) {
1042 if (I == E) {
1043
1044 // We have processed both the inputs and the outputs. All of the outputs
1045 // should evaluate to LVals. Nuke all of their values.
1046
1047 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1048 // which interprets the inline asm and stores proper results in the
1049 // outputs.
1050
1051 ValueState* St = GetState(Pred);
1052
1053 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1054 OE = A->end_outputs(); OI != OE; ++OI) {
1055
1056 RVal X = GetLVal(St, *OI);
1057
1058 assert (!isa<NonLVal>(X));
1059
1060 if (isa<LVal>(X))
1061 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1062 }
1063
1064 Nodify(Dst, A, Pred, St);
1065 return;
1066 }
1067
1068 NodeSet Tmp;
1069 Visit(*I, Pred, Tmp);
1070
1071 ++I;
1072
1073 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1074 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1075}
1076
1077
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001078void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001079 GRExprEngine::NodeTy* Pred,
1080 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001081 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001082
1083 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001084 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001085 else
1086 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001087
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001088 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001089
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001090 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001091
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001092 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001093 // In such cases, we want to (initially) treat the LHS as an LVal,
1094 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1095 // evaluated to LValDecl's instead of to an NonLVal.
1096
Ted Kremenek0d093d32008-03-10 04:11:42 +00001097 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1098 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001099
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001100 // Visit the RHS...
1101
1102 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001103 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001104
1105 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001106
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001107 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001108
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001109 NodeTy* N2 = *I2;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001110 ValueState* St = GetState(N2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001111 Expr* RHS = B->getRHS();
1112 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001113
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001114 BinaryOperator::Opcode Op = B->getOpcode();
1115
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001116 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1117 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001118
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001119 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001120
Ted Kremenek00155412008-02-26 22:27:51 +00001121 if (!RightV.isUnknown()) {
1122
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001123 if (RightV.isUndef()) {
1124 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001125
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001126 if (DivUndef) {
1127 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001128 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001129 }
1130
1131 continue;
1132 }
1133
1134 // Check for divide/remainder-by-zero.
1135 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001136 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001137
Ted Kremenek4d839b42008-03-07 19:04:53 +00001138 bool isFeasibleZero = false;
1139 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001140
Ted Kremenek00155412008-02-26 22:27:51 +00001141 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001142
Ted Kremenek4d839b42008-03-07 19:04:53 +00001143 bool isFeasibleNotZero = false;
1144 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001145
Ted Kremenek4d839b42008-03-07 19:04:53 +00001146 // Create the node for the divide-by-zero (if it occurred).
1147
1148 if (isFeasibleZero)
1149 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1150 DivZeroNode->markAsSink();
1151
1152 if (isFeasibleNotZero)
1153 ImplicitBadDivides.insert(DivZeroNode);
1154 else
1155 ExplicitBadDivides.insert(DivZeroNode);
1156
1157 }
1158
1159 if (!isFeasibleNotZero)
Ted Kremenek00155412008-02-26 22:27:51 +00001160 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001161 }
1162
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001163 // Fall-through. The logic below processes the divide.
1164 }
1165
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001166
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001167 if (Op <= BinaryOperator::Or) {
1168
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001169 // Process non-assignements except commas or short-circuited
1170 // logical expressions (LAnd and LOr).
1171
1172 RVal Result = EvalBinOp(Op, LeftV, RightV);
1173
1174 if (Result.isUnknown()) {
1175 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001176 continue;
1177 }
1178
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001179 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1180
1181 // The operands were not undefined, but the result is undefined.
1182
1183 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1184 UndefNode->markAsSink();
1185 UndefResults.insert(UndefNode);
1186 }
1187
1188 continue;
1189 }
1190
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001191 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001192 continue;
1193 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001194
1195 // Process assignments.
1196
1197 switch (Op) {
1198
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001199 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001200
1201 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001202
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001203 if (LeftV.isUndef()) {
1204 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001205 continue;
1206 }
1207
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001208 // EXPERIMENTAL: "Conjured" symbols.
1209
1210 if (RightV.isUnknown()) {
1211 unsigned Count = Builder->getCurrentBlockCount();
1212 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1213
1214 RightV = B->getRHS()->getType()->isPointerType()
1215 ? cast<RVal>(lval::SymbolVal(Sym))
1216 : cast<RVal>(nonlval::SymbolVal(Sym));
1217 }
1218
1219 // Even if the LHS evaluates to an unknown L-Value, the entire
1220 // expression still evaluates to the RHS.
1221
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001222 if (LeftV.isUnknown()) {
1223 St = SetRVal(St, B, RightV);
1224 break;
1225 }
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001226
1227 // Simulate the effects of a "store": bind the value of the RHS
1228 // to the L-Value represented by the LHS.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001229
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001230 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001231 break;
1232 }
1233
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001234 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001235
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001236 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001237
Ted Kremenek00155412008-02-26 22:27:51 +00001238 assert (B->isCompoundAssignmentOp());
1239
1240 if (Op >= BinaryOperator::AndAssign)
1241 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1242 else
1243 ((int&) Op) -= BinaryOperator::MulAssign;
1244
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001245 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001246
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001247 if (LeftV.isUndef()) {
1248 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001249 continue;
1250 }
1251
1252 if (LeftV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001253 assert (isa<UnknownVal>(GetRVal(St, B)));
1254 Dst.Add(N2);
1255 continue;
Ted Kremenek9dca0622008-02-19 00:22:37 +00001256 }
1257
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001258 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek3ce92142008-03-09 08:12:37 +00001259 // that is neither "Unknown" or "Undefined."
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001260
1261 LVal LeftLV = cast<LVal>(LeftV);
1262
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001263 // Fetch the value of the LHS (the value of the variable, etc.).
1264
Ted Kremenek0d093d32008-03-10 04:11:42 +00001265 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001266
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001267 // Propagate undefined value (left-side). We
1268 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001269 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001270
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001271 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001272 St = SetRVal(St, B, V);
1273 break;
1274 }
1275
1276 // Propagate unknown values.
1277
Ted Kremenek89063af2008-02-21 19:15:37 +00001278 if (V.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001279 // The value bound to LeftV is unknown. Thus we just
1280 // propagate the current node (as "B" is already bound to nothing).
1281 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenek89063af2008-02-21 19:15:37 +00001282 Dst.Add(N2);
1283 continue;
1284 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001285
1286 if (RightV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001287 assert (isa<UnknownVal>(GetRVal(St, B)));
1288 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001289 break;
1290 }
1291
Ted Kremenek00155412008-02-26 22:27:51 +00001292 // At this point:
1293 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001294 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001295 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001296
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001297 // Get the computation type.
1298 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1299
1300 // Perform promotions.
1301 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001302 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001303
1304 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001305
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001306 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1307 && RHS->getType()->isIntegerType()) {
1308
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001309 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001310
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001311 if (RightV.isUndef()) {
1312 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001313
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001314 if (DivUndef) {
1315 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001316 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001317 }
1318
1319 continue;
1320 }
Ted Kremenek00155412008-02-26 22:27:51 +00001321
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001322 // First, "assume" that the denominator is 0.
1323
Ted Kremenek4d839b42008-03-07 19:04:53 +00001324 bool isFeasibleZero = false;
1325 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001326
Ted Kremenek4d839b42008-03-07 19:04:53 +00001327 // Second, "assume" that the denominator cannot be 0.
1328
1329 bool isFeasibleNotZero = false;
1330 St = Assume(St, RightV, true, isFeasibleNotZero);
1331
1332 // Create the node for the divide-by-zero error (if it occurred).
1333
1334 if (isFeasibleZero) {
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001335 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1336
1337 if (DivZeroNode) {
1338 DivZeroNode->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001339
1340 if (isFeasibleNotZero)
1341 ImplicitBadDivides.insert(DivZeroNode);
1342 else
1343 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001344 }
1345 }
1346
Ted Kremenek4d839b42008-03-07 19:04:53 +00001347 if (!isFeasibleNotZero)
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001348 continue;
1349
1350 // Fall-through. The logic below processes the divide.
1351 }
Ted Kremenek00155412008-02-26 22:27:51 +00001352 else {
1353
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001354 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001355
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001356 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001357 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1358 break;
1359 }
1360
1361 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001362
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001363 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001364
1365 if (Result.isUndef()) {
1366
1367 // The operands were not undefined, but the result is undefined.
1368
1369 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1370 UndefNode->markAsSink();
1371 UndefResults.insert(UndefNode);
1372 }
1373
1374 continue;
1375 }
1376
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001377 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001378 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001379 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001380
1381 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001382 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001383 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001384}
Ted Kremenekee985462008-01-16 18:18:48 +00001385
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001386void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00001387 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001388 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001389 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001390}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001391
Ted Kremenek9dca0622008-02-19 00:22:37 +00001392void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001393
1394 // FIXME: add metadata to the CFG so that we can disable
1395 // this check when we KNOW that there is no block-level subexpression.
1396 // The motivation is that this check requires a hashtable lookup.
1397
1398 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1399 Dst.Add(Pred);
1400 return;
1401 }
1402
1403 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001404
1405 default:
1406 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001407 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001408
1409 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1410 break;
1411
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001412 case Stmt::BinaryOperatorClass: {
1413 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001414
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001415 if (B->isLogicalOp()) {
1416 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001417 break;
1418 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001419 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00001420 ValueState* St = GetState(Pred);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001421 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001422 break;
1423 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001424
1425 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1426 break;
1427 }
Ted Kremenekde434242008-02-19 01:44:53 +00001428
1429 case Stmt::CallExprClass: {
1430 CallExpr* C = cast<CallExpr>(S);
1431 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1432 break;
1433 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001434
1435 case Stmt::CastExprClass: {
1436 CastExpr* C = cast<CastExpr>(S);
1437 VisitCast(C, C->getSubExpr(), Pred, Dst);
1438 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001439 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001440
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001441 // FIXME: ChooseExpr is really a constant. We need to fix
1442 // the CFG do not model them as explicit control-flow.
1443
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001444 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1445 ChooseExpr* C = cast<ChooseExpr>(S);
1446 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1447 break;
1448 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001449
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001450 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001451 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1452 break;
1453
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001454 case Stmt::ConditionalOperatorClass: { // '?' operator
1455 ConditionalOperator* C = cast<ConditionalOperator>(S);
1456 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1457 break;
1458 }
1459
1460 case Stmt::DeclRefExprClass:
1461 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1462 break;
1463
1464 case Stmt::DeclStmtClass:
1465 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1466 break;
1467
1468 case Stmt::ImplicitCastExprClass: {
1469 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1470 VisitCast(C, C->getSubExpr(), Pred, Dst);
1471 break;
1472 }
1473
1474 case Stmt::ParenExprClass:
1475 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1476 break;
1477
1478 case Stmt::SizeOfAlignOfTypeExprClass:
1479 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1480 break;
1481
Ted Kremenekda9bd092008-02-08 07:05:39 +00001482 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001483 StmtExpr* SE = cast<StmtExpr>(S);
1484
Ted Kremenek0d093d32008-03-10 04:11:42 +00001485 ValueState* St = GetState(Pred);
Ted Kremenek3b4a8ef2008-03-15 03:27:30 +00001486
1487 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
1488 // probably just remove these from the CFG.
1489 assert (!SE->getSubStmt()->body_empty());
1490
1491 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
1492 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
1493 else
1494 Dst.Add(Pred);
1495
1496 break;
Ted Kremenekda9bd092008-02-08 07:05:39 +00001497 }
1498
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001499 // FIXME: We may wish to always bind state to ReturnStmts so
1500 // that users can quickly query what was the state at the
1501 // exit points of a function.
1502
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001503 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001504 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1505 Visit(R, Pred, Dst);
1506 else
1507 Dst.Add(Pred);
1508
1509 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001510 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001511
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001512 case Stmt::UnaryOperatorClass: {
1513 UnaryOperator* U = cast<UnaryOperator>(S);
1514
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001515 switch (U->getOpcode()) {
1516 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1517 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1518 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1519 default: VisitUnaryOperator(U, Pred, Dst); break;
1520 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001521
Ted Kremenek9de04c42008-01-24 20:55:43 +00001522 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001523 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001524 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001525}
1526
Ted Kremenekee985462008-01-16 18:18:48 +00001527//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001528// "Assume" logic.
1529//===----------------------------------------------------------------------===//
1530
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001531ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001532 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001533 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001534 switch (Cond.getSubKind()) {
1535 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001536 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001537 return St;
1538
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001539 case lval::SymbolValKind:
1540 if (Assumption)
1541 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001542 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001543 else
1544 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001545 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001546
Ted Kremenek08b66252008-02-06 04:31:33 +00001547
Ted Kremenek329f8542008-02-05 21:52:21 +00001548 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001549 case lval::FuncValKind:
1550 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001551 isFeasible = Assumption;
1552 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001553
Ted Kremenek329f8542008-02-05 21:52:21 +00001554 case lval::ConcreteIntKind: {
1555 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001556 isFeasible = b ? Assumption : !Assumption;
1557 return St;
1558 }
1559 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001560}
1561
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001562ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001563 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001564 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001565 switch (Cond.getSubKind()) {
1566 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001567 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001568 return St;
1569
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001570
1571 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001572 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001573 SymbolID sym = SV.getSymbol();
1574
1575 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00001576 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001577 isFeasible);
1578 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00001579 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001580 isFeasible);
1581 }
1582
Ted Kremenek08b66252008-02-06 04:31:33 +00001583 case nonlval::SymIntConstraintValKind:
1584 return
1585 AssumeSymInt(St, Assumption,
1586 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1587 isFeasible);
1588
Ted Kremenek329f8542008-02-05 21:52:21 +00001589 case nonlval::ConcreteIntKind: {
1590 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001591 isFeasible = b ? Assumption : !Assumption;
1592 return St;
1593 }
1594 }
1595}
1596
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001597ValueState*
1598GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001599 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001600
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001601 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001602 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001603 isFeasible = *X != V;
1604 return St;
1605 }
1606
1607 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001608 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001609 isFeasible = true;
1610 return St;
1611 }
1612
1613 // If we reach here, sym is not a constant and we don't know if it is != V.
1614 // Make that assumption.
1615
1616 isFeasible = true;
1617 return StateMgr.AddNE(St, sym, V);
1618}
1619
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001620ValueState*
1621GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001622 const llvm::APSInt& V, bool& isFeasible) {
1623
1624 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001625 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001626 isFeasible = *X == V;
1627 return St;
1628 }
1629
1630 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001631 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001632 isFeasible = false;
1633 return St;
1634 }
1635
1636 // If we reach here, sym is not a constant and we don't know if it is == V.
1637 // Make that assumption.
1638
1639 isFeasible = true;
1640 return StateMgr.AddEQ(St, sym, V);
1641}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001642
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001643ValueState*
1644GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001645 const SymIntConstraint& C, bool& isFeasible) {
1646
1647 switch (C.getOpcode()) {
1648 default:
1649 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001650 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00001651 return St;
1652
1653 case BinaryOperator::EQ:
1654 if (Assumption)
1655 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1656 else
1657 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1658
1659 case BinaryOperator::NE:
1660 if (Assumption)
1661 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1662 else
1663 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1664 }
1665}
1666
Ted Kremenekb38911f2008-01-30 23:03:39 +00001667//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001668// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001669//===----------------------------------------------------------------------===//
1670
Ted Kremenekaa66a322008-01-16 21:46:15 +00001671#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001672static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00001673static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00001674static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001675
Ted Kremenekaa66a322008-01-16 21:46:15 +00001676namespace llvm {
1677template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001678struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001679 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001680
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001681 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001682
1683 Out << "Variables:\\l";
1684
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001685 bool isFirst = true;
1686
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001687 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001688
1689 if (isFirst)
1690 isFirst = false;
1691 else
1692 Out << "\\l";
1693
1694 Out << ' ' << I.getKey()->getName() << " : ";
1695 I.getData().print(Out);
1696 }
1697
1698 }
1699
Ted Kremeneke7d22112008-02-11 19:21:59 +00001700
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001701 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001702
1703 bool isFirst = true;
1704
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001705 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001706
1707 if (isFirst) {
1708 Out << "\\l\\lSub-Expressions:\\l";
1709 isFirst = false;
1710 }
1711 else
1712 Out << "\\l";
1713
1714 Out << " (" << (void*) I.getKey() << ") ";
1715 I.getKey()->printPretty(Out);
1716 Out << " : ";
1717 I.getData().print(Out);
1718 }
1719 }
1720
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001721 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001722
Ted Kremenek016f52f2008-02-08 21:10:02 +00001723 bool isFirst = true;
1724
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001725 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001726 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001727 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001728 isFirst = false;
1729 }
1730 else
1731 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001732
Ted Kremeneke7d22112008-02-11 19:21:59 +00001733 Out << " (" << (void*) I.getKey() << ") ";
1734 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001735 Out << " : ";
1736 I.getData().print(Out);
1737 }
1738 }
1739
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001740 static void PrintEQ(std::ostream& Out, ValueState* St) {
1741 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001742
1743 if (CE.isEmpty())
1744 return;
1745
1746 Out << "\\l\\|'==' constraints:";
1747
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001748 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001749 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1750 }
1751
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001752 static void PrintNE(std::ostream& Out, ValueState* St) {
1753 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001754
1755 if (NE.isEmpty())
1756 return;
1757
1758 Out << "\\l\\|'!=' constraints:";
1759
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001760 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001761 I != EI; ++I){
1762
1763 Out << "\\l $" << I.getKey() << " : ";
1764 bool isFirst = true;
1765
1766 ValueState::IntSetTy::iterator J=I.getData().begin(),
1767 EJ=I.getData().end();
1768 for ( ; J != EJ; ++J) {
1769 if (isFirst) isFirst = false;
1770 else Out << ", ";
1771
1772 Out << (*J)->toString();
1773 }
1774 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001775 }
1776
1777 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1778
1779 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001780 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001781 GraphPrintCheckerState->isUndefDeref(N) ||
1782 GraphPrintCheckerState->isUndefStore(N) ||
1783 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00001784 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1785 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001786 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001787 GraphPrintCheckerState->isBadCall(N) ||
1788 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001789 return "color=\"red\",style=\"filled\"";
1790
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001791 if (GraphPrintCheckerState->isNoReturnCall(N))
1792 return "color=\"blue\",style=\"filled\"";
1793
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001794 return "";
1795 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001796
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001797 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001798 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001799
1800 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001801 ProgramPoint Loc = N->getLocation();
1802
1803 switch (Loc.getKind()) {
1804 case ProgramPoint::BlockEntranceKind:
1805 Out << "Block Entrance: B"
1806 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1807 break;
1808
1809 case ProgramPoint::BlockExitKind:
1810 assert (false);
1811 break;
1812
1813 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00001814 const PostStmt& L = cast<PostStmt>(Loc);
1815 Stmt* S = L.getStmt();
1816 SourceLocation SLoc = S->getLocStart();
1817
1818 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
1819 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001820
Ted Kremenek9b5551d2008-03-09 03:30:59 +00001821 if (SLoc.isFileID()) {
1822 Out << "\\lline="
1823 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1824 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
1825 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001826
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001827 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001828 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001829 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00001830 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001831 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001832 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001833 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001834 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00001835 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1836 Out << "\\|Explicit divide-by zero or undefined value.";
1837 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
1838 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001839 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001840 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001841 else if (GraphPrintCheckerState->isNoReturnCall(N))
1842 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001843 else if (GraphPrintCheckerState->isBadCall(N))
1844 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001845 else if (GraphPrintCheckerState->isUndefArg(N))
1846 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001847
Ted Kremenekaa66a322008-01-16 21:46:15 +00001848 break;
1849 }
1850
1851 default: {
1852 const BlockEdge& E = cast<BlockEdge>(Loc);
1853 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1854 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001855
1856 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00001857
1858 SourceLocation SLoc = T->getLocStart();
1859
Ted Kremenekb38911f2008-01-30 23:03:39 +00001860 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00001861
Ted Kremenekb38911f2008-01-30 23:03:39 +00001862 E.getSrc()->printTerminator(Out);
1863
Ted Kremenek9b5551d2008-03-09 03:30:59 +00001864 if (SLoc.isFileID()) {
1865 Out << "\\lline="
1866 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1867 << GraphPrintSourceManager->getColumnNumber(SLoc);
1868 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00001869
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001870 if (isa<SwitchStmt>(T)) {
1871 Stmt* Label = E.getDst()->getLabel();
1872
1873 if (Label) {
1874 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1875 Out << "\\lcase ";
1876 C->getLHS()->printPretty(Out);
1877
1878 if (Stmt* RHS = C->getRHS()) {
1879 Out << " .. ";
1880 RHS->printPretty(Out);
1881 }
1882
1883 Out << ":";
1884 }
1885 else {
1886 assert (isa<DefaultStmt>(Label));
1887 Out << "\\ldefault:";
1888 }
1889 }
1890 else
1891 Out << "\\l(implicit) default:";
1892 }
1893 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001894 // FIXME
1895 }
1896 else {
1897 Out << "\\lCondition: ";
1898 if (*E.getSrc()->succ_begin() == E.getDst())
1899 Out << "true";
1900 else
1901 Out << "false";
1902 }
1903
1904 Out << "\\l";
1905 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001906
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001907 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1908 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001909 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001910 }
1911 }
1912
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001913 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001914
Ted Kremenek75da3e82008-03-11 19:02:40 +00001915 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001916
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001917 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001918 return Out.str();
1919 }
1920};
1921} // end llvm namespace
1922#endif
1923
Ted Kremenekffe0f432008-03-07 22:58:01 +00001924#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00001925
1926template <typename ITERATOR>
1927GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
1928
1929template <>
1930GRExprEngine::NodeTy*
1931GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
1932 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
1933 return I->first;
1934}
1935
Ted Kremenekffe0f432008-03-07 22:58:01 +00001936template <typename ITERATOR>
1937static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00001938 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00001939
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00001940 llvm::SmallPtrSet<void*,10> CachedSources;
1941
1942 for ( ; I != E; ++I ) {
1943 GRExprEngine::NodeTy* N = GetGraphNode(I);
1944 void* p = N->getLocation().getRawData();
1945
1946 if (CachedSources.count(p))
1947 continue;
1948
1949 CachedSources.insert(p);
1950
1951 Sources.push_back(N);
1952 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00001953}
1954#endif
1955
1956void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00001957#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00001958 if (trim) {
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00001959 llvm::SmallVector<NodeTy*, 10> Src;
1960 AddSources(Src, null_derefs_begin(), null_derefs_end());
1961 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
1962 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
1963 AddSources(Src, undef_results_begin(), undef_results_end());
1964 AddSources(Src, bad_calls_begin(), bad_calls_end());
1965 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00001966 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00001967
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00001968 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00001969 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00001970 else {
1971 GraphPrintCheckerState = this;
1972 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00001973 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00001974
Ted Kremenekffe0f432008-03-07 22:58:01 +00001975 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00001976
1977 GraphPrintCheckerState = NULL;
1978 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00001979 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00001980 }
1981#endif
1982}
1983
1984void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
1985#ifndef NDEBUG
1986 GraphPrintCheckerState = this;
1987 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00001988 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00001989
Ted Kremenek493d7a22008-03-11 18:25:33 +00001990 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
1991
1992 if (!TrimmedG)
1993 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
1994 else {
1995 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
1996 delete TrimmedG;
1997 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00001998
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001999 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002000 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002001 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002002#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002003}