blob: d5b2ce8b358592b83085a3a55f1e6741cd85f538 [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;
565
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000566 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000567
568 CaseStmt* Case = cast<CaseStmt>(I.getCase());
569
570 // Evaluate the case.
571 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
572 assert (false && "Case condition must evaluate to an integer constant.");
573 return;
574 }
575
576 // Get the RHS of the case, if it exists.
577
578 if (Expr* E = Case->getRHS()) {
579 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
580 assert (false &&
581 "Case condition (RHS) must evaluate to an integer constant.");
582 return ;
583 }
584
585 assert (V1 <= V2);
586 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000587 else
588 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000589
590 // FIXME: Eventually we should replace the logic below with a range
591 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000592 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000593
Ted Kremenek14a11402008-03-17 22:17:56 +0000594 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000595 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000596
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000597 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000598
599 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000600
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000601 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000602 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000603
604 if (isFeasible) {
605 builder.generateCaseStmtNode(I, StNew);
606
607 // If CondV evaluates to a constant, then we know that this
608 // is the *only* case that we can take, so stop evaluating the
609 // others.
610 if (isa<nonlval::ConcreteInt>(CondV))
611 return;
612 }
613
614 // Now "assume" that the case doesn't match. Add this state
615 // to the default state (if it is feasible).
616
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000617 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000618 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000619
620 if (isFeasible)
621 DefaultSt = StNew;
622
Ted Kremenek14a11402008-03-17 22:17:56 +0000623 // Concretize the next value in the range.
624 if (V1 == V2)
625 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000626
Ted Kremenek14a11402008-03-17 22:17:56 +0000627 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000628 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000629
630 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000631 }
632
633 // If we reach here, than we know that the default branch is
634 // possible.
635 builder.generateDefaultCaseNode(DefaultSt);
636}
637
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000638//===----------------------------------------------------------------------===//
639// Transfer functions: logical operations ('&&', '||').
640//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000641
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000642void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000643 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000644
Ted Kremenek05a23782008-02-26 19:05:15 +0000645 assert (B->getOpcode() == BinaryOperator::LAnd ||
646 B->getOpcode() == BinaryOperator::LOr);
647
648 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
649
Ted Kremenek0d093d32008-03-10 04:11:42 +0000650 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000651 RVal X = GetBlkExprRVal(St, B);
652
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000653 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000654
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000655 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000656
657 assert (Ex);
658
659 if (Ex == B->getRHS()) {
660
661 X = GetBlkExprRVal(St, Ex);
662
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000663 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000664
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000665 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000666 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000667 return;
668 }
669
Ted Kremenek05a23782008-02-26 19:05:15 +0000670 // We took the RHS. Because the value of the '&&' or '||' expression must
671 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
672 // or 1. Alternatively, we could take a lazy approach, and calculate this
673 // value later when necessary. We don't have the machinery in place for
674 // this right now, and since most logical expressions are used for branches,
675 // the payoff is not likely to be large. Instead, we do eager evaluation.
676
677 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000678 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000679
680 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000681 MakeNode(Dst, B, Pred,
682 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000683
684 isFeasible = false;
685 NewState = Assume(St, X, false, isFeasible);
686
687 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000688 MakeNode(Dst, B, Pred,
689 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000690 }
691 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000692 // We took the LHS expression. Depending on whether we are '&&' or
693 // '||' we know what the value of the expression is via properties of
694 // the short-circuiting.
695
696 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000697 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000698 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000699}
Ted Kremenek05a23782008-02-26 19:05:15 +0000700
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000701//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000702// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000703//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000704
Ted Kremenek44842c22008-02-13 18:06:44 +0000705void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000707 if (D != CurrentStmt) {
708 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
709 return;
710 }
711
712 // If we are here, we are loading the value of the decl and binding
713 // it to the block-level expression.
714
Ted Kremenek0d093d32008-03-10 04:11:42 +0000715 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000716 RVal X = RVal::MakeVal(BasicVals, D);
717 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenek0e561a32008-03-21 21:30:14 +0000718 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000719}
720
Ted Kremenek540cbe22008-04-22 04:56:29 +0000721/// VisitArraySubscriptExpr - Transfer function for array accesses
722void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
723 NodeSet& Dst, bool asLVal) {
724
725 Expr* Base = A->getBase()->IgnoreParens();
726
727 // Evaluate the base.
728 NodeSet Tmp1;
729 Visit(Base, Pred, Tmp1);
730
731 // Dereference the base.
732 NodeSet Tmp2;
733
734 for (NodeSet::iterator I=Tmp1.begin(), E=Tmp1.end(); I!=E; ++I) {
735 ValueState* St = GetState(*I);
736 VisitDeref(Base, GetRVal(St, Base), St, *I, Tmp2, true);
737 }
738
739 // Get the index.
740 Tmp1.clear();
741 Expr* Index = A->getIdx()->IgnoreParens();
742
743 for (NodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I)
744 Visit(Index, *I, Dst);
745}
746
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000747/// VisitMemberExpr - Transfer function for member expressions.
748void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
749 NodeSet& Dst, bool asLVal) {
750
751 Expr* Base = M->getBase()->IgnoreParens();
752
753 NodeSet Tmp;
754 VisitLVal(Base, Pred, Tmp);
755
756 if (Base->getType()->isPointerType()) {
757 NodeSet Tmp2;
758
759 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
760 ValueState* St = GetState(*I);
761 VisitDeref(Base, GetRVal(St, Base), St, *I, Tmp2, true);
762 }
763
764 for (NodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I)
765 VisitMemberExprField(M, Base, *I, Dst, asLVal);
766 }
767 else
768 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I)
769 VisitMemberExprField(M, Base, *I, Dst, asLVal);
770}
771
772void GRExprEngine::VisitMemberExprField(MemberExpr* M, Expr* Base, NodeTy* Pred,
773 NodeSet& Dst, bool asLVal) {
774 Dst.Add(Pred);
775}
776
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000777void GRExprEngine::EvalStore(NodeSet& Dst, Expr* E, NodeTy* Pred,
778 ValueState* St, RVal TargetLV, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000779
780 assert (Builder && "GRStmtNodeBuilder must be defined.");
781
782 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000783
784 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
785 OldHasGen(Builder->HasGeneratedNode);
786
787 Builder->HasGeneratedNode = false;
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000788
Ted Kremenek13922612008-04-16 20:40:59 +0000789 assert (!TargetLV.isUndef());
790
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000791 TF->EvalStore(Dst, *this, *Builder, E, Pred, St, TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000792
793 // Handle the case where no nodes where generated. Auto-generate that
794 // contains the updated state if we aren't generating sinks.
795
Ted Kremenekb0533962008-04-18 20:35:30 +0000796 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek13922612008-04-16 20:40:59 +0000797 TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, E, Pred, St,
798 TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000799}
800
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000801//===----------------------------------------------------------------------===//
802// Transfer function: Function calls.
803//===----------------------------------------------------------------------===//
804
Ted Kremenekde434242008-02-19 01:44:53 +0000805void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000806 CallExpr::arg_iterator AI,
807 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000808 NodeSet& Dst) {
809
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000810 // Process the arguments.
811
812 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000813
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000814 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000815 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000816 ++AI;
817
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000818 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000819 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000820
821 return;
822 }
823
824 // If we reach here we have processed all of the arguments. Evaluate
825 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000826
Ted Kremenek994a09b2008-02-25 21:16:03 +0000827 NodeSet DstTmp;
828 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000829
Ted Kremenek994a09b2008-02-25 21:16:03 +0000830 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000831
832 if (DstTmp.empty())
833 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000834
835 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000836 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
837
Ted Kremenek0d093d32008-03-10 04:11:42 +0000838 ValueState* St = GetState(*DI);
Ted Kremenek994a09b2008-02-25 21:16:03 +0000839 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000840
Ted Kremeneka1354a52008-03-03 16:47:31 +0000841 // FIXME: Add support for symbolic function calls (calls involving
842 // function pointer values that are symbolic).
843
844 // Check for undefined control-flow or calls to NULL.
845
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000846 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000847 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000848
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000849 if (N) {
850 N->markAsSink();
851 BadCalls.insert(N);
852 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000853
Ted Kremenekde434242008-02-19 01:44:53 +0000854 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000855 }
856
857 // Check for the "noreturn" attribute.
858
859 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
860
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000861 if (isa<lval::FuncVal>(L)) {
862
863 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
864
865 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000866 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000867 else {
868 // HACK: Some functions are not marked noreturn, and don't return.
869 // Here are a few hardwired ones. If this takes too long, we can
870 // potentially cache these results.
871 const char* s = FD->getIdentifier()->getName();
872 unsigned n = strlen(s);
873
874 switch (n) {
875 default:
876 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000877
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000878 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000879 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
880 break;
881
882 case 5:
883 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
884 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +0000885
886 case 6:
887 if (!memcmp(s, "Assert", 6)) Builder->BuildSinks = true;
888 break;
Ted Kremenek688738f2008-04-23 00:41:25 +0000889
890 case 7:
891 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
892 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +0000893
894 case 14:
895 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
896 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000897 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +0000898
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000899 }
900 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000901
902 // Evaluate the call.
903
Ted Kremeneka1354a52008-03-03 16:47:31 +0000904
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000905 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000906
Ted Kremenek03da0d72008-02-21 19:46:04 +0000907 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000908 // Check for an "unknown" callee.
909 invalidateArgs = true;
910 }
911 else if (isa<lval::FuncVal>(L)) {
912
913 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
914
Ted Kremenek55aea312008-03-05 22:59:42 +0000915 if (unsigned id = Info->getBuiltinID()) {
916 switch (id) {
917 case Builtin::BI__builtin_expect: {
918 // For __builtin_expect, just return the value of the subexpression.
919 assert (CE->arg_begin() != CE->arg_end());
920 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +0000921 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +0000922 continue;
923 }
924
925 default:
926 invalidateArgs = true;
927 break;
928 }
929 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000930 }
931
932 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000933 // Invalidate all arguments passed in by reference (LVals).
934 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
935 I != E; ++I) {
936 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000937
Ted Kremenek03da0d72008-02-21 19:46:04 +0000938 if (isa<LVal>(V))
939 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000940 }
941
Ted Kremenek0e561a32008-03-21 21:30:14 +0000942 MakeNode(Dst, CE, *DI, St);
Ted Kremenek03da0d72008-02-21 19:46:04 +0000943 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000944 else {
945
946 // Check any arguments passed-by-value against being undefined.
947
948 bool badArg = false;
949
950 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
951 I != E; ++I) {
952
Ted Kremenek0d093d32008-03-10 04:11:42 +0000953 if (GetRVal(GetState(*DI), *I).isUndef()) {
954 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000955
956 if (N) {
957 N->markAsSink();
958 UndefArgs[N] = *I;
959 }
960
961 badArg = true;
962 break;
963 }
964 }
965
966 if (badArg)
967 continue;
968
969 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000970
971 unsigned size = Dst.size();
972
Ted Kremenekb0533962008-04-18 20:35:30 +0000973 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
974 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek940b1d82008-04-10 23:44:06 +0000975
Ted Kremenekb0533962008-04-18 20:35:30 +0000976 Builder->HasGeneratedNode = false;
977
Ted Kremenek330dddd2008-03-05 00:33:14 +0000978 EvalCall(Dst, CE, cast<LVal>(L), *DI);
979
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000980 // Handle the case where no nodes where generated. Auto-generate that
981 // contains the updated state if we aren't generating sinks.
982
Ted Kremenekb0533962008-04-18 20:35:30 +0000983 if (!Builder->BuildSinks && Dst.size() == size &&
984 !Builder->HasGeneratedNode)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000985 MakeNode(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000986 }
Ted Kremenekde434242008-02-19 01:44:53 +0000987 }
988}
989
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000990//===----------------------------------------------------------------------===//
991// Transfer function: Objective-C message expressions.
992//===----------------------------------------------------------------------===//
993
994void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
995 NodeSet& Dst){
996
997 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
998 Pred, Dst);
999}
1000
1001void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1002 ObjCMessageExpr::arg_iterator AI,
1003 ObjCMessageExpr::arg_iterator AE,
1004 NodeTy* Pred, NodeSet& Dst) {
1005 if (AI == AE) {
1006
1007 // Process the receiver.
1008
1009 if (Expr* Receiver = ME->getReceiver()) {
1010 NodeSet Tmp;
1011 Visit(Receiver, Pred, Tmp);
1012
1013 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1014 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1015
1016 return;
1017 }
1018
1019 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1020 return;
1021 }
1022
1023 NodeSet Tmp;
1024 Visit(*AI, Pred, Tmp);
1025
1026 ++AI;
1027
1028 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1029 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1030}
1031
1032void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1033 NodeTy* Pred,
1034 NodeSet& Dst) {
1035
1036 // FIXME: More logic for the processing the method call.
1037
1038 ValueState* St = GetState(Pred);
1039
1040 if (Expr* Receiver = ME->getReceiver()) {
1041
1042 RVal L = GetRVal(St, Receiver);
1043
1044 // Check for undefined control-flow or calls to NULL.
1045
1046 if (L.isUndef()) {
1047 NodeTy* N = Builder->generateNode(ME, St, Pred);
1048
1049 if (N) {
1050 N->markAsSink();
1051 UndefReceivers.insert(N);
1052 }
1053
1054 return;
1055 }
1056 }
1057
1058 // Check for any arguments that are uninitialized/undefined.
1059
1060 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1061 I != E; ++I) {
1062
1063 if (GetRVal(St, *I).isUndef()) {
1064
1065 // Generate an error node for passing an uninitialized/undefined value
1066 // as an argument to a message expression. This node is a sink.
1067 NodeTy* N = Builder->generateNode(ME, St, Pred);
1068
1069 if (N) {
1070 N->markAsSink();
1071 MsgExprUndefArgs[N] = *I;
1072 }
1073
1074 return;
1075 }
1076 }
1077 // Dispatch to plug-in transfer function.
1078
1079 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001080
1081 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1082 OldHasGen(Builder->HasGeneratedNode);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001083
Ted Kremenekb0533962008-04-18 20:35:30 +00001084 Builder->HasGeneratedNode = false;
1085
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001086 EvalObjCMessageExpr(Dst, ME, Pred);
1087
1088 // Handle the case where no nodes where generated. Auto-generate that
1089 // contains the updated state if we aren't generating sinks.
1090
Ted Kremenekb0533962008-04-18 20:35:30 +00001091 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001092 MakeNode(Dst, ME, Pred, St);
1093}
1094
1095//===----------------------------------------------------------------------===//
1096// Transfer functions: Miscellaneous statements.
1097//===----------------------------------------------------------------------===//
1098
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001099void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001100
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001101 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001102 QualType T = CastE->getType();
1103
Ted Kremenek65cfb732008-03-04 22:16:08 +00001104 if (T->isReferenceType())
1105 VisitLVal(Ex, Pred, S1);
1106 else
1107 Visit(Ex, Pred, S1);
1108
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001109 // Check for casting to "void".
1110 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001111
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001112 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001113 Dst.Add(*I1);
1114
Ted Kremenek874d63f2008-01-24 02:02:54 +00001115 return;
1116 }
1117
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001118 // FIXME: The rest of this should probably just go into EvalCall, and
1119 // let the transfer function object be responsible for constructing
1120 // nodes.
1121
1122 QualType ExTy = Ex->getType();
1123
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001124 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001125 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001126 ValueState* St = GetState(N);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001127 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001128
1129 // Unknown?
1130
1131 if (V.isUnknown()) {
1132 Dst.Add(N);
1133 continue;
1134 }
1135
1136 // Undefined?
1137
1138 if (V.isUndef()) {
1139 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1140 continue;
1141 }
1142
1143 // Check for casts from pointers to integers.
1144 if (T->isIntegerType() && ExTy->isPointerType()) {
1145 unsigned bits = getContext().getTypeSize(ExTy);
1146
1147 // FIXME: Determine if the number of bits of the target type is
1148 // equal or exceeds the number of bits to store the pointer value.
1149 // If not, flag an error.
1150
1151 V = nonlval::LValAsInteger::Make(BasicVals, cast<LVal>(V), bits);
1152 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1153 continue;
1154 }
1155
1156 // Check for casts from integers to pointers.
1157 if (T->isPointerType() && ExTy->isIntegerType())
1158 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1159 // Just unpackage the lval and return it.
1160 V = LV->getLVal();
1161 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1162 continue;
1163 }
1164
1165 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001166
Ted Kremenek0e561a32008-03-21 21:30:14 +00001167 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001168 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001169}
1170
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001171void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1172 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1173}
1174
1175void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1176 NodeTy* Pred, NodeSet& Dst) {
1177
1178 if (!D)
1179 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001180
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001181 if (!isa<VarDecl>(D)) {
1182 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1183 return;
1184 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001185
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001186 const VarDecl* VD = dyn_cast<VarDecl>(D);
1187
1188 // FIXME: Add support for local arrays.
1189 if (VD->getType()->isArrayType()) {
1190 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1191 return;
1192 }
1193
1194 Expr* Ex = const_cast<Expr*>(VD->getInit());
1195
1196 // FIXME: static variables may have an initializer, but the second
1197 // time a function is called those values may not be current.
1198 NodeSet Tmp;
1199
1200 if (Ex) Visit(Ex, Pred, Tmp);
1201 if (Tmp.empty()) Tmp.Add(Pred);
1202
1203 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001204
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001205 ValueState* St = GetState(*I);
1206
1207 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001208
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001209 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001210
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001211 // FIXME: static variables may have an initializer, but the second
1212 // time a function is called those values may not be current.
1213
1214
1215 // In this context, Static => Local variable.
1216
1217 assert (!VD->getStorageClass() == VarDecl::Static ||
1218 !VD->isFileVarDecl());
1219
1220 // If there is no initializer, set the value of the
1221 // variable to "Undefined".
1222
1223 if (VD->getStorageClass() == VarDecl::Static) {
1224
1225 // C99: 6.7.8 Initialization
1226 // If an object that has static storage duration is not initialized
1227 // explicitly, then:
1228 // —if it has pointer type, it is initialized to a null pointer;
1229 // —if it has arithmetic type, it is initialized to (positive or
1230 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001231
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001232 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001233
1234 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001235
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001236 if (T->isPointerType())
1237 St = SetRVal(St, lval::DeclVal(VD),
1238 lval::ConcreteInt(BasicVals.getValue(0, T)));
1239 else if (T->isIntegerType())
1240 St = SetRVal(St, lval::DeclVal(VD),
1241 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001242
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001243 // FIXME: Handle structs. Now we treat them as unknown. What
1244 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001245 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001246 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001247 else {
1248
1249 // FIXME: Handle structs. Now we treat them as unknown. What
1250 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001251
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001252 QualType T = VD->getType();
1253
1254 if (T->isPointerType() || T->isIntegerType())
1255 St = SetRVal(St, lval::DeclVal(VD),
1256 Ex ? GetRVal(St, Ex) : UndefinedVal());
1257 }
1258
1259 // Create a new node. We don't really need to create a new NodeSet
1260 // here, but it simplifies things and doesn't cost much.
1261 NodeSet Tmp2;
1262 MakeNode(Tmp2, DS, *I, St);
1263 if (Tmp2.empty()) Tmp2.Add(*I);
1264
1265 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1266 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1267 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001268}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001269
Ted Kremenekf233d482008-02-05 00:26:40 +00001270
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001271/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001272void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1273 NodeTy* Pred,
1274 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001275
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001276 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001277 uint64_t amt;
1278
1279 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001280
Ted Kremenek87e80342008-03-15 03:13:20 +00001281 // FIXME: Add support for VLAs.
1282 if (!T.getTypePtr()->isConstantSizeType())
1283 return;
1284
1285 amt = 1; // Handle sizeof(void)
1286
1287 if (T != getContext().VoidTy)
1288 amt = getContext().getTypeSize(T) / 8;
1289
1290 }
1291 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001292 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001293
Ted Kremenek0e561a32008-03-21 21:30:14 +00001294 MakeNode(Dst, Ex, Pred,
Ted Kremenek0d093d32008-03-10 04:11:42 +00001295 SetRVal(GetState(Pred), Ex,
Ted Kremenek87e80342008-03-15 03:13:20 +00001296 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001297}
1298
Ted Kremenekffe0f432008-03-07 22:58:01 +00001299void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
1300 NodeSet& Dst, bool GetLVal) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001301
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001302 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001303
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001304 NodeSet DstTmp;
1305
Ted Kremenek018c15f2008-02-26 03:44:25 +00001306 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001307 DstTmp.Add(Pred);
1308 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001309 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001310
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001311 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001312 ValueState* St = GetState(Pred);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001313 RVal V = GetRVal(St, Ex);
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001314 VisitDeref(U, V, St, Pred, Dst, GetLVal);
1315 }
1316}
1317
1318void GRExprEngine::VisitDeref(Expr* Ex, RVal V, ValueState* St, NodeTy* Pred,
1319 NodeSet& Dst, bool GetLVal) {
1320
1321 // Check for dereferences of undefined values.
1322
1323 if (V.isUndef()) {
1324 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred)) {
1325 Succ->markAsSink();
1326 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001327 }
1328
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001329 return;
1330 }
1331
1332 // Check for dereferences of unknown values. Treat as No-Ops.
1333
1334 if (V.isUnknown()) {
1335 Dst.Add(Pred);
1336 return;
1337 }
1338
1339 // After a dereference, one of two possible situations arise:
1340 // (1) A crash, because the pointer was NULL.
1341 // (2) The pointer is not NULL, and the dereference works.
1342 //
1343 // We add these assumptions.
1344
1345 LVal LV = cast<LVal>(V);
1346 bool isFeasibleNotNull;
1347
1348 // "Assume" that the pointer is Not-NULL.
1349
1350 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
1351
1352 if (isFeasibleNotNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001353
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001354 if (GetLVal)
1355 MakeNode(Dst, Ex, Pred, SetRVal(StNotNull, Ex, LV));
1356 else {
1357
1358 // FIXME: Currently symbolic analysis "generates" new symbols
1359 // for the contents of values. We need a better approach.
1360
1361 MakeNode(Dst, Ex, Pred,
1362 SetRVal(StNotNull, Ex, GetRVal(StNotNull, LV, Ex->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001363 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001364 }
1365
1366 bool isFeasibleNull;
1367
1368 // Now "assume" that the pointer is NULL.
1369
1370 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
1371
1372 if (isFeasibleNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001373
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001374 // We don't use "MakeNode" here because the node will be a sink
1375 // and we have no intention of processing it later.
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001376
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001377 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001378
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001379 if (NullNode) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001380
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001381 NullNode->markAsSink();
1382
1383 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1384 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001385 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001386 }
1387}
1388
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001389void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
1390 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001391
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001392 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001393
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001394 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001395 assert (U->getOpcode() != UnaryOperator::SizeOf);
1396 assert (U->getOpcode() != UnaryOperator::AlignOf);
1397
1398 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001399
1400 switch (U->getOpcode()) {
1401 case UnaryOperator::PostInc:
1402 case UnaryOperator::PostDec:
1403 case UnaryOperator::PreInc:
1404 case UnaryOperator::PreDec:
1405 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001406 // Evalue subexpression as an LVal.
1407 use_GetLVal = true;
1408 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001409 break;
1410
1411 default:
1412 Visit(U->getSubExpr(), Pred, S1);
1413 break;
1414 }
1415
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001416 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001417
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001418 NodeTy* N1 = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001419 ValueState* St = GetState(N1);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001420
1421 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
1422 GetRVal(St, U->getSubExpr());
1423
1424 if (SubV.isUnknown()) {
1425 Dst.Add(N1);
1426 continue;
1427 }
1428
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001429 if (SubV.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001430 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001431 continue;
1432 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001433
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001434 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001435
1436 // Handle ++ and -- (both pre- and post-increment).
1437
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001438 LVal SubLV = cast<LVal>(SubV);
1439 RVal V = GetRVal(St, SubLV, U->getType());
1440
Ted Kremenek89063af2008-02-21 19:15:37 +00001441 if (V.isUnknown()) {
1442 Dst.Add(N1);
1443 continue;
1444 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001445
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001446 // Propagate undefined values.
1447 if (V.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001448 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001449 continue;
1450 }
1451
Ted Kremenek443003b2008-02-21 19:29:23 +00001452 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001453
1454 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1455 : BinaryOperator::Sub;
1456
Ted Kremenek443003b2008-02-21 19:29:23 +00001457 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001458
1459 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001460 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001461 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001462 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001463
Ted Kremenek0e561a32008-03-21 21:30:14 +00001464 MakeNode(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001465 continue;
1466 }
1467
1468 // Handle all other unary operators.
1469
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001470 switch (U->getOpcode()) {
Ted Kremenek5139c782008-03-15 03:05:30 +00001471
1472 case UnaryOperator::Extension:
1473 St = SetRVal(St, U, SubV);
1474 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001475
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001476 case UnaryOperator::Minus:
1477 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001478 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001479
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001480 case UnaryOperator::Not:
1481 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +00001482 break;
Ted Kremenek90e42032008-02-20 04:12:31 +00001483
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001484 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +00001485
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001486 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1487 //
1488 // Note: technically we do "E == 0", but this is the same in the
1489 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001490
1491 if (isa<LVal>(SubV)) {
Ted Kremenek240f1f02008-03-07 20:13:31 +00001492 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001493 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
1494 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001495 }
1496 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +00001497 Expr* Ex = U->getSubExpr();
Ted Kremenek240f1f02008-03-07 20:13:31 +00001498 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001499 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
1500 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001501 }
1502
1503 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001504
Ted Kremenek64924852008-01-31 02:35:41 +00001505 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001506 assert (isa<LVal>(SubV));
1507 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +00001508 break;
1509 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001510
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001511 default: ;
1512 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001513 }
1514
Ted Kremenek0e561a32008-03-21 21:30:14 +00001515 MakeNode(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001516 }
1517}
1518
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001519void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
1520 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001521
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001522 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001523
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001524 // FIXME: Add support for VLAs.
1525 if (!T.getTypePtr()->isConstantSizeType())
1526 return;
1527
Chris Lattner98be4942008-03-05 18:54:05 +00001528 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001529 ValueState* St = GetState(Pred);
Ted Kremenek240f1f02008-03-07 20:13:31 +00001530 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001531
Ted Kremenek0e561a32008-03-21 21:30:14 +00001532 MakeNode(Dst, U, Pred, St);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001533}
1534
1535void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +00001536
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001537 Ex = Ex->IgnoreParens();
1538
Ted Kremenek2ad88682008-02-27 07:04:16 +00001539 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1540 Dst.Add(Pred);
1541 return;
1542 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001543
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001544 switch (Ex->getStmtClass()) {
1545 default:
1546 break;
1547
Ted Kremenek540cbe22008-04-22 04:56:29 +00001548 case Stmt::ArraySubscriptExprClass:
1549 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
1550 return;
1551
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001552 case Stmt::DeclRefExprClass:
1553 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001554 return;
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001555
1556 case Stmt::UnaryOperatorClass: {
1557 UnaryOperator* U = cast<UnaryOperator>(Ex);
1558
1559 if (U->getOpcode() == UnaryOperator::Deref) {
1560 VisitDeref(U, Pred, Dst, true);
1561 return;
1562 }
1563
1564 break;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001565 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001566
1567 case Stmt::MemberExprClass:
1568 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
1569 return;
1570 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001571
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001572 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001573}
1574
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001575void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1576 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1577}
1578
1579void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1580 AsmStmt::outputs_iterator I,
1581 AsmStmt::outputs_iterator E,
1582 NodeTy* Pred, NodeSet& Dst) {
1583 if (I == E) {
1584 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1585 return;
1586 }
1587
1588 NodeSet Tmp;
1589 VisitLVal(*I, Pred, Tmp);
1590
1591 ++I;
1592
1593 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1594 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1595}
1596
1597void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1598 AsmStmt::inputs_iterator I,
1599 AsmStmt::inputs_iterator E,
1600 NodeTy* Pred, NodeSet& Dst) {
1601 if (I == E) {
1602
1603 // We have processed both the inputs and the outputs. All of the outputs
1604 // should evaluate to LVals. Nuke all of their values.
1605
1606 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1607 // which interprets the inline asm and stores proper results in the
1608 // outputs.
1609
1610 ValueState* St = GetState(Pred);
1611
1612 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1613 OE = A->end_outputs(); OI != OE; ++OI) {
1614
1615 RVal X = GetLVal(St, *OI);
1616
1617 assert (!isa<NonLVal>(X));
1618
1619 if (isa<LVal>(X))
1620 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1621 }
1622
Ted Kremenek0e561a32008-03-21 21:30:14 +00001623 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001624 return;
1625 }
1626
1627 NodeSet Tmp;
1628 Visit(*I, Pred, Tmp);
1629
1630 ++I;
1631
1632 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1633 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1634}
1635
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001636void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1637 assert (Builder && "GRStmtNodeBuilder must be defined.");
1638
1639 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001640
1641 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1642 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001643
Ted Kremenekb0533962008-04-18 20:35:30 +00001644 Builder->HasGeneratedNode = false;
1645
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001646 TF->EvalReturn(Dst, *this, *Builder, S, Pred);
1647
Ted Kremenekb0533962008-04-18 20:35:30 +00001648 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001649
Ted Kremenekb0533962008-04-18 20:35:30 +00001650 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001651 MakeNode(Dst, S, Pred, GetState(Pred));
1652}
1653
Ted Kremenek02737ed2008-03-31 15:02:58 +00001654void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1655
1656 Expr* R = S->getRetValue();
1657
1658 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001659 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001660 return;
1661 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001662
1663 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001664 QualType T = R->getType();
1665
Chris Lattner423a3c92008-04-02 17:45:06 +00001666 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001667
1668 // Check if any of the return values return the address of a stack variable.
1669
1670 NodeSet Tmp;
1671 Visit(R, Pred, Tmp);
1672
1673 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1674 RVal X = GetRVal((*I)->getState(), R);
1675
1676 if (isa<lval::DeclVal>(X)) {
1677
1678 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1679
1680 // Create a special node representing the v
1681
1682 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1683
1684 if (RetStackNode) {
1685 RetStackNode->markAsSink();
1686 RetsStackAddr.insert(RetStackNode);
1687 }
1688
1689 continue;
1690 }
1691 }
1692
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001693 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001694 }
1695 }
1696 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001697 Visit(R, Pred, DstRet);
1698
1699 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1700 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001701}
Ted Kremenek55deb972008-03-25 00:34:37 +00001702
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001703//===----------------------------------------------------------------------===//
1704// Transfer functions: Binary operators.
1705//===----------------------------------------------------------------------===//
1706
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001707void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001708 GRExprEngine::NodeTy* Pred,
1709 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001710 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001711
1712 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001713 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001714 else
1715 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001716
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001717 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001718
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001719 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001720
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001721 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001722 // In such cases, we want to (initially) treat the LHS as an LVal,
1723 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1724 // evaluated to LValDecl's instead of to an NonLVal.
1725
Ted Kremenek0d093d32008-03-10 04:11:42 +00001726 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1727 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001728
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001729 // Visit the RHS...
1730
1731 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001732 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001733
1734 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001735
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001736 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001737
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001738 NodeTy* N2 = *I2;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001739 ValueState* St = GetState(N2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001740 Expr* RHS = B->getRHS();
1741 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001742
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001743 BinaryOperator::Opcode Op = B->getOpcode();
1744
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001745 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1746 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001747
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001748 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001749
Ted Kremenek00155412008-02-26 22:27:51 +00001750 if (!RightV.isUnknown()) {
1751
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001752 if (RightV.isUndef()) {
1753 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001754
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001755 if (DivUndef) {
1756 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001757 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001758 }
1759
1760 continue;
1761 }
1762
1763 // Check for divide/remainder-by-zero.
1764 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001765 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001766
Ted Kremenek4d839b42008-03-07 19:04:53 +00001767 bool isFeasibleZero = false;
1768 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001769
Ted Kremenek00155412008-02-26 22:27:51 +00001770 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001771
Ted Kremenek4d839b42008-03-07 19:04:53 +00001772 bool isFeasibleNotZero = false;
1773 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001774
Ted Kremenek4d839b42008-03-07 19:04:53 +00001775 // Create the node for the divide-by-zero (if it occurred).
1776
1777 if (isFeasibleZero)
1778 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1779 DivZeroNode->markAsSink();
1780
1781 if (isFeasibleNotZero)
1782 ImplicitBadDivides.insert(DivZeroNode);
1783 else
1784 ExplicitBadDivides.insert(DivZeroNode);
1785
1786 }
1787
1788 if (!isFeasibleNotZero)
Ted Kremenek00155412008-02-26 22:27:51 +00001789 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001790 }
1791
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001792 // Fall-through. The logic below processes the divide.
1793 }
1794
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001795
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001796 if (Op <= BinaryOperator::Or) {
1797
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001798 // Process non-assignements except commas or short-circuited
1799 // logical expressions (LAnd and LOr).
1800
1801 RVal Result = EvalBinOp(Op, LeftV, RightV);
1802
1803 if (Result.isUnknown()) {
1804 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001805 continue;
1806 }
1807
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001808 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1809
1810 // The operands were not undefined, but the result is undefined.
1811
1812 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1813 UndefNode->markAsSink();
1814 UndefResults.insert(UndefNode);
1815 }
1816
1817 continue;
1818 }
1819
Ted Kremenek0e561a32008-03-21 21:30:14 +00001820 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001821 continue;
1822 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001823
1824 // Process assignments.
1825
1826 switch (Op) {
1827
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001828 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001829
1830 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001831
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001832 if (LeftV.isUndef()) {
1833 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001834 continue;
1835 }
1836
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001837 // EXPERIMENTAL: "Conjured" symbols.
1838
1839 if (RightV.isUnknown()) {
1840 unsigned Count = Builder->getCurrentBlockCount();
1841 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1842
1843 RightV = B->getRHS()->getType()->isPointerType()
1844 ? cast<RVal>(lval::SymbolVal(Sym))
1845 : cast<RVal>(nonlval::SymbolVal(Sym));
1846 }
1847
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001848 // Simulate the effects of a "store": bind the value of the RHS
1849 // to the L-Value represented by the LHS.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001850
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001851 EvalStore(Dst, B, N2, SetRVal(St, B, RightV),
Ted Kremenek13922612008-04-16 20:40:59 +00001852 LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00001853
Ted Kremeneke38718e2008-04-16 18:21:25 +00001854 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001855 }
1856
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001857 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001858
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001859 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001860
Ted Kremenek00155412008-02-26 22:27:51 +00001861 assert (B->isCompoundAssignmentOp());
1862
1863 if (Op >= BinaryOperator::AndAssign)
1864 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1865 else
1866 ((int&) Op) -= BinaryOperator::MulAssign;
1867
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001868 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001869
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001870 if (LeftV.isUndef()) {
1871 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001872 continue;
1873 }
1874
1875 if (LeftV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001876 assert (isa<UnknownVal>(GetRVal(St, B)));
1877 Dst.Add(N2);
1878 continue;
Ted Kremenek9dca0622008-02-19 00:22:37 +00001879 }
1880
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001881 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek3ce92142008-03-09 08:12:37 +00001882 // that is neither "Unknown" or "Undefined."
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001883
1884 LVal LeftLV = cast<LVal>(LeftV);
1885
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001886 // Fetch the value of the LHS (the value of the variable, etc.).
1887
Ted Kremenek0d093d32008-03-10 04:11:42 +00001888 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001889
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001890 // Propagate undefined value (left-side). We
1891 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001892 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001893
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001894 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001895 St = SetRVal(St, B, V);
1896 break;
1897 }
1898
1899 // Propagate unknown values.
1900
Ted Kremenek89063af2008-02-21 19:15:37 +00001901 if (V.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001902 // The value bound to LeftV is unknown. Thus we just
1903 // propagate the current node (as "B" is already bound to nothing).
1904 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenek89063af2008-02-21 19:15:37 +00001905 Dst.Add(N2);
1906 continue;
1907 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001908
1909 if (RightV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001910 assert (isa<UnknownVal>(GetRVal(St, B)));
1911 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001912 break;
1913 }
1914
Ted Kremenek00155412008-02-26 22:27:51 +00001915 // At this point:
1916 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001917 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001918 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001919
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001920 // Get the computation type.
1921 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1922
1923 // Perform promotions.
1924 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001925 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001926
1927 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001928
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001929 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1930 && RHS->getType()->isIntegerType()) {
1931
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001932 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001933
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001934 if (RightV.isUndef()) {
1935 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001936
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001937 if (DivUndef) {
1938 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001939 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001940 }
1941
1942 continue;
1943 }
Ted Kremenek00155412008-02-26 22:27:51 +00001944
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001945 // First, "assume" that the denominator is 0.
1946
Ted Kremenek4d839b42008-03-07 19:04:53 +00001947 bool isFeasibleZero = false;
1948 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001949
Ted Kremenek4d839b42008-03-07 19:04:53 +00001950 // Second, "assume" that the denominator cannot be 0.
1951
1952 bool isFeasibleNotZero = false;
1953 St = Assume(St, RightV, true, isFeasibleNotZero);
1954
1955 // Create the node for the divide-by-zero error (if it occurred).
1956
1957 if (isFeasibleZero) {
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001958 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1959
1960 if (DivZeroNode) {
1961 DivZeroNode->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001962
1963 if (isFeasibleNotZero)
1964 ImplicitBadDivides.insert(DivZeroNode);
1965 else
1966 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001967 }
1968 }
1969
Ted Kremenek4d839b42008-03-07 19:04:53 +00001970 if (!isFeasibleNotZero)
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001971 continue;
1972
1973 // Fall-through. The logic below processes the divide.
1974 }
Ted Kremenek00155412008-02-26 22:27:51 +00001975 else {
1976
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001977 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001978
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001979 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001980 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1981 break;
1982 }
1983
1984 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001985
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001986 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001987
1988 if (Result.isUndef()) {
1989
1990 // The operands were not undefined, but the result is undefined.
1991
1992 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1993 UndefNode->markAsSink();
1994 UndefResults.insert(UndefNode);
1995 }
1996
1997 continue;
1998 }
1999
Ted Kremeneke38718e2008-04-16 18:21:25 +00002000 // St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002001 EvalStore(Dst, B, N2, SetRVal(St, B, Result), LeftLV, Result);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002002 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002003 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002004 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002005
Ted Kremenek0e561a32008-03-21 21:30:14 +00002006 MakeNode(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002007 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002008 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002009}
Ted Kremenekee985462008-01-16 18:18:48 +00002010
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002011void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00002012 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenek9dca0622008-02-19 00:22:37 +00002013 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002014 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00002015}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00002016
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00002017
Ted Kremenekee985462008-01-16 18:18:48 +00002018//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00002019// "Assume" logic.
2020//===----------------------------------------------------------------------===//
2021
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002022ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002023 bool Assumption, bool& isFeasible) {
2024
2025 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002026
2027 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2028 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002029}
2030
2031ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond,
2032 bool Assumption, bool& isFeasible) {
2033
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002034 switch (Cond.getSubKind()) {
2035 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002036 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002037 return St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002038
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002039 case lval::SymbolValKind:
2040 if (Assumption)
2041 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002042 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002043 else
2044 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002045 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek550a0f92008-04-18 17:20:23 +00002046
2047
Ted Kremenek329f8542008-02-05 21:52:21 +00002048 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00002049 case lval::FuncValKind:
2050 case lval::GotoLabelKind:
Ted Kremeneka5488462008-04-22 21:39:21 +00002051 case lval::StringLiteralValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002052 isFeasible = Assumption;
2053 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002054
Ted Kremenek329f8542008-02-05 21:52:21 +00002055 case lval::ConcreteIntKind: {
2056 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002057 isFeasible = b ? Assumption : !Assumption;
2058 return St;
2059 }
2060 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002061}
2062
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002063ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002064 bool Assumption, bool& isFeasible) {
2065
2066 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002067
2068 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2069 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002070}
2071
2072ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond,
2073 bool Assumption, bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002074 switch (Cond.getSubKind()) {
2075 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002076 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00002077 return St;
2078
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002079
2080 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00002081 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002082 SymbolID sym = SV.getSymbol();
2083
2084 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00002085 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002086 isFeasible);
2087 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00002088 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002089 isFeasible);
2090 }
2091
Ted Kremenek08b66252008-02-06 04:31:33 +00002092 case nonlval::SymIntConstraintValKind:
2093 return
2094 AssumeSymInt(St, Assumption,
2095 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
2096 isFeasible);
2097
Ted Kremenek329f8542008-02-05 21:52:21 +00002098 case nonlval::ConcreteIntKind: {
2099 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00002100 isFeasible = b ? Assumption : !Assumption;
2101 return St;
2102 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002103
2104 case nonlval::LValAsIntegerKind: {
2105 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
2106 Assumption, isFeasible);
2107 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002108 }
2109}
2110
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002111ValueState*
2112GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002113 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00002114
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002115 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002116 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002117 isFeasible = *X != V;
2118 return St;
2119 }
2120
2121 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002122 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002123 isFeasible = true;
2124 return St;
2125 }
2126
2127 // If we reach here, sym is not a constant and we don't know if it is != V.
2128 // Make that assumption.
2129
2130 isFeasible = true;
2131 return StateMgr.AddNE(St, sym, V);
2132}
2133
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002134ValueState*
2135GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002136 const llvm::APSInt& V, bool& isFeasible) {
2137
2138 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002139 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002140 isFeasible = *X == V;
2141 return St;
2142 }
2143
2144 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002145 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002146 isFeasible = false;
2147 return St;
2148 }
2149
2150 // If we reach here, sym is not a constant and we don't know if it is == V.
2151 // Make that assumption.
2152
2153 isFeasible = true;
2154 return StateMgr.AddEQ(St, sym, V);
2155}
Ted Kremenekb38911f2008-01-30 23:03:39 +00002156
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002157ValueState*
2158GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00002159 const SymIntConstraint& C, bool& isFeasible) {
2160
2161 switch (C.getOpcode()) {
2162 default:
2163 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002164 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00002165 return St;
2166
2167 case BinaryOperator::EQ:
2168 if (Assumption)
2169 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2170 else
2171 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2172
2173 case BinaryOperator::NE:
2174 if (Assumption)
2175 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2176 else
2177 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2178 }
2179}
2180
Ted Kremenekb38911f2008-01-30 23:03:39 +00002181//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002182// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002183//===----------------------------------------------------------------------===//
2184
Ted Kremenekaa66a322008-01-16 21:46:15 +00002185#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002186static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002187static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002188static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002189
Ted Kremenekaa66a322008-01-16 21:46:15 +00002190namespace llvm {
2191template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002192struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002193 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002194
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002195 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002196
2197 Out << "Variables:\\l";
2198
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002199 bool isFirst = true;
2200
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002201 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002202
2203 if (isFirst)
2204 isFirst = false;
2205 else
2206 Out << "\\l";
2207
2208 Out << ' ' << I.getKey()->getName() << " : ";
2209 I.getData().print(Out);
2210 }
2211
2212 }
2213
Ted Kremeneke7d22112008-02-11 19:21:59 +00002214
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002215 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002216
2217 bool isFirst = true;
2218
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002219 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002220
2221 if (isFirst) {
2222 Out << "\\l\\lSub-Expressions:\\l";
2223 isFirst = false;
2224 }
2225 else
2226 Out << "\\l";
2227
2228 Out << " (" << (void*) I.getKey() << ") ";
2229 I.getKey()->printPretty(Out);
2230 Out << " : ";
2231 I.getData().print(Out);
2232 }
2233 }
2234
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002235 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002236
Ted Kremenek016f52f2008-02-08 21:10:02 +00002237 bool isFirst = true;
2238
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002239 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002240 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002241 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002242 isFirst = false;
2243 }
2244 else
2245 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002246
Ted Kremeneke7d22112008-02-11 19:21:59 +00002247 Out << " (" << (void*) I.getKey() << ") ";
2248 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002249 Out << " : ";
2250 I.getData().print(Out);
2251 }
2252 }
2253
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002254 static void PrintEQ(std::ostream& Out, ValueState* St) {
2255 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002256
2257 if (CE.isEmpty())
2258 return;
2259
2260 Out << "\\l\\|'==' constraints:";
2261
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002262 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002263 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2264 }
2265
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002266 static void PrintNE(std::ostream& Out, ValueState* St) {
2267 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002268
2269 if (NE.isEmpty())
2270 return;
2271
2272 Out << "\\l\\|'!=' constraints:";
2273
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002274 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002275 I != EI; ++I){
2276
2277 Out << "\\l $" << I.getKey() << " : ";
2278 bool isFirst = true;
2279
2280 ValueState::IntSetTy::iterator J=I.getData().begin(),
2281 EJ=I.getData().end();
2282 for ( ; J != EJ; ++J) {
2283 if (isFirst) isFirst = false;
2284 else Out << ", ";
2285
2286 Out << (*J)->toString();
2287 }
2288 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002289 }
2290
2291 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2292
2293 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002294 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002295 GraphPrintCheckerState->isUndefDeref(N) ||
2296 GraphPrintCheckerState->isUndefStore(N) ||
2297 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002298 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2299 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002300 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002301 GraphPrintCheckerState->isBadCall(N) ||
2302 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002303 return "color=\"red\",style=\"filled\"";
2304
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002305 if (GraphPrintCheckerState->isNoReturnCall(N))
2306 return "color=\"blue\",style=\"filled\"";
2307
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002308 return "";
2309 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002310
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002311 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002312 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002313
2314 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002315 ProgramPoint Loc = N->getLocation();
2316
2317 switch (Loc.getKind()) {
2318 case ProgramPoint::BlockEntranceKind:
2319 Out << "Block Entrance: B"
2320 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2321 break;
2322
2323 case ProgramPoint::BlockExitKind:
2324 assert (false);
2325 break;
2326
2327 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002328 const PostStmt& L = cast<PostStmt>(Loc);
2329 Stmt* S = L.getStmt();
2330 SourceLocation SLoc = S->getLocStart();
2331
2332 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2333 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002334
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002335 if (SLoc.isFileID()) {
2336 Out << "\\lline="
2337 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2338 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2339 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002340
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002341 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002342 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002343 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002344 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002345 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002346 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002347 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002348 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002349 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2350 Out << "\\|Explicit divide-by zero or undefined value.";
2351 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2352 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002353 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002354 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002355 else if (GraphPrintCheckerState->isNoReturnCall(N))
2356 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002357 else if (GraphPrintCheckerState->isBadCall(N))
2358 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002359 else if (GraphPrintCheckerState->isUndefArg(N))
2360 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002361
Ted Kremenekaa66a322008-01-16 21:46:15 +00002362 break;
2363 }
2364
2365 default: {
2366 const BlockEdge& E = cast<BlockEdge>(Loc);
2367 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2368 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002369
2370 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002371
2372 SourceLocation SLoc = T->getLocStart();
2373
Ted Kremenekb38911f2008-01-30 23:03:39 +00002374 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002375
Ted Kremenekb38911f2008-01-30 23:03:39 +00002376 E.getSrc()->printTerminator(Out);
2377
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002378 if (SLoc.isFileID()) {
2379 Out << "\\lline="
2380 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2381 << GraphPrintSourceManager->getColumnNumber(SLoc);
2382 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002383
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002384 if (isa<SwitchStmt>(T)) {
2385 Stmt* Label = E.getDst()->getLabel();
2386
2387 if (Label) {
2388 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2389 Out << "\\lcase ";
2390 C->getLHS()->printPretty(Out);
2391
2392 if (Stmt* RHS = C->getRHS()) {
2393 Out << " .. ";
2394 RHS->printPretty(Out);
2395 }
2396
2397 Out << ":";
2398 }
2399 else {
2400 assert (isa<DefaultStmt>(Label));
2401 Out << "\\ldefault:";
2402 }
2403 }
2404 else
2405 Out << "\\l(implicit) default:";
2406 }
2407 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002408 // FIXME
2409 }
2410 else {
2411 Out << "\\lCondition: ";
2412 if (*E.getSrc()->succ_begin() == E.getDst())
2413 Out << "true";
2414 else
2415 Out << "false";
2416 }
2417
2418 Out << "\\l";
2419 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002420
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002421 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2422 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002423 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002424 }
2425 }
2426
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002427 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002428
Ted Kremenek75da3e82008-03-11 19:02:40 +00002429 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002430
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002431 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002432 return Out.str();
2433 }
2434};
2435} // end llvm namespace
2436#endif
2437
Ted Kremenekffe0f432008-03-07 22:58:01 +00002438#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002439
2440template <typename ITERATOR>
2441GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2442
2443template <>
2444GRExprEngine::NodeTy*
2445GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2446 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2447 return I->first;
2448}
2449
Ted Kremenekffe0f432008-03-07 22:58:01 +00002450template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002451static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002452 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002453
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002454 llvm::SmallPtrSet<void*,10> CachedSources;
2455
2456 for ( ; I != E; ++I ) {
2457 GRExprEngine::NodeTy* N = GetGraphNode(I);
2458 void* p = N->getLocation().getRawData();
2459
2460 if (CachedSources.count(p))
2461 continue;
2462
2463 CachedSources.insert(p);
2464
2465 Sources.push_back(N);
2466 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002467}
2468#endif
2469
2470void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002471#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002472 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002473 std::vector<NodeTy*> Src;
2474
2475 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002476 AddSources(Src, null_derefs_begin(), null_derefs_end());
2477 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2478 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2479 AddSources(Src, undef_results_begin(), undef_results_end());
2480 AddSources(Src, bad_calls_begin(), bad_calls_end());
2481 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002482 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002483
Ted Kremenekcb612922008-04-18 19:23:43 +00002484 // The new way.
2485 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2486 (*I)->GetErrorNodes(Src);
2487
2488
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002489 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002490 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002491 else {
2492 GraphPrintCheckerState = this;
2493 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002494 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002495
Ted Kremenekffe0f432008-03-07 22:58:01 +00002496 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002497
2498 GraphPrintCheckerState = NULL;
2499 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002500 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002501 }
2502#endif
2503}
2504
2505void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2506#ifndef NDEBUG
2507 GraphPrintCheckerState = this;
2508 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002509 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002510
Ted Kremenek493d7a22008-03-11 18:25:33 +00002511 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2512
2513 if (!TrimmedG)
2514 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2515 else {
2516 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2517 delete TrimmedG;
2518 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002519
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002520 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002521 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002522 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002523#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002524}