blob: 2481f568386a3865e07a0f9d89841dbef64b5d6a [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 Kremeneke01c9872008-02-14 22:36:46 +000017#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000018
Ted Kremenek0f5f0592008-02-27 06:07:00 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#include <sstream>
22#endif
23
Ted Kremenekb387a3f2008-02-14 22:16:04 +000024using namespace clang;
25using llvm::dyn_cast;
26using llvm::cast;
27using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000028
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000029ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +000030
31 // The LiveVariables information already has a compilation of all VarDecls
32 // used in the function. Iterate through this set, and "symbolicate"
33 // any VarDecl whose value originally comes from outside the function.
34
35 typedef LiveVariables::AnalysisDataTy LVDataTy;
36 LVDataTy& D = Liveness.getAnalysisData();
37
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000038 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +000039
40 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
41
42 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
43
44 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
45 RVal X = RVal::GetSymbolValue(SymMgr, VD);
46 StateMgr.BindVar(StateImpl, VD, X);
47 }
48 }
49
50 return StateMgr.getPersistentState(StateImpl);
51}
52
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000053ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000054
Ted Kremeneke070a1d2008-02-04 21:59:01 +000055 if (!StateCleaned) {
56 St = RemoveDeadBindings(CurrentStmt, St);
57 StateCleaned = true;
58 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000059
Ted Kremeneke070a1d2008-02-04 21:59:01 +000060 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000061
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000062 if (Ex == CurrentStmt) {
63 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000064
65 if (!isBlkExpr)
66 return St;
67 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000068
Ted Kremenek5a7b3822008-02-26 23:37:01 +000069 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000070}
71
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000072#if 0
73const ValueState::BufferTy&
74GRExprEngine::SetRVal(ValueState* St, Expr* Ex, const RVal::BufferTy& RB,
75 ValueState::BufferTy& RetBuf) {
Ted Kremenekcba2e432008-02-05 19:35:18 +000076
77 assert (RetBuf.empty());
78
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000079 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
80 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekcba2e432008-02-05 19:35:18 +000081
82 return RetBuf;
83}
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000084#endif
Ted Kremenekcba2e432008-02-05 19:35:18 +000085
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000086ValueState* GRExprEngine::SetRVal(ValueState* St, LVal LV, RVal RV) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000087
88 if (!StateCleaned) {
89 St = RemoveDeadBindings(CurrentStmt, St);
90 StateCleaned = true;
91 }
92
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000093 return StateMgr.SetRVal(St, LV, RV);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000094}
95
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000096ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
97 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +000098
99 switch (Terminator->getStmtClass()) {
100 default:
101 return St;
102
103 case Stmt::BinaryOperatorClass: { // '&&' and '||'
104
105 BinaryOperator* B = cast<BinaryOperator>(Terminator);
106 BinaryOperator::Opcode Op = B->getOpcode();
107
108 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
109
110 // For &&, if we take the true branch, then the value of the whole
111 // expression is that of the RHS expression.
112 //
113 // For ||, if we take the false branch, then the value of the whole
114 // expression is that of the RHS expression.
115
116 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
117 (Op == BinaryOperator::LOr && !branchTaken)
118 ? B->getRHS() : B->getLHS();
119
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000120 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000121 }
122
123 case Stmt::ConditionalOperatorClass: { // ?:
124
125 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
126
127 // For ?, if branchTaken == true then the value is either the LHS or
128 // the condition itself. (GNU extension).
129
130 Expr* Ex;
131
132 if (branchTaken)
133 Ex = C->getLHS() ? C->getLHS() : C->getCond();
134 else
135 Ex = C->getRHS();
136
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000137 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000138 }
139
140 case Stmt::ChooseExprClass: { // ?:
141
142 ChooseExpr* C = cast<ChooseExpr>(Terminator);
143
144 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000145 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000146 }
147 }
148}
149
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000150bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
151 GRBlockCounter BC) {
152
153 return BC.getNumVisited(B->getBlockID()) < 3;
154}
155
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000156void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000157 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000158
Ted Kremeneke7d22112008-02-11 19:21:59 +0000159 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000160 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000161
Ted Kremenekb2331832008-02-15 22:29:00 +0000162 // Check for NULL conditions; e.g. "for(;;)"
163 if (!Condition) {
164 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000165 return;
166 }
167
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000168 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000169
170 switch (V.getBaseKind()) {
171 default:
172 break;
173
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000174 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000175 builder.generateNode(MarkBranch(PrevState, Term, true), true);
176 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000177 return;
178
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000179 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000180 NodeTy* N = builder.generateNode(PrevState, true);
181
182 if (N) {
183 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000184 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000185 }
186
187 builder.markInfeasible(false);
188 return;
189 }
190 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000191
Ted Kremenekb38911f2008-01-30 23:03:39 +0000192
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000193 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000194
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000195 bool isFeasible = true;
196 ValueState* St = Assume(PrevState, V, true, isFeasible);
197
198 if (isFeasible)
199 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000200 else
201 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000202
203 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000204
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000205 isFeasible = false;
206 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000207
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000208 if (isFeasible)
209 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000210 else
211 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000212}
213
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000214/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000215/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000216void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000217
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000218 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000219 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000220
221 // Three possibilities:
222 //
223 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000224 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000225 // (3) We have no clue about the label. Dispatch to all targets.
226 //
227
228 typedef IndirectGotoNodeBuilder::iterator iterator;
229
230 if (isa<lval::GotoLabel>(V)) {
231 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
232
233 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000234 if (I.getLabel() == L) {
235 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000236 return;
237 }
238 }
239
240 assert (false && "No block with label.");
241 return;
242 }
243
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000244 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000245 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000246 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000247 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000248 return;
249 }
250
251 // This is really a catch-all. We don't support symbolics yet.
252
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000253 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000254
255 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000256 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000257}
Ted Kremenekf233d482008-02-05 00:26:40 +0000258
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000259/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
260/// nodes by processing the 'effects' of a switch statement.
261void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
262
263 typedef SwitchNodeBuilder::iterator iterator;
264
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000265 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000266 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000267 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000268
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000269 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000270 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000271 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000272 return;
273 }
274
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000275 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000276
277 // While most of this can be assumed (such as the signedness), having it
278 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000279
280 unsigned bits = getContext().getTypeSize(CondE->getType(),
281 CondE->getExprLoc());
282
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000283 APSInt V1(bits, false);
284 APSInt V2 = V1;
285
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000286 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000287
288 CaseStmt* Case = cast<CaseStmt>(I.getCase());
289
290 // Evaluate the case.
291 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
292 assert (false && "Case condition must evaluate to an integer constant.");
293 return;
294 }
295
296 // Get the RHS of the case, if it exists.
297
298 if (Expr* E = Case->getRHS()) {
299 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
300 assert (false &&
301 "Case condition (RHS) must evaluate to an integer constant.");
302 return ;
303 }
304
305 assert (V1 <= V2);
306 }
307 else V2 = V1;
308
309 // FIXME: Eventually we should replace the logic below with a range
310 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000311 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000312
313 do {
314 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
315
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000316 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000317
318 // Now "assume" that the case matches.
Ted Kremenek692416c2008-02-18 22:57:02 +0000319 bool isFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000320
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000321 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000322
323 if (isFeasible) {
324 builder.generateCaseStmtNode(I, StNew);
325
326 // If CondV evaluates to a constant, then we know that this
327 // is the *only* case that we can take, so stop evaluating the
328 // others.
329 if (isa<nonlval::ConcreteInt>(CondV))
330 return;
331 }
332
333 // Now "assume" that the case doesn't match. Add this state
334 // to the default state (if it is feasible).
335
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000336 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000337
338 if (isFeasible)
339 DefaultSt = StNew;
340
341 // Concretize the next value in the range.
342 ++V1;
343
344 } while (V1 < V2);
345 }
346
347 // If we reach here, than we know that the default branch is
348 // possible.
349 builder.generateDefaultCaseNode(DefaultSt);
350}
351
352
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000353void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000354 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000355
Ted Kremenek05a23782008-02-26 19:05:15 +0000356 assert (B->getOpcode() == BinaryOperator::LAnd ||
357 B->getOpcode() == BinaryOperator::LOr);
358
359 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
360
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000361 ValueState* St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000362 RVal X = GetBlkExprRVal(St, B);
363
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000364 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000365
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000366 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000367
368 assert (Ex);
369
370 if (Ex == B->getRHS()) {
371
372 X = GetBlkExprRVal(St, Ex);
373
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000374 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000375
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000376 if (X.isUndef()) {
Ted Kremenek58b33212008-02-26 19:40:44 +0000377 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
378 return;
379 }
380
Ted Kremenek05a23782008-02-26 19:05:15 +0000381 // We took the RHS. Because the value of the '&&' or '||' expression must
382 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
383 // or 1. Alternatively, we could take a lazy approach, and calculate this
384 // value later when necessary. We don't have the machinery in place for
385 // this right now, and since most logical expressions are used for branches,
386 // the payoff is not likely to be large. Instead, we do eager evaluation.
387
388 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000389 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000390
391 if (isFeasible)
392 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
393
394 isFeasible = false;
395 NewState = Assume(St, X, false, isFeasible);
396
397 if (isFeasible)
398 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000399 }
400 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000401 // We took the LHS expression. Depending on whether we are '&&' or
402 // '||' we know what the value of the expression is via properties of
403 // the short-circuiting.
404
405 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
406 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000407 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000408}
Ted Kremenek05a23782008-02-26 19:05:15 +0000409
Ted Kremenekf233d482008-02-05 00:26:40 +0000410
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000411void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000412
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000413 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000414 StmtEntryNode = builder.getLastNode();
415 CurrentStmt = S;
416 NodeSet Dst;
417 StateCleaned = false;
418
419 Visit(S, StmtEntryNode, Dst);
420
421 // If no nodes were generated, generate a new node that has all the
422 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000423
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000424 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000425 ValueState* St = RemoveDeadBindings(S, StmtEntryNode->getState());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000426 builder.generateNode(S, St, StmtEntryNode);
427 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000428
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000429 // For safety, NULL out these variables.
430
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000431 CurrentStmt = NULL;
432 StmtEntryNode = NULL;
433 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000434}
435
Ted Kremenek44842c22008-02-13 18:06:44 +0000436void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000437
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000438 if (D != CurrentStmt) {
439 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
440 return;
441 }
442
443 // If we are here, we are loading the value of the decl and binding
444 // it to the block-level expression.
445
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000446 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000447 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000448}
449
Ted Kremenekde434242008-02-19 01:44:53 +0000450void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000451 CallExpr::arg_iterator AI,
452 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000453 NodeSet& Dst) {
454
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000455 // Process the arguments.
456
457 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000458
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000459 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000460 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000461 ++AI;
462
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000463 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000464 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000465
466 return;
467 }
468
469 // If we reach here we have processed all of the arguments. Evaluate
470 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000471
Ted Kremenek994a09b2008-02-25 21:16:03 +0000472 NodeSet DstTmp;
473 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000474
Ted Kremenek994a09b2008-02-25 21:16:03 +0000475 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000476
477 if (DstTmp.empty())
478 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000479
480 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000481 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
482
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000483 ValueState* St = (*DI)->getState();
Ted Kremenek994a09b2008-02-25 21:16:03 +0000484 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000485
Ted Kremeneka1354a52008-03-03 16:47:31 +0000486 // FIXME: Add support for symbolic function calls (calls involving
487 // function pointer values that are symbolic).
488
489 // Check for undefined control-flow or calls to NULL.
490
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000491 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000492 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000493
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000494 if (N) {
495 N->markAsSink();
496 BadCalls.insert(N);
497 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000498
Ted Kremenekde434242008-02-19 01:44:53 +0000499 continue;
Ted Kremeneka1354a52008-03-03 16:47:31 +0000500 }
501
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000502 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000503
Ted Kremenek03da0d72008-02-21 19:46:04 +0000504 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000505 // Check for an "unknown" callee.
506 invalidateArgs = true;
507 }
508 else if (isa<lval::FuncVal>(L)) {
509
510 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
511
512 if (Info->getBuiltinID())
513 invalidateArgs = true;
514 }
515
516 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000517 // Invalidate all arguments passed in by reference (LVals).
518 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
519 I != E; ++I) {
520 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000521
Ted Kremenek03da0d72008-02-21 19:46:04 +0000522 if (isa<LVal>(V))
523 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremeneka1354a52008-03-03 16:47:31 +0000524 }
Ted Kremenek03da0d72008-02-21 19:46:04 +0000525 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000526 else {
527
528 // Check any arguments passed-by-value against being undefined.
529
530 bool badArg = false;
531
532 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
533 I != E; ++I) {
534
535 if (GetRVal((*DI)->getState(), *I).isUndef()) {
536 NodeTy* N = Builder->generateNode(CE, (*DI)->getState(), *DI);
537
538 if (N) {
539 N->markAsSink();
540 UndefArgs[N] = *I;
541 }
542
543 badArg = true;
544 break;
545 }
546 }
547
548 if (badArg)
549 continue;
550
551 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000552
553 unsigned size = Dst.size();
554
555 EvalCall(Dst, CE, cast<LVal>(L), *DI);
556
557 if (Dst.size() == size)
558 Nodify(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000559 }
Ted Kremenek03da0d72008-02-21 19:46:04 +0000560
Ted Kremenekaffb2152008-02-27 20:43:44 +0000561 // Check for the "noreturn" attribute.
562
563 if (isa<lval::FuncVal>(L))
564 if (cast<lval::FuncVal>(L).getDecl()->getAttr<NoReturnAttr>()) {
565
566 NodeTy* N = Builder->generateNode(CE, St, *DI);
567
568 if (N) {
569 N->markAsSink();
570 NoReturnCalls.insert(N);
571 }
572
573 continue;
574 }
575
Ted Kremenek03da0d72008-02-21 19:46:04 +0000576 Nodify(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +0000577 }
578}
579
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000580void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000581
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000582 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000583 QualType T = CastE->getType();
584
Ted Kremenek65cfb732008-03-04 22:16:08 +0000585 if (T->isReferenceType())
586 VisitLVal(Ex, Pred, S1);
587 else
588 Visit(Ex, Pred, S1);
589
Ted Kremenek402563b2008-02-19 18:47:04 +0000590 // Check for redundant casts or casting to "void"
591 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000592 Ex->getType() == T ||
593 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000594
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000595 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000596 Dst.Add(*I1);
597
Ted Kremenek874d63f2008-01-24 02:02:54 +0000598 return;
599 }
600
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000601 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000602 NodeTy* N = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000603 ValueState* St = N->getState();
Ted Kremenek65cfb732008-03-04 22:16:08 +0000604
605 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
606
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000607 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000608 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000609}
610
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000611void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000612 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000613
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000614 ValueState* St = Pred->getState();
Ted Kremenek9de04c42008-01-24 20:55:43 +0000615
616 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000617 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000618
619 // FIXME: Add support for local arrays.
620 if (VD->getType()->isArrayType())
621 continue;
622
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000623 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000624
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000625 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
626
627 // In this context, Static => Local variable.
628
629 assert (!VD->getStorageClass() == VarDecl::Static ||
630 !isa<FileVarDecl>(VD));
631
632 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000633 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000634 //
635 // FIXME: static variables may have an initializer, but the second
636 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000637
638 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000639
Ted Kremenek16d81562008-03-04 04:18:04 +0000640 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +0000641
642 // C99: 6.7.8 Initialization
643 // If an object that has static storage duration is not initialized
644 // explicitly, then:
645 // —if it has pointer type, it is initialized to a null pointer;
646 // —if it has arithmetic type, it is initialized to (positive or
647 // unsigned) zero;
648
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000649 // FIXME: Handle structs. Now we treat their values as unknown.
650
Ted Kremenek16d81562008-03-04 04:18:04 +0000651 if (T->isPointerType()) {
652
653 St = SetRVal(St, lval::DeclVal(VD),
654 lval::ConcreteInt(ValMgr.getValue(0, T)));
655 }
656 else if (T->isIntegerType()) {
657
658 St = SetRVal(St, lval::DeclVal(VD),
659 nonlval::ConcreteInt(ValMgr.getValue(0, T)));
660 }
661
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000662
Ted Kremenek16d81562008-03-04 04:18:04 +0000663 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000664 else {
Ted Kremenek16d81562008-03-04 04:18:04 +0000665
Ted Kremenekfcb092b2008-03-04 20:40:11 +0000666 // FIXME: Handle structs. Now we treat them as unknown. What
667 // we need to do is treat their members as unknown.
668
669 if (T->isPointerType() || T->isIntegerType())
670 St = SetRVal(St, lval::DeclVal(VD),
671 Ex ? GetRVal(St, Ex) : UndefinedVal());
672 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +0000673 }
Ted Kremenek403c1812008-01-28 22:51:57 +0000674 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000675
676 Nodify(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +0000677}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000678
Ted Kremenekf233d482008-02-05 00:26:40 +0000679
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000680void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000681 NodeTy* Pred, NodeSet& Dst) {
682
Ted Kremenek05a23782008-02-26 19:05:15 +0000683 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
684
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000685 ValueState* St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000686 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000687
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000688 assert (X.isUndef());
Ted Kremenekf233d482008-02-05 00:26:40 +0000689
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000690 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000691
692 assert (SE);
693
694 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000695
696 // Make sure that we invalidate the previous binding.
697 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000698}
699
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000700/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000701void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
702 NodeTy* Pred,
703 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000704
705 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000706
707 // 6.5.3.4 sizeof: "The result type is an integer."
708
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000709 QualType T = Ex->getArgumentType();
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000710
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000711
Ted Kremenek297d0d72008-02-21 18:15:29 +0000712 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000713 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000714 return;
715
Ted Kremenek297d0d72008-02-21 18:15:29 +0000716
717 uint64_t size = 1; // Handle sizeof(void)
718
719 if (T != getContext().VoidTy) {
720 SourceLocation Loc = Ex->getExprLoc();
721 size = getContext().getTypeSize(T, Loc) / 8;
722 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000723
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000724 Nodify(Dst, Ex, Pred,
725 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000726 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000727
728}
729
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000730void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
731
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000733
734 NodeSet DstTmp;
735
Ted Kremenek018c15f2008-02-26 03:44:25 +0000736 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000737 DstTmp.Add(Pred);
738 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000739 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000740
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000741 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000742
743 NodeTy* N = *I;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000744 ValueState* St = N->getState();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000745
746 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
747
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000748 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000749
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000750 // Check for dereferences of undefined values.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000751
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000752 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000753
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000754 NodeTy* Succ = Builder->generateNode(U, St, N);
755
756 if (Succ) {
757 Succ->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000758 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000759 }
760
761 continue;
762 }
763
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000764 // Check for dereferences of unknown values. Treat as No-Ops.
765
766 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000767 Dst.Add(N);
768 continue;
769 }
770
771 // After a dereference, one of two possible situations arise:
772 // (1) A crash, because the pointer was NULL.
773 // (2) The pointer is not NULL, and the dereference works.
774 //
775 // We add these assumptions.
776
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000777 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000778 bool isFeasibleNotNull;
779
780 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000781
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000782 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000783
784 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000785
786 // FIXME: Currently symbolic analysis "generates" new symbols
787 // for the contents of values. We need a better approach.
788
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000789 Nodify(Dst, U, N, SetRVal(StNotNull, U,
790 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000791 }
792
793 bool isFeasibleNull;
794
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000795 // Now "assume" that the pointer is NULL.
796
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000797 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000798
799 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000800
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000801 // We don't use "Nodify" here because the node will be a sink
802 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000803
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000804 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
805
806 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000807
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000808 NullNode->markAsSink();
809
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000810 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
811 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000812 }
813 }
814 }
815}
816
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000817void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
818 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000819
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000820 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000821
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000822 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000823 assert (U->getOpcode() != UnaryOperator::SizeOf);
824 assert (U->getOpcode() != UnaryOperator::AlignOf);
825
826 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000827
828 switch (U->getOpcode()) {
829 case UnaryOperator::PostInc:
830 case UnaryOperator::PostDec:
831 case UnaryOperator::PreInc:
832 case UnaryOperator::PreDec:
833 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000834 // Evalue subexpression as an LVal.
835 use_GetLVal = true;
836 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000837 break;
838
839 default:
840 Visit(U->getSubExpr(), Pred, S1);
841 break;
842 }
843
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000844 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000845
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000846 NodeTy* N1 = *I1;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000847 ValueState* St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000848
849 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
850 GetRVal(St, U->getSubExpr());
851
852 if (SubV.isUnknown()) {
853 Dst.Add(N1);
854 continue;
855 }
856
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000857 if (SubV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000858 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
859 continue;
860 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000861
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000862 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000863
864 // Handle ++ and -- (both pre- and post-increment).
865
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000866 LVal SubLV = cast<LVal>(SubV);
867 RVal V = GetRVal(St, SubLV, U->getType());
868
Ted Kremenek89063af2008-02-21 19:15:37 +0000869 if (V.isUnknown()) {
870 Dst.Add(N1);
871 continue;
872 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000873
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000874 // Propagate undefined values.
875 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000876 Nodify(Dst, U, N1, SetRVal(St, U, V));
877 continue;
878 }
879
Ted Kremenek443003b2008-02-21 19:29:23 +0000880 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000881
882 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
883 : BinaryOperator::Sub;
884
Ted Kremenek443003b2008-02-21 19:29:23 +0000885 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000886
887 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000888 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000889 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000890 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000891
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000892 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000893 continue;
894 }
895
896 // Handle all other unary operators.
897
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000898 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000899
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000900 case UnaryOperator::Minus:
901 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000902 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000903
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000904 case UnaryOperator::Not:
905 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000906 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000907
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000908 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000909
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000910 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
911 //
912 // Note: technically we do "E == 0", but this is the same in the
913 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000914
915 if (isa<LVal>(SubV)) {
916 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
917 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
918 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000919 }
920 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000921 Expr* Ex = U->getSubExpr();
922 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000923 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
924 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000925 }
926
927 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000928
Ted Kremenek64924852008-01-31 02:35:41 +0000929 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000930 assert (isa<LVal>(SubV));
931 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000932 break;
933 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000934
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000935 default: ;
936 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000937 }
938
939 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000940 }
941}
942
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000943void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
944 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000945
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000946 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000947
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000948 // FIXME: Add support for VLAs.
949 if (!T.getTypePtr()->isConstantSizeType())
950 return;
951
952 SourceLocation Loc = U->getExprLoc();
953 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000954 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000955 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
956
957 Nodify(Dst, U, Pred, St);
958}
959
960void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +0000961
962 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
963 Dst.Add(Pred);
964 return;
965 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000966
967 Ex = Ex->IgnoreParens();
968
Ted Kremenek07932632008-02-27 06:47:26 +0000969 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000970 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000971 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000972 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000973
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000974 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000975 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000976 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000977
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000978 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000979 Dst.Add(Pred);
980 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000981 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000982
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000983 return;
984 }
985 }
986
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000987 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000988}
989
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000990void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000991 GRExprEngine::NodeTy* Pred,
992 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000993 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000994
995 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000996 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000997 else
998 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000999
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001000 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001001
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001002 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001003
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001004 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001005 // In such cases, we want to (initially) treat the LHS as an LVal,
1006 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1007 // evaluated to LValDecl's instead of to an NonLVal.
1008
1009 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
1010 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001011
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001012 // Visit the RHS...
1013
1014 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001015 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001016
1017 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001018
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001019 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001020
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001021 NodeTy* N2 = *I2;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001022 ValueState* St = N2->getState();
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001023 Expr* RHS = B->getRHS();
1024 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001025
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001026 BinaryOperator::Opcode Op = B->getOpcode();
1027
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001028 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1029 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001030
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001031 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001032
Ted Kremenek00155412008-02-26 22:27:51 +00001033 if (!RightV.isUnknown()) {
1034
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001035 if (RightV.isUndef()) {
1036 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001037
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001038 if (DivUndef) {
1039 DivUndef->markAsSink();
1040 BadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001041 }
1042
1043 continue;
1044 }
1045
1046 // Check for divide/remainder-by-zero.
1047 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001048 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001049
Ted Kremenek00155412008-02-26 22:27:51 +00001050 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001051 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek00155412008-02-26 22:27:51 +00001052
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001053 if (isFeasible)
1054 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
Ted Kremenek00155412008-02-26 22:27:51 +00001055 DivZeroNode->markAsSink();
1056 BadDivides.insert(DivZeroNode);
1057 }
Ted Kremenekd763eb92008-02-26 02:15:56 +00001058
Ted Kremenek00155412008-02-26 22:27:51 +00001059 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001060
Ted Kremenek00155412008-02-26 22:27:51 +00001061 isFeasible = false;
1062 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001063
Ted Kremenek00155412008-02-26 22:27:51 +00001064 if (!isFeasible)
1065 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001066 }
1067
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001068 // Fall-through. The logic below processes the divide.
1069 }
1070
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001071
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001072 if (Op <= BinaryOperator::Or) {
1073
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001074 // Process non-assignements except commas or short-circuited
1075 // logical expressions (LAnd and LOr).
1076
1077 RVal Result = EvalBinOp(Op, LeftV, RightV);
1078
1079 if (Result.isUnknown()) {
1080 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001081 continue;
1082 }
1083
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001084 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1085
1086 // The operands were not undefined, but the result is undefined.
1087
1088 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1089 UndefNode->markAsSink();
1090 UndefResults.insert(UndefNode);
1091 }
1092
1093 continue;
1094 }
1095
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001096 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001097 continue;
1098 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001099
1100 // Process assignments.
1101
1102 switch (Op) {
1103
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001104 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001105
1106 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001107
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001108 if (LeftV.isUndef()) {
1109 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001110 continue;
1111 }
1112
1113 if (LeftV.isUnknown()) {
1114 St = SetRVal(St, B, RightV);
1115 break;
1116 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001117
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001118 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001119 break;
1120 }
1121
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001122 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001123
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001124 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001125
Ted Kremenek00155412008-02-26 22:27:51 +00001126 assert (B->isCompoundAssignmentOp());
1127
1128 if (Op >= BinaryOperator::AndAssign)
1129 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1130 else
1131 ((int&) Op) -= BinaryOperator::MulAssign;
1132
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001133 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001134
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001135 if (LeftV.isUndef()) {
1136 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001137 continue;
1138 }
1139
1140 if (LeftV.isUnknown()) {
1141
1142 // While we do not know the location to store RightV,
1143 // the entire expression does evaluate to RightV.
1144
1145 if (RightV.isUnknown()) {
1146 Dst.Add(N2);
1147 continue;
1148 }
1149
1150 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001151 break;
1152 }
1153
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001154 // At this pointer we know that the LHS evaluates to an LVal
1155 // that is neither "Unknown" or "Unintialized."
1156
1157 LVal LeftLV = cast<LVal>(LeftV);
1158
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001159 // Fetch the value of the LHS (the value of the variable, etc.).
1160
1161 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1162
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001163 // Propagate undefined value (left-side). We
1164 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001165 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001166
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001167 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001168 St = SetRVal(St, B, V);
1169 break;
1170 }
1171
1172 // Propagate unknown values.
1173
Ted Kremenek89063af2008-02-21 19:15:37 +00001174 if (V.isUnknown()) {
1175 Dst.Add(N2);
1176 continue;
1177 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001178
1179 if (RightV.isUnknown()) {
1180 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1181 break;
1182 }
1183
Ted Kremenek00155412008-02-26 22:27:51 +00001184 // At this point:
1185 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001186 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001187 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001188
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001189 // Get the computation type.
1190 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1191
1192 // Perform promotions.
1193 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001194 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001195
1196 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001197
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001198 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1199 && RHS->getType()->isIntegerType()) {
1200
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001201 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001202
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001203 if (RightV.isUndef()) {
1204 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001205
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001206 if (DivUndef) {
1207 DivUndef->markAsSink();
1208 BadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001209 }
1210
1211 continue;
1212 }
Ted Kremenek00155412008-02-26 22:27:51 +00001213
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001214 // First, "assume" that the denominator is 0.
1215
1216 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001217 ValueState* ZeroSt = Assume(St, RightV, false, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001218
1219 if (isFeasible) {
1220 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1221
1222 if (DivZeroNode) {
1223 DivZeroNode->markAsSink();
Ted Kremenekd763eb92008-02-26 02:15:56 +00001224 BadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001225 }
1226 }
1227
1228 // Second, "assume" that the denominator cannot be 0.
1229
1230 isFeasible = false;
1231 St = Assume(St, RightV, true, isFeasible);
1232
1233 if (!isFeasible)
1234 continue;
1235
1236 // Fall-through. The logic below processes the divide.
1237 }
Ted Kremenek00155412008-02-26 22:27:51 +00001238 else {
1239
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001240 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001241
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001242 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001243 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1244 break;
1245 }
1246
1247 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001248
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001249 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001250
1251 if (Result.isUndef()) {
1252
1253 // The operands were not undefined, but the result is undefined.
1254
1255 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1256 UndefNode->markAsSink();
1257 UndefResults.insert(UndefNode);
1258 }
1259
1260 continue;
1261 }
1262
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001263 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001264 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001265 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001266
1267 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001268 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001269 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001270}
Ted Kremenekee985462008-01-16 18:18:48 +00001271
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001272void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001273 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1274 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001275 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001276}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001277
Ted Kremenek9dca0622008-02-19 00:22:37 +00001278void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001279
1280 // FIXME: add metadata to the CFG so that we can disable
1281 // this check when we KNOW that there is no block-level subexpression.
1282 // The motivation is that this check requires a hashtable lookup.
1283
1284 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1285 Dst.Add(Pred);
1286 return;
1287 }
1288
1289 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001290
1291 default:
1292 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001293 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001294
1295 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1296 break;
1297
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001298 case Stmt::BinaryOperatorClass: {
1299 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001300
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001301 if (B->isLogicalOp()) {
1302 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001303 break;
1304 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001305 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001306 ValueState* St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001307 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001308 break;
1309 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001310
1311 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1312 break;
1313 }
Ted Kremenekde434242008-02-19 01:44:53 +00001314
1315 case Stmt::CallExprClass: {
1316 CallExpr* C = cast<CallExpr>(S);
1317 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1318 break;
1319 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001320
1321 case Stmt::CastExprClass: {
1322 CastExpr* C = cast<CastExpr>(S);
1323 VisitCast(C, C->getSubExpr(), Pred, Dst);
1324 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001325 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001326
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001327 // FIXME: ChooseExpr is really a constant. We need to fix
1328 // the CFG do not model them as explicit control-flow.
1329
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001330 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1331 ChooseExpr* C = cast<ChooseExpr>(S);
1332 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1333 break;
1334 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001335
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001336 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001337 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1338 break;
1339
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001340 case Stmt::ConditionalOperatorClass: { // '?' operator
1341 ConditionalOperator* C = cast<ConditionalOperator>(S);
1342 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1343 break;
1344 }
1345
1346 case Stmt::DeclRefExprClass:
1347 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1348 break;
1349
1350 case Stmt::DeclStmtClass:
1351 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1352 break;
1353
1354 case Stmt::ImplicitCastExprClass: {
1355 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1356 VisitCast(C, C->getSubExpr(), Pred, Dst);
1357 break;
1358 }
1359
1360 case Stmt::ParenExprClass:
1361 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1362 break;
1363
1364 case Stmt::SizeOfAlignOfTypeExprClass:
1365 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1366 break;
1367
Ted Kremenekda9bd092008-02-08 07:05:39 +00001368 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001369 StmtExpr* SE = cast<StmtExpr>(S);
1370
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001371 ValueState* St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001372 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001373 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001374 break;
1375 }
1376
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001377 // FIXME: We may wish to always bind state to ReturnStmts so
1378 // that users can quickly query what was the state at the
1379 // exit points of a function.
1380
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001381 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001382 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1383 Visit(R, Pred, Dst);
1384 else
1385 Dst.Add(Pred);
1386
1387 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001388 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001389
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001390 case Stmt::UnaryOperatorClass: {
1391 UnaryOperator* U = cast<UnaryOperator>(S);
1392
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001393 switch (U->getOpcode()) {
1394 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1395 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1396 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1397 default: VisitUnaryOperator(U, Pred, Dst); break;
1398 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001399
Ted Kremenek9de04c42008-01-24 20:55:43 +00001400 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001401 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001402 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001403}
1404
Ted Kremenekee985462008-01-16 18:18:48 +00001405//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001406// "Assume" logic.
1407//===----------------------------------------------------------------------===//
1408
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001409ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001410 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001411 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001412 switch (Cond.getSubKind()) {
1413 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001414 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001415 return St;
1416
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001417 case lval::SymbolValKind:
1418 if (Assumption)
1419 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1420 ValMgr.getZeroWithPtrWidth(), isFeasible);
1421 else
1422 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1423 ValMgr.getZeroWithPtrWidth(), isFeasible);
1424
Ted Kremenek08b66252008-02-06 04:31:33 +00001425
Ted Kremenek329f8542008-02-05 21:52:21 +00001426 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001427 case lval::FuncValKind:
1428 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001429 isFeasible = Assumption;
1430 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001431
Ted Kremenek329f8542008-02-05 21:52:21 +00001432 case lval::ConcreteIntKind: {
1433 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001434 isFeasible = b ? Assumption : !Assumption;
1435 return St;
1436 }
1437 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001438}
1439
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001440ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001441 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001442 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001443 switch (Cond.getSubKind()) {
1444 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001445 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001446 return St;
1447
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001448
1449 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001450 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001451 SymbolID sym = SV.getSymbol();
1452
1453 if (Assumption)
1454 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1455 isFeasible);
1456 else
1457 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1458 isFeasible);
1459 }
1460
Ted Kremenek08b66252008-02-06 04:31:33 +00001461 case nonlval::SymIntConstraintValKind:
1462 return
1463 AssumeSymInt(St, Assumption,
1464 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1465 isFeasible);
1466
Ted Kremenek329f8542008-02-05 21:52:21 +00001467 case nonlval::ConcreteIntKind: {
1468 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001469 isFeasible = b ? Assumption : !Assumption;
1470 return St;
1471 }
1472 }
1473}
1474
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001475ValueState*
1476GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001477 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001478
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001479 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001480 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001481 isFeasible = *X != V;
1482 return St;
1483 }
1484
1485 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001486 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001487 isFeasible = true;
1488 return St;
1489 }
1490
1491 // If we reach here, sym is not a constant and we don't know if it is != V.
1492 // Make that assumption.
1493
1494 isFeasible = true;
1495 return StateMgr.AddNE(St, sym, V);
1496}
1497
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001498ValueState*
1499GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001500 const llvm::APSInt& V, bool& isFeasible) {
1501
1502 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001503 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001504 isFeasible = *X == V;
1505 return St;
1506 }
1507
1508 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001509 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001510 isFeasible = false;
1511 return St;
1512 }
1513
1514 // If we reach here, sym is not a constant and we don't know if it is == V.
1515 // Make that assumption.
1516
1517 isFeasible = true;
1518 return StateMgr.AddEQ(St, sym, V);
1519}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001520
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001521ValueState*
1522GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001523 const SymIntConstraint& C, bool& isFeasible) {
1524
1525 switch (C.getOpcode()) {
1526 default:
1527 // No logic yet for other operators.
1528 return St;
1529
1530 case BinaryOperator::EQ:
1531 if (Assumption)
1532 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1533 else
1534 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1535
1536 case BinaryOperator::NE:
1537 if (Assumption)
1538 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1539 else
1540 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1541 }
1542}
1543
Ted Kremenekb38911f2008-01-30 23:03:39 +00001544//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001545// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001546//===----------------------------------------------------------------------===//
1547
Ted Kremenekaa66a322008-01-16 21:46:15 +00001548#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001549static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001550
Ted Kremenekaa66a322008-01-16 21:46:15 +00001551namespace llvm {
1552template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001553struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001554 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001555
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001556 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001557
1558 Out << "Variables:\\l";
1559
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001560 bool isFirst = true;
1561
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001562 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001563
1564 if (isFirst)
1565 isFirst = false;
1566 else
1567 Out << "\\l";
1568
1569 Out << ' ' << I.getKey()->getName() << " : ";
1570 I.getData().print(Out);
1571 }
1572
1573 }
1574
Ted Kremeneke7d22112008-02-11 19:21:59 +00001575
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001576 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001577
1578 bool isFirst = true;
1579
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001580 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001581
1582 if (isFirst) {
1583 Out << "\\l\\lSub-Expressions:\\l";
1584 isFirst = false;
1585 }
1586 else
1587 Out << "\\l";
1588
1589 Out << " (" << (void*) I.getKey() << ") ";
1590 I.getKey()->printPretty(Out);
1591 Out << " : ";
1592 I.getData().print(Out);
1593 }
1594 }
1595
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001596 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001597
Ted Kremenek016f52f2008-02-08 21:10:02 +00001598 bool isFirst = true;
1599
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001600 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001601 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001602 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001603 isFirst = false;
1604 }
1605 else
1606 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001607
Ted Kremeneke7d22112008-02-11 19:21:59 +00001608 Out << " (" << (void*) I.getKey() << ") ";
1609 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001610 Out << " : ";
1611 I.getData().print(Out);
1612 }
1613 }
1614
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001615 static void PrintEQ(std::ostream& Out, ValueState* St) {
1616 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001617
1618 if (CE.isEmpty())
1619 return;
1620
1621 Out << "\\l\\|'==' constraints:";
1622
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001623 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001624 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1625 }
1626
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001627 static void PrintNE(std::ostream& Out, ValueState* St) {
1628 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001629
1630 if (NE.isEmpty())
1631 return;
1632
1633 Out << "\\l\\|'!=' constraints:";
1634
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001635 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001636 I != EI; ++I){
1637
1638 Out << "\\l $" << I.getKey() << " : ";
1639 bool isFirst = true;
1640
1641 ValueState::IntSetTy::iterator J=I.getData().begin(),
1642 EJ=I.getData().end();
1643 for ( ; J != EJ; ++J) {
1644 if (isFirst) isFirst = false;
1645 else Out << ", ";
1646
1647 Out << (*J)->toString();
1648 }
1649 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001650 }
1651
1652 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1653
1654 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001655 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001656 GraphPrintCheckerState->isUndefDeref(N) ||
1657 GraphPrintCheckerState->isUndefStore(N) ||
1658 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001659 GraphPrintCheckerState->isBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001660 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001661 GraphPrintCheckerState->isBadCall(N) ||
1662 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001663 return "color=\"red\",style=\"filled\"";
1664
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001665 if (GraphPrintCheckerState->isNoReturnCall(N))
1666 return "color=\"blue\",style=\"filled\"";
1667
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001668 return "";
1669 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001670
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001671 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001672 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001673
1674 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001675 ProgramPoint Loc = N->getLocation();
1676
1677 switch (Loc.getKind()) {
1678 case ProgramPoint::BlockEntranceKind:
1679 Out << "Block Entrance: B"
1680 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1681 break;
1682
1683 case ProgramPoint::BlockExitKind:
1684 assert (false);
1685 break;
1686
1687 case ProgramPoint::PostStmtKind: {
1688 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001689 Out << L.getStmt()->getStmtClassName() << ':'
1690 << (void*) L.getStmt() << ' ';
1691
Ted Kremenekaa66a322008-01-16 21:46:15 +00001692 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001693
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001694 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001695 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001696 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00001697 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001698 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001699 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001700 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001701 Out << "\\|Store to Undefined LVal.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001702 else if (GraphPrintCheckerState->isBadDivide(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001703 Out << "\\|Divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001704 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001705 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001706 else if (GraphPrintCheckerState->isNoReturnCall(N))
1707 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001708 else if (GraphPrintCheckerState->isBadCall(N))
1709 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001710 else if (GraphPrintCheckerState->isUndefArg(N))
1711 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001712
Ted Kremenekaa66a322008-01-16 21:46:15 +00001713 break;
1714 }
1715
1716 default: {
1717 const BlockEdge& E = cast<BlockEdge>(Loc);
1718 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1719 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001720
1721 if (Stmt* T = E.getSrc()->getTerminator()) {
1722 Out << "\\|Terminator: ";
1723 E.getSrc()->printTerminator(Out);
1724
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001725 if (isa<SwitchStmt>(T)) {
1726 Stmt* Label = E.getDst()->getLabel();
1727
1728 if (Label) {
1729 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1730 Out << "\\lcase ";
1731 C->getLHS()->printPretty(Out);
1732
1733 if (Stmt* RHS = C->getRHS()) {
1734 Out << " .. ";
1735 RHS->printPretty(Out);
1736 }
1737
1738 Out << ":";
1739 }
1740 else {
1741 assert (isa<DefaultStmt>(Label));
1742 Out << "\\ldefault:";
1743 }
1744 }
1745 else
1746 Out << "\\l(implicit) default:";
1747 }
1748 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001749 // FIXME
1750 }
1751 else {
1752 Out << "\\lCondition: ";
1753 if (*E.getSrc()->succ_begin() == E.getDst())
1754 Out << "true";
1755 else
1756 Out << "false";
1757 }
1758
1759 Out << "\\l";
1760 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001761
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001762 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
1763 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001764 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001765 }
1766 }
1767
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001768 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001769
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001770 N->getState()->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001771
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001772 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001773 return Out.str();
1774 }
1775};
1776} // end llvm namespace
1777#endif
1778
Ted Kremeneke01c9872008-02-14 22:36:46 +00001779void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001780#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001781 GraphPrintCheckerState = this;
1782 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001783 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001784#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001785}