blob: 896f473df8a8c41717af1d535acab13dcc450264 [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;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000180 tf->RegisterChecks(*this);
181 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000182}
183
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000184void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
185 if (!BatchAuditor)
186 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
187
188 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000189}
190
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000191const GRState* GRExprEngine::getInitialState() {
Ted Kremenek07932632008-02-27 06:47:26 +0000192
193 // The LiveVariables information already has a compilation of all VarDecls
194 // used in the function. Iterate through this set, and "symbolicate"
195 // any VarDecl whose value originally comes from outside the function.
196
197 typedef LiveVariables::AnalysisDataTy LVDataTy;
198 LVDataTy& D = Liveness.getAnalysisData();
199
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000200 GRState StateImpl = *StateMgr.getInitialState();
Ted Kremenek07932632008-02-27 06:47:26 +0000201
202 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
203
Chris Lattner41110242008-06-17 18:05:57 +0000204 ScopedDecl *SD = const_cast<ScopedDecl*>(I->first);
205 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
Ted Kremenekfa077842008-08-14 22:11:13 +0000206 // Punt on static variables for now.
207 if (VD->getStorageClass() == VarDecl::Static)
208 continue;
209
210 // Only handle pointers and integers for now.
211 QualType T = VD->getType();
212 if (!(LVal::IsLValType(T) || T->isIntegerType()))
213 continue;
214
Ted Kremenek72c59d02008-08-13 03:28:04 +0000215 // Initialize globals and parameters to symbolic values.
216 // Initialize local variables to undefined.
217 RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
218 isa<ImplicitParamDecl>(VD))
219 ? RVal::GetSymbolValue(SymMgr, VD)
220 : UndefinedVal();
221
222 StateMgr.SetRVal(StateImpl, lval::DeclVal(VD), X);
223
Chris Lattner41110242008-06-17 18:05:57 +0000224 } else if (ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(SD)) {
225 RVal X = RVal::GetSymbolValue(SymMgr, IPD);
Ted Kremenek4323a572008-07-10 22:03:41 +0000226 StateMgr.SetRVal(StateImpl, lval::DeclVal(IPD), X);
Ted Kremenek07932632008-02-27 06:47:26 +0000227 }
Chris Lattner41110242008-06-17 18:05:57 +0000228
229
Ted Kremenek07932632008-02-27 06:47:26 +0000230 }
231
232 return StateMgr.getPersistentState(StateImpl);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000233}
234
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000235//===----------------------------------------------------------------------===//
236// Top-level transfer function logic (Dispatcher).
237//===----------------------------------------------------------------------===//
238
239void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
240
241 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000243
244 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000245 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000246 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000247
248 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000249 if (BatchAuditor)
250 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000251
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000252 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000253 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
254 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000255
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000257 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000259 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000261 else {
262 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000263 SaveOr OldHasGen(Builder->HasGeneratedNode);
264
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000265 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
266 Builder->PurgingDeadSymbols = true;
267
Ted Kremenek729a9a22008-07-17 23:15:45 +0000268 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000269 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000270
271 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
272 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000273 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000274
275 bool HasAutoGenerated = false;
276
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000277 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000278
279 NodeSet Dst;
280
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000281 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000282 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
283
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000284 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000285 Visit(S, *I, Dst);
286
287 // Do we need to auto-generate a node? We only need to do this to generate
288 // a node with a "cleaned" state; GRCoreEngine will actually handle
289 // auto-transitions for other cases.
290 if (Dst.size() == 1 && *Dst.begin() == EntryNode
291 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
292 HasAutoGenerated = true;
293 builder.generateNode(S, GetState(EntryNode), *I);
294 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000295 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000298 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000299 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000300
301 // FIXME: Consolidate.
302 StateMgr.CurrentStmt = 0;
303 CurrentStmt = 0;
304
Ted Kremenek846d4e92008-04-24 23:35:58 +0000305 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000306}
307
308void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
309
310 // FIXME: add metadata to the CFG so that we can disable
311 // this check when we KNOW that there is no block-level subexpression.
312 // The motivation is that this check requires a hashtable lookup.
313
314 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
315 Dst.Add(Pred);
316 return;
317 }
318
319 switch (S->getStmtClass()) {
320
321 default:
322 // Cases we intentionally have "default" handle:
323 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
324
325 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
326 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000327
328 case Stmt::ArraySubscriptExprClass:
329 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
330 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000331
332 case Stmt::AsmStmtClass:
333 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
334 break;
335
336 case Stmt::BinaryOperatorClass: {
337 BinaryOperator* B = cast<BinaryOperator>(S);
338
339 if (B->isLogicalOp()) {
340 VisitLogicalExpr(B, Pred, Dst);
341 break;
342 }
343 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000344 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000345 MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
346 break;
347 }
348
349 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
350 break;
351 }
352
353 case Stmt::CallExprClass: {
354 CallExpr* C = cast<CallExpr>(S);
355 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
356 break;
357 }
358
359 case Stmt::CastExprClass: {
360 CastExpr* C = cast<CastExpr>(S);
361 VisitCast(C, C->getSubExpr(), Pred, Dst);
362 break;
363 }
364
365 // FIXME: ChooseExpr is really a constant. We need to fix
366 // the CFG do not model them as explicit control-flow.
367
368 case Stmt::ChooseExprClass: { // __builtin_choose_expr
369 ChooseExpr* C = cast<ChooseExpr>(S);
370 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
371 break;
372 }
373
374 case Stmt::CompoundAssignOperatorClass:
375 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
376 break;
377
378 case Stmt::ConditionalOperatorClass: { // '?' operator
379 ConditionalOperator* C = cast<ConditionalOperator>(S);
380 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
381 break;
382 }
383
384 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000385 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000386 break;
387
388 case Stmt::DeclStmtClass:
389 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
390 break;
391
392 case Stmt::ImplicitCastExprClass: {
393 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
394 VisitCast(C, C->getSubExpr(), Pred, Dst);
395 break;
396 }
397
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000398 case Stmt::MemberExprClass: {
399 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
400 break;
401 }
402
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 case Stmt::ObjCMessageExprClass: {
404 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
405 break;
406 }
407
408 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000409 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000410 break;
411
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000412 case Stmt::ReturnStmtClass:
413 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
414 break;
415
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000416 case Stmt::SizeOfAlignOfTypeExprClass:
417 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
418 break;
419
420 case Stmt::StmtExprClass: {
421 StmtExpr* SE = cast<StmtExpr>(S);
422
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000423 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000424
425 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
426 // probably just remove these from the CFG.
427 assert (!SE->getSubStmt()->body_empty());
428
429 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
430 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
431 else
432 Dst.Add(Pred);
433
434 break;
435 }
436
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000437 case Stmt::UnaryOperatorClass:
438 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000439 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000440 }
441}
442
443void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
444
445 Ex = Ex->IgnoreParens();
446
447 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
448 Dst.Add(Pred);
449 return;
450 }
451
452 switch (Ex->getStmtClass()) {
453 default:
454 Visit(Ex, Pred, Dst);
455 return;
456
457 case Stmt::ArraySubscriptExprClass:
458 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
459 return;
460
461 case Stmt::DeclRefExprClass:
462 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
463 return;
464
465 case Stmt::UnaryOperatorClass:
466 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::MemberExprClass:
470 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
471 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000472 }
473}
474
475//===----------------------------------------------------------------------===//
476// Block entrance. (Update counters).
477//===----------------------------------------------------------------------===//
478
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000479bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000480 GRBlockCounter BC) {
481
482 return BC.getNumVisited(B->getBlockID()) < 3;
483}
484
485//===----------------------------------------------------------------------===//
486// Branch processing.
487//===----------------------------------------------------------------------===//
488
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000489const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000490 Stmt* Terminator,
491 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000492
493 switch (Terminator->getStmtClass()) {
494 default:
495 return St;
496
497 case Stmt::BinaryOperatorClass: { // '&&' and '||'
498
499 BinaryOperator* B = cast<BinaryOperator>(Terminator);
500 BinaryOperator::Opcode Op = B->getOpcode();
501
502 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
503
504 // For &&, if we take the true branch, then the value of the whole
505 // expression is that of the RHS expression.
506 //
507 // For ||, if we take the false branch, then the value of the whole
508 // expression is that of the RHS expression.
509
510 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
511 (Op == BinaryOperator::LOr && !branchTaken)
512 ? B->getRHS() : B->getLHS();
513
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000514 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000515 }
516
517 case Stmt::ConditionalOperatorClass: { // ?:
518
519 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
520
521 // For ?, if branchTaken == true then the value is either the LHS or
522 // the condition itself. (GNU extension).
523
524 Expr* Ex;
525
526 if (branchTaken)
527 Ex = C->getLHS() ? C->getLHS() : C->getCond();
528 else
529 Ex = C->getRHS();
530
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000531 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000532 }
533
534 case Stmt::ChooseExprClass: { // ?:
535
536 ChooseExpr* C = cast<ChooseExpr>(Terminator);
537
538 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000539 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000540 }
541 }
542}
543
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000544void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000545 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000546
Ted Kremeneke7d22112008-02-11 19:21:59 +0000547 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000548 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000549 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000550
Ted Kremenekb2331832008-02-15 22:29:00 +0000551 // Check for NULL conditions; e.g. "for(;;)"
552 if (!Condition) {
553 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000554 return;
555 }
556
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000557 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000558
559 switch (V.getBaseKind()) {
560 default:
561 break;
562
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000563 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000564 builder.generateNode(MarkBranch(PrevState, Term, true), true);
565 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000566 return;
567
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000568 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000569 NodeTy* N = builder.generateNode(PrevState, true);
570
571 if (N) {
572 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000573 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000574 }
575
576 builder.markInfeasible(false);
577 return;
578 }
579 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000580
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000581 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000582
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000583 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000584 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000585
586 if (isFeasible)
587 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000588 else
589 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000590
591 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000592
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000593 isFeasible = false;
594 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000595
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596 if (isFeasible)
597 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000598 else
599 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000600}
601
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000602/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000603/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000604void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000605
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000606 const GRState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000607 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000608
609 // Three possibilities:
610 //
611 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000612 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000613 // (3) We have no clue about the label. Dispatch to all targets.
614 //
615
616 typedef IndirectGotoNodeBuilder::iterator iterator;
617
618 if (isa<lval::GotoLabel>(V)) {
619 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
620
621 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000622 if (I.getLabel() == L) {
623 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000624 return;
625 }
626 }
627
628 assert (false && "No block with label.");
629 return;
630 }
631
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000632 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000633 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000634 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000635 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000636 return;
637 }
638
639 // This is really a catch-all. We don't support symbolics yet.
640
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000641 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000642
643 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000644 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000645}
Ted Kremenekf233d482008-02-05 00:26:40 +0000646
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000647
648void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
649 NodeTy* Pred, NodeSet& Dst) {
650
651 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
652
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000653 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000654 RVal X = GetBlkExprRVal(St, Ex);
655
656 assert (X.isUndef());
657
658 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
659
660 assert (SE);
661
662 X = GetBlkExprRVal(St, SE);
663
664 // Make sure that we invalidate the previous binding.
665 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
666}
667
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000668/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
669/// nodes by processing the 'effects' of a switch statement.
670void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
671
672 typedef SwitchNodeBuilder::iterator iterator;
673
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000674 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000675 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000676 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000677
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000678 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000680 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000681 return;
682 }
683
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000684 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000685
686 // While most of this can be assumed (such as the signedness), having it
687 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000688
Chris Lattner98be4942008-03-05 18:54:05 +0000689 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000690
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691 APSInt V1(bits, false);
692 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000693 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000695 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000696
697 CaseStmt* Case = cast<CaseStmt>(I.getCase());
698
699 // Evaluate the case.
700 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
701 assert (false && "Case condition must evaluate to an integer constant.");
702 return;
703 }
704
705 // Get the RHS of the case, if it exists.
706
707 if (Expr* E = Case->getRHS()) {
708 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
709 assert (false &&
710 "Case condition (RHS) must evaluate to an integer constant.");
711 return ;
712 }
713
714 assert (V1 <= V2);
715 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000716 else
717 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000718
719 // FIXME: Eventually we should replace the logic below with a range
720 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000721 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722
Ted Kremenek14a11402008-03-17 22:17:56 +0000723 do {
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000724 nonlval::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000725
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000726 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
728 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000729
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000730 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000731 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000732
733 if (isFeasible) {
734 builder.generateCaseStmtNode(I, StNew);
735
736 // If CondV evaluates to a constant, then we know that this
737 // is the *only* case that we can take, so stop evaluating the
738 // others.
739 if (isa<nonlval::ConcreteInt>(CondV))
740 return;
741 }
742
743 // Now "assume" that the case doesn't match. Add this state
744 // to the default state (if it is feasible).
745
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000746 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000747 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000748
Ted Kremenek5014ab12008-04-23 05:03:18 +0000749 if (isFeasible) {
750 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000752 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753
Ted Kremenek14a11402008-03-17 22:17:56 +0000754 // Concretize the next value in the range.
755 if (V1 == V2)
756 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000757
Ted Kremenek14a11402008-03-17 22:17:56 +0000758 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000759 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000760
761 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000762 }
763
764 // If we reach here, than we know that the default branch is
765 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000766 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000767}
768
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000769//===----------------------------------------------------------------------===//
770// Transfer functions: logical operations ('&&', '||').
771//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000773void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000774 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000775
Ted Kremenek05a23782008-02-26 19:05:15 +0000776 assert (B->getOpcode() == BinaryOperator::LAnd ||
777 B->getOpcode() == BinaryOperator::LOr);
778
779 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
780
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000781 const GRState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000782 RVal X = GetBlkExprRVal(St, B);
783
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000784 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000785
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000786 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000787
788 assert (Ex);
789
790 if (Ex == B->getRHS()) {
791
792 X = GetBlkExprRVal(St, Ex);
793
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000794 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000795
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000796 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000797 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000798 return;
799 }
800
Ted Kremenek05a23782008-02-26 19:05:15 +0000801 // We took the RHS. Because the value of the '&&' or '||' expression must
802 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
803 // or 1. Alternatively, we could take a lazy approach, and calculate this
804 // value later when necessary. We don't have the machinery in place for
805 // this right now, and since most logical expressions are used for branches,
806 // the payoff is not likely to be large. Instead, we do eager evaluation.
807
808 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000809 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000810
811 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000812 MakeNode(Dst, B, Pred,
813 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000814
815 isFeasible = false;
816 NewState = Assume(St, X, false, isFeasible);
817
818 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000819 MakeNode(Dst, B, Pred,
820 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000821 }
822 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000823 // We took the LHS expression. Depending on whether we are '&&' or
824 // '||' we know what the value of the expression is via properties of
825 // the short-circuiting.
826
827 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000828 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000829 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000830}
Ted Kremenek05a23782008-02-26 19:05:15 +0000831
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000832//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000833// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000834//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000835
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000836void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
837 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000838
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000839 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000840 RVal X = RVal::MakeVal(getBasicVals(), D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000841
842 if (asLVal)
843 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
844 else {
845 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
846 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
847 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000848}
849
Ted Kremenek540cbe22008-04-22 04:56:29 +0000850/// VisitArraySubscriptExpr - Transfer function for array accesses
851void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
852 NodeSet& Dst, bool asLVal) {
853
854 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000855 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000856
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000857 // Always visit the base as an LVal expression. This computes the
858 // abstract address of the base object.
859 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000860
Ted Kremenek0e470a52008-05-09 23:45:33 +0000861 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000862 Visit(Base, Pred, Tmp);
863 else
864 VisitLVal(Base, Pred, Tmp);
865
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000866 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000867
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000868 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000869
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000870 NodeSet Tmp2;
871 Visit(Idx, *I1, Tmp2);
872
873 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
874
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000875 const GRState* St = GetState(*I2);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000876 RVal BaseV = GetRVal(St, Base);
877 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000878
879 // If IdxV is 0, return just BaseV.
880
881 bool useBase = false;
882
883 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
884 useBase = IdxInt->getValue() == 0;
885
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000886 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(getBasicVals(), BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000887
888 if (asLVal)
889 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
890 else
891 EvalLoad(Dst, A, *I2, St, V);
892 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000893 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000894}
895
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000896/// VisitMemberExpr - Transfer function for member expressions.
897void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
898 NodeSet& Dst, bool asLVal) {
899
900 Expr* Base = M->getBase()->IgnoreParens();
901
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000902 // Always visit the base as an LVal expression. This computes the
903 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000904 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000905
Ted Kremenekee90dba2008-04-30 22:17:15 +0000906 if (asLVal) {
907
Ted Kremenek0e470a52008-05-09 23:45:33 +0000908 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000909 Visit(Base, Pred, Tmp);
910 else
911 VisitLVal(Base, Pred, Tmp);
912
913 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000914 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000915 RVal BaseV = GetRVal(St, Base);
916
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000917 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000918 M->getMemberDecl());
919
920 MakeNode(Dst, M, *I, SetRVal(St, M, V));
921 }
922
923 return;
924 }
925
926 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
927 // or not isArrow() is true).
928 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000929
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000930 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000931
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000932 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000933 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000934
Ted Kremenek0e470a52008-05-09 23:45:33 +0000935 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000936
Ted Kremenekee90dba2008-04-30 22:17:15 +0000937 assert (M->isArrow());
938
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000939 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000940 M->getMemberDecl());
941
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000942 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000943 }
944 else {
945
946 assert (!M->isArrow());
947
948 if (BaseV.isUnknownOrUndef()) {
949 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
950 continue;
951 }
952
953 // FIXME: Implement nonlval objects representing struct temporaries.
954 assert (isa<NonLVal>(BaseV));
955 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
956 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000957 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000958}
959
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000961 const GRState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000962
963 assert (Builder && "GRStmtNodeBuilder must be defined.");
964
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965 // Evaluate the location (checks for bad dereferences).
966 St = EvalLocation(Ex, Pred, St, location);
967
968 if (!St)
969 return;
970
971 // Proceed with the store.
972
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000973 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000974
Ted Kremenek186350f2008-04-23 20:12:28 +0000975 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
976 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000977
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000979
Ted Kremenek729a9a22008-07-17 23:15:45 +0000980 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000981
982 // Handle the case where no nodes where generated. Auto-generate that
983 // contains the updated state if we aren't generating sinks.
984
Ted Kremenekb0533962008-04-18 20:35:30 +0000985 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000986 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000987 location, Val);
988}
989
990void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000991 const GRState* St, RVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000992 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000993
994 // Evaluate the location (checks for bad dereferences).
995
996 St = EvalLocation(Ex, Pred, St, location, true);
997
998 if (!St)
999 return;
1000
1001 // Proceed with the load.
1002
1003 // FIXME: Currently symbolic analysis "generates" new symbols
1004 // for the contents of values. We need a better approach.
1005
1006 // FIXME: The "CheckOnly" option exists only because Array and Field
1007 // loads aren't fully implemented. Eventually this option will go away.
1008
Ted Kremenek436f2b92008-04-30 04:23:07 +00001009 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001011 else if (location.isUnknown()) {
1012 // This is important. We must nuke the old binding.
1013 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
1014 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015 else
1016 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
1017 Ex->getType())));
1018}
1019
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001020const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
1021 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +00001022 RVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023
1024 // Check for loads/stores from/to undefined values.
1025 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001026 ProgramPoint::Kind K =
1027 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1028
1029 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001030 Succ->markAsSink();
1031 UndefDeref.insert(Succ);
1032 }
1033
1034 return NULL;
1035 }
1036
1037 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1038 if (location.isUnknown())
1039 return St;
1040
1041 // During a load, one of two possible situations arise:
1042 // (1) A crash, because the location (pointer) was NULL.
1043 // (2) The location (pointer) is not NULL, and the dereference works.
1044 //
1045 // We add these assumptions.
1046
1047 LVal LV = cast<LVal>(location);
1048
1049 // "Assume" that the pointer is not NULL.
1050
1051 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001052 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053
1054 // "Assume" that the pointer is NULL.
1055
1056 bool isFeasibleNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001057 const GRState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058
1059 if (isFeasibleNull) {
1060
1061 // We don't use "MakeNode" here because the node will be a sink
1062 // and we have no intention of processing it later.
1063
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001064 ProgramPoint::Kind K =
1065 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1066
1067 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001068
1069 if (NullNode) {
1070
1071 NullNode->markAsSink();
1072
1073 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1074 else ExplicitNullDeref.insert(NullNode);
1075 }
1076 }
1077
1078 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001079}
1080
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001081//===----------------------------------------------------------------------===//
1082// Transfer function: Function calls.
1083//===----------------------------------------------------------------------===//
1084
Ted Kremenekde434242008-02-19 01:44:53 +00001085void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001086 CallExpr::arg_iterator AI,
1087 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001088 NodeSet& Dst) {
1089
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001090 // Process the arguments.
1091
1092 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001093
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001094 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001095 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001096 ++AI;
1097
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001098 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001099 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001100
1101 return;
1102 }
1103
1104 // If we reach here we have processed all of the arguments. Evaluate
1105 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001106
Ted Kremenek994a09b2008-02-25 21:16:03 +00001107 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001108 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001109
Ted Kremenek994a09b2008-02-25 21:16:03 +00001110 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001111
Ted Kremenekde434242008-02-19 01:44:53 +00001112 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001113 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1114
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001115 const GRState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001117
Ted Kremeneka1354a52008-03-03 16:47:31 +00001118 // FIXME: Add support for symbolic function calls (calls involving
1119 // function pointer values that are symbolic).
1120
1121 // Check for undefined control-flow or calls to NULL.
1122
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001123 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001124 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001125
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001126 if (N) {
1127 N->markAsSink();
1128 BadCalls.insert(N);
1129 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001130
Ted Kremenekde434242008-02-19 01:44:53 +00001131 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001132 }
1133
1134 // Check for the "noreturn" attribute.
1135
1136 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1137
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001138 if (isa<lval::FuncVal>(L)) {
1139
1140 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1141
1142 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001143 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001144 else {
1145 // HACK: Some functions are not marked noreturn, and don't return.
1146 // Here are a few hardwired ones. If this takes too long, we can
1147 // potentially cache these results.
1148 const char* s = FD->getIdentifier()->getName();
1149 unsigned n = strlen(s);
1150
1151 switch (n) {
1152 default:
1153 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001154
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001155 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001156 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1157 break;
1158
1159 case 5:
1160 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1161 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001162
1163 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001164 if (!memcmp(s, "Assert", 6)) {
1165 Builder->BuildSinks = true;
1166 break;
1167 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001168
1169 // FIXME: This is just a wrapper around throwing an exception.
1170 // Eventually inter-procedural analysis should handle this easily.
1171 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1172
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001173 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001174
1175 case 7:
1176 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1177 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001178
Ted Kremenekf47bb782008-04-30 17:54:04 +00001179 case 8:
1180 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1181 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001182
1183 case 12:
1184 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1185 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001186
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001187 case 14:
1188 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1189 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001190
1191 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001192 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1193 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001194 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001195
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001196 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001197 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001198
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001199 }
1200 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001201
1202 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001203
1204 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001205
1206 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1207
Ted Kremenek186350f2008-04-23 20:12:28 +00001208 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001209 switch (id) {
1210 case Builtin::BI__builtin_expect: {
1211 // For __builtin_expect, just return the value of the subexpression.
1212 assert (CE->arg_begin() != CE->arg_end());
1213 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001214 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001215 continue;
1216 }
1217
1218 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001219 break;
1220 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001221 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001222
Ted Kremenek186350f2008-04-23 20:12:28 +00001223 // Check any arguments passed-by-value against being undefined.
1224
1225 bool badArg = false;
1226
1227 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1228 I != E; ++I) {
1229
1230 if (GetRVal(GetState(*DI), *I).isUndef()) {
1231 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001232
Ted Kremenek186350f2008-04-23 20:12:28 +00001233 if (N) {
1234 N->markAsSink();
1235 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001236 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001237
Ted Kremenek186350f2008-04-23 20:12:28 +00001238 badArg = true;
1239 break;
1240 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001241 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001242
1243 if (badArg)
1244 continue;
1245
1246 // Dispatch to the plug-in transfer function.
1247
1248 unsigned size = Dst.size();
1249 SaveOr OldHasGen(Builder->HasGeneratedNode);
1250 EvalCall(Dst, CE, L, *DI);
1251
1252 // Handle the case where no nodes where generated. Auto-generate that
1253 // contains the updated state if we aren't generating sinks.
1254
1255 if (!Builder->BuildSinks && Dst.size() == size &&
1256 !Builder->HasGeneratedNode)
1257 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001258 }
1259}
1260
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001261//===----------------------------------------------------------------------===//
1262// Transfer function: Objective-C message expressions.
1263//===----------------------------------------------------------------------===//
1264
1265void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1266 NodeSet& Dst){
1267
1268 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1269 Pred, Dst);
1270}
1271
1272void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1273 ObjCMessageExpr::arg_iterator AI,
1274 ObjCMessageExpr::arg_iterator AE,
1275 NodeTy* Pred, NodeSet& Dst) {
1276 if (AI == AE) {
1277
1278 // Process the receiver.
1279
1280 if (Expr* Receiver = ME->getReceiver()) {
1281 NodeSet Tmp;
1282 Visit(Receiver, Pred, Tmp);
1283
1284 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1285 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1286
1287 return;
1288 }
1289
1290 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1291 return;
1292 }
1293
1294 NodeSet Tmp;
1295 Visit(*AI, Pred, Tmp);
1296
1297 ++AI;
1298
1299 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1300 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1301}
1302
1303void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1304 NodeTy* Pred,
1305 NodeSet& Dst) {
1306
1307 // FIXME: More logic for the processing the method call.
1308
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001309 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001310 bool RaisesException = false;
1311
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001312
1313 if (Expr* Receiver = ME->getReceiver()) {
1314
1315 RVal L = GetRVal(St, Receiver);
1316
1317 // Check for undefined control-flow or calls to NULL.
1318
1319 if (L.isUndef()) {
1320 NodeTy* N = Builder->generateNode(ME, St, Pred);
1321
1322 if (N) {
1323 N->markAsSink();
1324 UndefReceivers.insert(N);
1325 }
1326
1327 return;
1328 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001329
1330 // Check if the "raise" message was sent.
1331 if (ME->getSelector() == RaiseSel)
1332 RaisesException = true;
1333 }
1334 else {
1335
1336 IdentifierInfo* ClsName = ME->getClassName();
1337 Selector S = ME->getSelector();
1338
1339 // Check for special instance methods.
1340
1341 if (!NSExceptionII) {
1342 ASTContext& Ctx = getContext();
1343
1344 NSExceptionII = &Ctx.Idents.get("NSException");
1345 }
1346
1347 if (ClsName == NSExceptionII) {
1348
1349 enum { NUM_RAISE_SELECTORS = 2 };
1350
1351 // Lazily create a cache of the selectors.
1352
1353 if (!NSExceptionInstanceRaiseSelectors) {
1354
1355 ASTContext& Ctx = getContext();
1356
1357 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1358
1359 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1360 unsigned idx = 0;
1361
1362 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001363 II.push_back(&Ctx.Idents.get("raise"));
1364 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001365 NSExceptionInstanceRaiseSelectors[idx++] =
1366 Ctx.Selectors.getSelector(II.size(), &II[0]);
1367
1368 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001369 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001370 NSExceptionInstanceRaiseSelectors[idx++] =
1371 Ctx.Selectors.getSelector(II.size(), &II[0]);
1372 }
1373
1374 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1375 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1376 RaisesException = true; break;
1377 }
1378 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001379 }
1380
1381 // Check for any arguments that are uninitialized/undefined.
1382
1383 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1384 I != E; ++I) {
1385
1386 if (GetRVal(St, *I).isUndef()) {
1387
1388 // Generate an error node for passing an uninitialized/undefined value
1389 // as an argument to a message expression. This node is a sink.
1390 NodeTy* N = Builder->generateNode(ME, St, Pred);
1391
1392 if (N) {
1393 N->markAsSink();
1394 MsgExprUndefArgs[N] = *I;
1395 }
1396
1397 return;
1398 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001399 }
1400
1401 // Check if we raise an exception. For now treat these as sinks. Eventually
1402 // we will want to handle exceptions properly.
1403
1404 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1405
1406 if (RaisesException)
1407 Builder->BuildSinks = true;
1408
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001409 // Dispatch to plug-in transfer function.
1410
1411 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001412 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001413
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001414 EvalObjCMessageExpr(Dst, ME, Pred);
1415
1416 // Handle the case where no nodes where generated. Auto-generate that
1417 // contains the updated state if we aren't generating sinks.
1418
Ted Kremenekb0533962008-04-18 20:35:30 +00001419 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001420 MakeNode(Dst, ME, Pred, St);
1421}
1422
1423//===----------------------------------------------------------------------===//
1424// Transfer functions: Miscellaneous statements.
1425//===----------------------------------------------------------------------===//
1426
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001427void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001428
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001429 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001430 QualType T = CastE->getType();
1431
Ted Kremenek65cfb732008-03-04 22:16:08 +00001432 if (T->isReferenceType())
1433 VisitLVal(Ex, Pred, S1);
1434 else
1435 Visit(Ex, Pred, S1);
1436
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001437 // Check for casting to "void".
1438 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001439
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001440 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001441 Dst.Add(*I1);
1442
Ted Kremenek874d63f2008-01-24 02:02:54 +00001443 return;
1444 }
1445
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001446 // FIXME: The rest of this should probably just go into EvalCall, and
1447 // let the transfer function object be responsible for constructing
1448 // nodes.
1449
1450 QualType ExTy = Ex->getType();
1451
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001452 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001453 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001454 const GRState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001455 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001456
1457 // Unknown?
1458
1459 if (V.isUnknown()) {
1460 Dst.Add(N);
1461 continue;
1462 }
1463
1464 // Undefined?
1465
1466 if (V.isUndef()) {
1467 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1468 continue;
1469 }
1470
1471 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001472 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001473 unsigned bits = getContext().getTypeSize(ExTy);
1474
1475 // FIXME: Determine if the number of bits of the target type is
1476 // equal or exceeds the number of bits to store the pointer value.
1477 // If not, flag an error.
1478
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001479 V = nonlval::LValAsInteger::Make(getBasicVals(), cast<LVal>(V), bits);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001480 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1481 continue;
1482 }
1483
1484 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001485 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001486 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1487 // Just unpackage the lval and return it.
1488 V = LV->getLVal();
1489 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1490 continue;
1491 }
1492
1493 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001494
Ted Kremenek0e561a32008-03-21 21:30:14 +00001495 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001496 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001497}
1498
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001499void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1500 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1501}
1502
1503void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1504 NodeTy* Pred, NodeSet& Dst) {
1505
1506 if (!D)
1507 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001508
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001509 if (!isa<VarDecl>(D)) {
1510 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1511 return;
1512 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001513
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001514 const VarDecl* VD = dyn_cast<VarDecl>(D);
1515
1516 // FIXME: Add support for local arrays.
1517 if (VD->getType()->isArrayType()) {
1518 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1519 return;
1520 }
1521
1522 Expr* Ex = const_cast<Expr*>(VD->getInit());
1523
1524 // FIXME: static variables may have an initializer, but the second
1525 // time a function is called those values may not be current.
1526 NodeSet Tmp;
1527
1528 if (Ex) Visit(Ex, Pred, Tmp);
1529 if (Tmp.empty()) Tmp.Add(Pred);
1530
1531 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001532
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001533 const GRState* St = GetState(*I);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001534
1535 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001536
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001537 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001538
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001539 // FIXME: static variables may have an initializer, but the second
1540 // time a function is called those values may not be current.
1541
1542
1543 // In this context, Static => Local variable.
1544
1545 assert (!VD->getStorageClass() == VarDecl::Static ||
1546 !VD->isFileVarDecl());
1547
1548 // If there is no initializer, set the value of the
1549 // variable to "Undefined".
1550
1551 if (VD->getStorageClass() == VarDecl::Static) {
1552
1553 // C99: 6.7.8 Initialization
1554 // If an object that has static storage duration is not initialized
1555 // explicitly, then:
1556 // —if it has pointer type, it is initialized to a null pointer;
1557 // —if it has arithmetic type, it is initialized to (positive or
1558 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001559
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001560 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001561
1562 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001563
Ted Kremenek0e470a52008-05-09 23:45:33 +00001564 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001565 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001566 lval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001567 else if (T->isIntegerType())
1568 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001569 nonlval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001570
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001571 // FIXME: Handle structs. Now we treat them as unknown. What
1572 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001573 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001574 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001575 else {
1576
1577 // FIXME: Handle structs. Now we treat them as unknown. What
1578 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001579
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001580 QualType T = VD->getType();
1581
Ted Kremenek0e470a52008-05-09 23:45:33 +00001582 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001583
1584 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1585
1586 if (Ex && V.isUnknown()) {
1587
1588 // EXPERIMENTAL: "Conjured" symbols.
1589
1590 unsigned Count = Builder->getCurrentBlockCount();
1591 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1592
Ted Kremenek0e470a52008-05-09 23:45:33 +00001593 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001594 ? cast<RVal>(lval::SymbolVal(Sym))
1595 : cast<RVal>(nonlval::SymbolVal(Sym));
1596 }
1597
1598 St = SetRVal(St, lval::DeclVal(VD), V);
1599 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001600 }
1601
1602 // Create a new node. We don't really need to create a new NodeSet
1603 // here, but it simplifies things and doesn't cost much.
1604 NodeSet Tmp2;
1605 MakeNode(Tmp2, DS, *I, St);
1606 if (Tmp2.empty()) Tmp2.Add(*I);
1607
1608 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1609 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1610 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001611}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001612
Ted Kremenekf233d482008-02-05 00:26:40 +00001613
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001614/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001615void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1616 NodeTy* Pred,
1617 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001618 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001619 uint64_t amt;
1620
1621 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001622
Ted Kremenek87e80342008-03-15 03:13:20 +00001623 // FIXME: Add support for VLAs.
1624 if (!T.getTypePtr()->isConstantSizeType())
1625 return;
1626
Ted Kremenekf342d182008-04-30 21:31:12 +00001627 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1628 // the compiler has laid out its representation. Just report Unknown
1629 // for these.
1630 if (T->isObjCInterfaceType())
1631 return;
1632
Ted Kremenek87e80342008-03-15 03:13:20 +00001633 amt = 1; // Handle sizeof(void)
1634
1635 if (T != getContext().VoidTy)
1636 amt = getContext().getTypeSize(T) / 8;
1637
1638 }
1639 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001640 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001641
Ted Kremenek0e561a32008-03-21 21:30:14 +00001642 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001643 SetRVal(GetState(Pred), Ex,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001644 NonLVal::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001645}
1646
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001647
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001648void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001649 NodeSet& Dst, bool asLVal) {
1650
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001651 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001652
1653 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001654 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001655
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001656 case UnaryOperator::Deref: {
1657
1658 Expr* Ex = U->getSubExpr()->IgnoreParens();
1659 NodeSet Tmp;
1660 Visit(Ex, Pred, Tmp);
1661
1662 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001663
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001664 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001665 RVal location = GetRVal(St, Ex);
1666
1667 if (asLVal)
1668 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1669 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001670 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001671 }
1672
1673 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001674 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001675
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001676 case UnaryOperator::Real: {
1677
1678 Expr* Ex = U->getSubExpr()->IgnoreParens();
1679 NodeSet Tmp;
1680 Visit(Ex, Pred, Tmp);
1681
1682 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1683
1684 // FIXME: We don't have complex RValues yet.
1685 if (Ex->getType()->isAnyComplexType()) {
1686 // Just report "Unknown."
1687 Dst.Add(*I);
1688 continue;
1689 }
1690
1691 // For all other types, UnaryOperator::Real is an identity operation.
1692 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001693 const GRState* St = GetState(*I);
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001694 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1695 }
1696
1697 return;
1698 }
1699
1700 case UnaryOperator::Imag: {
1701
1702 Expr* Ex = U->getSubExpr()->IgnoreParens();
1703 NodeSet Tmp;
1704 Visit(Ex, Pred, Tmp);
1705
1706 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1707 // FIXME: We don't have complex RValues yet.
1708 if (Ex->getType()->isAnyComplexType()) {
1709 // Just report "Unknown."
1710 Dst.Add(*I);
1711 continue;
1712 }
1713
1714 // For all other types, UnaryOperator::Float returns 0.
1715 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001716 const GRState* St = GetState(*I);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001717 RVal X = NonLVal::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001718 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1719 }
1720
1721 return;
1722 }
1723
1724 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001725 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001726 Dst.Add(Pred);
1727 return;
1728
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001729 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1730 case UnaryOperator::Extension: {
1731
1732 // Unary "+" is a no-op, similar to a parentheses. We still have places
1733 // where it may be a block-level expression, so we need to
1734 // generate an extra node that just propagates the value of the
1735 // subexpression.
1736
1737 Expr* Ex = U->getSubExpr()->IgnoreParens();
1738 NodeSet Tmp;
1739 Visit(Ex, Pred, Tmp);
1740
1741 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001742 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001743 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1744 }
1745
1746 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001747 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001748
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001749 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001750
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001751 assert (!asLVal);
1752 Expr* Ex = U->getSubExpr()->IgnoreParens();
1753 NodeSet Tmp;
1754 VisitLVal(Ex, Pred, Tmp);
1755
1756 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001757 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001758 RVal V = GetRVal(St, Ex);
1759 St = SetRVal(St, U, V);
1760 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001761 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001762
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001763 return;
1764 }
1765
1766 case UnaryOperator::LNot:
1767 case UnaryOperator::Minus:
1768 case UnaryOperator::Not: {
1769
1770 assert (!asLVal);
1771 Expr* Ex = U->getSubExpr()->IgnoreParens();
1772 NodeSet Tmp;
1773 Visit(Ex, Pred, Tmp);
1774
1775 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001776 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001777 RVal V = GetRVal(St, Ex);
1778
1779 if (V.isUnknownOrUndef()) {
1780 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1781 continue;
1782 }
1783
1784 switch (U->getOpcode()) {
1785 default:
1786 assert(false && "Invalid Opcode.");
1787 break;
1788
1789 case UnaryOperator::Not:
1790 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1791 break;
1792
1793 case UnaryOperator::Minus:
1794 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1795 break;
1796
1797 case UnaryOperator::LNot:
1798
1799 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1800 //
1801 // Note: technically we do "E == 0", but this is the same in the
1802 // transfer functions as "0 == E".
1803
1804 if (isa<LVal>(V)) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001805 lval::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001806 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1807 St = SetRVal(St, U, Result);
1808 }
1809 else {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001810 nonlval::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001811#if 0
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001812 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1813 St = SetRVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001814#else
1815 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLVal>(V), X, *I);
1816 continue;
1817#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001818 }
1819
1820 break;
1821 }
1822
1823 MakeNode(Dst, U, *I, St);
1824 }
1825
1826 return;
1827 }
1828
1829 case UnaryOperator::SizeOf: {
1830
1831 QualType T = U->getSubExpr()->getType();
1832
1833 // FIXME: Add support for VLAs.
1834
1835 if (!T.getTypePtr()->isConstantSizeType())
1836 return;
1837
1838 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001839 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001840 St = SetRVal(St, U, NonLVal::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001841
1842 MakeNode(Dst, U, Pred, St);
1843 return;
1844 }
1845 }
1846
1847 // Handle ++ and -- (both pre- and post-increment).
1848
1849 assert (U->isIncrementDecrementOp());
1850 NodeSet Tmp;
1851 Expr* Ex = U->getSubExpr()->IgnoreParens();
1852 VisitLVal(Ex, Pred, Tmp);
1853
1854 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1855
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001856 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001857 RVal V1 = GetRVal(St, Ex);
1858
1859 // Perform a load.
1860 NodeSet Tmp2;
1861 EvalLoad(Tmp2, Ex, *I, St, V1);
1862
1863 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1864
1865 St = GetState(*I2);
1866 RVal V2 = GetRVal(St, Ex);
1867
1868 // Propagate unknown and undefined values.
1869 if (V2.isUnknownOrUndef()) {
1870 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001871 continue;
1872 }
1873
Ted Kremenek443003b2008-02-21 19:29:23 +00001874 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001875
1876 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1877 : BinaryOperator::Sub;
1878
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001879 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1880 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001881
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001882 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001883 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001884 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001885 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001886}
1887
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001888void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1889 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1890}
1891
1892void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1893 AsmStmt::outputs_iterator I,
1894 AsmStmt::outputs_iterator E,
1895 NodeTy* Pred, NodeSet& Dst) {
1896 if (I == E) {
1897 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1898 return;
1899 }
1900
1901 NodeSet Tmp;
1902 VisitLVal(*I, Pred, Tmp);
1903
1904 ++I;
1905
1906 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1907 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1908}
1909
1910void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1911 AsmStmt::inputs_iterator I,
1912 AsmStmt::inputs_iterator E,
1913 NodeTy* Pred, NodeSet& Dst) {
1914 if (I == E) {
1915
1916 // We have processed both the inputs and the outputs. All of the outputs
1917 // should evaluate to LVals. Nuke all of their values.
1918
1919 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1920 // which interprets the inline asm and stores proper results in the
1921 // outputs.
1922
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001923 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001924
1925 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1926 OE = A->end_outputs(); OI != OE; ++OI) {
1927
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001928 RVal X = GetRVal(St, *OI);
1929 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001930
1931 if (isa<LVal>(X))
1932 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1933 }
1934
Ted Kremenek0e561a32008-03-21 21:30:14 +00001935 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001936 return;
1937 }
1938
1939 NodeSet Tmp;
1940 Visit(*I, Pred, Tmp);
1941
1942 ++I;
1943
1944 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1945 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1946}
1947
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001948void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1949 assert (Builder && "GRStmtNodeBuilder must be defined.");
1950
1951 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001952
Ted Kremenek186350f2008-04-23 20:12:28 +00001953 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1954 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001955
Ted Kremenek729a9a22008-07-17 23:15:45 +00001956 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001957
Ted Kremenekb0533962008-04-18 20:35:30 +00001958 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001959
Ted Kremenekb0533962008-04-18 20:35:30 +00001960 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001961 MakeNode(Dst, S, Pred, GetState(Pred));
1962}
1963
Ted Kremenek02737ed2008-03-31 15:02:58 +00001964void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1965
1966 Expr* R = S->getRetValue();
1967
1968 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001969 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001970 return;
1971 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001972
1973 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001974 QualType T = R->getType();
1975
Chris Lattner423a3c92008-04-02 17:45:06 +00001976 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001977
1978 // Check if any of the return values return the address of a stack variable.
1979
1980 NodeSet Tmp;
1981 Visit(R, Pred, Tmp);
1982
1983 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1984 RVal X = GetRVal((*I)->getState(), R);
1985
1986 if (isa<lval::DeclVal>(X)) {
1987
1988 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1989
1990 // Create a special node representing the v
1991
1992 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1993
1994 if (RetStackNode) {
1995 RetStackNode->markAsSink();
1996 RetsStackAddr.insert(RetStackNode);
1997 }
1998
1999 continue;
2000 }
2001 }
2002
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002003 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002004 }
2005 }
2006 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002007 Visit(R, Pred, DstRet);
2008
2009 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2010 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002011}
Ted Kremenek55deb972008-03-25 00:34:37 +00002012
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002013//===----------------------------------------------------------------------===//
2014// Transfer functions: Binary operators.
2015//===----------------------------------------------------------------------===//
2016
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002017bool GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002018 NodeTy* Pred, RVal Denom) {
2019
2020 // Divide by undefined? (potentially zero)
2021
2022 if (Denom.isUndef()) {
2023 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2024
2025 if (DivUndef) {
2026 DivUndef->markAsSink();
2027 ExplicitBadDivides.insert(DivUndef);
2028 }
2029
2030 return true;
2031 }
2032
2033 // Check for divide/remainder-by-zero.
2034 // First, "assume" that the denominator is 0 or undefined.
2035
2036 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002037 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002038
2039 // Second, "assume" that the denominator cannot be 0.
2040
2041 bool isFeasibleNotZero = false;
2042 St = Assume(St, Denom, true, isFeasibleNotZero);
2043
2044 // Create the node for the divide-by-zero (if it occurred).
2045
2046 if (isFeasibleZero)
2047 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2048 DivZeroNode->markAsSink();
2049
2050 if (isFeasibleNotZero)
2051 ImplicitBadDivides.insert(DivZeroNode);
2052 else
2053 ExplicitBadDivides.insert(DivZeroNode);
2054
2055 }
2056
2057 return !isFeasibleNotZero;
2058}
2059
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002060void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002061 GRExprEngine::NodeTy* Pred,
2062 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002063
2064 NodeSet Tmp1;
2065 Expr* LHS = B->getLHS()->IgnoreParens();
2066 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002067
2068 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002069 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002070 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002071 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002072
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002073 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002074
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002075 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002076
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002077 // Process the RHS.
2078
2079 NodeSet Tmp2;
2080 Visit(RHS, *I1, Tmp2);
2081
2082 // With both the LHS and RHS evaluated, process the operation itself.
2083
2084 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002085
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002086 const GRState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002087 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002088 BinaryOperator::Opcode Op = B->getOpcode();
2089
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002090 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002091
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002092 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002093
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002094 // EXPERIMENTAL: "Conjured" symbols.
2095
2096 if (RightV.isUnknown()) {
2097 unsigned Count = Builder->getCurrentBlockCount();
2098 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2099
Ted Kremenek0e470a52008-05-09 23:45:33 +00002100 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002101 ? cast<RVal>(lval::SymbolVal(Sym))
2102 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002103 }
2104
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002105 // Simulate the effects of a "store": bind the value of the RHS
2106 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002107
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002108 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002109 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002110 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002111
2112 case BinaryOperator::Div:
2113 case BinaryOperator::Rem:
2114
2115 // Special checking for integer denominators.
2116
2117 if (RHS->getType()->isIntegerType()
2118 && CheckDivideZero(B, St, *I2, RightV))
2119 continue;
2120
2121 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002123 default: {
2124
2125 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002126 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002127
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002128 // Process non-assignements except commas or short-circuited
2129 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002130
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002131 RVal Result = EvalBinOp(Op, LeftV, RightV);
2132
2133 if (Result.isUnknown()) {
2134 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002135 continue;
2136 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002137
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002138 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002139
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002140 // The operands were *not* undefined, but the result is undefined.
2141 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002142
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002143 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002144 UndefNode->markAsSink();
2145 UndefResults.insert(UndefNode);
2146 }
2147
2148 continue;
2149 }
2150
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002151 // Otherwise, create a new node.
2152
2153 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002154 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002155 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002156 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002157
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002158 assert (B->isCompoundAssignmentOp());
2159
2160 if (Op >= BinaryOperator::AndAssign)
2161 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2162 else
2163 ((int&) Op) -= BinaryOperator::MulAssign;
2164
2165 // Perform a load (the LHS). This performs the checks for
2166 // null dereferences, and so on.
2167 NodeSet Tmp3;
2168 RVal location = GetRVal(St, LHS);
2169 EvalLoad(Tmp3, LHS, *I2, St, location);
2170
2171 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2172
2173 St = GetState(*I3);
2174 RVal V = GetRVal(St, LHS);
2175
2176 // Propagate undefined values (left-side).
2177 if (V.isUndef()) {
2178 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2179 continue;
2180 }
2181
2182 // Propagate unknown values (left and right-side).
2183 if (RightV.isUnknown() || V.isUnknown()) {
2184 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2185 UnknownVal());
2186 continue;
2187 }
2188
2189 // At this point:
2190 //
2191 // The LHS is not Undef/Unknown.
2192 // The RHS is not Unknown.
2193
2194 // Get the computation type.
2195 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2196
2197 // Perform promotions.
2198 V = EvalCast(V, CTy);
2199 RightV = EvalCast(RightV, CTy);
2200
2201 // Evaluate operands and promote to result type.
2202
2203 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2204 && RHS->getType()->isIntegerType()) {
2205
2206 if (CheckDivideZero(B, St, *I3, RightV))
2207 continue;
2208 }
2209 else if (RightV.isUndef()) {
2210
2211 // Propagate undefined values (right-side).
2212
2213 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2214 continue;
2215 }
2216
2217 // Compute the result of the operation.
2218
2219 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2220
2221 if (Result.isUndef()) {
2222
2223 // The operands were not undefined, but the result is undefined.
2224
2225 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2226 UndefNode->markAsSink();
2227 UndefResults.insert(UndefNode);
2228 }
2229
2230 continue;
2231 }
2232
2233 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2234 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002235 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002236 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002237}
Ted Kremenekee985462008-01-16 18:18:48 +00002238
2239//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002240// Transfer-function Helpers.
2241//===----------------------------------------------------------------------===//
2242
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002243void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002244 BinaryOperator::Opcode Op,
2245 NonLVal L, NonLVal R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002246 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002247
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002248 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002249 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2250
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002251 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002252 MakeNode(Dst, Ex, Pred, *I);
2253}
2254
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002255void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002256 Expr* Ex, BinaryOperator::Opcode Op,
2257 NonLVal L, NonLVal R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002258
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002259 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002260 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002261}
2262
2263//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002264// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002265//===----------------------------------------------------------------------===//
2266
Ted Kremenekaa66a322008-01-16 21:46:15 +00002267#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002268static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002269static SourceManager* GraphPrintSourceManager;
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 Kremeneka3fadfc2008-02-14 22:54:53 +00002276 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2277
2278 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002279 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002280 GraphPrintCheckerState->isUndefDeref(N) ||
2281 GraphPrintCheckerState->isUndefStore(N) ||
2282 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002283 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2284 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002285 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002286 GraphPrintCheckerState->isBadCall(N) ||
2287 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002288 return "color=\"red\",style=\"filled\"";
2289
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002290 if (GraphPrintCheckerState->isNoReturnCall(N))
2291 return "color=\"blue\",style=\"filled\"";
2292
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002293 return "";
2294 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002295
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002296 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002297 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002298
2299 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002300 ProgramPoint Loc = N->getLocation();
2301
2302 switch (Loc.getKind()) {
2303 case ProgramPoint::BlockEntranceKind:
2304 Out << "Block Entrance: B"
2305 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2306 break;
2307
2308 case ProgramPoint::BlockExitKind:
2309 assert (false);
2310 break;
2311
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002312 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002313 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002314 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002315 const PostStmt& L = cast<PostStmt>(Loc);
2316 Stmt* S = L.getStmt();
2317 SourceLocation SLoc = S->getLocStart();
2318
2319 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2320 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002321
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002322 if (SLoc.isFileID()) {
2323 Out << "\\lline="
2324 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2325 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2326 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002327
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002328 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002329 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002330 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002331 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002332 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002333 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002334 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002335 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002336 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2337 Out << "\\|Explicit divide-by zero or undefined value.";
2338 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2339 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002340 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002341 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002342 else if (GraphPrintCheckerState->isNoReturnCall(N))
2343 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002344 else if (GraphPrintCheckerState->isBadCall(N))
2345 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002346 else if (GraphPrintCheckerState->isUndefArg(N))
2347 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002348
Ted Kremenekaa66a322008-01-16 21:46:15 +00002349 break;
2350 }
2351
2352 default: {
2353 const BlockEdge& E = cast<BlockEdge>(Loc);
2354 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2355 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002356
2357 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002358
2359 SourceLocation SLoc = T->getLocStart();
2360
Ted Kremenekb38911f2008-01-30 23:03:39 +00002361 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002362
Ted Kremenekb38911f2008-01-30 23:03:39 +00002363 E.getSrc()->printTerminator(Out);
2364
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002365 if (SLoc.isFileID()) {
2366 Out << "\\lline="
2367 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2368 << GraphPrintSourceManager->getColumnNumber(SLoc);
2369 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002370
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002371 if (isa<SwitchStmt>(T)) {
2372 Stmt* Label = E.getDst()->getLabel();
2373
2374 if (Label) {
2375 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2376 Out << "\\lcase ";
2377 C->getLHS()->printPretty(Out);
2378
2379 if (Stmt* RHS = C->getRHS()) {
2380 Out << " .. ";
2381 RHS->printPretty(Out);
2382 }
2383
2384 Out << ":";
2385 }
2386 else {
2387 assert (isa<DefaultStmt>(Label));
2388 Out << "\\ldefault:";
2389 }
2390 }
2391 else
2392 Out << "\\l(implicit) default:";
2393 }
2394 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002395 // FIXME
2396 }
2397 else {
2398 Out << "\\lCondition: ";
2399 if (*E.getSrc()->succ_begin() == E.getDst())
2400 Out << "true";
2401 else
2402 Out << "false";
2403 }
2404
2405 Out << "\\l";
2406 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002407
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002408 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2409 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002410 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002411 }
2412 }
2413
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002414 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002415
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002416 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2417 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002418
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002419 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002420 return Out.str();
2421 }
2422};
2423} // end llvm namespace
2424#endif
2425
Ted Kremenekffe0f432008-03-07 22:58:01 +00002426#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002427
2428template <typename ITERATOR>
2429GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2430
2431template <>
2432GRExprEngine::NodeTy*
2433GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2434 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2435 return I->first;
2436}
2437
Ted Kremenekffe0f432008-03-07 22:58:01 +00002438template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002439static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002440 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002441
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002442 llvm::SmallPtrSet<void*,10> CachedSources;
2443
2444 for ( ; I != E; ++I ) {
2445 GRExprEngine::NodeTy* N = GetGraphNode(I);
2446 void* p = N->getLocation().getRawData();
2447
2448 if (CachedSources.count(p))
2449 continue;
2450
2451 CachedSources.insert(p);
2452
2453 Sources.push_back(N);
2454 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002455}
2456#endif
2457
2458void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002459#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002460 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002461 std::vector<NodeTy*> Src;
2462
2463 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002464 AddSources(Src, null_derefs_begin(), null_derefs_end());
2465 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2466 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2467 AddSources(Src, undef_results_begin(), undef_results_end());
2468 AddSources(Src, bad_calls_begin(), bad_calls_end());
2469 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002470 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002471
Ted Kremenekcb612922008-04-18 19:23:43 +00002472 // The new way.
2473 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2474 (*I)->GetErrorNodes(Src);
2475
2476
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002477 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002478 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002479 else {
2480 GraphPrintCheckerState = this;
2481 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002482
Ted Kremenekffe0f432008-03-07 22:58:01 +00002483 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002484
2485 GraphPrintCheckerState = NULL;
2486 GraphPrintSourceManager = NULL;
2487 }
2488#endif
2489}
2490
2491void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2492#ifndef NDEBUG
2493 GraphPrintCheckerState = this;
2494 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002495
Ted Kremenek493d7a22008-03-11 18:25:33 +00002496 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2497
2498 if (!TrimmedG)
2499 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2500 else {
2501 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2502 delete TrimmedG;
2503 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002504
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002505 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002506 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002507#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002508}