blob: f4ffcb12e015ff06da304e54851bc4661db491bb [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 Kremenekbdb435d2008-07-11 18:37:32 +000016#include "clang/Analysis/PathSensitive/BasicStore.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 Kremenekbdb435d2008-07-11 18:37:32 +000021#include "llvm/ADT/ImmutableList.h"
22#include "llvm/Support/Compiler.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
118 LiveVariables& L)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 Builder(NULL),
Ted Kremenek4323a572008-07-10 22:03:41 +0000123 StateMgr(G.getContext(), CreateBasicStoreManager(G.getAllocator()),
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000124 G.getAllocator(), G.getCFG()),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000125 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000128 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000129
Ted Kremenek1a654b62008-06-20 21:45:25 +0000130GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
132 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000133
Ted Kremeneke448ab42008-05-01 18:33:28 +0000134
135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136}
137
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
142// SaveAndRestore - A utility class that uses RIIA to save and restore
143// the value of a variable.
144template<typename T>
145struct VISIBILITY_HIDDEN SaveAndRestore {
146 SaveAndRestore(T& x) : X(x), old_value(x) {}
147 ~SaveAndRestore() { X = old_value; }
148 T get() { return old_value; }
149
150 T& X;
151 T old_value;
152};
153
Ted Kremenek186350f2008-04-23 20:12:28 +0000154// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
155// value of a variable is saved, and during the dstor the old value is
156// or'ed with the new value.
157struct VISIBILITY_HIDDEN SaveOr {
158 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
159 ~SaveOr() { X |= old_value; }
160
161 bool& X;
162 bool old_value;
163};
164
165
Ted Kremenekc0959972008-07-02 21:24:01 +0000166void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000167 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000168 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000169 (*I)->EmitWarnings(BR);
170 }
171
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000172 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000173 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000174 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000175 }
176}
177
178void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000179 StateMgr.TF = tf;
180 getTF().RegisterChecks(*this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000181}
182
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000183void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
184 if (!BatchAuditor)
185 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
186
187 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000188}
189
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000190const GRState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000191
192 // The LiveVariables information already has a compilation of all VarDecls
193 // used in the function. Iterate through this set, and "symbolicate"
194 // any VarDecl whose value originally comes from outside the function.
195
196 typedef LiveVariables::AnalysisDataTy LVDataTy;
197 LVDataTy& D = Liveness.getAnalysisData();
198
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000199 GRState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000200
201 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
202
Chris Lattner41110242008-06-17 18:05:57 +0000203 ScopedDecl *SD = const_cast<ScopedDecl*>(I->first);
204 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
Ted Kremenekfa077842008-08-14 22:11:13 +0000205 // Punt on static variables for now.
206 if (VD->getStorageClass() == VarDecl::Static)
207 continue;
208
209 // Only handle pointers and integers for now.
210 QualType T = VD->getType();
211 if (!(LVal::IsLValType(T) || T->isIntegerType()))
212 continue;
213
Ted Kremenek72c59d02008-08-13 03:28:04 +0000214 // Initialize globals and parameters to symbolic values.
215 // Initialize local variables to undefined.
216 RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
217 isa<ImplicitParamDecl>(VD))
218 ? RVal::GetSymbolValue(SymMgr, VD)
219 : UndefinedVal();
220
221 StateMgr.SetRVal(StateImpl, lval::DeclVal(VD), X);
222
Chris Lattner41110242008-06-17 18:05:57 +0000223 } else if (ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(SD)) {
224 RVal X = RVal::GetSymbolValue(SymMgr, IPD);
Ted Kremenek4323a572008-07-10 22:03:41 +0000225 StateMgr.SetRVal(StateImpl, lval::DeclVal(IPD), X);
Ted Kremenek07932632008-02-27 06:47:26 +0000226 }
Chris Lattner41110242008-06-17 18:05:57 +0000227
228
Ted Kremenek07932632008-02-27 06:47:26 +0000229 }
230
231 return StateMgr.getPersistentState(StateImpl);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000232}
233
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000234//===----------------------------------------------------------------------===//
235// Top-level transfer function logic (Dispatcher).
236//===----------------------------------------------------------------------===//
237
238void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
239
240 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000241 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000242
243 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000244 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000245 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000246
247 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000248 if (BatchAuditor)
249 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000250
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000251 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000252 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
253 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000254
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000255 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000258 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000259 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000260 else {
261 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000262 SaveOr OldHasGen(Builder->HasGeneratedNode);
263
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000264 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
265 Builder->PurgingDeadSymbols = true;
266
Ted Kremenek729a9a22008-07-17 23:15:45 +0000267 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000268 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000269
270 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
271 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000272 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000273
274 bool HasAutoGenerated = false;
275
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000276 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000277
278 NodeSet Dst;
279
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000280 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000281 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
282
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000283 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000284 Visit(S, *I, Dst);
285
286 // Do we need to auto-generate a node? We only need to do this to generate
287 // a node with a "cleaned" state; GRCoreEngine will actually handle
288 // auto-transitions for other cases.
289 if (Dst.size() == 1 && *Dst.begin() == EntryNode
290 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
291 HasAutoGenerated = true;
292 builder.generateNode(S, GetState(EntryNode), *I);
293 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000294 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000295
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000298 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000299
300 // FIXME: Consolidate.
301 StateMgr.CurrentStmt = 0;
302 CurrentStmt = 0;
303
Ted Kremenek846d4e92008-04-24 23:35:58 +0000304 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000305}
306
307void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
308
309 // FIXME: add metadata to the CFG so that we can disable
310 // this check when we KNOW that there is no block-level subexpression.
311 // The motivation is that this check requires a hashtable lookup.
312
313 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
314 Dst.Add(Pred);
315 return;
316 }
317
318 switch (S->getStmtClass()) {
319
320 default:
321 // Cases we intentionally have "default" handle:
322 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
323
324 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
325 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000326
327 case Stmt::ArraySubscriptExprClass:
328 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
329 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000330
331 case Stmt::AsmStmtClass:
332 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
333 break;
334
335 case Stmt::BinaryOperatorClass: {
336 BinaryOperator* B = cast<BinaryOperator>(S);
337
338 if (B->isLogicalOp()) {
339 VisitLogicalExpr(B, Pred, Dst);
340 break;
341 }
342 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000343 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000344 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
345 break;
346 }
347
348 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
349 break;
350 }
351
352 case Stmt::CallExprClass: {
353 CallExpr* C = cast<CallExpr>(S);
354 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
355 break;
356 }
357
358 case Stmt::CastExprClass: {
359 CastExpr* C = cast<CastExpr>(S);
360 VisitCast(C, C->getSubExpr(), Pred, Dst);
361 break;
362 }
363
364 // FIXME: ChooseExpr is really a constant. We need to fix
365 // the CFG do not model them as explicit control-flow.
366
367 case Stmt::ChooseExprClass: { // __builtin_choose_expr
368 ChooseExpr* C = cast<ChooseExpr>(S);
369 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
370 break;
371 }
372
373 case Stmt::CompoundAssignOperatorClass:
374 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
375 break;
376
377 case Stmt::ConditionalOperatorClass: { // '?' operator
378 ConditionalOperator* C = cast<ConditionalOperator>(S);
379 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
380 break;
381 }
382
383 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000384 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000385 break;
386
387 case Stmt::DeclStmtClass:
388 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
389 break;
390
391 case Stmt::ImplicitCastExprClass: {
392 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
393 VisitCast(C, C->getSubExpr(), Pred, Dst);
394 break;
395 }
396
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000397 case Stmt::MemberExprClass: {
398 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
399 break;
400 }
401
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000402 case Stmt::ObjCMessageExprClass: {
403 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
404 break;
405 }
406
407 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000408 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000409 break;
410
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000411 case Stmt::ReturnStmtClass:
412 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
413 break;
414
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000415 case Stmt::SizeOfAlignOfTypeExprClass:
416 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
417 break;
418
419 case Stmt::StmtExprClass: {
420 StmtExpr* SE = cast<StmtExpr>(S);
421
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000422 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000423
424 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
425 // probably just remove these from the CFG.
426 assert (!SE->getSubStmt()->body_empty());
427
428 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
429 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
430 else
431 Dst.Add(Pred);
432
433 break;
434 }
435
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000436 case Stmt::UnaryOperatorClass:
437 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000438 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000439 }
440}
441
442void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
443
444 Ex = Ex->IgnoreParens();
445
446 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
447 Dst.Add(Pred);
448 return;
449 }
450
451 switch (Ex->getStmtClass()) {
452 default:
453 Visit(Ex, Pred, Dst);
454 return;
455
456 case Stmt::ArraySubscriptExprClass:
457 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
458 return;
459
460 case Stmt::DeclRefExprClass:
461 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
462 return;
463
464 case Stmt::UnaryOperatorClass:
465 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
466 return;
467
468 case Stmt::MemberExprClass:
469 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
470 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000471 }
472}
473
474//===----------------------------------------------------------------------===//
475// Block entrance. (Update counters).
476//===----------------------------------------------------------------------===//
477
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000478bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000479 GRBlockCounter BC) {
480
481 return BC.getNumVisited(B->getBlockID()) < 3;
482}
483
484//===----------------------------------------------------------------------===//
485// Branch processing.
486//===----------------------------------------------------------------------===//
487
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000488const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000489 Stmt* Terminator,
490 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000491
492 switch (Terminator->getStmtClass()) {
493 default:
494 return St;
495
496 case Stmt::BinaryOperatorClass: { // '&&' and '||'
497
498 BinaryOperator* B = cast<BinaryOperator>(Terminator);
499 BinaryOperator::Opcode Op = B->getOpcode();
500
501 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
502
503 // For &&, if we take the true branch, then the value of the whole
504 // expression is that of the RHS expression.
505 //
506 // For ||, if we take the false branch, then the value of the whole
507 // expression is that of the RHS expression.
508
509 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
510 (Op == BinaryOperator::LOr && !branchTaken)
511 ? B->getRHS() : B->getLHS();
512
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000513 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000514 }
515
516 case Stmt::ConditionalOperatorClass: { // ?:
517
518 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
519
520 // For ?, if branchTaken == true then the value is either the LHS or
521 // the condition itself. (GNU extension).
522
523 Expr* Ex;
524
525 if (branchTaken)
526 Ex = C->getLHS() ? C->getLHS() : C->getCond();
527 else
528 Ex = C->getRHS();
529
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000530 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000531 }
532
533 case Stmt::ChooseExprClass: { // ?:
534
535 ChooseExpr* C = cast<ChooseExpr>(Terminator);
536
537 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000538 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000539 }
540 }
541}
542
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000543void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000544 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000545
Ted Kremeneke7d22112008-02-11 19:21:59 +0000546 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000547 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000548 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000549
Ted Kremenekb2331832008-02-15 22:29:00 +0000550 // Check for NULL conditions; e.g. "for(;;)"
551 if (!Condition) {
552 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000553 return;
554 }
555
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000556 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000557
558 switch (V.getBaseKind()) {
559 default:
560 break;
561
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000562 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000563 builder.generateNode(MarkBranch(PrevState, Term, true), true);
564 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000565 return;
566
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000567 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000568 NodeTy* N = builder.generateNode(PrevState, true);
569
570 if (N) {
571 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000572 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000573 }
574
575 builder.markInfeasible(false);
576 return;
577 }
578 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000579
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000580 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000581
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000582 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000583 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000584
585 if (isFeasible)
586 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000587 else
588 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000589
590 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000591
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000592 isFeasible = false;
593 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000594
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000595 if (isFeasible)
596 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000597 else
598 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000599}
600
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000601/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000602/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000603void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000604
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000605 const GRState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000606 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000607
608 // Three possibilities:
609 //
610 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000611 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000612 // (3) We have no clue about the label. Dispatch to all targets.
613 //
614
615 typedef IndirectGotoNodeBuilder::iterator iterator;
616
617 if (isa<lval::GotoLabel>(V)) {
618 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
619
620 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000621 if (I.getLabel() == L) {
622 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000623 return;
624 }
625 }
626
627 assert (false && "No block with label.");
628 return;
629 }
630
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000631 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000632 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000633 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000634 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000635 return;
636 }
637
638 // This is really a catch-all. We don't support symbolics yet.
639
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000640 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000641
642 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000643 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000644}
Ted Kremenekf233d482008-02-05 00:26:40 +0000645
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000646
647void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
648 NodeTy* Pred, NodeSet& Dst) {
649
650 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
651
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000652 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000653 RVal X = GetBlkExprRVal(St, Ex);
654
655 assert (X.isUndef());
656
657 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
658
659 assert (SE);
660
661 X = GetBlkExprRVal(St, SE);
662
663 // Make sure that we invalidate the previous binding.
664 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
665}
666
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000667/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
668/// nodes by processing the 'effects' of a switch statement.
669void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
670
671 typedef SwitchNodeBuilder::iterator iterator;
672
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000673 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000674 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000675 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000676
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000677 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000678 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000679 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000680 return;
681 }
682
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000683 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000684
685 // While most of this can be assumed (such as the signedness), having it
686 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000687
Chris Lattner98be4942008-03-05 18:54:05 +0000688 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000689
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000690 APSInt V1(bits, false);
691 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000692 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000693
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000694 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000695
696 CaseStmt* Case = cast<CaseStmt>(I.getCase());
697
698 // Evaluate the case.
699 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
700 assert (false && "Case condition must evaluate to an integer constant.");
701 return;
702 }
703
704 // Get the RHS of the case, if it exists.
705
706 if (Expr* E = Case->getRHS()) {
707 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
708 assert (false &&
709 "Case condition (RHS) must evaluate to an integer constant.");
710 return ;
711 }
712
713 assert (V1 <= V2);
714 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000715 else
716 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717
718 // FIXME: Eventually we should replace the logic below with a range
719 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000720 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000721
Ted Kremenek14a11402008-03-17 22:17:56 +0000722 do {
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000723 nonlval::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000724
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000725 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000726
727 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000728
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000729 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000730 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000731
732 if (isFeasible) {
733 builder.generateCaseStmtNode(I, StNew);
734
735 // If CondV evaluates to a constant, then we know that this
736 // is the *only* case that we can take, so stop evaluating the
737 // others.
738 if (isa<nonlval::ConcreteInt>(CondV))
739 return;
740 }
741
742 // Now "assume" that the case doesn't match. Add this state
743 // to the default state (if it is feasible).
744
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000745 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000746 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747
Ted Kremenek5014ab12008-04-23 05:03:18 +0000748 if (isFeasible) {
749 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000750 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000751 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752
Ted Kremenek14a11402008-03-17 22:17:56 +0000753 // Concretize the next value in the range.
754 if (V1 == V2)
755 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000756
Ted Kremenek14a11402008-03-17 22:17:56 +0000757 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000758 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000759
760 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000761 }
762
763 // If we reach here, than we know that the default branch is
764 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000765 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000766}
767
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000768//===----------------------------------------------------------------------===//
769// Transfer functions: logical operations ('&&', '||').
770//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000771
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000772void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000773 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000774
Ted Kremenek05a23782008-02-26 19:05:15 +0000775 assert (B->getOpcode() == BinaryOperator::LAnd ||
776 B->getOpcode() == BinaryOperator::LOr);
777
778 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
779
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000780 const GRState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000781 RVal X = GetBlkExprRVal(St, B);
782
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000783 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000784
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000785 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000786
787 assert (Ex);
788
789 if (Ex == B->getRHS()) {
790
791 X = GetBlkExprRVal(St, Ex);
792
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000793 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000794
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000795 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000796 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000797 return;
798 }
799
Ted Kremenek05a23782008-02-26 19:05:15 +0000800 // We took the RHS. Because the value of the '&&' or '||' expression must
801 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
802 // or 1. Alternatively, we could take a lazy approach, and calculate this
803 // value later when necessary. We don't have the machinery in place for
804 // this right now, and since most logical expressions are used for branches,
805 // the payoff is not likely to be large. Instead, we do eager evaluation.
806
807 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000808 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000809
810 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000811 MakeNode(Dst, B, Pred,
812 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000813
814 isFeasible = false;
815 NewState = Assume(St, X, false, isFeasible);
816
817 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000818 MakeNode(Dst, B, Pred,
819 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000820 }
821 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000822 // We took the LHS expression. Depending on whether we are '&&' or
823 // '||' we know what the value of the expression is via properties of
824 // the short-circuiting.
825
826 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000827 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000828 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000829}
Ted Kremenek05a23782008-02-26 19:05:15 +0000830
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000831//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000832// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000833//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000834
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000835void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
836 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000837
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000838 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000839 RVal X = RVal::MakeVal(getBasicVals(), D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000840
841 if (asLVal)
842 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
843 else {
844 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
845 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
846 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000847}
848
Ted Kremenek540cbe22008-04-22 04:56:29 +0000849/// VisitArraySubscriptExpr - Transfer function for array accesses
850void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
851 NodeSet& Dst, bool asLVal) {
852
853 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000854 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000855
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000856 // Always visit the base as an LVal expression. This computes the
857 // abstract address of the base object.
858 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000859
Ted Kremenek0e470a52008-05-09 23:45:33 +0000860 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000861 Visit(Base, Pred, Tmp);
862 else
863 VisitLVal(Base, Pred, Tmp);
864
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000865 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000866
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000867 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000868
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000869 NodeSet Tmp2;
870 Visit(Idx, *I1, Tmp2);
871
872 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
873
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000874 const GRState* St = GetState(*I2);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000875 RVal BaseV = GetRVal(St, Base);
876 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000877
878 // If IdxV is 0, return just BaseV.
879
880 bool useBase = false;
881
882 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
883 useBase = IdxInt->getValue() == 0;
884
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000885 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(getBasicVals(), BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000886
887 if (asLVal)
888 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
889 else
890 EvalLoad(Dst, A, *I2, St, V);
891 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000892 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000893}
894
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000895/// VisitMemberExpr - Transfer function for member expressions.
896void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
897 NodeSet& Dst, bool asLVal) {
898
899 Expr* Base = M->getBase()->IgnoreParens();
900
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000901 // Always visit the base as an LVal expression. This computes the
902 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000903 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000904
Ted Kremenekee90dba2008-04-30 22:17:15 +0000905 if (asLVal) {
906
Ted Kremenek0e470a52008-05-09 23:45:33 +0000907 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000908 Visit(Base, Pred, Tmp);
909 else
910 VisitLVal(Base, Pred, Tmp);
911
912 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000913 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000914 RVal BaseV = GetRVal(St, Base);
915
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000916 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000917 M->getMemberDecl());
918
919 MakeNode(Dst, M, *I, SetRVal(St, M, V));
920 }
921
922 return;
923 }
924
925 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
926 // or not isArrow() is true).
927 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000928
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000929 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000930
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000931 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000932 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000933
Ted Kremenek0e470a52008-05-09 23:45:33 +0000934 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000935
Ted Kremenekee90dba2008-04-30 22:17:15 +0000936 assert (M->isArrow());
937
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000938 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000939 M->getMemberDecl());
940
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000941 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000942 }
943 else {
944
945 assert (!M->isArrow());
946
947 if (BaseV.isUnknownOrUndef()) {
948 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
949 continue;
950 }
951
952 // FIXME: Implement nonlval objects representing struct temporaries.
953 assert (isa<NonLVal>(BaseV));
954 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
955 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000956 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000957}
958
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000959void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000960 const GRState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000961
962 assert (Builder && "GRStmtNodeBuilder must be defined.");
963
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964 // Evaluate the location (checks for bad dereferences).
965 St = EvalLocation(Ex, Pred, St, location);
966
967 if (!St)
968 return;
969
970 // Proceed with the store.
971
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000972 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000973
Ted Kremenek186350f2008-04-23 20:12:28 +0000974 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
975 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000976
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000977 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000978
Ted Kremenek729a9a22008-07-17 23:15:45 +0000979 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000980
981 // Handle the case where no nodes where generated. Auto-generate that
982 // contains the updated state if we aren't generating sinks.
983
Ted Kremenekb0533962008-04-18 20:35:30 +0000984 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000985 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000986 location, Val);
987}
988
989void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000990 const GRState* St, RVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000991 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000992
993 // Evaluate the location (checks for bad dereferences).
994
995 St = EvalLocation(Ex, Pred, St, location, true);
996
997 if (!St)
998 return;
999
1000 // Proceed with the load.
1001
1002 // FIXME: Currently symbolic analysis "generates" new symbols
1003 // for the contents of values. We need a better approach.
1004
1005 // FIXME: The "CheckOnly" option exists only because Array and Field
1006 // loads aren't fully implemented. Eventually this option will go away.
1007
Ted Kremenek436f2b92008-04-30 04:23:07 +00001008 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001009 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001010 else if (location.isUnknown()) {
1011 // This is important. We must nuke the old binding.
1012 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
1013 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001014 else
1015 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
1016 Ex->getType())));
1017}
1018
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001019const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
1020 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +00001021 RVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001022
1023 // Check for loads/stores from/to undefined values.
1024 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001025 ProgramPoint::Kind K =
1026 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1027
1028 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001029 Succ->markAsSink();
1030 UndefDeref.insert(Succ);
1031 }
1032
1033 return NULL;
1034 }
1035
1036 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1037 if (location.isUnknown())
1038 return St;
1039
1040 // During a load, one of two possible situations arise:
1041 // (1) A crash, because the location (pointer) was NULL.
1042 // (2) The location (pointer) is not NULL, and the dereference works.
1043 //
1044 // We add these assumptions.
1045
1046 LVal LV = cast<LVal>(location);
1047
1048 // "Assume" that the pointer is not NULL.
1049
1050 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001051 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052
1053 // "Assume" that the pointer is NULL.
1054
1055 bool isFeasibleNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001056 const GRState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001057
1058 if (isFeasibleNull) {
1059
1060 // We don't use "MakeNode" here because the node will be a sink
1061 // and we have no intention of processing it later.
1062
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001063 ProgramPoint::Kind K =
1064 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1065
1066 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001067
1068 if (NullNode) {
1069
1070 NullNode->markAsSink();
1071
1072 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1073 else ExplicitNullDeref.insert(NullNode);
1074 }
1075 }
1076
1077 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001078}
1079
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001080//===----------------------------------------------------------------------===//
1081// Transfer function: Function calls.
1082//===----------------------------------------------------------------------===//
1083
Ted Kremenekde434242008-02-19 01:44:53 +00001084void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001085 CallExpr::arg_iterator AI,
1086 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001087 NodeSet& Dst) {
1088
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001089 // Process the arguments.
1090
1091 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001092
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001093 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001094 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001095 ++AI;
1096
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001097 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001098 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001099
1100 return;
1101 }
1102
1103 // If we reach here we have processed all of the arguments. Evaluate
1104 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001105
Ted Kremenek994a09b2008-02-25 21:16:03 +00001106 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001107 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001108
Ted Kremenek994a09b2008-02-25 21:16:03 +00001109 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001110
Ted Kremenekde434242008-02-19 01:44:53 +00001111 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001112 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1113
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001114 const GRState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001115 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001116
Ted Kremeneka1354a52008-03-03 16:47:31 +00001117 // FIXME: Add support for symbolic function calls (calls involving
1118 // function pointer values that are symbolic).
1119
1120 // Check for undefined control-flow or calls to NULL.
1121
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001122 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001123 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001124
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001125 if (N) {
1126 N->markAsSink();
1127 BadCalls.insert(N);
1128 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001129
Ted Kremenekde434242008-02-19 01:44:53 +00001130 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001131 }
1132
1133 // Check for the "noreturn" attribute.
1134
1135 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1136
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001137 if (isa<lval::FuncVal>(L)) {
1138
1139 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1140
1141 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001142 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001143 else {
1144 // HACK: Some functions are not marked noreturn, and don't return.
1145 // Here are a few hardwired ones. If this takes too long, we can
1146 // potentially cache these results.
1147 const char* s = FD->getIdentifier()->getName();
1148 unsigned n = strlen(s);
1149
1150 switch (n) {
1151 default:
1152 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001153
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001154 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001155 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1156 break;
1157
1158 case 5:
1159 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1160 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001161
1162 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001163 if (!memcmp(s, "Assert", 6)) {
1164 Builder->BuildSinks = true;
1165 break;
1166 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001167
1168 // FIXME: This is just a wrapper around throwing an exception.
1169 // Eventually inter-procedural analysis should handle this easily.
1170 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1171
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001172 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001173
1174 case 7:
1175 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1176 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001177
Ted Kremenekf47bb782008-04-30 17:54:04 +00001178 case 8:
1179 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1180 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001181
1182 case 12:
1183 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1184 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001185
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001186 case 14:
1187 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1188 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001189
1190 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001191 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1192 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001193 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001194
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001195 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001196 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001197
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001198 }
1199 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001200
1201 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001202
1203 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001204
1205 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1206
Ted Kremenek186350f2008-04-23 20:12:28 +00001207 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001208 switch (id) {
1209 case Builtin::BI__builtin_expect: {
1210 // For __builtin_expect, just return the value of the subexpression.
1211 assert (CE->arg_begin() != CE->arg_end());
1212 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001213 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001214 continue;
1215 }
1216
1217 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001218 break;
1219 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001220 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001221
Ted Kremenek186350f2008-04-23 20:12:28 +00001222 // Check any arguments passed-by-value against being undefined.
1223
1224 bool badArg = false;
1225
1226 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1227 I != E; ++I) {
1228
1229 if (GetRVal(GetState(*DI), *I).isUndef()) {
1230 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001231
Ted Kremenek186350f2008-04-23 20:12:28 +00001232 if (N) {
1233 N->markAsSink();
1234 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001235 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001236
Ted Kremenek186350f2008-04-23 20:12:28 +00001237 badArg = true;
1238 break;
1239 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001240 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001241
1242 if (badArg)
1243 continue;
1244
1245 // Dispatch to the plug-in transfer function.
1246
1247 unsigned size = Dst.size();
1248 SaveOr OldHasGen(Builder->HasGeneratedNode);
1249 EvalCall(Dst, CE, L, *DI);
1250
1251 // Handle the case where no nodes where generated. Auto-generate that
1252 // contains the updated state if we aren't generating sinks.
1253
1254 if (!Builder->BuildSinks && Dst.size() == size &&
1255 !Builder->HasGeneratedNode)
1256 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001257 }
1258}
1259
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001260//===----------------------------------------------------------------------===//
1261// Transfer function: Objective-C message expressions.
1262//===----------------------------------------------------------------------===//
1263
1264void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1265 NodeSet& Dst){
1266
1267 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1268 Pred, Dst);
1269}
1270
1271void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1272 ObjCMessageExpr::arg_iterator AI,
1273 ObjCMessageExpr::arg_iterator AE,
1274 NodeTy* Pred, NodeSet& Dst) {
1275 if (AI == AE) {
1276
1277 // Process the receiver.
1278
1279 if (Expr* Receiver = ME->getReceiver()) {
1280 NodeSet Tmp;
1281 Visit(Receiver, Pred, Tmp);
1282
1283 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1284 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1285
1286 return;
1287 }
1288
1289 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1290 return;
1291 }
1292
1293 NodeSet Tmp;
1294 Visit(*AI, Pred, Tmp);
1295
1296 ++AI;
1297
1298 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1299 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1300}
1301
1302void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1303 NodeTy* Pred,
1304 NodeSet& Dst) {
1305
1306 // FIXME: More logic for the processing the method call.
1307
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001308 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001309 bool RaisesException = false;
1310
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001311
1312 if (Expr* Receiver = ME->getReceiver()) {
1313
1314 RVal L = GetRVal(St, Receiver);
1315
1316 // Check for undefined control-flow or calls to NULL.
1317
1318 if (L.isUndef()) {
1319 NodeTy* N = Builder->generateNode(ME, St, Pred);
1320
1321 if (N) {
1322 N->markAsSink();
1323 UndefReceivers.insert(N);
1324 }
1325
1326 return;
1327 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001328
1329 // Check if the "raise" message was sent.
1330 if (ME->getSelector() == RaiseSel)
1331 RaisesException = true;
1332 }
1333 else {
1334
1335 IdentifierInfo* ClsName = ME->getClassName();
1336 Selector S = ME->getSelector();
1337
1338 // Check for special instance methods.
1339
1340 if (!NSExceptionII) {
1341 ASTContext& Ctx = getContext();
1342
1343 NSExceptionII = &Ctx.Idents.get("NSException");
1344 }
1345
1346 if (ClsName == NSExceptionII) {
1347
1348 enum { NUM_RAISE_SELECTORS = 2 };
1349
1350 // Lazily create a cache of the selectors.
1351
1352 if (!NSExceptionInstanceRaiseSelectors) {
1353
1354 ASTContext& Ctx = getContext();
1355
1356 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1357
1358 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1359 unsigned idx = 0;
1360
1361 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001362 II.push_back(&Ctx.Idents.get("raise"));
1363 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001364 NSExceptionInstanceRaiseSelectors[idx++] =
1365 Ctx.Selectors.getSelector(II.size(), &II[0]);
1366
1367 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001368 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001369 NSExceptionInstanceRaiseSelectors[idx++] =
1370 Ctx.Selectors.getSelector(II.size(), &II[0]);
1371 }
1372
1373 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1374 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1375 RaisesException = true; break;
1376 }
1377 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001378 }
1379
1380 // Check for any arguments that are uninitialized/undefined.
1381
1382 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1383 I != E; ++I) {
1384
1385 if (GetRVal(St, *I).isUndef()) {
1386
1387 // Generate an error node for passing an uninitialized/undefined value
1388 // as an argument to a message expression. This node is a sink.
1389 NodeTy* N = Builder->generateNode(ME, St, Pred);
1390
1391 if (N) {
1392 N->markAsSink();
1393 MsgExprUndefArgs[N] = *I;
1394 }
1395
1396 return;
1397 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001398 }
1399
1400 // Check if we raise an exception. For now treat these as sinks. Eventually
1401 // we will want to handle exceptions properly.
1402
1403 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1404
1405 if (RaisesException)
1406 Builder->BuildSinks = true;
1407
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001408 // Dispatch to plug-in transfer function.
1409
1410 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001411 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001412
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001413 EvalObjCMessageExpr(Dst, ME, Pred);
1414
1415 // Handle the case where no nodes where generated. Auto-generate that
1416 // contains the updated state if we aren't generating sinks.
1417
Ted Kremenekb0533962008-04-18 20:35:30 +00001418 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001419 MakeNode(Dst, ME, Pred, St);
1420}
1421
1422//===----------------------------------------------------------------------===//
1423// Transfer functions: Miscellaneous statements.
1424//===----------------------------------------------------------------------===//
1425
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001426void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001427
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001428 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001429 QualType T = CastE->getType();
1430
Ted Kremenek65cfb732008-03-04 22:16:08 +00001431 if (T->isReferenceType())
1432 VisitLVal(Ex, Pred, S1);
1433 else
1434 Visit(Ex, Pred, S1);
1435
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001436 // Check for casting to "void".
1437 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001438
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001439 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001440 Dst.Add(*I1);
1441
Ted Kremenek874d63f2008-01-24 02:02:54 +00001442 return;
1443 }
1444
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001445 // FIXME: The rest of this should probably just go into EvalCall, and
1446 // let the transfer function object be responsible for constructing
1447 // nodes.
1448
1449 QualType ExTy = Ex->getType();
1450
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001451 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001452 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001453 const GRState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001454 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001455
1456 // Unknown?
1457
1458 if (V.isUnknown()) {
1459 Dst.Add(N);
1460 continue;
1461 }
1462
1463 // Undefined?
1464
1465 if (V.isUndef()) {
1466 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1467 continue;
1468 }
1469
1470 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001471 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001472 unsigned bits = getContext().getTypeSize(ExTy);
1473
1474 // FIXME: Determine if the number of bits of the target type is
1475 // equal or exceeds the number of bits to store the pointer value.
1476 // If not, flag an error.
1477
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001478 V = nonlval::LValAsInteger::Make(getBasicVals(), cast<LVal>(V), bits);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001479 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1480 continue;
1481 }
1482
1483 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001484 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001485 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1486 // Just unpackage the lval and return it.
1487 V = LV->getLVal();
1488 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1489 continue;
1490 }
1491
1492 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001493
Ted Kremenek0e561a32008-03-21 21:30:14 +00001494 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001495 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001496}
1497
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001498void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1499 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1500}
1501
1502void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1503 NodeTy* Pred, NodeSet& Dst) {
1504
1505 if (!D)
1506 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001507
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001508 if (!isa<VarDecl>(D)) {
1509 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1510 return;
1511 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001512
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001513 const VarDecl* VD = dyn_cast<VarDecl>(D);
1514
1515 // FIXME: Add support for local arrays.
1516 if (VD->getType()->isArrayType()) {
1517 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1518 return;
1519 }
1520
1521 Expr* Ex = const_cast<Expr*>(VD->getInit());
1522
1523 // FIXME: static variables may have an initializer, but the second
1524 // time a function is called those values may not be current.
1525 NodeSet Tmp;
1526
1527 if (Ex) Visit(Ex, Pred, Tmp);
1528 if (Tmp.empty()) Tmp.Add(Pred);
1529
1530 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001531
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001532 const GRState* St = GetState(*I);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001533
1534 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001535
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001536 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001537
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001538 // FIXME: static variables may have an initializer, but the second
1539 // time a function is called those values may not be current.
1540
1541
1542 // In this context, Static => Local variable.
1543
1544 assert (!VD->getStorageClass() == VarDecl::Static ||
1545 !VD->isFileVarDecl());
1546
1547 // If there is no initializer, set the value of the
1548 // variable to "Undefined".
1549
1550 if (VD->getStorageClass() == VarDecl::Static) {
1551
1552 // C99: 6.7.8 Initialization
1553 // If an object that has static storage duration is not initialized
1554 // explicitly, then:
1555 // —if it has pointer type, it is initialized to a null pointer;
1556 // —if it has arithmetic type, it is initialized to (positive or
1557 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001558
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001559 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001560
1561 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001562
Ted Kremenek0e470a52008-05-09 23:45:33 +00001563 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001564 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001565 lval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001566 else if (T->isIntegerType())
1567 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001568 nonlval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001569
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001570 // FIXME: Handle structs. Now we treat them as unknown. What
1571 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001572 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001573 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001574 else {
1575
1576 // FIXME: Handle structs. Now we treat them as unknown. What
1577 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001578
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001579 QualType T = VD->getType();
1580
Ted Kremenek0e470a52008-05-09 23:45:33 +00001581 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001582
1583 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1584
1585 if (Ex && V.isUnknown()) {
1586
1587 // EXPERIMENTAL: "Conjured" symbols.
1588
1589 unsigned Count = Builder->getCurrentBlockCount();
1590 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1591
Ted Kremenek0e470a52008-05-09 23:45:33 +00001592 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001593 ? cast<RVal>(lval::SymbolVal(Sym))
1594 : cast<RVal>(nonlval::SymbolVal(Sym));
1595 }
1596
1597 St = SetRVal(St, lval::DeclVal(VD), V);
1598 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001599 }
1600
1601 // Create a new node. We don't really need to create a new NodeSet
1602 // here, but it simplifies things and doesn't cost much.
1603 NodeSet Tmp2;
1604 MakeNode(Tmp2, DS, *I, St);
1605 if (Tmp2.empty()) Tmp2.Add(*I);
1606
1607 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1608 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1609 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001610}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001611
Ted Kremenekf233d482008-02-05 00:26:40 +00001612
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001613/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001614void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1615 NodeTy* Pred,
1616 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001617 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001618 uint64_t amt;
1619
1620 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001621
Ted Kremenek87e80342008-03-15 03:13:20 +00001622 // FIXME: Add support for VLAs.
1623 if (!T.getTypePtr()->isConstantSizeType())
1624 return;
1625
Ted Kremenekf342d182008-04-30 21:31:12 +00001626 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1627 // the compiler has laid out its representation. Just report Unknown
1628 // for these.
1629 if (T->isObjCInterfaceType())
1630 return;
1631
Ted Kremenek87e80342008-03-15 03:13:20 +00001632 amt = 1; // Handle sizeof(void)
1633
1634 if (T != getContext().VoidTy)
1635 amt = getContext().getTypeSize(T) / 8;
1636
1637 }
1638 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001639 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001640
Ted Kremenek0e561a32008-03-21 21:30:14 +00001641 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001642 SetRVal(GetState(Pred), Ex,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001643 NonLVal::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001644}
1645
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001646
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001647void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001648 NodeSet& Dst, bool asLVal) {
1649
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001650 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001651
1652 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001653 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001654
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001655 case UnaryOperator::Deref: {
1656
1657 Expr* Ex = U->getSubExpr()->IgnoreParens();
1658 NodeSet Tmp;
1659 Visit(Ex, Pred, Tmp);
1660
1661 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001662
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001663 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001664 RVal location = GetRVal(St, Ex);
1665
1666 if (asLVal)
1667 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1668 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001669 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001670 }
1671
1672 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001673 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001674
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001675 case UnaryOperator::Real: {
1676
1677 Expr* Ex = U->getSubExpr()->IgnoreParens();
1678 NodeSet Tmp;
1679 Visit(Ex, Pred, Tmp);
1680
1681 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1682
1683 // FIXME: We don't have complex RValues yet.
1684 if (Ex->getType()->isAnyComplexType()) {
1685 // Just report "Unknown."
1686 Dst.Add(*I);
1687 continue;
1688 }
1689
1690 // For all other types, UnaryOperator::Real is an identity operation.
1691 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001692 const GRState* St = GetState(*I);
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001693 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1694 }
1695
1696 return;
1697 }
1698
1699 case UnaryOperator::Imag: {
1700
1701 Expr* Ex = U->getSubExpr()->IgnoreParens();
1702 NodeSet Tmp;
1703 Visit(Ex, Pred, Tmp);
1704
1705 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1706 // FIXME: We don't have complex RValues yet.
1707 if (Ex->getType()->isAnyComplexType()) {
1708 // Just report "Unknown."
1709 Dst.Add(*I);
1710 continue;
1711 }
1712
1713 // For all other types, UnaryOperator::Float returns 0.
1714 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001715 const GRState* St = GetState(*I);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001716 RVal X = NonLVal::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001717 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1718 }
1719
1720 return;
1721 }
1722
1723 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001724 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001725 Dst.Add(Pred);
1726 return;
1727
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001728 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1729 case UnaryOperator::Extension: {
1730
1731 // Unary "+" is a no-op, similar to a parentheses. We still have places
1732 // where it may be a block-level expression, so we need to
1733 // generate an extra node that just propagates the value of the
1734 // subexpression.
1735
1736 Expr* Ex = U->getSubExpr()->IgnoreParens();
1737 NodeSet Tmp;
1738 Visit(Ex, Pred, Tmp);
1739
1740 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001741 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001742 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1743 }
1744
1745 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001746 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001747
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001748 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001749
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001750 assert (!asLVal);
1751 Expr* Ex = U->getSubExpr()->IgnoreParens();
1752 NodeSet Tmp;
1753 VisitLVal(Ex, Pred, Tmp);
1754
1755 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001756 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001757 RVal V = GetRVal(St, Ex);
1758 St = SetRVal(St, U, V);
1759 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001760 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001761
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001762 return;
1763 }
1764
1765 case UnaryOperator::LNot:
1766 case UnaryOperator::Minus:
1767 case UnaryOperator::Not: {
1768
1769 assert (!asLVal);
1770 Expr* Ex = U->getSubExpr()->IgnoreParens();
1771 NodeSet Tmp;
1772 Visit(Ex, Pred, Tmp);
1773
1774 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001775 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001776 RVal V = GetRVal(St, Ex);
1777
1778 if (V.isUnknownOrUndef()) {
1779 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1780 continue;
1781 }
1782
1783 switch (U->getOpcode()) {
1784 default:
1785 assert(false && "Invalid Opcode.");
1786 break;
1787
1788 case UnaryOperator::Not:
1789 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1790 break;
1791
1792 case UnaryOperator::Minus:
1793 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1794 break;
1795
1796 case UnaryOperator::LNot:
1797
1798 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1799 //
1800 // Note: technically we do "E == 0", but this is the same in the
1801 // transfer functions as "0 == E".
1802
1803 if (isa<LVal>(V)) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001804 lval::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001805 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1806 St = SetRVal(St, U, Result);
1807 }
1808 else {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001809 nonlval::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001810#if 0
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001811 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1812 St = SetRVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001813#else
1814 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLVal>(V), X, *I);
1815 continue;
1816#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001817 }
1818
1819 break;
1820 }
1821
1822 MakeNode(Dst, U, *I, St);
1823 }
1824
1825 return;
1826 }
1827
1828 case UnaryOperator::SizeOf: {
1829
1830 QualType T = U->getSubExpr()->getType();
1831
1832 // FIXME: Add support for VLAs.
1833
1834 if (!T.getTypePtr()->isConstantSizeType())
1835 return;
1836
1837 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001838 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001839 St = SetRVal(St, U, NonLVal::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001840
1841 MakeNode(Dst, U, Pred, St);
1842 return;
1843 }
1844 }
1845
1846 // Handle ++ and -- (both pre- and post-increment).
1847
1848 assert (U->isIncrementDecrementOp());
1849 NodeSet Tmp;
1850 Expr* Ex = U->getSubExpr()->IgnoreParens();
1851 VisitLVal(Ex, Pred, Tmp);
1852
1853 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1854
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001855 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001856 RVal V1 = GetRVal(St, Ex);
1857
1858 // Perform a load.
1859 NodeSet Tmp2;
1860 EvalLoad(Tmp2, Ex, *I, St, V1);
1861
1862 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1863
1864 St = GetState(*I2);
1865 RVal V2 = GetRVal(St, Ex);
1866
1867 // Propagate unknown and undefined values.
1868 if (V2.isUnknownOrUndef()) {
1869 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001870 continue;
1871 }
1872
Ted Kremenek443003b2008-02-21 19:29:23 +00001873 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001874
1875 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1876 : BinaryOperator::Sub;
1877
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001878 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1879 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001880
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001881 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001882 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001883 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001884 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001885}
1886
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001887void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1888 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1889}
1890
1891void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1892 AsmStmt::outputs_iterator I,
1893 AsmStmt::outputs_iterator E,
1894 NodeTy* Pred, NodeSet& Dst) {
1895 if (I == E) {
1896 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1897 return;
1898 }
1899
1900 NodeSet Tmp;
1901 VisitLVal(*I, Pred, Tmp);
1902
1903 ++I;
1904
1905 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1906 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1907}
1908
1909void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1910 AsmStmt::inputs_iterator I,
1911 AsmStmt::inputs_iterator E,
1912 NodeTy* Pred, NodeSet& Dst) {
1913 if (I == E) {
1914
1915 // We have processed both the inputs and the outputs. All of the outputs
1916 // should evaluate to LVals. Nuke all of their values.
1917
1918 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1919 // which interprets the inline asm and stores proper results in the
1920 // outputs.
1921
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001922 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001923
1924 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1925 OE = A->end_outputs(); OI != OE; ++OI) {
1926
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001927 RVal X = GetRVal(St, *OI);
1928 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001929
1930 if (isa<LVal>(X))
1931 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1932 }
1933
Ted Kremenek0e561a32008-03-21 21:30:14 +00001934 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001935 return;
1936 }
1937
1938 NodeSet Tmp;
1939 Visit(*I, Pred, Tmp);
1940
1941 ++I;
1942
1943 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1944 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1945}
1946
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001947void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1948 assert (Builder && "GRStmtNodeBuilder must be defined.");
1949
1950 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001951
Ted Kremenek186350f2008-04-23 20:12:28 +00001952 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1953 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001954
Ted Kremenek729a9a22008-07-17 23:15:45 +00001955 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001956
Ted Kremenekb0533962008-04-18 20:35:30 +00001957 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001958
Ted Kremenekb0533962008-04-18 20:35:30 +00001959 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001960 MakeNode(Dst, S, Pred, GetState(Pred));
1961}
1962
Ted Kremenek02737ed2008-03-31 15:02:58 +00001963void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1964
1965 Expr* R = S->getRetValue();
1966
1967 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001968 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001969 return;
1970 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001971
1972 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001973 QualType T = R->getType();
1974
Chris Lattner423a3c92008-04-02 17:45:06 +00001975 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001976
1977 // Check if any of the return values return the address of a stack variable.
1978
1979 NodeSet Tmp;
1980 Visit(R, Pred, Tmp);
1981
1982 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1983 RVal X = GetRVal((*I)->getState(), R);
1984
1985 if (isa<lval::DeclVal>(X)) {
1986
1987 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1988
1989 // Create a special node representing the v
1990
1991 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1992
1993 if (RetStackNode) {
1994 RetStackNode->markAsSink();
1995 RetsStackAddr.insert(RetStackNode);
1996 }
1997
1998 continue;
1999 }
2000 }
2001
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002002 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002003 }
2004 }
2005 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002006 Visit(R, Pred, DstRet);
2007
2008 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2009 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002010}
Ted Kremenek55deb972008-03-25 00:34:37 +00002011
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002012//===----------------------------------------------------------------------===//
2013// Transfer functions: Binary operators.
2014//===----------------------------------------------------------------------===//
2015
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002016bool GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002017 NodeTy* Pred, RVal Denom) {
2018
2019 // Divide by undefined? (potentially zero)
2020
2021 if (Denom.isUndef()) {
2022 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2023
2024 if (DivUndef) {
2025 DivUndef->markAsSink();
2026 ExplicitBadDivides.insert(DivUndef);
2027 }
2028
2029 return true;
2030 }
2031
2032 // Check for divide/remainder-by-zero.
2033 // First, "assume" that the denominator is 0 or undefined.
2034
2035 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002036 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002037
2038 // Second, "assume" that the denominator cannot be 0.
2039
2040 bool isFeasibleNotZero = false;
2041 St = Assume(St, Denom, true, isFeasibleNotZero);
2042
2043 // Create the node for the divide-by-zero (if it occurred).
2044
2045 if (isFeasibleZero)
2046 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2047 DivZeroNode->markAsSink();
2048
2049 if (isFeasibleNotZero)
2050 ImplicitBadDivides.insert(DivZeroNode);
2051 else
2052 ExplicitBadDivides.insert(DivZeroNode);
2053
2054 }
2055
2056 return !isFeasibleNotZero;
2057}
2058
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002059void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002060 GRExprEngine::NodeTy* Pred,
2061 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002062
2063 NodeSet Tmp1;
2064 Expr* LHS = B->getLHS()->IgnoreParens();
2065 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002066
2067 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002068 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002069 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002070 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002071
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002072 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002073
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002074 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002076 // Process the RHS.
2077
2078 NodeSet Tmp2;
2079 Visit(RHS, *I1, Tmp2);
2080
2081 // With both the LHS and RHS evaluated, process the operation itself.
2082
2083 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002084
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002085 const GRState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002086 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002087 BinaryOperator::Opcode Op = B->getOpcode();
2088
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002089 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002090
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002091 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002092
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002093 // EXPERIMENTAL: "Conjured" symbols.
2094
2095 if (RightV.isUnknown()) {
2096 unsigned Count = Builder->getCurrentBlockCount();
2097 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2098
Ted Kremenek0e470a52008-05-09 23:45:33 +00002099 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002100 ? cast<RVal>(lval::SymbolVal(Sym))
2101 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002102 }
2103
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002104 // Simulate the effects of a "store": bind the value of the RHS
2105 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002106
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002107 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002108 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002109 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002110
2111 case BinaryOperator::Div:
2112 case BinaryOperator::Rem:
2113
2114 // Special checking for integer denominators.
2115
2116 if (RHS->getType()->isIntegerType()
2117 && CheckDivideZero(B, St, *I2, RightV))
2118 continue;
2119
2120 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002121
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002122 default: {
2123
2124 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002125 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002126
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002127 // Process non-assignements except commas or short-circuited
2128 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002129
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002130 RVal Result = EvalBinOp(Op, LeftV, RightV);
2131
2132 if (Result.isUnknown()) {
2133 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002134 continue;
2135 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002136
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002137 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002138
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002139 // The operands were *not* undefined, but the result is undefined.
2140 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002141
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002142 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002143 UndefNode->markAsSink();
2144 UndefResults.insert(UndefNode);
2145 }
2146
2147 continue;
2148 }
2149
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002150 // Otherwise, create a new node.
2151
2152 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002153 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002154 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002155 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002156
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002157 assert (B->isCompoundAssignmentOp());
2158
2159 if (Op >= BinaryOperator::AndAssign)
2160 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2161 else
2162 ((int&) Op) -= BinaryOperator::MulAssign;
2163
2164 // Perform a load (the LHS). This performs the checks for
2165 // null dereferences, and so on.
2166 NodeSet Tmp3;
2167 RVal location = GetRVal(St, LHS);
2168 EvalLoad(Tmp3, LHS, *I2, St, location);
2169
2170 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2171
2172 St = GetState(*I3);
2173 RVal V = GetRVal(St, LHS);
2174
2175 // Propagate undefined values (left-side).
2176 if (V.isUndef()) {
2177 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2178 continue;
2179 }
2180
2181 // Propagate unknown values (left and right-side).
2182 if (RightV.isUnknown() || V.isUnknown()) {
2183 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2184 UnknownVal());
2185 continue;
2186 }
2187
2188 // At this point:
2189 //
2190 // The LHS is not Undef/Unknown.
2191 // The RHS is not Unknown.
2192
2193 // Get the computation type.
2194 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2195
2196 // Perform promotions.
2197 V = EvalCast(V, CTy);
2198 RightV = EvalCast(RightV, CTy);
2199
2200 // Evaluate operands and promote to result type.
2201
2202 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2203 && RHS->getType()->isIntegerType()) {
2204
2205 if (CheckDivideZero(B, St, *I3, RightV))
2206 continue;
2207 }
2208 else if (RightV.isUndef()) {
2209
2210 // Propagate undefined values (right-side).
2211
2212 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2213 continue;
2214 }
2215
2216 // Compute the result of the operation.
2217
2218 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2219
2220 if (Result.isUndef()) {
2221
2222 // The operands were not undefined, but the result is undefined.
2223
2224 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2225 UndefNode->markAsSink();
2226 UndefResults.insert(UndefNode);
2227 }
2228
2229 continue;
2230 }
2231
2232 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2233 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002234 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002235 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002236}
Ted Kremenekee985462008-01-16 18:18:48 +00002237
2238//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002239// Transfer-function Helpers.
2240//===----------------------------------------------------------------------===//
2241
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002242void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002243 BinaryOperator::Opcode Op,
2244 NonLVal L, NonLVal R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002245 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002246
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002247 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002248 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2249
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002250 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002251 MakeNode(Dst, Ex, Pred, *I);
2252}
2253
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002254void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002255 Expr* Ex, BinaryOperator::Opcode Op,
2256 NonLVal L, NonLVal R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002257
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002258 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002259 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002260}
2261
2262//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002263// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002264//===----------------------------------------------------------------------===//
2265
Ted Kremenekaa66a322008-01-16 21:46:15 +00002266#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002267static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002268static SourceManager* GraphPrintSourceManager;
Ted Kremenekae6814e2008-08-13 21:24:49 +00002269static GRState::Printer **GraphStatePrinterBeg, **GraphStatePrinterEnd;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002270
Ted Kremenekaa66a322008-01-16 21:46:15 +00002271namespace llvm {
2272template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002273struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002274 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002275
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002276 static void PrintVarBindings(std::ostream& Out, GRState* St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002277
2278 Out << "Variables:\\l";
2279
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002280 bool isFirst = true;
2281
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002282 for (GRState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E;++I) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002283
2284 if (isFirst)
2285 isFirst = false;
2286 else
2287 Out << "\\l";
2288
2289 Out << ' ' << I.getKey()->getName() << " : ";
2290 I.getData().print(Out);
2291 }
2292
2293 }
2294
Ted Kremeneke7d22112008-02-11 19:21:59 +00002295
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002296 static void PrintSubExprBindings(std::ostream& Out, GRState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002297
2298 bool isFirst = true;
2299
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002300 for (GRState::seb_iterator I=St->seb_begin(), E=St->seb_end();I!=E;++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002301
2302 if (isFirst) {
2303 Out << "\\l\\lSub-Expressions:\\l";
2304 isFirst = false;
2305 }
2306 else
2307 Out << "\\l";
2308
2309 Out << " (" << (void*) I.getKey() << ") ";
2310 I.getKey()->printPretty(Out);
2311 Out << " : ";
2312 I.getData().print(Out);
2313 }
2314 }
2315
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002316 static void PrintBlkExprBindings(std::ostream& Out, GRState* St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00002317
Ted Kremenek016f52f2008-02-08 21:10:02 +00002318 bool isFirst = true;
2319
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002320 for (GRState::beb_iterator I=St->beb_begin(), E=St->beb_end(); I!=E;++I){
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002321 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00002322 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002323 isFirst = false;
2324 }
2325 else
2326 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002327
Ted Kremeneke7d22112008-02-11 19:21:59 +00002328 Out << " (" << (void*) I.getKey() << ") ";
2329 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002330 Out << " : ";
2331 I.getData().print(Out);
2332 }
2333 }
2334
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002335 static void PrintEQ(std::ostream& Out, GRState* St) {
2336 GRState::ConstEqTy CE = St->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002337
2338 if (CE.isEmpty())
2339 return;
2340
2341 Out << "\\l\\|'==' constraints:";
2342
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002343 for (GRState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00002344 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
2345 }
2346
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002347 static void PrintNE(std::ostream& Out, GRState* St) {
2348 GRState::ConstNotEqTy NE = St->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00002349
2350 if (NE.isEmpty())
2351 return;
2352
2353 Out << "\\l\\|'!=' constraints:";
2354
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002355 for (GRState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00002356 I != EI; ++I){
2357
2358 Out << "\\l $" << I.getKey() << " : ";
2359 bool isFirst = true;
2360
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002361 GRState::IntSetTy::iterator J=I.getData().begin(),
Ted Kremeneked4de312008-02-06 03:56:15 +00002362 EJ=I.getData().end();
2363 for ( ; J != EJ; ++J) {
2364 if (isFirst) isFirst = false;
2365 else Out << ", ";
2366
2367 Out << (*J)->toString();
2368 }
2369 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002370 }
2371
2372 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2373
2374 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002375 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002376 GraphPrintCheckerState->isUndefDeref(N) ||
2377 GraphPrintCheckerState->isUndefStore(N) ||
2378 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002379 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2380 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002381 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002382 GraphPrintCheckerState->isBadCall(N) ||
2383 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002384 return "color=\"red\",style=\"filled\"";
2385
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002386 if (GraphPrintCheckerState->isNoReturnCall(N))
2387 return "color=\"blue\",style=\"filled\"";
2388
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002389 return "";
2390 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002391
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002392 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002393 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002394
2395 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002396 ProgramPoint Loc = N->getLocation();
2397
2398 switch (Loc.getKind()) {
2399 case ProgramPoint::BlockEntranceKind:
2400 Out << "Block Entrance: B"
2401 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2402 break;
2403
2404 case ProgramPoint::BlockExitKind:
2405 assert (false);
2406 break;
2407
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002408 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002409 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002410 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002411 const PostStmt& L = cast<PostStmt>(Loc);
2412 Stmt* S = L.getStmt();
2413 SourceLocation SLoc = S->getLocStart();
2414
2415 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2416 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002417
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002418 if (SLoc.isFileID()) {
2419 Out << "\\lline="
2420 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2421 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2422 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002423
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002424 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002425 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002426 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002427 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002428 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002429 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002430 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002431 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002432 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2433 Out << "\\|Explicit divide-by zero or undefined value.";
2434 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2435 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002436 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002437 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002438 else if (GraphPrintCheckerState->isNoReturnCall(N))
2439 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002440 else if (GraphPrintCheckerState->isBadCall(N))
2441 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002442 else if (GraphPrintCheckerState->isUndefArg(N))
2443 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002444
Ted Kremenekaa66a322008-01-16 21:46:15 +00002445 break;
2446 }
2447
2448 default: {
2449 const BlockEdge& E = cast<BlockEdge>(Loc);
2450 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2451 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002452
2453 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002454
2455 SourceLocation SLoc = T->getLocStart();
2456
Ted Kremenekb38911f2008-01-30 23:03:39 +00002457 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002458
Ted Kremenekb38911f2008-01-30 23:03:39 +00002459 E.getSrc()->printTerminator(Out);
2460
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002461 if (SLoc.isFileID()) {
2462 Out << "\\lline="
2463 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2464 << GraphPrintSourceManager->getColumnNumber(SLoc);
2465 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002466
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002467 if (isa<SwitchStmt>(T)) {
2468 Stmt* Label = E.getDst()->getLabel();
2469
2470 if (Label) {
2471 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2472 Out << "\\lcase ";
2473 C->getLHS()->printPretty(Out);
2474
2475 if (Stmt* RHS = C->getRHS()) {
2476 Out << " .. ";
2477 RHS->printPretty(Out);
2478 }
2479
2480 Out << ":";
2481 }
2482 else {
2483 assert (isa<DefaultStmt>(Label));
2484 Out << "\\ldefault:";
2485 }
2486 }
2487 else
2488 Out << "\\l(implicit) default:";
2489 }
2490 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002491 // FIXME
2492 }
2493 else {
2494 Out << "\\lCondition: ";
2495 if (*E.getSrc()->succ_begin() == E.getDst())
2496 Out << "true";
2497 else
2498 Out << "false";
2499 }
2500
2501 Out << "\\l";
2502 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002503
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002504 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2505 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002506 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002507 }
2508 }
2509
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002510 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002511
Ted Kremenekae6814e2008-08-13 21:24:49 +00002512 N->getState()->printDOT(Out, GraphStatePrinterBeg, GraphStatePrinterEnd);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002513
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002514 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002515 return Out.str();
2516 }
2517};
2518} // end llvm namespace
2519#endif
2520
Ted Kremenekffe0f432008-03-07 22:58:01 +00002521#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002522
2523template <typename ITERATOR>
2524GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2525
2526template <>
2527GRExprEngine::NodeTy*
2528GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2529 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2530 return I->first;
2531}
2532
Ted Kremenekffe0f432008-03-07 22:58:01 +00002533template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002534static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002535 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002536
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002537 llvm::SmallPtrSet<void*,10> CachedSources;
2538
2539 for ( ; I != E; ++I ) {
2540 GRExprEngine::NodeTy* N = GetGraphNode(I);
2541 void* p = N->getLocation().getRawData();
2542
2543 if (CachedSources.count(p))
2544 continue;
2545
2546 CachedSources.insert(p);
2547
2548 Sources.push_back(N);
2549 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002550}
2551#endif
2552
2553void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002554#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002555 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002556 std::vector<NodeTy*> Src;
2557
2558 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002559 AddSources(Src, null_derefs_begin(), null_derefs_end());
2560 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2561 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2562 AddSources(Src, undef_results_begin(), undef_results_end());
2563 AddSources(Src, bad_calls_begin(), bad_calls_end());
2564 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002565 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002566
Ted Kremenekcb612922008-04-18 19:23:43 +00002567 // The new way.
2568 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2569 (*I)->GetErrorNodes(Src);
2570
2571
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002572 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002573 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002574 else {
2575 GraphPrintCheckerState = this;
2576 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002577
2578 // Get the state printers.
2579 std::vector<GRState::Printer*> Printers;
2580 getTF().getStatePrinters(Printers);
2581 GraphStatePrinterBeg = Printers.empty() ? 0 : &Printers[0];
2582 GraphStatePrinterEnd = Printers.empty() ? 0 : &Printers[0]+Printers.size();
2583
Ted Kremenek493d7a22008-03-11 18:25:33 +00002584
Ted Kremenekffe0f432008-03-07 22:58:01 +00002585 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002586
2587 GraphPrintCheckerState = NULL;
2588 GraphPrintSourceManager = NULL;
Ted Kremenekae6814e2008-08-13 21:24:49 +00002589 GraphStatePrinterBeg = NULL;
2590 GraphStatePrinterEnd = NULL;
Ted Kremenek493d7a22008-03-11 18:25:33 +00002591 }
2592#endif
2593}
2594
2595void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2596#ifndef NDEBUG
2597 GraphPrintCheckerState = this;
2598 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002599
2600 // Get the state printers.
2601 std::vector<GRState::Printer*> Printers;
2602 getTF().getStatePrinters(Printers);
2603 GraphStatePrinterBeg = Printers.empty() ? 0 : &Printers[0];
2604 GraphStatePrinterEnd = Printers.empty() ? 0 : &Printers[0]+Printers.size();
Ted Kremenekffe0f432008-03-07 22:58:01 +00002605
Ted Kremenek493d7a22008-03-11 18:25:33 +00002606 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2607
2608 if (!TrimmedG)
2609 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2610 else {
2611 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2612 delete TrimmedG;
2613 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002614
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002615 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002616 GraphPrintSourceManager = NULL;
Ted Kremenekae6814e2008-08-13 21:24:49 +00002617 GraphStatePrinterBeg = NULL;
2618 GraphStatePrinterEnd = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002619#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002620}