blob: e7f6f778595bb309471e2caf6727ade44d1c9e8d [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000017#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000018
Ted Kremenek0f5f0592008-02-27 06:07:00 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#include <sstream>
22#endif
23
Ted Kremenekb387a3f2008-02-14 22:16:04 +000024using namespace clang;
25using llvm::dyn_cast;
26using llvm::cast;
27using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000028
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000029ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +000030
31 // The LiveVariables information already has a compilation of all VarDecls
32 // used in the function. Iterate through this set, and "symbolicate"
33 // any VarDecl whose value originally comes from outside the function.
34
35 typedef LiveVariables::AnalysisDataTy LVDataTy;
36 LVDataTy& D = Liveness.getAnalysisData();
37
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000038 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +000039
40 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
41
42 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
43
44 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
45 RVal X = RVal::GetSymbolValue(SymMgr, VD);
46 StateMgr.BindVar(StateImpl, VD, X);
47 }
48 }
49
50 return StateMgr.getPersistentState(StateImpl);
51}
52
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000053ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000054
Ted Kremeneke070a1d2008-02-04 21:59:01 +000055 if (!StateCleaned) {
56 St = RemoveDeadBindings(CurrentStmt, St);
57 StateCleaned = true;
58 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000059
Ted Kremeneke070a1d2008-02-04 21:59:01 +000060 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000061
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000062 if (Ex == CurrentStmt) {
63 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000064
65 if (!isBlkExpr)
66 return St;
67 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000068
Ted Kremenek5a7b3822008-02-26 23:37:01 +000069 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000070}
71
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000072#if 0
73const ValueState::BufferTy&
74GRExprEngine::SetRVal(ValueState* St, Expr* Ex, const RVal::BufferTy& RB,
75 ValueState::BufferTy& RetBuf) {
Ted Kremenekcba2e432008-02-05 19:35:18 +000076
77 assert (RetBuf.empty());
78
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000079 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
80 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekcba2e432008-02-05 19:35:18 +000081
82 return RetBuf;
83}
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000084#endif
Ted Kremenekcba2e432008-02-05 19:35:18 +000085
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000086ValueState* GRExprEngine::SetRVal(ValueState* St, LVal LV, RVal RV) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000087
88 if (!StateCleaned) {
89 St = RemoveDeadBindings(CurrentStmt, St);
90 StateCleaned = true;
91 }
92
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000093 return StateMgr.SetRVal(St, LV, RV);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000094}
95
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000096ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
97 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +000098
99 switch (Terminator->getStmtClass()) {
100 default:
101 return St;
102
103 case Stmt::BinaryOperatorClass: { // '&&' and '||'
104
105 BinaryOperator* B = cast<BinaryOperator>(Terminator);
106 BinaryOperator::Opcode Op = B->getOpcode();
107
108 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
109
110 // For &&, if we take the true branch, then the value of the whole
111 // expression is that of the RHS expression.
112 //
113 // For ||, if we take the false branch, then the value of the whole
114 // expression is that of the RHS expression.
115
116 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
117 (Op == BinaryOperator::LOr && !branchTaken)
118 ? B->getRHS() : B->getLHS();
119
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000120 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000121 }
122
123 case Stmt::ConditionalOperatorClass: { // ?:
124
125 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
126
127 // For ?, if branchTaken == true then the value is either the LHS or
128 // the condition itself. (GNU extension).
129
130 Expr* Ex;
131
132 if (branchTaken)
133 Ex = C->getLHS() ? C->getLHS() : C->getCond();
134 else
135 Ex = C->getRHS();
136
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000137 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000138 }
139
140 case Stmt::ChooseExprClass: { // ?:
141
142 ChooseExpr* C = cast<ChooseExpr>(Terminator);
143
144 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000145 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000146 }
147 }
148}
149
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000150bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
151 GRBlockCounter BC) {
152
153 return BC.getNumVisited(B->getBlockID()) < 3;
154}
155
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000156void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000157 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000158
Ted Kremeneke7d22112008-02-11 19:21:59 +0000159 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000160 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000161
Ted Kremenekb2331832008-02-15 22:29:00 +0000162 // Check for NULL conditions; e.g. "for(;;)"
163 if (!Condition) {
164 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000165 return;
166 }
167
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000168 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000169
170 switch (V.getBaseKind()) {
171 default:
172 break;
173
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000174 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000175 builder.generateNode(MarkBranch(PrevState, Term, true), true);
176 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000177 return;
178
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000179 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000180 NodeTy* N = builder.generateNode(PrevState, true);
181
182 if (N) {
183 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000184 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000185 }
186
187 builder.markInfeasible(false);
188 return;
189 }
190 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000191
Ted Kremenekb38911f2008-01-30 23:03:39 +0000192
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000193 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000194
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000195 bool isFeasible = true;
196 ValueState* St = Assume(PrevState, V, true, isFeasible);
197
198 if (isFeasible)
199 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000200 else
201 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000202
203 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000204
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000205 isFeasible = false;
206 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000207
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000208 if (isFeasible)
209 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000210 else
211 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000212}
213
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000214/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000215/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000216void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000217
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000218 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000219 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000220
221 // Three possibilities:
222 //
223 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000224 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000225 // (3) We have no clue about the label. Dispatch to all targets.
226 //
227
228 typedef IndirectGotoNodeBuilder::iterator iterator;
229
230 if (isa<lval::GotoLabel>(V)) {
231 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
232
233 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000234 if (I.getLabel() == L) {
235 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000236 return;
237 }
238 }
239
240 assert (false && "No block with label.");
241 return;
242 }
243
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000244 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000245 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000246 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000247 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000248 return;
249 }
250
251 // This is really a catch-all. We don't support symbolics yet.
252
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000253 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000254
255 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000256 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000257}
Ted Kremenekf233d482008-02-05 00:26:40 +0000258
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000259/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
260/// nodes by processing the 'effects' of a switch statement.
261void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
262
263 typedef SwitchNodeBuilder::iterator iterator;
264
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000265 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000266 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000267 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000268
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000269 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000270 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000271 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000272 return;
273 }
274
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000275 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000276
277 // While most of this can be assumed (such as the signedness), having it
278 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000279
280 unsigned bits = getContext().getTypeSize(CondE->getType(),
281 CondE->getExprLoc());
282
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000283 APSInt V1(bits, false);
284 APSInt V2 = V1;
285
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000286 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000287
288 CaseStmt* Case = cast<CaseStmt>(I.getCase());
289
290 // Evaluate the case.
291 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
292 assert (false && "Case condition must evaluate to an integer constant.");
293 return;
294 }
295
296 // Get the RHS of the case, if it exists.
297
298 if (Expr* E = Case->getRHS()) {
299 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
300 assert (false &&
301 "Case condition (RHS) must evaluate to an integer constant.");
302 return ;
303 }
304
305 assert (V1 <= V2);
306 }
307 else V2 = V1;
308
309 // FIXME: Eventually we should replace the logic below with a range
310 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000311 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000312
313 do {
314 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
315
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000316 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000317
318 // Now "assume" that the case matches.
Ted Kremenek692416c2008-02-18 22:57:02 +0000319 bool isFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000320
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000321 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000322
323 if (isFeasible) {
324 builder.generateCaseStmtNode(I, StNew);
325
326 // If CondV evaluates to a constant, then we know that this
327 // is the *only* case that we can take, so stop evaluating the
328 // others.
329 if (isa<nonlval::ConcreteInt>(CondV))
330 return;
331 }
332
333 // Now "assume" that the case doesn't match. Add this state
334 // to the default state (if it is feasible).
335
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000336 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000337
338 if (isFeasible)
339 DefaultSt = StNew;
340
341 // Concretize the next value in the range.
342 ++V1;
343
344 } while (V1 < V2);
345 }
346
347 // If we reach here, than we know that the default branch is
348 // possible.
349 builder.generateDefaultCaseNode(DefaultSt);
350}
351
352
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000353void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000354 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000355
Ted Kremenek05a23782008-02-26 19:05:15 +0000356 assert (B->getOpcode() == BinaryOperator::LAnd ||
357 B->getOpcode() == BinaryOperator::LOr);
358
359 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
360
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000361 ValueState* St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000362 RVal X = GetBlkExprRVal(St, B);
363
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000364 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000365
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000366 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000367
368 assert (Ex);
369
370 if (Ex == B->getRHS()) {
371
372 X = GetBlkExprRVal(St, Ex);
373
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000374 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000375
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000376 if (X.isUndef()) {
Ted Kremenek58b33212008-02-26 19:40:44 +0000377 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
378 return;
379 }
380
Ted Kremenek05a23782008-02-26 19:05:15 +0000381 // We took the RHS. Because the value of the '&&' or '||' expression must
382 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
383 // or 1. Alternatively, we could take a lazy approach, and calculate this
384 // value later when necessary. We don't have the machinery in place for
385 // this right now, and since most logical expressions are used for branches,
386 // the payoff is not likely to be large. Instead, we do eager evaluation.
387
388 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000389 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000390
391 if (isFeasible)
392 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
393
394 isFeasible = false;
395 NewState = Assume(St, X, false, isFeasible);
396
397 if (isFeasible)
398 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000399 }
400 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000401 // We took the LHS expression. Depending on whether we are '&&' or
402 // '||' we know what the value of the expression is via properties of
403 // the short-circuiting.
404
405 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
406 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000407 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000408}
Ted Kremenek05a23782008-02-26 19:05:15 +0000409
Ted Kremenekf233d482008-02-05 00:26:40 +0000410
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000411void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000412
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000413 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000414 StmtEntryNode = builder.getLastNode();
415 CurrentStmt = S;
416 NodeSet Dst;
417 StateCleaned = false;
418
419 Visit(S, StmtEntryNode, Dst);
420
421 // If no nodes were generated, generate a new node that has all the
422 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000423
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000424 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000425 ValueState* St = RemoveDeadBindings(S, StmtEntryNode->getState());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000426 builder.generateNode(S, St, StmtEntryNode);
427 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000428
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000429 // For safety, NULL out these variables.
430
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000431 CurrentStmt = NULL;
432 StmtEntryNode = NULL;
433 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000434}
435
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000436GRExprEngine::NodeTy*
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000437GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, ValueState* St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000438
439 // If the state hasn't changed, don't generate a new node.
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000440 if (St == Pred->getState()) {
441 Dst.Add(Pred);
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000442 return NULL;
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000443 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000444
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000445 NodeTy* N = Builder->generateNode(S, St, Pred);
446 Dst.Add(N);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000447
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000448 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000449}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000450
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000451#if 0
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000452void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000453 const ValueState::BufferTy& SB) {
Ted Kremenekcba2e432008-02-05 19:35:18 +0000454
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000455 for (ValueState::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
Ted Kremenekcba2e432008-02-05 19:35:18 +0000456 Nodify(Dst, S, Pred, *I);
457}
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000458#endif
Ted Kremenekcba2e432008-02-05 19:35:18 +0000459
Ted Kremenek44842c22008-02-13 18:06:44 +0000460void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000461
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000462 if (D != CurrentStmt) {
463 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
464 return;
465 }
466
467 // If we are here, we are loading the value of the decl and binding
468 // it to the block-level expression.
469
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000470 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000471 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000472}
473
Ted Kremenekde434242008-02-19 01:44:53 +0000474void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000475 CallExpr::arg_iterator AI,
476 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000477 NodeSet& Dst) {
478
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000479 // Process the arguments.
480
481 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000482
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000483 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000484 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000485 ++AI;
486
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000487 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000488 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000489
490 return;
491 }
492
493 // If we reach here we have processed all of the arguments. Evaluate
494 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000495
Ted Kremenek994a09b2008-02-25 21:16:03 +0000496 NodeSet DstTmp;
497 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000498
Ted Kremenek994a09b2008-02-25 21:16:03 +0000499 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000500
501 if (DstTmp.empty())
502 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000503
504 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000505 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
506
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000507 ValueState* St = (*DI)->getState();
Ted Kremenek994a09b2008-02-25 21:16:03 +0000508 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000509
Ted Kremeneka1354a52008-03-03 16:47:31 +0000510 // FIXME: Add support for symbolic function calls (calls involving
511 // function pointer values that are symbolic).
512
513 // Check for undefined control-flow or calls to NULL.
514
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000515 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000516 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000517
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000518 if (N) {
519 N->markAsSink();
520 BadCalls.insert(N);
521 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000522
Ted Kremenekde434242008-02-19 01:44:53 +0000523 continue;
Ted Kremeneka1354a52008-03-03 16:47:31 +0000524 }
525
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000526 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000527
Ted Kremenek03da0d72008-02-21 19:46:04 +0000528 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000529 // Check for an "unknown" callee.
530 invalidateArgs = true;
531 }
532 else if (isa<lval::FuncVal>(L)) {
533
534 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
535
536 if (Info->getBuiltinID())
537 invalidateArgs = true;
538 }
539
540 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000541 // Invalidate all arguments passed in by reference (LVals).
542 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
543 I != E; ++I) {
544 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000545
Ted Kremenek03da0d72008-02-21 19:46:04 +0000546 if (isa<LVal>(V))
547 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremeneka1354a52008-03-03 16:47:31 +0000548 }
Ted Kremenek03da0d72008-02-21 19:46:04 +0000549 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000550 else {
551
552 // Check any arguments passed-by-value against being undefined.
553
554 bool badArg = false;
555
556 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
557 I != E; ++I) {
558
559 if (GetRVal((*DI)->getState(), *I).isUndef()) {
560 NodeTy* N = Builder->generateNode(CE, (*DI)->getState(), *DI);
561
562 if (N) {
563 N->markAsSink();
564 UndefArgs[N] = *I;
565 }
566
567 badArg = true;
568 break;
569 }
570 }
571
572 if (badArg)
573 continue;
574
575 // Dispatch to the plug-in transfer function.
Ted Kremenek03da0d72008-02-21 19:46:04 +0000576 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000577 }
Ted Kremenek03da0d72008-02-21 19:46:04 +0000578
Ted Kremenekaffb2152008-02-27 20:43:44 +0000579 // Check for the "noreturn" attribute.
580
581 if (isa<lval::FuncVal>(L))
582 if (cast<lval::FuncVal>(L).getDecl()->getAttr<NoReturnAttr>()) {
583
584 NodeTy* N = Builder->generateNode(CE, St, *DI);
585
586 if (N) {
587 N->markAsSink();
588 NoReturnCalls.insert(N);
589 }
590
591 continue;
592 }
593
Ted Kremenek03da0d72008-02-21 19:46:04 +0000594 Nodify(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +0000595 }
596}
597
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000598void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000599
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000600 NodeSet S1;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000601 Visit(Ex, Pred, S1);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000602
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000603 QualType T = CastE->getType();
604
Ted Kremenek402563b2008-02-19 18:47:04 +0000605 // Check for redundant casts or casting to "void"
606 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000607 Ex->getType() == T ||
608 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000609
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000610 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000611 Dst.Add(*I1);
612
Ted Kremenek874d63f2008-01-24 02:02:54 +0000613 return;
614 }
615
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000616 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000617 NodeTy* N = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000618 ValueState* St = N->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000619 RVal V = GetRVal(St, Ex);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000620 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000621 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000622}
623
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000624void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000625 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000626
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000627 ValueState* St = Pred->getState();
Ted Kremenek9de04c42008-01-24 20:55:43 +0000628
629 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000630 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000631
632 // FIXME: Add support for local arrays.
633 if (VD->getType()->isArrayType())
634 continue;
635
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000636 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000637
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000638 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
639
640 // In this context, Static => Local variable.
641
642 assert (!VD->getStorageClass() == VarDecl::Static ||
643 !isa<FileVarDecl>(VD));
644
645 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000646 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000647 //
648 // FIXME: static variables may have an initializer, but the second
649 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000650
651 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000652
Ted Kremenek16d81562008-03-04 04:18:04 +0000653 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +0000654
655 // C99: 6.7.8 Initialization
656 // If an object that has static storage duration is not initialized
657 // explicitly, then:
658 // —if it has pointer type, it is initialized to a null pointer;
659 // —if it has arithmetic type, it is initialized to (positive or
660 // unsigned) zero;
661
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000662 // FIXME: Handle structs. Now we treat their values as unknown.
663
Ted Kremenek16d81562008-03-04 04:18:04 +0000664 if (T->isPointerType()) {
665
666 St = SetRVal(St, lval::DeclVal(VD),
667 lval::ConcreteInt(ValMgr.getValue(0, T)));
668 }
669 else if (T->isIntegerType()) {
670
671 St = SetRVal(St, lval::DeclVal(VD),
672 nonlval::ConcreteInt(ValMgr.getValue(0, T)));
673 }
674
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000675
Ted Kremenek16d81562008-03-04 04:18:04 +0000676 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000677 else {
Ted Kremenek16d81562008-03-04 04:18:04 +0000678
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000679 // FIXME: Handle structs. Now we treat them as unknown. What
680 // we need to do is treat their members as unknown.
681
682 if (T->isPointerType() || T->isIntegerType())
683 St = SetRVal(St, lval::DeclVal(VD),
684 Ex ? GetRVal(St, Ex) : UndefinedVal());
685 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000686 }
Ted Kremenek403c1812008-01-28 22:51:57 +0000687 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000688
689 Nodify(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000690}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000691
Ted Kremenekf233d482008-02-05 00:26:40 +0000692
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000693void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000694 NodeTy* Pred, NodeSet& Dst) {
695
Ted Kremenek05a23782008-02-26 19:05:15 +0000696 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
697
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000698 ValueState* St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000699 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000700
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000701 assert (X.isUndef());
Ted Kremenekf233d482008-02-05 00:26:40 +0000702
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000703 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000704
705 assert (SE);
706
707 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000708
709 // Make sure that we invalidate the previous binding.
710 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000711}
712
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000713/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000714void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
715 NodeTy* Pred,
716 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000717
718 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000719
720 // 6.5.3.4 sizeof: "The result type is an integer."
721
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000722 QualType T = Ex->getArgumentType();
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000723
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000724
Ted Kremenek297d0d72008-02-21 18:15:29 +0000725 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000726 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000727 return;
728
Ted Kremenek297d0d72008-02-21 18:15:29 +0000729
730 uint64_t size = 1; // Handle sizeof(void)
731
732 if (T != getContext().VoidTy) {
733 SourceLocation Loc = Ex->getExprLoc();
734 size = getContext().getTypeSize(T, Loc) / 8;
735 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000736
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000737 Nodify(Dst, Ex, Pred,
738 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000739 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000740
741}
742
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000743void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
744
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000745 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000746
747 NodeSet DstTmp;
748
Ted Kremenek018c15f2008-02-26 03:44:25 +0000749 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000750 DstTmp.Add(Pred);
751 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000752 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000753
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000754 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000755
756 NodeTy* N = *I;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000757 ValueState* St = N->getState();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000758
759 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
760
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000761 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000762
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000763 // Check for dereferences of undefined values.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000764
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000765 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000766
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000767 NodeTy* Succ = Builder->generateNode(U, St, N);
768
769 if (Succ) {
770 Succ->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000771 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000772 }
773
774 continue;
775 }
776
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000777 // Check for dereferences of unknown values. Treat as No-Ops.
778
779 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000780 Dst.Add(N);
781 continue;
782 }
783
784 // After a dereference, one of two possible situations arise:
785 // (1) A crash, because the pointer was NULL.
786 // (2) The pointer is not NULL, and the dereference works.
787 //
788 // We add these assumptions.
789
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000790 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000791 bool isFeasibleNotNull;
792
793 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000794
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000795 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000796
797 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000798
799 // FIXME: Currently symbolic analysis "generates" new symbols
800 // for the contents of values. We need a better approach.
801
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000802 Nodify(Dst, U, N, SetRVal(StNotNull, U,
803 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000804 }
805
806 bool isFeasibleNull;
807
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000808 // Now "assume" that the pointer is NULL.
809
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000810 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000811
812 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000813
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000814 // We don't use "Nodify" here because the node will be a sink
815 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000816
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000817 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
818
819 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000820
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000821 NullNode->markAsSink();
822
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000823 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
824 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000825 }
826 }
827 }
828}
829
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000830void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
831 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000832
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000833 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000834
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000835 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000836 assert (U->getOpcode() != UnaryOperator::SizeOf);
837 assert (U->getOpcode() != UnaryOperator::AlignOf);
838
839 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000840
841 switch (U->getOpcode()) {
842 case UnaryOperator::PostInc:
843 case UnaryOperator::PostDec:
844 case UnaryOperator::PreInc:
845 case UnaryOperator::PreDec:
846 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000847 // Evalue subexpression as an LVal.
848 use_GetLVal = true;
849 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000850 break;
851
852 default:
853 Visit(U->getSubExpr(), Pred, S1);
854 break;
855 }
856
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000857 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000858
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000859 NodeTy* N1 = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000860 ValueState* St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000861
862 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
863 GetRVal(St, U->getSubExpr());
864
865 if (SubV.isUnknown()) {
866 Dst.Add(N1);
867 continue;
868 }
869
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000870 if (SubV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000871 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
872 continue;
873 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000874
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000875 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000876
877 // Handle ++ and -- (both pre- and post-increment).
878
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000879 LVal SubLV = cast<LVal>(SubV);
880 RVal V = GetRVal(St, SubLV, U->getType());
881
Ted Kremenek89063af2008-02-21 19:15:37 +0000882 if (V.isUnknown()) {
883 Dst.Add(N1);
884 continue;
885 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000886
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000887 // Propagate undefined values.
888 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000889 Nodify(Dst, U, N1, SetRVal(St, U, V));
890 continue;
891 }
892
Ted Kremenek443003b2008-02-21 19:29:23 +0000893 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000894
895 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
896 : BinaryOperator::Sub;
897
Ted Kremenek443003b2008-02-21 19:29:23 +0000898 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000899
900 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000901 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000902 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000903 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000904
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000905 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000906 continue;
907 }
908
909 // Handle all other unary operators.
910
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000911 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000912
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000913 case UnaryOperator::Minus:
914 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000915 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000916
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000917 case UnaryOperator::Not:
918 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000919 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000920
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000921 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000922
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000923 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
924 //
925 // Note: technically we do "E == 0", but this is the same in the
926 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000927
928 if (isa<LVal>(SubV)) {
929 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
930 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
931 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000932 }
933 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000934 Expr* Ex = U->getSubExpr();
935 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000936 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
937 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000938 }
939
940 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000941
Ted Kremenek64924852008-01-31 02:35:41 +0000942 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000943 assert (isa<LVal>(SubV));
944 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000945 break;
946 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000947
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000948 default: ;
949 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000950 }
951
952 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000953 }
954}
955
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000956void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
957 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000958
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000959 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000960
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000961 // FIXME: Add support for VLAs.
962 if (!T.getTypePtr()->isConstantSizeType())
963 return;
964
965 SourceLocation Loc = U->getExprLoc();
966 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000967 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000968 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
969
970 Nodify(Dst, U, Pred, St);
971}
972
973void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +0000974
975 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
976 Dst.Add(Pred);
977 return;
978 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000979
980 Ex = Ex->IgnoreParens();
981
Ted Kremenek07932632008-02-27 06:47:26 +0000982 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000983 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000984 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000985 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000986
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000987 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000988 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000989 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000990
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000991 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000992 Dst.Add(Pred);
993 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000994 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000995
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000996 return;
997 }
998 }
999
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001000 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001001}
1002
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001003void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001004 GRExprEngine::NodeTy* Pred,
1005 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001006 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001007
1008 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001009 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001010 else
1011 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001012
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001013 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001014
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001015 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001016
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001017 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001018 // In such cases, we want to (initially) treat the LHS as an LVal,
1019 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1020 // evaluated to LValDecl's instead of to an NonLVal.
1021
1022 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
1023 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001024
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001025 // Visit the RHS...
1026
1027 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001028 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001029
1030 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001031
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001032 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001033
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001034 NodeTy* N2 = *I2;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001035 ValueState* St = N2->getState();
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001036 Expr* RHS = B->getRHS();
1037 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001038
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001039 BinaryOperator::Opcode Op = B->getOpcode();
1040
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001041 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1042 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001043
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001044 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001045
Ted Kremenek00155412008-02-26 22:27:51 +00001046 if (!RightV.isUnknown()) {
1047
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001048 if (RightV.isUndef()) {
1049 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001050
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001051 if (DivUndef) {
1052 DivUndef->markAsSink();
1053 BadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001054 }
1055
1056 continue;
1057 }
1058
1059 // Check for divide/remainder-by-zero.
1060 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001061 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001062
Ted Kremenek00155412008-02-26 22:27:51 +00001063 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001064 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek00155412008-02-26 22:27:51 +00001065
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001066 if (isFeasible)
1067 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
Ted Kremenek00155412008-02-26 22:27:51 +00001068 DivZeroNode->markAsSink();
1069 BadDivides.insert(DivZeroNode);
1070 }
Ted Kremenekd763eb92008-02-26 02:15:56 +00001071
Ted Kremenek00155412008-02-26 22:27:51 +00001072 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001073
Ted Kremenek00155412008-02-26 22:27:51 +00001074 isFeasible = false;
1075 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001076
Ted Kremenek00155412008-02-26 22:27:51 +00001077 if (!isFeasible)
1078 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001079 }
1080
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001081 // Fall-through. The logic below processes the divide.
1082 }
1083
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001084
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001085 if (Op <= BinaryOperator::Or) {
1086
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001087 // Process non-assignements except commas or short-circuited
1088 // logical expressions (LAnd and LOr).
1089
1090 RVal Result = EvalBinOp(Op, LeftV, RightV);
1091
1092 if (Result.isUnknown()) {
1093 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001094 continue;
1095 }
1096
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001097 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1098
1099 // The operands were not undefined, but the result is undefined.
1100
1101 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1102 UndefNode->markAsSink();
1103 UndefResults.insert(UndefNode);
1104 }
1105
1106 continue;
1107 }
1108
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001109 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001110 continue;
1111 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001112
1113 // Process assignments.
1114
1115 switch (Op) {
1116
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001117 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001118
1119 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001120
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001121 if (LeftV.isUndef()) {
1122 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001123 continue;
1124 }
1125
1126 if (LeftV.isUnknown()) {
1127 St = SetRVal(St, B, RightV);
1128 break;
1129 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001130
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001131 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001132 break;
1133 }
1134
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001135 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001136
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001137 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001138
Ted Kremenek00155412008-02-26 22:27:51 +00001139 assert (B->isCompoundAssignmentOp());
1140
1141 if (Op >= BinaryOperator::AndAssign)
1142 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1143 else
1144 ((int&) Op) -= BinaryOperator::MulAssign;
1145
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001146 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001147
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001148 if (LeftV.isUndef()) {
1149 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001150 continue;
1151 }
1152
1153 if (LeftV.isUnknown()) {
1154
1155 // While we do not know the location to store RightV,
1156 // the entire expression does evaluate to RightV.
1157
1158 if (RightV.isUnknown()) {
1159 Dst.Add(N2);
1160 continue;
1161 }
1162
1163 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001164 break;
1165 }
1166
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001167 // At this pointer we know that the LHS evaluates to an LVal
1168 // that is neither "Unknown" or "Unintialized."
1169
1170 LVal LeftLV = cast<LVal>(LeftV);
1171
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001172 // Fetch the value of the LHS (the value of the variable, etc.).
1173
1174 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1175
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001176 // Propagate undefined value (left-side). We
1177 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001178 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001179
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001180 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001181 St = SetRVal(St, B, V);
1182 break;
1183 }
1184
1185 // Propagate unknown values.
1186
Ted Kremenek89063af2008-02-21 19:15:37 +00001187 if (V.isUnknown()) {
1188 Dst.Add(N2);
1189 continue;
1190 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001191
1192 if (RightV.isUnknown()) {
1193 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1194 break;
1195 }
1196
Ted Kremenek00155412008-02-26 22:27:51 +00001197 // At this point:
1198 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001199 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001200 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001201
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001202 // Get the computation type.
1203 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1204
1205 // Perform promotions.
1206 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001207 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001208
1209 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001210
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001211 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1212 && RHS->getType()->isIntegerType()) {
1213
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001214 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001215
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001216 if (RightV.isUndef()) {
1217 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001218
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001219 if (DivUndef) {
1220 DivUndef->markAsSink();
1221 BadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001222 }
1223
1224 continue;
1225 }
Ted Kremenek00155412008-02-26 22:27:51 +00001226
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001227 // First, "assume" that the denominator is 0.
1228
1229 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001230 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001231
1232 if (isFeasible) {
1233 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1234
1235 if (DivZeroNode) {
1236 DivZeroNode->markAsSink();
Ted Kremenekd763eb92008-02-26 02:15:56 +00001237 BadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001238 }
1239 }
1240
1241 // Second, "assume" that the denominator cannot be 0.
1242
1243 isFeasible = false;
1244 St = Assume(St, RightV, true, isFeasible);
1245
1246 if (!isFeasible)
1247 continue;
1248
1249 // Fall-through. The logic below processes the divide.
1250 }
Ted Kremenek00155412008-02-26 22:27:51 +00001251 else {
1252
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001253 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001254
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001255 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001256 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1257 break;
1258 }
1259
1260 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001261
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001262 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001263
1264 if (Result.isUndef()) {
1265
1266 // The operands were not undefined, but the result is undefined.
1267
1268 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1269 UndefNode->markAsSink();
1270 UndefResults.insert(UndefNode);
1271 }
1272
1273 continue;
1274 }
1275
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001276 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001277 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001278 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001279
1280 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001281 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001282 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001283}
Ted Kremenekee985462008-01-16 18:18:48 +00001284
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001285void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001286 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1287 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001288 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001289}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001290
Ted Kremenek9dca0622008-02-19 00:22:37 +00001291void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001292
1293 // FIXME: add metadata to the CFG so that we can disable
1294 // this check when we KNOW that there is no block-level subexpression.
1295 // The motivation is that this check requires a hashtable lookup.
1296
1297 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1298 Dst.Add(Pred);
1299 return;
1300 }
1301
1302 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001303
1304 default:
1305 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001306 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001307
1308 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1309 break;
1310
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001311 case Stmt::BinaryOperatorClass: {
1312 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001313
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001314 if (B->isLogicalOp()) {
1315 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001316 break;
1317 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001318 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001319 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001320 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001321 break;
1322 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001323
1324 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1325 break;
1326 }
Ted Kremenekde434242008-02-19 01:44:53 +00001327
1328 case Stmt::CallExprClass: {
1329 CallExpr* C = cast<CallExpr>(S);
1330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1331 break;
1332 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001333
1334 case Stmt::CastExprClass: {
1335 CastExpr* C = cast<CastExpr>(S);
1336 VisitCast(C, C->getSubExpr(), Pred, Dst);
1337 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001338 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001339
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001340 // FIXME: ChooseExpr is really a constant. We need to fix
1341 // the CFG do not model them as explicit control-flow.
1342
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001343 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1344 ChooseExpr* C = cast<ChooseExpr>(S);
1345 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1346 break;
1347 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001348
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001349 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001350 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1351 break;
1352
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001353 case Stmt::ConditionalOperatorClass: { // '?' operator
1354 ConditionalOperator* C = cast<ConditionalOperator>(S);
1355 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1356 break;
1357 }
1358
1359 case Stmt::DeclRefExprClass:
1360 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1361 break;
1362
1363 case Stmt::DeclStmtClass:
1364 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1365 break;
1366
1367 case Stmt::ImplicitCastExprClass: {
1368 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1369 VisitCast(C, C->getSubExpr(), Pred, Dst);
1370 break;
1371 }
1372
1373 case Stmt::ParenExprClass:
1374 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1375 break;
1376
1377 case Stmt::SizeOfAlignOfTypeExprClass:
1378 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1379 break;
1380
Ted Kremenekda9bd092008-02-08 07:05:39 +00001381 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001382 StmtExpr* SE = cast<StmtExpr>(S);
1383
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001384 ValueState* St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001385 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001386 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001387 break;
1388 }
1389
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001390 // FIXME: We may wish to always bind state to ReturnStmts so
1391 // that users can quickly query what was the state at the
1392 // exit points of a function.
1393
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001394 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001395 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1396 Visit(R, Pred, Dst);
1397 else
1398 Dst.Add(Pred);
1399
1400 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001401 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001402
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001403 case Stmt::UnaryOperatorClass: {
1404 UnaryOperator* U = cast<UnaryOperator>(S);
1405
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001406 switch (U->getOpcode()) {
1407 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1408 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1409 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1410 default: VisitUnaryOperator(U, Pred, Dst); break;
1411 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001412
Ted Kremenek9de04c42008-01-24 20:55:43 +00001413 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001414 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001415 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001416}
1417
Ted Kremenekee985462008-01-16 18:18:48 +00001418//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001419// "Assume" logic.
1420//===----------------------------------------------------------------------===//
1421
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001422ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001423 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001424 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001425 switch (Cond.getSubKind()) {
1426 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001427 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001428 return St;
1429
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001430 case lval::SymbolValKind:
1431 if (Assumption)
1432 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1433 ValMgr.getZeroWithPtrWidth(), isFeasible);
1434 else
1435 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1436 ValMgr.getZeroWithPtrWidth(), isFeasible);
1437
Ted Kremenek08b66252008-02-06 04:31:33 +00001438
Ted Kremenek329f8542008-02-05 21:52:21 +00001439 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001440 case lval::FuncValKind:
1441 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001442 isFeasible = Assumption;
1443 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001444
Ted Kremenek329f8542008-02-05 21:52:21 +00001445 case lval::ConcreteIntKind: {
1446 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001447 isFeasible = b ? Assumption : !Assumption;
1448 return St;
1449 }
1450 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001451}
1452
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001453ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001454 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001455 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001456 switch (Cond.getSubKind()) {
1457 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001458 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001459 return St;
1460
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001461
1462 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001463 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001464 SymbolID sym = SV.getSymbol();
1465
1466 if (Assumption)
1467 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1468 isFeasible);
1469 else
1470 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1471 isFeasible);
1472 }
1473
Ted Kremenek08b66252008-02-06 04:31:33 +00001474 case nonlval::SymIntConstraintValKind:
1475 return
1476 AssumeSymInt(St, Assumption,
1477 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1478 isFeasible);
1479
Ted Kremenek329f8542008-02-05 21:52:21 +00001480 case nonlval::ConcreteIntKind: {
1481 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001482 isFeasible = b ? Assumption : !Assumption;
1483 return St;
1484 }
1485 }
1486}
1487
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001488ValueState*
1489GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001490 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001491
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001492 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001493 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001494 isFeasible = *X != V;
1495 return St;
1496 }
1497
1498 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001499 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001500 isFeasible = true;
1501 return St;
1502 }
1503
1504 // If we reach here, sym is not a constant and we don't know if it is != V.
1505 // Make that assumption.
1506
1507 isFeasible = true;
1508 return StateMgr.AddNE(St, sym, V);
1509}
1510
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001511ValueState*
1512GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001513 const llvm::APSInt& V, bool& isFeasible) {
1514
1515 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001516 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001517 isFeasible = *X == V;
1518 return St;
1519 }
1520
1521 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001522 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001523 isFeasible = false;
1524 return St;
1525 }
1526
1527 // If we reach here, sym is not a constant and we don't know if it is == V.
1528 // Make that assumption.
1529
1530 isFeasible = true;
1531 return StateMgr.AddEQ(St, sym, V);
1532}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001533
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001534ValueState*
1535GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001536 const SymIntConstraint& C, bool& isFeasible) {
1537
1538 switch (C.getOpcode()) {
1539 default:
1540 // No logic yet for other operators.
1541 return St;
1542
1543 case BinaryOperator::EQ:
1544 if (Assumption)
1545 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1546 else
1547 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1548
1549 case BinaryOperator::NE:
1550 if (Assumption)
1551 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1552 else
1553 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1554 }
1555}
1556
Ted Kremenekb38911f2008-01-30 23:03:39 +00001557//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001558// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001559//===----------------------------------------------------------------------===//
1560
Ted Kremenekaa66a322008-01-16 21:46:15 +00001561#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001562static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001563
Ted Kremenekaa66a322008-01-16 21:46:15 +00001564namespace llvm {
1565template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001566struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001567 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001568
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001569 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001570
1571 Out << "Variables:\\l";
1572
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001573 bool isFirst = true;
1574
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001575 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001576
1577 if (isFirst)
1578 isFirst = false;
1579 else
1580 Out << "\\l";
1581
1582 Out << ' ' << I.getKey()->getName() << " : ";
1583 I.getData().print(Out);
1584 }
1585
1586 }
1587
Ted Kremeneke7d22112008-02-11 19:21:59 +00001588
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001589 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001590
1591 bool isFirst = true;
1592
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001593 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001594
1595 if (isFirst) {
1596 Out << "\\l\\lSub-Expressions:\\l";
1597 isFirst = false;
1598 }
1599 else
1600 Out << "\\l";
1601
1602 Out << " (" << (void*) I.getKey() << ") ";
1603 I.getKey()->printPretty(Out);
1604 Out << " : ";
1605 I.getData().print(Out);
1606 }
1607 }
1608
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001609 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001610
Ted Kremenek016f52f2008-02-08 21:10:02 +00001611 bool isFirst = true;
1612
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001613 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001614 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001615 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001616 isFirst = false;
1617 }
1618 else
1619 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001620
Ted Kremeneke7d22112008-02-11 19:21:59 +00001621 Out << " (" << (void*) I.getKey() << ") ";
1622 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001623 Out << " : ";
1624 I.getData().print(Out);
1625 }
1626 }
1627
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001628 static void PrintEQ(std::ostream& Out, ValueState* St) {
1629 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001630
1631 if (CE.isEmpty())
1632 return;
1633
1634 Out << "\\l\\|'==' constraints:";
1635
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001636 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001637 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1638 }
1639
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001640 static void PrintNE(std::ostream& Out, ValueState* St) {
1641 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001642
1643 if (NE.isEmpty())
1644 return;
1645
1646 Out << "\\l\\|'!=' constraints:";
1647
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001648 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001649 I != EI; ++I){
1650
1651 Out << "\\l $" << I.getKey() << " : ";
1652 bool isFirst = true;
1653
1654 ValueState::IntSetTy::iterator J=I.getData().begin(),
1655 EJ=I.getData().end();
1656 for ( ; J != EJ; ++J) {
1657 if (isFirst) isFirst = false;
1658 else Out << ", ";
1659
1660 Out << (*J)->toString();
1661 }
1662 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001663 }
1664
1665 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1666
1667 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001668 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001669 GraphPrintCheckerState->isUndefDeref(N) ||
1670 GraphPrintCheckerState->isUndefStore(N) ||
1671 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001672 GraphPrintCheckerState->isBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001673 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001674 GraphPrintCheckerState->isBadCall(N) ||
1675 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001676 return "color=\"red\",style=\"filled\"";
1677
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001678 if (GraphPrintCheckerState->isNoReturnCall(N))
1679 return "color=\"blue\",style=\"filled\"";
1680
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001681 return "";
1682 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001683
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001684 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001685 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001686
1687 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001688 ProgramPoint Loc = N->getLocation();
1689
1690 switch (Loc.getKind()) {
1691 case ProgramPoint::BlockEntranceKind:
1692 Out << "Block Entrance: B"
1693 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1694 break;
1695
1696 case ProgramPoint::BlockExitKind:
1697 assert (false);
1698 break;
1699
1700 case ProgramPoint::PostStmtKind: {
1701 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001702 Out << L.getStmt()->getStmtClassName() << ':'
1703 << (void*) L.getStmt() << ' ';
1704
Ted Kremenekaa66a322008-01-16 21:46:15 +00001705 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001706
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001707 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001708 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001709 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00001710 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001711 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001712 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001713 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001714 Out << "\\|Store to Undefined LVal.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001715 else if (GraphPrintCheckerState->isBadDivide(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001716 Out << "\\|Divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001717 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001718 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001719 else if (GraphPrintCheckerState->isNoReturnCall(N))
1720 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001721 else if (GraphPrintCheckerState->isBadCall(N))
1722 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001723 else if (GraphPrintCheckerState->isUndefArg(N))
1724 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001725
Ted Kremenekaa66a322008-01-16 21:46:15 +00001726 break;
1727 }
1728
1729 default: {
1730 const BlockEdge& E = cast<BlockEdge>(Loc);
1731 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1732 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001733
1734 if (Stmt* T = E.getSrc()->getTerminator()) {
1735 Out << "\\|Terminator: ";
1736 E.getSrc()->printTerminator(Out);
1737
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001738 if (isa<SwitchStmt>(T)) {
1739 Stmt* Label = E.getDst()->getLabel();
1740
1741 if (Label) {
1742 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1743 Out << "\\lcase ";
1744 C->getLHS()->printPretty(Out);
1745
1746 if (Stmt* RHS = C->getRHS()) {
1747 Out << " .. ";
1748 RHS->printPretty(Out);
1749 }
1750
1751 Out << ":";
1752 }
1753 else {
1754 assert (isa<DefaultStmt>(Label));
1755 Out << "\\ldefault:";
1756 }
1757 }
1758 else
1759 Out << "\\l(implicit) default:";
1760 }
1761 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001762 // FIXME
1763 }
1764 else {
1765 Out << "\\lCondition: ";
1766 if (*E.getSrc()->succ_begin() == E.getDst())
1767 Out << "true";
1768 else
1769 Out << "false";
1770 }
1771
1772 Out << "\\l";
1773 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001774
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001775 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1776 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001777 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001778 }
1779 }
1780
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001781 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001782
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001783 N->getState()->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001784
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001785 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001786 return Out.str();
1787 }
1788};
1789} // end llvm namespace
1790#endif
1791
Ted Kremeneke01c9872008-02-14 22:36:46 +00001792void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001793#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001794 GraphPrintCheckerState = this;
1795 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001796 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001797#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001798}