blob: 87c3bd74a0a95a8a51e9509458f7d2eb18f4a7bf [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek50df4f42008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000017#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000018
Ted Kremenek9f6b1612008-02-27 06:07:00 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#include <sstream>
22#endif
23
Ted Kremenekb451dd32008-03-05 21:15:02 +000024// SaveAndRestore - A utility class that uses RIIA to save and restore
25// the value of a variable.
26template<typename T>
27struct VISIBILITY_HIDDEN SaveAndRestore {
28 SaveAndRestore(T& x) : X(x), old_value(x) {}
29 ~SaveAndRestore() { X = old_value; }
30 T get() { return old_value; }
31
32 T& X;
33 T old_value;
34};
35
Ted Kremenekd4467432008-02-14 22:16:04 +000036using namespace clang;
37using llvm::dyn_cast;
38using llvm::cast;
39using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000040
Ted Kremenekf4b49df2008-02-28 10:21:43 +000041ValueState* GRExprEngine::getInitialState() {
Ted Kremenekb5175bf2008-02-27 06:47:26 +000042
43 // The LiveVariables information already has a compilation of all VarDecls
44 // used in the function. Iterate through this set, and "symbolicate"
45 // any VarDecl whose value originally comes from outside the function.
46
47 typedef LiveVariables::AnalysisDataTy LVDataTy;
48 LVDataTy& D = Liveness.getAnalysisData();
49
Ted Kremenekf4b49df2008-02-28 10:21:43 +000050 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenekb5175bf2008-02-27 06:47:26 +000051
52 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
53
54 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
55
56 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
57 RVal X = RVal::GetSymbolValue(SymMgr, VD);
58 StateMgr.BindVar(StateImpl, VD, X);
59 }
60 }
61
62 return StateMgr.getPersistentState(StateImpl);
63}
64
Ted Kremenekf4b49df2008-02-28 10:21:43 +000065ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000066
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000067 if (!StateCleaned) {
68 St = RemoveDeadBindings(CurrentStmt, St);
69 StateCleaned = true;
70 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000071
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000072 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000073
Ted Kremenek07baa252008-02-21 18:02:17 +000074 if (Ex == CurrentStmt) {
75 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000076
77 if (!isBlkExpr)
78 return St;
79 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000080
Ted Kremenekad5d5c52008-02-26 23:37:01 +000081 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000082}
83
Ted Kremenekf4b49df2008-02-28 10:21:43 +000084ValueState* GRExprEngine::SetRVal(ValueState* St, LVal LV, RVal RV) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000085
86 if (!StateCleaned) {
87 St = RemoveDeadBindings(CurrentStmt, St);
88 StateCleaned = true;
89 }
90
Ted Kremenek07baa252008-02-21 18:02:17 +000091 return StateMgr.SetRVal(St, LV, RV);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000092}
93
Ted Kremenekf4b49df2008-02-28 10:21:43 +000094ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
95 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +000096
97 switch (Terminator->getStmtClass()) {
98 default:
99 return St;
100
101 case Stmt::BinaryOperatorClass: { // '&&' and '||'
102
103 BinaryOperator* B = cast<BinaryOperator>(Terminator);
104 BinaryOperator::Opcode Op = B->getOpcode();
105
106 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
107
108 // For &&, if we take the true branch, then the value of the whole
109 // expression is that of the RHS expression.
110 //
111 // For ||, if we take the false branch, then the value of the whole
112 // expression is that of the RHS expression.
113
114 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
115 (Op == BinaryOperator::LOr && !branchTaken)
116 ? B->getRHS() : B->getLHS();
117
Ted Kremenekb31af242008-02-28 09:25:22 +0000118 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000119 }
120
121 case Stmt::ConditionalOperatorClass: { // ?:
122
123 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
124
125 // For ?, if branchTaken == true then the value is either the LHS or
126 // the condition itself. (GNU extension).
127
128 Expr* Ex;
129
130 if (branchTaken)
131 Ex = C->getLHS() ? C->getLHS() : C->getCond();
132 else
133 Ex = C->getRHS();
134
Ted Kremenekb31af242008-02-28 09:25:22 +0000135 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000136 }
137
138 case Stmt::ChooseExprClass: { // ?:
139
140 ChooseExpr* C = cast<ChooseExpr>(Terminator);
141
142 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenekb31af242008-02-28 09:25:22 +0000143 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000144 }
145 }
146}
147
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000148bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
149 GRBlockCounter BC) {
150
151 return BC.getNumVisited(B->getBlockID()) < 3;
152}
153
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000154void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000155 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000156
Ted Kremenek17c5f112008-02-11 19:21:59 +0000157 // Remove old bindings for subexpressions.
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000158 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000159
Ted Kremenek022b6052008-02-15 22:29:00 +0000160 // Check for NULL conditions; e.g. "for(;;)"
161 if (!Condition) {
162 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000163 return;
164 }
165
Ted Kremenek07baa252008-02-21 18:02:17 +0000166 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000167
168 switch (V.getBaseKind()) {
169 default:
170 break;
171
Ted Kremenek07baa252008-02-21 18:02:17 +0000172 case RVal::UnknownKind:
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000173 builder.generateNode(MarkBranch(PrevState, Term, true), true);
174 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000175 return;
176
Ted Kremenekb31af242008-02-28 09:25:22 +0000177 case RVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000178 NodeTy* N = builder.generateNode(PrevState, true);
179
180 if (N) {
181 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000182 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000183 }
184
185 builder.markInfeasible(false);
186 return;
187 }
188 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000189
Ted Kremenek90960972008-01-30 23:03:39 +0000190
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000191 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000192
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000193 bool isFeasible = true;
194 ValueState* St = Assume(PrevState, V, true, isFeasible);
195
196 if (isFeasible)
197 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000198 else
199 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000200
201 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000202
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000203 isFeasible = false;
204 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000205
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000206 if (isFeasible)
207 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000208 else
209 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000210}
211
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000212/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000213/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000214void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000215
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000216 ValueState* St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000217 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000218
219 // Three possibilities:
220 //
221 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000222 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000223 // (3) We have no clue about the label. Dispatch to all targets.
224 //
225
226 typedef IndirectGotoNodeBuilder::iterator iterator;
227
228 if (isa<lval::GotoLabel>(V)) {
229 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
230
231 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000232 if (I.getLabel() == L) {
233 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000234 return;
235 }
236 }
237
238 assert (false && "No block with label.");
239 return;
240 }
241
Ted Kremenekb31af242008-02-28 09:25:22 +0000242 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000243 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000244 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000245 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000246 return;
247 }
248
249 // This is really a catch-all. We don't support symbolics yet.
250
Ted Kremenek07baa252008-02-21 18:02:17 +0000251 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000252
253 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000254 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000255}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000256
Ted Kremenekaee121c2008-02-13 23:08:21 +0000257/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
258/// nodes by processing the 'effects' of a switch statement.
259void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
260
261 typedef SwitchNodeBuilder::iterator iterator;
262
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000263 ValueState* St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000264 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000265 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000266
Ted Kremenekb31af242008-02-28 09:25:22 +0000267 if (CondV.isUndef()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000268 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000269 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000270 return;
271 }
272
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000273 ValueState* DefaultSt = St;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000274
275 // While most of this can be assumed (such as the signedness), having it
276 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000277
Chris Lattner8cd0e932008-03-05 18:54:05 +0000278 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenekbc965a62008-02-18 22:57:02 +0000279
Ted Kremenekaee121c2008-02-13 23:08:21 +0000280 APSInt V1(bits, false);
281 APSInt V2 = V1;
282
Ted Kremenek07baa252008-02-21 18:02:17 +0000283 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000284
285 CaseStmt* Case = cast<CaseStmt>(I.getCase());
286
287 // Evaluate the case.
288 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
289 assert (false && "Case condition must evaluate to an integer constant.");
290 return;
291 }
292
293 // Get the RHS of the case, if it exists.
294
295 if (Expr* E = Case->getRHS()) {
296 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
297 assert (false &&
298 "Case condition (RHS) must evaluate to an integer constant.");
299 return ;
300 }
301
302 assert (V1 <= V2);
303 }
304 else V2 = V1;
305
306 // FIXME: Eventually we should replace the logic below with a range
307 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000308 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000309
310 do {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000311 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000312
Ted Kremenek07baa252008-02-21 18:02:17 +0000313 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000314
315 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000316 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000317
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000318 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000319
320 if (isFeasible) {
321 builder.generateCaseStmtNode(I, StNew);
322
323 // If CondV evaluates to a constant, then we know that this
324 // is the *only* case that we can take, so stop evaluating the
325 // others.
326 if (isa<nonlval::ConcreteInt>(CondV))
327 return;
328 }
329
330 // Now "assume" that the case doesn't match. Add this state
331 // to the default state (if it is feasible).
332
Ted Kremenekb1934132008-02-14 19:37:24 +0000333 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000334
335 if (isFeasible)
336 DefaultSt = StNew;
337
338 // Concretize the next value in the range.
339 ++V1;
340
341 } while (V1 < V2);
342 }
343
344 // If we reach here, than we know that the default branch is
345 // possible.
346 builder.generateDefaultCaseNode(DefaultSt);
347}
348
349
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000350void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000351 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000352
Ted Kremenek99ecce72008-02-26 19:05:15 +0000353 assert (B->getOpcode() == BinaryOperator::LAnd ||
354 B->getOpcode() == BinaryOperator::LOr);
355
356 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
357
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000358 ValueState* St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000359 RVal X = GetBlkExprRVal(St, B);
360
Ted Kremenekb31af242008-02-28 09:25:22 +0000361 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000362
Ted Kremenekb31af242008-02-28 09:25:22 +0000363 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000364
365 assert (Ex);
366
367 if (Ex == B->getRHS()) {
368
369 X = GetBlkExprRVal(St, Ex);
370
Ted Kremenekb31af242008-02-28 09:25:22 +0000371 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000372
Ted Kremenekb31af242008-02-28 09:25:22 +0000373 if (X.isUndef()) {
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000374 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
375 return;
376 }
377
Ted Kremenek99ecce72008-02-26 19:05:15 +0000378 // We took the RHS. Because the value of the '&&' or '||' expression must
379 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
380 // or 1. Alternatively, we could take a lazy approach, and calculate this
381 // value later when necessary. We don't have the machinery in place for
382 // this right now, and since most logical expressions are used for branches,
383 // the payoff is not likely to be large. Instead, we do eager evaluation.
384
385 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000386 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000387
388 if (isFeasible)
389 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
390
391 isFeasible = false;
392 NewState = Assume(St, X, false, isFeasible);
393
394 if (isFeasible)
395 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000396 }
397 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000398 // We took the LHS expression. Depending on whether we are '&&' or
399 // '||' we know what the value of the expression is via properties of
400 // the short-circuiting.
401
402 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
403 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000404 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000405}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000406
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000407
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000408void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000409
Ted Kremenek07baa252008-02-21 18:02:17 +0000410 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000411 StmtEntryNode = builder.getLastNode();
412 CurrentStmt = S;
413 NodeSet Dst;
414 StateCleaned = false;
415
416 Visit(S, StmtEntryNode, Dst);
417
418 // If no nodes were generated, generate a new node that has all the
419 // dead mappings removed.
Ted Kremenek07baa252008-02-21 18:02:17 +0000420
Ted Kremenekf031b872008-01-23 19:59:44 +0000421 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000422 ValueState* St = RemoveDeadBindings(S, StmtEntryNode->getState());
Ted Kremenekf031b872008-01-23 19:59:44 +0000423 builder.generateNode(S, St, StmtEntryNode);
424 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000425
Ted Kremenek07baa252008-02-21 18:02:17 +0000426 // For safety, NULL out these variables.
427
Ted Kremenekf031b872008-01-23 19:59:44 +0000428 CurrentStmt = NULL;
429 StmtEntryNode = NULL;
430 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000431}
432
Ted Kremenek6d409922008-02-13 18:06:44 +0000433void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000434
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000435 if (D != CurrentStmt) {
436 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
437 return;
438 }
439
440 // If we are here, we are loading the value of the decl and binding
441 // it to the block-level expression.
442
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000443 ValueState* St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000444 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000445}
446
Ted Kremenekd9268e32008-02-19 01:44:53 +0000447void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000448 CallExpr::arg_iterator AI,
449 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000450 NodeSet& Dst) {
451
Ted Kremenek07baa252008-02-21 18:02:17 +0000452 // Process the arguments.
453
454 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000455
Ted Kremenekef7ea072008-03-04 00:56:45 +0000456 NodeSet DstTmp;
Ted Kremenek769f3482008-03-04 22:01:56 +0000457 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +0000458 ++AI;
459
Ted Kremenek769f3482008-03-04 22:01:56 +0000460 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenek07baa252008-02-21 18:02:17 +0000461 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000462
463 return;
464 }
465
466 // If we reach here we have processed all of the arguments. Evaluate
467 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000468
Ted Kremenekc71901d2008-02-25 21:16:03 +0000469 NodeSet DstTmp;
470 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000471
Ted Kremenekc71901d2008-02-25 21:16:03 +0000472 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000473
474 if (DstTmp.empty())
475 DstTmp.Add(Pred);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000476
477 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000478 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
479
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000480 ValueState* St = (*DI)->getState();
Ted Kremenekc71901d2008-02-25 21:16:03 +0000481 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000482
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000483 // FIXME: Add support for symbolic function calls (calls involving
484 // function pointer values that are symbolic).
485
486 // Check for undefined control-flow or calls to NULL.
487
Ted Kremenek43863eb2008-02-29 23:14:48 +0000488 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000489 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000490
Ted Kremenek9b31f5b2008-02-29 23:53:11 +0000491 if (N) {
492 N->markAsSink();
493 BadCalls.insert(N);
494 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000495
Ted Kremenekd9268e32008-02-19 01:44:53 +0000496 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +0000497 }
498
499 // Check for the "noreturn" attribute.
500
501 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
502
503 if (isa<lval::FuncVal>(L))
Ted Kremeneka2822eb2008-03-05 22:49:16 +0000504 if (cast<lval::FuncVal>(L).getDecl()->getAttr<NoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +0000505 Builder->BuildSinks = true;
Ted Kremenekb451dd32008-03-05 21:15:02 +0000506
507 // Evaluate the call.
508
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000509
Ted Kremenek769f3482008-03-04 22:01:56 +0000510 bool invalidateArgs = false;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000511
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000512 if (L.isUnknown()) {
Ted Kremenek769f3482008-03-04 22:01:56 +0000513 // Check for an "unknown" callee.
514 invalidateArgs = true;
515 }
516 else if (isa<lval::FuncVal>(L)) {
517
518 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
519
Ted Kremenek21581c62008-03-05 22:59:42 +0000520 if (unsigned id = Info->getBuiltinID()) {
521 switch (id) {
522 case Builtin::BI__builtin_expect: {
523 // For __builtin_expect, just return the value of the subexpression.
524 assert (CE->arg_begin() != CE->arg_end());
525 RVal X = GetRVal(St, *(CE->arg_begin()));
526 Nodify(Dst, CE, *DI, SetRVal(St, CE, X));
527 continue;
528 }
529
530 default:
531 invalidateArgs = true;
532 break;
533 }
534 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000535 }
536
537 if (invalidateArgs) {
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000538 // Invalidate all arguments passed in by reference (LVals).
539 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
540 I != E; ++I) {
541 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000542
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000543 if (isa<LVal>(V))
544 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenekb451dd32008-03-05 21:15:02 +0000545 }
546
547 Nodify(Dst, CE, *DI, St);
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000548 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000549 else {
550
551 // Check any arguments passed-by-value against being undefined.
552
553 bool badArg = false;
554
555 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
556 I != E; ++I) {
557
558 if (GetRVal((*DI)->getState(), *I).isUndef()) {
559 NodeTy* N = Builder->generateNode(CE, (*DI)->getState(), *DI);
560
561 if (N) {
562 N->markAsSink();
563 UndefArgs[N] = *I;
564 }
565
566 badArg = true;
567 break;
568 }
569 }
570
571 if (badArg)
572 continue;
573
574 // Dispatch to the plug-in transfer function.
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000575
576 unsigned size = Dst.size();
577
578 EvalCall(Dst, CE, cast<LVal>(L), *DI);
579
Ted Kremeneka2822eb2008-03-05 22:49:16 +0000580 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000581 Nodify(Dst, CE, *DI, St);
Ted Kremenek769f3482008-03-04 22:01:56 +0000582 }
Ted Kremenekd9268e32008-02-19 01:44:53 +0000583 }
584}
585
Ted Kremenek07baa252008-02-21 18:02:17 +0000586void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000587
Ted Kremenek5f585b02008-02-19 18:52:54 +0000588 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +0000589 QualType T = CastE->getType();
590
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000591 if (T->isReferenceType())
592 VisitLVal(Ex, Pred, S1);
593 else
594 Visit(Ex, Pred, S1);
595
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000596 // Check for redundant casts or casting to "void"
597 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000598 Ex->getType() == T ||
599 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000600
Ted Kremenek07baa252008-02-21 18:02:17 +0000601 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000602 Dst.Add(*I1);
603
Ted Kremenek54eddae2008-01-24 02:02:54 +0000604 return;
605 }
606
Ted Kremenek07baa252008-02-21 18:02:17 +0000607 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000608 NodeTy* N = *I1;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000609 ValueState* St = N->getState();
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000610
611 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
612
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000613 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000614 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000615}
616
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000617void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000618 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000619
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000620 ValueState* St = Pred->getState();
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000621
622 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000623 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000624
625 // FIXME: Add support for local arrays.
626 if (VD->getType()->isArrayType())
627 continue;
628
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000629 const Expr* Ex = VD->getInit();
Ted Kremenek07baa252008-02-21 18:02:17 +0000630
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000631 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
632
633 // In this context, Static => Local variable.
634
635 assert (!VD->getStorageClass() == VarDecl::Static ||
636 !isa<FileVarDecl>(VD));
637
638 // If there is no initializer, set the value of the
Ted Kremenekb31af242008-02-28 09:25:22 +0000639 // variable to "Undefined".
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000640 //
641 // FIXME: static variables may have an initializer, but the second
642 // time a function is called those values may not be current.
Ted Kremenek98d093b2008-03-04 20:40:11 +0000643
644 QualType T = VD->getType();
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000645
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000646 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000647
648 // C99: 6.7.8 Initialization
649 // If an object that has static storage duration is not initialized
650 // explicitly, then:
651 // —if it has pointer type, it is initialized to a null pointer;
652 // —if it has arithmetic type, it is initialized to (positive or
653 // unsigned) zero;
654
Ted Kremenek98d093b2008-03-04 20:40:11 +0000655 // FIXME: Handle structs. Now we treat their values as unknown.
656
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000657 if (T->isPointerType()) {
658
659 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000660 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000661 }
662 else if (T->isIntegerType()) {
663
664 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000665 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000666 }
667
Ted Kremenek98d093b2008-03-04 20:40:11 +0000668
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000669 }
Ted Kremenek98d093b2008-03-04 20:40:11 +0000670 else {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000671
Ted Kremenek98d093b2008-03-04 20:40:11 +0000672 // FIXME: Handle structs. Now we treat them as unknown. What
673 // we need to do is treat their members as unknown.
674
675 if (T->isPointerType() || T->isIntegerType())
676 St = SetRVal(St, lval::DeclVal(VD),
677 Ex ? GetRVal(St, Ex) : UndefinedVal());
678 }
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000679 }
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000680 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000681
682 Nodify(Dst, DS, Pred, St);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000683}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000684
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000685
Ted Kremenek07baa252008-02-21 18:02:17 +0000686void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000687 NodeTy* Pred, NodeSet& Dst) {
688
Ted Kremenek99ecce72008-02-26 19:05:15 +0000689 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
690
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000691 ValueState* St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000692 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000693
Ted Kremenekb31af242008-02-28 09:25:22 +0000694 assert (X.isUndef());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000695
Ted Kremenekb31af242008-02-28 09:25:22 +0000696 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000697
698 assert (SE);
699
700 X = GetBlkExprRVal(St, SE);
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000701
702 // Make sure that we invalidate the previous binding.
703 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000704}
705
Ted Kremenekfd85f292008-02-12 19:49:57 +0000706/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000707void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
708 NodeTy* Pred,
709 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000710
711 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000712
713 // 6.5.3.4 sizeof: "The result type is an integer."
714
Ted Kremenek07baa252008-02-21 18:02:17 +0000715 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000716
Ted Kremenek07baa252008-02-21 18:02:17 +0000717
Ted Kremenek571f5192008-02-21 18:15:29 +0000718 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000719 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000720 return;
721
Ted Kremenek571f5192008-02-21 18:15:29 +0000722
723 uint64_t size = 1; // Handle sizeof(void)
724
Chris Lattner8cd0e932008-03-05 18:54:05 +0000725 if (T != getContext().VoidTy)
726 size = getContext().getTypeSize(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000727
Ted Kremenek07baa252008-02-21 18:02:17 +0000728 Nodify(Dst, Ex, Pred,
729 SetRVal(Pred->getState(), Ex,
Ted Kremenek8ad19872008-03-07 20:13:31 +0000730 NonLVal::MakeVal(BasicVals, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000731
732}
733
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000734void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
735
Ted Kremenek07baa252008-02-21 18:02:17 +0000736 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000737
738 NodeSet DstTmp;
739
Ted Kremenek56a89992008-02-26 03:44:25 +0000740 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000741 DstTmp.Add(Pred);
742 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000743 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000744
Ted Kremenek07baa252008-02-21 18:02:17 +0000745 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000746
747 NodeTy* N = *I;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000748 ValueState* St = N->getState();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000749
750 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
751
Ted Kremenek07baa252008-02-21 18:02:17 +0000752 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000753
Ted Kremenekb31af242008-02-28 09:25:22 +0000754 // Check for dereferences of undefined values.
Ted Kremenek07baa252008-02-21 18:02:17 +0000755
Ted Kremenekb31af242008-02-28 09:25:22 +0000756 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000757
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000758 NodeTy* Succ = Builder->generateNode(U, St, N);
759
760 if (Succ) {
761 Succ->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000762 UndefDeref.insert(Succ);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000763 }
764
765 continue;
766 }
767
Ted Kremenek07baa252008-02-21 18:02:17 +0000768 // Check for dereferences of unknown values. Treat as No-Ops.
769
770 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000771 Dst.Add(N);
772 continue;
773 }
774
775 // After a dereference, one of two possible situations arise:
776 // (1) A crash, because the pointer was NULL.
777 // (2) The pointer is not NULL, and the dereference works.
778 //
779 // We add these assumptions.
780
Ted Kremenek07baa252008-02-21 18:02:17 +0000781 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000782 bool isFeasibleNotNull;
783
784 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000785
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000786 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000787
788 if (isFeasibleNotNull) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000789
790 // FIXME: Currently symbolic analysis "generates" new symbols
791 // for the contents of values. We need a better approach.
792
Ted Kremenek07baa252008-02-21 18:02:17 +0000793 Nodify(Dst, U, N, SetRVal(StNotNull, U,
794 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000795 }
796
797 bool isFeasibleNull;
798
Ted Kremenek07baa252008-02-21 18:02:17 +0000799 // Now "assume" that the pointer is NULL.
800
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000801 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000802
803 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000804
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000805 // We don't use "Nodify" here because the node will be a sink
806 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000807
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000808 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
809
810 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000811
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000812 NullNode->markAsSink();
813
Ted Kremenek07baa252008-02-21 18:02:17 +0000814 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
815 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000816 }
817 }
818 }
819}
820
Ted Kremenek07baa252008-02-21 18:02:17 +0000821void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
822 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000823
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000824 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000825
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000826 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000827 assert (U->getOpcode() != UnaryOperator::SizeOf);
828 assert (U->getOpcode() != UnaryOperator::AlignOf);
829
830 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000831
832 switch (U->getOpcode()) {
833 case UnaryOperator::PostInc:
834 case UnaryOperator::PostDec:
835 case UnaryOperator::PreInc:
836 case UnaryOperator::PreDec:
837 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000838 // Evalue subexpression as an LVal.
839 use_GetLVal = true;
840 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000841 break;
842
843 default:
844 Visit(U->getSubExpr(), Pred, S1);
845 break;
846 }
847
Ted Kremenek07baa252008-02-21 18:02:17 +0000848 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000849
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000850 NodeTy* N1 = *I1;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000851 ValueState* St = N1->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000852
853 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
854 GetRVal(St, U->getSubExpr());
855
856 if (SubV.isUnknown()) {
857 Dst.Add(N1);
858 continue;
859 }
860
Ted Kremenekb31af242008-02-28 09:25:22 +0000861 if (SubV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000862 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
863 continue;
864 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000865
Ted Kremenek22640ce2008-02-15 22:09:30 +0000866 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000867
868 // Handle ++ and -- (both pre- and post-increment).
869
Ted Kremenek07baa252008-02-21 18:02:17 +0000870 LVal SubLV = cast<LVal>(SubV);
871 RVal V = GetRVal(St, SubLV, U->getType());
872
Ted Kremenekb8782e12008-02-21 19:15:37 +0000873 if (V.isUnknown()) {
874 Dst.Add(N1);
875 continue;
876 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000877
Ted Kremenekb31af242008-02-28 09:25:22 +0000878 // Propagate undefined values.
879 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000880 Nodify(Dst, U, N1, SetRVal(St, U, V));
881 continue;
882 }
883
Ted Kremenekb1669d42008-02-21 19:29:23 +0000884 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000885
886 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
887 : BinaryOperator::Sub;
888
Ted Kremenekb1669d42008-02-21 19:29:23 +0000889 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000890
891 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000892 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000893 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000894 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000895
Ted Kremenek07baa252008-02-21 18:02:17 +0000896 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000897 continue;
898 }
899
900 // Handle all other unary operators.
901
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000902 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000903
Ted Kremenek07baa252008-02-21 18:02:17 +0000904 case UnaryOperator::Minus:
905 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000906 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000907
Ted Kremenek07baa252008-02-21 18:02:17 +0000908 case UnaryOperator::Not:
909 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000910 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000911
Ted Kremenek07baa252008-02-21 18:02:17 +0000912 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000913
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000914 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
915 //
916 // Note: technically we do "E == 0", but this is the same in the
917 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000918
919 if (isa<LVal>(SubV)) {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000920 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenek07baa252008-02-21 18:02:17 +0000921 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
922 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000923 }
924 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000925 Expr* Ex = U->getSubExpr();
Ted Kremenek8ad19872008-03-07 20:13:31 +0000926 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000927 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
928 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000929 }
930
931 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000932
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000933 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000934 assert (isa<LVal>(SubV));
935 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000936 break;
937 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000938
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000939 default: ;
940 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000941 }
942
943 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000944 }
945}
946
Ted Kremenek07baa252008-02-21 18:02:17 +0000947void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
948 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000949
Ted Kremenek07baa252008-02-21 18:02:17 +0000950 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000951
Ted Kremenek07baa252008-02-21 18:02:17 +0000952 // FIXME: Add support for VLAs.
953 if (!T.getTypePtr()->isConstantSizeType())
954 return;
955
Chris Lattner8cd0e932008-03-05 18:54:05 +0000956 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000957 ValueState* St = Pred->getState();
Ted Kremenek8ad19872008-03-07 20:13:31 +0000958 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000959
960 Nodify(Dst, U, Pred, St);
961}
962
963void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneka129a2e2008-02-27 07:04:16 +0000964
965 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
966 Dst.Add(Pred);
967 return;
968 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000969
970 Ex = Ex->IgnoreParens();
971
Ted Kremenekb5175bf2008-02-27 06:47:26 +0000972 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000973 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000974 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000975 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000976
Ted Kremenek07baa252008-02-21 18:02:17 +0000977 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000978 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000979 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000980
Ted Kremenek07baa252008-02-21 18:02:17 +0000981 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000982 Dst.Add(Pred);
983 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000984 Visit(Ex, Pred, Dst);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000985
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000986 return;
987 }
988 }
989
Ted Kremenek07baa252008-02-21 18:02:17 +0000990 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000991}
992
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000993void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000994 GRExprEngine::NodeTy* Pred,
995 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000996 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000997
998 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +0000999 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001000 else
1001 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001002
Ted Kremenekf031b872008-01-23 19:59:44 +00001003 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001004
Ted Kremenekf031b872008-01-23 19:59:44 +00001005 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +00001006
Ted Kremenekf031b872008-01-23 19:59:44 +00001007 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +00001008 // In such cases, we want to (initially) treat the LHS as an LVal,
1009 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1010 // evaluated to LValDecl's instead of to an NonLVal.
1011
1012 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
1013 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +00001014
Ted Kremenek07baa252008-02-21 18:02:17 +00001015 // Visit the RHS...
1016
1017 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +00001018 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001019
1020 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +00001021
Ted Kremenek07baa252008-02-21 18:02:17 +00001022 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +00001023
Ted Kremenekf031b872008-01-23 19:59:44 +00001024 NodeTy* N2 = *I2;
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001025 ValueState* St = N2->getState();
Ted Kremenek2c369792008-02-25 18:42:54 +00001026 Expr* RHS = B->getRHS();
1027 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +00001028
Ted Kremenek15cb0782008-02-06 22:50:25 +00001029 BinaryOperator::Opcode Op = B->getOpcode();
1030
Ted Kremenek2c369792008-02-25 18:42:54 +00001031 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1032 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +00001033
Ted Kremenekb31af242008-02-28 09:25:22 +00001034 // Check if the denominator is undefined.
Ted Kremenek2c369792008-02-25 18:42:54 +00001035
Ted Kremenek5a600862008-02-26 22:27:51 +00001036 if (!RightV.isUnknown()) {
1037
Ted Kremenekb31af242008-02-28 09:25:22 +00001038 if (RightV.isUndef()) {
1039 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek5a600862008-02-26 22:27:51 +00001040
Ted Kremenekb31af242008-02-28 09:25:22 +00001041 if (DivUndef) {
1042 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001043 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek5a600862008-02-26 22:27:51 +00001044 }
1045
1046 continue;
1047 }
1048
1049 // Check for divide/remainder-by-zero.
1050 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001051 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001052
Ted Kremenek75f32c62008-03-07 19:04:53 +00001053 bool isFeasibleZero = false;
1054 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001055
Ted Kremenek5a600862008-02-26 22:27:51 +00001056 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001057
Ted Kremenek75f32c62008-03-07 19:04:53 +00001058 bool isFeasibleNotZero = false;
1059 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001060
Ted Kremenek75f32c62008-03-07 19:04:53 +00001061 // Create the node for the divide-by-zero (if it occurred).
1062
1063 if (isFeasibleZero)
1064 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1065 DivZeroNode->markAsSink();
1066
1067 if (isFeasibleNotZero)
1068 ImplicitBadDivides.insert(DivZeroNode);
1069 else
1070 ExplicitBadDivides.insert(DivZeroNode);
1071
1072 }
1073
1074 if (!isFeasibleNotZero)
Ted Kremenek5a600862008-02-26 22:27:51 +00001075 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001076 }
1077
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001078 // Fall-through. The logic below processes the divide.
1079 }
1080
Ted Kremenekc2d07202008-02-28 20:32:03 +00001081
Ted Kremenek15cb0782008-02-06 22:50:25 +00001082 if (Op <= BinaryOperator::Or) {
1083
Ted Kremenek07baa252008-02-21 18:02:17 +00001084 // Process non-assignements except commas or short-circuited
1085 // logical expressions (LAnd and LOr).
1086
1087 RVal Result = EvalBinOp(Op, LeftV, RightV);
1088
1089 if (Result.isUnknown()) {
1090 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001091 continue;
1092 }
1093
Ted Kremenekc2d07202008-02-28 20:32:03 +00001094 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1095
1096 // The operands were not undefined, but the result is undefined.
1097
1098 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1099 UndefNode->markAsSink();
1100 UndefResults.insert(UndefNode);
1101 }
1102
1103 continue;
1104 }
1105
Ted Kremenek07baa252008-02-21 18:02:17 +00001106 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001107 continue;
1108 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001109
1110 // Process assignments.
1111
1112 switch (Op) {
1113
Ted Kremenekf031b872008-01-23 19:59:44 +00001114 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001115
1116 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001117
Ted Kremenekb31af242008-02-28 09:25:22 +00001118 if (LeftV.isUndef()) {
1119 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001120 continue;
1121 }
1122
1123 if (LeftV.isUnknown()) {
1124 St = SetRVal(St, B, RightV);
1125 break;
1126 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001127
Ted Kremenek07baa252008-02-21 18:02:17 +00001128 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001129 break;
1130 }
1131
Ted Kremenek07baa252008-02-21 18:02:17 +00001132 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001133
Ted Kremenek07baa252008-02-21 18:02:17 +00001134 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001135
Ted Kremenek5a600862008-02-26 22:27:51 +00001136 assert (B->isCompoundAssignmentOp());
1137
1138 if (Op >= BinaryOperator::AndAssign)
1139 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1140 else
1141 ((int&) Op) -= BinaryOperator::MulAssign;
1142
Ted Kremenekb31af242008-02-28 09:25:22 +00001143 // Check if the LHS is undefined.
Ted Kremenek07baa252008-02-21 18:02:17 +00001144
Ted Kremenekb31af242008-02-28 09:25:22 +00001145 if (LeftV.isUndef()) {
1146 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001147 continue;
1148 }
1149
1150 if (LeftV.isUnknown()) {
1151
1152 // While we do not know the location to store RightV,
1153 // the entire expression does evaluate to RightV.
1154
1155 if (RightV.isUnknown()) {
1156 Dst.Add(N2);
1157 continue;
1158 }
1159
1160 St = SetRVal(St, B, RightV);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001161 break;
1162 }
1163
Ted Kremenek07baa252008-02-21 18:02:17 +00001164 // At this pointer we know that the LHS evaluates to an LVal
1165 // that is neither "Unknown" or "Unintialized."
1166
1167 LVal LeftLV = cast<LVal>(LeftV);
1168
Ted Kremenek07baa252008-02-21 18:02:17 +00001169 // Fetch the value of the LHS (the value of the variable, etc.).
1170
1171 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1172
Ted Kremenekb31af242008-02-28 09:25:22 +00001173 // Propagate undefined value (left-side). We
1174 // propogate undefined values for the RHS below when
Ted Kremenek5a600862008-02-26 22:27:51 +00001175 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001176
Ted Kremenekb31af242008-02-28 09:25:22 +00001177 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001178 St = SetRVal(St, B, V);
1179 break;
1180 }
1181
1182 // Propagate unknown values.
1183
Ted Kremenekb8782e12008-02-21 19:15:37 +00001184 if (V.isUnknown()) {
1185 Dst.Add(N2);
1186 continue;
1187 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001188
1189 if (RightV.isUnknown()) {
1190 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1191 break;
1192 }
1193
Ted Kremenek5a600862008-02-26 22:27:51 +00001194 // At this point:
1195 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001196 // The LHS is not Undef/Unknown.
Ted Kremenek5a600862008-02-26 22:27:51 +00001197 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001198
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001199 // Get the computation type.
1200 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1201
1202 // Perform promotions.
1203 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001204 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001205
1206 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001207
Ted Kremenek2c369792008-02-25 18:42:54 +00001208 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1209 && RHS->getType()->isIntegerType()) {
1210
Ted Kremenekb31af242008-02-28 09:25:22 +00001211 // Check if the denominator is undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001212
Ted Kremenekb31af242008-02-28 09:25:22 +00001213 if (RightV.isUndef()) {
1214 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001215
Ted Kremenekb31af242008-02-28 09:25:22 +00001216 if (DivUndef) {
1217 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001218 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001219 }
1220
1221 continue;
1222 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001223
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001224 // First, "assume" that the denominator is 0.
1225
Ted Kremenek75f32c62008-03-07 19:04:53 +00001226 bool isFeasibleZero = false;
1227 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001228
Ted Kremenek75f32c62008-03-07 19:04:53 +00001229 // Second, "assume" that the denominator cannot be 0.
1230
1231 bool isFeasibleNotZero = false;
1232 St = Assume(St, RightV, true, isFeasibleNotZero);
1233
1234 // Create the node for the divide-by-zero error (if it occurred).
1235
1236 if (isFeasibleZero) {
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001237 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1238
1239 if (DivZeroNode) {
1240 DivZeroNode->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001241
1242 if (isFeasibleNotZero)
1243 ImplicitBadDivides.insert(DivZeroNode);
1244 else
1245 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001246 }
1247 }
1248
Ted Kremenek75f32c62008-03-07 19:04:53 +00001249 if (!isFeasibleNotZero)
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001250 continue;
1251
1252 // Fall-through. The logic below processes the divide.
1253 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001254 else {
1255
Ted Kremenekb31af242008-02-28 09:25:22 +00001256 // Propagate undefined values (right-side).
Ted Kremenek5a600862008-02-26 22:27:51 +00001257
Ted Kremenekb31af242008-02-28 09:25:22 +00001258 if (RightV.isUndef()) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001259 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1260 break;
1261 }
1262
1263 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001264
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001265 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekc2d07202008-02-28 20:32:03 +00001266
1267 if (Result.isUndef()) {
1268
1269 // The operands were not undefined, but the result is undefined.
1270
1271 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1272 UndefNode->markAsSink();
1273 UndefResults.insert(UndefNode);
1274 }
1275
1276 continue;
1277 }
1278
Ted Kremenek07baa252008-02-21 18:02:17 +00001279 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001280 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001281 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001282
1283 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001284 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001285 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001286}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001287
Ted Kremenekb31af242008-02-28 09:25:22 +00001288void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001289 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1290 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +00001291 UndefStores.insert(N);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001292}
Ted Kremenekbe962452008-01-16 19:42:59 +00001293
Ted Kremenekbf988d02008-02-19 00:22:37 +00001294void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001295
1296 // FIXME: add metadata to the CFG so that we can disable
1297 // this check when we KNOW that there is no block-level subexpression.
1298 // The motivation is that this check requires a hashtable lookup.
1299
1300 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1301 Dst.Add(Pred);
1302 return;
1303 }
1304
1305 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001306
1307 default:
1308 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001309 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001310
1311 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1312 break;
1313
Ted Kremenek744a7862008-02-08 20:29:23 +00001314 case Stmt::BinaryOperatorClass: {
1315 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001316
Ted Kremenek744a7862008-02-08 20:29:23 +00001317 if (B->isLogicalOp()) {
1318 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001319 break;
1320 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001321 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001322 ValueState* St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001323 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001324 break;
1325 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001326
1327 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1328 break;
1329 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001330
1331 case Stmt::CallExprClass: {
1332 CallExpr* C = cast<CallExpr>(S);
1333 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1334 break;
1335 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001336
1337 case Stmt::CastExprClass: {
1338 CastExpr* C = cast<CastExpr>(S);
1339 VisitCast(C, C->getSubExpr(), Pred, Dst);
1340 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001341 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001342
Ted Kremenek07baa252008-02-21 18:02:17 +00001343 // FIXME: ChooseExpr is really a constant. We need to fix
1344 // the CFG do not model them as explicit control-flow.
1345
Ted Kremenekfd85f292008-02-12 19:49:57 +00001346 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1347 ChooseExpr* C = cast<ChooseExpr>(S);
1348 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1349 break;
1350 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001351
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001352 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001353 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1354 break;
1355
Ted Kremenekfd85f292008-02-12 19:49:57 +00001356 case Stmt::ConditionalOperatorClass: { // '?' operator
1357 ConditionalOperator* C = cast<ConditionalOperator>(S);
1358 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1359 break;
1360 }
1361
1362 case Stmt::DeclRefExprClass:
1363 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1364 break;
1365
1366 case Stmt::DeclStmtClass:
1367 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1368 break;
1369
1370 case Stmt::ImplicitCastExprClass: {
1371 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1372 VisitCast(C, C->getSubExpr(), Pred, Dst);
1373 break;
1374 }
1375
1376 case Stmt::ParenExprClass:
1377 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1378 break;
1379
1380 case Stmt::SizeOfAlignOfTypeExprClass:
1381 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1382 break;
1383
Ted Kremenek106f37c2008-02-08 07:05:39 +00001384 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001385 StmtExpr* SE = cast<StmtExpr>(S);
1386
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001387 ValueState* St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +00001388 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001389 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001390 break;
1391 }
1392
Ted Kremenek07baa252008-02-21 18:02:17 +00001393 // FIXME: We may wish to always bind state to ReturnStmts so
1394 // that users can quickly query what was the state at the
1395 // exit points of a function.
1396
Ted Kremenekfd85f292008-02-12 19:49:57 +00001397 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001398 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1399 Visit(R, Pred, Dst);
1400 else
1401 Dst.Add(Pred);
1402
1403 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001404 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001405
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001406 case Stmt::UnaryOperatorClass: {
1407 UnaryOperator* U = cast<UnaryOperator>(S);
1408
Ted Kremenek07baa252008-02-21 18:02:17 +00001409 switch (U->getOpcode()) {
1410 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1411 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1412 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1413 default: VisitUnaryOperator(U, Pred, Dst); break;
1414 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001415
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001416 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001417 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001418 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001419}
1420
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001421//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001422// "Assume" logic.
1423//===----------------------------------------------------------------------===//
1424
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001425ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001426 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001427 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001428 switch (Cond.getSubKind()) {
1429 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001430 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001431 return St;
1432
Ted Kremenek13f31562008-02-06 00:54:14 +00001433 case lval::SymbolValKind:
1434 if (Assumption)
1435 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001436 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001437 else
1438 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001439 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001440
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001441
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001442 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001443 case lval::FuncValKind:
1444 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001445 isFeasible = Assumption;
1446 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001447
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001448 case lval::ConcreteIntKind: {
1449 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001450 isFeasible = b ? Assumption : !Assumption;
1451 return St;
1452 }
1453 }
Ted Kremenek90960972008-01-30 23:03:39 +00001454}
1455
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001456ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001457 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001458 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001459 switch (Cond.getSubKind()) {
1460 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001461 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001462 return St;
1463
Ted Kremenekab359c12008-02-06 17:32:17 +00001464
1465 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001466 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001467 SymbolID sym = SV.getSymbol();
1468
1469 if (Assumption)
Ted Kremenek8ad19872008-03-07 20:13:31 +00001470 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001471 isFeasible);
1472 else
Ted Kremenek8ad19872008-03-07 20:13:31 +00001473 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001474 isFeasible);
1475 }
1476
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001477 case nonlval::SymIntConstraintValKind:
1478 return
1479 AssumeSymInt(St, Assumption,
1480 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1481 isFeasible);
1482
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001483 case nonlval::ConcreteIntKind: {
1484 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001485 isFeasible = b ? Assumption : !Assumption;
1486 return St;
1487 }
1488 }
1489}
1490
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001491ValueState*
1492GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001493 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001494
Ted Kremenek13f31562008-02-06 00:54:14 +00001495 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001496 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001497 isFeasible = *X != V;
1498 return St;
1499 }
1500
1501 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001502 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001503 isFeasible = true;
1504 return St;
1505 }
1506
1507 // If we reach here, sym is not a constant and we don't know if it is != V.
1508 // Make that assumption.
1509
1510 isFeasible = true;
1511 return StateMgr.AddNE(St, sym, V);
1512}
1513
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001514ValueState*
1515GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001516 const llvm::APSInt& V, bool& isFeasible) {
1517
1518 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001519 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001520 isFeasible = *X == V;
1521 return St;
1522 }
1523
1524 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001525 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001526 isFeasible = false;
1527 return St;
1528 }
1529
1530 // If we reach here, sym is not a constant and we don't know if it is == V.
1531 // Make that assumption.
1532
1533 isFeasible = true;
1534 return StateMgr.AddEQ(St, sym, V);
1535}
Ted Kremenek90960972008-01-30 23:03:39 +00001536
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001537ValueState*
1538GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001539 const SymIntConstraint& C, bool& isFeasible) {
1540
1541 switch (C.getOpcode()) {
1542 default:
1543 // No logic yet for other operators.
1544 return St;
1545
1546 case BinaryOperator::EQ:
1547 if (Assumption)
1548 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1549 else
1550 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1551
1552 case BinaryOperator::NE:
1553 if (Assumption)
1554 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1555 else
1556 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1557 }
1558}
1559
Ted Kremenek90960972008-01-30 23:03:39 +00001560//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001561// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001562//===----------------------------------------------------------------------===//
1563
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001564#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001565static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001566
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001567namespace llvm {
1568template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001569struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001570 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001571
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001572 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001573
1574 Out << "Variables:\\l";
1575
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001576 bool isFirst = true;
1577
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001578 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001579
1580 if (isFirst)
1581 isFirst = false;
1582 else
1583 Out << "\\l";
1584
1585 Out << ' ' << I.getKey()->getName() << " : ";
1586 I.getData().print(Out);
1587 }
1588
1589 }
1590
Ted Kremenek17c5f112008-02-11 19:21:59 +00001591
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001592 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001593
1594 bool isFirst = true;
1595
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001596 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001597
1598 if (isFirst) {
1599 Out << "\\l\\lSub-Expressions:\\l";
1600 isFirst = false;
1601 }
1602 else
1603 Out << "\\l";
1604
1605 Out << " (" << (void*) I.getKey() << ") ";
1606 I.getKey()->printPretty(Out);
1607 Out << " : ";
1608 I.getData().print(Out);
1609 }
1610 }
1611
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001612 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001613
Ted Kremenek08cfd832008-02-08 21:10:02 +00001614 bool isFirst = true;
1615
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001616 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001617 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001618 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001619 isFirst = false;
1620 }
1621 else
1622 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001623
Ted Kremenek17c5f112008-02-11 19:21:59 +00001624 Out << " (" << (void*) I.getKey() << ") ";
1625 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001626 Out << " : ";
1627 I.getData().print(Out);
1628 }
1629 }
1630
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001631 static void PrintEQ(std::ostream& Out, ValueState* St) {
1632 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001633
1634 if (CE.isEmpty())
1635 return;
1636
1637 Out << "\\l\\|'==' constraints:";
1638
Ted Kremenek07baa252008-02-21 18:02:17 +00001639 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001640 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1641 }
1642
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001643 static void PrintNE(std::ostream& Out, ValueState* St) {
1644 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001645
1646 if (NE.isEmpty())
1647 return;
1648
1649 Out << "\\l\\|'!=' constraints:";
1650
Ted Kremenek07baa252008-02-21 18:02:17 +00001651 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001652 I != EI; ++I){
1653
1654 Out << "\\l $" << I.getKey() << " : ";
1655 bool isFirst = true;
1656
1657 ValueState::IntSetTy::iterator J=I.getData().begin(),
1658 EJ=I.getData().end();
1659 for ( ; J != EJ; ++J) {
1660 if (isFirst) isFirst = false;
1661 else Out << ", ";
1662
1663 Out << (*J)->toString();
1664 }
1665 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001666 }
1667
1668 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1669
1670 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001671 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00001672 GraphPrintCheckerState->isUndefDeref(N) ||
1673 GraphPrintCheckerState->isUndefStore(N) ||
1674 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00001675 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1676 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00001677 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001678 GraphPrintCheckerState->isBadCall(N) ||
1679 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001680 return "color=\"red\",style=\"filled\"";
1681
Ted Kremenekc2d07202008-02-28 20:32:03 +00001682 if (GraphPrintCheckerState->isNoReturnCall(N))
1683 return "color=\"blue\",style=\"filled\"";
1684
Ted Kremeneka853de62008-02-14 22:54:53 +00001685 return "";
1686 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001687
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001688 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001689 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001690
1691 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001692 ProgramPoint Loc = N->getLocation();
1693
1694 switch (Loc.getKind()) {
1695 case ProgramPoint::BlockEntranceKind:
1696 Out << "Block Entrance: B"
1697 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1698 break;
1699
1700 case ProgramPoint::BlockExitKind:
1701 assert (false);
1702 break;
1703
1704 case ProgramPoint::PostStmtKind: {
1705 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001706 Out << L.getStmt()->getStmtClassName() << ':'
1707 << (void*) L.getStmt() << ' ';
1708
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001709 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001710
Ted Kremenek43863eb2008-02-29 23:14:48 +00001711 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenek80d52d02008-02-07 05:48:01 +00001712 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001713 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001714 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001715 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001716 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001717 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001718 Out << "\\|Store to Undefined LVal.";
Ted Kremenek75f32c62008-03-07 19:04:53 +00001719 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1720 Out << "\\|Explicit divide-by zero or undefined value.";
1721 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
1722 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001723 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenekc2d07202008-02-28 20:32:03 +00001724 Out << "\\|Result of operation is undefined.";
Ted Kremenekc2d07202008-02-28 20:32:03 +00001725 else if (GraphPrintCheckerState->isNoReturnCall(N))
1726 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001727 else if (GraphPrintCheckerState->isBadCall(N))
1728 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001729 else if (GraphPrintCheckerState->isUndefArg(N))
1730 Out << "\\|Argument in call is undefined";
Ted Kremenek80d52d02008-02-07 05:48:01 +00001731
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001732 break;
1733 }
1734
1735 default: {
1736 const BlockEdge& E = cast<BlockEdge>(Loc);
1737 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1738 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001739
1740 if (Stmt* T = E.getSrc()->getTerminator()) {
1741 Out << "\\|Terminator: ";
1742 E.getSrc()->printTerminator(Out);
1743
Ted Kremenekaee121c2008-02-13 23:08:21 +00001744 if (isa<SwitchStmt>(T)) {
1745 Stmt* Label = E.getDst()->getLabel();
1746
1747 if (Label) {
1748 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1749 Out << "\\lcase ";
1750 C->getLHS()->printPretty(Out);
1751
1752 if (Stmt* RHS = C->getRHS()) {
1753 Out << " .. ";
1754 RHS->printPretty(Out);
1755 }
1756
1757 Out << ":";
1758 }
1759 else {
1760 assert (isa<DefaultStmt>(Label));
1761 Out << "\\ldefault:";
1762 }
1763 }
1764 else
1765 Out << "\\l(implicit) default:";
1766 }
1767 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001768 // FIXME
1769 }
1770 else {
1771 Out << "\\lCondition: ";
1772 if (*E.getSrc()->succ_begin() == E.getDst())
1773 Out << "true";
1774 else
1775 Out << "false";
1776 }
1777
1778 Out << "\\l";
1779 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001780
Ted Kremenekb31af242008-02-28 09:25:22 +00001781 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1782 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00001783 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001784 }
1785 }
1786
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001787 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001788
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001789 N->getState()->printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001790
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001791 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001792 return Out.str();
1793 }
1794};
1795} // end llvm namespace
1796#endif
1797
Ted Kremenek3862eb12008-02-14 22:36:46 +00001798void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001799#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001800 GraphPrintCheckerState = this;
1801 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001802 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001803#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001804}