blob: 6072094d057bac12565b97212cc03b3b52fd77c0 [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 Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000020
Ted Kremenek0f5f0592008-02-27 06:07:00 +000021#ifndef NDEBUG
22#include "llvm/Support/GraphWriter.h"
23#include <sstream>
24#endif
25
Ted Kremenekb387a3f2008-02-14 22:16:04 +000026using namespace clang;
27using llvm::dyn_cast;
28using llvm::cast;
29using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000030
Ted Kremeneke695e1c2008-04-15 23:06:53 +000031//===----------------------------------------------------------------------===//
32// Engine construction and deletion.
33//===----------------------------------------------------------------------===//
34
Ted Kremenekdaa497e2008-03-09 18:05:48 +000035
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000036GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx)
37 : CoreEngine(cfg, CD, Ctx, *this),
38 G(CoreEngine.getGraph()),
39 Liveness(G.getCFG()),
40 Builder(NULL),
41 StateMgr(G.getContext(), G.getAllocator()),
42 BasicVals(StateMgr.getBasicValueFactory()),
43 TF(NULL), // FIXME
44 SymMgr(StateMgr.getSymbolManager()),
45 StmtEntryNode(NULL), CleanedState(NULL), CurrentStmt(NULL) {
46
47 // Compute liveness information.
48 Liveness.runOnCFG(G.getCFG());
49 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
50}
51
52GRExprEngine::~GRExprEngine() {
53 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
54 delete *I;
55
56 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
57 I != E; ++I)
58 delete *I;
59
60 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
61 I != E; ++I)
62 delete *I;
63}
64
Ted Kremeneke695e1c2008-04-15 23:06:53 +000065//===----------------------------------------------------------------------===//
66// Utility methods.
67//===----------------------------------------------------------------------===//
68
69// SaveAndRestore - A utility class that uses RIIA to save and restore
70// the value of a variable.
71template<typename T>
72struct VISIBILITY_HIDDEN SaveAndRestore {
73 SaveAndRestore(T& x) : X(x), old_value(x) {}
74 ~SaveAndRestore() { X = old_value; }
75 T get() { return old_value; }
76
77 T& X;
78 T old_value;
79};
80
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000081void GRExprEngine::EmitWarnings(Diagnostic& Diag, PathDiagnosticClient* PD) {
82 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
83 BugReporter BR(Diag, PD, getContext(), *this);
84 (*I)->EmitWarnings(BR);
85 }
86
87 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
88 I != E; ++I) {
89 BugReporter BR(Diag, PD, getContext(), *this);
90 (*I)->EmitWarnings(BR);
91 }
92
93 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
94 I != E; ++I) {
95 BugReporter BR(Diag, PD, getContext(), *this);
96 (*I)->EmitWarnings(BR);
97 }
98}
99
100void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
101 TF = tf;
102 TF->RegisterChecks(*this);
103}
104
105void GRExprEngine::AddCallCheck(GRSimpleAPICheck* A) {
106 CallChecks.push_back(A);
107}
108
109void GRExprEngine::AddObjCMessageExprCheck(GRSimpleAPICheck* A) {
110 MsgExprChecks.push_back(A);
111}
112
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000113ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000114
115 // The LiveVariables information already has a compilation of all VarDecls
116 // used in the function. Iterate through this set, and "symbolicate"
117 // any VarDecl whose value originally comes from outside the function.
118
119 typedef LiveVariables::AnalysisDataTy LVDataTy;
120 LVDataTy& D = Liveness.getAnalysisData();
121
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000122 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000123
124 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
125
126 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
127
128 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
129 RVal X = RVal::GetSymbolValue(SymMgr, VD);
130 StateMgr.BindVar(StateImpl, VD, X);
131 }
132 }
133
134 return StateMgr.getPersistentState(StateImpl);
135}
136
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000137ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000138
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000139 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000140
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000141 if (Ex == CurrentStmt) {
142 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000143
144 if (!isBlkExpr)
145 return St;
146 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000147
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000148 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000149}
150
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000151//===----------------------------------------------------------------------===//
152// Top-level transfer function logic (Dispatcher).
153//===----------------------------------------------------------------------===//
154
155void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
156
157 Builder = &builder;
158 StmtEntryNode = builder.getLastNode();
159 CurrentStmt = S;
160 NodeSet Dst;
161
162 // Set up our simple checks.
163
164 // FIXME: This can probably be installed directly in GRCoreEngine, obviating
165 // the need to do a copy every time we hit a block-level statement.
166
167 if (!MsgExprChecks.empty())
168 Builder->setObjCMsgExprAuditors((GRAuditor<ValueState>**) &MsgExprChecks[0],
169 (GRAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size()));
170
171
172 if (!CallChecks.empty())
173 Builder->setCallExprAuditors((GRAuditor<ValueState>**) &CallChecks[0],
174 (GRAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size()));
175
176 // Create the cleaned state.
177
178 CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(),
179 CurrentStmt, Liveness);
180
181 Builder->SetCleanedState(CleanedState);
182
183 // Visit the statement.
184
185 Visit(S, StmtEntryNode, Dst);
186
187 // If no nodes were generated, generate a new node that has all the
188 // dead mappings removed.
189
Ted Kremenekeaa9eda2008-04-18 19:34:16 +0000190 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode &&
Ted Kremenekb0533962008-04-18 20:35:30 +0000191 !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000192 builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode);
193
194 // NULL out these variables to cleanup.
195
196 CurrentStmt = NULL;
197 StmtEntryNode = NULL;
198 Builder = NULL;
199 CleanedState = NULL;
200}
201
202void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
203
204 // FIXME: add metadata to the CFG so that we can disable
205 // this check when we KNOW that there is no block-level subexpression.
206 // The motivation is that this check requires a hashtable lookup.
207
208 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
209 Dst.Add(Pred);
210 return;
211 }
212
213 switch (S->getStmtClass()) {
214
215 default:
216 // Cases we intentionally have "default" handle:
217 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
218
219 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
220 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000221
222 case Stmt::ArraySubscriptExprClass:
223 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
224 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000225
226 case Stmt::AsmStmtClass:
227 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
228 break;
229
230 case Stmt::BinaryOperatorClass: {
231 BinaryOperator* B = cast<BinaryOperator>(S);
232
233 if (B->isLogicalOp()) {
234 VisitLogicalExpr(B, Pred, Dst);
235 break;
236 }
237 else if (B->getOpcode() == BinaryOperator::Comma) {
238 ValueState* St = GetState(Pred);
239 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
240 break;
241 }
242
243 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
244 break;
245 }
246
247 case Stmt::CallExprClass: {
248 CallExpr* C = cast<CallExpr>(S);
249 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
250 break;
251 }
252
253 case Stmt::CastExprClass: {
254 CastExpr* C = cast<CastExpr>(S);
255 VisitCast(C, C->getSubExpr(), Pred, Dst);
256 break;
257 }
258
259 // FIXME: ChooseExpr is really a constant. We need to fix
260 // the CFG do not model them as explicit control-flow.
261
262 case Stmt::ChooseExprClass: { // __builtin_choose_expr
263 ChooseExpr* C = cast<ChooseExpr>(S);
264 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
265 break;
266 }
267
268 case Stmt::CompoundAssignOperatorClass:
269 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
270 break;
271
272 case Stmt::ConditionalOperatorClass: { // '?' operator
273 ConditionalOperator* C = cast<ConditionalOperator>(S);
274 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
275 break;
276 }
277
278 case Stmt::DeclRefExprClass:
279 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
280 break;
281
282 case Stmt::DeclStmtClass:
283 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
284 break;
285
286 case Stmt::ImplicitCastExprClass: {
287 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
288 VisitCast(C, C->getSubExpr(), Pred, Dst);
289 break;
290 }
291
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000292 case Stmt::MemberExprClass: {
293 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
294 break;
295 }
296
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297 case Stmt::ObjCMessageExprClass: {
298 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
299 break;
300 }
301
302 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000303 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000304 break;
305
306 case Stmt::SizeOfAlignOfTypeExprClass:
307 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
308 break;
309
310 case Stmt::StmtExprClass: {
311 StmtExpr* SE = cast<StmtExpr>(S);
312
313 ValueState* St = GetState(Pred);
314
315 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
316 // probably just remove these from the CFG.
317 assert (!SE->getSubStmt()->body_empty());
318
319 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
320 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
321 else
322 Dst.Add(Pred);
323
324 break;
325 }
326
327 // FIXME: We may wish to always bind state to ReturnStmts so
328 // that users can quickly query what was the state at the
329 // exit points of a function.
330
331 case Stmt::ReturnStmtClass:
332 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst); break;
333
334 case Stmt::UnaryOperatorClass: {
335 UnaryOperator* U = cast<UnaryOperator>(S);
336
337 switch (U->getOpcode()) {
338 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
339 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
340 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
341 default: VisitUnaryOperator(U, Pred, Dst); break;
342 }
343
344 break;
345 }
346 }
347}
348
349//===----------------------------------------------------------------------===//
350// Block entrance. (Update counters).
351//===----------------------------------------------------------------------===//
352
353bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
354 GRBlockCounter BC) {
355
356 return BC.getNumVisited(B->getBlockID()) < 3;
357}
358
359//===----------------------------------------------------------------------===//
360// Branch processing.
361//===----------------------------------------------------------------------===//
362
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000363ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
364 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000365
366 switch (Terminator->getStmtClass()) {
367 default:
368 return St;
369
370 case Stmt::BinaryOperatorClass: { // '&&' and '||'
371
372 BinaryOperator* B = cast<BinaryOperator>(Terminator);
373 BinaryOperator::Opcode Op = B->getOpcode();
374
375 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
376
377 // For &&, if we take the true branch, then the value of the whole
378 // expression is that of the RHS expression.
379 //
380 // For ||, if we take the false branch, then the value of the whole
381 // expression is that of the RHS expression.
382
383 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
384 (Op == BinaryOperator::LOr && !branchTaken)
385 ? B->getRHS() : B->getLHS();
386
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000387 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000388 }
389
390 case Stmt::ConditionalOperatorClass: { // ?:
391
392 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
393
394 // For ?, if branchTaken == true then the value is either the LHS or
395 // the condition itself. (GNU extension).
396
397 Expr* Ex;
398
399 if (branchTaken)
400 Ex = C->getLHS() ? C->getLHS() : C->getCond();
401 else
402 Ex = C->getRHS();
403
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000404 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000405 }
406
407 case Stmt::ChooseExprClass: { // ?:
408
409 ChooseExpr* C = cast<ChooseExpr>(Terminator);
410
411 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000412 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000413 }
414 }
415}
416
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000417void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000418 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000419
Ted Kremeneke7d22112008-02-11 19:21:59 +0000420 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000421 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000422
Ted Kremenekb2331832008-02-15 22:29:00 +0000423 // Check for NULL conditions; e.g. "for(;;)"
424 if (!Condition) {
425 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000426 return;
427 }
428
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000429 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000430
431 switch (V.getBaseKind()) {
432 default:
433 break;
434
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000435 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000436 builder.generateNode(MarkBranch(PrevState, Term, true), true);
437 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000438 return;
439
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000440 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000441 NodeTy* N = builder.generateNode(PrevState, true);
442
443 if (N) {
444 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000445 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000446 }
447
448 builder.markInfeasible(false);
449 return;
450 }
451 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000452
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000453 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000454
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000455 bool isFeasible = false;
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000456 ValueState* St = Assume(PrevState, V, true, isFeasible);
457
458 if (isFeasible)
459 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000460 else
461 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000462
463 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000464
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000465 isFeasible = false;
466 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000467
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000468 if (isFeasible)
469 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000470 else
471 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000472}
473
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000474/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000475/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000476void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000477
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000478 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000479 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000480
481 // Three possibilities:
482 //
483 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000484 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000485 // (3) We have no clue about the label. Dispatch to all targets.
486 //
487
488 typedef IndirectGotoNodeBuilder::iterator iterator;
489
490 if (isa<lval::GotoLabel>(V)) {
491 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
492
493 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000494 if (I.getLabel() == L) {
495 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000496 return;
497 }
498 }
499
500 assert (false && "No block with label.");
501 return;
502 }
503
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000504 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000505 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000506 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000507 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000508 return;
509 }
510
511 // This is really a catch-all. We don't support symbolics yet.
512
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000513 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000514
515 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000516 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000517}
Ted Kremenekf233d482008-02-05 00:26:40 +0000518
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000519
520void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
521 NodeTy* Pred, NodeSet& Dst) {
522
523 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
524
525 ValueState* St = GetState(Pred);
526 RVal X = GetBlkExprRVal(St, Ex);
527
528 assert (X.isUndef());
529
530 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
531
532 assert (SE);
533
534 X = GetBlkExprRVal(St, SE);
535
536 // Make sure that we invalidate the previous binding.
537 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
538}
539
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000540/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
541/// nodes by processing the 'effects' of a switch statement.
542void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
543
544 typedef SwitchNodeBuilder::iterator iterator;
545
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000546 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000547 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000548 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000549
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000550 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000551 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000552 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000553 return;
554 }
555
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000556 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000557
558 // While most of this can be assumed (such as the signedness), having it
559 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000560
Chris Lattner98be4942008-03-05 18:54:05 +0000561 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000562
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000563 APSInt V1(bits, false);
564 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000565 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000566
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000567 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000568
569 CaseStmt* Case = cast<CaseStmt>(I.getCase());
570
571 // Evaluate the case.
572 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
573 assert (false && "Case condition must evaluate to an integer constant.");
574 return;
575 }
576
577 // Get the RHS of the case, if it exists.
578
579 if (Expr* E = Case->getRHS()) {
580 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
581 assert (false &&
582 "Case condition (RHS) must evaluate to an integer constant.");
583 return ;
584 }
585
586 assert (V1 <= V2);
587 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000588 else
589 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000590
591 // FIXME: Eventually we should replace the logic below with a range
592 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000593 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000594
Ted Kremenek14a11402008-03-17 22:17:56 +0000595 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000596 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000597
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000598 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000599
600 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000601
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000602 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000603 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000604
605 if (isFeasible) {
606 builder.generateCaseStmtNode(I, StNew);
607
608 // If CondV evaluates to a constant, then we know that this
609 // is the *only* case that we can take, so stop evaluating the
610 // others.
611 if (isa<nonlval::ConcreteInt>(CondV))
612 return;
613 }
614
615 // Now "assume" that the case doesn't match. Add this state
616 // to the default state (if it is feasible).
617
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000618 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000619 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000620
Ted Kremenek5014ab12008-04-23 05:03:18 +0000621 if (isFeasible) {
622 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000623 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000624 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000625
Ted Kremenek14a11402008-03-17 22:17:56 +0000626 // Concretize the next value in the range.
627 if (V1 == V2)
628 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000629
Ted Kremenek14a11402008-03-17 22:17:56 +0000630 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000631 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000632
633 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000634 }
635
636 // If we reach here, than we know that the default branch is
637 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000638 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000639}
640
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000641//===----------------------------------------------------------------------===//
642// Transfer functions: logical operations ('&&', '||').
643//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000644
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000645void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000646 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000647
Ted Kremenek05a23782008-02-26 19:05:15 +0000648 assert (B->getOpcode() == BinaryOperator::LAnd ||
649 B->getOpcode() == BinaryOperator::LOr);
650
651 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
652
Ted Kremenek0d093d32008-03-10 04:11:42 +0000653 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000654 RVal X = GetBlkExprRVal(St, B);
655
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000656 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000657
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000658 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000659
660 assert (Ex);
661
662 if (Ex == B->getRHS()) {
663
664 X = GetBlkExprRVal(St, Ex);
665
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000666 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000667
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000668 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000669 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000670 return;
671 }
672
Ted Kremenek05a23782008-02-26 19:05:15 +0000673 // We took the RHS. Because the value of the '&&' or '||' expression must
674 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
675 // or 1. Alternatively, we could take a lazy approach, and calculate this
676 // value later when necessary. We don't have the machinery in place for
677 // this right now, and since most logical expressions are used for branches,
678 // the payoff is not likely to be large. Instead, we do eager evaluation.
679
680 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000681 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000682
683 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000684 MakeNode(Dst, B, Pred,
685 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000686
687 isFeasible = false;
688 NewState = Assume(St, X, false, isFeasible);
689
690 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000691 MakeNode(Dst, B, Pred,
692 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000693 }
694 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000695 // We took the LHS expression. Depending on whether we are '&&' or
696 // '||' we know what the value of the expression is via properties of
697 // the short-circuiting.
698
699 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000700 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000701 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000702}
Ted Kremenek05a23782008-02-26 19:05:15 +0000703
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000704//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000705// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000706//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000707
Ted Kremenek44842c22008-02-13 18:06:44 +0000708void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000709
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000710 if (D != CurrentStmt) {
711 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
712 return;
713 }
714
715 // If we are here, we are loading the value of the decl and binding
716 // it to the block-level expression.
717
Ted Kremenek0d093d32008-03-10 04:11:42 +0000718 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000719 RVal X = RVal::MakeVal(BasicVals, D);
720 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenek0e561a32008-03-21 21:30:14 +0000721 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000722}
723
Ted Kremenek540cbe22008-04-22 04:56:29 +0000724/// VisitArraySubscriptExpr - Transfer function for array accesses
725void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
726 NodeSet& Dst, bool asLVal) {
727
728 Expr* Base = A->getBase()->IgnoreParens();
729
730 // Evaluate the base.
731 NodeSet Tmp1;
732 Visit(Base, Pred, Tmp1);
733
734 // Dereference the base.
735 NodeSet Tmp2;
736
737 for (NodeSet::iterator I=Tmp1.begin(), E=Tmp1.end(); I!=E; ++I) {
738 ValueState* St = GetState(*I);
739 VisitDeref(Base, GetRVal(St, Base), St, *I, Tmp2, true);
740 }
741
742 // Get the index.
743 Tmp1.clear();
744 Expr* Index = A->getIdx()->IgnoreParens();
745
746 for (NodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I)
747 Visit(Index, *I, Dst);
748}
749
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000750/// VisitMemberExpr - Transfer function for member expressions.
751void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
752 NodeSet& Dst, bool asLVal) {
753
754 Expr* Base = M->getBase()->IgnoreParens();
755
756 NodeSet Tmp;
757 VisitLVal(Base, Pred, Tmp);
758
759 if (Base->getType()->isPointerType()) {
760 NodeSet Tmp2;
761
762 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
763 ValueState* St = GetState(*I);
764 VisitDeref(Base, GetRVal(St, Base), St, *I, Tmp2, true);
765 }
766
767 for (NodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I)
768 VisitMemberExprField(M, Base, *I, Dst, asLVal);
769 }
770 else
771 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I)
772 VisitMemberExprField(M, Base, *I, Dst, asLVal);
773}
774
775void GRExprEngine::VisitMemberExprField(MemberExpr* M, Expr* Base, NodeTy* Pred,
776 NodeSet& Dst, bool asLVal) {
777 Dst.Add(Pred);
778}
779
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000780void GRExprEngine::EvalStore(NodeSet& Dst, Expr* E, NodeTy* Pred,
781 ValueState* St, RVal TargetLV, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000782
783 assert (Builder && "GRStmtNodeBuilder must be defined.");
784
785 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000786
787 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
788 OldHasGen(Builder->HasGeneratedNode);
789
790 Builder->HasGeneratedNode = false;
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000791
Ted Kremenek13922612008-04-16 20:40:59 +0000792 assert (!TargetLV.isUndef());
793
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000794 TF->EvalStore(Dst, *this, *Builder, E, Pred, St, TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000795
796 // Handle the case where no nodes where generated. Auto-generate that
797 // contains the updated state if we aren't generating sinks.
798
Ted Kremenekb0533962008-04-18 20:35:30 +0000799 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek13922612008-04-16 20:40:59 +0000800 TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, E, Pred, St,
801 TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000802}
803
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000804//===----------------------------------------------------------------------===//
805// Transfer function: Function calls.
806//===----------------------------------------------------------------------===//
807
Ted Kremenekde434242008-02-19 01:44:53 +0000808void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000809 CallExpr::arg_iterator AI,
810 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000811 NodeSet& Dst) {
812
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000813 // Process the arguments.
814
815 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000816
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000817 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000818 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000819 ++AI;
820
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000821 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000822 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000823
824 return;
825 }
826
827 // If we reach here we have processed all of the arguments. Evaluate
828 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000829
Ted Kremenek994a09b2008-02-25 21:16:03 +0000830 NodeSet DstTmp;
831 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000832
Ted Kremenek994a09b2008-02-25 21:16:03 +0000833 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000834
835 if (DstTmp.empty())
836 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000837
838 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000839 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
840
Ted Kremenek0d093d32008-03-10 04:11:42 +0000841 ValueState* St = GetState(*DI);
Ted Kremenek994a09b2008-02-25 21:16:03 +0000842 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000843
Ted Kremeneka1354a52008-03-03 16:47:31 +0000844 // FIXME: Add support for symbolic function calls (calls involving
845 // function pointer values that are symbolic).
846
847 // Check for undefined control-flow or calls to NULL.
848
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000849 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000850 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000851
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000852 if (N) {
853 N->markAsSink();
854 BadCalls.insert(N);
855 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000856
Ted Kremenekde434242008-02-19 01:44:53 +0000857 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000858 }
859
860 // Check for the "noreturn" attribute.
861
862 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
863
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000864 if (isa<lval::FuncVal>(L)) {
865
866 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
867
868 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000869 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000870 else {
871 // HACK: Some functions are not marked noreturn, and don't return.
872 // Here are a few hardwired ones. If this takes too long, we can
873 // potentially cache these results.
874 const char* s = FD->getIdentifier()->getName();
875 unsigned n = strlen(s);
876
877 switch (n) {
878 default:
879 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000880
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000881 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000882 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
883 break;
884
885 case 5:
886 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
887 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +0000888
889 case 6:
890 if (!memcmp(s, "Assert", 6)) Builder->BuildSinks = true;
891 break;
Ted Kremenek688738f2008-04-23 00:41:25 +0000892
893 case 7:
894 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
895 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +0000896
897 case 14:
898 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
899 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000900 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +0000901
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000902 }
903 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000904
905 // Evaluate the call.
906
Ted Kremeneka1354a52008-03-03 16:47:31 +0000907
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000908 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000909
Ted Kremenek03da0d72008-02-21 19:46:04 +0000910 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000911 // Check for an "unknown" callee.
912 invalidateArgs = true;
913 }
914 else if (isa<lval::FuncVal>(L)) {
915
916 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
917
Ted Kremenek55aea312008-03-05 22:59:42 +0000918 if (unsigned id = Info->getBuiltinID()) {
919 switch (id) {
920 case Builtin::BI__builtin_expect: {
921 // For __builtin_expect, just return the value of the subexpression.
922 assert (CE->arg_begin() != CE->arg_end());
923 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +0000924 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +0000925 continue;
926 }
927
928 default:
929 invalidateArgs = true;
930 break;
931 }
932 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000933 }
934
935 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000936 // Invalidate all arguments passed in by reference (LVals).
937 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
938 I != E; ++I) {
939 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000940
Ted Kremenek03da0d72008-02-21 19:46:04 +0000941 if (isa<LVal>(V))
942 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000943 }
944
Ted Kremenek0e561a32008-03-21 21:30:14 +0000945 MakeNode(Dst, CE, *DI, St);
Ted Kremenek03da0d72008-02-21 19:46:04 +0000946 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000947 else {
948
949 // Check any arguments passed-by-value against being undefined.
950
951 bool badArg = false;
952
953 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
954 I != E; ++I) {
955
Ted Kremenek0d093d32008-03-10 04:11:42 +0000956 if (GetRVal(GetState(*DI), *I).isUndef()) {
957 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000958
959 if (N) {
960 N->markAsSink();
961 UndefArgs[N] = *I;
962 }
963
964 badArg = true;
965 break;
966 }
967 }
968
969 if (badArg)
970 continue;
971
972 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000973
974 unsigned size = Dst.size();
975
Ted Kremenekb0533962008-04-18 20:35:30 +0000976 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
977 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek940b1d82008-04-10 23:44:06 +0000978
Ted Kremenekb0533962008-04-18 20:35:30 +0000979 Builder->HasGeneratedNode = false;
980
Ted Kremenek330dddd2008-03-05 00:33:14 +0000981 EvalCall(Dst, CE, cast<LVal>(L), *DI);
982
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000983 // Handle the case where no nodes where generated. Auto-generate that
984 // contains the updated state if we aren't generating sinks.
985
Ted Kremenekb0533962008-04-18 20:35:30 +0000986 if (!Builder->BuildSinks && Dst.size() == size &&
987 !Builder->HasGeneratedNode)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000988 MakeNode(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000989 }
Ted Kremenekde434242008-02-19 01:44:53 +0000990 }
991}
992
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000993//===----------------------------------------------------------------------===//
994// Transfer function: Objective-C message expressions.
995//===----------------------------------------------------------------------===//
996
997void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
998 NodeSet& Dst){
999
1000 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1001 Pred, Dst);
1002}
1003
1004void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1005 ObjCMessageExpr::arg_iterator AI,
1006 ObjCMessageExpr::arg_iterator AE,
1007 NodeTy* Pred, NodeSet& Dst) {
1008 if (AI == AE) {
1009
1010 // Process the receiver.
1011
1012 if (Expr* Receiver = ME->getReceiver()) {
1013 NodeSet Tmp;
1014 Visit(Receiver, Pred, Tmp);
1015
1016 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1017 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1018
1019 return;
1020 }
1021
1022 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1023 return;
1024 }
1025
1026 NodeSet Tmp;
1027 Visit(*AI, Pred, Tmp);
1028
1029 ++AI;
1030
1031 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1032 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1033}
1034
1035void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1036 NodeTy* Pred,
1037 NodeSet& Dst) {
1038
1039 // FIXME: More logic for the processing the method call.
1040
1041 ValueState* St = GetState(Pred);
1042
1043 if (Expr* Receiver = ME->getReceiver()) {
1044
1045 RVal L = GetRVal(St, Receiver);
1046
1047 // Check for undefined control-flow or calls to NULL.
1048
1049 if (L.isUndef()) {
1050 NodeTy* N = Builder->generateNode(ME, St, Pred);
1051
1052 if (N) {
1053 N->markAsSink();
1054 UndefReceivers.insert(N);
1055 }
1056
1057 return;
1058 }
1059 }
1060
1061 // Check for any arguments that are uninitialized/undefined.
1062
1063 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1064 I != E; ++I) {
1065
1066 if (GetRVal(St, *I).isUndef()) {
1067
1068 // Generate an error node for passing an uninitialized/undefined value
1069 // as an argument to a message expression. This node is a sink.
1070 NodeTy* N = Builder->generateNode(ME, St, Pred);
1071
1072 if (N) {
1073 N->markAsSink();
1074 MsgExprUndefArgs[N] = *I;
1075 }
1076
1077 return;
1078 }
1079 }
1080 // Dispatch to plug-in transfer function.
1081
1082 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001083
1084 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1085 OldHasGen(Builder->HasGeneratedNode);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001086
Ted Kremenekb0533962008-04-18 20:35:30 +00001087 Builder->HasGeneratedNode = false;
1088
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001089 EvalObjCMessageExpr(Dst, ME, Pred);
1090
1091 // Handle the case where no nodes where generated. Auto-generate that
1092 // contains the updated state if we aren't generating sinks.
1093
Ted Kremenekb0533962008-04-18 20:35:30 +00001094 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001095 MakeNode(Dst, ME, Pred, St);
1096}
1097
1098//===----------------------------------------------------------------------===//
1099// Transfer functions: Miscellaneous statements.
1100//===----------------------------------------------------------------------===//
1101
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001102void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001103
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001104 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001105 QualType T = CastE->getType();
1106
Ted Kremenek65cfb732008-03-04 22:16:08 +00001107 if (T->isReferenceType())
1108 VisitLVal(Ex, Pred, S1);
1109 else
1110 Visit(Ex, Pred, S1);
1111
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001112 // Check for casting to "void".
1113 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001114
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001115 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001116 Dst.Add(*I1);
1117
Ted Kremenek874d63f2008-01-24 02:02:54 +00001118 return;
1119 }
1120
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001121 // FIXME: The rest of this should probably just go into EvalCall, and
1122 // let the transfer function object be responsible for constructing
1123 // nodes.
1124
1125 QualType ExTy = Ex->getType();
1126
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001127 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001128 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001129 ValueState* St = GetState(N);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001130 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001131
1132 // Unknown?
1133
1134 if (V.isUnknown()) {
1135 Dst.Add(N);
1136 continue;
1137 }
1138
1139 // Undefined?
1140
1141 if (V.isUndef()) {
1142 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1143 continue;
1144 }
1145
1146 // Check for casts from pointers to integers.
1147 if (T->isIntegerType() && ExTy->isPointerType()) {
1148 unsigned bits = getContext().getTypeSize(ExTy);
1149
1150 // FIXME: Determine if the number of bits of the target type is
1151 // equal or exceeds the number of bits to store the pointer value.
1152 // If not, flag an error.
1153
1154 V = nonlval::LValAsInteger::Make(BasicVals, cast<LVal>(V), bits);
1155 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1156 continue;
1157 }
1158
1159 // Check for casts from integers to pointers.
1160 if (T->isPointerType() && ExTy->isIntegerType())
1161 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1162 // Just unpackage the lval and return it.
1163 V = LV->getLVal();
1164 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1165 continue;
1166 }
1167
1168 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001169
Ted Kremenek0e561a32008-03-21 21:30:14 +00001170 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001171 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001172}
1173
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001174void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1175 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1176}
1177
1178void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1179 NodeTy* Pred, NodeSet& Dst) {
1180
1181 if (!D)
1182 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001183
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001184 if (!isa<VarDecl>(D)) {
1185 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1186 return;
1187 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001188
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001189 const VarDecl* VD = dyn_cast<VarDecl>(D);
1190
1191 // FIXME: Add support for local arrays.
1192 if (VD->getType()->isArrayType()) {
1193 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1194 return;
1195 }
1196
1197 Expr* Ex = const_cast<Expr*>(VD->getInit());
1198
1199 // FIXME: static variables may have an initializer, but the second
1200 // time a function is called those values may not be current.
1201 NodeSet Tmp;
1202
1203 if (Ex) Visit(Ex, Pred, Tmp);
1204 if (Tmp.empty()) Tmp.Add(Pred);
1205
1206 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001207
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001208 ValueState* St = GetState(*I);
1209
1210 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001211
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001212 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001213
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001214 // FIXME: static variables may have an initializer, but the second
1215 // time a function is called those values may not be current.
1216
1217
1218 // In this context, Static => Local variable.
1219
1220 assert (!VD->getStorageClass() == VarDecl::Static ||
1221 !VD->isFileVarDecl());
1222
1223 // If there is no initializer, set the value of the
1224 // variable to "Undefined".
1225
1226 if (VD->getStorageClass() == VarDecl::Static) {
1227
1228 // C99: 6.7.8 Initialization
1229 // If an object that has static storage duration is not initialized
1230 // explicitly, then:
1231 // —if it has pointer type, it is initialized to a null pointer;
1232 // —if it has arithmetic type, it is initialized to (positive or
1233 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001234
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001235 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001236
1237 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001238
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001239 if (T->isPointerType())
1240 St = SetRVal(St, lval::DeclVal(VD),
1241 lval::ConcreteInt(BasicVals.getValue(0, T)));
1242 else if (T->isIntegerType())
1243 St = SetRVal(St, lval::DeclVal(VD),
1244 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001245
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001246 // FIXME: Handle structs. Now we treat them as unknown. What
1247 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001248 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001249 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001250 else {
1251
1252 // FIXME: Handle structs. Now we treat them as unknown. What
1253 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001254
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001255 QualType T = VD->getType();
1256
1257 if (T->isPointerType() || T->isIntegerType())
1258 St = SetRVal(St, lval::DeclVal(VD),
1259 Ex ? GetRVal(St, Ex) : UndefinedVal());
1260 }
1261
1262 // Create a new node. We don't really need to create a new NodeSet
1263 // here, but it simplifies things and doesn't cost much.
1264 NodeSet Tmp2;
1265 MakeNode(Tmp2, DS, *I, St);
1266 if (Tmp2.empty()) Tmp2.Add(*I);
1267
1268 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1269 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1270 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001271}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001272
Ted Kremenekf233d482008-02-05 00:26:40 +00001273
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001274/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001275void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1276 NodeTy* Pred,
1277 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001278
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001279 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001280 uint64_t amt;
1281
1282 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001283
Ted Kremenek87e80342008-03-15 03:13:20 +00001284 // FIXME: Add support for VLAs.
1285 if (!T.getTypePtr()->isConstantSizeType())
1286 return;
1287
1288 amt = 1; // Handle sizeof(void)
1289
1290 if (T != getContext().VoidTy)
1291 amt = getContext().getTypeSize(T) / 8;
1292
1293 }
1294 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001295 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001296
Ted Kremenek0e561a32008-03-21 21:30:14 +00001297 MakeNode(Dst, Ex, Pred,
Ted Kremenek0d093d32008-03-10 04:11:42 +00001298 SetRVal(GetState(Pred), Ex,
Ted Kremenek87e80342008-03-15 03:13:20 +00001299 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001300}
1301
Ted Kremenekffe0f432008-03-07 22:58:01 +00001302void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
1303 NodeSet& Dst, bool GetLVal) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001304
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001305 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001306
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001307 NodeSet DstTmp;
1308
Ted Kremenek018c15f2008-02-26 03:44:25 +00001309 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001310 DstTmp.Add(Pred);
1311 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001312 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001313
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001314 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001315 ValueState* St = GetState(Pred);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001316 RVal V = GetRVal(St, Ex);
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001317 VisitDeref(U, V, St, Pred, Dst, GetLVal);
1318 }
1319}
1320
1321void GRExprEngine::VisitDeref(Expr* Ex, RVal V, ValueState* St, NodeTy* Pred,
1322 NodeSet& Dst, bool GetLVal) {
1323
1324 // Check for dereferences of undefined values.
1325
1326 if (V.isUndef()) {
1327 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred)) {
1328 Succ->markAsSink();
1329 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001330 }
1331
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001332 return;
1333 }
1334
1335 // Check for dereferences of unknown values. Treat as No-Ops.
1336
1337 if (V.isUnknown()) {
1338 Dst.Add(Pred);
1339 return;
1340 }
1341
1342 // After a dereference, one of two possible situations arise:
1343 // (1) A crash, because the pointer was NULL.
1344 // (2) The pointer is not NULL, and the dereference works.
1345 //
1346 // We add these assumptions.
1347
1348 LVal LV = cast<LVal>(V);
1349 bool isFeasibleNotNull;
1350
1351 // "Assume" that the pointer is Not-NULL.
1352
1353 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
1354
1355 if (isFeasibleNotNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001356
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001357 if (GetLVal)
1358 MakeNode(Dst, Ex, Pred, SetRVal(StNotNull, Ex, LV));
1359 else {
1360
1361 // FIXME: Currently symbolic analysis "generates" new symbols
1362 // for the contents of values. We need a better approach.
1363
1364 MakeNode(Dst, Ex, Pred,
1365 SetRVal(StNotNull, Ex, GetRVal(StNotNull, LV, Ex->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001366 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001367 }
1368
1369 bool isFeasibleNull;
1370
1371 // Now "assume" that the pointer is NULL.
1372
1373 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
1374
1375 if (isFeasibleNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001376
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001377 // We don't use "MakeNode" here because the node will be a sink
1378 // and we have no intention of processing it later.
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001379
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001380 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001381
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001382 if (NullNode) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001383
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001384 NullNode->markAsSink();
1385
1386 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1387 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001388 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001389 }
1390}
1391
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001392void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
1393 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001394
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001395 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001396
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001397 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001398 assert (U->getOpcode() != UnaryOperator::SizeOf);
1399 assert (U->getOpcode() != UnaryOperator::AlignOf);
1400
1401 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001402
1403 switch (U->getOpcode()) {
1404 case UnaryOperator::PostInc:
1405 case UnaryOperator::PostDec:
1406 case UnaryOperator::PreInc:
1407 case UnaryOperator::PreDec:
1408 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001409 // Evalue subexpression as an LVal.
1410 use_GetLVal = true;
1411 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001412 break;
1413
1414 default:
1415 Visit(U->getSubExpr(), Pred, S1);
1416 break;
1417 }
1418
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001419 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001420
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001421 NodeTy* N1 = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001422 ValueState* St = GetState(N1);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001423
1424 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
1425 GetRVal(St, U->getSubExpr());
1426
1427 if (SubV.isUnknown()) {
1428 Dst.Add(N1);
1429 continue;
1430 }
1431
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001432 if (SubV.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001433 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001434 continue;
1435 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001436
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001437 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001438
1439 // Handle ++ and -- (both pre- and post-increment).
1440
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001441 LVal SubLV = cast<LVal>(SubV);
1442 RVal V = GetRVal(St, SubLV, U->getType());
1443
Ted Kremenek89063af2008-02-21 19:15:37 +00001444 if (V.isUnknown()) {
1445 Dst.Add(N1);
1446 continue;
1447 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001448
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001449 // Propagate undefined values.
1450 if (V.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001451 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001452 continue;
1453 }
1454
Ted Kremenek443003b2008-02-21 19:29:23 +00001455 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001456
1457 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1458 : BinaryOperator::Sub;
1459
Ted Kremenek443003b2008-02-21 19:29:23 +00001460 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001461
1462 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001463 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001464 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001465 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001466
Ted Kremenek0e561a32008-03-21 21:30:14 +00001467 MakeNode(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001468 continue;
1469 }
1470
1471 // Handle all other unary operators.
1472
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001473 switch (U->getOpcode()) {
Ted Kremenek5139c782008-03-15 03:05:30 +00001474
1475 case UnaryOperator::Extension:
1476 St = SetRVal(St, U, SubV);
1477 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001478
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001479 case UnaryOperator::Minus:
1480 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001481 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001482
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001483 case UnaryOperator::Not:
1484 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +00001485 break;
Ted Kremenek90e42032008-02-20 04:12:31 +00001486
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001487 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +00001488
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001489 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1490 //
1491 // Note: technically we do "E == 0", but this is the same in the
1492 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001493
1494 if (isa<LVal>(SubV)) {
Ted Kremenek240f1f02008-03-07 20:13:31 +00001495 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001496 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
1497 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001498 }
1499 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +00001500 Expr* Ex = U->getSubExpr();
Ted Kremenek240f1f02008-03-07 20:13:31 +00001501 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001502 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
1503 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001504 }
1505
1506 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001507
Ted Kremenek64924852008-01-31 02:35:41 +00001508 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001509 assert (isa<LVal>(SubV));
1510 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +00001511 break;
1512 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001513
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001514 default: ;
1515 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001516 }
1517
Ted Kremenek0e561a32008-03-21 21:30:14 +00001518 MakeNode(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001519 }
1520}
1521
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001522void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
1523 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001524
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001525 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001526
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001527 // FIXME: Add support for VLAs.
1528 if (!T.getTypePtr()->isConstantSizeType())
1529 return;
1530
Chris Lattner98be4942008-03-05 18:54:05 +00001531 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001532 ValueState* St = GetState(Pred);
Ted Kremenek240f1f02008-03-07 20:13:31 +00001533 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001534
Ted Kremenek0e561a32008-03-21 21:30:14 +00001535 MakeNode(Dst, U, Pred, St);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001536}
1537
1538void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +00001539
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001540 Ex = Ex->IgnoreParens();
1541
Ted Kremenek2ad88682008-02-27 07:04:16 +00001542 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1543 Dst.Add(Pred);
1544 return;
1545 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001546
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001547 switch (Ex->getStmtClass()) {
1548 default:
1549 break;
1550
Ted Kremenek540cbe22008-04-22 04:56:29 +00001551 case Stmt::ArraySubscriptExprClass:
1552 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
1553 return;
1554
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001555 case Stmt::DeclRefExprClass:
1556 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001557 return;
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001558
1559 case Stmt::UnaryOperatorClass: {
1560 UnaryOperator* U = cast<UnaryOperator>(Ex);
1561
1562 if (U->getOpcode() == UnaryOperator::Deref) {
1563 VisitDeref(U, Pred, Dst, true);
1564 return;
1565 }
1566
1567 break;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001568 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001569
1570 case Stmt::MemberExprClass:
1571 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
1572 return;
1573 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001574
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001575 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001576}
1577
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001578void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1579 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1580}
1581
1582void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1583 AsmStmt::outputs_iterator I,
1584 AsmStmt::outputs_iterator E,
1585 NodeTy* Pred, NodeSet& Dst) {
1586 if (I == E) {
1587 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1588 return;
1589 }
1590
1591 NodeSet Tmp;
1592 VisitLVal(*I, Pred, Tmp);
1593
1594 ++I;
1595
1596 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1597 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1598}
1599
1600void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1601 AsmStmt::inputs_iterator I,
1602 AsmStmt::inputs_iterator E,
1603 NodeTy* Pred, NodeSet& Dst) {
1604 if (I == E) {
1605
1606 // We have processed both the inputs and the outputs. All of the outputs
1607 // should evaluate to LVals. Nuke all of their values.
1608
1609 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1610 // which interprets the inline asm and stores proper results in the
1611 // outputs.
1612
1613 ValueState* St = GetState(Pred);
1614
1615 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1616 OE = A->end_outputs(); OI != OE; ++OI) {
1617
1618 RVal X = GetLVal(St, *OI);
1619
1620 assert (!isa<NonLVal>(X));
1621
1622 if (isa<LVal>(X))
1623 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1624 }
1625
Ted Kremenek0e561a32008-03-21 21:30:14 +00001626 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001627 return;
1628 }
1629
1630 NodeSet Tmp;
1631 Visit(*I, Pred, Tmp);
1632
1633 ++I;
1634
1635 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1636 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1637}
1638
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001639void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1640 assert (Builder && "GRStmtNodeBuilder must be defined.");
1641
1642 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001643
1644 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1645 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001646
Ted Kremenekb0533962008-04-18 20:35:30 +00001647 Builder->HasGeneratedNode = false;
1648
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001649 TF->EvalReturn(Dst, *this, *Builder, S, Pred);
1650
Ted Kremenekb0533962008-04-18 20:35:30 +00001651 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001652
Ted Kremenekb0533962008-04-18 20:35:30 +00001653 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001654 MakeNode(Dst, S, Pred, GetState(Pred));
1655}
1656
Ted Kremenek02737ed2008-03-31 15:02:58 +00001657void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1658
1659 Expr* R = S->getRetValue();
1660
1661 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001662 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001663 return;
1664 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001665
1666 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001667 QualType T = R->getType();
1668
Chris Lattner423a3c92008-04-02 17:45:06 +00001669 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001670
1671 // Check if any of the return values return the address of a stack variable.
1672
1673 NodeSet Tmp;
1674 Visit(R, Pred, Tmp);
1675
1676 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1677 RVal X = GetRVal((*I)->getState(), R);
1678
1679 if (isa<lval::DeclVal>(X)) {
1680
1681 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1682
1683 // Create a special node representing the v
1684
1685 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1686
1687 if (RetStackNode) {
1688 RetStackNode->markAsSink();
1689 RetsStackAddr.insert(RetStackNode);
1690 }
1691
1692 continue;
1693 }
1694 }
1695
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001696 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001697 }
1698 }
1699 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001700 Visit(R, Pred, DstRet);
1701
1702 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1703 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001704}
Ted Kremenek55deb972008-03-25 00:34:37 +00001705
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001706//===----------------------------------------------------------------------===//
1707// Transfer functions: Binary operators.
1708//===----------------------------------------------------------------------===//
1709
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001710void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001711 GRExprEngine::NodeTy* Pred,
1712 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001713 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001714
1715 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001716 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001717 else
1718 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001719
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001720 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001721
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001722 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001723
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001724 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001725 // In such cases, we want to (initially) treat the LHS as an LVal,
1726 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1727 // evaluated to LValDecl's instead of to an NonLVal.
1728
Ted Kremenek0d093d32008-03-10 04:11:42 +00001729 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1730 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001731
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001732 // Visit the RHS...
1733
1734 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001735 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001736
1737 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001738
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001739 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001740
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001741 NodeTy* N2 = *I2;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001742 ValueState* St = GetState(N2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001743 Expr* RHS = B->getRHS();
1744 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001745
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001746 BinaryOperator::Opcode Op = B->getOpcode();
1747
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001748 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1749 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001750
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001751 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001752
Ted Kremenek00155412008-02-26 22:27:51 +00001753 if (!RightV.isUnknown()) {
1754
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001755 if (RightV.isUndef()) {
1756 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001757
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001758 if (DivUndef) {
1759 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001760 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001761 }
1762
1763 continue;
1764 }
1765
1766 // Check for divide/remainder-by-zero.
1767 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001768 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001769
Ted Kremenek4d839b42008-03-07 19:04:53 +00001770 bool isFeasibleZero = false;
1771 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001772
Ted Kremenek00155412008-02-26 22:27:51 +00001773 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001774
Ted Kremenek4d839b42008-03-07 19:04:53 +00001775 bool isFeasibleNotZero = false;
1776 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001777
Ted Kremenek4d839b42008-03-07 19:04:53 +00001778 // Create the node for the divide-by-zero (if it occurred).
1779
1780 if (isFeasibleZero)
1781 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1782 DivZeroNode->markAsSink();
1783
1784 if (isFeasibleNotZero)
1785 ImplicitBadDivides.insert(DivZeroNode);
1786 else
1787 ExplicitBadDivides.insert(DivZeroNode);
1788
1789 }
1790
1791 if (!isFeasibleNotZero)
Ted Kremenek00155412008-02-26 22:27:51 +00001792 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001793 }
1794
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001795 // Fall-through. The logic below processes the divide.
1796 }
1797
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001798
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001799 if (Op <= BinaryOperator::Or) {
1800
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001801 // Process non-assignements except commas or short-circuited
1802 // logical expressions (LAnd and LOr).
1803
1804 RVal Result = EvalBinOp(Op, LeftV, RightV);
1805
1806 if (Result.isUnknown()) {
1807 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001808 continue;
1809 }
1810
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001811 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1812
1813 // The operands were not undefined, but the result is undefined.
1814
1815 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1816 UndefNode->markAsSink();
1817 UndefResults.insert(UndefNode);
1818 }
1819
1820 continue;
1821 }
1822
Ted Kremenek0e561a32008-03-21 21:30:14 +00001823 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001824 continue;
1825 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001826
1827 // Process assignments.
1828
1829 switch (Op) {
1830
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001831 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001832
1833 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001834
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001835 if (LeftV.isUndef()) {
1836 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001837 continue;
1838 }
1839
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001840 // EXPERIMENTAL: "Conjured" symbols.
1841
1842 if (RightV.isUnknown()) {
1843 unsigned Count = Builder->getCurrentBlockCount();
1844 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1845
1846 RightV = B->getRHS()->getType()->isPointerType()
1847 ? cast<RVal>(lval::SymbolVal(Sym))
1848 : cast<RVal>(nonlval::SymbolVal(Sym));
1849 }
1850
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001851 // Simulate the effects of a "store": bind the value of the RHS
1852 // to the L-Value represented by the LHS.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001853
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001854 EvalStore(Dst, B, N2, SetRVal(St, B, RightV),
Ted Kremenek13922612008-04-16 20:40:59 +00001855 LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00001856
Ted Kremeneke38718e2008-04-16 18:21:25 +00001857 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001858 }
1859
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001860 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001861
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001862 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001863
Ted Kremenek00155412008-02-26 22:27:51 +00001864 assert (B->isCompoundAssignmentOp());
1865
1866 if (Op >= BinaryOperator::AndAssign)
1867 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1868 else
1869 ((int&) Op) -= BinaryOperator::MulAssign;
1870
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001871 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001872
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001873 if (LeftV.isUndef()) {
1874 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001875 continue;
1876 }
1877
1878 if (LeftV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001879 assert (isa<UnknownVal>(GetRVal(St, B)));
1880 Dst.Add(N2);
1881 continue;
Ted Kremenek9dca0622008-02-19 00:22:37 +00001882 }
1883
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001884 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek3ce92142008-03-09 08:12:37 +00001885 // that is neither "Unknown" or "Undefined."
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001886
1887 LVal LeftLV = cast<LVal>(LeftV);
1888
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001889 // Fetch the value of the LHS (the value of the variable, etc.).
1890
Ted Kremenek0d093d32008-03-10 04:11:42 +00001891 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001892
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001893 // Propagate undefined value (left-side). We
1894 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001895 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001896
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001897 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001898 St = SetRVal(St, B, V);
1899 break;
1900 }
1901
1902 // Propagate unknown values.
1903
Ted Kremenek89063af2008-02-21 19:15:37 +00001904 if (V.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001905 // The value bound to LeftV is unknown. Thus we just
1906 // propagate the current node (as "B" is already bound to nothing).
1907 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenek89063af2008-02-21 19:15:37 +00001908 Dst.Add(N2);
1909 continue;
1910 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001911
1912 if (RightV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001913 assert (isa<UnknownVal>(GetRVal(St, B)));
1914 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001915 break;
1916 }
1917
Ted Kremenek00155412008-02-26 22:27:51 +00001918 // At this point:
1919 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001920 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001921 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001922
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001923 // Get the computation type.
1924 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1925
1926 // Perform promotions.
1927 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001928 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001929
1930 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001931
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001932 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1933 && RHS->getType()->isIntegerType()) {
1934
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001935 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001936
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001937 if (RightV.isUndef()) {
1938 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001939
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001940 if (DivUndef) {
1941 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001942 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001943 }
1944
1945 continue;
1946 }
Ted Kremenek00155412008-02-26 22:27:51 +00001947
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001948 // First, "assume" that the denominator is 0.
1949
Ted Kremenek4d839b42008-03-07 19:04:53 +00001950 bool isFeasibleZero = false;
1951 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001952
Ted Kremenek4d839b42008-03-07 19:04:53 +00001953 // Second, "assume" that the denominator cannot be 0.
1954
1955 bool isFeasibleNotZero = false;
1956 St = Assume(St, RightV, true, isFeasibleNotZero);
1957
1958 // Create the node for the divide-by-zero error (if it occurred).
1959
1960 if (isFeasibleZero) {
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001961 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1962
1963 if (DivZeroNode) {
1964 DivZeroNode->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001965
1966 if (isFeasibleNotZero)
1967 ImplicitBadDivides.insert(DivZeroNode);
1968 else
1969 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001970 }
1971 }
1972
Ted Kremenek4d839b42008-03-07 19:04:53 +00001973 if (!isFeasibleNotZero)
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001974 continue;
1975
1976 // Fall-through. The logic below processes the divide.
1977 }
Ted Kremenek00155412008-02-26 22:27:51 +00001978 else {
1979
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001980 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001981
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001982 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001983 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1984 break;
1985 }
1986
1987 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001988
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001989 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001990
1991 if (Result.isUndef()) {
1992
1993 // The operands were not undefined, but the result is undefined.
1994
1995 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1996 UndefNode->markAsSink();
1997 UndefResults.insert(UndefNode);
1998 }
1999
2000 continue;
2001 }
2002
Ted Kremeneke38718e2008-04-16 18:21:25 +00002003 // St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002004 EvalStore(Dst, B, N2, SetRVal(St, B, Result), LeftLV, Result);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002005 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002006 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002007 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002008
Ted Kremenek0e561a32008-03-21 21:30:14 +00002009 MakeNode(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002010 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002011 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002012}
Ted Kremenekee985462008-01-16 18:18:48 +00002013
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002014void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00002015 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenek9dca0622008-02-19 00:22:37 +00002016 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002017 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00002018}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00002019
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00002020
Ted Kremenekee985462008-01-16 18:18:48 +00002021//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00002022// "Assume" logic.
2023//===----------------------------------------------------------------------===//
2024
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002025ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002026 bool Assumption, bool& isFeasible) {
2027
2028 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002029
2030 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2031 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002032}
2033
2034ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond,
2035 bool Assumption, bool& isFeasible) {
2036
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002037 switch (Cond.getSubKind()) {
2038 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002039 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002040 return St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002041
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002042 case lval::SymbolValKind:
2043 if (Assumption)
2044 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002045 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002046 else
2047 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002048 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek550a0f92008-04-18 17:20:23 +00002049
2050
Ted Kremenek329f8542008-02-05 21:52:21 +00002051 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00002052 case lval::FuncValKind:
2053 case lval::GotoLabelKind:
Ted Kremeneka5488462008-04-22 21:39:21 +00002054 case lval::StringLiteralValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002055 isFeasible = Assumption;
2056 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002057
Ted Kremenek329f8542008-02-05 21:52:21 +00002058 case lval::ConcreteIntKind: {
2059 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002060 isFeasible = b ? Assumption : !Assumption;
2061 return St;
2062 }
2063 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002064}
2065
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002066ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002067 bool Assumption, bool& isFeasible) {
2068
2069 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002070
2071 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2072 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002073}
2074
2075ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond,
2076 bool Assumption, bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002077 switch (Cond.getSubKind()) {
2078 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002079 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00002080 return St;
2081
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002082
2083 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00002084 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002085 SymbolID sym = SV.getSymbol();
2086
2087 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00002088 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002089 isFeasible);
2090 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00002091 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002092 isFeasible);
2093 }
2094
Ted Kremenek08b66252008-02-06 04:31:33 +00002095 case nonlval::SymIntConstraintValKind:
2096 return
2097 AssumeSymInt(St, Assumption,
2098 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
2099 isFeasible);
2100
Ted Kremenek329f8542008-02-05 21:52:21 +00002101 case nonlval::ConcreteIntKind: {
2102 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00002103 isFeasible = b ? Assumption : !Assumption;
2104 return St;
2105 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002106
2107 case nonlval::LValAsIntegerKind: {
2108 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
2109 Assumption, isFeasible);
2110 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002111 }
2112}
2113
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002114ValueState*
2115GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002116 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00002117
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002118 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002119 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002120 isFeasible = *X != V;
2121 return St;
2122 }
2123
2124 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002125 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002126 isFeasible = true;
2127 return St;
2128 }
2129
2130 // If we reach here, sym is not a constant and we don't know if it is != V.
2131 // Make that assumption.
2132
2133 isFeasible = true;
2134 return StateMgr.AddNE(St, sym, V);
2135}
2136
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002137ValueState*
2138GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002139 const llvm::APSInt& V, bool& isFeasible) {
2140
2141 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002142 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002143 isFeasible = *X == V;
2144 return St;
2145 }
2146
2147 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002148 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002149 isFeasible = false;
2150 return St;
2151 }
2152
2153 // If we reach here, sym is not a constant and we don't know if it is == V.
2154 // Make that assumption.
2155
2156 isFeasible = true;
2157 return StateMgr.AddEQ(St, sym, V);
2158}
Ted Kremenekb38911f2008-01-30 23:03:39 +00002159
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002160ValueState*
2161GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00002162 const SymIntConstraint& C, bool& isFeasible) {
2163
2164 switch (C.getOpcode()) {
2165 default:
2166 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002167 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00002168 return St;
2169
2170 case BinaryOperator::EQ:
2171 if (Assumption)
2172 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2173 else
2174 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2175
2176 case BinaryOperator::NE:
2177 if (Assumption)
2178 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2179 else
2180 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2181 }
2182}
2183
Ted Kremenekb38911f2008-01-30 23:03:39 +00002184//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002185// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002186//===----------------------------------------------------------------------===//
2187
Ted Kremenekaa66a322008-01-16 21:46:15 +00002188#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002189static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002190static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002191static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002192
Ted Kremenekaa66a322008-01-16 21:46:15 +00002193namespace llvm {
2194template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002195struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002196 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002197
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002198 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002199
2200 Out << "Variables:\\l";
2201
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002202 bool isFirst = true;
2203
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002204 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002205
2206 if (isFirst)
2207 isFirst = false;
2208 else
2209 Out << "\\l";
2210
2211 Out << ' ' << I.getKey()->getName() << " : ";
2212 I.getData().print(Out);
2213 }
2214
2215 }
2216
Ted Kremeneke7d22112008-02-11 19:21:59 +00002217
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002218 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002219
2220 bool isFirst = true;
2221
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002222 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002223
2224 if (isFirst) {
2225 Out << "\\l\\lSub-Expressions:\\l";
2226 isFirst = false;
2227 }
2228 else
2229 Out << "\\l";
2230
2231 Out << " (" << (void*) I.getKey() << ") ";
2232 I.getKey()->printPretty(Out);
2233 Out << " : ";
2234 I.getData().print(Out);
2235 }
2236 }
2237
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002238 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002239
Ted Kremenek016f52f2008-02-08 21:10:02 +00002240 bool isFirst = true;
2241
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002242 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002243 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002244 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002245 isFirst = false;
2246 }
2247 else
2248 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002249
Ted Kremeneke7d22112008-02-11 19:21:59 +00002250 Out << " (" << (void*) I.getKey() << ") ";
2251 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002252 Out << " : ";
2253 I.getData().print(Out);
2254 }
2255 }
2256
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002257 static void PrintEQ(std::ostream& Out, ValueState* St) {
2258 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002259
2260 if (CE.isEmpty())
2261 return;
2262
2263 Out << "\\l\\|'==' constraints:";
2264
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002265 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002266 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2267 }
2268
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002269 static void PrintNE(std::ostream& Out, ValueState* St) {
2270 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002271
2272 if (NE.isEmpty())
2273 return;
2274
2275 Out << "\\l\\|'!=' constraints:";
2276
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002277 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002278 I != EI; ++I){
2279
2280 Out << "\\l $" << I.getKey() << " : ";
2281 bool isFirst = true;
2282
2283 ValueState::IntSetTy::iterator J=I.getData().begin(),
2284 EJ=I.getData().end();
2285 for ( ; J != EJ; ++J) {
2286 if (isFirst) isFirst = false;
2287 else Out << ", ";
2288
2289 Out << (*J)->toString();
2290 }
2291 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002292 }
2293
2294 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2295
2296 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002297 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002298 GraphPrintCheckerState->isUndefDeref(N) ||
2299 GraphPrintCheckerState->isUndefStore(N) ||
2300 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002301 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2302 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002303 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002304 GraphPrintCheckerState->isBadCall(N) ||
2305 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002306 return "color=\"red\",style=\"filled\"";
2307
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002308 if (GraphPrintCheckerState->isNoReturnCall(N))
2309 return "color=\"blue\",style=\"filled\"";
2310
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002311 return "";
2312 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002313
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002314 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002315 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002316
2317 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002318 ProgramPoint Loc = N->getLocation();
2319
2320 switch (Loc.getKind()) {
2321 case ProgramPoint::BlockEntranceKind:
2322 Out << "Block Entrance: B"
2323 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2324 break;
2325
2326 case ProgramPoint::BlockExitKind:
2327 assert (false);
2328 break;
2329
2330 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002331 const PostStmt& L = cast<PostStmt>(Loc);
2332 Stmt* S = L.getStmt();
2333 SourceLocation SLoc = S->getLocStart();
2334
2335 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2336 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002337
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002338 if (SLoc.isFileID()) {
2339 Out << "\\lline="
2340 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2341 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2342 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002343
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002344 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002345 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002346 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002347 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002348 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002349 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002350 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002351 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002352 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2353 Out << "\\|Explicit divide-by zero or undefined value.";
2354 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2355 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002356 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002357 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002358 else if (GraphPrintCheckerState->isNoReturnCall(N))
2359 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002360 else if (GraphPrintCheckerState->isBadCall(N))
2361 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002362 else if (GraphPrintCheckerState->isUndefArg(N))
2363 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002364
Ted Kremenekaa66a322008-01-16 21:46:15 +00002365 break;
2366 }
2367
2368 default: {
2369 const BlockEdge& E = cast<BlockEdge>(Loc);
2370 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2371 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002372
2373 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002374
2375 SourceLocation SLoc = T->getLocStart();
2376
Ted Kremenekb38911f2008-01-30 23:03:39 +00002377 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002378
Ted Kremenekb38911f2008-01-30 23:03:39 +00002379 E.getSrc()->printTerminator(Out);
2380
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002381 if (SLoc.isFileID()) {
2382 Out << "\\lline="
2383 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2384 << GraphPrintSourceManager->getColumnNumber(SLoc);
2385 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002386
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002387 if (isa<SwitchStmt>(T)) {
2388 Stmt* Label = E.getDst()->getLabel();
2389
2390 if (Label) {
2391 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2392 Out << "\\lcase ";
2393 C->getLHS()->printPretty(Out);
2394
2395 if (Stmt* RHS = C->getRHS()) {
2396 Out << " .. ";
2397 RHS->printPretty(Out);
2398 }
2399
2400 Out << ":";
2401 }
2402 else {
2403 assert (isa<DefaultStmt>(Label));
2404 Out << "\\ldefault:";
2405 }
2406 }
2407 else
2408 Out << "\\l(implicit) default:";
2409 }
2410 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002411 // FIXME
2412 }
2413 else {
2414 Out << "\\lCondition: ";
2415 if (*E.getSrc()->succ_begin() == E.getDst())
2416 Out << "true";
2417 else
2418 Out << "false";
2419 }
2420
2421 Out << "\\l";
2422 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002423
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002424 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2425 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002426 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002427 }
2428 }
2429
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002430 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002431
Ted Kremenek75da3e82008-03-11 19:02:40 +00002432 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002433
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002434 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002435 return Out.str();
2436 }
2437};
2438} // end llvm namespace
2439#endif
2440
Ted Kremenekffe0f432008-03-07 22:58:01 +00002441#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002442
2443template <typename ITERATOR>
2444GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2445
2446template <>
2447GRExprEngine::NodeTy*
2448GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2449 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2450 return I->first;
2451}
2452
Ted Kremenekffe0f432008-03-07 22:58:01 +00002453template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002454static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002455 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002456
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002457 llvm::SmallPtrSet<void*,10> CachedSources;
2458
2459 for ( ; I != E; ++I ) {
2460 GRExprEngine::NodeTy* N = GetGraphNode(I);
2461 void* p = N->getLocation().getRawData();
2462
2463 if (CachedSources.count(p))
2464 continue;
2465
2466 CachedSources.insert(p);
2467
2468 Sources.push_back(N);
2469 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002470}
2471#endif
2472
2473void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002474#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002475 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002476 std::vector<NodeTy*> Src;
2477
2478 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002479 AddSources(Src, null_derefs_begin(), null_derefs_end());
2480 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2481 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2482 AddSources(Src, undef_results_begin(), undef_results_end());
2483 AddSources(Src, bad_calls_begin(), bad_calls_end());
2484 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002485 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002486
Ted Kremenekcb612922008-04-18 19:23:43 +00002487 // The new way.
2488 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2489 (*I)->GetErrorNodes(Src);
2490
2491
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002492 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002493 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002494 else {
2495 GraphPrintCheckerState = this;
2496 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002497 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002498
Ted Kremenekffe0f432008-03-07 22:58:01 +00002499 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002500
2501 GraphPrintCheckerState = NULL;
2502 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002503 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002504 }
2505#endif
2506}
2507
2508void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2509#ifndef NDEBUG
2510 GraphPrintCheckerState = this;
2511 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002512 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002513
Ted Kremenek493d7a22008-03-11 18:25:33 +00002514 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2515
2516 if (!TrimmedG)
2517 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2518 else {
2519 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2520 delete TrimmedG;
2521 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002522
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002523 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002524 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002525 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002526#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002527}