blob: a32999eb513f8f635d59920cc50f70a72df12c67 [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 }
291 else V2 = V1;
292
293 // FIXME: Eventually we should replace the logic below with a range
294 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000295 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000296
297 do {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000298 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000299
Ted Kremenek07baa252008-02-21 18:02:17 +0000300 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000301
302 // Now "assume" that the case matches.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000303
Ted Kremenekd4676512008-03-12 21:45:47 +0000304 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000305 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000306
307 if (isFeasible) {
308 builder.generateCaseStmtNode(I, StNew);
309
310 // If CondV evaluates to a constant, then we know that this
311 // is the *only* case that we can take, so stop evaluating the
312 // others.
313 if (isa<nonlval::ConcreteInt>(CondV))
314 return;
315 }
316
317 // Now "assume" that the case doesn't match. Add this state
318 // to the default state (if it is feasible).
319
Ted Kremenekd4676512008-03-12 21:45:47 +0000320 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000321 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000322
323 if (isFeasible)
324 DefaultSt = StNew;
325
326 // Concretize the next value in the range.
327 ++V1;
328
329 } while (V1 < V2);
330 }
331
332 // If we reach here, than we know that the default branch is
333 // possible.
334 builder.generateDefaultCaseNode(DefaultSt);
335}
336
337
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000338void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000339 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000340
Ted Kremenek99ecce72008-02-26 19:05:15 +0000341 assert (B->getOpcode() == BinaryOperator::LAnd ||
342 B->getOpcode() == BinaryOperator::LOr);
343
344 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
345
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000346 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000347 RVal X = GetBlkExprRVal(St, B);
348
Ted Kremenekb31af242008-02-28 09:25:22 +0000349 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000350
Ted Kremenekb31af242008-02-28 09:25:22 +0000351 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000352
353 assert (Ex);
354
355 if (Ex == B->getRHS()) {
356
357 X = GetBlkExprRVal(St, Ex);
358
Ted Kremenekb31af242008-02-28 09:25:22 +0000359 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000360
Ted Kremenekb31af242008-02-28 09:25:22 +0000361 if (X.isUndef()) {
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000362 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
363 return;
364 }
365
Ted Kremenek99ecce72008-02-26 19:05:15 +0000366 // We took the RHS. Because the value of the '&&' or '||' expression must
367 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
368 // or 1. Alternatively, we could take a lazy approach, and calculate this
369 // value later when necessary. We don't have the machinery in place for
370 // this right now, and since most logical expressions are used for branches,
371 // the payoff is not likely to be large. Instead, we do eager evaluation.
372
373 bool isFeasible = false;
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000374 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000375
376 if (isFeasible)
377 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
378
379 isFeasible = false;
380 NewState = Assume(St, X, false, isFeasible);
381
382 if (isFeasible)
383 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000384 }
385 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000386 // We took the LHS expression. Depending on whether we are '&&' or
387 // '||' we know what the value of the expression is via properties of
388 // the short-circuiting.
389
390 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
391 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000392 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000393}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000394
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000395
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000396void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000397
Ted Kremenek07baa252008-02-21 18:02:17 +0000398 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000399 StmtEntryNode = builder.getLastNode();
400 CurrentStmt = S;
401 NodeSet Dst;
Ted Kremenek02331462008-03-09 18:28:41 +0000402
403 // Create the cleaned state.
404
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000405 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
406 CurrentStmt, Liveness);
Ted Kremeneka1d070b2008-03-10 04:45:00 +0000407
408 Builder->SetCleanedState(CleanedState);
Ted Kremenek02331462008-03-09 18:28:41 +0000409
410 // Visit the statement.
Ted Kremenekf031b872008-01-23 19:59:44 +0000411
412 Visit(S, StmtEntryNode, Dst);
413
414 // If no nodes were generated, generate a new node that has all the
415 // dead mappings removed.
Ted Kremenek07baa252008-02-21 18:02:17 +0000416
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000417 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode)
418 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
Ted Kremeneka57214f2008-01-18 00:41:32 +0000419
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000420 // NULL out these variables to cleanup.
Ted Kremenek07baa252008-02-21 18:02:17 +0000421
Ted Kremenekf031b872008-01-23 19:59:44 +0000422 CurrentStmt = NULL;
423 StmtEntryNode = NULL;
424 Builder = NULL;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000425 CleanedState = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000426}
427
Ted Kremenek6d409922008-02-13 18:06:44 +0000428void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000429
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000430 if (D != CurrentStmt) {
431 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
432 return;
433 }
434
435 // If we are here, we are loading the value of the decl and binding
436 // it to the block-level expression.
437
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000438 ValueState* St = GetState(Pred);
Ted Kremenekf97c6682008-03-09 03:30:59 +0000439 RVal X = RVal::MakeVal(BasicVals, D);
440 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
441 Nodify(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000442}
443
Ted Kremenekd9268e32008-02-19 01:44:53 +0000444void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000445 CallExpr::arg_iterator AI,
446 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000447 NodeSet& Dst) {
448
Ted Kremenek07baa252008-02-21 18:02:17 +0000449 // Process the arguments.
450
451 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000452
Ted Kremenekef7ea072008-03-04 00:56:45 +0000453 NodeSet DstTmp;
Ted Kremenek769f3482008-03-04 22:01:56 +0000454 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +0000455 ++AI;
456
Ted Kremenek769f3482008-03-04 22:01:56 +0000457 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenek07baa252008-02-21 18:02:17 +0000458 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000459
460 return;
461 }
462
463 // If we reach here we have processed all of the arguments. Evaluate
464 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000465
Ted Kremenekc71901d2008-02-25 21:16:03 +0000466 NodeSet DstTmp;
467 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000468
Ted Kremenekc71901d2008-02-25 21:16:03 +0000469 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000470
471 if (DstTmp.empty())
472 DstTmp.Add(Pred);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000473
474 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000475 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
476
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000477 ValueState* St = GetState(*DI);
Ted Kremenekc71901d2008-02-25 21:16:03 +0000478 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000479
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000480 // FIXME: Add support for symbolic function calls (calls involving
481 // function pointer values that are symbolic).
482
483 // Check for undefined control-flow or calls to NULL.
484
Ted Kremenek43863eb2008-02-29 23:14:48 +0000485 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000486 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000487
Ted Kremenek9b31f5b2008-02-29 23:53:11 +0000488 if (N) {
489 N->markAsSink();
490 BadCalls.insert(N);
491 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000492
Ted Kremenekd9268e32008-02-19 01:44:53 +0000493 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +0000494 }
495
496 // Check for the "noreturn" attribute.
497
498 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
499
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000500 if (isa<lval::FuncVal>(L)) {
501
502 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
503
504 if (FD->getAttr<NoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +0000505 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000506 else {
507 // HACK: Some functions are not marked noreturn, and don't return.
508 // Here are a few hardwired ones. If this takes too long, we can
509 // potentially cache these results.
510 const char* s = FD->getIdentifier()->getName();
511 unsigned n = strlen(s);
512
513 switch (n) {
514 default:
515 break;
Ted Kremenek550025b2008-03-14 23:25:49 +0000516
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000517 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +0000518 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
519 break;
520
521 case 5:
522 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
523 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +0000524 }
525 }
526 }
Ted Kremenekb451dd32008-03-05 21:15:02 +0000527
528 // Evaluate the call.
529
Ted Kremenekcda2efd2008-03-03 16:47:31 +0000530
Ted Kremenek769f3482008-03-04 22:01:56 +0000531 bool invalidateArgs = false;
Ted Kremenekd9268e32008-02-19 01:44:53 +0000532
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000533 if (L.isUnknown()) {
Ted Kremenek769f3482008-03-04 22:01:56 +0000534 // Check for an "unknown" callee.
535 invalidateArgs = true;
536 }
537 else if (isa<lval::FuncVal>(L)) {
538
539 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
540
Ted Kremenek21581c62008-03-05 22:59:42 +0000541 if (unsigned id = Info->getBuiltinID()) {
542 switch (id) {
543 case Builtin::BI__builtin_expect: {
544 // For __builtin_expect, just return the value of the subexpression.
545 assert (CE->arg_begin() != CE->arg_end());
546 RVal X = GetRVal(St, *(CE->arg_begin()));
547 Nodify(Dst, CE, *DI, SetRVal(St, CE, X));
548 continue;
549 }
550
551 default:
552 invalidateArgs = true;
553 break;
554 }
555 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000556 }
557
558 if (invalidateArgs) {
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000559 // Invalidate all arguments passed in by reference (LVals).
560 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
561 I != E; ++I) {
562 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000563
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000564 if (isa<LVal>(V))
565 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenekb451dd32008-03-05 21:15:02 +0000566 }
567
568 Nodify(Dst, CE, *DI, St);
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000569 }
Ted Kremenek769f3482008-03-04 22:01:56 +0000570 else {
571
572 // Check any arguments passed-by-value against being undefined.
573
574 bool badArg = false;
575
576 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
577 I != E; ++I) {
578
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000579 if (GetRVal(GetState(*DI), *I).isUndef()) {
580 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +0000581
582 if (N) {
583 N->markAsSink();
584 UndefArgs[N] = *I;
585 }
586
587 badArg = true;
588 break;
589 }
590 }
591
592 if (badArg)
593 continue;
594
595 // Dispatch to the plug-in transfer function.
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000596
597 unsigned size = Dst.size();
598
599 EvalCall(Dst, CE, cast<LVal>(L), *DI);
600
Ted Kremeneka2822eb2008-03-05 22:49:16 +0000601 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000602 Nodify(Dst, CE, *DI, St);
Ted Kremenek769f3482008-03-04 22:01:56 +0000603 }
Ted Kremenekd9268e32008-02-19 01:44:53 +0000604 }
605}
606
Ted Kremenek07baa252008-02-21 18:02:17 +0000607void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000608
Ted Kremenek5f585b02008-02-19 18:52:54 +0000609 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +0000610 QualType T = CastE->getType();
611
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000612 if (T->isReferenceType())
613 VisitLVal(Ex, Pred, S1);
614 else
615 Visit(Ex, Pred, S1);
616
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000617 // Check for redundant casts or casting to "void"
618 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000619 Ex->getType() == T ||
620 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000621
Ted Kremenek07baa252008-02-21 18:02:17 +0000622 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000623 Dst.Add(*I1);
624
Ted Kremenek54eddae2008-01-24 02:02:54 +0000625 return;
626 }
627
Ted Kremenek07baa252008-02-21 18:02:17 +0000628 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000629 NodeTy* N = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000630 ValueState* St = GetState(N);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +0000631
632 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
633
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000634 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000635 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000636}
637
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000638void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000639 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000640
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000641 ValueState* St = GetState(Pred);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000642
643 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000644 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000645
646 // FIXME: Add support for local arrays.
647 if (VD->getType()->isArrayType())
648 continue;
649
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000650 const Expr* Ex = VD->getInit();
Ted Kremenek07baa252008-02-21 18:02:17 +0000651
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000652 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
653
654 // In this context, Static => Local variable.
655
656 assert (!VD->getStorageClass() == VarDecl::Static ||
657 !isa<FileVarDecl>(VD));
658
659 // If there is no initializer, set the value of the
Ted Kremenekb31af242008-02-28 09:25:22 +0000660 // variable to "Undefined".
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000661 //
662 // FIXME: static variables may have an initializer, but the second
663 // time a function is called those values may not be current.
Ted Kremenek98d093b2008-03-04 20:40:11 +0000664
665 QualType T = VD->getType();
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000666
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000667 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000668
669 // C99: 6.7.8 Initialization
670 // If an object that has static storage duration is not initialized
671 // explicitly, then:
672 // —if it has pointer type, it is initialized to a null pointer;
673 // —if it has arithmetic type, it is initialized to (positive or
674 // unsigned) zero;
675
Ted Kremenek98d093b2008-03-04 20:40:11 +0000676 // FIXME: Handle structs. Now we treat their values as unknown.
677
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000678 if (T->isPointerType()) {
679
680 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000681 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000682 }
683 else if (T->isIntegerType()) {
684
685 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek8ad19872008-03-07 20:13:31 +0000686 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000687 }
688
Ted Kremenek98d093b2008-03-04 20:40:11 +0000689
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000690 }
Ted Kremenek98d093b2008-03-04 20:40:11 +0000691 else {
Ted Kremeneka6cfcc92008-03-04 04:18:04 +0000692
Ted Kremenek98d093b2008-03-04 20:40:11 +0000693 // FIXME: Handle structs. Now we treat them as unknown. What
694 // we need to do is treat their members as unknown.
695
696 if (T->isPointerType() || T->isIntegerType())
697 St = SetRVal(St, lval::DeclVal(VD),
698 Ex ? GetRVal(St, Ex) : UndefinedVal());
699 }
Ted Kremenek04ab4c12008-02-27 19:21:33 +0000700 }
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000701 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000702
703 Nodify(Dst, DS, Pred, St);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000704}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000705
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000706
Ted Kremenek07baa252008-02-21 18:02:17 +0000707void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000708 NodeTy* Pred, NodeSet& Dst) {
709
Ted Kremenek99ecce72008-02-26 19:05:15 +0000710 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
711
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000712 ValueState* St = GetState(Pred);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000713 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000714
Ted Kremenekb31af242008-02-28 09:25:22 +0000715 assert (X.isUndef());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000716
Ted Kremenekb31af242008-02-28 09:25:22 +0000717 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000718
719 assert (SE);
720
721 X = GetBlkExprRVal(St, SE);
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000722
723 // Make sure that we invalidate the previous binding.
724 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000725}
726
Ted Kremenekfd85f292008-02-12 19:49:57 +0000727/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000728void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
729 NodeTy* Pred,
730 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000731
732 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000733
734 // 6.5.3.4 sizeof: "The result type is an integer."
735
Ted Kremenek07baa252008-02-21 18:02:17 +0000736 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000737
Ted Kremenek07baa252008-02-21 18:02:17 +0000738
Ted Kremenek571f5192008-02-21 18:15:29 +0000739 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000740 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000741 return;
742
Ted Kremenek571f5192008-02-21 18:15:29 +0000743
744 uint64_t size = 1; // Handle sizeof(void)
745
Chris Lattner8cd0e932008-03-05 18:54:05 +0000746 if (T != getContext().VoidTy)
747 size = getContext().getTypeSize(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000748
Ted Kremenek07baa252008-02-21 18:02:17 +0000749 Nodify(Dst, Ex, Pred,
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000750 SetRVal(GetState(Pred), Ex,
Ted Kremenek8ad19872008-03-07 20:13:31 +0000751 NonLVal::MakeVal(BasicVals, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000752
753}
754
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000755void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
756 NodeSet& Dst, bool GetLVal) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000757
Ted Kremenek07baa252008-02-21 18:02:17 +0000758 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000759
760 NodeSet DstTmp;
761
Ted Kremenek56a89992008-02-26 03:44:25 +0000762 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000763 DstTmp.Add(Pred);
764 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000765 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000766
Ted Kremenek07baa252008-02-21 18:02:17 +0000767 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000768
769 NodeTy* N = *I;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000770 ValueState* St = GetState(N);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000771
772 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
773
Ted Kremenek07baa252008-02-21 18:02:17 +0000774 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000775
Ted Kremenekb31af242008-02-28 09:25:22 +0000776 // Check for dereferences of undefined values.
Ted Kremenek07baa252008-02-21 18:02:17 +0000777
Ted Kremenekb31af242008-02-28 09:25:22 +0000778 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000779
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000780 NodeTy* Succ = Builder->generateNode(U, St, N);
781
782 if (Succ) {
783 Succ->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000784 UndefDeref.insert(Succ);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000785 }
786
787 continue;
788 }
789
Ted Kremenek07baa252008-02-21 18:02:17 +0000790 // Check for dereferences of unknown values. Treat as No-Ops.
791
792 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000793 Dst.Add(N);
794 continue;
795 }
796
797 // After a dereference, one of two possible situations arise:
798 // (1) A crash, because the pointer was NULL.
799 // (2) The pointer is not NULL, and the dereference works.
800 //
801 // We add these assumptions.
802
Ted Kremenek07baa252008-02-21 18:02:17 +0000803 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000804 bool isFeasibleNotNull;
805
806 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000807
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000808 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000809
810 if (isFeasibleNotNull) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000811
812 if (GetLVal) Nodify(Dst, U, N, SetRVal(StNotNull, U, LV));
813 else {
814
815 // FIXME: Currently symbolic analysis "generates" new symbols
816 // for the contents of values. We need a better approach.
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000817
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000818 Nodify(Dst, U, N, SetRVal(StNotNull, U,
819 GetRVal(StNotNull, LV, U->getType())));
820 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000821 }
822
823 bool isFeasibleNull;
824
Ted Kremenek07baa252008-02-21 18:02:17 +0000825 // Now "assume" that the pointer is NULL.
826
Ted Kremenekf4b49df2008-02-28 10:21:43 +0000827 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000828
829 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000830
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000831 // We don't use "Nodify" here because the node will be a sink
832 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000833
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000834 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
835
836 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000837
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000838 NullNode->markAsSink();
839
Ted Kremenek07baa252008-02-21 18:02:17 +0000840 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
841 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000842 }
843 }
844 }
845}
846
Ted Kremenek07baa252008-02-21 18:02:17 +0000847void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
848 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000849
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000850 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000851
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000852 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000853 assert (U->getOpcode() != UnaryOperator::SizeOf);
854 assert (U->getOpcode() != UnaryOperator::AlignOf);
855
856 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000857
858 switch (U->getOpcode()) {
859 case UnaryOperator::PostInc:
860 case UnaryOperator::PostDec:
861 case UnaryOperator::PreInc:
862 case UnaryOperator::PreDec:
863 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000864 // Evalue subexpression as an LVal.
865 use_GetLVal = true;
866 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000867 break;
868
869 default:
870 Visit(U->getSubExpr(), Pred, S1);
871 break;
872 }
873
Ted Kremenek07baa252008-02-21 18:02:17 +0000874 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000875
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000876 NodeTy* N1 = *I1;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000877 ValueState* St = GetState(N1);
Ted Kremenek07baa252008-02-21 18:02:17 +0000878
879 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
880 GetRVal(St, U->getSubExpr());
881
882 if (SubV.isUnknown()) {
883 Dst.Add(N1);
884 continue;
885 }
886
Ted Kremenekb31af242008-02-28 09:25:22 +0000887 if (SubV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000888 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
889 continue;
890 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000891
Ted Kremenek22640ce2008-02-15 22:09:30 +0000892 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000893
894 // Handle ++ and -- (both pre- and post-increment).
895
Ted Kremenek07baa252008-02-21 18:02:17 +0000896 LVal SubLV = cast<LVal>(SubV);
897 RVal V = GetRVal(St, SubLV, U->getType());
898
Ted Kremenekb8782e12008-02-21 19:15:37 +0000899 if (V.isUnknown()) {
900 Dst.Add(N1);
901 continue;
902 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000903
Ted Kremenekb31af242008-02-28 09:25:22 +0000904 // Propagate undefined values.
905 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000906 Nodify(Dst, U, N1, SetRVal(St, U, V));
907 continue;
908 }
909
Ted Kremenekb1669d42008-02-21 19:29:23 +0000910 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000911
912 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
913 : BinaryOperator::Sub;
914
Ted Kremenekb1669d42008-02-21 19:29:23 +0000915 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000916
917 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000918 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000919 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000920 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000921
Ted Kremenek07baa252008-02-21 18:02:17 +0000922 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000923 continue;
924 }
925
926 // Handle all other unary operators.
927
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000928 switch (U->getOpcode()) {
Ted Kremenek3c536072008-03-15 03:05:30 +0000929
930 case UnaryOperator::Extension:
931 St = SetRVal(St, U, SubV);
932 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000933
Ted Kremenek07baa252008-02-21 18:02:17 +0000934 case UnaryOperator::Minus:
935 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000936 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000937
Ted Kremenek07baa252008-02-21 18:02:17 +0000938 case UnaryOperator::Not:
939 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000940 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000941
Ted Kremenek07baa252008-02-21 18:02:17 +0000942 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000943
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000944 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
945 //
946 // Note: technically we do "E == 0", but this is the same in the
947 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000948
949 if (isa<LVal>(SubV)) {
Ted Kremenek8ad19872008-03-07 20:13:31 +0000950 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenek07baa252008-02-21 18:02:17 +0000951 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
952 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000953 }
954 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000955 Expr* Ex = U->getSubExpr();
Ted Kremenek8ad19872008-03-07 20:13:31 +0000956 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000957 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
958 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000959 }
960
961 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000962
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000963 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000964 assert (isa<LVal>(SubV));
965 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000966 break;
967 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000968
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000969 default: ;
970 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000971 }
972
973 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000974 }
975}
976
Ted Kremenek07baa252008-02-21 18:02:17 +0000977void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
978 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000979
Ted Kremenek07baa252008-02-21 18:02:17 +0000980 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000981
Ted Kremenek07baa252008-02-21 18:02:17 +0000982 // FIXME: Add support for VLAs.
983 if (!T.getTypePtr()->isConstantSizeType())
984 return;
985
Chris Lattner8cd0e932008-03-05 18:54:05 +0000986 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +0000987 ValueState* St = GetState(Pred);
Ted Kremenek8ad19872008-03-07 20:13:31 +0000988 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000989
990 Nodify(Dst, U, Pred, St);
991}
992
993void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneka129a2e2008-02-27 07:04:16 +0000994
995 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
996 Dst.Add(Pred);
997 return;
998 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000999
1000 Ex = Ex->IgnoreParens();
1001
Ted Kremenekb5175bf2008-02-27 06:47:26 +00001002 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001003 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001004 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +00001005 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001006
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001007 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex))
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001008 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001009 VisitDeref(U, Pred, Dst, true);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001010 return;
1011 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001012
Ted Kremenek07baa252008-02-21 18:02:17 +00001013 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001014}
1015
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001016void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00001017 GRExprEngine::NodeTy* Pred,
1018 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001019 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001020
1021 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00001022 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001023 else
1024 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001025
Ted Kremenekf031b872008-01-23 19:59:44 +00001026 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001027
Ted Kremenekf031b872008-01-23 19:59:44 +00001028 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +00001029
Ted Kremenekf031b872008-01-23 19:59:44 +00001030 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +00001031 // In such cases, we want to (initially) treat the LHS as an LVal,
1032 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1033 // evaluated to LValDecl's instead of to an NonLVal.
1034
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001035 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1036 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +00001037
Ted Kremenek07baa252008-02-21 18:02:17 +00001038 // Visit the RHS...
1039
1040 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +00001041 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001042
1043 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +00001044
Ted Kremenek07baa252008-02-21 18:02:17 +00001045 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +00001046
Ted Kremenekf031b872008-01-23 19:59:44 +00001047 NodeTy* N2 = *I2;
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001048 ValueState* St = GetState(N2);
Ted Kremenek2c369792008-02-25 18:42:54 +00001049 Expr* RHS = B->getRHS();
1050 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +00001051
Ted Kremenek15cb0782008-02-06 22:50:25 +00001052 BinaryOperator::Opcode Op = B->getOpcode();
1053
Ted Kremenek2c369792008-02-25 18:42:54 +00001054 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1055 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +00001056
Ted Kremenekb31af242008-02-28 09:25:22 +00001057 // Check if the denominator is undefined.
Ted Kremenek2c369792008-02-25 18:42:54 +00001058
Ted Kremenek5a600862008-02-26 22:27:51 +00001059 if (!RightV.isUnknown()) {
1060
Ted Kremenekb31af242008-02-28 09:25:22 +00001061 if (RightV.isUndef()) {
1062 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek5a600862008-02-26 22:27:51 +00001063
Ted Kremenekb31af242008-02-28 09:25:22 +00001064 if (DivUndef) {
1065 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001066 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek5a600862008-02-26 22:27:51 +00001067 }
1068
1069 continue;
1070 }
1071
1072 // Check for divide/remainder-by-zero.
1073 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001074 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001075
Ted Kremenek75f32c62008-03-07 19:04:53 +00001076 bool isFeasibleZero = false;
1077 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001078
Ted Kremenek5a600862008-02-26 22:27:51 +00001079 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001080
Ted Kremenek75f32c62008-03-07 19:04:53 +00001081 bool isFeasibleNotZero = false;
1082 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001083
Ted Kremenek75f32c62008-03-07 19:04:53 +00001084 // Create the node for the divide-by-zero (if it occurred).
1085
1086 if (isFeasibleZero)
1087 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1088 DivZeroNode->markAsSink();
1089
1090 if (isFeasibleNotZero)
1091 ImplicitBadDivides.insert(DivZeroNode);
1092 else
1093 ExplicitBadDivides.insert(DivZeroNode);
1094
1095 }
1096
1097 if (!isFeasibleNotZero)
Ted Kremenek5a600862008-02-26 22:27:51 +00001098 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001099 }
1100
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001101 // Fall-through. The logic below processes the divide.
1102 }
1103
Ted Kremenekc2d07202008-02-28 20:32:03 +00001104
Ted Kremenek15cb0782008-02-06 22:50:25 +00001105 if (Op <= BinaryOperator::Or) {
1106
Ted Kremenek07baa252008-02-21 18:02:17 +00001107 // Process non-assignements except commas or short-circuited
1108 // logical expressions (LAnd and LOr).
1109
1110 RVal Result = EvalBinOp(Op, LeftV, RightV);
1111
1112 if (Result.isUnknown()) {
1113 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001114 continue;
1115 }
1116
Ted Kremenekc2d07202008-02-28 20:32:03 +00001117 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1118
1119 // The operands were not undefined, but the result is undefined.
1120
1121 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1122 UndefNode->markAsSink();
1123 UndefResults.insert(UndefNode);
1124 }
1125
1126 continue;
1127 }
1128
Ted Kremenek07baa252008-02-21 18:02:17 +00001129 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001130 continue;
1131 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001132
1133 // Process assignments.
1134
1135 switch (Op) {
1136
Ted Kremenekf031b872008-01-23 19:59:44 +00001137 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001138
1139 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001140
Ted Kremenekb31af242008-02-28 09:25:22 +00001141 if (LeftV.isUndef()) {
1142 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001143 continue;
1144 }
1145
Ted Kremenekd4676512008-03-12 21:45:47 +00001146 // EXPERIMENTAL: "Conjured" symbols.
1147
1148 if (RightV.isUnknown()) {
1149 unsigned Count = Builder->getCurrentBlockCount();
1150 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1151
1152 RightV = B->getRHS()->getType()->isPointerType()
1153 ? cast<RVal>(lval::SymbolVal(Sym))
1154 : cast<RVal>(nonlval::SymbolVal(Sym));
1155 }
1156
1157 // Even if the LHS evaluates to an unknown L-Value, the entire
1158 // expression still evaluates to the RHS.
1159
Ted Kremenek07baa252008-02-21 18:02:17 +00001160 if (LeftV.isUnknown()) {
1161 St = SetRVal(St, B, RightV);
1162 break;
1163 }
Ted Kremenekd4676512008-03-12 21:45:47 +00001164
1165 // Simulate the effects of a "store": bind the value of the RHS
1166 // to the L-Value represented by the LHS.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001167
Ted Kremenek07baa252008-02-21 18:02:17 +00001168 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001169 break;
1170 }
1171
Ted Kremenek07baa252008-02-21 18:02:17 +00001172 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001173
Ted Kremenek07baa252008-02-21 18:02:17 +00001174 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001175
Ted Kremenek5a600862008-02-26 22:27:51 +00001176 assert (B->isCompoundAssignmentOp());
1177
1178 if (Op >= BinaryOperator::AndAssign)
1179 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1180 else
1181 ((int&) Op) -= BinaryOperator::MulAssign;
1182
Ted Kremenekb31af242008-02-28 09:25:22 +00001183 // Check if the LHS is undefined.
Ted Kremenek07baa252008-02-21 18:02:17 +00001184
Ted Kremenekb31af242008-02-28 09:25:22 +00001185 if (LeftV.isUndef()) {
1186 HandleUndefinedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001187 continue;
1188 }
1189
1190 if (LeftV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001191 assert (isa<UnknownVal>(GetRVal(St, B)));
1192 Dst.Add(N2);
1193 continue;
Ted Kremenekbf988d02008-02-19 00:22:37 +00001194 }
1195
Ted Kremenek07baa252008-02-21 18:02:17 +00001196 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001197 // that is neither "Unknown" or "Undefined."
Ted Kremenek07baa252008-02-21 18:02:17 +00001198
1199 LVal LeftLV = cast<LVal>(LeftV);
1200
Ted Kremenek07baa252008-02-21 18:02:17 +00001201 // Fetch the value of the LHS (the value of the variable, etc.).
1202
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001203 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenek07baa252008-02-21 18:02:17 +00001204
Ted Kremenekb31af242008-02-28 09:25:22 +00001205 // Propagate undefined value (left-side). We
1206 // propogate undefined values for the RHS below when
Ted Kremenek5a600862008-02-26 22:27:51 +00001207 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001208
Ted Kremenekb31af242008-02-28 09:25:22 +00001209 if (V.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001210 St = SetRVal(St, B, V);
1211 break;
1212 }
1213
1214 // Propagate unknown values.
1215
Ted Kremenekb8782e12008-02-21 19:15:37 +00001216 if (V.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001217 // The value bound to LeftV is unknown. Thus we just
1218 // propagate the current node (as "B" is already bound to nothing).
1219 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenekb8782e12008-02-21 19:15:37 +00001220 Dst.Add(N2);
1221 continue;
1222 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001223
1224 if (RightV.isUnknown()) {
Ted Kremenek80cb2c82008-03-09 08:12:37 +00001225 assert (isa<UnknownVal>(GetRVal(St, B)));
1226 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenek07baa252008-02-21 18:02:17 +00001227 break;
1228 }
1229
Ted Kremenek5a600862008-02-26 22:27:51 +00001230 // At this point:
1231 //
Ted Kremenekb31af242008-02-28 09:25:22 +00001232 // The LHS is not Undef/Unknown.
Ted Kremenek5a600862008-02-26 22:27:51 +00001233 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001234
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001235 // Get the computation type.
1236 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1237
1238 // Perform promotions.
1239 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001240 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001241
1242 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001243
Ted Kremenek2c369792008-02-25 18:42:54 +00001244 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1245 && RHS->getType()->isIntegerType()) {
1246
Ted Kremenekb31af242008-02-28 09:25:22 +00001247 // Check if the denominator is undefined.
Ted Kremenekef0007f2008-02-26 02:15:56 +00001248
Ted Kremenekb31af242008-02-28 09:25:22 +00001249 if (RightV.isUndef()) {
1250 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001251
Ted Kremenekb31af242008-02-28 09:25:22 +00001252 if (DivUndef) {
1253 DivUndef->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001254 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekef0007f2008-02-26 02:15:56 +00001255 }
1256
1257 continue;
1258 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001259
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001260 // First, "assume" that the denominator is 0.
1261
Ted Kremenek75f32c62008-03-07 19:04:53 +00001262 bool isFeasibleZero = false;
1263 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001264
Ted Kremenek75f32c62008-03-07 19:04:53 +00001265 // Second, "assume" that the denominator cannot be 0.
1266
1267 bool isFeasibleNotZero = false;
1268 St = Assume(St, RightV, true, isFeasibleNotZero);
1269
1270 // Create the node for the divide-by-zero error (if it occurred).
1271
1272 if (isFeasibleZero) {
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001273 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1274
1275 if (DivZeroNode) {
1276 DivZeroNode->markAsSink();
Ted Kremenek75f32c62008-03-07 19:04:53 +00001277
1278 if (isFeasibleNotZero)
1279 ImplicitBadDivides.insert(DivZeroNode);
1280 else
1281 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001282 }
1283 }
1284
Ted Kremenek75f32c62008-03-07 19:04:53 +00001285 if (!isFeasibleNotZero)
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001286 continue;
1287
1288 // Fall-through. The logic below processes the divide.
1289 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001290 else {
1291
Ted Kremenekb31af242008-02-28 09:25:22 +00001292 // Propagate undefined values (right-side).
Ted Kremenek5a600862008-02-26 22:27:51 +00001293
Ted Kremenekb31af242008-02-28 09:25:22 +00001294 if (RightV.isUndef()) {
Ted Kremenek5a600862008-02-26 22:27:51 +00001295 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1296 break;
1297 }
1298
1299 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001300
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001301 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekc2d07202008-02-28 20:32:03 +00001302
1303 if (Result.isUndef()) {
1304
1305 // The operands were not undefined, but the result is undefined.
1306
1307 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1308 UndefNode->markAsSink();
1309 UndefResults.insert(UndefNode);
1310 }
1311
1312 continue;
1313 }
1314
Ted Kremenek07baa252008-02-21 18:02:17 +00001315 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001316 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001317 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001318
1319 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001320 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001321 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001322}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001323
Ted Kremenekb31af242008-02-28 09:25:22 +00001324void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001325 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001326 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +00001327 UndefStores.insert(N);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001328}
Ted Kremenekbe962452008-01-16 19:42:59 +00001329
Ted Kremenekbf988d02008-02-19 00:22:37 +00001330void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001331
1332 // FIXME: add metadata to the CFG so that we can disable
1333 // this check when we KNOW that there is no block-level subexpression.
1334 // The motivation is that this check requires a hashtable lookup.
1335
1336 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1337 Dst.Add(Pred);
1338 return;
1339 }
1340
1341 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001342
1343 default:
1344 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001345 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001346
1347 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1348 break;
1349
Ted Kremenek744a7862008-02-08 20:29:23 +00001350 case Stmt::BinaryOperatorClass: {
1351 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001352
Ted Kremenek744a7862008-02-08 20:29:23 +00001353 if (B->isLogicalOp()) {
1354 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001355 break;
1356 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001357 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001358 ValueState* St = GetState(Pred);
Ted Kremenek07baa252008-02-21 18:02:17 +00001359 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001360 break;
1361 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001362
1363 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1364 break;
1365 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001366
1367 case Stmt::CallExprClass: {
1368 CallExpr* C = cast<CallExpr>(S);
1369 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1370 break;
1371 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001372
1373 case Stmt::CastExprClass: {
1374 CastExpr* C = cast<CastExpr>(S);
1375 VisitCast(C, C->getSubExpr(), Pred, Dst);
1376 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001377 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001378
Ted Kremenek07baa252008-02-21 18:02:17 +00001379 // FIXME: ChooseExpr is really a constant. We need to fix
1380 // the CFG do not model them as explicit control-flow.
1381
Ted Kremenekfd85f292008-02-12 19:49:57 +00001382 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1383 ChooseExpr* C = cast<ChooseExpr>(S);
1384 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1385 break;
1386 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001387
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001388 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001389 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1390 break;
1391
Ted Kremenekfd85f292008-02-12 19:49:57 +00001392 case Stmt::ConditionalOperatorClass: { // '?' operator
1393 ConditionalOperator* C = cast<ConditionalOperator>(S);
1394 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1395 break;
1396 }
1397
1398 case Stmt::DeclRefExprClass:
1399 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1400 break;
1401
1402 case Stmt::DeclStmtClass:
1403 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1404 break;
1405
1406 case Stmt::ImplicitCastExprClass: {
1407 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1408 VisitCast(C, C->getSubExpr(), Pred, Dst);
1409 break;
1410 }
1411
1412 case Stmt::ParenExprClass:
1413 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1414 break;
1415
1416 case Stmt::SizeOfAlignOfTypeExprClass:
1417 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1418 break;
1419
Ted Kremenek106f37c2008-02-08 07:05:39 +00001420 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001421 StmtExpr* SE = cast<StmtExpr>(S);
1422
Ted Kremenek50ef5ff2008-03-10 04:11:42 +00001423 ValueState* St = GetState(Pred);
Ted Kremenek744a7862008-02-08 20:29:23 +00001424 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001425 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001426 break;
1427 }
1428
Ted Kremenek07baa252008-02-21 18:02:17 +00001429 // FIXME: We may wish to always bind state to ReturnStmts so
1430 // that users can quickly query what was the state at the
1431 // exit points of a function.
1432
Ted Kremenekfd85f292008-02-12 19:49:57 +00001433 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001434 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1435 Visit(R, Pred, Dst);
1436 else
1437 Dst.Add(Pred);
1438
1439 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001440 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001441
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001442 case Stmt::UnaryOperatorClass: {
1443 UnaryOperator* U = cast<UnaryOperator>(S);
1444
Ted Kremenek07baa252008-02-21 18:02:17 +00001445 switch (U->getOpcode()) {
1446 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1447 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1448 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1449 default: VisitUnaryOperator(U, Pred, Dst); break;
1450 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001451
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001452 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001453 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001454 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001455}
1456
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001457//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001458// "Assume" logic.
1459//===----------------------------------------------------------------------===//
1460
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001461ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001462 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001463 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001464 switch (Cond.getSubKind()) {
1465 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001466 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001467 return St;
1468
Ted Kremenek13f31562008-02-06 00:54:14 +00001469 case lval::SymbolValKind:
1470 if (Assumption)
1471 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001472 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001473 else
1474 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek8ad19872008-03-07 20:13:31 +00001475 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek13f31562008-02-06 00:54:14 +00001476
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001477
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001478 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001479 case lval::FuncValKind:
1480 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001481 isFeasible = Assumption;
1482 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001483
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001484 case lval::ConcreteIntKind: {
1485 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001486 isFeasible = b ? Assumption : !Assumption;
1487 return St;
1488 }
1489 }
Ted Kremenek90960972008-01-30 23:03:39 +00001490}
1491
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001492ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001493 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001494 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001495 switch (Cond.getSubKind()) {
1496 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001497 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001498 return St;
1499
Ted Kremenekab359c12008-02-06 17:32:17 +00001500
1501 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001502 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001503 SymbolID sym = SV.getSymbol();
1504
1505 if (Assumption)
Ted Kremenek8ad19872008-03-07 20:13:31 +00001506 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001507 isFeasible);
1508 else
Ted Kremenek8ad19872008-03-07 20:13:31 +00001509 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekab359c12008-02-06 17:32:17 +00001510 isFeasible);
1511 }
1512
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001513 case nonlval::SymIntConstraintValKind:
1514 return
1515 AssumeSymInt(St, Assumption,
1516 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1517 isFeasible);
1518
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001519 case nonlval::ConcreteIntKind: {
1520 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001521 isFeasible = b ? Assumption : !Assumption;
1522 return St;
1523 }
1524 }
1525}
1526
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001527ValueState*
1528GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001529 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001530
Ted Kremenek13f31562008-02-06 00:54:14 +00001531 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001532 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001533 isFeasible = *X != V;
1534 return St;
1535 }
1536
1537 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001538 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001539 isFeasible = true;
1540 return St;
1541 }
1542
1543 // If we reach here, sym is not a constant and we don't know if it is != V.
1544 // Make that assumption.
1545
1546 isFeasible = true;
1547 return StateMgr.AddNE(St, sym, V);
1548}
1549
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001550ValueState*
1551GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001552 const llvm::APSInt& V, bool& isFeasible) {
1553
1554 // First, determine if sym == X, where X != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001555 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001556 isFeasible = *X == V;
1557 return St;
1558 }
1559
1560 // Second, determine if sym != V.
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001561 if (St->isNotEqual(sym, V)) {
Ted Kremenek13f31562008-02-06 00:54:14 +00001562 isFeasible = false;
1563 return St;
1564 }
1565
1566 // If we reach here, sym is not a constant and we don't know if it is == V.
1567 // Make that assumption.
1568
1569 isFeasible = true;
1570 return StateMgr.AddEQ(St, sym, V);
1571}
Ted Kremenek90960972008-01-30 23:03:39 +00001572
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001573ValueState*
1574GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001575 const SymIntConstraint& C, bool& isFeasible) {
1576
1577 switch (C.getOpcode()) {
1578 default:
1579 // No logic yet for other operators.
Ted Kremenekd4676512008-03-12 21:45:47 +00001580 isFeasible = true;
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001581 return St;
1582
1583 case BinaryOperator::EQ:
1584 if (Assumption)
1585 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1586 else
1587 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1588
1589 case BinaryOperator::NE:
1590 if (Assumption)
1591 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1592 else
1593 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1594 }
1595}
1596
Ted Kremenek90960972008-01-30 23:03:39 +00001597//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001598// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001599//===----------------------------------------------------------------------===//
1600
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001601#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001602static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001603static SourceManager* GraphPrintSourceManager;
Ted Kremenek9f597922008-03-11 19:02:40 +00001604static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001605
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001606namespace llvm {
1607template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001608struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001609 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001610
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001611 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001612
1613 Out << "Variables:\\l";
1614
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001615 bool isFirst = true;
1616
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001617 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001618
1619 if (isFirst)
1620 isFirst = false;
1621 else
1622 Out << "\\l";
1623
1624 Out << ' ' << I.getKey()->getName() << " : ";
1625 I.getData().print(Out);
1626 }
1627
1628 }
1629
Ted Kremenek17c5f112008-02-11 19:21:59 +00001630
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001631 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001632
1633 bool isFirst = true;
1634
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001635 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001636
1637 if (isFirst) {
1638 Out << "\\l\\lSub-Expressions:\\l";
1639 isFirst = false;
1640 }
1641 else
1642 Out << "\\l";
1643
1644 Out << " (" << (void*) I.getKey() << ") ";
1645 I.getKey()->printPretty(Out);
1646 Out << " : ";
1647 I.getData().print(Out);
1648 }
1649 }
1650
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001651 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001652
Ted Kremenek08cfd832008-02-08 21:10:02 +00001653 bool isFirst = true;
1654
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001655 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001656 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001657 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001658 isFirst = false;
1659 }
1660 else
1661 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001662
Ted Kremenek17c5f112008-02-11 19:21:59 +00001663 Out << " (" << (void*) I.getKey() << ") ";
1664 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001665 Out << " : ";
1666 I.getData().print(Out);
1667 }
1668 }
1669
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001670 static void PrintEQ(std::ostream& Out, ValueState* St) {
1671 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001672
1673 if (CE.isEmpty())
1674 return;
1675
1676 Out << "\\l\\|'==' constraints:";
1677
Ted Kremenek07baa252008-02-21 18:02:17 +00001678 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001679 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1680 }
1681
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001682 static void PrintNE(std::ostream& Out, ValueState* St) {
1683 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001684
1685 if (NE.isEmpty())
1686 return;
1687
1688 Out << "\\l\\|'!=' constraints:";
1689
Ted Kremenek07baa252008-02-21 18:02:17 +00001690 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001691 I != EI; ++I){
1692
1693 Out << "\\l $" << I.getKey() << " : ";
1694 bool isFirst = true;
1695
1696 ValueState::IntSetTy::iterator J=I.getData().begin(),
1697 EJ=I.getData().end();
1698 for ( ; J != EJ; ++J) {
1699 if (isFirst) isFirst = false;
1700 else Out << ", ";
1701
1702 Out << (*J)->toString();
1703 }
1704 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001705 }
1706
1707 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1708
1709 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001710 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00001711 GraphPrintCheckerState->isUndefDeref(N) ||
1712 GraphPrintCheckerState->isUndefStore(N) ||
1713 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00001714 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1715 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00001716 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001717 GraphPrintCheckerState->isBadCall(N) ||
1718 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001719 return "color=\"red\",style=\"filled\"";
1720
Ted Kremenekc2d07202008-02-28 20:32:03 +00001721 if (GraphPrintCheckerState->isNoReturnCall(N))
1722 return "color=\"blue\",style=\"filled\"";
1723
Ted Kremeneka853de62008-02-14 22:54:53 +00001724 return "";
1725 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001726
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001727 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001728 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001729
1730 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001731 ProgramPoint Loc = N->getLocation();
1732
1733 switch (Loc.getKind()) {
1734 case ProgramPoint::BlockEntranceKind:
1735 Out << "Block Entrance: B"
1736 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1737 break;
1738
1739 case ProgramPoint::BlockExitKind:
1740 assert (false);
1741 break;
1742
1743 case ProgramPoint::PostStmtKind: {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001744 const PostStmt& L = cast<PostStmt>(Loc);
1745 Stmt* S = L.getStmt();
1746 SourceLocation SLoc = S->getLocStart();
1747
1748 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
1749 S->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001750
Ted Kremenekf97c6682008-03-09 03:30:59 +00001751 if (SLoc.isFileID()) {
1752 Out << "\\lline="
1753 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1754 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
1755 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001756
Ted Kremenek43863eb2008-02-29 23:14:48 +00001757 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenek80d52d02008-02-07 05:48:01 +00001758 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001759 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001760 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001761 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001762 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001763 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenekb31af242008-02-28 09:25:22 +00001764 Out << "\\|Store to Undefined LVal.";
Ted Kremenek75f32c62008-03-07 19:04:53 +00001765 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1766 Out << "\\|Explicit divide-by zero or undefined value.";
1767 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
1768 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001769 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenekc2d07202008-02-28 20:32:03 +00001770 Out << "\\|Result of operation is undefined.";
Ted Kremenekc2d07202008-02-28 20:32:03 +00001771 else if (GraphPrintCheckerState->isNoReturnCall(N))
1772 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek43863eb2008-02-29 23:14:48 +00001773 else if (GraphPrintCheckerState->isBadCall(N))
1774 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001775 else if (GraphPrintCheckerState->isUndefArg(N))
1776 Out << "\\|Argument in call is undefined";
Ted Kremenek80d52d02008-02-07 05:48:01 +00001777
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001778 break;
1779 }
1780
1781 default: {
1782 const BlockEdge& E = cast<BlockEdge>(Loc);
1783 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1784 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001785
1786 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001787
1788 SourceLocation SLoc = T->getLocStart();
1789
Ted Kremenek90960972008-01-30 23:03:39 +00001790 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001791
Ted Kremenek90960972008-01-30 23:03:39 +00001792 E.getSrc()->printTerminator(Out);
1793
Ted Kremenekf97c6682008-03-09 03:30:59 +00001794 if (SLoc.isFileID()) {
1795 Out << "\\lline="
1796 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1797 << GraphPrintSourceManager->getColumnNumber(SLoc);
1798 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001799
Ted Kremenekaee121c2008-02-13 23:08:21 +00001800 if (isa<SwitchStmt>(T)) {
1801 Stmt* Label = E.getDst()->getLabel();
1802
1803 if (Label) {
1804 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1805 Out << "\\lcase ";
1806 C->getLHS()->printPretty(Out);
1807
1808 if (Stmt* RHS = C->getRHS()) {
1809 Out << " .. ";
1810 RHS->printPretty(Out);
1811 }
1812
1813 Out << ":";
1814 }
1815 else {
1816 assert (isa<DefaultStmt>(Label));
1817 Out << "\\ldefault:";
1818 }
1819 }
1820 else
1821 Out << "\\l(implicit) default:";
1822 }
1823 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001824 // FIXME
1825 }
1826 else {
1827 Out << "\\lCondition: ";
1828 if (*E.getSrc()->succ_begin() == E.getDst())
1829 Out << "true";
1830 else
1831 Out << "false";
1832 }
1833
1834 Out << "\\l";
1835 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001836
Ted Kremenekb31af242008-02-28 09:25:22 +00001837 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1838 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00001839 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001840 }
1841 }
1842
Ted Kremenekf4b49df2008-02-28 10:21:43 +00001843 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001844
Ted Kremenek9f597922008-03-11 19:02:40 +00001845 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001846
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001847 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001848 return Out.str();
1849 }
1850};
1851} // end llvm namespace
1852#endif
1853
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001854#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00001855
1856template <typename ITERATOR>
1857GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
1858
1859template <>
1860GRExprEngine::NodeTy*
1861GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
1862 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
1863 return I->first;
1864}
1865
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001866template <typename ITERATOR>
1867static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources,
Ted Kremenek83f04aa2008-03-12 17:18:20 +00001868 ITERATOR I, ITERATOR E) {
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001869
Ted Kremenek83f04aa2008-03-12 17:18:20 +00001870 llvm::SmallPtrSet<void*,10> CachedSources;
1871
1872 for ( ; I != E; ++I ) {
1873 GRExprEngine::NodeTy* N = GetGraphNode(I);
1874 void* p = N->getLocation().getRawData();
1875
1876 if (CachedSources.count(p))
1877 continue;
1878
1879 CachedSources.insert(p);
1880
1881 Sources.push_back(N);
1882 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001883}
1884#endif
1885
1886void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00001887#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001888 if (trim) {
Ted Kremenek83f04aa2008-03-12 17:18:20 +00001889 llvm::SmallVector<NodeTy*, 10> Src;
1890 AddSources(Src, null_derefs_begin(), null_derefs_end());
1891 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
1892 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
1893 AddSources(Src, undef_results_begin(), undef_results_end());
1894 AddSources(Src, bad_calls_begin(), bad_calls_end());
1895 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek2f0c0e12008-03-14 18:14:50 +00001896 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001897
Ted Kremenek83f04aa2008-03-12 17:18:20 +00001898 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001899 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00001900 else {
1901 GraphPrintCheckerState = this;
1902 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00001903 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremeneke44a8302008-03-11 18:25:33 +00001904
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001905 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00001906
1907 GraphPrintCheckerState = NULL;
1908 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00001909 GraphCheckerStatePrinter = NULL;
Ted Kremeneke44a8302008-03-11 18:25:33 +00001910 }
1911#endif
1912}
1913
1914void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
1915#ifndef NDEBUG
1916 GraphPrintCheckerState = this;
1917 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek9f597922008-03-11 19:02:40 +00001918 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001919
Ted Kremeneke44a8302008-03-11 18:25:33 +00001920 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
1921
1922 if (!TrimmedG)
1923 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
1924 else {
1925 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
1926 delete TrimmedG;
1927 }
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00001928
Ted Kremenek428d39e2008-01-30 23:24:39 +00001929 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00001930 GraphPrintSourceManager = NULL;
Ted Kremenek9f597922008-03-11 19:02:40 +00001931 GraphCheckerStatePrinter = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001932#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001933}