blob: 86d2cc9ae5fa359c45b36ffea8e79baf93b8a3a7 [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;
221
222 case Stmt::AsmStmtClass:
223 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
224 break;
225
226 case Stmt::BinaryOperatorClass: {
227 BinaryOperator* B = cast<BinaryOperator>(S);
228
229 if (B->isLogicalOp()) {
230 VisitLogicalExpr(B, Pred, Dst);
231 break;
232 }
233 else if (B->getOpcode() == BinaryOperator::Comma) {
234 ValueState* St = GetState(Pred);
235 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
236 break;
237 }
238
239 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
240 break;
241 }
242
243 case Stmt::CallExprClass: {
244 CallExpr* C = cast<CallExpr>(S);
245 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
246 break;
247 }
248
249 case Stmt::CastExprClass: {
250 CastExpr* C = cast<CastExpr>(S);
251 VisitCast(C, C->getSubExpr(), Pred, Dst);
252 break;
253 }
254
255 // FIXME: ChooseExpr is really a constant. We need to fix
256 // the CFG do not model them as explicit control-flow.
257
258 case Stmt::ChooseExprClass: { // __builtin_choose_expr
259 ChooseExpr* C = cast<ChooseExpr>(S);
260 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
261 break;
262 }
263
264 case Stmt::CompoundAssignOperatorClass:
265 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
266 break;
267
268 case Stmt::ConditionalOperatorClass: { // '?' operator
269 ConditionalOperator* C = cast<ConditionalOperator>(S);
270 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
271 break;
272 }
273
274 case Stmt::DeclRefExprClass:
275 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
276 break;
277
278 case Stmt::DeclStmtClass:
279 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
280 break;
281
282 case Stmt::ImplicitCastExprClass: {
283 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
284 VisitCast(C, C->getSubExpr(), Pred, Dst);
285 break;
286 }
287
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000288 case Stmt::MemberExprClass: {
289 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
290 break;
291 }
292
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000293 case Stmt::ObjCMessageExprClass: {
294 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
295 break;
296 }
297
298 case Stmt::ParenExprClass:
299 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
300 break;
301
302 case Stmt::SizeOfAlignOfTypeExprClass:
303 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
304 break;
305
306 case Stmt::StmtExprClass: {
307 StmtExpr* SE = cast<StmtExpr>(S);
308
309 ValueState* St = GetState(Pred);
310
311 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
312 // probably just remove these from the CFG.
313 assert (!SE->getSubStmt()->body_empty());
314
315 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
316 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
317 else
318 Dst.Add(Pred);
319
320 break;
321 }
322
323 // FIXME: We may wish to always bind state to ReturnStmts so
324 // that users can quickly query what was the state at the
325 // exit points of a function.
326
327 case Stmt::ReturnStmtClass:
328 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst); break;
329
330 case Stmt::UnaryOperatorClass: {
331 UnaryOperator* U = cast<UnaryOperator>(S);
332
333 switch (U->getOpcode()) {
334 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
335 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
336 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
337 default: VisitUnaryOperator(U, Pred, Dst); break;
338 }
339
340 break;
341 }
342 }
343}
344
345//===----------------------------------------------------------------------===//
346// Block entrance. (Update counters).
347//===----------------------------------------------------------------------===//
348
349bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
350 GRBlockCounter BC) {
351
352 return BC.getNumVisited(B->getBlockID()) < 3;
353}
354
355//===----------------------------------------------------------------------===//
356// Branch processing.
357//===----------------------------------------------------------------------===//
358
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000359ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
360 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000361
362 switch (Terminator->getStmtClass()) {
363 default:
364 return St;
365
366 case Stmt::BinaryOperatorClass: { // '&&' and '||'
367
368 BinaryOperator* B = cast<BinaryOperator>(Terminator);
369 BinaryOperator::Opcode Op = B->getOpcode();
370
371 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
372
373 // For &&, if we take the true branch, then the value of the whole
374 // expression is that of the RHS expression.
375 //
376 // For ||, if we take the false branch, then the value of the whole
377 // expression is that of the RHS expression.
378
379 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
380 (Op == BinaryOperator::LOr && !branchTaken)
381 ? B->getRHS() : B->getLHS();
382
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000383 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000384 }
385
386 case Stmt::ConditionalOperatorClass: { // ?:
387
388 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
389
390 // For ?, if branchTaken == true then the value is either the LHS or
391 // the condition itself. (GNU extension).
392
393 Expr* Ex;
394
395 if (branchTaken)
396 Ex = C->getLHS() ? C->getLHS() : C->getCond();
397 else
398 Ex = C->getRHS();
399
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000400 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000401 }
402
403 case Stmt::ChooseExprClass: { // ?:
404
405 ChooseExpr* C = cast<ChooseExpr>(Terminator);
406
407 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000408 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000409 }
410 }
411}
412
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000413void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000414 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000415
Ted Kremeneke7d22112008-02-11 19:21:59 +0000416 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000417 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000418
Ted Kremenekb2331832008-02-15 22:29:00 +0000419 // Check for NULL conditions; e.g. "for(;;)"
420 if (!Condition) {
421 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000422 return;
423 }
424
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000425 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000426
427 switch (V.getBaseKind()) {
428 default:
429 break;
430
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000431 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000432 builder.generateNode(MarkBranch(PrevState, Term, true), true);
433 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000434 return;
435
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000436 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000437 NodeTy* N = builder.generateNode(PrevState, true);
438
439 if (N) {
440 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000441 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000442 }
443
444 builder.markInfeasible(false);
445 return;
446 }
447 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000448
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000449 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000450
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000451 bool isFeasible = false;
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000452 ValueState* St = Assume(PrevState, V, true, isFeasible);
453
454 if (isFeasible)
455 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000456 else
457 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000458
459 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000460
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000461 isFeasible = false;
462 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000463
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000464 if (isFeasible)
465 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000466 else
467 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000468}
469
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000470/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000471/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000472void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000473
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000474 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000475 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000476
477 // Three possibilities:
478 //
479 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000480 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000481 // (3) We have no clue about the label. Dispatch to all targets.
482 //
483
484 typedef IndirectGotoNodeBuilder::iterator iterator;
485
486 if (isa<lval::GotoLabel>(V)) {
487 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
488
489 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000490 if (I.getLabel() == L) {
491 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000492 return;
493 }
494 }
495
496 assert (false && "No block with label.");
497 return;
498 }
499
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000500 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000501 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000502 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000503 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000504 return;
505 }
506
507 // This is really a catch-all. We don't support symbolics yet.
508
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000509 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000510
511 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000512 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000513}
Ted Kremenekf233d482008-02-05 00:26:40 +0000514
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000515
516void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
517 NodeTy* Pred, NodeSet& Dst) {
518
519 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
520
521 ValueState* St = GetState(Pred);
522 RVal X = GetBlkExprRVal(St, Ex);
523
524 assert (X.isUndef());
525
526 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
527
528 assert (SE);
529
530 X = GetBlkExprRVal(St, SE);
531
532 // Make sure that we invalidate the previous binding.
533 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
534}
535
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000536/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
537/// nodes by processing the 'effects' of a switch statement.
538void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
539
540 typedef SwitchNodeBuilder::iterator iterator;
541
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000542 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000543 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000544 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000545
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000546 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000547 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000548 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000549 return;
550 }
551
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000552 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000553
554 // While most of this can be assumed (such as the signedness), having it
555 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000556
Chris Lattner98be4942008-03-05 18:54:05 +0000557 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000558
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000559 APSInt V1(bits, false);
560 APSInt V2 = V1;
561
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000562 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000563
564 CaseStmt* Case = cast<CaseStmt>(I.getCase());
565
566 // Evaluate the case.
567 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
568 assert (false && "Case condition must evaluate to an integer constant.");
569 return;
570 }
571
572 // Get the RHS of the case, if it exists.
573
574 if (Expr* E = Case->getRHS()) {
575 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
576 assert (false &&
577 "Case condition (RHS) must evaluate to an integer constant.");
578 return ;
579 }
580
581 assert (V1 <= V2);
582 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000583 else
584 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000585
586 // FIXME: Eventually we should replace the logic below with a range
587 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000588 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000589
Ted Kremenek14a11402008-03-17 22:17:56 +0000590 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000591 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000592
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000593 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000594
595 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000596
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000597 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000598 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000599
600 if (isFeasible) {
601 builder.generateCaseStmtNode(I, StNew);
602
603 // If CondV evaluates to a constant, then we know that this
604 // is the *only* case that we can take, so stop evaluating the
605 // others.
606 if (isa<nonlval::ConcreteInt>(CondV))
607 return;
608 }
609
610 // Now "assume" that the case doesn't match. Add this state
611 // to the default state (if it is feasible).
612
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000613 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000614 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000615
616 if (isFeasible)
617 DefaultSt = StNew;
618
Ted Kremenek14a11402008-03-17 22:17:56 +0000619 // Concretize the next value in the range.
620 if (V1 == V2)
621 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000622
Ted Kremenek14a11402008-03-17 22:17:56 +0000623 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000624 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000625
626 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000627 }
628
629 // If we reach here, than we know that the default branch is
630 // possible.
631 builder.generateDefaultCaseNode(DefaultSt);
632}
633
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000634//===----------------------------------------------------------------------===//
635// Transfer functions: logical operations ('&&', '||').
636//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000637
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000638void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000639 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000640
Ted Kremenek05a23782008-02-26 19:05:15 +0000641 assert (B->getOpcode() == BinaryOperator::LAnd ||
642 B->getOpcode() == BinaryOperator::LOr);
643
644 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
645
Ted Kremenek0d093d32008-03-10 04:11:42 +0000646 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000647 RVal X = GetBlkExprRVal(St, B);
648
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000649 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000650
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000651 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000652
653 assert (Ex);
654
655 if (Ex == B->getRHS()) {
656
657 X = GetBlkExprRVal(St, Ex);
658
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000659 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000660
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000661 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000662 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000663 return;
664 }
665
Ted Kremenek05a23782008-02-26 19:05:15 +0000666 // We took the RHS. Because the value of the '&&' or '||' expression must
667 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
668 // or 1. Alternatively, we could take a lazy approach, and calculate this
669 // value later when necessary. We don't have the machinery in place for
670 // this right now, and since most logical expressions are used for branches,
671 // the payoff is not likely to be large. Instead, we do eager evaluation.
672
673 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000674 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000675
676 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000677 MakeNode(Dst, B, Pred,
678 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000679
680 isFeasible = false;
681 NewState = Assume(St, X, false, isFeasible);
682
683 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000684 MakeNode(Dst, B, Pred,
685 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000686 }
687 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000688 // We took the LHS expression. Depending on whether we are '&&' or
689 // '||' we know what the value of the expression is via properties of
690 // the short-circuiting.
691
692 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000693 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000694 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000695}
Ted Kremenek05a23782008-02-26 19:05:15 +0000696
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000697//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000698// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000699//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000700
Ted Kremenek44842c22008-02-13 18:06:44 +0000701void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000702
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000703 if (D != CurrentStmt) {
704 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
705 return;
706 }
707
708 // If we are here, we are loading the value of the decl and binding
709 // it to the block-level expression.
710
Ted Kremenek0d093d32008-03-10 04:11:42 +0000711 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000712 RVal X = RVal::MakeVal(BasicVals, D);
713 RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X;
Ted Kremenek0e561a32008-03-21 21:30:14 +0000714 MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000715}
716
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000717/// VisitMemberExpr - Transfer function for member expressions.
718void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
719 NodeSet& Dst, bool asLVal) {
720
721 Expr* Base = M->getBase()->IgnoreParens();
722
723 NodeSet Tmp;
724 VisitLVal(Base, Pred, Tmp);
725
726 if (Base->getType()->isPointerType()) {
727 NodeSet Tmp2;
728
729 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
730 ValueState* St = GetState(*I);
731 VisitDeref(Base, GetRVal(St, Base), St, *I, Tmp2, true);
732 }
733
734 for (NodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I)
735 VisitMemberExprField(M, Base, *I, Dst, asLVal);
736 }
737 else
738 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I)
739 VisitMemberExprField(M, Base, *I, Dst, asLVal);
740}
741
742void GRExprEngine::VisitMemberExprField(MemberExpr* M, Expr* Base, NodeTy* Pred,
743 NodeSet& Dst, bool asLVal) {
744 Dst.Add(Pred);
745}
746
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000747void GRExprEngine::EvalStore(NodeSet& Dst, Expr* E, NodeTy* Pred,
748 ValueState* St, RVal TargetLV, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000749
750 assert (Builder && "GRStmtNodeBuilder must be defined.");
751
752 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000753
754 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
755 OldHasGen(Builder->HasGeneratedNode);
756
757 Builder->HasGeneratedNode = false;
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000758
Ted Kremenek13922612008-04-16 20:40:59 +0000759 assert (!TargetLV.isUndef());
760
Ted Kremenek6b31e8e2008-04-16 23:05:51 +0000761 TF->EvalStore(Dst, *this, *Builder, E, Pred, St, TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000762
763 // Handle the case where no nodes where generated. Auto-generate that
764 // contains the updated state if we aren't generating sinks.
765
Ted Kremenekb0533962008-04-18 20:35:30 +0000766 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek13922612008-04-16 20:40:59 +0000767 TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, E, Pred, St,
768 TargetLV, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000769}
770
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000771//===----------------------------------------------------------------------===//
772// Transfer function: Function calls.
773//===----------------------------------------------------------------------===//
774
Ted Kremenekde434242008-02-19 01:44:53 +0000775void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000776 CallExpr::arg_iterator AI,
777 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000778 NodeSet& Dst) {
779
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000780 // Process the arguments.
781
782 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000783
Ted Kremeneked2d2ed2008-03-04 00:56:45 +0000784 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000785 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000786 ++AI;
787
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000788 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000789 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000790
791 return;
792 }
793
794 // If we reach here we have processed all of the arguments. Evaluate
795 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +0000796
Ted Kremenek994a09b2008-02-25 21:16:03 +0000797 NodeSet DstTmp;
798 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
Ted Kremeneka1354a52008-03-03 16:47:31 +0000799
Ted Kremenek994a09b2008-02-25 21:16:03 +0000800 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +0000801
802 if (DstTmp.empty())
803 DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000804
805 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000806 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
807
Ted Kremenek0d093d32008-03-10 04:11:42 +0000808 ValueState* St = GetState(*DI);
Ted Kremenek994a09b2008-02-25 21:16:03 +0000809 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000810
Ted Kremeneka1354a52008-03-03 16:47:31 +0000811 // FIXME: Add support for symbolic function calls (calls involving
812 // function pointer values that are symbolic).
813
814 // Check for undefined control-flow or calls to NULL.
815
Ted Kremenek5e03fcb2008-02-29 23:14:48 +0000816 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +0000817 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000818
Ted Kremenek2ded35a2008-02-29 23:53:11 +0000819 if (N) {
820 N->markAsSink();
821 BadCalls.insert(N);
822 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000823
Ted Kremenekde434242008-02-19 01:44:53 +0000824 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000825 }
826
827 // Check for the "noreturn" attribute.
828
829 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
830
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000831 if (isa<lval::FuncVal>(L)) {
832
833 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
834
835 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000836 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000837 else {
838 // HACK: Some functions are not marked noreturn, and don't return.
839 // Here are a few hardwired ones. If this takes too long, we can
840 // potentially cache these results.
841 const char* s = FD->getIdentifier()->getName();
842 unsigned n = strlen(s);
843
844 switch (n) {
845 default:
846 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000847
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000848 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +0000849 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
850 break;
851
852 case 5:
853 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
854 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +0000855 }
856 }
857 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000858
859 // Evaluate the call.
860
Ted Kremeneka1354a52008-03-03 16:47:31 +0000861
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000862 bool invalidateArgs = false;
Ted Kremenekde434242008-02-19 01:44:53 +0000863
Ted Kremenek03da0d72008-02-21 19:46:04 +0000864 if (L.isUnknown()) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000865 // Check for an "unknown" callee.
866 invalidateArgs = true;
867 }
868 else if (isa<lval::FuncVal>(L)) {
869
870 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
871
Ted Kremenek55aea312008-03-05 22:59:42 +0000872 if (unsigned id = Info->getBuiltinID()) {
873 switch (id) {
874 case Builtin::BI__builtin_expect: {
875 // For __builtin_expect, just return the value of the subexpression.
876 assert (CE->arg_begin() != CE->arg_end());
877 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +0000878 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +0000879 continue;
880 }
881
882 default:
883 invalidateArgs = true;
884 break;
885 }
886 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000887 }
888
889 if (invalidateArgs) {
Ted Kremenek03da0d72008-02-21 19:46:04 +0000890 // Invalidate all arguments passed in by reference (LVals).
891 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
892 I != E; ++I) {
893 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000894
Ted Kremenek03da0d72008-02-21 19:46:04 +0000895 if (isa<LVal>(V))
896 St = SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremenek4bf38da2008-03-05 21:15:02 +0000897 }
898
Ted Kremenek0e561a32008-03-21 21:30:14 +0000899 MakeNode(Dst, CE, *DI, St);
Ted Kremenek03da0d72008-02-21 19:46:04 +0000900 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000901 else {
902
903 // Check any arguments passed-by-value against being undefined.
904
905 bool badArg = false;
906
907 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
908 I != E; ++I) {
909
Ted Kremenek0d093d32008-03-10 04:11:42 +0000910 if (GetRVal(GetState(*DI), *I).isUndef()) {
911 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000912
913 if (N) {
914 N->markAsSink();
915 UndefArgs[N] = *I;
916 }
917
918 badArg = true;
919 break;
920 }
921 }
922
923 if (badArg)
924 continue;
925
926 // Dispatch to the plug-in transfer function.
Ted Kremenek330dddd2008-03-05 00:33:14 +0000927
928 unsigned size = Dst.size();
929
Ted Kremenekb0533962008-04-18 20:35:30 +0000930 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
931 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek940b1d82008-04-10 23:44:06 +0000932
Ted Kremenekb0533962008-04-18 20:35:30 +0000933 Builder->HasGeneratedNode = false;
934
Ted Kremenek330dddd2008-03-05 00:33:14 +0000935 EvalCall(Dst, CE, cast<LVal>(L), *DI);
936
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000937 // Handle the case where no nodes where generated. Auto-generate that
938 // contains the updated state if we aren't generating sinks.
939
Ted Kremenekb0533962008-04-18 20:35:30 +0000940 if (!Builder->BuildSinks && Dst.size() == size &&
941 !Builder->HasGeneratedNode)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000942 MakeNode(Dst, CE, *DI, St);
Ted Kremenekd753f3c2008-03-04 22:01:56 +0000943 }
Ted Kremenekde434242008-02-19 01:44:53 +0000944 }
945}
946
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000947//===----------------------------------------------------------------------===//
948// Transfer function: Objective-C message expressions.
949//===----------------------------------------------------------------------===//
950
951void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
952 NodeSet& Dst){
953
954 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
955 Pred, Dst);
956}
957
958void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
959 ObjCMessageExpr::arg_iterator AI,
960 ObjCMessageExpr::arg_iterator AE,
961 NodeTy* Pred, NodeSet& Dst) {
962 if (AI == AE) {
963
964 // Process the receiver.
965
966 if (Expr* Receiver = ME->getReceiver()) {
967 NodeSet Tmp;
968 Visit(Receiver, Pred, Tmp);
969
970 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
971 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
972
973 return;
974 }
975
976 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
977 return;
978 }
979
980 NodeSet Tmp;
981 Visit(*AI, Pred, Tmp);
982
983 ++AI;
984
985 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
986 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
987}
988
989void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
990 NodeTy* Pred,
991 NodeSet& Dst) {
992
993 // FIXME: More logic for the processing the method call.
994
995 ValueState* St = GetState(Pred);
996
997 if (Expr* Receiver = ME->getReceiver()) {
998
999 RVal L = GetRVal(St, Receiver);
1000
1001 // Check for undefined control-flow or calls to NULL.
1002
1003 if (L.isUndef()) {
1004 NodeTy* N = Builder->generateNode(ME, St, Pred);
1005
1006 if (N) {
1007 N->markAsSink();
1008 UndefReceivers.insert(N);
1009 }
1010
1011 return;
1012 }
1013 }
1014
1015 // Check for any arguments that are uninitialized/undefined.
1016
1017 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1018 I != E; ++I) {
1019
1020 if (GetRVal(St, *I).isUndef()) {
1021
1022 // Generate an error node for passing an uninitialized/undefined value
1023 // as an argument to a message expression. This node is a sink.
1024 NodeTy* N = Builder->generateNode(ME, St, Pred);
1025
1026 if (N) {
1027 N->markAsSink();
1028 MsgExprUndefArgs[N] = *I;
1029 }
1030
1031 return;
1032 }
1033 }
1034 // Dispatch to plug-in transfer function.
1035
1036 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001037
1038 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1039 OldHasGen(Builder->HasGeneratedNode);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001040
Ted Kremenekb0533962008-04-18 20:35:30 +00001041 Builder->HasGeneratedNode = false;
1042
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001043 EvalObjCMessageExpr(Dst, ME, Pred);
1044
1045 // Handle the case where no nodes where generated. Auto-generate that
1046 // contains the updated state if we aren't generating sinks.
1047
Ted Kremenekb0533962008-04-18 20:35:30 +00001048 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001049 MakeNode(Dst, ME, Pred, St);
1050}
1051
1052//===----------------------------------------------------------------------===//
1053// Transfer functions: Miscellaneous statements.
1054//===----------------------------------------------------------------------===//
1055
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001056void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001057
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001058 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001059 QualType T = CastE->getType();
1060
Ted Kremenek65cfb732008-03-04 22:16:08 +00001061 if (T->isReferenceType())
1062 VisitLVal(Ex, Pred, S1);
1063 else
1064 Visit(Ex, Pred, S1);
1065
Ted Kremenek402563b2008-02-19 18:47:04 +00001066 // Check for redundant casts or casting to "void"
1067 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001068 Ex->getType() == T ||
1069 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001070
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001071 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001072 Dst.Add(*I1);
1073
Ted Kremenek874d63f2008-01-24 02:02:54 +00001074 return;
1075 }
1076
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001077 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001078 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001079 ValueState* St = GetState(N);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001080
1081 RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex);
1082
Ted Kremenek0e561a32008-03-21 21:30:14 +00001083 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001084 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001085}
1086
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001087void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001088 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +00001089
Ted Kremenek0d093d32008-03-10 04:11:42 +00001090 ValueState* St = GetState(Pred);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001091
1092 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +00001093 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001094
1095 // FIXME: Add support for local arrays.
1096 if (VD->getType()->isArrayType())
1097 continue;
1098
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001099 const Expr* Ex = VD->getInit();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001100
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001101 if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) {
1102
1103 // In this context, Static => Local variable.
1104
1105 assert (!VD->getStorageClass() == VarDecl::Static ||
Steve Naroff248a7532008-04-15 22:42:06 +00001106 !VD->isFileVarDecl());
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001107
1108 // If there is no initializer, set the value of the
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001109 // variable to "Undefined".
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001110 //
1111 // FIXME: static variables may have an initializer, but the second
1112 // time a function is called those values may not be current.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001113
1114 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001115
Ted Kremenek16d81562008-03-04 04:18:04 +00001116 if ( VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek16d81562008-03-04 04:18:04 +00001117
1118 // C99: 6.7.8 Initialization
1119 // If an object that has static storage duration is not initialized
1120 // explicitly, then:
1121 // —if it has pointer type, it is initialized to a null pointer;
1122 // —if it has arithmetic type, it is initialized to (positive or
1123 // unsigned) zero;
1124
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001125 // FIXME: Handle structs. Now we treat their values as unknown.
1126
Ted Kremenek16d81562008-03-04 04:18:04 +00001127 if (T->isPointerType()) {
1128
1129 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001130 lval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001131 }
1132 else if (T->isIntegerType()) {
1133
1134 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001135 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001136 }
1137
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001138
Ted Kremenek16d81562008-03-04 04:18:04 +00001139 }
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001140 else {
Ted Kremenek16d81562008-03-04 04:18:04 +00001141
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001142 // FIXME: Handle structs. Now we treat them as unknown. What
1143 // we need to do is treat their members as unknown.
1144
1145 if (T->isPointerType() || T->isIntegerType())
1146 St = SetRVal(St, lval::DeclVal(VD),
1147 Ex ? GetRVal(St, Ex) : UndefinedVal());
1148 }
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001149 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001150 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001151
Ted Kremenek0e561a32008-03-21 21:30:14 +00001152 MakeNode(Dst, DS, Pred, St);
Ted Kremenek9de04c42008-01-24 20:55:43 +00001153}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001154
Ted Kremenekf233d482008-02-05 00:26:40 +00001155
Ted Kremenekf233d482008-02-05 00:26:40 +00001156
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001157/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001158void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1159 NodeTy* Pred,
1160 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001161
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001162 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001163 uint64_t amt;
1164
1165 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001166
Ted Kremenek87e80342008-03-15 03:13:20 +00001167 // FIXME: Add support for VLAs.
1168 if (!T.getTypePtr()->isConstantSizeType())
1169 return;
1170
1171 amt = 1; // Handle sizeof(void)
1172
1173 if (T != getContext().VoidTy)
1174 amt = getContext().getTypeSize(T) / 8;
1175
1176 }
1177 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001178 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001179
Ted Kremenek0e561a32008-03-21 21:30:14 +00001180 MakeNode(Dst, Ex, Pred,
Ted Kremenek0d093d32008-03-10 04:11:42 +00001181 SetRVal(GetState(Pred), Ex,
Ted Kremenek87e80342008-03-15 03:13:20 +00001182 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001183}
1184
Ted Kremenekffe0f432008-03-07 22:58:01 +00001185void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred,
1186 NodeSet& Dst, bool GetLVal) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001187
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001188 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001189
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001190 NodeSet DstTmp;
1191
Ted Kremenek018c15f2008-02-26 03:44:25 +00001192 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001193 DstTmp.Add(Pred);
1194 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001195 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001196
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001197 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001198 ValueState* St = GetState(Pred);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001199 RVal V = GetRVal(St, Ex);
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001200 VisitDeref(U, V, St, Pred, Dst, GetLVal);
1201 }
1202}
1203
1204void GRExprEngine::VisitDeref(Expr* Ex, RVal V, ValueState* St, NodeTy* Pred,
1205 NodeSet& Dst, bool GetLVal) {
1206
1207 // Check for dereferences of undefined values.
1208
1209 if (V.isUndef()) {
1210 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred)) {
1211 Succ->markAsSink();
1212 UndefDeref.insert(Succ);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001213 }
1214
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001215 return;
1216 }
1217
1218 // Check for dereferences of unknown values. Treat as No-Ops.
1219
1220 if (V.isUnknown()) {
1221 Dst.Add(Pred);
1222 return;
1223 }
1224
1225 // After a dereference, one of two possible situations arise:
1226 // (1) A crash, because the pointer was NULL.
1227 // (2) The pointer is not NULL, and the dereference works.
1228 //
1229 // We add these assumptions.
1230
1231 LVal LV = cast<LVal>(V);
1232 bool isFeasibleNotNull;
1233
1234 // "Assume" that the pointer is Not-NULL.
1235
1236 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
1237
1238 if (isFeasibleNotNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001239
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001240 if (GetLVal)
1241 MakeNode(Dst, Ex, Pred, SetRVal(StNotNull, Ex, LV));
1242 else {
1243
1244 // FIXME: Currently symbolic analysis "generates" new symbols
1245 // for the contents of values. We need a better approach.
1246
1247 MakeNode(Dst, Ex, Pred,
1248 SetRVal(StNotNull, Ex, GetRVal(StNotNull, LV, Ex->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001249 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001250 }
1251
1252 bool isFeasibleNull;
1253
1254 // Now "assume" that the pointer is NULL.
1255
1256 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
1257
1258 if (isFeasibleNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001259
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001260 // We don't use "MakeNode" here because the node will be a sink
1261 // and we have no intention of processing it later.
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001262
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001263 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001264
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001265 if (NullNode) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001266
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001267 NullNode->markAsSink();
1268
1269 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1270 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001271 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001272 }
1273}
1274
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001275void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
1276 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001277
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001278 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +00001279
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001280 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001281 assert (U->getOpcode() != UnaryOperator::SizeOf);
1282 assert (U->getOpcode() != UnaryOperator::AlignOf);
1283
1284 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001285
1286 switch (U->getOpcode()) {
1287 case UnaryOperator::PostInc:
1288 case UnaryOperator::PostDec:
1289 case UnaryOperator::PreInc:
1290 case UnaryOperator::PreDec:
1291 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001292 // Evalue subexpression as an LVal.
1293 use_GetLVal = true;
1294 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001295 break;
1296
1297 default:
1298 Visit(U->getSubExpr(), Pred, S1);
1299 break;
1300 }
1301
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001302 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001303
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001304 NodeTy* N1 = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001305 ValueState* St = GetState(N1);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001306
1307 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
1308 GetRVal(St, U->getSubExpr());
1309
1310 if (SubV.isUnknown()) {
1311 Dst.Add(N1);
1312 continue;
1313 }
1314
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001315 if (SubV.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001316 MakeNode(Dst, U, N1, SetRVal(St, U, SubV));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001317 continue;
1318 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001319
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001320 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001321
1322 // Handle ++ and -- (both pre- and post-increment).
1323
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001324 LVal SubLV = cast<LVal>(SubV);
1325 RVal V = GetRVal(St, SubLV, U->getType());
1326
Ted Kremenek89063af2008-02-21 19:15:37 +00001327 if (V.isUnknown()) {
1328 Dst.Add(N1);
1329 continue;
1330 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001331
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001332 // Propagate undefined values.
1333 if (V.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +00001334 MakeNode(Dst, U, N1, SetRVal(St, U, V));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001335 continue;
1336 }
1337
Ted Kremenek443003b2008-02-21 19:29:23 +00001338 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001339
1340 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1341 : BinaryOperator::Sub;
1342
Ted Kremenek443003b2008-02-21 19:29:23 +00001343 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001344
1345 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001346 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001347 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001348 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001349
Ted Kremenek0e561a32008-03-21 21:30:14 +00001350 MakeNode(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001351 continue;
1352 }
1353
1354 // Handle all other unary operators.
1355
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001356 switch (U->getOpcode()) {
Ted Kremenek5139c782008-03-15 03:05:30 +00001357
1358 case UnaryOperator::Extension:
1359 St = SetRVal(St, U, SubV);
1360 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001361
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001362 case UnaryOperator::Minus:
1363 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001364 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +00001365
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001366 case UnaryOperator::Not:
1367 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +00001368 break;
Ted Kremenek90e42032008-02-20 04:12:31 +00001369
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001370 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +00001371
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001372 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1373 //
1374 // Note: technically we do "E == 0", but this is the same in the
1375 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001376
1377 if (isa<LVal>(SubV)) {
Ted Kremenek240f1f02008-03-07 20:13:31 +00001378 lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001379 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
1380 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001381 }
1382 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +00001383 Expr* Ex = U->getSubExpr();
Ted Kremenek240f1f02008-03-07 20:13:31 +00001384 nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001385 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
1386 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001387 }
1388
1389 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +00001390
Ted Kremenek64924852008-01-31 02:35:41 +00001391 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001392 assert (isa<LVal>(SubV));
1393 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +00001394 break;
1395 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001396
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001397 default: ;
1398 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001399 }
1400
Ted Kremenek0e561a32008-03-21 21:30:14 +00001401 MakeNode(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001402 }
1403}
1404
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001405void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
1406 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001407
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001408 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001409
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001410 // FIXME: Add support for VLAs.
1411 if (!T.getTypePtr()->isConstantSizeType())
1412 return;
1413
Chris Lattner98be4942008-03-05 18:54:05 +00001414 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001415 ValueState* St = GetState(Pred);
Ted Kremenek240f1f02008-03-07 20:13:31 +00001416 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001417
Ted Kremenek0e561a32008-03-21 21:30:14 +00001418 MakeNode(Dst, U, Pred, St);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001419}
1420
1421void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek2ad88682008-02-27 07:04:16 +00001422
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001423 Ex = Ex->IgnoreParens();
1424
Ted Kremenek2ad88682008-02-27 07:04:16 +00001425 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
1426 Dst.Add(Pred);
1427 return;
1428 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001429
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001430 switch (Ex->getStmtClass()) {
1431 default:
1432 break;
1433
1434 case Stmt::DeclRefExprClass:
1435 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001436 return;
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001437
1438 case Stmt::UnaryOperatorClass: {
1439 UnaryOperator* U = cast<UnaryOperator>(Ex);
1440
1441 if (U->getOpcode() == UnaryOperator::Deref) {
1442 VisitDeref(U, Pred, Dst, true);
1443 return;
1444 }
1445
1446 break;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001447 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001448
1449 case Stmt::MemberExprClass:
1450 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
1451 return;
1452 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001453
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001454 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001455}
1456
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001457void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1458 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1459}
1460
1461void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1462 AsmStmt::outputs_iterator I,
1463 AsmStmt::outputs_iterator E,
1464 NodeTy* Pred, NodeSet& Dst) {
1465 if (I == E) {
1466 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1467 return;
1468 }
1469
1470 NodeSet Tmp;
1471 VisitLVal(*I, Pred, Tmp);
1472
1473 ++I;
1474
1475 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1476 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1477}
1478
1479void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1480 AsmStmt::inputs_iterator I,
1481 AsmStmt::inputs_iterator E,
1482 NodeTy* Pred, NodeSet& Dst) {
1483 if (I == E) {
1484
1485 // We have processed both the inputs and the outputs. All of the outputs
1486 // should evaluate to LVals. Nuke all of their values.
1487
1488 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1489 // which interprets the inline asm and stores proper results in the
1490 // outputs.
1491
1492 ValueState* St = GetState(Pred);
1493
1494 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1495 OE = A->end_outputs(); OI != OE; ++OI) {
1496
1497 RVal X = GetLVal(St, *OI);
1498
1499 assert (!isa<NonLVal>(X));
1500
1501 if (isa<LVal>(X))
1502 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1503 }
1504
Ted Kremenek0e561a32008-03-21 21:30:14 +00001505 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001506 return;
1507 }
1508
1509 NodeSet Tmp;
1510 Visit(*I, Pred, Tmp);
1511
1512 ++I;
1513
1514 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1515 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1516}
1517
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001518void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1519 assert (Builder && "GRStmtNodeBuilder must be defined.");
1520
1521 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001522
1523 SaveAndRestore<bool> OldSink(Builder->BuildSinks),
1524 OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001525
Ted Kremenekb0533962008-04-18 20:35:30 +00001526 Builder->HasGeneratedNode = false;
1527
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001528 TF->EvalReturn(Dst, *this, *Builder, S, Pred);
1529
Ted Kremenekb0533962008-04-18 20:35:30 +00001530 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001531
Ted Kremenekb0533962008-04-18 20:35:30 +00001532 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001533 MakeNode(Dst, S, Pred, GetState(Pred));
1534}
1535
Ted Kremenek02737ed2008-03-31 15:02:58 +00001536void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1537
1538 Expr* R = S->getRetValue();
1539
1540 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001541 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001542 return;
1543 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001544
1545 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001546 QualType T = R->getType();
1547
Chris Lattner423a3c92008-04-02 17:45:06 +00001548 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001549
1550 // Check if any of the return values return the address of a stack variable.
1551
1552 NodeSet Tmp;
1553 Visit(R, Pred, Tmp);
1554
1555 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1556 RVal X = GetRVal((*I)->getState(), R);
1557
1558 if (isa<lval::DeclVal>(X)) {
1559
1560 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1561
1562 // Create a special node representing the v
1563
1564 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1565
1566 if (RetStackNode) {
1567 RetStackNode->markAsSink();
1568 RetsStackAddr.insert(RetStackNode);
1569 }
1570
1571 continue;
1572 }
1573 }
1574
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001575 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001576 }
1577 }
1578 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001579 Visit(R, Pred, DstRet);
1580
1581 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1582 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001583}
Ted Kremenek55deb972008-03-25 00:34:37 +00001584
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001585//===----------------------------------------------------------------------===//
1586// Transfer functions: Binary operators.
1587//===----------------------------------------------------------------------===//
1588
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001589void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001590 GRExprEngine::NodeTy* Pred,
1591 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001592 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001593
1594 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001595 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001596 else
1597 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001598
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001599 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001600
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001601 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00001602
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001603 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001604 // In such cases, we want to (initially) treat the LHS as an LVal,
1605 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
1606 // evaluated to LValDecl's instead of to an NonLVal.
1607
Ted Kremenek0d093d32008-03-10 04:11:42 +00001608 RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS())
1609 : GetRVal(GetState(N1), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001610
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001611 // Visit the RHS...
1612
1613 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001614 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001615
1616 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001617
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001618 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001619
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001620 NodeTy* N2 = *I2;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001621 ValueState* St = GetState(N2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001622 Expr* RHS = B->getRHS();
1623 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001624
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001625 BinaryOperator::Opcode Op = B->getOpcode();
1626
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001627 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1628 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +00001629
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001630 // Check if the denominator is undefined.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001631
Ted Kremenek00155412008-02-26 22:27:51 +00001632 if (!RightV.isUnknown()) {
1633
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001634 if (RightV.isUndef()) {
1635 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenek00155412008-02-26 22:27:51 +00001636
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001637 if (DivUndef) {
1638 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001639 ExplicitBadDivides.insert(DivUndef);
Ted Kremenek00155412008-02-26 22:27:51 +00001640 }
1641
1642 continue;
1643 }
1644
1645 // Check for divide/remainder-by-zero.
1646 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001647 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001648
Ted Kremenek4d839b42008-03-07 19:04:53 +00001649 bool isFeasibleZero = false;
1650 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001651
Ted Kremenek00155412008-02-26 22:27:51 +00001652 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001653
Ted Kremenek4d839b42008-03-07 19:04:53 +00001654 bool isFeasibleNotZero = false;
1655 St = Assume(St, RightV, true, isFeasibleNotZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001656
Ted Kremenek4d839b42008-03-07 19:04:53 +00001657 // Create the node for the divide-by-zero (if it occurred).
1658
1659 if (isFeasibleZero)
1660 if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) {
1661 DivZeroNode->markAsSink();
1662
1663 if (isFeasibleNotZero)
1664 ImplicitBadDivides.insert(DivZeroNode);
1665 else
1666 ExplicitBadDivides.insert(DivZeroNode);
1667
1668 }
1669
1670 if (!isFeasibleNotZero)
Ted Kremenek00155412008-02-26 22:27:51 +00001671 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001672 }
1673
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001674 // Fall-through. The logic below processes the divide.
1675 }
1676
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001677
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001678 if (Op <= BinaryOperator::Or) {
1679
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001680 // Process non-assignements except commas or short-circuited
1681 // logical expressions (LAnd and LOr).
1682
1683 RVal Result = EvalBinOp(Op, LeftV, RightV);
1684
1685 if (Result.isUnknown()) {
1686 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001687 continue;
1688 }
1689
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001690 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
1691
1692 // The operands were not undefined, but the result is undefined.
1693
1694 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1695 UndefNode->markAsSink();
1696 UndefResults.insert(UndefNode);
1697 }
1698
1699 continue;
1700 }
1701
Ted Kremenek0e561a32008-03-21 21:30:14 +00001702 MakeNode(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001703 continue;
1704 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001705
1706 // Process assignments.
1707
1708 switch (Op) {
1709
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001710 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001711
1712 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001713
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001714 if (LeftV.isUndef()) {
1715 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001716 continue;
1717 }
1718
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001719 // EXPERIMENTAL: "Conjured" symbols.
1720
1721 if (RightV.isUnknown()) {
1722 unsigned Count = Builder->getCurrentBlockCount();
1723 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
1724
1725 RightV = B->getRHS()->getType()->isPointerType()
1726 ? cast<RVal>(lval::SymbolVal(Sym))
1727 : cast<RVal>(nonlval::SymbolVal(Sym));
1728 }
1729
Ted Kremenek361fa8e2008-03-12 21:45:47 +00001730 // Simulate the effects of a "store": bind the value of the RHS
1731 // to the L-Value represented by the LHS.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001732
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001733 EvalStore(Dst, B, N2, SetRVal(St, B, RightV),
Ted Kremenek13922612008-04-16 20:40:59 +00001734 LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00001735
Ted Kremeneke38718e2008-04-16 18:21:25 +00001736 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001737 }
1738
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001739 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001740
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001741 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001742
Ted Kremenek00155412008-02-26 22:27:51 +00001743 assert (B->isCompoundAssignmentOp());
1744
1745 if (Op >= BinaryOperator::AndAssign)
1746 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1747 else
1748 ((int&) Op) -= BinaryOperator::MulAssign;
1749
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001750 // Check if the LHS is undefined.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001751
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001752 if (LeftV.isUndef()) {
1753 HandleUndefinedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001754 continue;
1755 }
1756
1757 if (LeftV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001758 assert (isa<UnknownVal>(GetRVal(St, B)));
1759 Dst.Add(N2);
1760 continue;
Ted Kremenek9dca0622008-02-19 00:22:37 +00001761 }
1762
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001763 // At this pointer we know that the LHS evaluates to an LVal
Ted Kremenek3ce92142008-03-09 08:12:37 +00001764 // that is neither "Unknown" or "Undefined."
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001765
1766 LVal LeftLV = cast<LVal>(LeftV);
1767
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001768 // Fetch the value of the LHS (the value of the variable, etc.).
1769
Ted Kremenek0d093d32008-03-10 04:11:42 +00001770 RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001771
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001772 // Propagate undefined value (left-side). We
1773 // propogate undefined values for the RHS below when
Ted Kremenek00155412008-02-26 22:27:51 +00001774 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001775
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001776 if (V.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001777 St = SetRVal(St, B, V);
1778 break;
1779 }
1780
1781 // Propagate unknown values.
1782
Ted Kremenek89063af2008-02-21 19:15:37 +00001783 if (V.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001784 // The value bound to LeftV is unknown. Thus we just
1785 // propagate the current node (as "B" is already bound to nothing).
1786 assert (isa<UnknownVal>(GetRVal(St, B)));
Ted Kremenek89063af2008-02-21 19:15:37 +00001787 Dst.Add(N2);
1788 continue;
1789 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001790
1791 if (RightV.isUnknown()) {
Ted Kremenek3ce92142008-03-09 08:12:37 +00001792 assert (isa<UnknownVal>(GetRVal(St, B)));
1793 St = SetRVal(St, LeftLV, UnknownVal());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001794 break;
1795 }
1796
Ted Kremenek00155412008-02-26 22:27:51 +00001797 // At this point:
1798 //
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001799 // The LHS is not Undef/Unknown.
Ted Kremenek00155412008-02-26 22:27:51 +00001800 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001801
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001802 // Get the computation type.
1803 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1804
1805 // Perform promotions.
1806 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001807 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001808
1809 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001810
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001811 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1812 && RHS->getType()->isIntegerType()) {
1813
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001814 // Check if the denominator is undefined.
Ted Kremenekd763eb92008-02-26 02:15:56 +00001815
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001816 if (RightV.isUndef()) {
1817 NodeTy* DivUndef = Builder->generateNode(B, St, N2);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001818
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001819 if (DivUndef) {
1820 DivUndef->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001821 ExplicitBadDivides.insert(DivUndef);
Ted Kremenekd763eb92008-02-26 02:15:56 +00001822 }
1823
1824 continue;
1825 }
Ted Kremenek00155412008-02-26 22:27:51 +00001826
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001827 // First, "assume" that the denominator is 0.
1828
Ted Kremenek4d839b42008-03-07 19:04:53 +00001829 bool isFeasibleZero = false;
1830 ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001831
Ted Kremenek4d839b42008-03-07 19:04:53 +00001832 // Second, "assume" that the denominator cannot be 0.
1833
1834 bool isFeasibleNotZero = false;
1835 St = Assume(St, RightV, true, isFeasibleNotZero);
1836
1837 // Create the node for the divide-by-zero error (if it occurred).
1838
1839 if (isFeasibleZero) {
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001840 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1841
1842 if (DivZeroNode) {
1843 DivZeroNode->markAsSink();
Ted Kremenek4d839b42008-03-07 19:04:53 +00001844
1845 if (isFeasibleNotZero)
1846 ImplicitBadDivides.insert(DivZeroNode);
1847 else
1848 ExplicitBadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001849 }
1850 }
1851
Ted Kremenek4d839b42008-03-07 19:04:53 +00001852 if (!isFeasibleNotZero)
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001853 continue;
1854
1855 // Fall-through. The logic below processes the divide.
1856 }
Ted Kremenek00155412008-02-26 22:27:51 +00001857 else {
1858
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001859 // Propagate undefined values (right-side).
Ted Kremenek00155412008-02-26 22:27:51 +00001860
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001861 if (RightV.isUndef()) {
Ted Kremenek00155412008-02-26 22:27:51 +00001862 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1863 break;
1864 }
1865
1866 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001867
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001868 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00001869
1870 if (Result.isUndef()) {
1871
1872 // The operands were not undefined, but the result is undefined.
1873
1874 if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) {
1875 UndefNode->markAsSink();
1876 UndefResults.insert(UndefNode);
1877 }
1878
1879 continue;
1880 }
1881
Ted Kremeneke38718e2008-04-16 18:21:25 +00001882 // St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001883 EvalStore(Dst, B, N2, SetRVal(St, B, Result), LeftLV, Result);
Ted Kremeneke38718e2008-04-16 18:21:25 +00001884 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001885 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001886 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001887
Ted Kremenek0e561a32008-03-21 21:30:14 +00001888 MakeNode(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001889 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001890 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001891}
Ted Kremenekee985462008-01-16 18:18:48 +00001892
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001893void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek0d093d32008-03-10 04:11:42 +00001894 NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001895 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +00001896 UndefStores.insert(N);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001897}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001898
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001899
Ted Kremenekee985462008-01-16 18:18:48 +00001900//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001901// "Assume" logic.
1902//===----------------------------------------------------------------------===//
1903
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001904ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00001905 bool Assumption, bool& isFeasible) {
1906
1907 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001908
1909 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
1910 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00001911}
1912
1913ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond,
1914 bool Assumption, bool& isFeasible) {
1915
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001916 switch (Cond.getSubKind()) {
1917 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001918 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001919 return St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00001920
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001921 case lval::SymbolValKind:
1922 if (Assumption)
1923 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001924 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001925 else
1926 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00001927 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek550a0f92008-04-18 17:20:23 +00001928
1929
Ted Kremenek329f8542008-02-05 21:52:21 +00001930 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001931 case lval::FuncValKind:
1932 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001933 isFeasible = Assumption;
1934 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001935
Ted Kremenek329f8542008-02-05 21:52:21 +00001936 case lval::ConcreteIntKind: {
1937 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001938 isFeasible = b ? Assumption : !Assumption;
1939 return St;
1940 }
1941 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001942}
1943
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001944ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00001945 bool Assumption, bool& isFeasible) {
1946
1947 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001948
1949 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
1950 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00001951}
1952
1953ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond,
1954 bool Assumption, bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001955 switch (Cond.getSubKind()) {
1956 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001957 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001958 return St;
1959
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001960
1961 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001962 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001963 SymbolID sym = SV.getSymbol();
1964
1965 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00001966 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001967 isFeasible);
1968 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00001969 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001970 isFeasible);
1971 }
1972
Ted Kremenek08b66252008-02-06 04:31:33 +00001973 case nonlval::SymIntConstraintValKind:
1974 return
1975 AssumeSymInt(St, Assumption,
1976 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1977 isFeasible);
1978
Ted Kremenek329f8542008-02-05 21:52:21 +00001979 case nonlval::ConcreteIntKind: {
1980 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001981 isFeasible = b ? Assumption : !Assumption;
1982 return St;
1983 }
1984 }
1985}
1986
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001987ValueState*
1988GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001989 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001990
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001991 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001992 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001993 isFeasible = *X != V;
1994 return St;
1995 }
1996
1997 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001998 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001999 isFeasible = true;
2000 return St;
2001 }
2002
2003 // If we reach here, sym is not a constant and we don't know if it is != V.
2004 // Make that assumption.
2005
2006 isFeasible = true;
2007 return StateMgr.AddNE(St, sym, V);
2008}
2009
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002010ValueState*
2011GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002012 const llvm::APSInt& V, bool& isFeasible) {
2013
2014 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002015 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002016 isFeasible = *X == V;
2017 return St;
2018 }
2019
2020 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002021 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002022 isFeasible = false;
2023 return St;
2024 }
2025
2026 // If we reach here, sym is not a constant and we don't know if it is == V.
2027 // Make that assumption.
2028
2029 isFeasible = true;
2030 return StateMgr.AddEQ(St, sym, V);
2031}
Ted Kremenekb38911f2008-01-30 23:03:39 +00002032
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002033ValueState*
2034GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00002035 const SymIntConstraint& C, bool& isFeasible) {
2036
2037 switch (C.getOpcode()) {
2038 default:
2039 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002040 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00002041 return St;
2042
2043 case BinaryOperator::EQ:
2044 if (Assumption)
2045 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2046 else
2047 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2048
2049 case BinaryOperator::NE:
2050 if (Assumption)
2051 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2052 else
2053 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2054 }
2055}
2056
Ted Kremenekb38911f2008-01-30 23:03:39 +00002057//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002058// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002059//===----------------------------------------------------------------------===//
2060
Ted Kremenekaa66a322008-01-16 21:46:15 +00002061#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002062static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002063static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002064static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002065
Ted Kremenekaa66a322008-01-16 21:46:15 +00002066namespace llvm {
2067template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002068struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002069 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002070
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002071 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002072
2073 Out << "Variables:\\l";
2074
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002075 bool isFirst = true;
2076
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002077 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002078
2079 if (isFirst)
2080 isFirst = false;
2081 else
2082 Out << "\\l";
2083
2084 Out << ' ' << I.getKey()->getName() << " : ";
2085 I.getData().print(Out);
2086 }
2087
2088 }
2089
Ted Kremeneke7d22112008-02-11 19:21:59 +00002090
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002091 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002092
2093 bool isFirst = true;
2094
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002095 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002096
2097 if (isFirst) {
2098 Out << "\\l\\lSub-Expressions:\\l";
2099 isFirst = false;
2100 }
2101 else
2102 Out << "\\l";
2103
2104 Out << " (" << (void*) I.getKey() << ") ";
2105 I.getKey()->printPretty(Out);
2106 Out << " : ";
2107 I.getData().print(Out);
2108 }
2109 }
2110
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002111 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002112
Ted Kremenek016f52f2008-02-08 21:10:02 +00002113 bool isFirst = true;
2114
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002115 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002116 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002117 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002118 isFirst = false;
2119 }
2120 else
2121 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002122
Ted Kremeneke7d22112008-02-11 19:21:59 +00002123 Out << " (" << (void*) I.getKey() << ") ";
2124 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002125 Out << " : ";
2126 I.getData().print(Out);
2127 }
2128 }
2129
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002130 static void PrintEQ(std::ostream& Out, ValueState* St) {
2131 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002132
2133 if (CE.isEmpty())
2134 return;
2135
2136 Out << "\\l\\|'==' constraints:";
2137
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002138 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002139 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2140 }
2141
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002142 static void PrintNE(std::ostream& Out, ValueState* St) {
2143 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002144
2145 if (NE.isEmpty())
2146 return;
2147
2148 Out << "\\l\\|'!=' constraints:";
2149
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002150 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002151 I != EI; ++I){
2152
2153 Out << "\\l $" << I.getKey() << " : ";
2154 bool isFirst = true;
2155
2156 ValueState::IntSetTy::iterator J=I.getData().begin(),
2157 EJ=I.getData().end();
2158 for ( ; J != EJ; ++J) {
2159 if (isFirst) isFirst = false;
2160 else Out << ", ";
2161
2162 Out << (*J)->toString();
2163 }
2164 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002165 }
2166
2167 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2168
2169 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002170 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002171 GraphPrintCheckerState->isUndefDeref(N) ||
2172 GraphPrintCheckerState->isUndefStore(N) ||
2173 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002174 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2175 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002176 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002177 GraphPrintCheckerState->isBadCall(N) ||
2178 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002179 return "color=\"red\",style=\"filled\"";
2180
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002181 if (GraphPrintCheckerState->isNoReturnCall(N))
2182 return "color=\"blue\",style=\"filled\"";
2183
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002184 return "";
2185 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002186
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002187 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002188 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002189
2190 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002191 ProgramPoint Loc = N->getLocation();
2192
2193 switch (Loc.getKind()) {
2194 case ProgramPoint::BlockEntranceKind:
2195 Out << "Block Entrance: B"
2196 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2197 break;
2198
2199 case ProgramPoint::BlockExitKind:
2200 assert (false);
2201 break;
2202
2203 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002204 const PostStmt& L = cast<PostStmt>(Loc);
2205 Stmt* S = L.getStmt();
2206 SourceLocation SLoc = S->getLocStart();
2207
2208 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2209 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002210
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002211 if (SLoc.isFileID()) {
2212 Out << "\\lline="
2213 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2214 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2215 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002216
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002217 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002218 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002219 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002220 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002221 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002222 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002223 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002224 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002225 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2226 Out << "\\|Explicit divide-by zero or undefined value.";
2227 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2228 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002229 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002230 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002231 else if (GraphPrintCheckerState->isNoReturnCall(N))
2232 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002233 else if (GraphPrintCheckerState->isBadCall(N))
2234 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002235 else if (GraphPrintCheckerState->isUndefArg(N))
2236 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002237
Ted Kremenekaa66a322008-01-16 21:46:15 +00002238 break;
2239 }
2240
2241 default: {
2242 const BlockEdge& E = cast<BlockEdge>(Loc);
2243 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2244 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002245
2246 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002247
2248 SourceLocation SLoc = T->getLocStart();
2249
Ted Kremenekb38911f2008-01-30 23:03:39 +00002250 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002251
Ted Kremenekb38911f2008-01-30 23:03:39 +00002252 E.getSrc()->printTerminator(Out);
2253
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002254 if (SLoc.isFileID()) {
2255 Out << "\\lline="
2256 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2257 << GraphPrintSourceManager->getColumnNumber(SLoc);
2258 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002259
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002260 if (isa<SwitchStmt>(T)) {
2261 Stmt* Label = E.getDst()->getLabel();
2262
2263 if (Label) {
2264 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2265 Out << "\\lcase ";
2266 C->getLHS()->printPretty(Out);
2267
2268 if (Stmt* RHS = C->getRHS()) {
2269 Out << " .. ";
2270 RHS->printPretty(Out);
2271 }
2272
2273 Out << ":";
2274 }
2275 else {
2276 assert (isa<DefaultStmt>(Label));
2277 Out << "\\ldefault:";
2278 }
2279 }
2280 else
2281 Out << "\\l(implicit) default:";
2282 }
2283 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002284 // FIXME
2285 }
2286 else {
2287 Out << "\\lCondition: ";
2288 if (*E.getSrc()->succ_begin() == E.getDst())
2289 Out << "true";
2290 else
2291 Out << "false";
2292 }
2293
2294 Out << "\\l";
2295 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002296
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002297 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2298 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002299 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002300 }
2301 }
2302
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002303 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002304
Ted Kremenek75da3e82008-03-11 19:02:40 +00002305 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002306
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002307 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002308 return Out.str();
2309 }
2310};
2311} // end llvm namespace
2312#endif
2313
Ted Kremenekffe0f432008-03-07 22:58:01 +00002314#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002315
2316template <typename ITERATOR>
2317GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2318
2319template <>
2320GRExprEngine::NodeTy*
2321GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2322 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2323 return I->first;
2324}
2325
Ted Kremenekffe0f432008-03-07 22:58:01 +00002326template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002327static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002328 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002329
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002330 llvm::SmallPtrSet<void*,10> CachedSources;
2331
2332 for ( ; I != E; ++I ) {
2333 GRExprEngine::NodeTy* N = GetGraphNode(I);
2334 void* p = N->getLocation().getRawData();
2335
2336 if (CachedSources.count(p))
2337 continue;
2338
2339 CachedSources.insert(p);
2340
2341 Sources.push_back(N);
2342 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002343}
2344#endif
2345
2346void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002347#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002348 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002349 std::vector<NodeTy*> Src;
2350
2351 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002352 AddSources(Src, null_derefs_begin(), null_derefs_end());
2353 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2354 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2355 AddSources(Src, undef_results_begin(), undef_results_end());
2356 AddSources(Src, bad_calls_begin(), bad_calls_end());
2357 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002358 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002359
Ted Kremenekcb612922008-04-18 19:23:43 +00002360 // The new way.
2361 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2362 (*I)->GetErrorNodes(Src);
2363
2364
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002365 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002366 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002367 else {
2368 GraphPrintCheckerState = this;
2369 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002370 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002371
Ted Kremenekffe0f432008-03-07 22:58:01 +00002372 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002373
2374 GraphPrintCheckerState = NULL;
2375 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002376 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002377 }
2378#endif
2379}
2380
2381void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2382#ifndef NDEBUG
2383 GraphPrintCheckerState = this;
2384 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002385 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002386
Ted Kremenek493d7a22008-03-11 18:25:33 +00002387 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2388
2389 if (!TrimmedG)
2390 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2391 else {
2392 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2393 delete TrimmedG;
2394 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002395
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002396 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002397 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002398 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002399#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002400}