blob: a011e7b36024cf541fc0a734fcc6085416cf8c44 [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-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 Kremenek50df4f42008-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 Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000017#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000018
Ted Kremenek9f6b1612008-02-27 06:07:00 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#include <sstream>
22#endif
23
Ted Kremenekd4467432008-02-14 22:16:04 +000024using namespace clang;
25using llvm::dyn_cast;
26using llvm::cast;
27using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000028
Ted Kremenekf4b49df2008-02-28 10:21:43 +000029ValueState* GRExprEngine::getInitialState() {
Ted Kremenekb5175bf2008-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 Kremenekf4b49df2008-02-28 10:21:43 +000038 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenekb5175bf2008-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 Kremenekf4b49df2008-02-28 10:21:43 +000053ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000054
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000055 if (!StateCleaned) {
56 St = RemoveDeadBindings(CurrentStmt, St);
57 StateCleaned = true;
58 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000059
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000061
Ted Kremenek07baa252008-02-21 18:02:17 +000062 if (Ex == CurrentStmt) {
63 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000064
65 if (!isBlkExpr)
66 return St;
67 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000068
Ted Kremenekad5d5c52008-02-26 23:37:01 +000069 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000070}
71
Ted Kremenekf4b49df2008-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 Kremenekf20c2012008-02-05 19:35:18 +000076
77 assert (RetBuf.empty());
78
Ted Kremenek07baa252008-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 Kremenekf20c2012008-02-05 19:35:18 +000081
82 return RetBuf;
83}
Ted Kremenekf4b49df2008-02-28 10:21:43 +000084#endif
Ted Kremenekf20c2012008-02-05 19:35:18 +000085
Ted Kremenekf4b49df2008-02-28 10:21:43 +000086ValueState* GRExprEngine::SetRVal(ValueState* St, LVal LV, RVal RV) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000087
88 if (!StateCleaned) {
89 St = RemoveDeadBindings(CurrentStmt, St);
90 StateCleaned = true;
91 }
92
Ted Kremenek07baa252008-02-21 18:02:17 +000093 return StateMgr.SetRVal(St, LV, RV);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000094}
95
Ted Kremenekf4b49df2008-02-28 10:21:43 +000096ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
97 bool branchTaken) {
Ted Kremenek99ecce72008-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 Kremenekb31af242008-02-28 09:25:22 +0000120 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-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 Kremenekb31af242008-02-28 09:25:22 +0000137 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-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 Kremenekb31af242008-02-28 09:25:22 +0000145 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000146 }
147 }
148}
149
Ted Kremenek5c6eeb12008-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 Kremenek30fa28b2008-02-13 17:41:41 +0000156void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000157 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000158
Ted Kremenek17c5f112008-02-11 19:21:59 +0000159 // Remove old bindings for subexpressions.
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000160 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000161
Ted Kremenek022b6052008-02-15 22:29:00 +0000162 // Check for NULL conditions; e.g. "for(;;)"
163 if (!Condition) {
164 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000165 return;
166 }
167
Ted Kremenek07baa252008-02-21 18:02:17 +0000168 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000169
170 switch (V.getBaseKind()) {
171 default:
172 break;
173
Ted Kremenek07baa252008-02-21 18:02:17 +0000174 case RVal::UnknownKind:
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000175 builder.generateNode(MarkBranch(PrevState, Term, true), true);
176 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000177 return;
178
Ted Kremenekb31af242008-02-28 09:25:22 +0000179 case RVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000180 NodeTy* N = builder.generateNode(PrevState, true);
181
182 if (N) {
183 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000184 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000185 }
186
187 builder.markInfeasible(false);
188 return;
189 }
190 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000191
Ted Kremenek90960972008-01-30 23:03:39 +0000192
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000193 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000194
Ted Kremenek5c6eeb12008-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 Kremenek4b170e52008-02-12 18:08:17 +0000200 else
201 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000202
203 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000204
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000205 isFeasible = false;
206 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000207
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000208 if (isFeasible)
209 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000210 else
211 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000212}
213
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000214/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000215/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000216void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000217
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000218 ValueState* St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000219 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000220
221 // Three possibilities:
222 //
223 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000224 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-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 Kremenek79f63f52008-02-13 17:27:37 +0000234 if (I.getLabel() == L) {
235 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000236 return;
237 }
238 }
239
240 assert (false && "No block with label.");
241 return;
242 }
243
Ted Kremenekb31af242008-02-28 09:25:22 +0000244 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000245 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000246 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000247 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000248 return;
249 }
250
251 // This is really a catch-all. We don't support symbolics yet.
252
Ted Kremenek07baa252008-02-21 18:02:17 +0000253 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000254
255 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000256 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000257}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000258
Ted Kremenekaee121c2008-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 Kremenekf4b49df2008-02-28 10:21:43 +0000265 ValueState* St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000266 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000267 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000268
Ted Kremenekb31af242008-02-28 09:25:22 +0000269 if (CondV.isUndef()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000270 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000271 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000272 return;
273 }
274
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000275 ValueState* DefaultSt = St;
Ted Kremenekaee121c2008-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 Kremenekbc965a62008-02-18 22:57:02 +0000279
280 unsigned bits = getContext().getTypeSize(CondE->getType(),
281 CondE->getExprLoc());
282
Ted Kremenekaee121c2008-02-13 23:08:21 +0000283 APSInt V1(bits, false);
284 APSInt V2 = V1;
285
Ted Kremenek07baa252008-02-21 18:02:17 +0000286 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-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 Kremenek07baa252008-02-21 18:02:17 +0000311 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000312
313 do {
314 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
315
Ted Kremenek07baa252008-02-21 18:02:17 +0000316 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000317
318 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000319 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000320
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000321 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-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 Kremenekb1934132008-02-14 19:37:24 +0000336 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-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 Kremenek30fa28b2008-02-13 17:41:41 +0000353void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000354 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000355
Ted Kremenek99ecce72008-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 Kremenekf4b49df2008-02-28 10:21:43 +0000361 ValueState* St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000362 RVal X = GetBlkExprRVal(St, B);
363
Ted Kremenekb31af242008-02-28 09:25:22 +0000364 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000365
Ted Kremenekb31af242008-02-28 09:25:22 +0000366 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000367
368 assert (Ex);
369
370 if (Ex == B->getRHS()) {
371
372 X = GetBlkExprRVal(St, Ex);
373
Ted Kremenekb31af242008-02-28 09:25:22 +0000374 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000375
Ted Kremenekb31af242008-02-28 09:25:22 +0000376 if (X.isUndef()) {
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000377 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
378 return;
379 }
380
Ted Kremenek99ecce72008-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 Kremenekf4b49df2008-02-28 10:21:43 +0000389 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek99ecce72008-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 Kremenek1f0eb992008-02-05 00:26:40 +0000399 }
400 else {
Ted Kremenek99ecce72008-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 Kremenek1f0eb992008-02-05 00:26:40 +0000407 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000408}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000409
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000410
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000411void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000412
Ted Kremenek07baa252008-02-21 18:02:17 +0000413 Builder = &builder;
Ted Kremenekf031b872008-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 Kremenek07baa252008-02-21 18:02:17 +0000423
Ted Kremenekf031b872008-01-23 19:59:44 +0000424 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000425 ValueState* St = RemoveDeadBindings(S, StmtEntryNode->getState());
Ted Kremenekf031b872008-01-23 19:59:44 +0000426 builder.generateNode(S, St, StmtEntryNode);
427 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000428
Ted Kremenek07baa252008-02-21 18:02:17 +0000429 // For safety, NULL out these variables.
430
Ted Kremenekf031b872008-01-23 19:59:44 +0000431 CurrentStmt = NULL;
432 StmtEntryNode = NULL;
433 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000434}
435
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000436GRExprEngine::NodeTy*
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000437GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, ValueState* St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000438
439 // If the state hasn't changed, don't generate a new node.
Ted Kremenekef7ea072008-03-04 00:56:45 +0000440 if (St == Pred->getState()) {
441 Dst.Add(Pred);
Ted Kremenek80d52d02008-02-07 05:48:01 +0000442 return NULL;
Ted Kremenekef7ea072008-03-04 00:56:45 +0000443 }
Ted Kremenekafba4b22008-01-16 00:53:15 +0000444
Ted Kremenek80d52d02008-02-07 05:48:01 +0000445 NodeTy* N = Builder->generateNode(S, St, Pred);
446 Dst.Add(N);
Ted Kremenek07baa252008-02-21 18:02:17 +0000447
Ted Kremenek80d52d02008-02-07 05:48:01 +0000448 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000449}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000450
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000451#if 0
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000452void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000453 const ValueState::BufferTy& SB) {
Ted Kremenekf20c2012008-02-05 19:35:18 +0000454
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000455 for (ValueState::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
Ted Kremenekf20c2012008-02-05 19:35:18 +0000456 Nodify(Dst, S, Pred, *I);
457}
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000458#endif
Ted Kremenekf20c2012008-02-05 19:35:18 +0000459
Ted Kremenek6d409922008-02-13 18:06:44 +0000460void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000461
Ted Kremenek9b32cd02008-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 Kremenekf4b49df2008-02-28 10:21:43 +0000470 ValueState* St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000471 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000472}
473
Ted Kremenekd9268e32008-02-19 01:44:53 +0000474void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000475 CallExpr::arg_iterator AI,
476 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000477 NodeSet& Dst) {
478
Ted Kremenek07baa252008-02-21 18:02:17 +0000479 // Process the arguments.
480
481 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000482
Ted Kremenekef7ea072008-03-04 00:56:45 +0000483 NodeSet DstTmp;
Ted Kremenek769f3482008-03-04 22:01:56 +0000484 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +0000485 ++AI;
486
Ted Kremenek769f3482008-03-04 22:01:56 +0000487 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenek07baa252008-02-21 18:02:17 +0000488 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-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 Kremenekcda2efd2008-03-03 16:47:31 +0000495
Ted Kremenekc71901d2008-02-25 21:16:03 +0000496 NodeSet DstTmp;
497 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000498
Ted Kremenekc71901d2008-02-25 21:16:03 +0000499 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000500
501 if (DstTmp.empty())
502 DstTmp.Add(Pred);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000503
504 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000505 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
506
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000507 ValueState* St = (*DI)->getState();
Ted Kremenekc71901d2008-02-25 21:16:03 +0000508 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000509
Ted Kremenekcda2efd2008-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 Kremenek43863eb2008-02-29 23:14:48 +0000515 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000516 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000517
Ted Kremenek9b31f5b2008-02-29 23:53:11 +0000518 if (N) {
519 N->markAsSink();
520 BadCalls.insert(N);
521 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000522
Ted Kremenekd9268e32008-02-19 01:44:53 +0000523 continue;
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000524 }
525
Ted Kremenek769f3482008-03-04 22:01:56 +0000526 bool invalidateArgs = false;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000527
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000528 if (L.isUnknown()) {
Ted Kremenek769f3482008-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 Kremenekb9a20e02008-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 Kremenek07baa252008-02-21 18:02:17 +0000545
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000546 if (isa<LVal>(V))
547 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000548 }
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000549 }
Ted Kremenek769f3482008-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 Kremenekb9a20e02008-02-21 19:46:04 +0000576 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
Ted Kremenek769f3482008-03-04 22:01:56 +0000577 }
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000578
Ted Kremenek59938ca2008-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 Kremenekb9a20e02008-02-21 19:46:04 +0000594 Nodify(Dst, CE, *DI, St);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000595 }
596}
597
Ted Kremenek07baa252008-02-21 18:02:17 +0000598void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000599
Ted Kremenek5f585b02008-02-19 18:52:54 +0000600 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +0000601 QualType T = CastE->getType();
602
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000603 if (T->isReferenceType())
604 VisitLVal(Ex, Pred, S1);
605 else
606 Visit(Ex, Pred, S1);
607
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000608 // Check for redundant casts or casting to "void"
609 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000610 Ex->getType() == T ||
611 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000612
Ted Kremenek07baa252008-02-21 18:02:17 +0000613 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000614 Dst.Add(*I1);
615
Ted Kremenek54eddae2008-01-24 02:02:54 +0000616 return;
617 }
618
Ted Kremenek07baa252008-02-21 18:02:17 +0000619 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000620 NodeTy* N = *I1;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000621 ValueState* St = N->getState();
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000622
623 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
624
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000625 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000626 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000627}
628
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000629void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000630 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000631
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000632 ValueState* St = Pred->getState();
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000633
634 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000635 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000636
637 // FIXME: Add support for local arrays.
638 if (VD->getType()->isArrayType())
639 continue;
640
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000641 const Expr* Ex = VD->getInit();
Ted Kremenek07baa252008-02-21 18:02:17 +0000642
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000643 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
644
645 // In this context, Static => Local variable.
646
647 assert (!VD->getStorageClass() == VarDecl::Static ||
648 !isa<FileVarDecl>(VD));
649
650 // If there is no initializer, set the value of the
Ted Kremenekb31af242008-02-28 09:25:22 +0000651 // variable to "Undefined".
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000652 //
653 // FIXME: static variables may have an initializer, but the second
654 // time a function is called those values may not be current.
Ted Kremenek98d093b2008-03-04 20:40:11 +0000655
656 QualType T = VD->getType();
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000657
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000658 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000659
660 // C99: 6.7.8 Initialization
661 // If an object that has static storage duration is not initialized
662 // explicitly, then:
663 // —if it has pointer type, it is initialized to a null pointer;
664 // —if it has arithmetic type, it is initialized to (positive or
665 // unsigned) zero;
666
Ted Kremenek98d093b2008-03-04 20:40:11 +0000667 // FIXME: Handle structs. Now we treat their values as unknown.
668
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000669 if (T->isPointerType()) {
670
671 St = SetRVal(St, lval::DeclVal(VD),
672 lval::ConcreteInt(ValMgr.getValue(0, T)));
673 }
674 else if (T->isIntegerType()) {
675
676 St = SetRVal(St, lval::DeclVal(VD),
677 nonlval::ConcreteInt(ValMgr.getValue(0, T)));
678 }
679
Ted Kremenek98d093b2008-03-04 20:40:11 +0000680
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000681 }
Ted Kremenek98d093b2008-03-04 20:40:11 +0000682 else {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000683
Ted Kremenek98d093b2008-03-04 20:40:11 +0000684 // FIXME: Handle structs. Now we treat them as unknown. What
685 // we need to do is treat their members as unknown.
686
687 if (T->isPointerType() || T->isIntegerType())
688 St = SetRVal(St, lval::DeclVal(VD),
689 Ex ? GetRVal(St, Ex) : UndefinedVal());
690 }
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000691 }
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000692 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000693
694 Nodify(Dst, DS, Pred, St);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000695}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000696
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000697
Ted Kremenek07baa252008-02-21 18:02:17 +0000698void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000699 NodeTy* Pred, NodeSet& Dst) {
700
Ted Kremenek99ecce72008-02-26 19:05:15 +0000701 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
702
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000703 ValueState* St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000704 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000705
Ted Kremenekb31af242008-02-28 09:25:22 +0000706 assert (X.isUndef());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000707
Ted Kremenekb31af242008-02-28 09:25:22 +0000708 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000709
710 assert (SE);
711
712 X = GetBlkExprRVal(St, SE);
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000713
714 // Make sure that we invalidate the previous binding.
715 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000716}
717
Ted Kremenekfd85f292008-02-12 19:49:57 +0000718/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000719void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
720 NodeTy* Pred,
721 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000722
723 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000724
725 // 6.5.3.4 sizeof: "The result type is an integer."
726
Ted Kremenek07baa252008-02-21 18:02:17 +0000727 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000728
Ted Kremenek07baa252008-02-21 18:02:17 +0000729
Ted Kremenek571f5192008-02-21 18:15:29 +0000730 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000731 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000732 return;
733
Ted Kremenek571f5192008-02-21 18:15:29 +0000734
735 uint64_t size = 1; // Handle sizeof(void)
736
737 if (T != getContext().VoidTy) {
738 SourceLocation Loc = Ex->getExprLoc();
739 size = getContext().getTypeSize(T, Loc) / 8;
740 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000741
Ted Kremenek07baa252008-02-21 18:02:17 +0000742 Nodify(Dst, Ex, Pred,
743 SetRVal(Pred->getState(), Ex,
Ted Kremenek571f5192008-02-21 18:15:29 +0000744 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000745
746}
747
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000748void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
749
Ted Kremenek07baa252008-02-21 18:02:17 +0000750 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000751
752 NodeSet DstTmp;
753
Ted Kremenek56a89992008-02-26 03:44:25 +0000754 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000755 DstTmp.Add(Pred);
756 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000757 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000758
Ted Kremenek07baa252008-02-21 18:02:17 +0000759 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000760
761 NodeTy* N = *I;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000762 ValueState* St = N->getState();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000763
764 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
765
Ted Kremenek07baa252008-02-21 18:02:17 +0000766 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000767
Ted Kremenekb31af242008-02-28 09:25:22 +0000768 // Check for dereferences of undefined values.
Ted Kremenek07baa252008-02-21 18:02:17 +0000769
Ted Kremenekb31af242008-02-28 09:25:22 +0000770 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000771
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000772 NodeTy* Succ = Builder->generateNode(U, St, N);
773
774 if (Succ) {
775 Succ->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000776 UndefDeref.insert(Succ);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000777 }
778
779 continue;
780 }
781
Ted Kremenek07baa252008-02-21 18:02:17 +0000782 // Check for dereferences of unknown values. Treat as No-Ops.
783
784 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000785 Dst.Add(N);
786 continue;
787 }
788
789 // After a dereference, one of two possible situations arise:
790 // (1) A crash, because the pointer was NULL.
791 // (2) The pointer is not NULL, and the dereference works.
792 //
793 // We add these assumptions.
794
Ted Kremenek07baa252008-02-21 18:02:17 +0000795 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000796 bool isFeasibleNotNull;
797
798 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000799
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000800 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000801
802 if (isFeasibleNotNull) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000803
804 // FIXME: Currently symbolic analysis "generates" new symbols
805 // for the contents of values. We need a better approach.
806
Ted Kremenek07baa252008-02-21 18:02:17 +0000807 Nodify(Dst, U, N, SetRVal(StNotNull, U,
808 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000809 }
810
811 bool isFeasibleNull;
812
Ted Kremenek07baa252008-02-21 18:02:17 +0000813 // Now "assume" that the pointer is NULL.
814
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000815 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000816
817 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000818
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000819 // We don't use "Nodify" here because the node will be a sink
820 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000821
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000822 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
823
824 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000825
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000826 NullNode->markAsSink();
827
Ted Kremenek07baa252008-02-21 18:02:17 +0000828 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
829 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000830 }
831 }
832 }
833}
834
Ted Kremenek07baa252008-02-21 18:02:17 +0000835void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
836 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000837
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000838 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000839
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000840 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000841 assert (U->getOpcode() != UnaryOperator::SizeOf);
842 assert (U->getOpcode() != UnaryOperator::AlignOf);
843
844 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000845
846 switch (U->getOpcode()) {
847 case UnaryOperator::PostInc:
848 case UnaryOperator::PostDec:
849 case UnaryOperator::PreInc:
850 case UnaryOperator::PreDec:
851 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000852 // Evalue subexpression as an LVal.
853 use_GetLVal = true;
854 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000855 break;
856
857 default:
858 Visit(U->getSubExpr(), Pred, S1);
859 break;
860 }
861
Ted Kremenek07baa252008-02-21 18:02:17 +0000862 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000863
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000864 NodeTy* N1 = *I1;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000865 ValueState* St = N1->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000866
867 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
868 GetRVal(St, U->getSubExpr());
869
870 if (SubV.isUnknown()) {
871 Dst.Add(N1);
872 continue;
873 }
874
Ted Kremenekb31af242008-02-28 09:25:22 +0000875 if (SubV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000876 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
877 continue;
878 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000879
Ted Kremenek22640ce2008-02-15 22:09:30 +0000880 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000881
882 // Handle ++ and -- (both pre- and post-increment).
883
Ted Kremenek07baa252008-02-21 18:02:17 +0000884 LVal SubLV = cast<LVal>(SubV);
885 RVal V = GetRVal(St, SubLV, U->getType());
886
Ted Kremenekb8782e12008-02-21 19:15:37 +0000887 if (V.isUnknown()) {
888 Dst.Add(N1);
889 continue;
890 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000891
Ted Kremenekb31af242008-02-28 09:25:22 +0000892 // Propagate undefined values.
893 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000894 Nodify(Dst, U, N1, SetRVal(St, U, V));
895 continue;
896 }
897
Ted Kremenekb1669d42008-02-21 19:29:23 +0000898 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000899
900 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
901 : BinaryOperator::Sub;
902
Ted Kremenekb1669d42008-02-21 19:29:23 +0000903 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000904
905 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000906 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000907 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000908 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000909
Ted Kremenek07baa252008-02-21 18:02:17 +0000910 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000911 continue;
912 }
913
914 // Handle all other unary operators.
915
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000916 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000917
Ted Kremenek07baa252008-02-21 18:02:17 +0000918 case UnaryOperator::Minus:
919 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000920 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000921
Ted Kremenek07baa252008-02-21 18:02:17 +0000922 case UnaryOperator::Not:
923 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000924 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000925
Ted Kremenek07baa252008-02-21 18:02:17 +0000926 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000927
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000928 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
929 //
930 // Note: technically we do "E == 0", but this is the same in the
931 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000932
933 if (isa<LVal>(SubV)) {
934 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
935 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
936 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000937 }
938 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000939 Expr* Ex = U->getSubExpr();
940 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000941 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
942 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000943 }
944
945 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000946
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000947 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000948 assert (isa<LVal>(SubV));
949 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000950 break;
951 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000952
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000953 default: ;
954 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000955 }
956
957 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000958 }
959}
960
Ted Kremenek07baa252008-02-21 18:02:17 +0000961void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
962 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000963
Ted Kremenek07baa252008-02-21 18:02:17 +0000964 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000965
Ted Kremenek07baa252008-02-21 18:02:17 +0000966 // FIXME: Add support for VLAs.
967 if (!T.getTypePtr()->isConstantSizeType())
968 return;
969
970 SourceLocation Loc = U->getExprLoc();
971 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000972 ValueState* St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000973 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
974
975 Nodify(Dst, U, Pred, St);
976}
977
978void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneka129a2e2008-02-27 07:04:16 +0000979
980 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
981 Dst.Add(Pred);
982 return;
983 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000984
985 Ex = Ex->IgnoreParens();
986
Ted Kremenekb5175bf2008-02-27 06:47:26 +0000987 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000988 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000989 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000990 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000991
Ted Kremenek07baa252008-02-21 18:02:17 +0000992 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000993 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000994 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000995
Ted Kremenek07baa252008-02-21 18:02:17 +0000996 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000997 Dst.Add(Pred);
998 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000999 Visit(Ex, Pred, Dst);
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001000
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001001 return;
1002 }
1003 }
1004
Ted Kremenek07baa252008-02-21 18:02:17 +00001005 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001006}
1007
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001008void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00001009 GRExprEngine::NodeTy* Pred,
1010 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001011 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001012
1013 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00001014 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001015 else
1016 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001017
Ted Kremenekf031b872008-01-23 19:59:44 +00001018 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001019
Ted Kremenekf031b872008-01-23 19:59:44 +00001020 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +00001021
Ted Kremenekf031b872008-01-23 19:59:44 +00001022 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +00001023 // In such cases, we want to (initially) treat the LHS as an LVal,
1024 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1025 // evaluated to LValDecl's instead of to an NonLVal.
1026
1027 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
1028 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +00001029
Ted Kremenek07baa252008-02-21 18:02:17 +00001030 // Visit the RHS...
1031
1032 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +00001033 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001034
1035 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +00001036
Ted Kremenek07baa252008-02-21 18:02:17 +00001037 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +00001038
Ted Kremenekf031b872008-01-23 19:59:44 +00001039 NodeTy* N2 = *I2;
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001040 ValueState* St = N2->getState();
Ted Kremenek2c369792008-02-25 18:42:54 +00001041 Expr* RHS = B->getRHS();
1042 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +00001043
Ted Kremenek15cb0782008-02-06 22:50:25 +00001044 BinaryOperator::Opcode Op = B->getOpcode();
1045
Ted Kremenek2c369792008-02-25 18:42:54 +00001046 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1047 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +00001048
Ted Kremenekb31af242008-02-28 09:25:22 +00001049 // Check if the denominator is undefined.
Ted Kremenek2c369792008-02-25 18:42:54 +00001050
Ted Kremenek5a600862008-02-26 22:27:51 +00001051 if (!RightV.isUnknown()) {
1052
Ted Kremenekb31af242008-02-28 09:25:22 +00001053 if (RightV.isUndef()) {
1054 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek5a600862008-02-26 22:27:51 +00001055
Ted Kremenekb31af242008-02-28 09:25:22 +00001056 if (DivUndef) {
1057 DivUndef->markAsSink();
1058 BadDivides.insert(DivUndef);
Ted Kremenek5a600862008-02-26 22:27:51 +00001059 }
1060
1061 continue;
1062 }
1063
1064 // Check for divide/remainder-by-zero.
1065 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001066 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001067
Ted Kremenek5a600862008-02-26 22:27:51 +00001068 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001069 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek5a600862008-02-26 22:27:51 +00001070
Ted Kremenekc2d07202008-02-28 20:32:03 +00001071 if (isFeasible)
1072 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001073 DivZeroNode->markAsSink();
1074 BadDivides.insert(DivZeroNode);
1075 }
Ted Kremenekef0007f2008-02-26 02:15:56 +00001076
Ted Kremenek5a600862008-02-26 22:27:51 +00001077 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001078
Ted Kremenek5a600862008-02-26 22:27:51 +00001079 isFeasible = false;
1080 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001081
Ted Kremenek5a600862008-02-26 22:27:51 +00001082 if (!isFeasible)
1083 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001084 }
1085
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001086 // Fall-through. The logic below processes the divide.
1087 }
1088
Ted Kremenekc2d07202008-02-28 20:32:03 +00001089
Ted Kremenek15cb0782008-02-06 22:50:25 +00001090 if (Op <= BinaryOperator::Or) {
1091
Ted Kremenek07baa252008-02-21 18:02:17 +00001092 // Process non-assignements except commas or short-circuited
1093 // logical expressions (LAnd and LOr).
1094
1095 RVal Result = EvalBinOp(Op, LeftV, RightV);
1096
1097 if (Result.isUnknown()) {
1098 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001099 continue;
1100 }
1101
Ted Kremenekc2d07202008-02-28 20:32:03 +00001102 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1103
1104 // The operands were not undefined, but the result is undefined.
1105
1106 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1107 UndefNode->markAsSink();
1108 UndefResults.insert(UndefNode);
1109 }
1110
1111 continue;
1112 }
1113
Ted Kremenek07baa252008-02-21 18:02:17 +00001114 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001115 continue;
1116 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001117
1118 // Process assignments.
1119
1120 switch (Op) {
1121
Ted Kremenekf031b872008-01-23 19:59:44 +00001122 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001123
1124 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001125
Ted Kremenekb31af242008-02-28 09:25:22 +00001126 if (LeftV.isUndef()) {
1127 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001128 continue;
1129 }
1130
1131 if (LeftV.isUnknown()) {
1132 St = SetRVal(St, B, RightV);
1133 break;
1134 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001135
Ted Kremenek07baa252008-02-21 18:02:17 +00001136 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001137 break;
1138 }
1139
Ted Kremenek07baa252008-02-21 18:02:17 +00001140 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001141
Ted Kremenek07baa252008-02-21 18:02:17 +00001142 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001143
Ted Kremenek5a600862008-02-26 22:27:51 +00001144 assert (B->isCompoundAssignmentOp());
1145
1146 if (Op >= BinaryOperator::AndAssign)
1147 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1148 else
1149 ((int&) Op) -= BinaryOperator::MulAssign;
1150
Ted Kremenekb31af242008-02-28 09:25:22 +00001151 // Check if the LHS is undefined.
Ted Kremenek07baa252008-02-21 18:02:17 +00001152
Ted Kremenekb31af242008-02-28 09:25:22 +00001153 if (LeftV.isUndef()) {
1154 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001155 continue;
1156 }
1157
1158 if (LeftV.isUnknown()) {
1159
1160 // While we do not know the location to store RightV,
1161 // the entire expression does evaluate to RightV.
1162
1163 if (RightV.isUnknown()) {
1164 Dst.Add(N2);
1165 continue;
1166 }
1167
1168 St = SetRVal(St, B, RightV);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001169 break;
1170 }
1171
Ted Kremenek07baa252008-02-21 18:02:17 +00001172 // At this pointer we know that the LHS evaluates to an LVal
1173 // that is neither "Unknown" or "Unintialized."
1174
1175 LVal LeftLV = cast<LVal>(LeftV);
1176
Ted Kremenek07baa252008-02-21 18:02:17 +00001177 // Fetch the value of the LHS (the value of the variable, etc.).
1178
1179 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1180
Ted Kremenekb31af242008-02-28 09:25:22 +00001181 // Propagate undefined value (left-side). We
1182 // propogate undefined values for the RHS below when
Ted Kremenek5a600862008-02-26 22:27:51 +00001183 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001184
Ted Kremenekb31af242008-02-28 09:25:22 +00001185 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001186 St = SetRVal(St, B, V);
1187 break;
1188 }
1189
1190 // Propagate unknown values.
1191
Ted Kremenekb8782e12008-02-21 19:15:37 +00001192 if (V.isUnknown()) {
1193 Dst.Add(N2);
1194 continue;
1195 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001196
1197 if (RightV.isUnknown()) {
1198 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1199 break;
1200 }
1201
Ted Kremenek5a600862008-02-26 22:27:51 +00001202 // At this point:
1203 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001204 // The LHS is not Undef/Unknown.
Ted Kremenek5a600862008-02-26 22:27:51 +00001205 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001206
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001207 // Get the computation type.
1208 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1209
1210 // Perform promotions.
1211 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001212 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001213
1214 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001215
Ted Kremenek2c369792008-02-25 18:42:54 +00001216 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1217 && RHS->getType()->isIntegerType()) {
1218
Ted Kremenekb31af242008-02-28 09:25:22 +00001219 // Check if the denominator is undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001220
Ted Kremenekb31af242008-02-28 09:25:22 +00001221 if (RightV.isUndef()) {
1222 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001223
Ted Kremenekb31af242008-02-28 09:25:22 +00001224 if (DivUndef) {
1225 DivUndef->markAsSink();
1226 BadDivides.insert(DivUndef);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001227 }
1228
1229 continue;
1230 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001231
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001232 // First, "assume" that the denominator is 0.
1233
1234 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001235 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001236
1237 if (isFeasible) {
1238 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1239
1240 if (DivZeroNode) {
1241 DivZeroNode->markAsSink();
Ted Kremenekef0007f2008-02-26 02:15:56 +00001242 BadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001243 }
1244 }
1245
1246 // Second, "assume" that the denominator cannot be 0.
1247
1248 isFeasible = false;
1249 St = Assume(St, RightV, true, isFeasible);
1250
1251 if (!isFeasible)
1252 continue;
1253
1254 // Fall-through. The logic below processes the divide.
1255 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001256 else {
1257
Ted Kremenekb31af242008-02-28 09:25:22 +00001258 // Propagate undefined values (right-side).
Ted Kremenek5a600862008-02-26 22:27:51 +00001259
Ted Kremenekb31af242008-02-28 09:25:22 +00001260 if (RightV.isUndef()) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001261 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1262 break;
1263 }
1264
1265 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001266
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001267 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekc2d07202008-02-28 20:32:03 +00001268
1269 if (Result.isUndef()) {
1270
1271 // The operands were not undefined, but the result is undefined.
1272
1273 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1274 UndefNode->markAsSink();
1275 UndefResults.insert(UndefNode);
1276 }
1277
1278 continue;
1279 }
1280
Ted Kremenek07baa252008-02-21 18:02:17 +00001281 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001282 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001283 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001284
1285 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001286 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001287 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001288}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001289
Ted Kremenekb31af242008-02-28 09:25:22 +00001290void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001291 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1292 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +00001293 UndefStores.insert(N);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001294}
Ted Kremenekbe962452008-01-16 19:42:59 +00001295
Ted Kremenekbf988d02008-02-19 00:22:37 +00001296void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001297
1298 // FIXME: add metadata to the CFG so that we can disable
1299 // this check when we KNOW that there is no block-level subexpression.
1300 // The motivation is that this check requires a hashtable lookup.
1301
1302 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1303 Dst.Add(Pred);
1304 return;
1305 }
1306
1307 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001308
1309 default:
1310 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001311 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001312
1313 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1314 break;
1315
Ted Kremenek744a7862008-02-08 20:29:23 +00001316 case Stmt::BinaryOperatorClass: {
1317 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001318
Ted Kremenek744a7862008-02-08 20:29:23 +00001319 if (B->isLogicalOp()) {
1320 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001321 break;
1322 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001323 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001324 ValueState* St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001325 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001326 break;
1327 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001328
1329 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1330 break;
1331 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001332
1333 case Stmt::CallExprClass: {
1334 CallExpr* C = cast<CallExpr>(S);
1335 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1336 break;
1337 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001338
1339 case Stmt::CastExprClass: {
1340 CastExpr* C = cast<CastExpr>(S);
1341 VisitCast(C, C->getSubExpr(), Pred, Dst);
1342 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001343 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001344
Ted Kremenek07baa252008-02-21 18:02:17 +00001345 // FIXME: ChooseExpr is really a constant. We need to fix
1346 // the CFG do not model them as explicit control-flow.
1347
Ted Kremenekfd85f292008-02-12 19:49:57 +00001348 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1349 ChooseExpr* C = cast<ChooseExpr>(S);
1350 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1351 break;
1352 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001353
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001354 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001355 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1356 break;
1357
Ted Kremenekfd85f292008-02-12 19:49:57 +00001358 case Stmt::ConditionalOperatorClass: { // '?' operator
1359 ConditionalOperator* C = cast<ConditionalOperator>(S);
1360 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1361 break;
1362 }
1363
1364 case Stmt::DeclRefExprClass:
1365 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1366 break;
1367
1368 case Stmt::DeclStmtClass:
1369 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1370 break;
1371
1372 case Stmt::ImplicitCastExprClass: {
1373 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1374 VisitCast(C, C->getSubExpr(), Pred, Dst);
1375 break;
1376 }
1377
1378 case Stmt::ParenExprClass:
1379 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1380 break;
1381
1382 case Stmt::SizeOfAlignOfTypeExprClass:
1383 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1384 break;
1385
Ted Kremenek106f37c2008-02-08 07:05:39 +00001386 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001387 StmtExpr* SE = cast<StmtExpr>(S);
1388
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001389 ValueState* St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +00001390 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001391 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001392 break;
1393 }
1394
Ted Kremenek07baa252008-02-21 18:02:17 +00001395 // FIXME: We may wish to always bind state to ReturnStmts so
1396 // that users can quickly query what was the state at the
1397 // exit points of a function.
1398
Ted Kremenekfd85f292008-02-12 19:49:57 +00001399 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001400 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1401 Visit(R, Pred, Dst);
1402 else
1403 Dst.Add(Pred);
1404
1405 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001406 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001407
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001408 case Stmt::UnaryOperatorClass: {
1409 UnaryOperator* U = cast<UnaryOperator>(S);
1410
Ted Kremenek07baa252008-02-21 18:02:17 +00001411 switch (U->getOpcode()) {
1412 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1413 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1414 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1415 default: VisitUnaryOperator(U, Pred, Dst); break;
1416 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001417
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001418 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001419 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001420 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001421}
1422
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001423//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001424// "Assume" logic.
1425//===----------------------------------------------------------------------===//
1426
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001427ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001428 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001429 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001430 switch (Cond.getSubKind()) {
1431 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001432 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001433 return St;
1434
Ted Kremenek13f31562008-02-06 00:54:14 +00001435 case lval::SymbolValKind:
1436 if (Assumption)
1437 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1438 ValMgr.getZeroWithPtrWidth(), isFeasible);
1439 else
1440 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1441 ValMgr.getZeroWithPtrWidth(), isFeasible);
1442
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001443
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001444 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001445 case lval::FuncValKind:
1446 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001447 isFeasible = Assumption;
1448 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001449
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001450 case lval::ConcreteIntKind: {
1451 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001452 isFeasible = b ? Assumption : !Assumption;
1453 return St;
1454 }
1455 }
Ted Kremenek90960972008-01-30 23:03:39 +00001456}
1457
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001458ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001459 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001460 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001461 switch (Cond.getSubKind()) {
1462 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001463 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001464 return St;
1465
Ted Kremenekab359c12008-02-06 17:32:17 +00001466
1467 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001468 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001469 SymbolID sym = SV.getSymbol();
1470
1471 if (Assumption)
1472 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1473 isFeasible);
1474 else
1475 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1476 isFeasible);
1477 }
1478
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001479 case nonlval::SymIntConstraintValKind:
1480 return
1481 AssumeSymInt(St, Assumption,
1482 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1483 isFeasible);
1484
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001485 case nonlval::ConcreteIntKind: {
1486 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001487 isFeasible = b ? Assumption : !Assumption;
1488 return St;
1489 }
1490 }
1491}
1492
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001493ValueState*
1494GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001495 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001496
Ted Kremenek13f31562008-02-06 00:54:14 +00001497 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001498 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001499 isFeasible = *X != V;
1500 return St;
1501 }
1502
1503 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001504 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001505 isFeasible = true;
1506 return St;
1507 }
1508
1509 // If we reach here, sym is not a constant and we don't know if it is != V.
1510 // Make that assumption.
1511
1512 isFeasible = true;
1513 return StateMgr.AddNE(St, sym, V);
1514}
1515
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001516ValueState*
1517GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001518 const llvm::APSInt& V, bool& isFeasible) {
1519
1520 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001521 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001522 isFeasible = *X == V;
1523 return St;
1524 }
1525
1526 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001527 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001528 isFeasible = false;
1529 return St;
1530 }
1531
1532 // If we reach here, sym is not a constant and we don't know if it is == V.
1533 // Make that assumption.
1534
1535 isFeasible = true;
1536 return StateMgr.AddEQ(St, sym, V);
1537}
Ted Kremenek90960972008-01-30 23:03:39 +00001538
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001539ValueState*
1540GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001541 const SymIntConstraint& C, bool& isFeasible) {
1542
1543 switch (C.getOpcode()) {
1544 default:
1545 // No logic yet for other operators.
1546 return St;
1547
1548 case BinaryOperator::EQ:
1549 if (Assumption)
1550 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1551 else
1552 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1553
1554 case BinaryOperator::NE:
1555 if (Assumption)
1556 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1557 else
1558 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1559 }
1560}
1561
Ted Kremenek90960972008-01-30 23:03:39 +00001562//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001563// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001564//===----------------------------------------------------------------------===//
1565
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001566#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001567static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001568
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001569namespace llvm {
1570template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001571struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001572 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001573
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001574 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001575
1576 Out << "Variables:\\l";
1577
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001578 bool isFirst = true;
1579
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001580 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001581
1582 if (isFirst)
1583 isFirst = false;
1584 else
1585 Out << "\\l";
1586
1587 Out << ' ' << I.getKey()->getName() << " : ";
1588 I.getData().print(Out);
1589 }
1590
1591 }
1592
Ted Kremenek17c5f112008-02-11 19:21:59 +00001593
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001594 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001595
1596 bool isFirst = true;
1597
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001598 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001599
1600 if (isFirst) {
1601 Out << "\\l\\lSub-Expressions:\\l";
1602 isFirst = false;
1603 }
1604 else
1605 Out << "\\l";
1606
1607 Out << " (" << (void*) I.getKey() << ") ";
1608 I.getKey()->printPretty(Out);
1609 Out << " : ";
1610 I.getData().print(Out);
1611 }
1612 }
1613
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001614 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001615
Ted Kremenek08cfd832008-02-08 21:10:02 +00001616 bool isFirst = true;
1617
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001618 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001619 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001620 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001621 isFirst = false;
1622 }
1623 else
1624 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001625
Ted Kremenek17c5f112008-02-11 19:21:59 +00001626 Out << " (" << (void*) I.getKey() << ") ";
1627 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001628 Out << " : ";
1629 I.getData().print(Out);
1630 }
1631 }
1632
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001633 static void PrintEQ(std::ostream& Out, ValueState* St) {
1634 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001635
1636 if (CE.isEmpty())
1637 return;
1638
1639 Out << "\\l\\|'==' constraints:";
1640
Ted Kremenek07baa252008-02-21 18:02:17 +00001641 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001642 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1643 }
1644
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001645 static void PrintNE(std::ostream& Out, ValueState* St) {
1646 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001647
1648 if (NE.isEmpty())
1649 return;
1650
1651 Out << "\\l\\|'!=' constraints:";
1652
Ted Kremenek07baa252008-02-21 18:02:17 +00001653 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001654 I != EI; ++I){
1655
1656 Out << "\\l $" << I.getKey() << " : ";
1657 bool isFirst = true;
1658
1659 ValueState::IntSetTy::iterator J=I.getData().begin(),
1660 EJ=I.getData().end();
1661 for ( ; J != EJ; ++J) {
1662 if (isFirst) isFirst = false;
1663 else Out << ", ";
1664
1665 Out << (*J)->toString();
1666 }
1667 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001668 }
1669
1670 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1671
1672 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001673 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00001674 GraphPrintCheckerState->isUndefDeref(N) ||
1675 GraphPrintCheckerState->isUndefStore(N) ||
1676 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenekc2d07202008-02-28 20:32:03 +00001677 GraphPrintCheckerState->isBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00001678 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001679 GraphPrintCheckerState->isBadCall(N) ||
1680 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001681 return "color=\"red\",style=\"filled\"";
1682
Ted Kremenekc2d07202008-02-28 20:32:03 +00001683 if (GraphPrintCheckerState->isNoReturnCall(N))
1684 return "color=\"blue\",style=\"filled\"";
1685
Ted Kremeneka853de62008-02-14 22:54:53 +00001686 return "";
1687 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001688
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001689 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001690 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001691
1692 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001693 ProgramPoint Loc = N->getLocation();
1694
1695 switch (Loc.getKind()) {
1696 case ProgramPoint::BlockEntranceKind:
1697 Out << "Block Entrance: B"
1698 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1699 break;
1700
1701 case ProgramPoint::BlockExitKind:
1702 assert (false);
1703 break;
1704
1705 case ProgramPoint::PostStmtKind: {
1706 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001707 Out << L.getStmt()->getStmtClassName() << ':'
1708 << (void*) L.getStmt() << ' ';
1709
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001710 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001711
Ted Kremenek43863eb2008-02-29 23:14:48 +00001712 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenek80d52d02008-02-07 05:48:01 +00001713 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001714 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001715 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001716 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001717 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001718 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001719 Out << "\\|Store to Undefined LVal.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001720 else if (GraphPrintCheckerState->isBadDivide(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001721 Out << "\\|Divide-by zero or undefined value.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001722 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenekc2d07202008-02-28 20:32:03 +00001723 Out << "\\|Result of operation is undefined.";
Ted Kremenekc2d07202008-02-28 20:32:03 +00001724 else if (GraphPrintCheckerState->isNoReturnCall(N))
1725 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001726 else if (GraphPrintCheckerState->isBadCall(N))
1727 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001728 else if (GraphPrintCheckerState->isUndefArg(N))
1729 Out << "\\|Argument in call is undefined";
Ted Kremenek80d52d02008-02-07 05:48:01 +00001730
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001731 break;
1732 }
1733
1734 default: {
1735 const BlockEdge& E = cast<BlockEdge>(Loc);
1736 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1737 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001738
1739 if (Stmt* T = E.getSrc()->getTerminator()) {
1740 Out << "\\|Terminator: ";
1741 E.getSrc()->printTerminator(Out);
1742
Ted Kremenekaee121c2008-02-13 23:08:21 +00001743 if (isa<SwitchStmt>(T)) {
1744 Stmt* Label = E.getDst()->getLabel();
1745
1746 if (Label) {
1747 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1748 Out << "\\lcase ";
1749 C->getLHS()->printPretty(Out);
1750
1751 if (Stmt* RHS = C->getRHS()) {
1752 Out << " .. ";
1753 RHS->printPretty(Out);
1754 }
1755
1756 Out << ":";
1757 }
1758 else {
1759 assert (isa<DefaultStmt>(Label));
1760 Out << "\\ldefault:";
1761 }
1762 }
1763 else
1764 Out << "\\l(implicit) default:";
1765 }
1766 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001767 // FIXME
1768 }
1769 else {
1770 Out << "\\lCondition: ";
1771 if (*E.getSrc()->succ_begin() == E.getDst())
1772 Out << "true";
1773 else
1774 Out << "false";
1775 }
1776
1777 Out << "\\l";
1778 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001779
Ted Kremenekb31af242008-02-28 09:25:22 +00001780 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1781 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00001782 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001783 }
1784 }
1785
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001786 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001787
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001788 N->getState()->printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001789
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001790 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001791 return Out.str();
1792 }
1793};
1794} // end llvm namespace
1795#endif
1796
Ted Kremenek3862eb12008-02-14 22:36:46 +00001797void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001798#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001799 GraphPrintCheckerState = this;
1800 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001801 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001802#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001803}