blob: bce1cbd90c709b45439a83339e36288834305e49 [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 Kremenek8b41e8c2008-03-07 20:57:30 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000018#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000019
Ted Kremenek9f6b1612008-02-27 06:07:00 +000020#ifndef NDEBUG
21#include "llvm/Support/GraphWriter.h"
22#include <sstream>
23#endif
24
Ted Kremenekb451dd32008-03-05 21:15:02 +000025// SaveAndRestore - A utility class that uses RIIA to save and restore
26// the value of a variable.
27template<typename T>
28struct VISIBILITY_HIDDEN SaveAndRestore {
29 SaveAndRestore(T& x) : X(x), old_value(x) {}
30 ~SaveAndRestore() { X = old_value; }
31 T get() { return old_value; }
32
33 T& X;
34 T old_value;
35};
36
Ted Kremenekd4467432008-02-14 22:16:04 +000037using namespace clang;
38using llvm::dyn_cast;
39using llvm::cast;
40using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000041
Ted Kremenekf973eb02008-03-09 18:05:48 +000042
Ted Kremenekf4b49df2008-02-28 10:21:43 +000043ValueState* GRExprEngine::getInitialState() {
Ted Kremenekb5175bf2008-02-27 06:47:26 +000044
45 // The LiveVariables information already has a compilation of all VarDecls
46 // used in the function. Iterate through this set, and "symbolicate"
47 // any VarDecl whose value originally comes from outside the function.
48
49 typedef LiveVariables::AnalysisDataTy LVDataTy;
50 LVDataTy& D = Liveness.getAnalysisData();
51
Ted Kremenekf4b49df2008-02-28 10:21:43 +000052 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenekb5175bf2008-02-27 06:47:26 +000053
54 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
55
56 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
57
58 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
59 RVal X = RVal::GetSymbolValue(SymMgr, VD);
60 StateMgr.BindVar(StateImpl, VD, X);
61 }
62 }
63
64 return StateMgr.getPersistentState(StateImpl);
65}
66
Ted Kremenekf4b49df2008-02-28 10:21:43 +000067ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000068
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000069 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000070
Ted Kremenek07baa252008-02-21 18:02:17 +000071 if (Ex == CurrentStmt) {
72 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000073
74 if (!isBlkExpr)
75 return St;
76 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000077
Ted Kremenekad5d5c52008-02-26 23:37:01 +000078 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000079}
80
Ted Kremenekf4b49df2008-02-28 10:21:43 +000081ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
82 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +000083
84 switch (Terminator->getStmtClass()) {
85 default:
86 return St;
87
88 case Stmt::BinaryOperatorClass: { // '&&' and '||'
89
90 BinaryOperator* B = cast<BinaryOperator>(Terminator);
91 BinaryOperator::Opcode Op = B->getOpcode();
92
93 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
94
95 // For &&, if we take the true branch, then the value of the whole
96 // expression is that of the RHS expression.
97 //
98 // For ||, if we take the false branch, then the value of the whole
99 // expression is that of the RHS expression.
100
101 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
102 (Op == BinaryOperator::LOr && !branchTaken)
103 ? B->getRHS() : B->getLHS();
104
Ted Kremenekb31af242008-02-28 09:25:22 +0000105 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000106 }
107
108 case Stmt::ConditionalOperatorClass: { // ?:
109
110 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
111
112 // For ?, if branchTaken == true then the value is either the LHS or
113 // the condition itself. (GNU extension).
114
115 Expr* Ex;
116
117 if (branchTaken)
118 Ex = C->getLHS() ? C->getLHS() : C->getCond();
119 else
120 Ex = C->getRHS();
121
Ted Kremenekb31af242008-02-28 09:25:22 +0000122 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000123 }
124
125 case Stmt::ChooseExprClass: { // ?:
126
127 ChooseExpr* C = cast<ChooseExpr>(Terminator);
128
129 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenekb31af242008-02-28 09:25:22 +0000130 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000131 }
132 }
133}
134
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000135bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
136 GRBlockCounter BC) {
137
138 return BC.getNumVisited(B->getBlockID()) < 3;
139}
140
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000141void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000142 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000143
Ted Kremenek17c5f112008-02-11 19:21:59 +0000144 // Remove old bindings for subexpressions.
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000145 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000146
Ted Kremenek022b6052008-02-15 22:29:00 +0000147 // Check for NULL conditions; e.g. "for(;;)"
148 if (!Condition) {
149 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000150 return;
151 }
152
Ted Kremenek07baa252008-02-21 18:02:17 +0000153 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000154
155 switch (V.getBaseKind()) {
156 default:
157 break;
158
Ted Kremenek07baa252008-02-21 18:02:17 +0000159 case RVal::UnknownKind:
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000160 builder.generateNode(MarkBranch(PrevState, Term, true), true);
161 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000162 return;
163
Ted Kremenekb31af242008-02-28 09:25:22 +0000164 case RVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000165 NodeTy* N = builder.generateNode(PrevState, true);
166
167 if (N) {
168 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000169 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000170 }
171
172 builder.markInfeasible(false);
173 return;
174 }
175 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000176
Ted Kremenek90960972008-01-30 23:03:39 +0000177
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000178 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000179
Ted Kremenekd4676512008-03-12 21:45:47 +0000180 bool isFeasible = false;
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000181 ValueState* St = Assume(PrevState, V, true, isFeasible);
182
183 if (isFeasible)
184 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000185 else
186 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000187
188 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000189
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000190 isFeasible = false;
191 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000192
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000193 if (isFeasible)
194 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000195 else
196 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000197}
198
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000199/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000200/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000201void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000202
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000203 ValueState* St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000204 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000205
206 // Three possibilities:
207 //
208 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000209 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000210 // (3) We have no clue about the label. Dispatch to all targets.
211 //
212
213 typedef IndirectGotoNodeBuilder::iterator iterator;
214
215 if (isa<lval::GotoLabel>(V)) {
216 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
217
218 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000219 if (I.getLabel() == L) {
220 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000221 return;
222 }
223 }
224
225 assert (false && "No block with label.");
226 return;
227 }
228
Ted Kremenekb31af242008-02-28 09:25:22 +0000229 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000230 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000231 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000232 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000233 return;
234 }
235
236 // This is really a catch-all. We don't support symbolics yet.
237
Ted Kremenek07baa252008-02-21 18:02:17 +0000238 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000239
240 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000241 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000242}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000243
Ted Kremenekaee121c2008-02-13 23:08:21 +0000244/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
245/// nodes by processing the 'effects' of a switch statement.
246void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
247
248 typedef SwitchNodeBuilder::iterator iterator;
249
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000250 ValueState* St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000251 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000252 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000253
Ted Kremenekb31af242008-02-28 09:25:22 +0000254 if (CondV.isUndef()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000255 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000256 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000257 return;
258 }
259
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000260 ValueState* DefaultSt = St;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000261
262 // While most of this can be assumed (such as the signedness), having it
263 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000264
Chris Lattner8cd0e932008-03-05 18:54:05 +0000265 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenekbc965a62008-02-18 22:57:02 +0000266
Ted Kremenekaee121c2008-02-13 23:08:21 +0000267 APSInt V1(bits, false);
268 APSInt V2 = V1;
269
Ted Kremenek07baa252008-02-21 18:02:17 +0000270 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000271
272 CaseStmt* Case = cast<CaseStmt>(I.getCase());
273
274 // Evaluate the case.
275 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
276 assert (false && "Case condition must evaluate to an integer constant.");
277 return;
278 }
279
280 // Get the RHS of the case, if it exists.
281
282 if (Expr* E = Case->getRHS()) {
283 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
284 assert (false &&
285 "Case condition (RHS) must evaluate to an integer constant.");
286 return ;
287 }
288
289 assert (V1 <= V2);
290 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000291 else
292 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000293
294 // FIXME: Eventually we should replace the logic below with a range
295 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000296 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000297
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000298 do {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000299 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000300
Ted Kremenek07baa252008-02-21 18:02:17 +0000301 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000302
303 // Now "assume" that the case matches.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000304
Ted Kremenekd4676512008-03-12 21:45:47 +0000305 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000306 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000307
308 if (isFeasible) {
309 builder.generateCaseStmtNode(I, StNew);
310
311 // If CondV evaluates to a constant, then we know that this
312 // is the *only* case that we can take, so stop evaluating the
313 // others.
314 if (isa<nonlval::ConcreteInt>(CondV))
315 return;
316 }
317
318 // Now "assume" that the case doesn't match. Add this state
319 // to the default state (if it is feasible).
320
Ted Kremenekd4676512008-03-12 21:45:47 +0000321 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000322 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000323
324 if (isFeasible)
325 DefaultSt = StNew;
326
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000327 // Concretize the next value in the range.
328 if (V1 == V2)
329 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000330
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000331 ++V1;
Ted Kremenek539269c2008-03-17 22:18:22 +0000332 assert (V1 <= V2);
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000333
334 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000335 }
336
337 // If we reach here, than we know that the default branch is
338 // possible.
339 builder.generateDefaultCaseNode(DefaultSt);
340}
341
342
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000343void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000344 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000345
Ted Kremenek99ecce72008-02-26 19:05:15 +0000346 assert (B->getOpcode() == BinaryOperator::LAnd ||
347 B->getOpcode() == BinaryOperator::LOr);
348
349 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
350
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000351 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000352 RVal X = GetBlkExprRVal(St, B);
353
Ted Kremenekb31af242008-02-28 09:25:22 +0000354 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000355
Ted Kremenekb31af242008-02-28 09:25:22 +0000356 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000357
358 assert (Ex);
359
360 if (Ex == B->getRHS()) {
361
362 X = GetBlkExprRVal(St, Ex);
363
Ted Kremenekb31af242008-02-28 09:25:22 +0000364 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000365
Ted Kremenekb31af242008-02-28 09:25:22 +0000366 if (X.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000367 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000368 return;
369 }
370
Ted Kremenek99ecce72008-02-26 19:05:15 +0000371 // We took the RHS. Because the value of the '&&' or '||' expression must
372 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
373 // or 1. Alternatively, we could take a lazy approach, and calculate this
374 // value later when necessary. We don't have the machinery in place for
375 // this right now, and since most logical expressions are used for branches,
376 // the payoff is not likely to be large. Instead, we do eager evaluation.
377
378 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000379 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000380
381 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000382 MakeNode(Dst, B, Pred,
383 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000384
385 isFeasible = false;
386 NewState = Assume(St, X, false, isFeasible);
387
388 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000389 MakeNode(Dst, B, Pred,
390 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000391 }
392 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000393 // We took the LHS expression. Depending on whether we are '&&' or
394 // '||' we know what the value of the expression is via properties of
395 // the short-circuiting.
396
397 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenekf10f2882008-03-21 21:30:14 +0000398 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000399 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000400}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000401
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000402
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000403void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000404
Ted Kremenek07baa252008-02-21 18:02:17 +0000405 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000406 StmtEntryNode = builder.getLastNode();
407 CurrentStmt = S;
408 NodeSet Dst;
Ted Kremenek02331462008-03-09 18:28:41 +0000409
410 // Create the cleaned state.
411
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000412 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
413 CurrentStmt, Liveness);
Ted Kremeneka1d070b2008-03-10 04:45:00 +0000414
415 Builder->SetCleanedState(CleanedState);
Ted Kremenek02331462008-03-09 18:28:41 +0000416
417 // Visit the statement.
Ted Kremenekf031b872008-01-23 19:59:44 +0000418
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 Kremenek50ef5ff2008-03-10 04:11:42 +0000424 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode)
425 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
Ted Kremeneka57214f2008-01-18 00:41:32 +0000426
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000427 // NULL out these variables to cleanup.
Ted Kremenek07baa252008-02-21 18:02:17 +0000428
Ted Kremenekf031b872008-01-23 19:59:44 +0000429 CurrentStmt = NULL;
430 StmtEntryNode = NULL;
431 Builder = NULL;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000432 CleanedState = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000433}
434
Ted Kremenek6d409922008-02-13 18:06:44 +0000435void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000436
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000437 if (D != CurrentStmt) {
438 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
439 return;
440 }
441
442 // If we are here, we are loading the value of the decl and binding
443 // it to the block-level expression.
444
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000445 ValueState* St = GetState(Pred);
Ted Kremenekf97c6682008-03-09 03:30:59 +0000446 RVal X = RVal::MakeVal(BasicVals, D);
447 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenekf10f2882008-03-21 21:30:14 +0000448 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000449}
450
Ted Kremenekd9268e32008-02-19 01:44:53 +0000451void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000452 CallExpr::arg_iterator AI,
453 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000454 NodeSet& Dst) {
455
Ted Kremenek07baa252008-02-21 18:02:17 +0000456 // Process the arguments.
457
458 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000459
Ted Kremenekef7ea072008-03-04 00:56:45 +0000460 NodeSet DstTmp;
Ted Kremenek769f3482008-03-04 22:01:56 +0000461 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +0000462 ++AI;
463
Ted Kremenek769f3482008-03-04 22:01:56 +0000464 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenek07baa252008-02-21 18:02:17 +0000465 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000466
467 return;
468 }
469
470 // If we reach here we have processed all of the arguments. Evaluate
471 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000472
Ted Kremenekc71901d2008-02-25 21:16:03 +0000473 NodeSet DstTmp;
474 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000475
Ted Kremenekc71901d2008-02-25 21:16:03 +0000476 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000477
478 if (DstTmp.empty())
479 DstTmp.Add(Pred);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000480
481 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000482 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
483
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000484 ValueState* St = GetState(*DI);
Ted Kremenekc71901d2008-02-25 21:16:03 +0000485 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000486
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000487 // FIXME: Add support for symbolic function calls (calls involving
488 // function pointer values that are symbolic).
489
490 // Check for undefined control-flow or calls to NULL.
491
Ted Kremenek43863eb2008-02-29 23:14:48 +0000492 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000493 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000494
Ted Kremenek9b31f5b2008-02-29 23:53:11 +0000495 if (N) {
496 N->markAsSink();
497 BadCalls.insert(N);
498 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000499
Ted Kremenekd9268e32008-02-19 01:44:53 +0000500 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +0000501 }
502
503 // Check for the "noreturn" attribute.
504
505 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
506
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000507 if (isa<lval::FuncVal>(L)) {
508
509 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
510
511 if (FD->getAttr<NoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +0000512 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000513 else {
514 // HACK: Some functions are not marked noreturn, and don't return.
515 // Here are a few hardwired ones. If this takes too long, we can
516 // potentially cache these results.
517 const char* s = FD->getIdentifier()->getName();
518 unsigned n = strlen(s);
519
520 switch (n) {
521 default:
522 break;
Ted Kremenek550025b2008-03-14 23:25:49 +0000523
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000524 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +0000525 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
526 break;
527
528 case 5:
529 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
530 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000531 }
532 }
533 }
Ted Kremenekb451dd32008-03-05 21:15:02 +0000534
535 // Evaluate the call.
536
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000537
Ted Kremenek769f3482008-03-04 22:01:56 +0000538 bool invalidateArgs = false;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000539
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000540 if (L.isUnknown()) {
Ted Kremenek769f3482008-03-04 22:01:56 +0000541 // Check for an "unknown" callee.
542 invalidateArgs = true;
543 }
544 else if (isa<lval::FuncVal>(L)) {
545
546 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
547
Ted Kremenek21581c62008-03-05 22:59:42 +0000548 if (unsigned id = Info->getBuiltinID()) {
549 switch (id) {
550 case Builtin::BI__builtin_expect: {
551 // For __builtin_expect, just return the value of the subexpression.
552 assert (CE->arg_begin() != CE->arg_end());
553 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenekf10f2882008-03-21 21:30:14 +0000554 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +0000555 continue;
556 }
557
558 default:
559 invalidateArgs = true;
560 break;
561 }
562 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000563 }
564
565 if (invalidateArgs) {
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000566 // Invalidate all arguments passed in by reference (LVals).
567 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
568 I != E; ++I) {
569 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000570
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000571 if (isa<LVal>(V))
572 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenekb451dd32008-03-05 21:15:02 +0000573 }
574
Ted Kremenekf10f2882008-03-21 21:30:14 +0000575 MakeNode(Dst, CE, *DI, St);
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000576 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000577 else {
578
579 // Check any arguments passed-by-value against being undefined.
580
581 bool badArg = false;
582
583 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
584 I != E; ++I) {
585
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000586 if (GetRVal(GetState(*DI), *I).isUndef()) {
587 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000588
589 if (N) {
590 N->markAsSink();
591 UndefArgs[N] = *I;
592 }
593
594 badArg = true;
595 break;
596 }
597 }
598
599 if (badArg)
600 continue;
601
602 // Dispatch to the plug-in transfer function.
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000603
604 unsigned size = Dst.size();
605
606 EvalCall(Dst, CE, cast<LVal>(L), *DI);
607
Ted Kremeneka2822eb2008-03-05 22:49:16 +0000608 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000609 MakeNode(Dst, CE, *DI, St);
Ted Kremenek769f3482008-03-04 22:01:56 +0000610 }
Ted Kremenekd9268e32008-02-19 01:44:53 +0000611 }
612}
613
Ted Kremenek07baa252008-02-21 18:02:17 +0000614void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000615
Ted Kremenek5f585b02008-02-19 18:52:54 +0000616 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +0000617 QualType T = CastE->getType();
618
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000619 if (T->isReferenceType())
620 VisitLVal(Ex, Pred, S1);
621 else
622 Visit(Ex, Pred, S1);
623
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000624 // Check for redundant casts or casting to "void"
625 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000626 Ex->getType() == T ||
627 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000628
Ted Kremenek07baa252008-02-21 18:02:17 +0000629 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000630 Dst.Add(*I1);
631
Ted Kremenek54eddae2008-01-24 02:02:54 +0000632 return;
633 }
634
Ted Kremenek07baa252008-02-21 18:02:17 +0000635 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000636 NodeTy* N = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000637 ValueState* St = GetState(N);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000638
639 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
640
Ted Kremenekf10f2882008-03-21 21:30:14 +0000641 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000642 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000643}
644
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000645void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000646 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000647
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000648 ValueState* St = GetState(Pred);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000649
650 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000651 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000652
653 // FIXME: Add support for local arrays.
654 if (VD->getType()->isArrayType())
655 continue;
656
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000657 const Expr* Ex = VD->getInit();
Ted Kremenek07baa252008-02-21 18:02:17 +0000658
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000659 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
660
661 // In this context, Static => Local variable.
662
663 assert (!VD->getStorageClass() == VarDecl::Static ||
664 !isa<FileVarDecl>(VD));
665
666 // If there is no initializer, set the value of the
Ted Kremenekb31af242008-02-28 09:25:22 +0000667 // variable to "Undefined".
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000668 //
669 // FIXME: static variables may have an initializer, but the second
670 // time a function is called those values may not be current.
Ted Kremenek98d093b2008-03-04 20:40:11 +0000671
672 QualType T = VD->getType();
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000673
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000674 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000675
676 // C99: 6.7.8 Initialization
677 // If an object that has static storage duration is not initialized
678 // explicitly, then:
679 // —if it has pointer type, it is initialized to a null pointer;
680 // —if it has arithmetic type, it is initialized to (positive or
681 // unsigned) zero;
682
Ted Kremenek98d093b2008-03-04 20:40:11 +0000683 // FIXME: Handle structs. Now we treat their values as unknown.
684
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000685 if (T->isPointerType()) {
686
687 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000688 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000689 }
690 else if (T->isIntegerType()) {
691
692 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000693 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000694 }
695
Ted Kremenek98d093b2008-03-04 20:40:11 +0000696
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000697 }
Ted Kremenek98d093b2008-03-04 20:40:11 +0000698 else {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000699
Ted Kremenek98d093b2008-03-04 20:40:11 +0000700 // FIXME: Handle structs. Now we treat them as unknown. What
701 // we need to do is treat their members as unknown.
702
703 if (T->isPointerType() || T->isIntegerType())
704 St = SetRVal(St, lval::DeclVal(VD),
705 Ex ? GetRVal(St, Ex) : UndefinedVal());
706 }
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000707 }
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000708 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000709
Ted Kremenekf10f2882008-03-21 21:30:14 +0000710 MakeNode(Dst, DS, Pred, St);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000711}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000712
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000713
Ted Kremenek07baa252008-02-21 18:02:17 +0000714void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000715 NodeTy* Pred, NodeSet& Dst) {
716
Ted Kremenek99ecce72008-02-26 19:05:15 +0000717 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
718
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000719 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000720 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000721
Ted Kremenekb31af242008-02-28 09:25:22 +0000722 assert (X.isUndef());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000723
Ted Kremenekb31af242008-02-28 09:25:22 +0000724 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000725
726 assert (SE);
727
728 X = GetBlkExprRVal(St, SE);
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000729
730 // Make sure that we invalidate the previous binding.
Ted Kremenekf10f2882008-03-21 21:30:14 +0000731 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000732}
733
Ted Kremenekfd85f292008-02-12 19:49:57 +0000734/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000735void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
736 NodeTy* Pred,
737 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000738
Ted Kremenek07baa252008-02-21 18:02:17 +0000739 QualType T = Ex->getArgumentType();
Ted Kremenekc3b12832008-03-15 03:13:20 +0000740 uint64_t amt;
741
742 if (Ex->isSizeOf()) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000743
Ted Kremenekc3b12832008-03-15 03:13:20 +0000744 // FIXME: Add support for VLAs.
745 if (!T.getTypePtr()->isConstantSizeType())
746 return;
747
748 amt = 1; // Handle sizeof(void)
749
750 if (T != getContext().VoidTy)
751 amt = getContext().getTypeSize(T) / 8;
752
753 }
754 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +0000755 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000756
Ted Kremenekf10f2882008-03-21 21:30:14 +0000757 MakeNode(Dst, Ex, Pred,
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000758 SetRVal(GetState(Pred), Ex,
Ted Kremenekc3b12832008-03-15 03:13:20 +0000759 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000760}
761
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000762void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
763 NodeSet& Dst, bool GetLVal) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000764
Ted Kremenek07baa252008-02-21 18:02:17 +0000765 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000766
767 NodeSet DstTmp;
768
Ted Kremenek56a89992008-02-26 03:44:25 +0000769 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000770 DstTmp.Add(Pred);
771 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000772 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000773
Ted Kremenek07baa252008-02-21 18:02:17 +0000774 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000775
776 NodeTy* N = *I;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000777 ValueState* St = GetState(N);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000778
779 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
780
Ted Kremenek07baa252008-02-21 18:02:17 +0000781 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000782
Ted Kremenekb31af242008-02-28 09:25:22 +0000783 // Check for dereferences of undefined values.
Ted Kremenek07baa252008-02-21 18:02:17 +0000784
Ted Kremenekb31af242008-02-28 09:25:22 +0000785 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000786
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000787 NodeTy* Succ = Builder->generateNode(U, St, N);
788
789 if (Succ) {
790 Succ->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000791 UndefDeref.insert(Succ);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000792 }
793
794 continue;
795 }
796
Ted Kremenek07baa252008-02-21 18:02:17 +0000797 // Check for dereferences of unknown values. Treat as No-Ops.
798
799 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000800 Dst.Add(N);
801 continue;
802 }
803
804 // After a dereference, one of two possible situations arise:
805 // (1) A crash, because the pointer was NULL.
806 // (2) The pointer is not NULL, and the dereference works.
807 //
808 // We add these assumptions.
809
Ted Kremenek07baa252008-02-21 18:02:17 +0000810 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000811 bool isFeasibleNotNull;
812
813 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000814
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000815 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000816
817 if (isFeasibleNotNull) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000818
Ted Kremenekf10f2882008-03-21 21:30:14 +0000819 if (GetLVal) MakeNode(Dst, U, N, SetRVal(StNotNull, U, LV));
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000820 else {
821
822 // FIXME: Currently symbolic analysis "generates" new symbols
823 // for the contents of values. We need a better approach.
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000824
Ted Kremenekf10f2882008-03-21 21:30:14 +0000825 MakeNode(Dst, U, N, SetRVal(StNotNull, U,
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000826 GetRVal(StNotNull, LV, U->getType())));
827 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000828 }
829
830 bool isFeasibleNull;
831
Ted Kremenek07baa252008-02-21 18:02:17 +0000832 // Now "assume" that the pointer is NULL.
833
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000834 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000835
836 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000837
Ted Kremenekf10f2882008-03-21 21:30:14 +0000838 // We don't use "MakeNode" here because the node will be a sink
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000839 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000840
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000841 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
842
843 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000844
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000845 NullNode->markAsSink();
846
Ted Kremenek07baa252008-02-21 18:02:17 +0000847 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
848 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000849 }
850 }
851 }
852}
853
Ted Kremenek07baa252008-02-21 18:02:17 +0000854void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
855 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000856
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000857 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000858
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000859 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000860 assert (U->getOpcode() != UnaryOperator::SizeOf);
861 assert (U->getOpcode() != UnaryOperator::AlignOf);
862
863 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000864
865 switch (U->getOpcode()) {
866 case UnaryOperator::PostInc:
867 case UnaryOperator::PostDec:
868 case UnaryOperator::PreInc:
869 case UnaryOperator::PreDec:
870 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000871 // Evalue subexpression as an LVal.
872 use_GetLVal = true;
873 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000874 break;
875
876 default:
877 Visit(U->getSubExpr(), Pred, S1);
878 break;
879 }
880
Ted Kremenek07baa252008-02-21 18:02:17 +0000881 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000882
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000883 NodeTy* N1 = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000884 ValueState* St = GetState(N1);
Ted Kremenek07baa252008-02-21 18:02:17 +0000885
886 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
887 GetRVal(St, U->getSubExpr());
888
889 if (SubV.isUnknown()) {
890 Dst.Add(N1);
891 continue;
892 }
893
Ted Kremenekb31af242008-02-28 09:25:22 +0000894 if (SubV.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000895 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenek07baa252008-02-21 18:02:17 +0000896 continue;
897 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000898
Ted Kremenek22640ce2008-02-15 22:09:30 +0000899 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000900
901 // Handle ++ and -- (both pre- and post-increment).
902
Ted Kremenek07baa252008-02-21 18:02:17 +0000903 LVal SubLV = cast<LVal>(SubV);
904 RVal V = GetRVal(St, SubLV, U->getType());
905
Ted Kremenekb8782e12008-02-21 19:15:37 +0000906 if (V.isUnknown()) {
907 Dst.Add(N1);
908 continue;
909 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000910
Ted Kremenekb31af242008-02-28 09:25:22 +0000911 // Propagate undefined values.
912 if (V.isUndef()) {
Ted Kremenekf10f2882008-03-21 21:30:14 +0000913 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenek07baa252008-02-21 18:02:17 +0000914 continue;
915 }
916
Ted Kremenekb1669d42008-02-21 19:29:23 +0000917 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000918
919 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
920 : BinaryOperator::Sub;
921
Ted Kremenekb1669d42008-02-21 19:29:23 +0000922 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000923
924 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000925 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000926 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000927 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000928
Ted Kremenekf10f2882008-03-21 21:30:14 +0000929 MakeNode(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000930 continue;
931 }
932
933 // Handle all other unary operators.
934
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000935 switch (U->getOpcode()) {
Ted Kremenek3c536072008-03-15 03:05:30 +0000936
937 case UnaryOperator::Extension:
938 St = SetRVal(St, U, SubV);
939 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000940
Ted Kremenek07baa252008-02-21 18:02:17 +0000941 case UnaryOperator::Minus:
942 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000943 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000944
Ted Kremenek07baa252008-02-21 18:02:17 +0000945 case UnaryOperator::Not:
946 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000947 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000948
Ted Kremenek07baa252008-02-21 18:02:17 +0000949 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000950
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000951 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
952 //
953 // Note: technically we do "E == 0", but this is the same in the
954 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000955
956 if (isa<LVal>(SubV)) {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000957 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenek07baa252008-02-21 18:02:17 +0000958 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
959 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000960 }
961 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000962 Expr* Ex = U->getSubExpr();
Ted Kremenek8ad19872008-03-07 20:13:31 +0000963 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000964 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
965 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000966 }
967
968 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000969
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000970 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000971 assert (isa<LVal>(SubV));
972 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000973 break;
974 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000975
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000976 default: ;
977 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000978 }
979
Ted Kremenekf10f2882008-03-21 21:30:14 +0000980 MakeNode(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000981 }
982}
983
Ted Kremenek07baa252008-02-21 18:02:17 +0000984void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
985 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000986
Ted Kremenek07baa252008-02-21 18:02:17 +0000987 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000988
Ted Kremenek07baa252008-02-21 18:02:17 +0000989 // FIXME: Add support for VLAs.
990 if (!T.getTypePtr()->isConstantSizeType())
991 return;
992
Chris Lattner8cd0e932008-03-05 18:54:05 +0000993 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000994 ValueState* St = GetState(Pred);
Ted Kremenek8ad19872008-03-07 20:13:31 +0000995 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000996
Ted Kremenekf10f2882008-03-21 21:30:14 +0000997 MakeNode(Dst, U, Pred, St);
Ted Kremenek07baa252008-02-21 18:02:17 +0000998}
999
1000void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneka129a2e2008-02-27 07:04:16 +00001001
1002 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1003 Dst.Add(Pred);
1004 return;
1005 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001006
1007 Ex = Ex->IgnoreParens();
1008
Ted Kremenekb5175bf2008-02-27 06:47:26 +00001009 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001010 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001011 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001012 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001013
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001014 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex))
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001015 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001016 VisitDeref(U, Pred, Dst, true);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001017 return;
1018 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001019
Ted Kremenek07baa252008-02-21 18:02:17 +00001020 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001021}
1022
Ted Kremenek31803c32008-03-17 21:11:24 +00001023void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1024 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1025}
1026
1027void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1028 AsmStmt::outputs_iterator I,
1029 AsmStmt::outputs_iterator E,
1030 NodeTy* Pred, NodeSet& Dst) {
1031 if (I == E) {
1032 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1033 return;
1034 }
1035
1036 NodeSet Tmp;
1037 VisitLVal(*I, Pred, Tmp);
1038
1039 ++I;
1040
1041 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1042 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1043}
1044
1045void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1046 AsmStmt::inputs_iterator I,
1047 AsmStmt::inputs_iterator E,
1048 NodeTy* Pred, NodeSet& Dst) {
1049 if (I == E) {
1050
1051 // We have processed both the inputs and the outputs. All of the outputs
1052 // should evaluate to LVals. Nuke all of their values.
1053
1054 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1055 // which interprets the inline asm and stores proper results in the
1056 // outputs.
1057
1058 ValueState* St = GetState(Pred);
1059
1060 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1061 OE = A->end_outputs(); OI != OE; ++OI) {
1062
1063 RVal X = GetLVal(St, *OI);
1064
1065 assert (!isa<NonLVal>(X));
1066
1067 if (isa<LVal>(X))
1068 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1069 }
1070
Ted Kremenekf10f2882008-03-21 21:30:14 +00001071 MakeNode(Dst, A, Pred, St);
Ted Kremenek31803c32008-03-17 21:11:24 +00001072 return;
1073 }
1074
1075 NodeSet Tmp;
1076 Visit(*I, Pred, Tmp);
1077
1078 ++I;
1079
1080 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1081 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1082}
1083
1084
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001085void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1086 NodeSet& Dst){
Ted Kremenek71507472008-03-25 16:07:41 +00001087 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), Pred, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001088}
1089
Ted Kremenek71507472008-03-25 16:07:41 +00001090void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001091 ObjCMessageExpr::arg_iterator AI,
1092 ObjCMessageExpr::arg_iterator AE,
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001093 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001094 if (AI == AE) {
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001095
1096 // Process the receiver.
1097
Ted Kremenek71507472008-03-25 16:07:41 +00001098 if (Expr* Receiver = ME->getReceiver()) {
1099 NodeSet Tmp;
Ted Kremenek2380f372008-03-26 21:36:08 +00001100 Visit(Receiver, Pred, Tmp);
Ted Kremenek71507472008-03-25 16:07:41 +00001101
1102 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1103 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001104
Ted Kremenek71507472008-03-25 16:07:41 +00001105 return;
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001106 }
Ted Kremenek71507472008-03-25 16:07:41 +00001107
1108 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001109 return;
1110 }
1111
1112 NodeSet Tmp;
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001113 Visit(*AI, Pred, Tmp);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001114
Ted Kremenek4b2bdd52008-03-25 02:10:28 +00001115 ++AI;
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001116
1117 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenek71507472008-03-25 16:07:41 +00001118 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001119}
1120
Ted Kremenek71507472008-03-25 16:07:41 +00001121void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1122 NodeTy* Pred, NodeSet& Dst) {
1123
1124
1125 // FIXME: More logic for the processing the method call.
1126
1127 ValueState* St = GetState(Pred);
1128
1129 if (Expr* Receiver = ME->getReceiver()) {
1130
Ted Kremenek9ba5a1f2008-03-26 22:21:58 +00001131 RVal L = GetRVal(St, Receiver);
Ted Kremenek71507472008-03-25 16:07:41 +00001132
1133 // Check for undefined control-flow or calls to NULL.
1134
1135 if (L.isUndef()) {
1136 NodeTy* N = Builder->generateNode(ME, St, Pred);
1137
1138 if (N) {
1139 N->markAsSink();
1140 UndefReceivers.insert(N);
1141 }
1142
1143 return;
1144 }
1145 }
1146
1147 // Check for any arguments that are uninitialized/undefined.
1148
1149 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1150 I != E; ++I) {
1151
1152 if (GetRVal(St, *I).isUndef()) {
1153
1154 NodeTy* N = Builder->generateNode(ME, St, Pred);
1155
1156 if (N) {
1157 N->markAsSink();
1158 MsgExprUndefArgs[N] = *I;
1159 }
1160
1161 return;
1162 }
1163 }
1164
1165 // FIXME: Eventually we will properly handle the effects of a message
1166 // expr. For now invalidate all arguments passed in by references.
1167
1168 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1169 I != E; ++I) {
1170
1171 RVal V = GetRVal(St, *I);
1172
1173 if (isa<LVal>(V))
1174 St = SetRVal(St, cast<LVal>(V), UnknownVal());
1175 }
1176
1177 MakeNode(Dst, ME, Pred, St);
1178}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001179
1180
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001181void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00001182 GRExprEngine::NodeTy* Pred,
1183 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001184 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001185
1186 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00001187 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001188 else
1189 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001190
Ted Kremenekf031b872008-01-23 19:59:44 +00001191 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001192
Ted Kremenekf031b872008-01-23 19:59:44 +00001193 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +00001194
Ted Kremenekf031b872008-01-23 19:59:44 +00001195 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +00001196 // In such cases, we want to (initially) treat the LHS as an LVal,
1197 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1198 // evaluated to LValDecl's instead of to an NonLVal.
1199
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001200 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1201 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +00001202
Ted Kremenek07baa252008-02-21 18:02:17 +00001203 // Visit the RHS...
1204
1205 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +00001206 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001207
1208 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +00001209
Ted Kremenek07baa252008-02-21 18:02:17 +00001210 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +00001211
Ted Kremenekf031b872008-01-23 19:59:44 +00001212 NodeTy* N2 = *I2;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001213 ValueState* St = GetState(N2);
Ted Kremenek2c369792008-02-25 18:42:54 +00001214 Expr* RHS = B->getRHS();
1215 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +00001216
Ted Kremenek15cb0782008-02-06 22:50:25 +00001217 BinaryOperator::Opcode Op = B->getOpcode();
1218
Ted Kremenek2c369792008-02-25 18:42:54 +00001219 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1220 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +00001221
Ted Kremenekb31af242008-02-28 09:25:22 +00001222 // Check if the denominator is undefined.
Ted Kremenek2c369792008-02-25 18:42:54 +00001223
Ted Kremenek5a600862008-02-26 22:27:51 +00001224 if (!RightV.isUnknown()) {
1225
Ted Kremenekb31af242008-02-28 09:25:22 +00001226 if (RightV.isUndef()) {
1227 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek5a600862008-02-26 22:27:51 +00001228
Ted Kremenekb31af242008-02-28 09:25:22 +00001229 if (DivUndef) {
1230 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001231 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek5a600862008-02-26 22:27:51 +00001232 }
1233
1234 continue;
1235 }
1236
1237 // Check for divide/remainder-by-zero.
1238 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001239 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001240
Ted Kremenek75f32c62008-03-07 19:04:53 +00001241 bool isFeasibleZero = false;
1242 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001243
Ted Kremenek5a600862008-02-26 22:27:51 +00001244 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001245
Ted Kremenek75f32c62008-03-07 19:04:53 +00001246 bool isFeasibleNotZero = false;
1247 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001248
Ted Kremenek75f32c62008-03-07 19:04:53 +00001249 // Create the node for the divide-by-zero (if it occurred).
1250
1251 if (isFeasibleZero)
1252 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1253 DivZeroNode->markAsSink();
1254
1255 if (isFeasibleNotZero)
1256 ImplicitBadDivides.insert(DivZeroNode);
1257 else
1258 ExplicitBadDivides.insert(DivZeroNode);
1259
1260 }
1261
1262 if (!isFeasibleNotZero)
Ted Kremenek5a600862008-02-26 22:27:51 +00001263 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001264 }
1265
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001266 // Fall-through. The logic below processes the divide.
1267 }
1268
Ted Kremenekc2d07202008-02-28 20:32:03 +00001269
Ted Kremenek15cb0782008-02-06 22:50:25 +00001270 if (Op <= BinaryOperator::Or) {
1271
Ted Kremenek07baa252008-02-21 18:02:17 +00001272 // Process non-assignements except commas or short-circuited
1273 // logical expressions (LAnd and LOr).
1274
1275 RVal Result = EvalBinOp(Op, LeftV, RightV);
1276
1277 if (Result.isUnknown()) {
1278 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001279 continue;
1280 }
1281
Ted Kremenekc2d07202008-02-28 20:32:03 +00001282 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1283
1284 // The operands were not undefined, but the result is undefined.
1285
1286 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1287 UndefNode->markAsSink();
1288 UndefResults.insert(UndefNode);
1289 }
1290
1291 continue;
1292 }
1293
Ted Kremenekf10f2882008-03-21 21:30:14 +00001294 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001295 continue;
1296 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001297
1298 // Process assignments.
1299
1300 switch (Op) {
1301
Ted Kremenekf031b872008-01-23 19:59:44 +00001302 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001303
1304 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001305
Ted Kremenekb31af242008-02-28 09:25:22 +00001306 if (LeftV.isUndef()) {
1307 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001308 continue;
1309 }
1310
Ted Kremenekd4676512008-03-12 21:45:47 +00001311 // EXPERIMENTAL: "Conjured" symbols.
1312
1313 if (RightV.isUnknown()) {
1314 unsigned Count = Builder->getCurrentBlockCount();
1315 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1316
1317 RightV = B->getRHS()->getType()->isPointerType()
1318 ? cast<RVal>(lval::SymbolVal(Sym))
1319 : cast<RVal>(nonlval::SymbolVal(Sym));
1320 }
1321
1322 // Even if the LHS evaluates to an unknown L-Value, the entire
1323 // expression still evaluates to the RHS.
1324
Ted Kremenek07baa252008-02-21 18:02:17 +00001325 if (LeftV.isUnknown()) {
1326 St = SetRVal(St, B, RightV);
1327 break;
1328 }
Ted Kremenekd4676512008-03-12 21:45:47 +00001329
1330 // Simulate the effects of a "store": bind the value of the RHS
1331 // to the L-Value represented by the LHS.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001332
Ted Kremenek07baa252008-02-21 18:02:17 +00001333 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001334 break;
1335 }
1336
Ted Kremenek07baa252008-02-21 18:02:17 +00001337 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001338
Ted Kremenek07baa252008-02-21 18:02:17 +00001339 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001340
Ted Kremenek5a600862008-02-26 22:27:51 +00001341 assert (B->isCompoundAssignmentOp());
1342
1343 if (Op >= BinaryOperator::AndAssign)
1344 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1345 else
1346 ((int&) Op) -= BinaryOperator::MulAssign;
1347
Ted Kremenekb31af242008-02-28 09:25:22 +00001348 // Check if the LHS is undefined.
Ted Kremenek07baa252008-02-21 18:02:17 +00001349
Ted Kremenekb31af242008-02-28 09:25:22 +00001350 if (LeftV.isUndef()) {
1351 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001352 continue;
1353 }
1354
1355 if (LeftV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001356 assert (isa<UnknownVal>(GetRVal(St, B)));
1357 Dst.Add(N2);
1358 continue;
Ted Kremenekbf988d02008-02-19 00:22:37 +00001359 }
1360
Ted Kremenek07baa252008-02-21 18:02:17 +00001361 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001362 // that is neither "Unknown" or "Undefined."
Ted Kremenek07baa252008-02-21 18:02:17 +00001363
1364 LVal LeftLV = cast<LVal>(LeftV);
1365
Ted Kremenek07baa252008-02-21 18:02:17 +00001366 // Fetch the value of the LHS (the value of the variable, etc.).
1367
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001368 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenek07baa252008-02-21 18:02:17 +00001369
Ted Kremenekb31af242008-02-28 09:25:22 +00001370 // Propagate undefined value (left-side). We
1371 // propogate undefined values for the RHS below when
Ted Kremenek5a600862008-02-26 22:27:51 +00001372 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001373
Ted Kremenekb31af242008-02-28 09:25:22 +00001374 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001375 St = SetRVal(St, B, V);
1376 break;
1377 }
1378
1379 // Propagate unknown values.
1380
Ted Kremenekb8782e12008-02-21 19:15:37 +00001381 if (V.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001382 // The value bound to LeftV is unknown. Thus we just
1383 // propagate the current node (as "B" is already bound to nothing).
1384 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenekb8782e12008-02-21 19:15:37 +00001385 Dst.Add(N2);
1386 continue;
1387 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001388
1389 if (RightV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001390 assert (isa<UnknownVal>(GetRVal(St, B)));
1391 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenek07baa252008-02-21 18:02:17 +00001392 break;
1393 }
1394
Ted Kremenek5a600862008-02-26 22:27:51 +00001395 // At this point:
1396 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001397 // The LHS is not Undef/Unknown.
Ted Kremenek5a600862008-02-26 22:27:51 +00001398 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001399
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001400 // Get the computation type.
1401 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1402
1403 // Perform promotions.
1404 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001405 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001406
1407 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001408
Ted Kremenek2c369792008-02-25 18:42:54 +00001409 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1410 && RHS->getType()->isIntegerType()) {
1411
Ted Kremenekb31af242008-02-28 09:25:22 +00001412 // Check if the denominator is undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001413
Ted Kremenekb31af242008-02-28 09:25:22 +00001414 if (RightV.isUndef()) {
1415 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001416
Ted Kremenekb31af242008-02-28 09:25:22 +00001417 if (DivUndef) {
1418 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001419 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001420 }
1421
1422 continue;
1423 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001424
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001425 // First, "assume" that the denominator is 0.
1426
Ted Kremenek75f32c62008-03-07 19:04:53 +00001427 bool isFeasibleZero = false;
1428 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001429
Ted Kremenek75f32c62008-03-07 19:04:53 +00001430 // Second, "assume" that the denominator cannot be 0.
1431
1432 bool isFeasibleNotZero = false;
1433 St = Assume(St, RightV, true, isFeasibleNotZero);
1434
1435 // Create the node for the divide-by-zero error (if it occurred).
1436
1437 if (isFeasibleZero) {
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001438 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1439
1440 if (DivZeroNode) {
1441 DivZeroNode->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001442
1443 if (isFeasibleNotZero)
1444 ImplicitBadDivides.insert(DivZeroNode);
1445 else
1446 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001447 }
1448 }
1449
Ted Kremenek75f32c62008-03-07 19:04:53 +00001450 if (!isFeasibleNotZero)
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001451 continue;
1452
1453 // Fall-through. The logic below processes the divide.
1454 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001455 else {
1456
Ted Kremenekb31af242008-02-28 09:25:22 +00001457 // Propagate undefined values (right-side).
Ted Kremenek5a600862008-02-26 22:27:51 +00001458
Ted Kremenekb31af242008-02-28 09:25:22 +00001459 if (RightV.isUndef()) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001460 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1461 break;
1462 }
1463
1464 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001465
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001466 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekc2d07202008-02-28 20:32:03 +00001467
1468 if (Result.isUndef()) {
1469
1470 // The operands were not undefined, but the result is undefined.
1471
1472 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1473 UndefNode->markAsSink();
1474 UndefResults.insert(UndefNode);
1475 }
1476
1477 continue;
1478 }
1479
Ted Kremenek07baa252008-02-21 18:02:17 +00001480 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001481 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001482 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001483
Ted Kremenekf10f2882008-03-21 21:30:14 +00001484 MakeNode(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001485 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001486 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001487}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001488
Ted Kremenekb31af242008-02-28 09:25:22 +00001489void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001490 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001491 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +00001492 UndefStores.insert(N);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001493}
Ted Kremenekbe962452008-01-16 19:42:59 +00001494
Ted Kremenekbf988d02008-02-19 00:22:37 +00001495void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001496
1497 // FIXME: add metadata to the CFG so that we can disable
1498 // this check when we KNOW that there is no block-level subexpression.
1499 // The motivation is that this check requires a hashtable lookup.
1500
1501 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1502 Dst.Add(Pred);
1503 return;
1504 }
1505
1506 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001507
1508 default:
1509 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001510 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001511
1512 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1513 break;
Ted Kremenek52b8d0e2008-03-17 21:31:48 +00001514
1515 case Stmt::AsmStmtClass:
1516 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
1517 break;
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001518
Ted Kremenek744a7862008-02-08 20:29:23 +00001519 case Stmt::BinaryOperatorClass: {
1520 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001521
Ted Kremenek744a7862008-02-08 20:29:23 +00001522 if (B->isLogicalOp()) {
1523 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001524 break;
1525 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001526 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001527 ValueState* St = GetState(Pred);
Ted Kremenekf10f2882008-03-21 21:30:14 +00001528 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001529 break;
1530 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001531
1532 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1533 break;
1534 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001535
1536 case Stmt::CallExprClass: {
1537 CallExpr* C = cast<CallExpr>(S);
1538 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1539 break;
1540 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001541
1542 case Stmt::CastExprClass: {
1543 CastExpr* C = cast<CastExpr>(S);
1544 VisitCast(C, C->getSubExpr(), Pred, Dst);
1545 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001546 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001547
Ted Kremenek07baa252008-02-21 18:02:17 +00001548 // FIXME: ChooseExpr is really a constant. We need to fix
1549 // the CFG do not model them as explicit control-flow.
1550
Ted Kremenekfd85f292008-02-12 19:49:57 +00001551 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1552 ChooseExpr* C = cast<ChooseExpr>(S);
1553 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1554 break;
1555 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001556
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001557 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001558 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1559 break;
1560
Ted Kremenekfd85f292008-02-12 19:49:57 +00001561 case Stmt::ConditionalOperatorClass: { // '?' operator
1562 ConditionalOperator* C = cast<ConditionalOperator>(S);
1563 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1564 break;
1565 }
1566
1567 case Stmt::DeclRefExprClass:
1568 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1569 break;
1570
1571 case Stmt::DeclStmtClass:
1572 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1573 break;
1574
1575 case Stmt::ImplicitCastExprClass: {
1576 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1577 VisitCast(C, C->getSubExpr(), Pred, Dst);
1578 break;
1579 }
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00001580
1581 case Stmt::ObjCMessageExprClass: {
1582 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
1583 break;
1584 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001585
1586 case Stmt::ParenExprClass:
1587 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1588 break;
1589
1590 case Stmt::SizeOfAlignOfTypeExprClass:
1591 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1592 break;
1593
Ted Kremenek106f37c2008-02-08 07:05:39 +00001594 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001595 StmtExpr* SE = cast<StmtExpr>(S);
1596
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001597 ValueState* St = GetState(Pred);
Ted Kremeneka0a7c412008-03-15 03:27:30 +00001598
1599 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
1600 // probably just remove these from the CFG.
1601 assert (!SE->getSubStmt()->body_empty());
1602
1603 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Ted Kremenekf10f2882008-03-21 21:30:14 +00001604 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremeneka0a7c412008-03-15 03:27:30 +00001605 else
1606 Dst.Add(Pred);
1607
1608 break;
Ted Kremenek106f37c2008-02-08 07:05:39 +00001609 }
1610
Ted Kremenek07baa252008-02-21 18:02:17 +00001611 // FIXME: We may wish to always bind state to ReturnStmts so
1612 // that users can quickly query what was the state at the
1613 // exit points of a function.
1614
Ted Kremenekfd85f292008-02-12 19:49:57 +00001615 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001616 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1617 Visit(R, Pred, Dst);
1618 else
1619 Dst.Add(Pred);
1620
1621 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001622 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001623
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001624 case Stmt::UnaryOperatorClass: {
1625 UnaryOperator* U = cast<UnaryOperator>(S);
1626
Ted Kremenek07baa252008-02-21 18:02:17 +00001627 switch (U->getOpcode()) {
1628 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1629 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1630 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1631 default: VisitUnaryOperator(U, Pred, Dst); break;
1632 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001633
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001634 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001635 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001636 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001637}
1638
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001639//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001640// "Assume" logic.
1641//===----------------------------------------------------------------------===//
1642
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001643ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001644 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001645 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001646 switch (Cond.getSubKind()) {
1647 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001648 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001649 return St;
1650
Ted Kremenek13f31562008-02-06 00:54:14 +00001651 case lval::SymbolValKind:
1652 if (Assumption)
1653 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001654 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001655 else
1656 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001657 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001658
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001659
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001660 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001661 case lval::FuncValKind:
1662 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001663 isFeasible = Assumption;
1664 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001665
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001666 case lval::ConcreteIntKind: {
1667 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001668 isFeasible = b ? Assumption : !Assumption;
1669 return St;
1670 }
1671 }
Ted Kremenek90960972008-01-30 23:03:39 +00001672}
1673
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001674ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001675 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001676 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001677 switch (Cond.getSubKind()) {
1678 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001679 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001680 return St;
1681
Ted Kremenekab359c12008-02-06 17:32:17 +00001682
1683 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001684 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001685 SymbolID sym = SV.getSymbol();
1686
1687 if (Assumption)
Ted Kremenek8ad19872008-03-07 20:13:31 +00001688 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001689 isFeasible);
1690 else
Ted Kremenek8ad19872008-03-07 20:13:31 +00001691 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001692 isFeasible);
1693 }
1694
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001695 case nonlval::SymIntConstraintValKind:
1696 return
1697 AssumeSymInt(St, Assumption,
1698 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1699 isFeasible);
1700
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001701 case nonlval::ConcreteIntKind: {
1702 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001703 isFeasible = b ? Assumption : !Assumption;
1704 return St;
1705 }
1706 }
1707}
1708
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001709ValueState*
1710GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001711 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001712
Ted Kremenek13f31562008-02-06 00:54:14 +00001713 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001714 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001715 isFeasible = *X != V;
1716 return St;
1717 }
1718
1719 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001720 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001721 isFeasible = true;
1722 return St;
1723 }
1724
1725 // If we reach here, sym is not a constant and we don't know if it is != V.
1726 // Make that assumption.
1727
1728 isFeasible = true;
1729 return StateMgr.AddNE(St, sym, V);
1730}
1731
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001732ValueState*
1733GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001734 const llvm::APSInt& V, bool& isFeasible) {
1735
1736 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001737 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001738 isFeasible = *X == V;
1739 return St;
1740 }
1741
1742 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001743 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001744 isFeasible = false;
1745 return St;
1746 }
1747
1748 // If we reach here, sym is not a constant and we don't know if it is == V.
1749 // Make that assumption.
1750
1751 isFeasible = true;
1752 return StateMgr.AddEQ(St, sym, V);
1753}
Ted Kremenek90960972008-01-30 23:03:39 +00001754
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001755ValueState*
1756GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001757 const SymIntConstraint& C, bool& isFeasible) {
1758
1759 switch (C.getOpcode()) {
1760 default:
1761 // No logic yet for other operators.
Ted Kremenekd4676512008-03-12 21:45:47 +00001762 isFeasible = true;
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001763 return St;
1764
1765 case BinaryOperator::EQ:
1766 if (Assumption)
1767 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1768 else
1769 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1770
1771 case BinaryOperator::NE:
1772 if (Assumption)
1773 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1774 else
1775 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1776 }
1777}
1778
Ted Kremenek90960972008-01-30 23:03:39 +00001779//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001780// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001781//===----------------------------------------------------------------------===//
1782
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001783#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001784static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001785static SourceManager* GraphPrintSourceManager;
Ted Kremenek9f597922008-03-11 19:02:40 +00001786static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001787
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001788namespace llvm {
1789template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001790struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001791 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001792
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001793 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001794
1795 Out << "Variables:\\l";
1796
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001797 bool isFirst = true;
1798
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001799 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001800
1801 if (isFirst)
1802 isFirst = false;
1803 else
1804 Out << "\\l";
1805
1806 Out << ' ' << I.getKey()->getName() << " : ";
1807 I.getData().print(Out);
1808 }
1809
1810 }
1811
Ted Kremenek17c5f112008-02-11 19:21:59 +00001812
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001813 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001814
1815 bool isFirst = true;
1816
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001817 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001818
1819 if (isFirst) {
1820 Out << "\\l\\lSub-Expressions:\\l";
1821 isFirst = false;
1822 }
1823 else
1824 Out << "\\l";
1825
1826 Out << " (" << (void*) I.getKey() << ") ";
1827 I.getKey()->printPretty(Out);
1828 Out << " : ";
1829 I.getData().print(Out);
1830 }
1831 }
1832
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001833 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001834
Ted Kremenek08cfd832008-02-08 21:10:02 +00001835 bool isFirst = true;
1836
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001837 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001838 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001839 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001840 isFirst = false;
1841 }
1842 else
1843 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001844
Ted Kremenek17c5f112008-02-11 19:21:59 +00001845 Out << " (" << (void*) I.getKey() << ") ";
1846 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001847 Out << " : ";
1848 I.getData().print(Out);
1849 }
1850 }
1851
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001852 static void PrintEQ(std::ostream& Out, ValueState* St) {
1853 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001854
1855 if (CE.isEmpty())
1856 return;
1857
1858 Out << "\\l\\|'==' constraints:";
1859
Ted Kremenek07baa252008-02-21 18:02:17 +00001860 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001861 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1862 }
1863
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001864 static void PrintNE(std::ostream& Out, ValueState* St) {
1865 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001866
1867 if (NE.isEmpty())
1868 return;
1869
1870 Out << "\\l\\|'!=' constraints:";
1871
Ted Kremenek07baa252008-02-21 18:02:17 +00001872 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001873 I != EI; ++I){
1874
1875 Out << "\\l $" << I.getKey() << " : ";
1876 bool isFirst = true;
1877
1878 ValueState::IntSetTy::iterator J=I.getData().begin(),
1879 EJ=I.getData().end();
1880 for ( ; J != EJ; ++J) {
1881 if (isFirst) isFirst = false;
1882 else Out << ", ";
1883
1884 Out << (*J)->toString();
1885 }
1886 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001887 }
1888
1889 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1890
1891 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001892 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00001893 GraphPrintCheckerState->isUndefDeref(N) ||
1894 GraphPrintCheckerState->isUndefStore(N) ||
1895 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00001896 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1897 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00001898 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001899 GraphPrintCheckerState->isBadCall(N) ||
1900 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001901 return "color=\"red\",style=\"filled\"";
1902
Ted Kremenekc2d07202008-02-28 20:32:03 +00001903 if (GraphPrintCheckerState->isNoReturnCall(N))
1904 return "color=\"blue\",style=\"filled\"";
1905
Ted Kremeneka853de62008-02-14 22:54:53 +00001906 return "";
1907 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001908
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001909 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001910 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001911
1912 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001913 ProgramPoint Loc = N->getLocation();
1914
1915 switch (Loc.getKind()) {
1916 case ProgramPoint::BlockEntranceKind:
1917 Out << "Block Entrance: B"
1918 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1919 break;
1920
1921 case ProgramPoint::BlockExitKind:
1922 assert (false);
1923 break;
1924
1925 case ProgramPoint::PostStmtKind: {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001926 const PostStmt& L = cast<PostStmt>(Loc);
1927 Stmt* S = L.getStmt();
1928 SourceLocation SLoc = S->getLocStart();
1929
1930 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
1931 S->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001932
Ted Kremenekf97c6682008-03-09 03:30:59 +00001933 if (SLoc.isFileID()) {
1934 Out << "\\lline="
1935 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1936 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
1937 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001938
Ted Kremenek43863eb2008-02-29 23:14:48 +00001939 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenek80d52d02008-02-07 05:48:01 +00001940 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001941 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001942 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001943 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001944 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001945 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001946 Out << "\\|Store to Undefined LVal.";
Ted Kremenek75f32c62008-03-07 19:04:53 +00001947 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1948 Out << "\\|Explicit divide-by zero or undefined value.";
1949 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
1950 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001951 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenekc2d07202008-02-28 20:32:03 +00001952 Out << "\\|Result of operation is undefined.";
Ted Kremenekc2d07202008-02-28 20:32:03 +00001953 else if (GraphPrintCheckerState->isNoReturnCall(N))
1954 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001955 else if (GraphPrintCheckerState->isBadCall(N))
1956 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001957 else if (GraphPrintCheckerState->isUndefArg(N))
1958 Out << "\\|Argument in call is undefined";
Ted Kremenek80d52d02008-02-07 05:48:01 +00001959
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001960 break;
1961 }
1962
1963 default: {
1964 const BlockEdge& E = cast<BlockEdge>(Loc);
1965 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1966 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001967
1968 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001969
1970 SourceLocation SLoc = T->getLocStart();
1971
Ted Kremenek90960972008-01-30 23:03:39 +00001972 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001973
Ted Kremenek90960972008-01-30 23:03:39 +00001974 E.getSrc()->printTerminator(Out);
1975
Ted Kremenekf97c6682008-03-09 03:30:59 +00001976 if (SLoc.isFileID()) {
1977 Out << "\\lline="
1978 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1979 << GraphPrintSourceManager->getColumnNumber(SLoc);
1980 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001981
Ted Kremenekaee121c2008-02-13 23:08:21 +00001982 if (isa<SwitchStmt>(T)) {
1983 Stmt* Label = E.getDst()->getLabel();
1984
1985 if (Label) {
1986 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1987 Out << "\\lcase ";
1988 C->getLHS()->printPretty(Out);
1989
1990 if (Stmt* RHS = C->getRHS()) {
1991 Out << " .. ";
1992 RHS->printPretty(Out);
1993 }
1994
1995 Out << ":";
1996 }
1997 else {
1998 assert (isa<DefaultStmt>(Label));
1999 Out << "\\ldefault:";
2000 }
2001 }
2002 else
2003 Out << "\\l(implicit) default:";
2004 }
2005 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00002006 // FIXME
2007 }
2008 else {
2009 Out << "\\lCondition: ";
2010 if (*E.getSrc()->succ_begin() == E.getDst())
2011 Out << "true";
2012 else
2013 Out << "false";
2014 }
2015
2016 Out << "\\l";
2017 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00002018
Ted Kremenekb31af242008-02-28 09:25:22 +00002019 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2020 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00002021 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002022 }
2023 }
2024
Ted Kremenekf4b49df2008-02-28 10:21:43 +00002025 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00002026
Ted Kremenek9f597922008-03-11 19:02:40 +00002027 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00002028
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00002029 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002030 return Out.str();
2031 }
2032};
2033} // end llvm namespace
2034#endif
2035
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002036#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002037
2038template <typename ITERATOR>
2039GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2040
2041template <>
2042GRExprEngine::NodeTy*
2043GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2044 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2045 return I->first;
2046}
2047
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002048template <typename ITERATOR>
2049static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources,
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002050 ITERATOR I, ITERATOR E) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002051
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002052 llvm::SmallPtrSet<void*,10> CachedSources;
2053
2054 for ( ; I != E; ++I ) {
2055 GRExprEngine::NodeTy* N = GetGraphNode(I);
2056 void* p = N->getLocation().getRawData();
2057
2058 if (CachedSources.count(p))
2059 continue;
2060
2061 CachedSources.insert(p);
2062
2063 Sources.push_back(N);
2064 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002065}
2066#endif
2067
2068void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00002069#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002070 if (trim) {
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002071 llvm::SmallVector<NodeTy*, 10> Src;
2072 AddSources(Src, null_derefs_begin(), null_derefs_end());
2073 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2074 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2075 AddSources(Src, undef_results_begin(), undef_results_end());
2076 AddSources(Src, bad_calls_begin(), bad_calls_end());
2077 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek2f0c0e12008-03-14 18:14:50 +00002078 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002079
Ted Kremenek83f04aa2008-03-12 17:18:20 +00002080 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002081 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00002082 else {
2083 GraphPrintCheckerState = this;
2084 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00002085 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremeneke44a8302008-03-11 18:25:33 +00002086
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002087 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00002088
2089 GraphPrintCheckerState = NULL;
2090 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00002091 GraphCheckerStatePrinter = NULL;
Ted Kremeneke44a8302008-03-11 18:25:33 +00002092 }
2093#endif
2094}
2095
2096void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2097#ifndef NDEBUG
2098 GraphPrintCheckerState = this;
2099 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00002100 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002101
Ted Kremeneke44a8302008-03-11 18:25:33 +00002102 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2103
2104 if (!TrimmedG)
2105 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2106 else {
2107 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2108 delete TrimmedG;
2109 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00002110
Ted Kremenek428d39e2008-01-30 23:24:39 +00002111 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00002112 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00002113 GraphCheckerStatePrinter = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00002114#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002115}