blob: 7a0ca710543af41fba08ca89cdb2dc60615151e9 [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;
484 Visit(*AI, Pred, DstTmp);
Ted Kremeneka6fbe802008-02-27 00:44:11 +0000485
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000486 Expr* CurrentArg = *AI;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000487 ++AI;
488
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000489 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) {
490 if (GetRVal((*DI)->getState(), CurrentArg).isUndef()) {
491
492 NodeTy* N = Builder->generateNode(CE, (*DI)->getState(), *DI);
493
494 if (N) {
495 N->markAsSink();
Ted Kremenek1e80aa42008-03-04 00:42:54 +0000496 UndefArgs[N] = CurrentArg;
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000497 }
498
499 continue;
500 }
501
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000502 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000503 }
Ted Kremenekde434242008-02-19 01:44:53 +0000504
505 return;
506 }
507
508 // If we reach here we have processed all of the arguments. Evaluate
509 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000510
Ted Kremenek994a09b2008-02-25 21:16:03 +0000511 NodeSet DstTmp;
512 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000513
Ted Kremenek994a09b2008-02-25 21:16:03 +0000514 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000515
516 if (DstTmp.empty())
517 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000518
519 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000520 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
521
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000522 ValueState* St = (*DI)->getState();
Ted Kremenek994a09b2008-02-25 21:16:03 +0000523 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000524
Ted Kremeneka1354a52008-03-03 16:47:31 +0000525 // FIXME: Add support for symbolic function calls (calls involving
526 // function pointer values that are symbolic).
527
528 // Check for undefined control-flow or calls to NULL.
529
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000530 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000531 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000532 if (N) {
533 N->markAsSink();
534 BadCalls.insert(N);
535 }
Ted Kremenekde434242008-02-19 01:44:53 +0000536 continue;
Ted Kremeneka1354a52008-03-03 16:47:31 +0000537 }
538
539 // Check for an "unknown" callee.
Ted Kremenekde434242008-02-19 01:44:53 +0000540
Ted Kremenek03da0d72008-02-21 19:46:04 +0000541 if (L.isUnknown()) {
542 // Invalidate all arguments passed in by reference (LVals).
543 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
544 I != E; ++I) {
545 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000546
Ted Kremenek03da0d72008-02-21 19:46:04 +0000547 if (isa<LVal>(V))
548 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremeneka1354a52008-03-03 16:47:31 +0000549 }
Ted Kremenek03da0d72008-02-21 19:46:04 +0000550 }
551 else
552 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
553
Ted Kremenekaffb2152008-02-27 20:43:44 +0000554 // Check for the "noreturn" attribute.
555
556 if (isa<lval::FuncVal>(L))
557 if (cast<lval::FuncVal>(L).getDecl()->getAttr<NoReturnAttr>()) {
558
559 NodeTy* N = Builder->generateNode(CE, St, *DI);
560
561 if (N) {
562 N->markAsSink();
563 NoReturnCalls.insert(N);
564 }
565
566 continue;
567 }
568
Ted Kremenek03da0d72008-02-21 19:46:04 +0000569 Nodify(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +0000570 }
571}
572
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000573void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000574
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000575 NodeSet S1;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000576 Visit(Ex, Pred, S1);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000577
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000578 QualType T = CastE->getType();
579
Ted Kremenek402563b2008-02-19 18:47:04 +0000580 // Check for redundant casts or casting to "void"
581 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000582 Ex->getType() == T ||
583 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000584
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000585 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000586 Dst.Add(*I1);
587
Ted Kremenek874d63f2008-01-24 02:02:54 +0000588 return;
589 }
590
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000591 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000592 NodeTy* N = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000593 ValueState* St = N->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000594 RVal V = GetRVal(St, Ex);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000595 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000596 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000597}
598
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000599void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000600 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000601
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000602 ValueState* St = Pred->getState();
Ted Kremenek9de04c42008-01-24 20:55:43 +0000603
604 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000605 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000606
607 // FIXME: Add support for local arrays.
608 if (VD->getType()->isArrayType())
609 continue;
610
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000611 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000612
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000613 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
614
615 // In this context, Static => Local variable.
616
617 assert (!VD->getStorageClass() == VarDecl::Static ||
618 !isa<FileVarDecl>(VD));
619
620 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000621 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000622 //
623 // FIXME: static variables may have an initializer, but the second
624 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000625
626 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000627
Ted Kremenek16d81562008-03-04 04:18:04 +0000628 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +0000629
630 // C99: 6.7.8 Initialization
631 // If an object that has static storage duration is not initialized
632 // explicitly, then:
633 // —if it has pointer type, it is initialized to a null pointer;
634 // —if it has arithmetic type, it is initialized to (positive or
635 // unsigned) zero;
636
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000637 // FIXME: Handle structs. Now we treat their values as unknown.
638
Ted Kremenek16d81562008-03-04 04:18:04 +0000639 if (T->isPointerType()) {
640
641 St = SetRVal(St, lval::DeclVal(VD),
642 lval::ConcreteInt(ValMgr.getValue(0, T)));
643 }
644 else if (T->isIntegerType()) {
645
646 St = SetRVal(St, lval::DeclVal(VD),
647 nonlval::ConcreteInt(ValMgr.getValue(0, T)));
648 }
649
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000650
Ted Kremenek16d81562008-03-04 04:18:04 +0000651 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000652 else {
Ted Kremenek16d81562008-03-04 04:18:04 +0000653
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000654 // FIXME: Handle structs. Now we treat them as unknown. What
655 // we need to do is treat their members as unknown.
656
657 if (T->isPointerType() || T->isIntegerType())
658 St = SetRVal(St, lval::DeclVal(VD),
659 Ex ? GetRVal(St, Ex) : UndefinedVal());
660 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000661 }
Ted Kremenek403c1812008-01-28 22:51:57 +0000662 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000663
664 Nodify(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000665}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000666
Ted Kremenekf233d482008-02-05 00:26:40 +0000667
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000668void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000669 NodeTy* Pred, NodeSet& Dst) {
670
Ted Kremenek05a23782008-02-26 19:05:15 +0000671 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
672
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000673 ValueState* St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000674 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000675
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000676 assert (X.isUndef());
Ted Kremenekf233d482008-02-05 00:26:40 +0000677
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000678 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000679
680 assert (SE);
681
682 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000683
684 // Make sure that we invalidate the previous binding.
685 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000686}
687
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000688/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000689void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
690 NodeTy* Pred,
691 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000692
693 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000694
695 // 6.5.3.4 sizeof: "The result type is an integer."
696
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000697 QualType T = Ex->getArgumentType();
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000698
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000699
Ted Kremenek297d0d72008-02-21 18:15:29 +0000700 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000701 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000702 return;
703
Ted Kremenek297d0d72008-02-21 18:15:29 +0000704
705 uint64_t size = 1; // Handle sizeof(void)
706
707 if (T != getContext().VoidTy) {
708 SourceLocation Loc = Ex->getExprLoc();
709 size = getContext().getTypeSize(T, Loc) / 8;
710 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000711
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000712 Nodify(Dst, Ex, Pred,
713 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000714 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000715
716}
717
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000718void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
719
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000720 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000721
722 NodeSet DstTmp;
723
Ted Kremenek018c15f2008-02-26 03:44:25 +0000724 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000725 DstTmp.Add(Pred);
726 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000727 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000728
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000729 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000730
731 NodeTy* N = *I;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000732 ValueState* St = N->getState();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000733
734 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
735
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000736 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000737
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000738 // Check for dereferences of undefined values.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000739
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000740 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000741
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000742 NodeTy* Succ = Builder->generateNode(U, St, N);
743
744 if (Succ) {
745 Succ->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000746 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000747 }
748
749 continue;
750 }
751
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000752 // Check for dereferences of unknown values. Treat as No-Ops.
753
754 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000755 Dst.Add(N);
756 continue;
757 }
758
759 // After a dereference, one of two possible situations arise:
760 // (1) A crash, because the pointer was NULL.
761 // (2) The pointer is not NULL, and the dereference works.
762 //
763 // We add these assumptions.
764
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000765 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000766 bool isFeasibleNotNull;
767
768 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000769
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000770 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000771
772 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000773
774 // FIXME: Currently symbolic analysis "generates" new symbols
775 // for the contents of values. We need a better approach.
776
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000777 Nodify(Dst, U, N, SetRVal(StNotNull, U,
778 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000779 }
780
781 bool isFeasibleNull;
782
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000783 // Now "assume" that the pointer is NULL.
784
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000785 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000786
787 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000788
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000789 // We don't use "Nodify" here because the node will be a sink
790 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000791
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000792 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
793
794 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000795
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000796 NullNode->markAsSink();
797
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000798 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
799 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000800 }
801 }
802 }
803}
804
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000805void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
806 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000807
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000808 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000809
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000810 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000811 assert (U->getOpcode() != UnaryOperator::SizeOf);
812 assert (U->getOpcode() != UnaryOperator::AlignOf);
813
814 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000815
816 switch (U->getOpcode()) {
817 case UnaryOperator::PostInc:
818 case UnaryOperator::PostDec:
819 case UnaryOperator::PreInc:
820 case UnaryOperator::PreDec:
821 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000822 // Evalue subexpression as an LVal.
823 use_GetLVal = true;
824 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000825 break;
826
827 default:
828 Visit(U->getSubExpr(), Pred, S1);
829 break;
830 }
831
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000832 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000833
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000834 NodeTy* N1 = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000835 ValueState* St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000836
837 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
838 GetRVal(St, U->getSubExpr());
839
840 if (SubV.isUnknown()) {
841 Dst.Add(N1);
842 continue;
843 }
844
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000845 if (SubV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000846 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
847 continue;
848 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000849
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000850 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000851
852 // Handle ++ and -- (both pre- and post-increment).
853
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000854 LVal SubLV = cast<LVal>(SubV);
855 RVal V = GetRVal(St, SubLV, U->getType());
856
Ted Kremenek89063af2008-02-21 19:15:37 +0000857 if (V.isUnknown()) {
858 Dst.Add(N1);
859 continue;
860 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000861
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000862 // Propagate undefined values.
863 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000864 Nodify(Dst, U, N1, SetRVal(St, U, V));
865 continue;
866 }
867
Ted Kremenek443003b2008-02-21 19:29:23 +0000868 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000869
870 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
871 : BinaryOperator::Sub;
872
Ted Kremenek443003b2008-02-21 19:29:23 +0000873 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000874
875 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000876 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000877 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000878 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000879
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000880 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000881 continue;
882 }
883
884 // Handle all other unary operators.
885
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000886 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000887
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000888 case UnaryOperator::Minus:
889 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000890 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000891
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000892 case UnaryOperator::Not:
893 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000894 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000895
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000896 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000897
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000898 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
899 //
900 // Note: technically we do "E == 0", but this is the same in the
901 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000902
903 if (isa<LVal>(SubV)) {
904 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
905 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
906 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000907 }
908 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000909 Expr* Ex = U->getSubExpr();
910 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000911 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
912 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000913 }
914
915 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000916
Ted Kremenek64924852008-01-31 02:35:41 +0000917 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000918 assert (isa<LVal>(SubV));
919 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000920 break;
921 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000922
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000923 default: ;
924 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000925 }
926
927 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000928 }
929}
930
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000931void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
932 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000933
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000934 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000935
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000936 // FIXME: Add support for VLAs.
937 if (!T.getTypePtr()->isConstantSizeType())
938 return;
939
940 SourceLocation Loc = U->getExprLoc();
941 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000942 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000943 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
944
945 Nodify(Dst, U, Pred, St);
946}
947
948void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +0000949
950 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
951 Dst.Add(Pred);
952 return;
953 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000954
955 Ex = Ex->IgnoreParens();
956
Ted Kremenek07932632008-02-27 06:47:26 +0000957 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000958 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000959 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000960 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000961
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000962 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000963 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000964 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000965
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000966 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000967 Dst.Add(Pred);
968 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000969 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000970
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000971 return;
972 }
973 }
974
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000975 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000976}
977
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000978void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000979 GRExprEngine::NodeTy* Pred,
980 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000981 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000982
983 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000984 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000985 else
986 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000987
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000988 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000989
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000990 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000991
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000992 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000993 // In such cases, we want to (initially) treat the LHS as an LVal,
994 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
995 // evaluated to LValDecl's instead of to an NonLVal.
996
997 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
998 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000999
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001000 // Visit the RHS...
1001
1002 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001003 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001004
1005 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001006
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001007 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001008
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001009 NodeTy* N2 = *I2;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001010 ValueState* St = N2->getState();
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001011 Expr* RHS = B->getRHS();
1012 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001013
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001014 BinaryOperator::Opcode Op = B->getOpcode();
1015
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001016 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1017 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001018
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001019 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001020
Ted Kremenek00155412008-02-26 22:27:51 +00001021 if (!RightV.isUnknown()) {
1022
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001023 if (RightV.isUndef()) {
1024 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001025
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001026 if (DivUndef) {
1027 DivUndef->markAsSink();
1028 BadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001029 }
1030
1031 continue;
1032 }
1033
1034 // Check for divide/remainder-by-zero.
1035 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001036 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001037
Ted Kremenek00155412008-02-26 22:27:51 +00001038 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001039 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek00155412008-02-26 22:27:51 +00001040
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001041 if (isFeasible)
1042 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
Ted Kremenek00155412008-02-26 22:27:51 +00001043 DivZeroNode->markAsSink();
1044 BadDivides.insert(DivZeroNode);
1045 }
Ted Kremenekd763eb92008-02-26 02:15:56 +00001046
Ted Kremenek00155412008-02-26 22:27:51 +00001047 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001048
Ted Kremenek00155412008-02-26 22:27:51 +00001049 isFeasible = false;
1050 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001051
Ted Kremenek00155412008-02-26 22:27:51 +00001052 if (!isFeasible)
1053 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001054 }
1055
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001056 // Fall-through. The logic below processes the divide.
1057 }
1058
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001059
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001060 if (Op <= BinaryOperator::Or) {
1061
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001062 // Process non-assignements except commas or short-circuited
1063 // logical expressions (LAnd and LOr).
1064
1065 RVal Result = EvalBinOp(Op, LeftV, RightV);
1066
1067 if (Result.isUnknown()) {
1068 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001069 continue;
1070 }
1071
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001072 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1073
1074 // The operands were not undefined, but the result is undefined.
1075
1076 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1077 UndefNode->markAsSink();
1078 UndefResults.insert(UndefNode);
1079 }
1080
1081 continue;
1082 }
1083
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001084 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001085 continue;
1086 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001087
1088 // Process assignments.
1089
1090 switch (Op) {
1091
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001092 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001093
1094 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001095
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001096 if (LeftV.isUndef()) {
1097 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001098 continue;
1099 }
1100
1101 if (LeftV.isUnknown()) {
1102 St = SetRVal(St, B, RightV);
1103 break;
1104 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001105
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001106 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001107 break;
1108 }
1109
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001110 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001111
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001112 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001113
Ted Kremenek00155412008-02-26 22:27:51 +00001114 assert (B->isCompoundAssignmentOp());
1115
1116 if (Op >= BinaryOperator::AndAssign)
1117 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1118 else
1119 ((int&) Op) -= BinaryOperator::MulAssign;
1120
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001121 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001122
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001123 if (LeftV.isUndef()) {
1124 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001125 continue;
1126 }
1127
1128 if (LeftV.isUnknown()) {
1129
1130 // While we do not know the location to store RightV,
1131 // the entire expression does evaluate to RightV.
1132
1133 if (RightV.isUnknown()) {
1134 Dst.Add(N2);
1135 continue;
1136 }
1137
1138 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001139 break;
1140 }
1141
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001142 // At this pointer we know that the LHS evaluates to an LVal
1143 // that is neither "Unknown" or "Unintialized."
1144
1145 LVal LeftLV = cast<LVal>(LeftV);
1146
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001147 // Fetch the value of the LHS (the value of the variable, etc.).
1148
1149 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1150
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001151 // Propagate undefined value (left-side). We
1152 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001153 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001154
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001155 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001156 St = SetRVal(St, B, V);
1157 break;
1158 }
1159
1160 // Propagate unknown values.
1161
Ted Kremenek89063af2008-02-21 19:15:37 +00001162 if (V.isUnknown()) {
1163 Dst.Add(N2);
1164 continue;
1165 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001166
1167 if (RightV.isUnknown()) {
1168 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1169 break;
1170 }
1171
Ted Kremenek00155412008-02-26 22:27:51 +00001172 // At this point:
1173 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001174 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001175 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001176
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001177 // Get the computation type.
1178 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1179
1180 // Perform promotions.
1181 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001182 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001183
1184 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001185
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001186 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1187 && RHS->getType()->isIntegerType()) {
1188
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001189 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001190
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001191 if (RightV.isUndef()) {
1192 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001193
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001194 if (DivUndef) {
1195 DivUndef->markAsSink();
1196 BadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001197 }
1198
1199 continue;
1200 }
Ted Kremenek00155412008-02-26 22:27:51 +00001201
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001202 // First, "assume" that the denominator is 0.
1203
1204 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001205 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001206
1207 if (isFeasible) {
1208 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1209
1210 if (DivZeroNode) {
1211 DivZeroNode->markAsSink();
Ted Kremenekd763eb92008-02-26 02:15:56 +00001212 BadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001213 }
1214 }
1215
1216 // Second, "assume" that the denominator cannot be 0.
1217
1218 isFeasible = false;
1219 St = Assume(St, RightV, true, isFeasible);
1220
1221 if (!isFeasible)
1222 continue;
1223
1224 // Fall-through. The logic below processes the divide.
1225 }
Ted Kremenek00155412008-02-26 22:27:51 +00001226 else {
1227
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001228 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001229
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001230 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001231 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1232 break;
1233 }
1234
1235 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001236
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001237 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001238
1239 if (Result.isUndef()) {
1240
1241 // The operands were not undefined, but the result is undefined.
1242
1243 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1244 UndefNode->markAsSink();
1245 UndefResults.insert(UndefNode);
1246 }
1247
1248 continue;
1249 }
1250
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001251 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001252 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001253 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001254
1255 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001256 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001257 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001258}
Ted Kremenekee985462008-01-16 18:18:48 +00001259
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001260void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001261 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1262 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001263 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001264}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001265
Ted Kremenek9dca0622008-02-19 00:22:37 +00001266void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001267
1268 // FIXME: add metadata to the CFG so that we can disable
1269 // this check when we KNOW that there is no block-level subexpression.
1270 // The motivation is that this check requires a hashtable lookup.
1271
1272 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1273 Dst.Add(Pred);
1274 return;
1275 }
1276
1277 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001278
1279 default:
1280 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001281 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001282
1283 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1284 break;
1285
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001286 case Stmt::BinaryOperatorClass: {
1287 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001288
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001289 if (B->isLogicalOp()) {
1290 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001291 break;
1292 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001293 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001294 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001295 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001296 break;
1297 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001298
1299 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1300 break;
1301 }
Ted Kremenekde434242008-02-19 01:44:53 +00001302
1303 case Stmt::CallExprClass: {
1304 CallExpr* C = cast<CallExpr>(S);
1305 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1306 break;
1307 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001308
1309 case Stmt::CastExprClass: {
1310 CastExpr* C = cast<CastExpr>(S);
1311 VisitCast(C, C->getSubExpr(), Pred, Dst);
1312 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001313 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001314
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001315 // FIXME: ChooseExpr is really a constant. We need to fix
1316 // the CFG do not model them as explicit control-flow.
1317
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001318 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1319 ChooseExpr* C = cast<ChooseExpr>(S);
1320 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1321 break;
1322 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001323
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001324 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001325 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1326 break;
1327
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001328 case Stmt::ConditionalOperatorClass: { // '?' operator
1329 ConditionalOperator* C = cast<ConditionalOperator>(S);
1330 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1331 break;
1332 }
1333
1334 case Stmt::DeclRefExprClass:
1335 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1336 break;
1337
1338 case Stmt::DeclStmtClass:
1339 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1340 break;
1341
1342 case Stmt::ImplicitCastExprClass: {
1343 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1344 VisitCast(C, C->getSubExpr(), Pred, Dst);
1345 break;
1346 }
1347
1348 case Stmt::ParenExprClass:
1349 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1350 break;
1351
1352 case Stmt::SizeOfAlignOfTypeExprClass:
1353 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1354 break;
1355
Ted Kremenekda9bd092008-02-08 07:05:39 +00001356 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001357 StmtExpr* SE = cast<StmtExpr>(S);
1358
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001359 ValueState* St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001360 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001361 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001362 break;
1363 }
1364
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001365 // FIXME: We may wish to always bind state to ReturnStmts so
1366 // that users can quickly query what was the state at the
1367 // exit points of a function.
1368
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001369 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001370 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1371 Visit(R, Pred, Dst);
1372 else
1373 Dst.Add(Pred);
1374
1375 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001376 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001377
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001378 case Stmt::UnaryOperatorClass: {
1379 UnaryOperator* U = cast<UnaryOperator>(S);
1380
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001381 switch (U->getOpcode()) {
1382 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1383 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1384 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1385 default: VisitUnaryOperator(U, Pred, Dst); break;
1386 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001387
Ted Kremenek9de04c42008-01-24 20:55:43 +00001388 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001389 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001390 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001391}
1392
Ted Kremenekee985462008-01-16 18:18:48 +00001393//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001394// "Assume" logic.
1395//===----------------------------------------------------------------------===//
1396
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001397ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001398 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001399 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001400 switch (Cond.getSubKind()) {
1401 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001402 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001403 return St;
1404
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001405 case lval::SymbolValKind:
1406 if (Assumption)
1407 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1408 ValMgr.getZeroWithPtrWidth(), isFeasible);
1409 else
1410 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1411 ValMgr.getZeroWithPtrWidth(), isFeasible);
1412
Ted Kremenek08b66252008-02-06 04:31:33 +00001413
Ted Kremenek329f8542008-02-05 21:52:21 +00001414 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001415 case lval::FuncValKind:
1416 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001417 isFeasible = Assumption;
1418 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001419
Ted Kremenek329f8542008-02-05 21:52:21 +00001420 case lval::ConcreteIntKind: {
1421 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001422 isFeasible = b ? Assumption : !Assumption;
1423 return St;
1424 }
1425 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001426}
1427
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001428ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001429 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001430 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001431 switch (Cond.getSubKind()) {
1432 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001433 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001434 return St;
1435
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001436
1437 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001438 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001439 SymbolID sym = SV.getSymbol();
1440
1441 if (Assumption)
1442 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1443 isFeasible);
1444 else
1445 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1446 isFeasible);
1447 }
1448
Ted Kremenek08b66252008-02-06 04:31:33 +00001449 case nonlval::SymIntConstraintValKind:
1450 return
1451 AssumeSymInt(St, Assumption,
1452 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1453 isFeasible);
1454
Ted Kremenek329f8542008-02-05 21:52:21 +00001455 case nonlval::ConcreteIntKind: {
1456 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001457 isFeasible = b ? Assumption : !Assumption;
1458 return St;
1459 }
1460 }
1461}
1462
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001463ValueState*
1464GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001465 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001466
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001467 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001468 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001469 isFeasible = *X != V;
1470 return St;
1471 }
1472
1473 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001474 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001475 isFeasible = true;
1476 return St;
1477 }
1478
1479 // If we reach here, sym is not a constant and we don't know if it is != V.
1480 // Make that assumption.
1481
1482 isFeasible = true;
1483 return StateMgr.AddNE(St, sym, V);
1484}
1485
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001486ValueState*
1487GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001488 const llvm::APSInt& V, bool& isFeasible) {
1489
1490 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001491 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001492 isFeasible = *X == V;
1493 return St;
1494 }
1495
1496 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001497 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001498 isFeasible = false;
1499 return St;
1500 }
1501
1502 // If we reach here, sym is not a constant and we don't know if it is == V.
1503 // Make that assumption.
1504
1505 isFeasible = true;
1506 return StateMgr.AddEQ(St, sym, V);
1507}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001508
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001509ValueState*
1510GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001511 const SymIntConstraint& C, bool& isFeasible) {
1512
1513 switch (C.getOpcode()) {
1514 default:
1515 // No logic yet for other operators.
1516 return St;
1517
1518 case BinaryOperator::EQ:
1519 if (Assumption)
1520 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1521 else
1522 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1523
1524 case BinaryOperator::NE:
1525 if (Assumption)
1526 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1527 else
1528 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1529 }
1530}
1531
Ted Kremenekb38911f2008-01-30 23:03:39 +00001532//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001533// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001534//===----------------------------------------------------------------------===//
1535
Ted Kremenekaa66a322008-01-16 21:46:15 +00001536#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001537static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001538
Ted Kremenekaa66a322008-01-16 21:46:15 +00001539namespace llvm {
1540template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001541struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001542 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001543
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001544 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001545
1546 Out << "Variables:\\l";
1547
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001548 bool isFirst = true;
1549
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001550 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001551
1552 if (isFirst)
1553 isFirst = false;
1554 else
1555 Out << "\\l";
1556
1557 Out << ' ' << I.getKey()->getName() << " : ";
1558 I.getData().print(Out);
1559 }
1560
1561 }
1562
Ted Kremeneke7d22112008-02-11 19:21:59 +00001563
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001564 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001565
1566 bool isFirst = true;
1567
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001568 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001569
1570 if (isFirst) {
1571 Out << "\\l\\lSub-Expressions:\\l";
1572 isFirst = false;
1573 }
1574 else
1575 Out << "\\l";
1576
1577 Out << " (" << (void*) I.getKey() << ") ";
1578 I.getKey()->printPretty(Out);
1579 Out << " : ";
1580 I.getData().print(Out);
1581 }
1582 }
1583
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001584 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001585
Ted Kremenek016f52f2008-02-08 21:10:02 +00001586 bool isFirst = true;
1587
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001588 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001589 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001590 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001591 isFirst = false;
1592 }
1593 else
1594 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001595
Ted Kremeneke7d22112008-02-11 19:21:59 +00001596 Out << " (" << (void*) I.getKey() << ") ";
1597 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001598 Out << " : ";
1599 I.getData().print(Out);
1600 }
1601 }
1602
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001603 static void PrintEQ(std::ostream& Out, ValueState* St) {
1604 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001605
1606 if (CE.isEmpty())
1607 return;
1608
1609 Out << "\\l\\|'==' constraints:";
1610
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001611 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001612 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1613 }
1614
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001615 static void PrintNE(std::ostream& Out, ValueState* St) {
1616 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001617
1618 if (NE.isEmpty())
1619 return;
1620
1621 Out << "\\l\\|'!=' constraints:";
1622
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001623 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001624 I != EI; ++I){
1625
1626 Out << "\\l $" << I.getKey() << " : ";
1627 bool isFirst = true;
1628
1629 ValueState::IntSetTy::iterator J=I.getData().begin(),
1630 EJ=I.getData().end();
1631 for ( ; J != EJ; ++J) {
1632 if (isFirst) isFirst = false;
1633 else Out << ", ";
1634
1635 Out << (*J)->toString();
1636 }
1637 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001638 }
1639
1640 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1641
1642 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001643 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001644 GraphPrintCheckerState->isUndefDeref(N) ||
1645 GraphPrintCheckerState->isUndefStore(N) ||
1646 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001647 GraphPrintCheckerState->isBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001648 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001649 GraphPrintCheckerState->isBadCall(N) ||
1650 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001651 return "color=\"red\",style=\"filled\"";
1652
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001653 if (GraphPrintCheckerState->isNoReturnCall(N))
1654 return "color=\"blue\",style=\"filled\"";
1655
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001656 return "";
1657 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001658
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001659 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001660 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001661
1662 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001663 ProgramPoint Loc = N->getLocation();
1664
1665 switch (Loc.getKind()) {
1666 case ProgramPoint::BlockEntranceKind:
1667 Out << "Block Entrance: B"
1668 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1669 break;
1670
1671 case ProgramPoint::BlockExitKind:
1672 assert (false);
1673 break;
1674
1675 case ProgramPoint::PostStmtKind: {
1676 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001677 Out << L.getStmt()->getStmtClassName() << ':'
1678 << (void*) L.getStmt() << ' ';
1679
Ted Kremenekaa66a322008-01-16 21:46:15 +00001680 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001681
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001682 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001683 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001684 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00001685 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001686 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001687 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001688 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001689 Out << "\\|Store to Undefined LVal.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001690 else if (GraphPrintCheckerState->isBadDivide(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001691 Out << "\\|Divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001692 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001693 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001694 else if (GraphPrintCheckerState->isNoReturnCall(N))
1695 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001696 else if (GraphPrintCheckerState->isBadCall(N))
1697 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001698 else if (GraphPrintCheckerState->isUndefArg(N))
1699 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001700
Ted Kremenekaa66a322008-01-16 21:46:15 +00001701 break;
1702 }
1703
1704 default: {
1705 const BlockEdge& E = cast<BlockEdge>(Loc);
1706 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1707 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001708
1709 if (Stmt* T = E.getSrc()->getTerminator()) {
1710 Out << "\\|Terminator: ";
1711 E.getSrc()->printTerminator(Out);
1712
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001713 if (isa<SwitchStmt>(T)) {
1714 Stmt* Label = E.getDst()->getLabel();
1715
1716 if (Label) {
1717 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1718 Out << "\\lcase ";
1719 C->getLHS()->printPretty(Out);
1720
1721 if (Stmt* RHS = C->getRHS()) {
1722 Out << " .. ";
1723 RHS->printPretty(Out);
1724 }
1725
1726 Out << ":";
1727 }
1728 else {
1729 assert (isa<DefaultStmt>(Label));
1730 Out << "\\ldefault:";
1731 }
1732 }
1733 else
1734 Out << "\\l(implicit) default:";
1735 }
1736 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001737 // FIXME
1738 }
1739 else {
1740 Out << "\\lCondition: ";
1741 if (*E.getSrc()->succ_begin() == E.getDst())
1742 Out << "true";
1743 else
1744 Out << "false";
1745 }
1746
1747 Out << "\\l";
1748 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001749
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001750 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1751 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001752 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001753 }
1754 }
1755
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001756 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001757
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001758 N->getState()->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001759
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001760 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001761 return Out.str();
1762 }
1763};
1764} // end llvm namespace
1765#endif
1766
Ted Kremeneke01c9872008-02-14 22:36:46 +00001767void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001768#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001769 GraphPrintCheckerState = this;
1770 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001771 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001772#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001773}