blob: bf0d5ea39551c0ae948868abd16ddba100e58ba1 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000017#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000018#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000019
Ted Kremenek0f5f0592008-02-27 06:07:00 +000020#ifndef NDEBUG
21#include "llvm/Support/GraphWriter.h"
22#include <sstream>
23#endif
24
Ted Kremenek4bf38da2008-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 Kremenekb387a3f2008-02-14 22:16:04 +000037using namespace clang;
38using llvm::dyn_cast;
39using llvm::cast;
40using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000041
Ted Kremenekdaa497e2008-03-09 18:05:48 +000042
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000043ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-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 Kremenekaed9b6a2008-02-28 10:21:43 +000052 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-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 Kremenekaed9b6a2008-02-28 10:21:43 +000067ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000068
Ted Kremeneke070a1d2008-02-04 21:59:01 +000069 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000070
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000071 if (Ex == CurrentStmt) {
72 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000073
74 if (!isBlkExpr)
75 return St;
76 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000077
Ted Kremenek5a7b3822008-02-26 23:37:01 +000078 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000079}
80
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000081ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
82 bool branchTaken) {
Ted Kremenek05a23782008-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 Kremenek4a4e5242008-02-28 09:25:22 +0000105 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-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 Kremenek4a4e5242008-02-28 09:25:22 +0000122 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-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 Kremenek4a4e5242008-02-28 09:25:22 +0000130 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000131 }
132 }
133}
134
Ted Kremenek6a6719a2008-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 Kremenek4d4dd852008-02-13 17:41:41 +0000141void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000142 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000143
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000145 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000146
Ted Kremenekb2331832008-02-15 22:29:00 +0000147 // Check for NULL conditions; e.g. "for(;;)"
148 if (!Condition) {
149 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000150 return;
151 }
152
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000153 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000154
155 switch (V.getBaseKind()) {
156 default:
157 break;
158
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000159 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000160 builder.generateNode(MarkBranch(PrevState, Term, true), true);
161 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000162 return;
163
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000164 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000165 NodeTy* N = builder.generateNode(PrevState, true);
166
167 if (N) {
168 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000169 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000170 }
171
172 builder.markInfeasible(false);
173 return;
174 }
175 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000176
Ted Kremenekb38911f2008-01-30 23:03:39 +0000177
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000178 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000179
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000180 bool isFeasible = false;
Ted Kremenek6a6719a2008-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 Kremenek8e49dd62008-02-12 18:08:17 +0000185 else
186 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000187
188 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000189
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000190 isFeasible = false;
191 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000192
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000193 if (isFeasible)
194 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000195 else
196 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000197}
198
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000199/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000200/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000201void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000202
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000203 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000204 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000205
206 // Three possibilities:
207 //
208 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000209 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-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 Kremenek24f1a962008-02-13 17:27:37 +0000219 if (I.getLabel() == L) {
220 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000221 return;
222 }
223 }
224
225 assert (false && "No block with label.");
226 return;
227 }
228
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000229 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000230 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000231 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000232 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000233 return;
234 }
235
236 // This is really a catch-all. We don't support symbolics yet.
237
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000238 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000239
240 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000241 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000242}
Ted Kremenekf233d482008-02-05 00:26:40 +0000243
Ted Kremenekdaeb9a72008-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 Kremenekaed9b6a2008-02-28 10:21:43 +0000250 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000251 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000252 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000253
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000254 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000255 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000256 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000257 return;
258 }
259
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000260 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-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 Kremenek692416c2008-02-18 22:57:02 +0000264
Chris Lattner98be4942008-03-05 18:54:05 +0000265 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000266
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000267 APSInt V1(bits, false);
268 APSInt V2 = V1;
269
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000270 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000271
272 CaseStmt* Case = cast<CaseStmt>(I.getCase());
273
274 // Evaluate the case.
275 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
276 assert (false && "Case condition must evaluate to an integer constant.");
277 return;
278 }
279
280 // Get the RHS of the case, if it exists.
281
282 if (Expr* E = Case->getRHS()) {
283 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
284 assert (false &&
285 "Case condition (RHS) must evaluate to an integer constant.");
286 return ;
287 }
288
289 assert (V1 <= V2);
290 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000291 else
292 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000293
294 // FIXME: Eventually we should replace the logic below with a range
295 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000296 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000297
Ted Kremenek14a11402008-03-17 22:17:56 +0000298 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000299 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000300
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000301 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000302
303 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000304
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000305 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000306 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000307
308 if (isFeasible) {
309 builder.generateCaseStmtNode(I, StNew);
310
311 // If CondV evaluates to a constant, then we know that this
312 // is the *only* case that we can take, so stop evaluating the
313 // others.
314 if (isa<nonlval::ConcreteInt>(CondV))
315 return;
316 }
317
318 // Now "assume" that the case doesn't match. Add this state
319 // to the default state (if it is feasible).
320
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000321 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000322 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000323
324 if (isFeasible)
325 DefaultSt = StNew;
326
Ted Kremenek14a11402008-03-17 22:17:56 +0000327 // Concretize the next value in the range.
328 if (V1 == V2)
329 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000330
Ted Kremenek14a11402008-03-17 22:17:56 +0000331 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000332 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000333
334 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000335 }
336
337 // If we reach here, than we know that the default branch is
338 // possible.
339 builder.generateDefaultCaseNode(DefaultSt);
340}
341
342
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000343void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000344 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000345
Ted Kremenek05a23782008-02-26 19:05:15 +0000346 assert (B->getOpcode() == BinaryOperator::LAnd ||
347 B->getOpcode() == BinaryOperator::LOr);
348
349 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
350
Ted Kremenek0d093d32008-03-10 04:11:42 +0000351 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000352 RVal X = GetBlkExprRVal(St, B);
353
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000354 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000355
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000356 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000357
358 assert (Ex);
359
360 if (Ex == B->getRHS()) {
361
362 X = GetBlkExprRVal(St, Ex);
363
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000364 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000365
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000366 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000367 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000368 return;
369 }
370
Ted Kremenek05a23782008-02-26 19:05:15 +0000371 // We took the RHS. Because the value of the '&&' or '||' expression must
372 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
373 // or 1. Alternatively, we could take a lazy approach, and calculate this
374 // value later when necessary. We don't have the machinery in place for
375 // this right now, and since most logical expressions are used for branches,
376 // the payoff is not likely to be large. Instead, we do eager evaluation.
377
378 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000379 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000380
381 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000382 MakeNode(Dst, B, Pred,
383 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000384
385 isFeasible = false;
386 NewState = Assume(St, X, false, isFeasible);
387
388 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000389 MakeNode(Dst, B, Pred,
390 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000391 }
392 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000393 // We took the LHS expression. Depending on whether we are '&&' or
394 // '||' we know what the value of the expression is via properties of
395 // the short-circuiting.
396
397 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000398 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000399 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000400}
Ted Kremenek05a23782008-02-26 19:05:15 +0000401
Ted Kremenekf233d482008-02-05 00:26:40 +0000402
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000403void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000404
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000405 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000406 StmtEntryNode = builder.getLastNode();
407 CurrentStmt = S;
408 NodeSet Dst;
Ted Kremenek512c9132008-03-09 18:28:41 +0000409
Ted Kremenek99c6ad32008-03-27 07:25:52 +0000410 // Set up our simple checks.
411
412 if (!MsgExprChecks.empty())
413 Builder->setObjCMsgExprAuditors(
Ted Kremeneke5d5c202008-03-27 21:15:17 +0000414 (GRAuditor<ValueState>**) &MsgExprChecks[0],
415 (GRAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size()));
Ted Kremenek99c6ad32008-03-27 07:25:52 +0000416
417
418 if (!CallChecks.empty())
419 Builder->setCallExprAuditors(
Ted Kremeneke5d5c202008-03-27 21:15:17 +0000420 (GRAuditor<ValueState>**) &CallChecks[0],
421 (GRAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size()));
Ted Kremenek99c6ad32008-03-27 07:25:52 +0000422
Ted Kremenek512c9132008-03-09 18:28:41 +0000423 // Create the cleaned state.
424
Ted Kremenek0d093d32008-03-10 04:11:42 +0000425 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
426 CurrentStmt, Liveness);
Ted Kremenek3bbad552008-03-10 04:45:00 +0000427
428 Builder->SetCleanedState(CleanedState);
Ted Kremenek512c9132008-03-09 18:28:41 +0000429
430 // Visit the statement.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000431
432 Visit(S, StmtEntryNode, Dst);
433
434 // If no nodes were generated, generate a new node that has all the
435 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000436
Ted Kremenek0d093d32008-03-10 04:11:42 +0000437 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode)
438 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
Ted Kremenekf84469b2008-01-18 00:41:32 +0000439
Ted Kremenek0d093d32008-03-10 04:11:42 +0000440 // NULL out these variables to cleanup.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000441
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000442 CurrentStmt = NULL;
443 StmtEntryNode = NULL;
444 Builder = NULL;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000445 CleanedState = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000446}
447
Ted Kremenek44842c22008-02-13 18:06:44 +0000448void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000449
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000450 if (D != CurrentStmt) {
451 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
452 return;
453 }
454
455 // If we are here, we are loading the value of the decl and binding
456 // it to the block-level expression.
457
Ted Kremenek0d093d32008-03-10 04:11:42 +0000458 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000459 RVal X = RVal::MakeVal(BasicVals, D);
460 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenek0e561a32008-03-21 21:30:14 +0000461 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000462}
463
Ted Kremenekde434242008-02-19 01:44:53 +0000464void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000465 CallExpr::arg_iterator AI,
466 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000467 NodeSet& Dst) {
468
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000469 // Process the arguments.
470
471 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000472
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000473 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000474 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000475 ++AI;
476
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000477 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000478 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000479
480 return;
481 }
482
483 // If we reach here we have processed all of the arguments. Evaluate
484 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000485
Ted Kremenek994a09b2008-02-25 21:16:03 +0000486 NodeSet DstTmp;
487 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000488
Ted Kremenek994a09b2008-02-25 21:16:03 +0000489 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000490
491 if (DstTmp.empty())
492 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000493
494 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000495 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
496
Ted Kremenek0d093d32008-03-10 04:11:42 +0000497 ValueState* St = GetState(*DI);
Ted Kremenek994a09b2008-02-25 21:16:03 +0000498 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000499
Ted Kremeneka1354a52008-03-03 16:47:31 +0000500 // FIXME: Add support for symbolic function calls (calls involving
501 // function pointer values that are symbolic).
502
503 // Check for undefined control-flow or calls to NULL.
504
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000505 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000506 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000507
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000508 if (N) {
509 N->markAsSink();
510 BadCalls.insert(N);
511 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000512
Ted Kremenekde434242008-02-19 01:44:53 +0000513 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000514 }
515
516 // Check for the "noreturn" attribute.
517
518 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
519
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000520 if (isa<lval::FuncVal>(L)) {
521
522 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
523
524 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000525 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000526 else {
527 // HACK: Some functions are not marked noreturn, and don't return.
528 // Here are a few hardwired ones. If this takes too long, we can
529 // potentially cache these results.
530 const char* s = FD->getIdentifier()->getName();
531 unsigned n = strlen(s);
532
533 switch (n) {
534 default:
535 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000536
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000537 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000538 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
539 break;
540
541 case 5:
542 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
543 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000544 }
545 }
546 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000547
548 // Evaluate the call.
549
Ted Kremeneka1354a52008-03-03 16:47:31 +0000550
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000551 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000552
Ted Kremenek03da0d72008-02-21 19:46:04 +0000553 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000554 // Check for an "unknown" callee.
555 invalidateArgs = true;
556 }
557 else if (isa<lval::FuncVal>(L)) {
558
559 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
560
Ted Kremenek55aea312008-03-05 22:59:42 +0000561 if (unsigned id = Info->getBuiltinID()) {
562 switch (id) {
563 case Builtin::BI__builtin_expect: {
564 // For __builtin_expect, just return the value of the subexpression.
565 assert (CE->arg_begin() != CE->arg_end());
566 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +0000567 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +0000568 continue;
569 }
570
571 default:
572 invalidateArgs = true;
573 break;
574 }
575 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000576 }
577
578 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000579 // Invalidate all arguments passed in by reference (LVals).
580 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
581 I != E; ++I) {
582 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000583
Ted Kremenek03da0d72008-02-21 19:46:04 +0000584 if (isa<LVal>(V))
585 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000586 }
587
Ted Kremenek0e561a32008-03-21 21:30:14 +0000588 MakeNode(Dst, CE, *DI, St);
Ted Kremenek03da0d72008-02-21 19:46:04 +0000589 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000590 else {
591
592 // Check any arguments passed-by-value against being undefined.
593
594 bool badArg = false;
595
596 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
597 I != E; ++I) {
598
Ted Kremenek0d093d32008-03-10 04:11:42 +0000599 if (GetRVal(GetState(*DI), *I).isUndef()) {
600 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000601
602 if (N) {
603 N->markAsSink();
604 UndefArgs[N] = *I;
605 }
606
607 badArg = true;
608 break;
609 }
610 }
611
612 if (badArg)
613 continue;
614
615 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000616
617 unsigned size = Dst.size();
618
619 EvalCall(Dst, CE, cast<LVal>(L), *DI);
620
Ted Kremenek5dc7f8b2008-03-05 22:49:16 +0000621 if (!Builder->BuildSinks && Dst.size() == size)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000622 MakeNode(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000623 }
Ted Kremenekde434242008-02-19 01:44:53 +0000624 }
625}
626
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000627void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000628
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000629 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000630 QualType T = CastE->getType();
631
Ted Kremenek65cfb732008-03-04 22:16:08 +0000632 if (T->isReferenceType())
633 VisitLVal(Ex, Pred, S1);
634 else
635 Visit(Ex, Pred, S1);
636
Ted Kremenek402563b2008-02-19 18:47:04 +0000637 // Check for redundant casts or casting to "void"
638 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000639 Ex->getType() == T ||
640 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000641
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000642 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000643 Dst.Add(*I1);
644
Ted Kremenek874d63f2008-01-24 02:02:54 +0000645 return;
646 }
647
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000648 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000649 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000650 ValueState* St = GetState(N);
Ted Kremenek65cfb732008-03-04 22:16:08 +0000651
652 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
653
Ted Kremenek0e561a32008-03-21 21:30:14 +0000654 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000655 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000656}
657
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000658void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000659 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000660
Ted Kremenek0d093d32008-03-10 04:11:42 +0000661 ValueState* St = GetState(Pred);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000662
663 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000664 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000665
666 // FIXME: Add support for local arrays.
667 if (VD->getType()->isArrayType())
668 continue;
669
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000670 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000671
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000672 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
673
674 // In this context, Static => Local variable.
675
676 assert (!VD->getStorageClass() == VarDecl::Static ||
677 !isa<FileVarDecl>(VD));
678
679 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000680 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000681 //
682 // FIXME: static variables may have an initializer, but the second
683 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000684
685 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000686
Ted Kremenek16d81562008-03-04 04:18:04 +0000687 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +0000688
689 // C99: 6.7.8 Initialization
690 // If an object that has static storage duration is not initialized
691 // explicitly, then:
692 // —if it has pointer type, it is initialized to a null pointer;
693 // —if it has arithmetic type, it is initialized to (positive or
694 // unsigned) zero;
695
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000696 // FIXME: Handle structs. Now we treat their values as unknown.
697
Ted Kremenek16d81562008-03-04 04:18:04 +0000698 if (T->isPointerType()) {
699
700 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +0000701 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +0000702 }
703 else if (T->isIntegerType()) {
704
705 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +0000706 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +0000707 }
708
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000709
Ted Kremenek16d81562008-03-04 04:18:04 +0000710 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000711 else {
Ted Kremenek16d81562008-03-04 04:18:04 +0000712
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000713 // FIXME: Handle structs. Now we treat them as unknown. What
714 // we need to do is treat their members as unknown.
715
716 if (T->isPointerType() || T->isIntegerType())
717 St = SetRVal(St, lval::DeclVal(VD),
718 Ex ? GetRVal(St, Ex) : UndefinedVal());
719 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000720 }
Ted Kremenek403c1812008-01-28 22:51:57 +0000721 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000722
Ted Kremenek0e561a32008-03-21 21:30:14 +0000723 MakeNode(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000724}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000725
Ted Kremenekf233d482008-02-05 00:26:40 +0000726
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000727void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000728 NodeTy* Pred, NodeSet& Dst) {
729
Ted Kremenek05a23782008-02-26 19:05:15 +0000730 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
731
Ted Kremenek0d093d32008-03-10 04:11:42 +0000732 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000733 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000734
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000735 assert (X.isUndef());
Ted Kremenekf233d482008-02-05 00:26:40 +0000736
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000737 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000738
739 assert (SE);
740
741 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000742
743 // Make sure that we invalidate the previous binding.
Ted Kremenek0e561a32008-03-21 21:30:14 +0000744 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000745}
746
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000747/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000748void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
749 NodeTy* Pred,
750 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000751
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000752 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +0000753 uint64_t amt;
754
755 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000756
Ted Kremenek87e80342008-03-15 03:13:20 +0000757 // FIXME: Add support for VLAs.
758 if (!T.getTypePtr()->isConstantSizeType())
759 return;
760
761 amt = 1; // Handle sizeof(void)
762
763 if (T != getContext().VoidTy)
764 amt = getContext().getTypeSize(T) / 8;
765
766 }
767 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +0000768 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000769
Ted Kremenek0e561a32008-03-21 21:30:14 +0000770 MakeNode(Dst, Ex, Pred,
Ted Kremenek0d093d32008-03-10 04:11:42 +0000771 SetRVal(GetState(Pred), Ex,
Ted Kremenek87e80342008-03-15 03:13:20 +0000772 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000773}
774
Ted Kremenekffe0f432008-03-07 22:58:01 +0000775void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
776 NodeSet& Dst, bool GetLVal) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000777
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000778 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000779
780 NodeSet DstTmp;
781
Ted Kremenek018c15f2008-02-26 03:44:25 +0000782 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000783 DstTmp.Add(Pred);
784 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000785 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000786
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000787 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000788
789 NodeTy* N = *I;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000790 ValueState* St = GetState(N);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000791
792 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
793
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000794 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000795
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000796 // Check for dereferences of undefined values.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000797
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000798 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000799
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000800 NodeTy* Succ = Builder->generateNode(U, St, N);
801
802 if (Succ) {
803 Succ->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000804 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000805 }
806
807 continue;
808 }
809
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000810 // Check for dereferences of unknown values. Treat as No-Ops.
811
812 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000813 Dst.Add(N);
814 continue;
815 }
816
817 // After a dereference, one of two possible situations arise:
818 // (1) A crash, because the pointer was NULL.
819 // (2) The pointer is not NULL, and the dereference works.
820 //
821 // We add these assumptions.
822
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000823 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000824 bool isFeasibleNotNull;
825
826 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000827
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000828 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000829
830 if (isFeasibleNotNull) {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000831
Ted Kremenek0e561a32008-03-21 21:30:14 +0000832 if (GetLVal) MakeNode(Dst, U, N, SetRVal(StNotNull, U, LV));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000833 else {
834
835 // FIXME: Currently symbolic analysis "generates" new symbols
836 // for the contents of values. We need a better approach.
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000837
Ted Kremenek0e561a32008-03-21 21:30:14 +0000838 MakeNode(Dst, U, N, SetRVal(StNotNull, U,
Ted Kremenekffe0f432008-03-07 22:58:01 +0000839 GetRVal(StNotNull, LV, U->getType())));
840 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000841 }
842
843 bool isFeasibleNull;
844
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000845 // Now "assume" that the pointer is NULL.
846
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000847 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000848
849 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000850
Ted Kremenek0e561a32008-03-21 21:30:14 +0000851 // We don't use "MakeNode" here because the node will be a sink
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000852 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000853
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000854 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
855
856 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000857
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000858 NullNode->markAsSink();
859
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000860 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
861 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000862 }
863 }
864 }
865}
866
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000867void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
868 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000869
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000870 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000871
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000872 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000873 assert (U->getOpcode() != UnaryOperator::SizeOf);
874 assert (U->getOpcode() != UnaryOperator::AlignOf);
875
876 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000877
878 switch (U->getOpcode()) {
879 case UnaryOperator::PostInc:
880 case UnaryOperator::PostDec:
881 case UnaryOperator::PreInc:
882 case UnaryOperator::PreDec:
883 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000884 // Evalue subexpression as an LVal.
885 use_GetLVal = true;
886 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000887 break;
888
889 default:
890 Visit(U->getSubExpr(), Pred, S1);
891 break;
892 }
893
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000894 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000895
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000896 NodeTy* N1 = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +0000897 ValueState* St = GetState(N1);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000898
899 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
900 GetRVal(St, U->getSubExpr());
901
902 if (SubV.isUnknown()) {
903 Dst.Add(N1);
904 continue;
905 }
906
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000907 if (SubV.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000908 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000909 continue;
910 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000911
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000912 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000913
914 // Handle ++ and -- (both pre- and post-increment).
915
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000916 LVal SubLV = cast<LVal>(SubV);
917 RVal V = GetRVal(St, SubLV, U->getType());
918
Ted Kremenek89063af2008-02-21 19:15:37 +0000919 if (V.isUnknown()) {
920 Dst.Add(N1);
921 continue;
922 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000923
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000924 // Propagate undefined values.
925 if (V.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000926 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000927 continue;
928 }
929
Ted Kremenek443003b2008-02-21 19:29:23 +0000930 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000931
932 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
933 : BinaryOperator::Sub;
934
Ted Kremenek443003b2008-02-21 19:29:23 +0000935 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000936
937 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000938 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000939 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000940 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000941
Ted Kremenek0e561a32008-03-21 21:30:14 +0000942 MakeNode(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000943 continue;
944 }
945
946 // Handle all other unary operators.
947
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000948 switch (U->getOpcode()) {
Ted Kremenek5139c782008-03-15 03:05:30 +0000949
950 case UnaryOperator::Extension:
951 St = SetRVal(St, U, SubV);
952 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000953
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000954 case UnaryOperator::Minus:
955 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000956 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000957
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000958 case UnaryOperator::Not:
959 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000960 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000961
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000962 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000963
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000964 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
965 //
966 // Note: technically we do "E == 0", but this is the same in the
967 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000968
969 if (isa<LVal>(SubV)) {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000970 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000971 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
972 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000973 }
974 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000975 Expr* Ex = U->getSubExpr();
Ted Kremenek240f1f02008-03-07 20:13:31 +0000976 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000977 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
978 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000979 }
980
981 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000982
Ted Kremenek64924852008-01-31 02:35:41 +0000983 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000984 assert (isa<LVal>(SubV));
985 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000986 break;
987 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000988
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000989 default: ;
990 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000991 }
992
Ted Kremenek0e561a32008-03-21 21:30:14 +0000993 MakeNode(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000994 }
995}
996
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000997void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
998 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000999
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001000 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001001
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001002 // FIXME: Add support for VLAs.
1003 if (!T.getTypePtr()->isConstantSizeType())
1004 return;
1005
Chris Lattner98be4942008-03-05 18:54:05 +00001006 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001007 ValueState* St = GetState(Pred);
Ted Kremenek240f1f02008-03-07 20:13:31 +00001008 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001009
Ted Kremenek0e561a32008-03-21 21:30:14 +00001010 MakeNode(Dst, U, Pred, St);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001011}
1012
1013void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +00001014
1015 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1016 Dst.Add(Pred);
1017 return;
1018 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001019
1020 Ex = Ex->IgnoreParens();
1021
Ted Kremenek07932632008-02-27 06:47:26 +00001022 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001023 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001024 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001025 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001026
Ted Kremenekffe0f432008-03-07 22:58:01 +00001027 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex))
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001028 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00001029 VisitDeref(U, Pred, Dst, true);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001030 return;
1031 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001032
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001033 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001034}
1035
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001036void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1037 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1038}
1039
1040void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1041 AsmStmt::outputs_iterator I,
1042 AsmStmt::outputs_iterator E,
1043 NodeTy* Pred, NodeSet& Dst) {
1044 if (I == E) {
1045 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1046 return;
1047 }
1048
1049 NodeSet Tmp;
1050 VisitLVal(*I, Pred, Tmp);
1051
1052 ++I;
1053
1054 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1055 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1056}
1057
1058void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1059 AsmStmt::inputs_iterator I,
1060 AsmStmt::inputs_iterator E,
1061 NodeTy* Pred, NodeSet& Dst) {
1062 if (I == E) {
1063
1064 // We have processed both the inputs and the outputs. All of the outputs
1065 // should evaluate to LVals. Nuke all of their values.
1066
1067 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1068 // which interprets the inline asm and stores proper results in the
1069 // outputs.
1070
1071 ValueState* St = GetState(Pred);
1072
1073 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1074 OE = A->end_outputs(); OI != OE; ++OI) {
1075
1076 RVal X = GetLVal(St, *OI);
1077
1078 assert (!isa<NonLVal>(X));
1079
1080 if (isa<LVal>(X))
1081 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1082 }
1083
Ted Kremenek0e561a32008-03-21 21:30:14 +00001084 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001085 return;
1086 }
1087
1088 NodeSet Tmp;
1089 Visit(*I, Pred, Tmp);
1090
1091 ++I;
1092
1093 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1094 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1095}
1096
1097
Ted Kremenek55deb972008-03-25 00:34:37 +00001098void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1099 NodeSet& Dst){
Ted Kremenekb9308372008-03-25 16:07:41 +00001100 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), Pred, Dst);
Ted Kremenek55deb972008-03-25 00:34:37 +00001101}
1102
Ted Kremenekb9308372008-03-25 16:07:41 +00001103void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Ted Kremenek23a4f912008-03-25 02:10:28 +00001104 ObjCMessageExpr::arg_iterator AI,
1105 ObjCMessageExpr::arg_iterator AE,
Ted Kremenek55deb972008-03-25 00:34:37 +00001106 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek23a4f912008-03-25 02:10:28 +00001107 if (AI == AE) {
Ted Kremenek55deb972008-03-25 00:34:37 +00001108
1109 // Process the receiver.
1110
Ted Kremenekb9308372008-03-25 16:07:41 +00001111 if (Expr* Receiver = ME->getReceiver()) {
1112 NodeSet Tmp;
Ted Kremenek5af373a2008-03-26 21:36:08 +00001113 Visit(Receiver, Pred, Tmp);
Ted Kremenekb9308372008-03-25 16:07:41 +00001114
1115 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1116 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Ted Kremenek23a4f912008-03-25 02:10:28 +00001117
Ted Kremenekb9308372008-03-25 16:07:41 +00001118 return;
Ted Kremenek23a4f912008-03-25 02:10:28 +00001119 }
Ted Kremenekb9308372008-03-25 16:07:41 +00001120
1121 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenek55deb972008-03-25 00:34:37 +00001122 return;
1123 }
1124
1125 NodeSet Tmp;
Ted Kremenek23a4f912008-03-25 02:10:28 +00001126 Visit(*AI, Pred, Tmp);
Ted Kremenek55deb972008-03-25 00:34:37 +00001127
Ted Kremenek23a4f912008-03-25 02:10:28 +00001128 ++AI;
Ted Kremenek55deb972008-03-25 00:34:37 +00001129
1130 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenekb9308372008-03-25 16:07:41 +00001131 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
Ted Kremenek55deb972008-03-25 00:34:37 +00001132}
1133
Ted Kremenekb9308372008-03-25 16:07:41 +00001134void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1135 NodeTy* Pred, NodeSet& Dst) {
1136
1137
1138 // FIXME: More logic for the processing the method call.
1139
1140 ValueState* St = GetState(Pred);
1141
1142 if (Expr* Receiver = ME->getReceiver()) {
1143
Ted Kremeneka056b4f2008-03-26 22:21:58 +00001144 RVal L = GetRVal(St, Receiver);
Ted Kremenekb9308372008-03-25 16:07:41 +00001145
1146 // Check for undefined control-flow or calls to NULL.
1147
1148 if (L.isUndef()) {
1149 NodeTy* N = Builder->generateNode(ME, St, Pred);
1150
1151 if (N) {
1152 N->markAsSink();
1153 UndefReceivers.insert(N);
1154 }
1155
1156 return;
1157 }
1158 }
1159
1160 // Check for any arguments that are uninitialized/undefined.
1161
1162 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1163 I != E; ++I) {
1164
1165 if (GetRVal(St, *I).isUndef()) {
1166
1167 NodeTy* N = Builder->generateNode(ME, St, Pred);
1168
1169 if (N) {
1170 N->markAsSink();
1171 MsgExprUndefArgs[N] = *I;
1172 }
1173
1174 return;
1175 }
1176 }
1177
1178 // FIXME: Eventually we will properly handle the effects of a message
1179 // expr. For now invalidate all arguments passed in by references.
1180
1181 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1182 I != E; ++I) {
1183
1184 RVal V = GetRVal(St, *I);
1185
1186 if (isa<LVal>(V))
1187 St = SetRVal(St, cast<LVal>(V), UnknownVal());
1188 }
1189
1190 MakeNode(Dst, ME, Pred, St);
1191}
Ted Kremenek55deb972008-03-25 00:34:37 +00001192
Ted Kremenek02737ed2008-03-31 15:02:58 +00001193void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1194
1195 Expr* R = S->getRetValue();
1196
1197 if (!R) {
1198 Dst.Add(Pred);
1199 return;
1200 }
1201
1202 QualType T = R->getType();
1203
1204 if (T->isPointerType() || T->isReferenceType()) {
1205
1206 // Check if any of the return values return the address of a stack variable.
1207
1208 NodeSet Tmp;
1209 Visit(R, Pred, Tmp);
1210
1211 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1212 RVal X = GetRVal((*I)->getState(), R);
1213
1214 if (isa<lval::DeclVal>(X)) {
1215
1216 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1217
1218 // Create a special node representing the v
1219
1220 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1221
1222 if (RetStackNode) {
1223 RetStackNode->markAsSink();
1224 RetsStackAddr.insert(RetStackNode);
1225 }
1226
1227 continue;
1228 }
1229 }
1230
1231 Dst.Add(*I);
1232 }
1233 }
1234 else
1235 Visit(R, Pred, Dst);
1236}
Ted Kremenek55deb972008-03-25 00:34:37 +00001237
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001238void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001239 GRExprEngine::NodeTy* Pred,
1240 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001241 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001242
1243 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001244 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001245 else
1246 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001247
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001248 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001249
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001250 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001251
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001252 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001253 // In such cases, we want to (initially) treat the LHS as an LVal,
1254 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1255 // evaluated to LValDecl's instead of to an NonLVal.
1256
Ted Kremenek0d093d32008-03-10 04:11:42 +00001257 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1258 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001259
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001260 // Visit the RHS...
1261
1262 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001263 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001264
1265 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001266
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001267 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001268
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001269 NodeTy* N2 = *I2;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001270 ValueState* St = GetState(N2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001271 Expr* RHS = B->getRHS();
1272 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001273
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001274 BinaryOperator::Opcode Op = B->getOpcode();
1275
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001276 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1277 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001278
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001279 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001280
Ted Kremenek00155412008-02-26 22:27:51 +00001281 if (!RightV.isUnknown()) {
1282
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001283 if (RightV.isUndef()) {
1284 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001285
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001286 if (DivUndef) {
1287 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001288 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001289 }
1290
1291 continue;
1292 }
1293
1294 // Check for divide/remainder-by-zero.
1295 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001296 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001297
Ted Kremenek4d839b42008-03-07 19:04:53 +00001298 bool isFeasibleZero = false;
1299 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001300
Ted Kremenek00155412008-02-26 22:27:51 +00001301 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001302
Ted Kremenek4d839b42008-03-07 19:04:53 +00001303 bool isFeasibleNotZero = false;
1304 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001305
Ted Kremenek4d839b42008-03-07 19:04:53 +00001306 // Create the node for the divide-by-zero (if it occurred).
1307
1308 if (isFeasibleZero)
1309 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1310 DivZeroNode->markAsSink();
1311
1312 if (isFeasibleNotZero)
1313 ImplicitBadDivides.insert(DivZeroNode);
1314 else
1315 ExplicitBadDivides.insert(DivZeroNode);
1316
1317 }
1318
1319 if (!isFeasibleNotZero)
Ted Kremenek00155412008-02-26 22:27:51 +00001320 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001321 }
1322
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001323 // Fall-through. The logic below processes the divide.
1324 }
1325
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001326
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001327 if (Op <= BinaryOperator::Or) {
1328
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001329 // Process non-assignements except commas or short-circuited
1330 // logical expressions (LAnd and LOr).
1331
1332 RVal Result = EvalBinOp(Op, LeftV, RightV);
1333
1334 if (Result.isUnknown()) {
1335 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001336 continue;
1337 }
1338
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001339 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1340
1341 // The operands were not undefined, but the result is undefined.
1342
1343 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1344 UndefNode->markAsSink();
1345 UndefResults.insert(UndefNode);
1346 }
1347
1348 continue;
1349 }
1350
Ted Kremenek0e561a32008-03-21 21:30:14 +00001351 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001352 continue;
1353 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001354
1355 // Process assignments.
1356
1357 switch (Op) {
1358
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001359 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001360
1361 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001362
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001363 if (LeftV.isUndef()) {
1364 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001365 continue;
1366 }
1367
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001368 // EXPERIMENTAL: "Conjured" symbols.
1369
1370 if (RightV.isUnknown()) {
1371 unsigned Count = Builder->getCurrentBlockCount();
1372 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1373
1374 RightV = B->getRHS()->getType()->isPointerType()
1375 ? cast<RVal>(lval::SymbolVal(Sym))
1376 : cast<RVal>(nonlval::SymbolVal(Sym));
1377 }
1378
1379 // Even if the LHS evaluates to an unknown L-Value, the entire
1380 // expression still evaluates to the RHS.
1381
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001382 if (LeftV.isUnknown()) {
1383 St = SetRVal(St, B, RightV);
1384 break;
1385 }
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001386
1387 // Simulate the effects of a "store": bind the value of the RHS
1388 // to the L-Value represented by the LHS.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001389
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001390 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001391 break;
1392 }
1393
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001394 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001395
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001396 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001397
Ted Kremenek00155412008-02-26 22:27:51 +00001398 assert (B->isCompoundAssignmentOp());
1399
1400 if (Op >= BinaryOperator::AndAssign)
1401 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1402 else
1403 ((int&) Op) -= BinaryOperator::MulAssign;
1404
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001405 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001406
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001407 if (LeftV.isUndef()) {
1408 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001409 continue;
1410 }
1411
1412 if (LeftV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001413 assert (isa<UnknownVal>(GetRVal(St, B)));
1414 Dst.Add(N2);
1415 continue;
Ted Kremenek9dca0622008-02-19 00:22:37 +00001416 }
1417
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001418 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek3ce92142008-03-09 08:12:37 +00001419 // that is neither "Unknown" or "Undefined."
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001420
1421 LVal LeftLV = cast<LVal>(LeftV);
1422
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001423 // Fetch the value of the LHS (the value of the variable, etc.).
1424
Ted Kremenek0d093d32008-03-10 04:11:42 +00001425 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001426
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001427 // Propagate undefined value (left-side). We
1428 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001429 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001430
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001431 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001432 St = SetRVal(St, B, V);
1433 break;
1434 }
1435
1436 // Propagate unknown values.
1437
Ted Kremenek89063af2008-02-21 19:15:37 +00001438 if (V.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001439 // The value bound to LeftV is unknown. Thus we just
1440 // propagate the current node (as "B" is already bound to nothing).
1441 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenek89063af2008-02-21 19:15:37 +00001442 Dst.Add(N2);
1443 continue;
1444 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001445
1446 if (RightV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001447 assert (isa<UnknownVal>(GetRVal(St, B)));
1448 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001449 break;
1450 }
1451
Ted Kremenek00155412008-02-26 22:27:51 +00001452 // At this point:
1453 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001454 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001455 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001456
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001457 // Get the computation type.
1458 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1459
1460 // Perform promotions.
1461 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001462 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001463
1464 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001465
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001466 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1467 && RHS->getType()->isIntegerType()) {
1468
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001469 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001470
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001471 if (RightV.isUndef()) {
1472 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001473
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001474 if (DivUndef) {
1475 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001476 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001477 }
1478
1479 continue;
1480 }
Ted Kremenek00155412008-02-26 22:27:51 +00001481
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001482 // First, "assume" that the denominator is 0.
1483
Ted Kremenek4d839b42008-03-07 19:04:53 +00001484 bool isFeasibleZero = false;
1485 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001486
Ted Kremenek4d839b42008-03-07 19:04:53 +00001487 // Second, "assume" that the denominator cannot be 0.
1488
1489 bool isFeasibleNotZero = false;
1490 St = Assume(St, RightV, true, isFeasibleNotZero);
1491
1492 // Create the node for the divide-by-zero error (if it occurred).
1493
1494 if (isFeasibleZero) {
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001495 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1496
1497 if (DivZeroNode) {
1498 DivZeroNode->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001499
1500 if (isFeasibleNotZero)
1501 ImplicitBadDivides.insert(DivZeroNode);
1502 else
1503 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001504 }
1505 }
1506
Ted Kremenek4d839b42008-03-07 19:04:53 +00001507 if (!isFeasibleNotZero)
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001508 continue;
1509
1510 // Fall-through. The logic below processes the divide.
1511 }
Ted Kremenek00155412008-02-26 22:27:51 +00001512 else {
1513
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001514 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001515
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001516 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001517 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1518 break;
1519 }
1520
1521 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001522
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001523 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001524
1525 if (Result.isUndef()) {
1526
1527 // The operands were not undefined, but the result is undefined.
1528
1529 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1530 UndefNode->markAsSink();
1531 UndefResults.insert(UndefNode);
1532 }
1533
1534 continue;
1535 }
1536
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001537 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001538 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001539 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001540
Ted Kremenek0e561a32008-03-21 21:30:14 +00001541 MakeNode(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001542 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001543 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001544}
Ted Kremenekee985462008-01-16 18:18:48 +00001545
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001546void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00001547 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001548 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001549 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001550}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001551
Ted Kremenek9dca0622008-02-19 00:22:37 +00001552void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001553
1554 // FIXME: add metadata to the CFG so that we can disable
1555 // this check when we KNOW that there is no block-level subexpression.
1556 // The motivation is that this check requires a hashtable lookup.
1557
1558 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1559 Dst.Add(Pred);
1560 return;
1561 }
1562
1563 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001564
1565 default:
1566 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001567 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001568
1569 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1570 break;
Ted Kremenek7ffa0aa2008-03-17 21:31:48 +00001571
1572 case Stmt::AsmStmtClass:
1573 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
1574 break;
Ted Kremenek230aaab2008-02-12 21:37:25 +00001575
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001576 case Stmt::BinaryOperatorClass: {
1577 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001578
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001579 if (B->isLogicalOp()) {
1580 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001581 break;
1582 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001583 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00001584 ValueState* St = GetState(Pred);
Ted Kremenek0e561a32008-03-21 21:30:14 +00001585 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001586 break;
1587 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001588
1589 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1590 break;
1591 }
Ted Kremenekde434242008-02-19 01:44:53 +00001592
1593 case Stmt::CallExprClass: {
1594 CallExpr* C = cast<CallExpr>(S);
1595 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1596 break;
1597 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001598
1599 case Stmt::CastExprClass: {
1600 CastExpr* C = cast<CastExpr>(S);
1601 VisitCast(C, C->getSubExpr(), Pred, Dst);
1602 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001603 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001604
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001605 // FIXME: ChooseExpr is really a constant. We need to fix
1606 // the CFG do not model them as explicit control-flow.
1607
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001608 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1609 ChooseExpr* C = cast<ChooseExpr>(S);
1610 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1611 break;
1612 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001613
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001614 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001615 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1616 break;
1617
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001618 case Stmt::ConditionalOperatorClass: { // '?' operator
1619 ConditionalOperator* C = cast<ConditionalOperator>(S);
1620 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1621 break;
1622 }
1623
1624 case Stmt::DeclRefExprClass:
1625 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1626 break;
1627
1628 case Stmt::DeclStmtClass:
1629 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1630 break;
1631
1632 case Stmt::ImplicitCastExprClass: {
1633 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1634 VisitCast(C, C->getSubExpr(), Pred, Dst);
1635 break;
1636 }
Ted Kremenek55deb972008-03-25 00:34:37 +00001637
1638 case Stmt::ObjCMessageExprClass: {
1639 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
1640 break;
1641 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001642
1643 case Stmt::ParenExprClass:
1644 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1645 break;
1646
1647 case Stmt::SizeOfAlignOfTypeExprClass:
1648 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1649 break;
1650
Ted Kremenekda9bd092008-02-08 07:05:39 +00001651 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001652 StmtExpr* SE = cast<StmtExpr>(S);
1653
Ted Kremenek0d093d32008-03-10 04:11:42 +00001654 ValueState* St = GetState(Pred);
Ted Kremenek3b4a8ef2008-03-15 03:27:30 +00001655
1656 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
1657 // probably just remove these from the CFG.
1658 assert (!SE->getSubStmt()->body_empty());
1659
1660 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Ted Kremenek0e561a32008-03-21 21:30:14 +00001661 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek3b4a8ef2008-03-15 03:27:30 +00001662 else
1663 Dst.Add(Pred);
1664
1665 break;
Ted Kremenekda9bd092008-02-08 07:05:39 +00001666 }
1667
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001668 // FIXME: We may wish to always bind state to ReturnStmts so
1669 // that users can quickly query what was the state at the
1670 // exit points of a function.
1671
Ted Kremenek02737ed2008-03-31 15:02:58 +00001672 case Stmt::ReturnStmtClass:
1673 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst); break;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001674
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001675 case Stmt::UnaryOperatorClass: {
1676 UnaryOperator* U = cast<UnaryOperator>(S);
1677
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001678 switch (U->getOpcode()) {
1679 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1680 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1681 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1682 default: VisitUnaryOperator(U, Pred, Dst); break;
1683 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001684
Ted Kremenek9de04c42008-01-24 20:55:43 +00001685 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001686 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001687 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001688}
1689
Ted Kremenekee985462008-01-16 18:18:48 +00001690//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001691// "Assume" logic.
1692//===----------------------------------------------------------------------===//
1693
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001694ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001695 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001696 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001697 switch (Cond.getSubKind()) {
1698 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001699 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001700 return St;
1701
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001702 case lval::SymbolValKind:
1703 if (Assumption)
1704 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001705 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001706 else
1707 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001708 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001709
Ted Kremenek08b66252008-02-06 04:31:33 +00001710
Ted Kremenek329f8542008-02-05 21:52:21 +00001711 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001712 case lval::FuncValKind:
1713 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001714 isFeasible = Assumption;
1715 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001716
Ted Kremenek329f8542008-02-05 21:52:21 +00001717 case lval::ConcreteIntKind: {
1718 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001719 isFeasible = b ? Assumption : !Assumption;
1720 return St;
1721 }
1722 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001723}
1724
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001725ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001726 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001727 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001728 switch (Cond.getSubKind()) {
1729 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001730 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001731 return St;
1732
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001733
1734 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001735 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001736 SymbolID sym = SV.getSymbol();
1737
1738 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00001739 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001740 isFeasible);
1741 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00001742 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001743 isFeasible);
1744 }
1745
Ted Kremenek08b66252008-02-06 04:31:33 +00001746 case nonlval::SymIntConstraintValKind:
1747 return
1748 AssumeSymInt(St, Assumption,
1749 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1750 isFeasible);
1751
Ted Kremenek329f8542008-02-05 21:52:21 +00001752 case nonlval::ConcreteIntKind: {
1753 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001754 isFeasible = b ? Assumption : !Assumption;
1755 return St;
1756 }
1757 }
1758}
1759
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001760ValueState*
1761GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001762 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001763
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001764 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001765 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001766 isFeasible = *X != V;
1767 return St;
1768 }
1769
1770 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001771 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001772 isFeasible = true;
1773 return St;
1774 }
1775
1776 // If we reach here, sym is not a constant and we don't know if it is != V.
1777 // Make that assumption.
1778
1779 isFeasible = true;
1780 return StateMgr.AddNE(St, sym, V);
1781}
1782
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001783ValueState*
1784GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001785 const llvm::APSInt& V, bool& isFeasible) {
1786
1787 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001788 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001789 isFeasible = *X == V;
1790 return St;
1791 }
1792
1793 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001794 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001795 isFeasible = false;
1796 return St;
1797 }
1798
1799 // If we reach here, sym is not a constant and we don't know if it is == V.
1800 // Make that assumption.
1801
1802 isFeasible = true;
1803 return StateMgr.AddEQ(St, sym, V);
1804}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001805
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001806ValueState*
1807GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001808 const SymIntConstraint& C, bool& isFeasible) {
1809
1810 switch (C.getOpcode()) {
1811 default:
1812 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001813 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00001814 return St;
1815
1816 case BinaryOperator::EQ:
1817 if (Assumption)
1818 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1819 else
1820 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1821
1822 case BinaryOperator::NE:
1823 if (Assumption)
1824 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1825 else
1826 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1827 }
1828}
1829
Ted Kremenekb38911f2008-01-30 23:03:39 +00001830//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001831// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001832//===----------------------------------------------------------------------===//
1833
Ted Kremenekaa66a322008-01-16 21:46:15 +00001834#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001835static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00001836static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00001837static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001838
Ted Kremenekaa66a322008-01-16 21:46:15 +00001839namespace llvm {
1840template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001841struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001842 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001843
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001844 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001845
1846 Out << "Variables:\\l";
1847
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001848 bool isFirst = true;
1849
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001850 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001851
1852 if (isFirst)
1853 isFirst = false;
1854 else
1855 Out << "\\l";
1856
1857 Out << ' ' << I.getKey()->getName() << " : ";
1858 I.getData().print(Out);
1859 }
1860
1861 }
1862
Ted Kremeneke7d22112008-02-11 19:21:59 +00001863
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001864 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001865
1866 bool isFirst = true;
1867
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001868 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001869
1870 if (isFirst) {
1871 Out << "\\l\\lSub-Expressions:\\l";
1872 isFirst = false;
1873 }
1874 else
1875 Out << "\\l";
1876
1877 Out << " (" << (void*) I.getKey() << ") ";
1878 I.getKey()->printPretty(Out);
1879 Out << " : ";
1880 I.getData().print(Out);
1881 }
1882 }
1883
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001884 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001885
Ted Kremenek016f52f2008-02-08 21:10:02 +00001886 bool isFirst = true;
1887
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001888 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001889 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001890 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001891 isFirst = false;
1892 }
1893 else
1894 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001895
Ted Kremeneke7d22112008-02-11 19:21:59 +00001896 Out << " (" << (void*) I.getKey() << ") ";
1897 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001898 Out << " : ";
1899 I.getData().print(Out);
1900 }
1901 }
1902
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001903 static void PrintEQ(std::ostream& Out, ValueState* St) {
1904 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001905
1906 if (CE.isEmpty())
1907 return;
1908
1909 Out << "\\l\\|'==' constraints:";
1910
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001911 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001912 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1913 }
1914
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001915 static void PrintNE(std::ostream& Out, ValueState* St) {
1916 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001917
1918 if (NE.isEmpty())
1919 return;
1920
1921 Out << "\\l\\|'!=' constraints:";
1922
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001923 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001924 I != EI; ++I){
1925
1926 Out << "\\l $" << I.getKey() << " : ";
1927 bool isFirst = true;
1928
1929 ValueState::IntSetTy::iterator J=I.getData().begin(),
1930 EJ=I.getData().end();
1931 for ( ; J != EJ; ++J) {
1932 if (isFirst) isFirst = false;
1933 else Out << ", ";
1934
1935 Out << (*J)->toString();
1936 }
1937 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001938 }
1939
1940 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1941
1942 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001943 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001944 GraphPrintCheckerState->isUndefDeref(N) ||
1945 GraphPrintCheckerState->isUndefStore(N) ||
1946 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00001947 GraphPrintCheckerState->isExplicitBadDivide(N) ||
1948 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001949 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001950 GraphPrintCheckerState->isBadCall(N) ||
1951 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001952 return "color=\"red\",style=\"filled\"";
1953
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001954 if (GraphPrintCheckerState->isNoReturnCall(N))
1955 return "color=\"blue\",style=\"filled\"";
1956
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001957 return "";
1958 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001959
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001960 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001961 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001962
1963 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001964 ProgramPoint Loc = N->getLocation();
1965
1966 switch (Loc.getKind()) {
1967 case ProgramPoint::BlockEntranceKind:
1968 Out << "Block Entrance: B"
1969 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1970 break;
1971
1972 case ProgramPoint::BlockExitKind:
1973 assert (false);
1974 break;
1975
1976 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00001977 const PostStmt& L = cast<PostStmt>(Loc);
1978 Stmt* S = L.getStmt();
1979 SourceLocation SLoc = S->getLocStart();
1980
1981 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
1982 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001983
Ted Kremenek9b5551d2008-03-09 03:30:59 +00001984 if (SLoc.isFileID()) {
1985 Out << "\\lline="
1986 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
1987 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
1988 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001989
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001990 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001991 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001992 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00001993 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001994 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001995 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001996 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001997 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00001998 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
1999 Out << "\\|Explicit divide-by zero or undefined value.";
2000 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2001 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002002 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002003 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002004 else if (GraphPrintCheckerState->isNoReturnCall(N))
2005 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002006 else if (GraphPrintCheckerState->isBadCall(N))
2007 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002008 else if (GraphPrintCheckerState->isUndefArg(N))
2009 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002010
Ted Kremenekaa66a322008-01-16 21:46:15 +00002011 break;
2012 }
2013
2014 default: {
2015 const BlockEdge& E = cast<BlockEdge>(Loc);
2016 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2017 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002018
2019 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002020
2021 SourceLocation SLoc = T->getLocStart();
2022
Ted Kremenekb38911f2008-01-30 23:03:39 +00002023 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002024
Ted Kremenekb38911f2008-01-30 23:03:39 +00002025 E.getSrc()->printTerminator(Out);
2026
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002027 if (SLoc.isFileID()) {
2028 Out << "\\lline="
2029 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2030 << GraphPrintSourceManager->getColumnNumber(SLoc);
2031 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002032
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002033 if (isa<SwitchStmt>(T)) {
2034 Stmt* Label = E.getDst()->getLabel();
2035
2036 if (Label) {
2037 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2038 Out << "\\lcase ";
2039 C->getLHS()->printPretty(Out);
2040
2041 if (Stmt* RHS = C->getRHS()) {
2042 Out << " .. ";
2043 RHS->printPretty(Out);
2044 }
2045
2046 Out << ":";
2047 }
2048 else {
2049 assert (isa<DefaultStmt>(Label));
2050 Out << "\\ldefault:";
2051 }
2052 }
2053 else
2054 Out << "\\l(implicit) default:";
2055 }
2056 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002057 // FIXME
2058 }
2059 else {
2060 Out << "\\lCondition: ";
2061 if (*E.getSrc()->succ_begin() == E.getDst())
2062 Out << "true";
2063 else
2064 Out << "false";
2065 }
2066
2067 Out << "\\l";
2068 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002069
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002070 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2071 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002072 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002073 }
2074 }
2075
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002076 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002077
Ted Kremenek75da3e82008-03-11 19:02:40 +00002078 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002079
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002080 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002081 return Out.str();
2082 }
2083};
2084} // end llvm namespace
2085#endif
2086
Ted Kremenekffe0f432008-03-07 22:58:01 +00002087#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002088
2089template <typename ITERATOR>
2090GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2091
2092template <>
2093GRExprEngine::NodeTy*
2094GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2095 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2096 return I->first;
2097}
2098
Ted Kremenekffe0f432008-03-07 22:58:01 +00002099template <typename ITERATOR>
2100static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002101 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002102
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002103 llvm::SmallPtrSet<void*,10> CachedSources;
2104
2105 for ( ; I != E; ++I ) {
2106 GRExprEngine::NodeTy* N = GetGraphNode(I);
2107 void* p = N->getLocation().getRawData();
2108
2109 if (CachedSources.count(p))
2110 continue;
2111
2112 CachedSources.insert(p);
2113
2114 Sources.push_back(N);
2115 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002116}
2117#endif
2118
2119void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002120#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002121 if (trim) {
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002122 llvm::SmallVector<NodeTy*, 10> Src;
2123 AddSources(Src, null_derefs_begin(), null_derefs_end());
2124 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2125 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2126 AddSources(Src, undef_results_begin(), undef_results_end());
2127 AddSources(Src, bad_calls_begin(), bad_calls_end());
2128 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002129 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002130
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002131 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002132 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002133 else {
2134 GraphPrintCheckerState = this;
2135 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002136 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002137
Ted Kremenekffe0f432008-03-07 22:58:01 +00002138 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002139
2140 GraphPrintCheckerState = NULL;
2141 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002142 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002143 }
2144#endif
2145}
2146
2147void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2148#ifndef NDEBUG
2149 GraphPrintCheckerState = this;
2150 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002151 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002152
Ted Kremenek493d7a22008-03-11 18:25:33 +00002153 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2154
2155 if (!TrimmedG)
2156 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2157 else {
2158 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2159 delete TrimmedG;
2160 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002161
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002162 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002163 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002164 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002165#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002166}