blob: 0c9a1b6df57f42f4990b86e030d735c47b50a4c8 [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 Kremenek1a654b62008-06-20 21:45:25 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek77349cb2008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000019#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000020#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000021
Ted Kremenek0f5f0592008-02-27 06:07:00 +000022#ifndef NDEBUG
23#include "llvm/Support/GraphWriter.h"
24#include <sstream>
25#endif
26
Ted Kremenekb387a3f2008-02-14 22:16:04 +000027using namespace clang;
28using llvm::dyn_cast;
29using llvm::cast;
30using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000031
Ted Kremeneke695e1c2008-04-15 23:06:53 +000032//===----------------------------------------------------------------------===//
33// Engine construction and deletion.
34//===----------------------------------------------------------------------===//
35
Ted Kremeneke448ab42008-05-01 18:33:28 +000036static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
37 IdentifierInfo* II = &Ctx.Idents.get(name);
38 return Ctx.Selectors.getSelector(0, &II);
39}
40
Ted Kremenekdaa497e2008-03-09 18:05:48 +000041
Ted Kremenek8b233612008-07-02 20:13:38 +000042GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
43 LiveVariables& L)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000044 : CoreEngine(cfg, CD, Ctx, *this),
45 G(CoreEngine.getGraph()),
Ted Kremenek1a654b62008-06-20 21:45:25 +000046 Parents(0),
Ted Kremenek8b233612008-07-02 20:13:38 +000047 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000048 Builder(NULL),
49 StateMgr(G.getContext(), G.getAllocator()),
50 BasicVals(StateMgr.getBasicValueFactory()),
51 TF(NULL), // FIXME
52 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +000053 CurrentStmt(NULL),
54 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +000055 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000056
Ted Kremenek1a654b62008-06-20 21:45:25 +000057GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000058 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
59 delete *I;
60
61 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
62 I != E; ++I)
63 delete *I;
64
65 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
66 I != E; ++I)
67 delete *I;
Ted Kremeneke448ab42008-05-01 18:33:28 +000068
69 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek1a654b62008-06-20 21:45:25 +000070
71 delete Parents;
72}
73
74ParentMap& GRExprEngine::getParentMap() {
75 if (!Parents) {
76 Stmt* Body = getGraph().getCodeDecl().getCodeBody();
77 Parents = new ParentMap(Body);
78 }
79
80 return *Parents;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000081}
82
Ted Kremeneke695e1c2008-04-15 23:06:53 +000083//===----------------------------------------------------------------------===//
84// Utility methods.
85//===----------------------------------------------------------------------===//
86
87// SaveAndRestore - A utility class that uses RIIA to save and restore
88// the value of a variable.
89template<typename T>
90struct VISIBILITY_HIDDEN SaveAndRestore {
91 SaveAndRestore(T& x) : X(x), old_value(x) {}
92 ~SaveAndRestore() { X = old_value; }
93 T get() { return old_value; }
94
95 T& X;
96 T old_value;
97};
98
Ted Kremenek186350f2008-04-23 20:12:28 +000099// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
100// value of a variable is saved, and during the dstor the old value is
101// or'ed with the new value.
102struct VISIBILITY_HIDDEN SaveOr {
103 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
104 ~SaveOr() { X |= old_value; }
105
106 bool& X;
107 bool old_value;
108};
109
110
Ted Kremenekc0959972008-07-02 21:24:01 +0000111void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000112 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000113 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000114 (*I)->EmitWarnings(BR);
115 }
116
117 for (SimpleChecksTy::iterator I = CallChecks.begin(), E = CallChecks.end();
118 I != E; ++I) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000119 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120 (*I)->EmitWarnings(BR);
121 }
122
123 for (SimpleChecksTy::iterator I=MsgExprChecks.begin(), E=MsgExprChecks.end();
124 I != E; ++I) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000125 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 (*I)->EmitWarnings(BR);
127 }
128}
129
130void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
131 TF = tf;
132 TF->RegisterChecks(*this);
133}
134
135void GRExprEngine::AddCallCheck(GRSimpleAPICheck* A) {
136 CallChecks.push_back(A);
137}
138
139void GRExprEngine::AddObjCMessageExprCheck(GRSimpleAPICheck* A) {
140 MsgExprChecks.push_back(A);
141}
142
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000143ValueState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000144
145 // The LiveVariables information already has a compilation of all VarDecls
146 // used in the function. Iterate through this set, and "symbolicate"
147 // any VarDecl whose value originally comes from outside the function.
148
149 typedef LiveVariables::AnalysisDataTy LVDataTy;
150 LVDataTy& D = Liveness.getAnalysisData();
151
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000152 ValueState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000153
154 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
155
Chris Lattner41110242008-06-17 18:05:57 +0000156 ScopedDecl *SD = const_cast<ScopedDecl*>(I->first);
157 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
158 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
159 RVal X = RVal::GetSymbolValue(SymMgr, VD);
160 StateMgr.BindVar(StateImpl, VD, X);
161 }
162 } else if (ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(SD)) {
163 RVal X = RVal::GetSymbolValue(SymMgr, IPD);
164 StateMgr.BindVar(StateImpl, IPD, X);
Ted Kremenek07932632008-02-27 06:47:26 +0000165 }
Chris Lattner41110242008-06-17 18:05:57 +0000166
167
Ted Kremenek07932632008-02-27 06:47:26 +0000168 }
169
170 return StateMgr.getPersistentState(StateImpl);
171}
172
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000173ValueState* GRExprEngine::SetRVal(ValueState* St, Expr* Ex, RVal V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000174
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000175 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000176
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000177 if (Ex == CurrentStmt) {
178 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000179
180 if (!isBlkExpr)
181 return St;
182 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000183
Ted Kremenek436f2b92008-04-30 04:23:07 +0000184 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, true);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000185}
186
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000187//===----------------------------------------------------------------------===//
188// Top-level transfer function logic (Dispatcher).
189//===----------------------------------------------------------------------===//
190
191void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
192
193 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000194 EntryNode = builder.getLastNode();
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000195 CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196
197 // Set up our simple checks.
198
199 // FIXME: This can probably be installed directly in GRCoreEngine, obviating
200 // the need to do a copy every time we hit a block-level statement.
201
202 if (!MsgExprChecks.empty())
203 Builder->setObjCMsgExprAuditors((GRAuditor<ValueState>**) &MsgExprChecks[0],
204 (GRAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size()));
205
206
207 if (!CallChecks.empty())
208 Builder->setCallExprAuditors((GRAuditor<ValueState>**) &CallChecks[0],
209 (GRAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size()));
210
211 // Create the cleaned state.
212
Ted Kremenek846d4e92008-04-24 23:35:58 +0000213 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
214 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000215
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000216 // Process any special transfer function for dead symbols.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000217
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek910e9992008-04-25 01:25:15 +0000229 TF->EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
261 CurrentStmt = NULL;
262 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263}
264
265void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
266
267 // FIXME: add metadata to the CFG so that we can disable
268 // this check when we KNOW that there is no block-level subexpression.
269 // The motivation is that this check requires a hashtable lookup.
270
271 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
272 Dst.Add(Pred);
273 return;
274 }
275
276 switch (S->getStmtClass()) {
277
278 default:
279 // Cases we intentionally have "default" handle:
280 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
281
282 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
283 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000284
285 case Stmt::ArraySubscriptExprClass:
286 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
287 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000288
289 case Stmt::AsmStmtClass:
290 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
291 break;
292
293 case Stmt::BinaryOperatorClass: {
294 BinaryOperator* B = cast<BinaryOperator>(S);
295
296 if (B->isLogicalOp()) {
297 VisitLogicalExpr(B, Pred, Dst);
298 break;
299 }
300 else if (B->getOpcode() == BinaryOperator::Comma) {
301 ValueState* St = GetState(Pred);
302 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
303 break;
304 }
305
306 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
307 break;
308 }
309
310 case Stmt::CallExprClass: {
311 CallExpr* C = cast<CallExpr>(S);
312 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
313 break;
314 }
315
316 case Stmt::CastExprClass: {
317 CastExpr* C = cast<CastExpr>(S);
318 VisitCast(C, C->getSubExpr(), Pred, Dst);
319 break;
320 }
321
322 // FIXME: ChooseExpr is really a constant. We need to fix
323 // the CFG do not model them as explicit control-flow.
324
325 case Stmt::ChooseExprClass: { // __builtin_choose_expr
326 ChooseExpr* C = cast<ChooseExpr>(S);
327 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
328 break;
329 }
330
331 case Stmt::CompoundAssignOperatorClass:
332 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
333 break;
334
335 case Stmt::ConditionalOperatorClass: { // '?' operator
336 ConditionalOperator* C = cast<ConditionalOperator>(S);
337 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
338 break;
339 }
340
341 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000342 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000343 break;
344
345 case Stmt::DeclStmtClass:
346 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
347 break;
348
349 case Stmt::ImplicitCastExprClass: {
350 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
351 VisitCast(C, C->getSubExpr(), Pred, Dst);
352 break;
353 }
354
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000355 case Stmt::MemberExprClass: {
356 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
357 break;
358 }
359
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 case Stmt::ObjCMessageExprClass: {
361 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
362 break;
363 }
364
365 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000366 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367 break;
368
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000369 case Stmt::ReturnStmtClass:
370 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
371 break;
372
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000373 case Stmt::SizeOfAlignOfTypeExprClass:
374 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
375 break;
376
377 case Stmt::StmtExprClass: {
378 StmtExpr* SE = cast<StmtExpr>(S);
379
380 ValueState* St = GetState(Pred);
381
382 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
383 // probably just remove these from the CFG.
384 assert (!SE->getSubStmt()->body_empty());
385
386 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
387 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
388 else
389 Dst.Add(Pred);
390
391 break;
392 }
393
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000394 case Stmt::UnaryOperatorClass:
395 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000397 }
398}
399
400void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
401
402 Ex = Ex->IgnoreParens();
403
404 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
405 Dst.Add(Pred);
406 return;
407 }
408
409 switch (Ex->getStmtClass()) {
410 default:
411 Visit(Ex, Pred, Dst);
412 return;
413
414 case Stmt::ArraySubscriptExprClass:
415 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
416 return;
417
418 case Stmt::DeclRefExprClass:
419 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
420 return;
421
422 case Stmt::UnaryOperatorClass:
423 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
424 return;
425
426 case Stmt::MemberExprClass:
427 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
428 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000429 }
430}
431
432//===----------------------------------------------------------------------===//
433// Block entrance. (Update counters).
434//===----------------------------------------------------------------------===//
435
436bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, ValueState*,
437 GRBlockCounter BC) {
438
439 return BC.getNumVisited(B->getBlockID()) < 3;
440}
441
442//===----------------------------------------------------------------------===//
443// Branch processing.
444//===----------------------------------------------------------------------===//
445
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000446ValueState* GRExprEngine::MarkBranch(ValueState* St, Stmt* Terminator,
447 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000448
449 switch (Terminator->getStmtClass()) {
450 default:
451 return St;
452
453 case Stmt::BinaryOperatorClass: { // '&&' and '||'
454
455 BinaryOperator* B = cast<BinaryOperator>(Terminator);
456 BinaryOperator::Opcode Op = B->getOpcode();
457
458 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
459
460 // For &&, if we take the true branch, then the value of the whole
461 // expression is that of the RHS expression.
462 //
463 // For ||, if we take the false branch, then the value of the whole
464 // expression is that of the RHS expression.
465
466 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
467 (Op == BinaryOperator::LOr && !branchTaken)
468 ? B->getRHS() : B->getLHS();
469
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000470 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000471 }
472
473 case Stmt::ConditionalOperatorClass: { // ?:
474
475 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
476
477 // For ?, if branchTaken == true then the value is either the LHS or
478 // the condition itself. (GNU extension).
479
480 Expr* Ex;
481
482 if (branchTaken)
483 Ex = C->getLHS() ? C->getLHS() : C->getCond();
484 else
485 Ex = C->getRHS();
486
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000487 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000488 }
489
490 case Stmt::ChooseExprClass: { // ?:
491
492 ChooseExpr* C = cast<ChooseExpr>(Terminator);
493
494 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000495 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000496 }
497 }
498}
499
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000500void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000501 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000502
Ted Kremeneke7d22112008-02-11 19:21:59 +0000503 // Remove old bindings for subexpressions.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000504 ValueState* PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000505
Ted Kremenekb2331832008-02-15 22:29:00 +0000506 // Check for NULL conditions; e.g. "for(;;)"
507 if (!Condition) {
508 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000509 return;
510 }
511
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000512 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000513
514 switch (V.getBaseKind()) {
515 default:
516 break;
517
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000518 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000519 builder.generateNode(MarkBranch(PrevState, Term, true), true);
520 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000521 return;
522
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000523 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000524 NodeTy* N = builder.generateNode(PrevState, true);
525
526 if (N) {
527 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000528 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000529 }
530
531 builder.markInfeasible(false);
532 return;
533 }
534 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000535
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000536 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000537
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000538 bool isFeasible = false;
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000539 ValueState* St = Assume(PrevState, V, true, isFeasible);
540
541 if (isFeasible)
542 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000543 else
544 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000545
546 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000547
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000548 isFeasible = false;
549 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000550
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000551 if (isFeasible)
552 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000553 else
554 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000555}
556
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000557/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000558/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000559void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000560
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000561 ValueState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000562 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000563
564 // Three possibilities:
565 //
566 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000567 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000568 // (3) We have no clue about the label. Dispatch to all targets.
569 //
570
571 typedef IndirectGotoNodeBuilder::iterator iterator;
572
573 if (isa<lval::GotoLabel>(V)) {
574 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
575
576 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000577 if (I.getLabel() == L) {
578 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000579 return;
580 }
581 }
582
583 assert (false && "No block with label.");
584 return;
585 }
586
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000587 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000588 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000589 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000590 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000591 return;
592 }
593
594 // This is really a catch-all. We don't support symbolics yet.
595
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000596 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000597
598 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000599 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000600}
Ted Kremenekf233d482008-02-05 00:26:40 +0000601
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000602
603void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
604 NodeTy* Pred, NodeSet& Dst) {
605
606 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
607
608 ValueState* St = GetState(Pred);
609 RVal X = GetBlkExprRVal(St, Ex);
610
611 assert (X.isUndef());
612
613 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
614
615 assert (SE);
616
617 X = GetBlkExprRVal(St, SE);
618
619 // Make sure that we invalidate the previous binding.
620 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
621}
622
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000623/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
624/// nodes by processing the 'effects' of a switch statement.
625void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
626
627 typedef SwitchNodeBuilder::iterator iterator;
628
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000629 ValueState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000630 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000631 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000632
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000633 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000634 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000635 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000636 return;
637 }
638
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000639 ValueState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000640
641 // While most of this can be assumed (such as the signedness), having it
642 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000643
Chris Lattner98be4942008-03-05 18:54:05 +0000644 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000645
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000646 APSInt V1(bits, false);
647 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000648 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000649
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000650 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000651
652 CaseStmt* Case = cast<CaseStmt>(I.getCase());
653
654 // Evaluate the case.
655 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
656 assert (false && "Case condition must evaluate to an integer constant.");
657 return;
658 }
659
660 // Get the RHS of the case, if it exists.
661
662 if (Expr* E = Case->getRHS()) {
663 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
664 assert (false &&
665 "Case condition (RHS) must evaluate to an integer constant.");
666 return ;
667 }
668
669 assert (V1 <= V2);
670 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000671 else
672 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000673
674 // FIXME: Eventually we should replace the logic below with a range
675 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000676 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000677
Ted Kremenek14a11402008-03-17 22:17:56 +0000678 do {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000679 nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000680
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000681 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000682
683 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000684
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000685 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000686 ValueState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687
688 if (isFeasible) {
689 builder.generateCaseStmtNode(I, StNew);
690
691 // If CondV evaluates to a constant, then we know that this
692 // is the *only* case that we can take, so stop evaluating the
693 // others.
694 if (isa<nonlval::ConcreteInt>(CondV))
695 return;
696 }
697
698 // Now "assume" that the case doesn't match. Add this state
699 // to the default state (if it is feasible).
700
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000701 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000702 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000703
Ted Kremenek5014ab12008-04-23 05:03:18 +0000704 if (isFeasible) {
705 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000707 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000708
Ted Kremenek14a11402008-03-17 22:17:56 +0000709 // Concretize the next value in the range.
710 if (V1 == V2)
711 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000712
Ted Kremenek14a11402008-03-17 22:17:56 +0000713 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000714 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000715
716 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717 }
718
719 // If we reach here, than we know that the default branch is
720 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000721 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722}
723
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000724//===----------------------------------------------------------------------===//
725// Transfer functions: logical operations ('&&', '||').
726//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000728void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000729 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000730
Ted Kremenek05a23782008-02-26 19:05:15 +0000731 assert (B->getOpcode() == BinaryOperator::LAnd ||
732 B->getOpcode() == BinaryOperator::LOr);
733
734 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
735
Ted Kremenek0d093d32008-03-10 04:11:42 +0000736 ValueState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000737 RVal X = GetBlkExprRVal(St, B);
738
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000739 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000740
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000741 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000742
743 assert (Ex);
744
745 if (Ex == B->getRHS()) {
746
747 X = GetBlkExprRVal(St, Ex);
748
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000749 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000750
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000751 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000752 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000753 return;
754 }
755
Ted Kremenek05a23782008-02-26 19:05:15 +0000756 // We took the RHS. Because the value of the '&&' or '||' expression must
757 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
758 // or 1. Alternatively, we could take a lazy approach, and calculate this
759 // value later when necessary. We don't have the machinery in place for
760 // this right now, and since most logical expressions are used for branches,
761 // the payoff is not likely to be large. Instead, we do eager evaluation.
762
763 bool isFeasible = false;
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000764 ValueState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000765
766 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000767 MakeNode(Dst, B, Pred,
768 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000769
770 isFeasible = false;
771 NewState = Assume(St, X, false, isFeasible);
772
773 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000774 MakeNode(Dst, B, Pred,
775 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000776 }
777 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000778 // We took the LHS expression. Depending on whether we are '&&' or
779 // '||' we know what the value of the expression is via properties of
780 // the short-circuiting.
781
782 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000783 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000784 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000785}
Ted Kremenek05a23782008-02-26 19:05:15 +0000786
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000787//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000788// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000789//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000790
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000791void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
792 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000793
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000794 ValueState* St = GetState(Pred);
Ted Kremenek9b5551d2008-03-09 03:30:59 +0000795 RVal X = RVal::MakeVal(BasicVals, D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000796
797 if (asLVal)
798 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
799 else {
800 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
801 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
802 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000803}
804
Ted Kremenek540cbe22008-04-22 04:56:29 +0000805/// VisitArraySubscriptExpr - Transfer function for array accesses
806void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
807 NodeSet& Dst, bool asLVal) {
808
809 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000810 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000811
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000812 // Always visit the base as an LVal expression. This computes the
813 // abstract address of the base object.
814 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000815
Ted Kremenek0e470a52008-05-09 23:45:33 +0000816 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000817 Visit(Base, Pred, Tmp);
818 else
819 VisitLVal(Base, Pred, Tmp);
820
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000821 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000822
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000823 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000824
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000825 NodeSet Tmp2;
826 Visit(Idx, *I1, Tmp2);
827
828 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
829
830 ValueState* St = GetState(*I2);
831 RVal BaseV = GetRVal(St, Base);
832 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000833
834 // If IdxV is 0, return just BaseV.
835
836 bool useBase = false;
837
838 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
839 useBase = IdxInt->getValue() == 0;
840
841 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(BasicVals, BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000842
843 if (asLVal)
844 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
845 else
846 EvalLoad(Dst, A, *I2, St, V);
847 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000848 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000849}
850
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000851/// VisitMemberExpr - Transfer function for member expressions.
852void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
853 NodeSet& Dst, bool asLVal) {
854
855 Expr* Base = M->getBase()->IgnoreParens();
856
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000857 // Always visit the base as an LVal expression. This computes the
858 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000859 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000860
Ted Kremenekee90dba2008-04-30 22:17:15 +0000861 if (asLVal) {
862
Ted Kremenek0e470a52008-05-09 23:45:33 +0000863 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000864 Visit(Base, Pred, Tmp);
865 else
866 VisitLVal(Base, Pred, Tmp);
867
868 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
869 ValueState* St = GetState(*I);
870 RVal BaseV = GetRVal(St, Base);
871
872 RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base),
873 M->getMemberDecl());
874
875 MakeNode(Dst, M, *I, SetRVal(St, M, V));
876 }
877
878 return;
879 }
880
881 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
882 // or not isArrow() is true).
883 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000884
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000885 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000886
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000887 ValueState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000888 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000889
Ted Kremenek0e470a52008-05-09 23:45:33 +0000890 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000891
Ted Kremenekee90dba2008-04-30 22:17:15 +0000892 assert (M->isArrow());
893
894 RVal V = lval::FieldOffset::Make(BasicVals, GetRVal(St, Base),
895 M->getMemberDecl());
896
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000897 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000898 }
899 else {
900
901 assert (!M->isArrow());
902
903 if (BaseV.isUnknownOrUndef()) {
904 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
905 continue;
906 }
907
908 // FIXME: Implement nonlval objects representing struct temporaries.
909 assert (isa<NonLVal>(BaseV));
910 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
911 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000912 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000913}
914
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
916 ValueState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000917
918 assert (Builder && "GRStmtNodeBuilder must be defined.");
919
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000920 // Evaluate the location (checks for bad dereferences).
921 St = EvalLocation(Ex, Pred, St, location);
922
923 if (!St)
924 return;
925
926 // Proceed with the store.
927
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000928 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000929
Ted Kremenek186350f2008-04-23 20:12:28 +0000930 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
931 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000932
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000933 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000934
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000935 TF->EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000936
937 // 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 && !Builder->HasGeneratedNode)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000941 TF->GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
942 location, Val);
943}
944
945void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
946 ValueState* St, RVal location, bool CheckOnly) {
947
948 // Evaluate the location (checks for bad dereferences).
949
950 St = EvalLocation(Ex, Pred, St, location, true);
951
952 if (!St)
953 return;
954
955 // Proceed with the load.
956
957 // FIXME: Currently symbolic analysis "generates" new symbols
958 // for the contents of values. We need a better approach.
959
960 // FIXME: The "CheckOnly" option exists only because Array and Field
961 // loads aren't fully implemented. Eventually this option will go away.
962
Ted Kremenek436f2b92008-04-30 04:23:07 +0000963 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000965 else if (location.isUnknown()) {
966 // This is important. We must nuke the old binding.
967 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
968 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969 else
970 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
971 Ex->getType())));
972}
973
974ValueState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
975 ValueState* St, RVal location,
976 bool isLoad) {
977
978 // Check for loads/stores from/to undefined values.
979 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000980 ProgramPoint::Kind K =
981 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
982
983 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984 Succ->markAsSink();
985 UndefDeref.insert(Succ);
986 }
987
988 return NULL;
989 }
990
991 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
992 if (location.isUnknown())
993 return St;
994
995 // During a load, one of two possible situations arise:
996 // (1) A crash, because the location (pointer) was NULL.
997 // (2) The location (pointer) is not NULL, and the dereference works.
998 //
999 // We add these assumptions.
1000
1001 LVal LV = cast<LVal>(location);
1002
1003 // "Assume" that the pointer is not NULL.
1004
1005 bool isFeasibleNotNull = false;
1006 ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
1007
1008 // "Assume" that the pointer is NULL.
1009
1010 bool isFeasibleNull = false;
1011 ValueState* StNull = Assume(St, LV, false, isFeasibleNull);
1012
1013 if (isFeasibleNull) {
1014
1015 // We don't use "MakeNode" here because the node will be a sink
1016 // and we have no intention of processing it later.
1017
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001018 ProgramPoint::Kind K =
1019 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1020
1021 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001022
1023 if (NullNode) {
1024
1025 NullNode->markAsSink();
1026
1027 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1028 else ExplicitNullDeref.insert(NullNode);
1029 }
1030 }
1031
1032 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001033}
1034
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001035//===----------------------------------------------------------------------===//
1036// Transfer function: Function calls.
1037//===----------------------------------------------------------------------===//
1038
Ted Kremenekde434242008-02-19 01:44:53 +00001039void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001040 CallExpr::arg_iterator AI,
1041 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001042 NodeSet& Dst) {
1043
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001044 // Process the arguments.
1045
1046 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001047
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001048 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001049 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001050 ++AI;
1051
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001052 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001053 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001054
1055 return;
1056 }
1057
1058 // If we reach here we have processed all of the arguments. Evaluate
1059 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001060
Ted Kremenek994a09b2008-02-25 21:16:03 +00001061 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001062 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001063
Ted Kremenek994a09b2008-02-25 21:16:03 +00001064 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001065
Ted Kremenekde434242008-02-19 01:44:53 +00001066 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001067 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1068
Ted Kremenek0d093d32008-03-10 04:11:42 +00001069 ValueState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001070 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001071
Ted Kremeneka1354a52008-03-03 16:47:31 +00001072 // FIXME: Add support for symbolic function calls (calls involving
1073 // function pointer values that are symbolic).
1074
1075 // Check for undefined control-flow or calls to NULL.
1076
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001077 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001078 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001079
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001080 if (N) {
1081 N->markAsSink();
1082 BadCalls.insert(N);
1083 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001084
Ted Kremenekde434242008-02-19 01:44:53 +00001085 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001086 }
1087
1088 // Check for the "noreturn" attribute.
1089
1090 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1091
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001092 if (isa<lval::FuncVal>(L)) {
1093
1094 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1095
1096 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001097 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001098 else {
1099 // HACK: Some functions are not marked noreturn, and don't return.
1100 // Here are a few hardwired ones. If this takes too long, we can
1101 // potentially cache these results.
1102 const char* s = FD->getIdentifier()->getName();
1103 unsigned n = strlen(s);
1104
1105 switch (n) {
1106 default:
1107 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001108
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001109 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001110 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1111 break;
1112
1113 case 5:
1114 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1115 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001116
1117 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001118 if (!memcmp(s, "Assert", 6)) {
1119 Builder->BuildSinks = true;
1120 break;
1121 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001122
1123 // FIXME: This is just a wrapper around throwing an exception.
1124 // Eventually inter-procedural analysis should handle this easily.
1125 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1126
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001127 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001128
1129 case 7:
1130 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1131 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001132
Ted Kremenekf47bb782008-04-30 17:54:04 +00001133 case 8:
1134 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1135 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001136
1137 case 12:
1138 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1139 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001140
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001141 case 14:
1142 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1143 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001144
1145 case 26:
Ted Kremenek05a91122008-05-17 00:40:45 +00001146 if (!memcmp(s, "_XCAssertionFailureHandler", 26))
1147 Builder->BuildSinks = true;
1148
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001149 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001150 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001151
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001152 }
1153 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001154
1155 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001156
1157 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001158
1159 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1160
Ted Kremenek186350f2008-04-23 20:12:28 +00001161 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001162 switch (id) {
1163 case Builtin::BI__builtin_expect: {
1164 // For __builtin_expect, just return the value of the subexpression.
1165 assert (CE->arg_begin() != CE->arg_end());
1166 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001167 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001168 continue;
1169 }
1170
1171 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001172 break;
1173 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001174 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001175
Ted Kremenek186350f2008-04-23 20:12:28 +00001176 // Check any arguments passed-by-value against being undefined.
1177
1178 bool badArg = false;
1179
1180 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1181 I != E; ++I) {
1182
1183 if (GetRVal(GetState(*DI), *I).isUndef()) {
1184 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001185
Ted Kremenek186350f2008-04-23 20:12:28 +00001186 if (N) {
1187 N->markAsSink();
1188 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001189 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001190
Ted Kremenek186350f2008-04-23 20:12:28 +00001191 badArg = true;
1192 break;
1193 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001194 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001195
1196 if (badArg)
1197 continue;
1198
1199 // Dispatch to the plug-in transfer function.
1200
1201 unsigned size = Dst.size();
1202 SaveOr OldHasGen(Builder->HasGeneratedNode);
1203 EvalCall(Dst, CE, L, *DI);
1204
1205 // Handle the case where no nodes where generated. Auto-generate that
1206 // contains the updated state if we aren't generating sinks.
1207
1208 if (!Builder->BuildSinks && Dst.size() == size &&
1209 !Builder->HasGeneratedNode)
1210 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001211 }
1212}
1213
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001214//===----------------------------------------------------------------------===//
1215// Transfer function: Objective-C message expressions.
1216//===----------------------------------------------------------------------===//
1217
1218void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1219 NodeSet& Dst){
1220
1221 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1222 Pred, Dst);
1223}
1224
1225void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1226 ObjCMessageExpr::arg_iterator AI,
1227 ObjCMessageExpr::arg_iterator AE,
1228 NodeTy* Pred, NodeSet& Dst) {
1229 if (AI == AE) {
1230
1231 // Process the receiver.
1232
1233 if (Expr* Receiver = ME->getReceiver()) {
1234 NodeSet Tmp;
1235 Visit(Receiver, Pred, Tmp);
1236
1237 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1238 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1239
1240 return;
1241 }
1242
1243 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1244 return;
1245 }
1246
1247 NodeSet Tmp;
1248 Visit(*AI, Pred, Tmp);
1249
1250 ++AI;
1251
1252 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1253 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1254}
1255
1256void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1257 NodeTy* Pred,
1258 NodeSet& Dst) {
1259
1260 // FIXME: More logic for the processing the method call.
1261
1262 ValueState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001263 bool RaisesException = false;
1264
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001265
1266 if (Expr* Receiver = ME->getReceiver()) {
1267
1268 RVal L = GetRVal(St, Receiver);
1269
1270 // Check for undefined control-flow or calls to NULL.
1271
1272 if (L.isUndef()) {
1273 NodeTy* N = Builder->generateNode(ME, St, Pred);
1274
1275 if (N) {
1276 N->markAsSink();
1277 UndefReceivers.insert(N);
1278 }
1279
1280 return;
1281 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001282
1283 // Check if the "raise" message was sent.
1284 if (ME->getSelector() == RaiseSel)
1285 RaisesException = true;
1286 }
1287 else {
1288
1289 IdentifierInfo* ClsName = ME->getClassName();
1290 Selector S = ME->getSelector();
1291
1292 // Check for special instance methods.
1293
1294 if (!NSExceptionII) {
1295 ASTContext& Ctx = getContext();
1296
1297 NSExceptionII = &Ctx.Idents.get("NSException");
1298 }
1299
1300 if (ClsName == NSExceptionII) {
1301
1302 enum { NUM_RAISE_SELECTORS = 2 };
1303
1304 // Lazily create a cache of the selectors.
1305
1306 if (!NSExceptionInstanceRaiseSelectors) {
1307
1308 ASTContext& Ctx = getContext();
1309
1310 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1311
1312 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1313 unsigned idx = 0;
1314
1315 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001316 II.push_back(&Ctx.Idents.get("raise"));
1317 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001318 NSExceptionInstanceRaiseSelectors[idx++] =
1319 Ctx.Selectors.getSelector(II.size(), &II[0]);
1320
1321 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001322 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001323 NSExceptionInstanceRaiseSelectors[idx++] =
1324 Ctx.Selectors.getSelector(II.size(), &II[0]);
1325 }
1326
1327 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1328 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1329 RaisesException = true; break;
1330 }
1331 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001332 }
1333
1334 // Check for any arguments that are uninitialized/undefined.
1335
1336 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1337 I != E; ++I) {
1338
1339 if (GetRVal(St, *I).isUndef()) {
1340
1341 // Generate an error node for passing an uninitialized/undefined value
1342 // as an argument to a message expression. This node is a sink.
1343 NodeTy* N = Builder->generateNode(ME, St, Pred);
1344
1345 if (N) {
1346 N->markAsSink();
1347 MsgExprUndefArgs[N] = *I;
1348 }
1349
1350 return;
1351 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001352 }
1353
1354 // Check if we raise an exception. For now treat these as sinks. Eventually
1355 // we will want to handle exceptions properly.
1356
1357 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1358
1359 if (RaisesException)
1360 Builder->BuildSinks = true;
1361
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001362 // Dispatch to plug-in transfer function.
1363
1364 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001365 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001366
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001367 EvalObjCMessageExpr(Dst, ME, Pred);
1368
1369 // Handle the case where no nodes where generated. Auto-generate that
1370 // contains the updated state if we aren't generating sinks.
1371
Ted Kremenekb0533962008-04-18 20:35:30 +00001372 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001373 MakeNode(Dst, ME, Pred, St);
1374}
1375
1376//===----------------------------------------------------------------------===//
1377// Transfer functions: Miscellaneous statements.
1378//===----------------------------------------------------------------------===//
1379
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001380void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001381
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001382 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001383 QualType T = CastE->getType();
1384
Ted Kremenek65cfb732008-03-04 22:16:08 +00001385 if (T->isReferenceType())
1386 VisitLVal(Ex, Pred, S1);
1387 else
1388 Visit(Ex, Pred, S1);
1389
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001390 // Check for casting to "void".
1391 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001392
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001393 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001394 Dst.Add(*I1);
1395
Ted Kremenek874d63f2008-01-24 02:02:54 +00001396 return;
1397 }
1398
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001399 // FIXME: The rest of this should probably just go into EvalCall, and
1400 // let the transfer function object be responsible for constructing
1401 // nodes.
1402
1403 QualType ExTy = Ex->getType();
1404
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001405 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001406 NodeTy* N = *I1;
Ted Kremenek0d093d32008-03-10 04:11:42 +00001407 ValueState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001408 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001409
1410 // Unknown?
1411
1412 if (V.isUnknown()) {
1413 Dst.Add(N);
1414 continue;
1415 }
1416
1417 // Undefined?
1418
1419 if (V.isUndef()) {
1420 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1421 continue;
1422 }
1423
1424 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001425 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001426 unsigned bits = getContext().getTypeSize(ExTy);
1427
1428 // FIXME: Determine if the number of bits of the target type is
1429 // equal or exceeds the number of bits to store the pointer value.
1430 // If not, flag an error.
1431
1432 V = nonlval::LValAsInteger::Make(BasicVals, cast<LVal>(V), bits);
1433 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1434 continue;
1435 }
1436
1437 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001438 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001439 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1440 // Just unpackage the lval and return it.
1441 V = LV->getLVal();
1442 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1443 continue;
1444 }
1445
1446 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001447
Ted Kremenek0e561a32008-03-21 21:30:14 +00001448 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001449 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001450}
1451
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001452void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1453 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1454}
1455
1456void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1457 NodeTy* Pred, NodeSet& Dst) {
1458
1459 if (!D)
1460 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001461
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001462 if (!isa<VarDecl>(D)) {
1463 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1464 return;
1465 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001466
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001467 const VarDecl* VD = dyn_cast<VarDecl>(D);
1468
1469 // FIXME: Add support for local arrays.
1470 if (VD->getType()->isArrayType()) {
1471 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1472 return;
1473 }
1474
1475 Expr* Ex = const_cast<Expr*>(VD->getInit());
1476
1477 // FIXME: static variables may have an initializer, but the second
1478 // time a function is called those values may not be current.
1479 NodeSet Tmp;
1480
1481 if (Ex) Visit(Ex, Pred, Tmp);
1482 if (Tmp.empty()) Tmp.Add(Pred);
1483
1484 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001485
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001486 ValueState* St = GetState(*I);
1487
1488 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001489
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001490 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001491
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001492 // FIXME: static variables may have an initializer, but the second
1493 // time a function is called those values may not be current.
1494
1495
1496 // In this context, Static => Local variable.
1497
1498 assert (!VD->getStorageClass() == VarDecl::Static ||
1499 !VD->isFileVarDecl());
1500
1501 // If there is no initializer, set the value of the
1502 // variable to "Undefined".
1503
1504 if (VD->getStorageClass() == VarDecl::Static) {
1505
1506 // C99: 6.7.8 Initialization
1507 // If an object that has static storage duration is not initialized
1508 // explicitly, then:
1509 // —if it has pointer type, it is initialized to a null pointer;
1510 // —if it has arithmetic type, it is initialized to (positive or
1511 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001512
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001513 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001514
1515 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001516
Ted Kremenek0e470a52008-05-09 23:45:33 +00001517 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001518 St = SetRVal(St, lval::DeclVal(VD),
1519 lval::ConcreteInt(BasicVals.getValue(0, T)));
1520 else if (T->isIntegerType())
1521 St = SetRVal(St, lval::DeclVal(VD),
1522 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001523
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001524 // FIXME: Handle structs. Now we treat them as unknown. What
1525 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001526 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001527 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001528 else {
1529
1530 // FIXME: Handle structs. Now we treat them as unknown. What
1531 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001532
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001533 QualType T = VD->getType();
1534
Ted Kremenek0e470a52008-05-09 23:45:33 +00001535 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001536
1537 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1538
1539 if (Ex && V.isUnknown()) {
1540
1541 // EXPERIMENTAL: "Conjured" symbols.
1542
1543 unsigned Count = Builder->getCurrentBlockCount();
1544 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1545
Ted Kremenek0e470a52008-05-09 23:45:33 +00001546 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001547 ? cast<RVal>(lval::SymbolVal(Sym))
1548 : cast<RVal>(nonlval::SymbolVal(Sym));
1549 }
1550
1551 St = SetRVal(St, lval::DeclVal(VD), V);
1552 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001553 }
1554
1555 // Create a new node. We don't really need to create a new NodeSet
1556 // here, but it simplifies things and doesn't cost much.
1557 NodeSet Tmp2;
1558 MakeNode(Tmp2, DS, *I, St);
1559 if (Tmp2.empty()) Tmp2.Add(*I);
1560
1561 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1562 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1563 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001564}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001565
Ted Kremenekf233d482008-02-05 00:26:40 +00001566
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001567/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001568void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1569 NodeTy* Pred,
1570 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001571 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001572 uint64_t amt;
1573
1574 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001575
Ted Kremenek87e80342008-03-15 03:13:20 +00001576 // FIXME: Add support for VLAs.
1577 if (!T.getTypePtr()->isConstantSizeType())
1578 return;
1579
Ted Kremenekf342d182008-04-30 21:31:12 +00001580 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1581 // the compiler has laid out its representation. Just report Unknown
1582 // for these.
1583 if (T->isObjCInterfaceType())
1584 return;
1585
Ted Kremenek87e80342008-03-15 03:13:20 +00001586 amt = 1; // Handle sizeof(void)
1587
1588 if (T != getContext().VoidTy)
1589 amt = getContext().getTypeSize(T) / 8;
1590
1591 }
1592 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001593 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001594
Ted Kremenek0e561a32008-03-21 21:30:14 +00001595 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001596 SetRVal(GetState(Pred), Ex,
1597 NonLVal::MakeVal(BasicVals, amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001598}
1599
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001600
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001601void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001602 NodeSet& Dst, bool asLVal) {
1603
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001604 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001605
1606 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001607 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001608
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001609 case UnaryOperator::Deref: {
1610
1611 Expr* Ex = U->getSubExpr()->IgnoreParens();
1612 NodeSet Tmp;
1613 Visit(Ex, Pred, Tmp);
1614
1615 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001616
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001617 ValueState* St = GetState(*I);
1618 RVal location = GetRVal(St, Ex);
1619
1620 if (asLVal)
1621 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1622 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001623 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001624 }
1625
1626 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001627 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001628
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001629 case UnaryOperator::Real: {
1630
1631 Expr* Ex = U->getSubExpr()->IgnoreParens();
1632 NodeSet Tmp;
1633 Visit(Ex, Pred, Tmp);
1634
1635 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1636
1637 // FIXME: We don't have complex RValues yet.
1638 if (Ex->getType()->isAnyComplexType()) {
1639 // Just report "Unknown."
1640 Dst.Add(*I);
1641 continue;
1642 }
1643
1644 // For all other types, UnaryOperator::Real is an identity operation.
1645 assert (U->getType() == Ex->getType());
1646 ValueState* St = GetState(*I);
1647 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1648 }
1649
1650 return;
1651 }
1652
1653 case UnaryOperator::Imag: {
1654
1655 Expr* Ex = U->getSubExpr()->IgnoreParens();
1656 NodeSet Tmp;
1657 Visit(Ex, Pred, Tmp);
1658
1659 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1660 // FIXME: We don't have complex RValues yet.
1661 if (Ex->getType()->isAnyComplexType()) {
1662 // Just report "Unknown."
1663 Dst.Add(*I);
1664 continue;
1665 }
1666
1667 // For all other types, UnaryOperator::Float returns 0.
1668 assert (Ex->getType()->isIntegerType());
1669 ValueState* St = GetState(*I);
1670 RVal X = NonLVal::MakeVal(BasicVals, 0, Ex->getType());
1671 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1672 }
1673
1674 return;
1675 }
1676
1677 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001678 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001679 Dst.Add(Pred);
1680 return;
1681
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001682 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1683 case UnaryOperator::Extension: {
1684
1685 // Unary "+" is a no-op, similar to a parentheses. We still have places
1686 // where it may be a block-level expression, so we need to
1687 // generate an extra node that just propagates the value of the
1688 // subexpression.
1689
1690 Expr* Ex = U->getSubExpr()->IgnoreParens();
1691 NodeSet Tmp;
1692 Visit(Ex, Pred, Tmp);
1693
1694 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1695 ValueState* St = GetState(*I);
1696 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1697 }
1698
1699 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001700 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001701
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001702 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001703
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001704 assert (!asLVal);
1705 Expr* Ex = U->getSubExpr()->IgnoreParens();
1706 NodeSet Tmp;
1707 VisitLVal(Ex, Pred, Tmp);
1708
1709 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1710 ValueState* St = GetState(*I);
1711 RVal V = GetRVal(St, Ex);
1712 St = SetRVal(St, U, V);
1713 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001714 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001715
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001716 return;
1717 }
1718
1719 case UnaryOperator::LNot:
1720 case UnaryOperator::Minus:
1721 case UnaryOperator::Not: {
1722
1723 assert (!asLVal);
1724 Expr* Ex = U->getSubExpr()->IgnoreParens();
1725 NodeSet Tmp;
1726 Visit(Ex, Pred, Tmp);
1727
1728 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1729 ValueState* St = GetState(*I);
1730 RVal V = GetRVal(St, Ex);
1731
1732 if (V.isUnknownOrUndef()) {
1733 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1734 continue;
1735 }
1736
1737 switch (U->getOpcode()) {
1738 default:
1739 assert(false && "Invalid Opcode.");
1740 break;
1741
1742 case UnaryOperator::Not:
1743 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1744 break;
1745
1746 case UnaryOperator::Minus:
1747 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1748 break;
1749
1750 case UnaryOperator::LNot:
1751
1752 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1753 //
1754 // Note: technically we do "E == 0", but this is the same in the
1755 // transfer functions as "0 == E".
1756
1757 if (isa<LVal>(V)) {
1758 lval::ConcreteInt X(BasicVals.getZeroWithPtrWidth());
1759 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1760 St = SetRVal(St, U, Result);
1761 }
1762 else {
1763 nonlval::ConcreteInt X(BasicVals.getValue(0, Ex->getType()));
1764 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1765 St = SetRVal(St, U, Result);
1766 }
1767
1768 break;
1769 }
1770
1771 MakeNode(Dst, U, *I, St);
1772 }
1773
1774 return;
1775 }
1776
1777 case UnaryOperator::SizeOf: {
1778
1779 QualType T = U->getSubExpr()->getType();
1780
1781 // FIXME: Add support for VLAs.
1782
1783 if (!T.getTypePtr()->isConstantSizeType())
1784 return;
1785
1786 uint64_t size = getContext().getTypeSize(T) / 8;
1787 ValueState* St = GetState(Pred);
1788 St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType()));
1789
1790 MakeNode(Dst, U, Pred, St);
1791 return;
1792 }
1793 }
1794
1795 // Handle ++ and -- (both pre- and post-increment).
1796
1797 assert (U->isIncrementDecrementOp());
1798 NodeSet Tmp;
1799 Expr* Ex = U->getSubExpr()->IgnoreParens();
1800 VisitLVal(Ex, Pred, Tmp);
1801
1802 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1803
1804 ValueState* St = GetState(*I);
1805 RVal V1 = GetRVal(St, Ex);
1806
1807 // Perform a load.
1808 NodeSet Tmp2;
1809 EvalLoad(Tmp2, Ex, *I, St, V1);
1810
1811 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1812
1813 St = GetState(*I2);
1814 RVal V2 = GetRVal(St, Ex);
1815
1816 // Propagate unknown and undefined values.
1817 if (V2.isUnknownOrUndef()) {
1818 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001819 continue;
1820 }
1821
Ted Kremenek443003b2008-02-21 19:29:23 +00001822 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001823
1824 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1825 : BinaryOperator::Sub;
1826
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001827 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1828 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001829
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001830 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001831 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001832 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001833 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001834}
1835
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001836void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1837 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1838}
1839
1840void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1841 AsmStmt::outputs_iterator I,
1842 AsmStmt::outputs_iterator E,
1843 NodeTy* Pred, NodeSet& Dst) {
1844 if (I == E) {
1845 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1846 return;
1847 }
1848
1849 NodeSet Tmp;
1850 VisitLVal(*I, Pred, Tmp);
1851
1852 ++I;
1853
1854 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1855 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1856}
1857
1858void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1859 AsmStmt::inputs_iterator I,
1860 AsmStmt::inputs_iterator E,
1861 NodeTy* Pred, NodeSet& Dst) {
1862 if (I == E) {
1863
1864 // We have processed both the inputs and the outputs. All of the outputs
1865 // should evaluate to LVals. Nuke all of their values.
1866
1867 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1868 // which interprets the inline asm and stores proper results in the
1869 // outputs.
1870
1871 ValueState* St = GetState(Pred);
1872
1873 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1874 OE = A->end_outputs(); OI != OE; ++OI) {
1875
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001876 RVal X = GetRVal(St, *OI);
1877 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001878
1879 if (isa<LVal>(X))
1880 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1881 }
1882
Ted Kremenek0e561a32008-03-21 21:30:14 +00001883 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001884 return;
1885 }
1886
1887 NodeSet Tmp;
1888 Visit(*I, Pred, Tmp);
1889
1890 ++I;
1891
1892 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1893 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1894}
1895
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001896void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1897 assert (Builder && "GRStmtNodeBuilder must be defined.");
1898
1899 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001900
Ted Kremenek186350f2008-04-23 20:12:28 +00001901 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1902 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001903
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001904 TF->EvalReturn(Dst, *this, *Builder, S, Pred);
1905
Ted Kremenekb0533962008-04-18 20:35:30 +00001906 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001907
Ted Kremenekb0533962008-04-18 20:35:30 +00001908 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001909 MakeNode(Dst, S, Pred, GetState(Pred));
1910}
1911
Ted Kremenek02737ed2008-03-31 15:02:58 +00001912void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1913
1914 Expr* R = S->getRetValue();
1915
1916 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001917 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001918 return;
1919 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001920
1921 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001922 QualType T = R->getType();
1923
Chris Lattner423a3c92008-04-02 17:45:06 +00001924 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001925
1926 // Check if any of the return values return the address of a stack variable.
1927
1928 NodeSet Tmp;
1929 Visit(R, Pred, Tmp);
1930
1931 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1932 RVal X = GetRVal((*I)->getState(), R);
1933
1934 if (isa<lval::DeclVal>(X)) {
1935
1936 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1937
1938 // Create a special node representing the v
1939
1940 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1941
1942 if (RetStackNode) {
1943 RetStackNode->markAsSink();
1944 RetsStackAddr.insert(RetStackNode);
1945 }
1946
1947 continue;
1948 }
1949 }
1950
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001951 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001952 }
1953 }
1954 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001955 Visit(R, Pred, DstRet);
1956
1957 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
1958 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001959}
Ted Kremenek55deb972008-03-25 00:34:37 +00001960
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001961//===----------------------------------------------------------------------===//
1962// Transfer functions: Binary operators.
1963//===----------------------------------------------------------------------===//
1964
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001965bool GRExprEngine::CheckDivideZero(Expr* Ex, ValueState* St,
1966 NodeTy* Pred, RVal Denom) {
1967
1968 // Divide by undefined? (potentially zero)
1969
1970 if (Denom.isUndef()) {
1971 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
1972
1973 if (DivUndef) {
1974 DivUndef->markAsSink();
1975 ExplicitBadDivides.insert(DivUndef);
1976 }
1977
1978 return true;
1979 }
1980
1981 // Check for divide/remainder-by-zero.
1982 // First, "assume" that the denominator is 0 or undefined.
1983
1984 bool isFeasibleZero = false;
1985 ValueState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
1986
1987 // Second, "assume" that the denominator cannot be 0.
1988
1989 bool isFeasibleNotZero = false;
1990 St = Assume(St, Denom, true, isFeasibleNotZero);
1991
1992 // Create the node for the divide-by-zero (if it occurred).
1993
1994 if (isFeasibleZero)
1995 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
1996 DivZeroNode->markAsSink();
1997
1998 if (isFeasibleNotZero)
1999 ImplicitBadDivides.insert(DivZeroNode);
2000 else
2001 ExplicitBadDivides.insert(DivZeroNode);
2002
2003 }
2004
2005 return !isFeasibleNotZero;
2006}
2007
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002008void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002009 GRExprEngine::NodeTy* Pred,
2010 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002011
2012 NodeSet Tmp1;
2013 Expr* LHS = B->getLHS()->IgnoreParens();
2014 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002015
2016 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002017 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002018 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002019 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002020
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002021 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002022
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002023 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002024
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002025 // Process the RHS.
2026
2027 NodeSet Tmp2;
2028 Visit(RHS, *I1, Tmp2);
2029
2030 // With both the LHS and RHS evaluated, process the operation itself.
2031
2032 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002033
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002034 ValueState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002035 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002036 BinaryOperator::Opcode Op = B->getOpcode();
2037
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002038 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002039
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002040 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002041
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002042 // EXPERIMENTAL: "Conjured" symbols.
2043
2044 if (RightV.isUnknown()) {
2045 unsigned Count = Builder->getCurrentBlockCount();
2046 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2047
Ted Kremenek0e470a52008-05-09 23:45:33 +00002048 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002049 ? cast<RVal>(lval::SymbolVal(Sym))
2050 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002051 }
2052
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002053 // Simulate the effects of a "store": bind the value of the RHS
2054 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002055
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002056 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002057 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002058 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002059
2060 case BinaryOperator::Div:
2061 case BinaryOperator::Rem:
2062
2063 // Special checking for integer denominators.
2064
2065 if (RHS->getType()->isIntegerType()
2066 && CheckDivideZero(B, St, *I2, RightV))
2067 continue;
2068
2069 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002070
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002071 default: {
2072
2073 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002074 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002076 // Process non-assignements except commas or short-circuited
2077 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002078
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002079 RVal Result = EvalBinOp(Op, LeftV, RightV);
2080
2081 if (Result.isUnknown()) {
2082 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002083 continue;
2084 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002085
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002086 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002087
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002088 // The operands were *not* undefined, but the result is undefined.
2089 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002090
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002091 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002092 UndefNode->markAsSink();
2093 UndefResults.insert(UndefNode);
2094 }
2095
2096 continue;
2097 }
2098
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002099 // Otherwise, create a new node.
2100
2101 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002102 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002103 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002104 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002105
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002106 assert (B->isCompoundAssignmentOp());
2107
2108 if (Op >= BinaryOperator::AndAssign)
2109 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2110 else
2111 ((int&) Op) -= BinaryOperator::MulAssign;
2112
2113 // Perform a load (the LHS). This performs the checks for
2114 // null dereferences, and so on.
2115 NodeSet Tmp3;
2116 RVal location = GetRVal(St, LHS);
2117 EvalLoad(Tmp3, LHS, *I2, St, location);
2118
2119 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2120
2121 St = GetState(*I3);
2122 RVal V = GetRVal(St, LHS);
2123
2124 // Propagate undefined values (left-side).
2125 if (V.isUndef()) {
2126 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2127 continue;
2128 }
2129
2130 // Propagate unknown values (left and right-side).
2131 if (RightV.isUnknown() || V.isUnknown()) {
2132 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2133 UnknownVal());
2134 continue;
2135 }
2136
2137 // At this point:
2138 //
2139 // The LHS is not Undef/Unknown.
2140 // The RHS is not Unknown.
2141
2142 // Get the computation type.
2143 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2144
2145 // Perform promotions.
2146 V = EvalCast(V, CTy);
2147 RightV = EvalCast(RightV, CTy);
2148
2149 // Evaluate operands and promote to result type.
2150
2151 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2152 && RHS->getType()->isIntegerType()) {
2153
2154 if (CheckDivideZero(B, St, *I3, RightV))
2155 continue;
2156 }
2157 else if (RightV.isUndef()) {
2158
2159 // Propagate undefined values (right-side).
2160
2161 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2162 continue;
2163 }
2164
2165 // Compute the result of the operation.
2166
2167 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2168
2169 if (Result.isUndef()) {
2170
2171 // The operands were not undefined, but the result is undefined.
2172
2173 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2174 UndefNode->markAsSink();
2175 UndefResults.insert(UndefNode);
2176 }
2177
2178 continue;
2179 }
2180
2181 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2182 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002183 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002184 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002185}
Ted Kremenekee985462008-01-16 18:18:48 +00002186
2187//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00002188// "Assume" logic.
2189//===----------------------------------------------------------------------===//
2190
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002191ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002192 bool Assumption, bool& isFeasible) {
2193
2194 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002195
2196 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2197 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002198}
2199
2200ValueState* GRExprEngine::AssumeAux(ValueState* St, LVal Cond,
2201 bool Assumption, bool& isFeasible) {
2202
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002203 switch (Cond.getSubKind()) {
2204 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002205 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002206 return St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002207
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002208 case lval::SymbolValKind:
2209 if (Assumption)
2210 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002211 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002212 else
2213 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
Ted Kremenek240f1f02008-03-07 20:13:31 +00002214 BasicVals.getZeroWithPtrWidth(), isFeasible);
Ted Kremenek550a0f92008-04-18 17:20:23 +00002215
2216
Ted Kremenek329f8542008-02-05 21:52:21 +00002217 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00002218 case lval::FuncValKind:
2219 case lval::GotoLabelKind:
Ted Kremeneka5488462008-04-22 21:39:21 +00002220 case lval::StringLiteralValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002221 isFeasible = Assumption;
2222 return St;
Ted Kremenek718c4f72008-04-29 22:17:41 +00002223
2224 case lval::FieldOffsetKind:
2225 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
2226 Assumption, isFeasible);
2227
Ted Kremenek4d0348b2008-04-29 23:24:44 +00002228 case lval::ArrayOffsetKind:
2229 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
2230 Assumption, isFeasible);
2231
Ted Kremenek329f8542008-02-05 21:52:21 +00002232 case lval::ConcreteIntKind: {
2233 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00002234 isFeasible = b ? Assumption : !Assumption;
2235 return St;
2236 }
2237 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002238}
2239
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002240ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond,
Ted Kremenek550a0f92008-04-18 17:20:23 +00002241 bool Assumption, bool& isFeasible) {
2242
2243 St = AssumeAux(St, Cond, Assumption, isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00002244
2245 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
2246 : St;
Ted Kremenek550a0f92008-04-18 17:20:23 +00002247}
2248
2249ValueState* GRExprEngine::AssumeAux(ValueState* St, NonLVal Cond,
2250 bool Assumption, bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002251 switch (Cond.getSubKind()) {
2252 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002253 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00002254 return St;
2255
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002256
2257 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00002258 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002259 SymbolID sym = SV.getSymbol();
2260
2261 if (Assumption)
Ted Kremenek240f1f02008-03-07 20:13:31 +00002262 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002263 isFeasible);
2264 else
Ted Kremenek240f1f02008-03-07 20:13:31 +00002265 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
Ted Kremenekfeb01f62008-02-06 17:32:17 +00002266 isFeasible);
2267 }
2268
Ted Kremenek08b66252008-02-06 04:31:33 +00002269 case nonlval::SymIntConstraintValKind:
2270 return
2271 AssumeSymInt(St, Assumption,
2272 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
2273 isFeasible);
2274
Ted Kremenek329f8542008-02-05 21:52:21 +00002275 case nonlval::ConcreteIntKind: {
2276 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00002277 isFeasible = b ? Assumption : !Assumption;
2278 return St;
2279 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002280
2281 case nonlval::LValAsIntegerKind: {
2282 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
2283 Assumption, isFeasible);
2284 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00002285 }
2286}
2287
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002288ValueState*
2289GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002290 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00002291
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002292 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002293 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002294 isFeasible = *X != V;
2295 return St;
2296 }
2297
2298 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002299 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002300 isFeasible = true;
2301 return St;
2302 }
2303
2304 // If we reach here, sym is not a constant and we don't know if it is != V.
2305 // Make that assumption.
2306
2307 isFeasible = true;
2308 return StateMgr.AddNE(St, sym, V);
2309}
2310
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002311ValueState*
2312GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002313 const llvm::APSInt& V, bool& isFeasible) {
2314
2315 // First, determine if sym == X, where X != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002316 if (const llvm::APSInt* X = St->getSymVal(sym)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002317 isFeasible = *X == V;
2318 return St;
2319 }
2320
2321 // Second, determine if sym != V.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002322 if (St->isNotEqual(sym, V)) {
Ted Kremenek862d5bb2008-02-06 00:54:14 +00002323 isFeasible = false;
2324 return St;
2325 }
2326
2327 // If we reach here, sym is not a constant and we don't know if it is == V.
2328 // Make that assumption.
2329
2330 isFeasible = true;
2331 return StateMgr.AddEQ(St, sym, V);
2332}
Ted Kremenekb38911f2008-01-30 23:03:39 +00002333
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002334ValueState*
2335GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00002336 const SymIntConstraint& C, bool& isFeasible) {
2337
2338 switch (C.getOpcode()) {
2339 default:
2340 // No logic yet for other operators.
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002341 isFeasible = true;
Ted Kremenek08b66252008-02-06 04:31:33 +00002342 return St;
2343
2344 case BinaryOperator::EQ:
2345 if (Assumption)
2346 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2347 else
2348 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2349
2350 case BinaryOperator::NE:
2351 if (Assumption)
2352 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
2353 else
2354 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
2355 }
2356}
2357
Ted Kremenekb38911f2008-01-30 23:03:39 +00002358//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002359// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002360//===----------------------------------------------------------------------===//
2361
Ted Kremenekaa66a322008-01-16 21:46:15 +00002362#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002363static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002364static SourceManager* GraphPrintSourceManager;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002365static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002366
Ted Kremenekaa66a322008-01-16 21:46:15 +00002367namespace llvm {
2368template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002369struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002370 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002371
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002372 static void PrintVarBindings(std::ostream& Out, ValueState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002373
2374 Out << "Variables:\\l";
2375
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002376 bool isFirst = true;
2377
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002378 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002379
2380 if (isFirst)
2381 isFirst = false;
2382 else
2383 Out << "\\l";
2384
2385 Out << ' ' << I.getKey()->getName() << " : ";
2386 I.getData().print(Out);
2387 }
2388
2389 }
2390
Ted Kremeneke7d22112008-02-11 19:21:59 +00002391
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002392 static void PrintSubExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002393
2394 bool isFirst = true;
2395
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002396 for (ValueState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002397
2398 if (isFirst) {
2399 Out << "\\l\\lSub-Expressions:\\l";
2400 isFirst = false;
2401 }
2402 else
2403 Out << "\\l";
2404
2405 Out << " (" << (void*) I.getKey() << ") ";
2406 I.getKey()->printPretty(Out);
2407 Out << " : ";
2408 I.getData().print(Out);
2409 }
2410 }
2411
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002412 static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002413
Ted Kremenek016f52f2008-02-08 21:10:02 +00002414 bool isFirst = true;
2415
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002416 for (ValueState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002417 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002418 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002419 isFirst = false;
2420 }
2421 else
2422 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002423
Ted Kremeneke7d22112008-02-11 19:21:59 +00002424 Out << " (" << (void*) I.getKey() << ") ";
2425 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002426 Out << " : ";
2427 I.getData().print(Out);
2428 }
2429 }
2430
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002431 static void PrintEQ(std::ostream& Out, ValueState* St) {
2432 ValueState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002433
2434 if (CE.isEmpty())
2435 return;
2436
2437 Out << "\\l\\|'==' constraints:";
2438
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002439 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002440 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2441 }
2442
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002443 static void PrintNE(std::ostream& Out, ValueState* St) {
2444 ValueState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002445
2446 if (NE.isEmpty())
2447 return;
2448
2449 Out << "\\l\\|'!=' constraints:";
2450
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002451 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002452 I != EI; ++I){
2453
2454 Out << "\\l $" << I.getKey() << " : ";
2455 bool isFirst = true;
2456
2457 ValueState::IntSetTy::iterator J=I.getData().begin(),
2458 EJ=I.getData().end();
2459 for ( ; J != EJ; ++J) {
2460 if (isFirst) isFirst = false;
2461 else Out << ", ";
2462
2463 Out << (*J)->toString();
2464 }
2465 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002466 }
2467
2468 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2469
2470 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002471 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002472 GraphPrintCheckerState->isUndefDeref(N) ||
2473 GraphPrintCheckerState->isUndefStore(N) ||
2474 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002475 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2476 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002477 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002478 GraphPrintCheckerState->isBadCall(N) ||
2479 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002480 return "color=\"red\",style=\"filled\"";
2481
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002482 if (GraphPrintCheckerState->isNoReturnCall(N))
2483 return "color=\"blue\",style=\"filled\"";
2484
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002485 return "";
2486 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002487
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002488 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002489 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002490
2491 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002492 ProgramPoint Loc = N->getLocation();
2493
2494 switch (Loc.getKind()) {
2495 case ProgramPoint::BlockEntranceKind:
2496 Out << "Block Entrance: B"
2497 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2498 break;
2499
2500 case ProgramPoint::BlockExitKind:
2501 assert (false);
2502 break;
2503
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002504 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002505 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002506 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002507 const PostStmt& L = cast<PostStmt>(Loc);
2508 Stmt* S = L.getStmt();
2509 SourceLocation SLoc = S->getLocStart();
2510
2511 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2512 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002513
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002514 if (SLoc.isFileID()) {
2515 Out << "\\lline="
2516 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2517 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2518 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002519
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002520 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002521 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002522 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002523 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002524 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002525 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002526 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002527 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002528 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2529 Out << "\\|Explicit divide-by zero or undefined value.";
2530 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2531 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002532 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002533 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002534 else if (GraphPrintCheckerState->isNoReturnCall(N))
2535 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002536 else if (GraphPrintCheckerState->isBadCall(N))
2537 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002538 else if (GraphPrintCheckerState->isUndefArg(N))
2539 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002540
Ted Kremenekaa66a322008-01-16 21:46:15 +00002541 break;
2542 }
2543
2544 default: {
2545 const BlockEdge& E = cast<BlockEdge>(Loc);
2546 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2547 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002548
2549 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002550
2551 SourceLocation SLoc = T->getLocStart();
2552
Ted Kremenekb38911f2008-01-30 23:03:39 +00002553 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002554
Ted Kremenekb38911f2008-01-30 23:03:39 +00002555 E.getSrc()->printTerminator(Out);
2556
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002557 if (SLoc.isFileID()) {
2558 Out << "\\lline="
2559 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2560 << GraphPrintSourceManager->getColumnNumber(SLoc);
2561 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002562
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002563 if (isa<SwitchStmt>(T)) {
2564 Stmt* Label = E.getDst()->getLabel();
2565
2566 if (Label) {
2567 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2568 Out << "\\lcase ";
2569 C->getLHS()->printPretty(Out);
2570
2571 if (Stmt* RHS = C->getRHS()) {
2572 Out << " .. ";
2573 RHS->printPretty(Out);
2574 }
2575
2576 Out << ":";
2577 }
2578 else {
2579 assert (isa<DefaultStmt>(Label));
2580 Out << "\\ldefault:";
2581 }
2582 }
2583 else
2584 Out << "\\l(implicit) default:";
2585 }
2586 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002587 // FIXME
2588 }
2589 else {
2590 Out << "\\lCondition: ";
2591 if (*E.getSrc()->succ_begin() == E.getDst())
2592 Out << "true";
2593 else
2594 Out << "false";
2595 }
2596
2597 Out << "\\l";
2598 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002599
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002600 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2601 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002602 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002603 }
2604 }
2605
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002606 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002607
Ted Kremenek75da3e82008-03-11 19:02:40 +00002608 N->getState()->printDOT(Out, GraphCheckerStatePrinter);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002609
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002610 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002611 return Out.str();
2612 }
2613};
2614} // end llvm namespace
2615#endif
2616
Ted Kremenekffe0f432008-03-07 22:58:01 +00002617#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002618
2619template <typename ITERATOR>
2620GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2621
2622template <>
2623GRExprEngine::NodeTy*
2624GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2625 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2626 return I->first;
2627}
2628
Ted Kremenekffe0f432008-03-07 22:58:01 +00002629template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002630static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002631 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002632
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002633 llvm::SmallPtrSet<void*,10> CachedSources;
2634
2635 for ( ; I != E; ++I ) {
2636 GRExprEngine::NodeTy* N = GetGraphNode(I);
2637 void* p = N->getLocation().getRawData();
2638
2639 if (CachedSources.count(p))
2640 continue;
2641
2642 CachedSources.insert(p);
2643
2644 Sources.push_back(N);
2645 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002646}
2647#endif
2648
2649void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002650#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002651 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002652 std::vector<NodeTy*> Src;
2653
2654 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002655 AddSources(Src, null_derefs_begin(), null_derefs_end());
2656 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2657 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2658 AddSources(Src, undef_results_begin(), undef_results_end());
2659 AddSources(Src, bad_calls_begin(), bad_calls_end());
2660 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002661 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002662
Ted Kremenekcb612922008-04-18 19:23:43 +00002663 // The new way.
2664 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2665 (*I)->GetErrorNodes(Src);
2666
2667
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002668 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002669 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002670 else {
2671 GraphPrintCheckerState = this;
2672 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002673 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenek493d7a22008-03-11 18:25:33 +00002674
Ted Kremenekffe0f432008-03-07 22:58:01 +00002675 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002676
2677 GraphPrintCheckerState = NULL;
2678 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002679 GraphCheckerStatePrinter = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002680 }
2681#endif
2682}
2683
2684void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2685#ifndef NDEBUG
2686 GraphPrintCheckerState = this;
2687 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek75da3e82008-03-11 19:02:40 +00002688 GraphCheckerStatePrinter = TF->getCheckerStatePrinter();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002689
Ted Kremenek493d7a22008-03-11 18:25:33 +00002690 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2691
2692 if (!TrimmedG)
2693 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2694 else {
2695 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2696 delete TrimmedG;
2697 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002698
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002699 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002700 GraphPrintSourceManager = NULL;
Ted Kremenek75da3e82008-03-11 19:02:40 +00002701 GraphCheckerStatePrinter = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002702#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002703}