blob: 18c5a5825ec579cc5bb7602c1241cb982916899d [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
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000359 // FIXME: ChooseExpr is really a constant. We need to fix
360 // the CFG do not model them as explicit control-flow.
361
362 case Stmt::ChooseExprClass: { // __builtin_choose_expr
363 ChooseExpr* C = cast<ChooseExpr>(S);
364 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
365 break;
366 }
367
368 case Stmt::CompoundAssignOperatorClass:
369 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
370 break;
371
372 case Stmt::ConditionalOperatorClass: { // '?' operator
373 ConditionalOperator* C = cast<ConditionalOperator>(S);
374 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
375 break;
376 }
377
378 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000379 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000380 break;
381
382 case Stmt::DeclStmtClass:
383 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
384 break;
385
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000386 case Stmt::ImplicitCastExprClass:
387 case Stmt::ExplicitCastExprClass: {
388 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 VisitCast(C, C->getSubExpr(), Pred, Dst);
390 break;
391 }
392
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000393 case Stmt::MemberExprClass: {
394 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
395 break;
396 }
397
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000398 case Stmt::ObjCMessageExprClass: {
399 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
400 break;
401 }
402
403 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 case Stmt::SizeOfAlignOfTypeExprClass:
412 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
417
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000418 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000419
420 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
421 // probably just remove these from the CFG.
422 assert (!SE->getSubStmt()->body_empty());
423
424 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
425 MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
426 else
427 Dst.Add(Pred);
428
429 break;
430 }
431
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000432 case Stmt::UnaryOperatorClass:
433 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000434 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000435 }
436}
437
438void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
439
440 Ex = Ex->IgnoreParens();
441
442 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
443 Dst.Add(Pred);
444 return;
445 }
446
447 switch (Ex->getStmtClass()) {
448 default:
449 Visit(Ex, Pred, Dst);
450 return;
451
452 case Stmt::ArraySubscriptExprClass:
453 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
454 return;
455
456 case Stmt::DeclRefExprClass:
457 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
458 return;
459
460 case Stmt::UnaryOperatorClass:
461 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
462 return;
463
464 case Stmt::MemberExprClass:
465 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
466 return;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000467 }
468}
469
470//===----------------------------------------------------------------------===//
471// Block entrance. (Update counters).
472//===----------------------------------------------------------------------===//
473
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000474bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000475 GRBlockCounter BC) {
476
477 return BC.getNumVisited(B->getBlockID()) < 3;
478}
479
480//===----------------------------------------------------------------------===//
481// Branch processing.
482//===----------------------------------------------------------------------===//
483
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000484const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000485 Stmt* Terminator,
486 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000487
488 switch (Terminator->getStmtClass()) {
489 default:
490 return St;
491
492 case Stmt::BinaryOperatorClass: { // '&&' and '||'
493
494 BinaryOperator* B = cast<BinaryOperator>(Terminator);
495 BinaryOperator::Opcode Op = B->getOpcode();
496
497 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
498
499 // For &&, if we take the true branch, then the value of the whole
500 // expression is that of the RHS expression.
501 //
502 // For ||, if we take the false branch, then the value of the whole
503 // expression is that of the RHS expression.
504
505 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
506 (Op == BinaryOperator::LOr && !branchTaken)
507 ? B->getRHS() : B->getLHS();
508
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000509 return SetBlkExprRVal(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000510 }
511
512 case Stmt::ConditionalOperatorClass: { // ?:
513
514 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
515
516 // For ?, if branchTaken == true then the value is either the LHS or
517 // the condition itself. (GNU extension).
518
519 Expr* Ex;
520
521 if (branchTaken)
522 Ex = C->getLHS() ? C->getLHS() : C->getCond();
523 else
524 Ex = C->getRHS();
525
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000526 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000527 }
528
529 case Stmt::ChooseExprClass: { // ?:
530
531 ChooseExpr* C = cast<ChooseExpr>(Terminator);
532
533 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000534 return SetBlkExprRVal(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000535 }
536 }
537}
538
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000539void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000540 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000541
Ted Kremeneke7d22112008-02-11 19:21:59 +0000542 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000543 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000544 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000545
Ted Kremenekb2331832008-02-15 22:29:00 +0000546 // Check for NULL conditions; e.g. "for(;;)"
547 if (!Condition) {
548 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000549 return;
550 }
551
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000552 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000553
554 switch (V.getBaseKind()) {
555 default:
556 break;
557
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000558 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000559 builder.generateNode(MarkBranch(PrevState, Term, true), true);
560 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000561 return;
562
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000563 case RVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000564 NodeTy* N = builder.generateNode(PrevState, true);
565
566 if (N) {
567 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000568 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000569 }
570
571 builder.markInfeasible(false);
572 return;
573 }
574 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000575
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000576 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000577
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000578 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000579 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000580
581 if (isFeasible)
582 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000583 else
584 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000585
586 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000587
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000588 isFeasible = false;
589 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000590
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000591 if (isFeasible)
592 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000593 else
594 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000595}
596
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000597/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000598/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000599void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000600
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000601 const GRState* St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000602 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000603
604 // Three possibilities:
605 //
606 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000607 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000608 // (3) We have no clue about the label. Dispatch to all targets.
609 //
610
611 typedef IndirectGotoNodeBuilder::iterator iterator;
612
613 if (isa<lval::GotoLabel>(V)) {
614 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
615
616 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000617 if (I.getLabel() == L) {
618 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000619 return;
620 }
621 }
622
623 assert (false && "No block with label.");
624 return;
625 }
626
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000627 if (isa<lval::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000628 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000629 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000630 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000631 return;
632 }
633
634 // This is really a catch-all. We don't support symbolics yet.
635
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000636 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000637
638 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000639 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000640}
Ted Kremenekf233d482008-02-05 00:26:40 +0000641
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000642
643void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
644 NodeTy* Pred, NodeSet& Dst) {
645
646 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
647
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000648 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000649 RVal X = GetBlkExprRVal(St, Ex);
650
651 assert (X.isUndef());
652
653 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
654
655 assert (SE);
656
657 X = GetBlkExprRVal(St, SE);
658
659 // Make sure that we invalidate the previous binding.
660 MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
661}
662
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000663/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
664/// nodes by processing the 'effects' of a switch statement.
665void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
666
667 typedef SwitchNodeBuilder::iterator iterator;
668
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000669 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000670 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000671 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000672
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000673 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000674 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000675 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000676 return;
677 }
678
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000679 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000680
681 // While most of this can be assumed (such as the signedness), having it
682 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000683
Chris Lattner98be4942008-03-05 18:54:05 +0000684 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000685
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000686 APSInt V1(bits, false);
687 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000688 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000689
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000690 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691
692 CaseStmt* Case = cast<CaseStmt>(I.getCase());
693
694 // Evaluate the case.
695 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
696 assert (false && "Case condition must evaluate to an integer constant.");
697 return;
698 }
699
700 // Get the RHS of the case, if it exists.
701
702 if (Expr* E = Case->getRHS()) {
703 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
704 assert (false &&
705 "Case condition (RHS) must evaluate to an integer constant.");
706 return ;
707 }
708
709 assert (V1 <= V2);
710 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000711 else
712 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000713
714 // FIXME: Eventually we should replace the logic below with a range
715 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000716 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717
Ted Kremenek14a11402008-03-17 22:17:56 +0000718 do {
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000719 nonlval::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000721 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722
723 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000724
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000725 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000726 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
728 if (isFeasible) {
729 builder.generateCaseStmtNode(I, StNew);
730
731 // If CondV evaluates to a constant, then we know that this
732 // is the *only* case that we can take, so stop evaluating the
733 // others.
734 if (isa<nonlval::ConcreteInt>(CondV))
735 return;
736 }
737
738 // Now "assume" that the case doesn't match. Add this state
739 // to the default state (if it is feasible).
740
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000741 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000742 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000743
Ted Kremenek5014ab12008-04-23 05:03:18 +0000744 if (isFeasible) {
745 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000747 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000748
Ted Kremenek14a11402008-03-17 22:17:56 +0000749 // Concretize the next value in the range.
750 if (V1 == V2)
751 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752
Ted Kremenek14a11402008-03-17 22:17:56 +0000753 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000754 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000755
756 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000757 }
758
759 // If we reach here, than we know that the default branch is
760 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000761 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000762}
763
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000764//===----------------------------------------------------------------------===//
765// Transfer functions: logical operations ('&&', '||').
766//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000767
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000768void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000769 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000770
Ted Kremenek05a23782008-02-26 19:05:15 +0000771 assert (B->getOpcode() == BinaryOperator::LAnd ||
772 B->getOpcode() == BinaryOperator::LOr);
773
774 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
775
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000776 const GRState* St = GetState(Pred);
Ted Kremenek05a23782008-02-26 19:05:15 +0000777 RVal X = GetBlkExprRVal(St, B);
778
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000779 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000780
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000781 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000782
783 assert (Ex);
784
785 if (Ex == B->getRHS()) {
786
787 X = GetBlkExprRVal(St, Ex);
788
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000789 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000790
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000791 if (X.isUndef()) {
Ted Kremenek0e561a32008-03-21 21:30:14 +0000792 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000793 return;
794 }
795
Ted Kremenek05a23782008-02-26 19:05:15 +0000796 // We took the RHS. Because the value of the '&&' or '||' expression must
797 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
798 // or 1. Alternatively, we could take a lazy approach, and calculate this
799 // value later when necessary. We don't have the machinery in place for
800 // this right now, and since most logical expressions are used for branches,
801 // the payoff is not likely to be large. Instead, we do eager evaluation.
802
803 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000804 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000805
806 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000807 MakeNode(Dst, B, Pred,
808 SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000809
810 isFeasible = false;
811 NewState = Assume(St, X, false, isFeasible);
812
813 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000814 MakeNode(Dst, B, Pred,
815 SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000816 }
817 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000818 // We took the LHS expression. Depending on whether we are '&&' or
819 // '||' we know what the value of the expression is via properties of
820 // the short-circuiting.
821
822 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek0e561a32008-03-21 21:30:14 +0000823 MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000824 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000825}
Ted Kremenek05a23782008-02-26 19:05:15 +0000826
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000827//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000828// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000829//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000830
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000831void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst,
832 bool asLVal) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000833
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000834 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000835 RVal X = RVal::MakeVal(getBasicVals(), D);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000836
837 if (asLVal)
838 MakeNode(Dst, D, Pred, SetRVal(St, D, cast<LVal>(X)));
839 else {
840 RVal V = isa<lval::DeclVal>(X) ? GetRVal(St, cast<LVal>(X)) : X;
841 MakeNode(Dst, D, Pred, SetRVal(St, D, V));
842 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000843}
844
Ted Kremenek540cbe22008-04-22 04:56:29 +0000845/// VisitArraySubscriptExpr - Transfer function for array accesses
846void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
847 NodeSet& Dst, bool asLVal) {
848
849 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000850 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek540cbe22008-04-22 04:56:29 +0000851
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000852 // Always visit the base as an LVal expression. This computes the
853 // abstract address of the base object.
854 NodeSet Tmp;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000855
Ted Kremenek0e470a52008-05-09 23:45:33 +0000856 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000857 Visit(Base, Pred, Tmp);
858 else
859 VisitLVal(Base, Pred, Tmp);
860
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000861 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000862
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000863 // Evaluate the index.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000864
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000865 NodeSet Tmp2;
866 Visit(Idx, *I1, Tmp2);
867
868 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
869
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000870 const GRState* St = GetState(*I2);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000871 RVal BaseV = GetRVal(St, Base);
872 RVal IdxV = GetRVal(St, Idx);
Ted Kremenekc52c89a2008-04-30 21:05:35 +0000873
874 // If IdxV is 0, return just BaseV.
875
876 bool useBase = false;
877
878 if (nonlval::ConcreteInt* IdxInt = dyn_cast<nonlval::ConcreteInt>(&IdxV))
879 useBase = IdxInt->getValue() == 0;
880
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000881 RVal V = useBase ? BaseV : lval::ArrayOffset::Make(getBasicVals(), BaseV,IdxV);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000882
883 if (asLVal)
884 MakeNode(Dst, A, *I2, SetRVal(St, A, V));
885 else
886 EvalLoad(Dst, A, *I2, St, V);
887 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000888 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000889}
890
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000891/// VisitMemberExpr - Transfer function for member expressions.
892void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
893 NodeSet& Dst, bool asLVal) {
894
895 Expr* Base = M->getBase()->IgnoreParens();
896
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000897 // Always visit the base as an LVal expression. This computes the
898 // abstract address of the base object.
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000899 NodeSet Tmp;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000900
Ted Kremenekee90dba2008-04-30 22:17:15 +0000901 if (asLVal) {
902
Ted Kremenek0e470a52008-05-09 23:45:33 +0000903 if (LVal::IsLValType(Base->getType())) // Base always is an LVal.
Ted Kremenekee90dba2008-04-30 22:17:15 +0000904 Visit(Base, Pred, Tmp);
905 else
906 VisitLVal(Base, Pred, Tmp);
907
908 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000909 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000910 RVal BaseV = GetRVal(St, Base);
911
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000912 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000913 M->getMemberDecl());
914
915 MakeNode(Dst, M, *I, SetRVal(St, M, V));
916 }
917
918 return;
919 }
920
921 // Evaluate the base. Can be an LVal or NonLVal (depends on whether
922 // or not isArrow() is true).
923 Visit(Base, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000924
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000925 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekee90dba2008-04-30 22:17:15 +0000926
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000927 const GRState* St = GetState(*I);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000928 RVal BaseV = GetRVal(St, Base);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000929
Ted Kremenek0e470a52008-05-09 23:45:33 +0000930 if (LVal::IsLValType(Base->getType())) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000931
Ted Kremenekee90dba2008-04-30 22:17:15 +0000932 assert (M->isArrow());
933
Ted Kremenek6297a8e2008-07-18 05:53:58 +0000934 RVal V = lval::FieldOffset::Make(getBasicVals(), GetRVal(St, Base),
Ted Kremenekee90dba2008-04-30 22:17:15 +0000935 M->getMemberDecl());
936
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000937 EvalLoad(Dst, M, *I, St, V);
Ted Kremenekee90dba2008-04-30 22:17:15 +0000938 }
939 else {
940
941 assert (!M->isArrow());
942
943 if (BaseV.isUnknownOrUndef()) {
944 MakeNode(Dst, M, *I, SetRVal(St, M, BaseV));
945 continue;
946 }
947
948 // FIXME: Implement nonlval objects representing struct temporaries.
949 assert (isa<NonLVal>(BaseV));
950 MakeNode(Dst, M, *I, SetRVal(St, M, UnknownVal()));
951 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000952 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000953}
954
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000955void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000956 const GRState* St, RVal location, RVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000957
958 assert (Builder && "GRStmtNodeBuilder must be defined.");
959
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960 // Evaluate the location (checks for bad dereferences).
961 St = EvalLocation(Ex, Pred, St, location);
962
963 if (!St)
964 return;
965
966 // Proceed with the store.
967
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000968 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000969
Ted Kremenek186350f2008-04-23 20:12:28 +0000970 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
971 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000972
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000973 assert (!location.isUndef());
Ted Kremenek13922612008-04-16 20:40:59 +0000974
Ted Kremenek729a9a22008-07-17 23:15:45 +0000975 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000976
977 // Handle the case where no nodes where generated. Auto-generate that
978 // contains the updated state if we aren't generating sinks.
979
Ted Kremenekb0533962008-04-18 20:35:30 +0000980 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000981 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000982 location, Val);
983}
984
985void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000986 const GRState* St, RVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000987 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000988
989 // Evaluate the location (checks for bad dereferences).
990
991 St = EvalLocation(Ex, Pred, St, location, true);
992
993 if (!St)
994 return;
995
996 // Proceed with the load.
997
998 // FIXME: Currently symbolic analysis "generates" new symbols
999 // for the contents of values. We need a better approach.
1000
1001 // FIXME: The "CheckOnly" option exists only because Array and Field
1002 // loads aren't fully implemented. Eventually this option will go away.
1003
Ted Kremenek436f2b92008-04-30 04:23:07 +00001004 if (CheckOnly)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001005 MakeNode(Dst, Ex, Pred, St);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001006 else if (location.isUnknown()) {
1007 // This is important. We must nuke the old binding.
1008 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, UnknownVal()));
1009 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010 else
1011 MakeNode(Dst, Ex, Pred, SetRVal(St, Ex, GetRVal(St, cast<LVal>(location),
1012 Ex->getType())));
1013}
1014
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001015const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
1016 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +00001017 RVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001018
1019 // Check for loads/stores from/to undefined values.
1020 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001021 ProgramPoint::Kind K =
1022 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1023
1024 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001025 Succ->markAsSink();
1026 UndefDeref.insert(Succ);
1027 }
1028
1029 return NULL;
1030 }
1031
1032 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1033 if (location.isUnknown())
1034 return St;
1035
1036 // During a load, one of two possible situations arise:
1037 // (1) A crash, because the location (pointer) was NULL.
1038 // (2) The location (pointer) is not NULL, and the dereference works.
1039 //
1040 // We add these assumptions.
1041
1042 LVal LV = cast<LVal>(location);
1043
1044 // "Assume" that the pointer is not NULL.
1045
1046 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001047 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001048
1049 // "Assume" that the pointer is NULL.
1050
1051 bool isFeasibleNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001052 const GRState* StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053
1054 if (isFeasibleNull) {
1055
1056 // We don't use "MakeNode" here because the node will be a sink
1057 // and we have no intention of processing it later.
1058
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001059 ProgramPoint::Kind K =
1060 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1061
1062 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001063
1064 if (NullNode) {
1065
1066 NullNode->markAsSink();
1067
1068 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1069 else ExplicitNullDeref.insert(NullNode);
1070 }
1071 }
1072
1073 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001074}
1075
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001076//===----------------------------------------------------------------------===//
1077// Transfer function: Function calls.
1078//===----------------------------------------------------------------------===//
1079
Ted Kremenekde434242008-02-19 01:44:53 +00001080void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001081 CallExpr::arg_iterator AI,
1082 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +00001083 NodeSet& Dst) {
1084
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001085 // Process the arguments.
1086
1087 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +00001088
Ted Kremeneked2d2ed2008-03-04 00:56:45 +00001089 NodeSet DstTmp;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001090 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001091 ++AI;
1092
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001093 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001094 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +00001095
1096 return;
1097 }
1098
1099 // If we reach here we have processed all of the arguments. Evaluate
1100 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001101
Ted Kremenek994a09b2008-02-25 21:16:03 +00001102 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001103 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001104
Ted Kremenek994a09b2008-02-25 21:16:03 +00001105 VisitLVal(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001106
Ted Kremenekde434242008-02-19 01:44:53 +00001107 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001108 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1109
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001110 const GRState* St = GetState(*DI);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001111 RVal L = GetRVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001112
Ted Kremeneka1354a52008-03-03 16:47:31 +00001113 // FIXME: Add support for symbolic function calls (calls involving
1114 // function pointer values that are symbolic).
1115
1116 // Check for undefined control-flow or calls to NULL.
1117
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00001118 if (L.isUndef() || isa<lval::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001119 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001120
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001121 if (N) {
1122 N->markAsSink();
1123 BadCalls.insert(N);
1124 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001125
Ted Kremenekde434242008-02-19 01:44:53 +00001126 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001127 }
1128
1129 // Check for the "noreturn" attribute.
1130
1131 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1132
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001133 if (isa<lval::FuncVal>(L)) {
1134
1135 FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl();
1136
1137 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001138 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001139 else {
1140 // HACK: Some functions are not marked noreturn, and don't return.
1141 // Here are a few hardwired ones. If this takes too long, we can
1142 // potentially cache these results.
1143 const char* s = FD->getIdentifier()->getName();
1144 unsigned n = strlen(s);
1145
1146 switch (n) {
1147 default:
1148 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001149
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001150 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001151 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1152 break;
1153
1154 case 5:
1155 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
1156 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001157
1158 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001159 if (!memcmp(s, "Assert", 6)) {
1160 Builder->BuildSinks = true;
1161 break;
1162 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001163
1164 // FIXME: This is just a wrapper around throwing an exception.
1165 // Eventually inter-procedural analysis should handle this easily.
1166 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1167
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001168 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001169
1170 case 7:
1171 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1172 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001173
Ted Kremenekf47bb782008-04-30 17:54:04 +00001174 case 8:
1175 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1176 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001177
1178 case 12:
1179 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1180 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001181
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001182 case 14:
1183 if (!memcmp(s, "dtrace_assfail", 14)) Builder->BuildSinks = true;
1184 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001185
1186 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001187 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1188 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001189 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001190
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001191 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001192 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001193
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001194 }
1195 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001196
1197 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001198
1199 if (isa<lval::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001200
1201 IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier();
1202
Ted Kremenek186350f2008-04-23 20:12:28 +00001203 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001204 switch (id) {
1205 case Builtin::BI__builtin_expect: {
1206 // For __builtin_expect, just return the value of the subexpression.
1207 assert (CE->arg_begin() != CE->arg_end());
1208 RVal X = GetRVal(St, *(CE->arg_begin()));
Ted Kremenek0e561a32008-03-21 21:30:14 +00001209 MakeNode(Dst, CE, *DI, SetRVal(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001210 continue;
1211 }
1212
1213 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001214 break;
1215 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001216 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001217
Ted Kremenek186350f2008-04-23 20:12:28 +00001218 // Check any arguments passed-by-value against being undefined.
1219
1220 bool badArg = false;
1221
1222 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1223 I != E; ++I) {
1224
1225 if (GetRVal(GetState(*DI), *I).isUndef()) {
1226 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001227
Ted Kremenek186350f2008-04-23 20:12:28 +00001228 if (N) {
1229 N->markAsSink();
1230 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001231 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001232
Ted Kremenek186350f2008-04-23 20:12:28 +00001233 badArg = true;
1234 break;
1235 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001236 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001237
1238 if (badArg)
1239 continue;
1240
1241 // Dispatch to the plug-in transfer function.
1242
1243 unsigned size = Dst.size();
1244 SaveOr OldHasGen(Builder->HasGeneratedNode);
1245 EvalCall(Dst, CE, L, *DI);
1246
1247 // Handle the case where no nodes where generated. Auto-generate that
1248 // contains the updated state if we aren't generating sinks.
1249
1250 if (!Builder->BuildSinks && Dst.size() == size &&
1251 !Builder->HasGeneratedNode)
1252 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001253 }
1254}
1255
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001256//===----------------------------------------------------------------------===//
1257// Transfer function: Objective-C message expressions.
1258//===----------------------------------------------------------------------===//
1259
1260void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1261 NodeSet& Dst){
1262
1263 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1264 Pred, Dst);
1265}
1266
1267void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
1268 ObjCMessageExpr::arg_iterator AI,
1269 ObjCMessageExpr::arg_iterator AE,
1270 NodeTy* Pred, NodeSet& Dst) {
1271 if (AI == AE) {
1272
1273 // Process the receiver.
1274
1275 if (Expr* Receiver = ME->getReceiver()) {
1276 NodeSet Tmp;
1277 Visit(Receiver, Pred, Tmp);
1278
1279 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1280 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1281
1282 return;
1283 }
1284
1285 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1286 return;
1287 }
1288
1289 NodeSet Tmp;
1290 Visit(*AI, Pred, Tmp);
1291
1292 ++AI;
1293
1294 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1295 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1296}
1297
1298void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1299 NodeTy* Pred,
1300 NodeSet& Dst) {
1301
1302 // FIXME: More logic for the processing the method call.
1303
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001304 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001305 bool RaisesException = false;
1306
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001307
1308 if (Expr* Receiver = ME->getReceiver()) {
1309
1310 RVal L = GetRVal(St, Receiver);
1311
1312 // Check for undefined control-flow or calls to NULL.
1313
1314 if (L.isUndef()) {
1315 NodeTy* N = Builder->generateNode(ME, St, Pred);
1316
1317 if (N) {
1318 N->markAsSink();
1319 UndefReceivers.insert(N);
1320 }
1321
1322 return;
1323 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001324
1325 // Check if the "raise" message was sent.
1326 if (ME->getSelector() == RaiseSel)
1327 RaisesException = true;
1328 }
1329 else {
1330
1331 IdentifierInfo* ClsName = ME->getClassName();
1332 Selector S = ME->getSelector();
1333
1334 // Check for special instance methods.
1335
1336 if (!NSExceptionII) {
1337 ASTContext& Ctx = getContext();
1338
1339 NSExceptionII = &Ctx.Idents.get("NSException");
1340 }
1341
1342 if (ClsName == NSExceptionII) {
1343
1344 enum { NUM_RAISE_SELECTORS = 2 };
1345
1346 // Lazily create a cache of the selectors.
1347
1348 if (!NSExceptionInstanceRaiseSelectors) {
1349
1350 ASTContext& Ctx = getContext();
1351
1352 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1353
1354 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1355 unsigned idx = 0;
1356
1357 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001358 II.push_back(&Ctx.Idents.get("raise"));
1359 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001360 NSExceptionInstanceRaiseSelectors[idx++] =
1361 Ctx.Selectors.getSelector(II.size(), &II[0]);
1362
1363 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001364 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001365 NSExceptionInstanceRaiseSelectors[idx++] =
1366 Ctx.Selectors.getSelector(II.size(), &II[0]);
1367 }
1368
1369 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1370 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1371 RaisesException = true; break;
1372 }
1373 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001374 }
1375
1376 // Check for any arguments that are uninitialized/undefined.
1377
1378 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1379 I != E; ++I) {
1380
1381 if (GetRVal(St, *I).isUndef()) {
1382
1383 // Generate an error node for passing an uninitialized/undefined value
1384 // as an argument to a message expression. This node is a sink.
1385 NodeTy* N = Builder->generateNode(ME, St, Pred);
1386
1387 if (N) {
1388 N->markAsSink();
1389 MsgExprUndefArgs[N] = *I;
1390 }
1391
1392 return;
1393 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001394 }
1395
1396 // Check if we raise an exception. For now treat these as sinks. Eventually
1397 // we will want to handle exceptions properly.
1398
1399 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1400
1401 if (RaisesException)
1402 Builder->BuildSinks = true;
1403
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001404 // Dispatch to plug-in transfer function.
1405
1406 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001407 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001408
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001409 EvalObjCMessageExpr(Dst, ME, Pred);
1410
1411 // Handle the case where no nodes where generated. Auto-generate that
1412 // contains the updated state if we aren't generating sinks.
1413
Ted Kremenekb0533962008-04-18 20:35:30 +00001414 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001415 MakeNode(Dst, ME, Pred, St);
1416}
1417
1418//===----------------------------------------------------------------------===//
1419// Transfer functions: Miscellaneous statements.
1420//===----------------------------------------------------------------------===//
1421
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001422void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +00001423
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001424 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001425 QualType T = CastE->getType();
1426
Ted Kremenek65cfb732008-03-04 22:16:08 +00001427 if (T->isReferenceType())
1428 VisitLVal(Ex, Pred, S1);
1429 else
1430 Visit(Ex, Pred, S1);
1431
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001432 // Check for casting to "void".
1433 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001434
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001435 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001436 Dst.Add(*I1);
1437
Ted Kremenek874d63f2008-01-24 02:02:54 +00001438 return;
1439 }
1440
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001441 // FIXME: The rest of this should probably just go into EvalCall, and
1442 // let the transfer function object be responsible for constructing
1443 // nodes.
1444
1445 QualType ExTy = Ex->getType();
1446
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001447 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001448 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001449 const GRState* St = GetState(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001450 RVal V = GetRVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001451
1452 // Unknown?
1453
1454 if (V.isUnknown()) {
1455 Dst.Add(N);
1456 continue;
1457 }
1458
1459 // Undefined?
1460
1461 if (V.isUndef()) {
1462 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1463 continue;
1464 }
1465
1466 // Check for casts from pointers to integers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001467 if (T->isIntegerType() && LVal::IsLValType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001468 unsigned bits = getContext().getTypeSize(ExTy);
1469
1470 // FIXME: Determine if the number of bits of the target type is
1471 // equal or exceeds the number of bits to store the pointer value.
1472 // If not, flag an error.
1473
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001474 V = nonlval::LValAsInteger::Make(getBasicVals(), cast<LVal>(V), bits);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001475 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1476 continue;
1477 }
1478
1479 // Check for casts from integers to pointers.
Ted Kremenek0e470a52008-05-09 23:45:33 +00001480 if (LVal::IsLValType(T) && ExTy->isIntegerType())
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001481 if (nonlval::LValAsInteger *LV = dyn_cast<nonlval::LValAsInteger>(&V)) {
1482 // Just unpackage the lval and return it.
1483 V = LV->getLVal();
1484 MakeNode(Dst, CastE, N, SetRVal(St, CastE, V));
1485 continue;
1486 }
1487
1488 // All other cases.
Ted Kremenek65cfb732008-03-04 22:16:08 +00001489
Ted Kremenek0e561a32008-03-21 21:30:14 +00001490 MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001491 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001492}
1493
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001494void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
1495 VisitDeclStmtAux(DS, DS->getDecl(), Pred, Dst);
1496}
1497
1498void GRExprEngine::VisitDeclStmtAux(DeclStmt* DS, ScopedDecl* D,
1499 NodeTy* Pred, NodeSet& Dst) {
1500
1501 if (!D)
1502 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001503
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001504 if (!isa<VarDecl>(D)) {
1505 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1506 return;
1507 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001508
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001509 const VarDecl* VD = dyn_cast<VarDecl>(D);
1510
1511 // FIXME: Add support for local arrays.
1512 if (VD->getType()->isArrayType()) {
1513 VisitDeclStmtAux(DS, D->getNextDeclarator(), Pred, Dst);
1514 return;
1515 }
1516
1517 Expr* Ex = const_cast<Expr*>(VD->getInit());
1518
1519 // FIXME: static variables may have an initializer, but the second
1520 // time a function is called those values may not be current.
1521 NodeSet Tmp;
1522
1523 if (Ex) Visit(Ex, Pred, Tmp);
1524 if (Tmp.empty()) Tmp.Add(Pred);
1525
1526 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001527
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001528 const GRState* St = GetState(*I);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001529
1530 if (!Ex && VD->hasGlobalStorage()) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +00001531
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001532 // Handle variables with global storage and no initializers.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001533
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001534 // FIXME: static variables may have an initializer, but the second
1535 // time a function is called those values may not be current.
1536
1537
1538 // In this context, Static => Local variable.
1539
1540 assert (!VD->getStorageClass() == VarDecl::Static ||
1541 !VD->isFileVarDecl());
1542
1543 // If there is no initializer, set the value of the
1544 // variable to "Undefined".
1545
1546 if (VD->getStorageClass() == VarDecl::Static) {
1547
1548 // C99: 6.7.8 Initialization
1549 // If an object that has static storage duration is not initialized
1550 // explicitly, then:
1551 // —if it has pointer type, it is initialized to a null pointer;
1552 // —if it has arithmetic type, it is initialized to (positive or
1553 // unsigned) zero;
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001554
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001555 // FIXME: Handle structs. Now we treat their values as unknown.
Ted Kremenekfcb092b2008-03-04 20:40:11 +00001556
1557 QualType T = VD->getType();
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001558
Ted Kremenek0e470a52008-05-09 23:45:33 +00001559 if (LVal::IsLValType(T))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001560 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001561 lval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001562 else if (T->isIntegerType())
1563 St = SetRVal(St, lval::DeclVal(VD),
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001564 nonlval::ConcreteInt(getBasicVals().getValue(0, T)));
Ted Kremenek16d81562008-03-04 04:18:04 +00001565
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001566 // FIXME: Handle structs. Now we treat them as unknown. What
1567 // we need to do is treat their members as unknown.
Ted Kremenekb0ab2122008-02-27 19:21:33 +00001568 }
Ted Kremenek403c1812008-01-28 22:51:57 +00001569 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001570 else {
1571
1572 // FIXME: Handle structs. Now we treat them as unknown. What
1573 // we need to do is treat their members as unknown.
Ted Kremenek9de04c42008-01-24 20:55:43 +00001574
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001575 QualType T = VD->getType();
1576
Ted Kremenek0e470a52008-05-09 23:45:33 +00001577 if (LVal::IsLValType(T) || T->isIntegerType()) {
Ted Kremenekf47bb782008-04-30 17:54:04 +00001578
1579 RVal V = Ex ? GetRVal(St, Ex) : UndefinedVal();
1580
1581 if (Ex && V.isUnknown()) {
1582
1583 // EXPERIMENTAL: "Conjured" symbols.
1584
1585 unsigned Count = Builder->getCurrentBlockCount();
1586 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
1587
Ted Kremenek0e470a52008-05-09 23:45:33 +00001588 V = LVal::IsLValType(Ex->getType())
Ted Kremenekf47bb782008-04-30 17:54:04 +00001589 ? cast<RVal>(lval::SymbolVal(Sym))
1590 : cast<RVal>(nonlval::SymbolVal(Sym));
1591 }
1592
1593 St = SetRVal(St, lval::DeclVal(VD), V);
1594 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001595 }
1596
1597 // Create a new node. We don't really need to create a new NodeSet
1598 // here, but it simplifies things and doesn't cost much.
1599 NodeSet Tmp2;
1600 MakeNode(Tmp2, DS, *I, St);
1601 if (Tmp2.empty()) Tmp2.Add(*I);
1602
1603 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2)
1604 VisitDeclStmtAux(DS, D->getNextDeclarator(), *I2, Dst);
1605 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001606}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001607
Ted Kremenekf233d482008-02-05 00:26:40 +00001608
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001609/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001610void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
1611 NodeTy* Pred,
1612 NodeSet& Dst) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001613 QualType T = Ex->getArgumentType();
Ted Kremenek87e80342008-03-15 03:13:20 +00001614 uint64_t amt;
1615
1616 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001617
Ted Kremenek87e80342008-03-15 03:13:20 +00001618 // FIXME: Add support for VLAs.
1619 if (!T.getTypePtr()->isConstantSizeType())
1620 return;
1621
Ted Kremenekf342d182008-04-30 21:31:12 +00001622 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1623 // the compiler has laid out its representation. Just report Unknown
1624 // for these.
1625 if (T->isObjCInterfaceType())
1626 return;
1627
Ted Kremenek87e80342008-03-15 03:13:20 +00001628 amt = 1; // Handle sizeof(void)
1629
1630 if (T != getContext().VoidTy)
1631 amt = getContext().getTypeSize(T) / 8;
1632
1633 }
1634 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001635 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001636
Ted Kremenek0e561a32008-03-21 21:30:14 +00001637 MakeNode(Dst, Ex, Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001638 SetRVal(GetState(Pred), Ex,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001639 NonLVal::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001640}
1641
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001642
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001643void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001644 NodeSet& Dst, bool asLVal) {
1645
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001646 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001647
1648 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001649 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001650
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001651 case UnaryOperator::Deref: {
1652
1653 Expr* Ex = U->getSubExpr()->IgnoreParens();
1654 NodeSet Tmp;
1655 Visit(Ex, Pred, Tmp);
1656
1657 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001658
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001659 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001660 RVal location = GetRVal(St, Ex);
1661
1662 if (asLVal)
1663 MakeNode(Dst, U, *I, SetRVal(St, U, location));
1664 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001665 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001666 }
1667
1668 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001669 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001670
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001671 case UnaryOperator::Real: {
1672
1673 Expr* Ex = U->getSubExpr()->IgnoreParens();
1674 NodeSet Tmp;
1675 Visit(Ex, Pred, Tmp);
1676
1677 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1678
1679 // FIXME: We don't have complex RValues yet.
1680 if (Ex->getType()->isAnyComplexType()) {
1681 // Just report "Unknown."
1682 Dst.Add(*I);
1683 continue;
1684 }
1685
1686 // For all other types, UnaryOperator::Real is an identity operation.
1687 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001688 const GRState* St = GetState(*I);
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001689 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1690 }
1691
1692 return;
1693 }
1694
1695 case UnaryOperator::Imag: {
1696
1697 Expr* Ex = U->getSubExpr()->IgnoreParens();
1698 NodeSet Tmp;
1699 Visit(Ex, Pred, Tmp);
1700
1701 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1702 // FIXME: We don't have complex RValues yet.
1703 if (Ex->getType()->isAnyComplexType()) {
1704 // Just report "Unknown."
1705 Dst.Add(*I);
1706 continue;
1707 }
1708
1709 // For all other types, UnaryOperator::Float returns 0.
1710 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001711 const GRState* St = GetState(*I);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001712 RVal X = NonLVal::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001713 MakeNode(Dst, U, *I, SetRVal(St, U, X));
1714 }
1715
1716 return;
1717 }
1718
1719 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001720 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001721 Dst.Add(Pred);
1722 return;
1723
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001724 case UnaryOperator::Plus: assert (!asLVal); // FALL-THROUGH.
1725 case UnaryOperator::Extension: {
1726
1727 // Unary "+" is a no-op, similar to a parentheses. We still have places
1728 // where it may be a block-level expression, so we need to
1729 // generate an extra node that just propagates the value of the
1730 // subexpression.
1731
1732 Expr* Ex = U->getSubExpr()->IgnoreParens();
1733 NodeSet Tmp;
1734 Visit(Ex, Pred, Tmp);
1735
1736 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001737 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001738 MakeNode(Dst, U, *I, SetRVal(St, U, GetRVal(St, Ex)));
1739 }
1740
1741 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001742 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001743
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001744 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001745
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001746 assert (!asLVal);
1747 Expr* Ex = U->getSubExpr()->IgnoreParens();
1748 NodeSet Tmp;
1749 VisitLVal(Ex, Pred, Tmp);
1750
1751 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001752 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001753 RVal V = GetRVal(St, Ex);
1754 St = SetRVal(St, U, V);
1755 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001756 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001757
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001758 return;
1759 }
1760
1761 case UnaryOperator::LNot:
1762 case UnaryOperator::Minus:
1763 case UnaryOperator::Not: {
1764
1765 assert (!asLVal);
1766 Expr* Ex = U->getSubExpr()->IgnoreParens();
1767 NodeSet Tmp;
1768 Visit(Ex, Pred, Tmp);
1769
1770 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001771 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001772 RVal V = GetRVal(St, Ex);
1773
1774 if (V.isUnknownOrUndef()) {
1775 MakeNode(Dst, U, *I, SetRVal(St, U, V));
1776 continue;
1777 }
1778
1779 switch (U->getOpcode()) {
1780 default:
1781 assert(false && "Invalid Opcode.");
1782 break;
1783
1784 case UnaryOperator::Not:
1785 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(V)));
1786 break;
1787
1788 case UnaryOperator::Minus:
1789 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(V)));
1790 break;
1791
1792 case UnaryOperator::LNot:
1793
1794 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1795 //
1796 // Note: technically we do "E == 0", but this is the same in the
1797 // transfer functions as "0 == E".
1798
1799 if (isa<LVal>(V)) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001800 lval::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001801 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(V), X);
1802 St = SetRVal(St, U, Result);
1803 }
1804 else {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001805 nonlval::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001806#if 0
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001807 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(V), X);
1808 St = SetRVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00001809#else
1810 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLVal>(V), X, *I);
1811 continue;
1812#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001813 }
1814
1815 break;
1816 }
1817
1818 MakeNode(Dst, U, *I, St);
1819 }
1820
1821 return;
1822 }
1823
1824 case UnaryOperator::SizeOf: {
1825
1826 QualType T = U->getSubExpr()->getType();
1827
1828 // FIXME: Add support for VLAs.
1829
1830 if (!T.getTypePtr()->isConstantSizeType())
1831 return;
1832
1833 uint64_t size = getContext().getTypeSize(T) / 8;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001834 const GRState* St = GetState(Pred);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00001835 St = SetRVal(St, U, NonLVal::MakeVal(getBasicVals(), size, U->getType()));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001836
1837 MakeNode(Dst, U, Pred, St);
1838 return;
1839 }
1840 }
1841
1842 // Handle ++ and -- (both pre- and post-increment).
1843
1844 assert (U->isIncrementDecrementOp());
1845 NodeSet Tmp;
1846 Expr* Ex = U->getSubExpr()->IgnoreParens();
1847 VisitLVal(Ex, Pred, Tmp);
1848
1849 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1850
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001851 const GRState* St = GetState(*I);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001852 RVal V1 = GetRVal(St, Ex);
1853
1854 // Perform a load.
1855 NodeSet Tmp2;
1856 EvalLoad(Tmp2, Ex, *I, St, V1);
1857
1858 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
1859
1860 St = GetState(*I2);
1861 RVal V2 = GetRVal(St, Ex);
1862
1863 // Propagate unknown and undefined values.
1864 if (V2.isUnknownOrUndef()) {
1865 MakeNode(Dst, U, *I2, SetRVal(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001866 continue;
1867 }
1868
Ted Kremenek443003b2008-02-21 19:29:23 +00001869 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00001870
1871 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
1872 : BinaryOperator::Sub;
1873
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001874 RVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
1875 St = SetRVal(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001876
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001877 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00001878 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001879 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001880 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001881}
1882
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001883void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
1884 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
1885}
1886
1887void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
1888 AsmStmt::outputs_iterator I,
1889 AsmStmt::outputs_iterator E,
1890 NodeTy* Pred, NodeSet& Dst) {
1891 if (I == E) {
1892 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
1893 return;
1894 }
1895
1896 NodeSet Tmp;
1897 VisitLVal(*I, Pred, Tmp);
1898
1899 ++I;
1900
1901 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1902 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
1903}
1904
1905void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
1906 AsmStmt::inputs_iterator I,
1907 AsmStmt::inputs_iterator E,
1908 NodeTy* Pred, NodeSet& Dst) {
1909 if (I == E) {
1910
1911 // We have processed both the inputs and the outputs. All of the outputs
1912 // should evaluate to LVals. Nuke all of their values.
1913
1914 // FIXME: Some day in the future it would be nice to allow a "plug-in"
1915 // which interprets the inline asm and stores proper results in the
1916 // outputs.
1917
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001918 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001919
1920 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
1921 OE = A->end_outputs(); OI != OE; ++OI) {
1922
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001923 RVal X = GetRVal(St, *OI);
1924 assert (!isa<NonLVal>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001925
1926 if (isa<LVal>(X))
1927 St = SetRVal(St, cast<LVal>(X), UnknownVal());
1928 }
1929
Ted Kremenek0e561a32008-03-21 21:30:14 +00001930 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00001931 return;
1932 }
1933
1934 NodeSet Tmp;
1935 Visit(*I, Pred, Tmp);
1936
1937 ++I;
1938
1939 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1940 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
1941}
1942
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001943void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
1944 assert (Builder && "GRStmtNodeBuilder must be defined.");
1945
1946 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00001947
Ted Kremenek186350f2008-04-23 20:12:28 +00001948 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1949 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001950
Ted Kremenek729a9a22008-07-17 23:15:45 +00001951 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001952
Ted Kremenekb0533962008-04-18 20:35:30 +00001953 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001954
Ted Kremenekb0533962008-04-18 20:35:30 +00001955 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001956 MakeNode(Dst, S, Pred, GetState(Pred));
1957}
1958
Ted Kremenek02737ed2008-03-31 15:02:58 +00001959void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
1960
1961 Expr* R = S->getRetValue();
1962
1963 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001964 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001965 return;
1966 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001967
1968 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00001969 QualType T = R->getType();
1970
Chris Lattner423a3c92008-04-02 17:45:06 +00001971 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00001972
1973 // Check if any of the return values return the address of a stack variable.
1974
1975 NodeSet Tmp;
1976 Visit(R, Pred, Tmp);
1977
1978 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1979 RVal X = GetRVal((*I)->getState(), R);
1980
1981 if (isa<lval::DeclVal>(X)) {
1982
1983 if (cast<lval::DeclVal>(X).getDecl()->hasLocalStorage()) {
1984
1985 // Create a special node representing the v
1986
1987 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
1988
1989 if (RetStackNode) {
1990 RetStackNode->markAsSink();
1991 RetsStackAddr.insert(RetStackNode);
1992 }
1993
1994 continue;
1995 }
1996 }
1997
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00001998 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00001999 }
2000 }
2001 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002002 Visit(R, Pred, DstRet);
2003
2004 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2005 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002006}
Ted Kremenek55deb972008-03-25 00:34:37 +00002007
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002008//===----------------------------------------------------------------------===//
2009// Transfer functions: Binary operators.
2010//===----------------------------------------------------------------------===//
2011
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002012bool GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002013 NodeTy* Pred, RVal Denom) {
2014
2015 // Divide by undefined? (potentially zero)
2016
2017 if (Denom.isUndef()) {
2018 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2019
2020 if (DivUndef) {
2021 DivUndef->markAsSink();
2022 ExplicitBadDivides.insert(DivUndef);
2023 }
2024
2025 return true;
2026 }
2027
2028 // Check for divide/remainder-by-zero.
2029 // First, "assume" that the denominator is 0 or undefined.
2030
2031 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002032 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002033
2034 // Second, "assume" that the denominator cannot be 0.
2035
2036 bool isFeasibleNotZero = false;
2037 St = Assume(St, Denom, true, isFeasibleNotZero);
2038
2039 // Create the node for the divide-by-zero (if it occurred).
2040
2041 if (isFeasibleZero)
2042 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2043 DivZeroNode->markAsSink();
2044
2045 if (isFeasibleNotZero)
2046 ImplicitBadDivides.insert(DivZeroNode);
2047 else
2048 ExplicitBadDivides.insert(DivZeroNode);
2049
2050 }
2051
2052 return !isFeasibleNotZero;
2053}
2054
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002055void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002056 GRExprEngine::NodeTy* Pred,
2057 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002058
2059 NodeSet Tmp1;
2060 Expr* LHS = B->getLHS()->IgnoreParens();
2061 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002062
2063 if (B->isAssignmentOp())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002064 VisitLVal(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002065 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002066 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002067
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002068 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002069
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002070 RVal LeftV = GetRVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002071
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002072 // Process the RHS.
2073
2074 NodeSet Tmp2;
2075 Visit(RHS, *I1, Tmp2);
2076
2077 // With both the LHS and RHS evaluated, process the operation itself.
2078
2079 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002080
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002081 const GRState* St = GetState(*I2);
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002082 RVal RightV = GetRVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002083 BinaryOperator::Opcode Op = B->getOpcode();
2084
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002085 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002086
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002087 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002088
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002089 // EXPERIMENTAL: "Conjured" symbols.
2090
2091 if (RightV.isUnknown()) {
2092 unsigned Count = Builder->getCurrentBlockCount();
2093 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2094
Ted Kremenek0e470a52008-05-09 23:45:33 +00002095 RightV = LVal::IsLValType(B->getRHS()->getType())
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002096 ? cast<RVal>(lval::SymbolVal(Sym))
2097 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002098 }
2099
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002100 // Simulate the effects of a "store": bind the value of the RHS
2101 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002102
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002103 EvalStore(Dst, B, *I2, SetRVal(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002104 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002105 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002106
2107 case BinaryOperator::Div:
2108 case BinaryOperator::Rem:
2109
2110 // Special checking for integer denominators.
2111
2112 if (RHS->getType()->isIntegerType()
2113 && CheckDivideZero(B, St, *I2, RightV))
2114 continue;
2115
2116 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002117
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002118 default: {
2119
2120 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002121 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002123 // Process non-assignements except commas or short-circuited
2124 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002125
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002126 RVal Result = EvalBinOp(Op, LeftV, RightV);
2127
2128 if (Result.isUnknown()) {
2129 Dst.Add(*I2);
Ted Kremenek89063af2008-02-21 19:15:37 +00002130 continue;
2131 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002132
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002133 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002134
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002135 // The operands were *not* undefined, but the result is undefined.
2136 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002137
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002138 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002139 UndefNode->markAsSink();
2140 UndefResults.insert(UndefNode);
2141 }
2142
2143 continue;
2144 }
2145
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002146 // Otherwise, create a new node.
2147
2148 MakeNode(Dst, B, *I2, SetRVal(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002149 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002150 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002151 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002152
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002153 assert (B->isCompoundAssignmentOp());
2154
2155 if (Op >= BinaryOperator::AndAssign)
2156 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
2157 else
2158 ((int&) Op) -= BinaryOperator::MulAssign;
2159
2160 // Perform a load (the LHS). This performs the checks for
2161 // null dereferences, and so on.
2162 NodeSet Tmp3;
2163 RVal location = GetRVal(St, LHS);
2164 EvalLoad(Tmp3, LHS, *I2, St, location);
2165
2166 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2167
2168 St = GetState(*I3);
2169 RVal V = GetRVal(St, LHS);
2170
2171 // Propagate undefined values (left-side).
2172 if (V.isUndef()) {
2173 EvalStore(Dst, B, *I3, SetRVal(St, B, V), location, V);
2174 continue;
2175 }
2176
2177 // Propagate unknown values (left and right-side).
2178 if (RightV.isUnknown() || V.isUnknown()) {
2179 EvalStore(Dst, B, *I3, SetRVal(St, B, UnknownVal()), location,
2180 UnknownVal());
2181 continue;
2182 }
2183
2184 // At this point:
2185 //
2186 // The LHS is not Undef/Unknown.
2187 // The RHS is not Unknown.
2188
2189 // Get the computation type.
2190 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2191
2192 // Perform promotions.
2193 V = EvalCast(V, CTy);
2194 RightV = EvalCast(RightV, CTy);
2195
2196 // Evaluate operands and promote to result type.
2197
2198 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2199 && RHS->getType()->isIntegerType()) {
2200
2201 if (CheckDivideZero(B, St, *I3, RightV))
2202 continue;
2203 }
2204 else if (RightV.isUndef()) {
2205
2206 // Propagate undefined values (right-side).
2207
2208 EvalStore(Dst, B, *I3, SetRVal(St, B, RightV), location, RightV);
2209 continue;
2210 }
2211
2212 // Compute the result of the operation.
2213
2214 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
2215
2216 if (Result.isUndef()) {
2217
2218 // The operands were not undefined, but the result is undefined.
2219
2220 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2221 UndefNode->markAsSink();
2222 UndefResults.insert(UndefNode);
2223 }
2224
2225 continue;
2226 }
2227
2228 EvalStore(Dst, B, *I3, SetRVal(St, B, Result), location, Result);
2229 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002230 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002231 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002232}
Ted Kremenekee985462008-01-16 18:18:48 +00002233
2234//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002235// Transfer-function Helpers.
2236//===----------------------------------------------------------------------===//
2237
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002238void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002239 BinaryOperator::Opcode Op,
2240 NonLVal L, NonLVal R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002241 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002242
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002243 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002244 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2245
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002246 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002247 MakeNode(Dst, Ex, Pred, *I);
2248}
2249
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002250void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002251 Expr* Ex, BinaryOperator::Opcode Op,
2252 NonLVal L, NonLVal R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002253
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002254 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002255 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002256}
2257
2258//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002259// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002260//===----------------------------------------------------------------------===//
2261
Ted Kremenekaa66a322008-01-16 21:46:15 +00002262#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002263static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002264static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002265
Ted Kremenekaa66a322008-01-16 21:46:15 +00002266namespace llvm {
2267template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002268struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002269 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002270
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002271 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2272
2273 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002274 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002275 GraphPrintCheckerState->isUndefDeref(N) ||
2276 GraphPrintCheckerState->isUndefStore(N) ||
2277 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002278 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2279 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002280 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002281 GraphPrintCheckerState->isBadCall(N) ||
2282 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002283 return "color=\"red\",style=\"filled\"";
2284
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002285 if (GraphPrintCheckerState->isNoReturnCall(N))
2286 return "color=\"blue\",style=\"filled\"";
2287
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002288 return "";
2289 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002290
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002291 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002292 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002293
2294 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002295 ProgramPoint Loc = N->getLocation();
2296
2297 switch (Loc.getKind()) {
2298 case ProgramPoint::BlockEntranceKind:
2299 Out << "Block Entrance: B"
2300 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2301 break;
2302
2303 case ProgramPoint::BlockExitKind:
2304 assert (false);
2305 break;
2306
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002307 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002308 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002309 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002310 const PostStmt& L = cast<PostStmt>(Loc);
2311 Stmt* S = L.getStmt();
2312 SourceLocation SLoc = S->getLocStart();
2313
2314 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2315 S->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002316
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002317 if (SLoc.isFileID()) {
2318 Out << "\\lline="
2319 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2320 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2321 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002322
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002323 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002324 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002325 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002326 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002327 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002328 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002329 else if (GraphPrintCheckerState->isUndefStore(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002330 Out << "\\|Store to Undefined LVal.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002331 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2332 Out << "\\|Explicit divide-by zero or undefined value.";
2333 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2334 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002335 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002336 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002337 else if (GraphPrintCheckerState->isNoReturnCall(N))
2338 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002339 else if (GraphPrintCheckerState->isBadCall(N))
2340 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002341 else if (GraphPrintCheckerState->isUndefArg(N))
2342 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002343
Ted Kremenekaa66a322008-01-16 21:46:15 +00002344 break;
2345 }
2346
2347 default: {
2348 const BlockEdge& E = cast<BlockEdge>(Loc);
2349 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2350 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002351
2352 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002353
2354 SourceLocation SLoc = T->getLocStart();
2355
Ted Kremenekb38911f2008-01-30 23:03:39 +00002356 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002357
Ted Kremenekb38911f2008-01-30 23:03:39 +00002358 E.getSrc()->printTerminator(Out);
2359
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002360 if (SLoc.isFileID()) {
2361 Out << "\\lline="
2362 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2363 << GraphPrintSourceManager->getColumnNumber(SLoc);
2364 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002365
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002366 if (isa<SwitchStmt>(T)) {
2367 Stmt* Label = E.getDst()->getLabel();
2368
2369 if (Label) {
2370 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2371 Out << "\\lcase ";
2372 C->getLHS()->printPretty(Out);
2373
2374 if (Stmt* RHS = C->getRHS()) {
2375 Out << " .. ";
2376 RHS->printPretty(Out);
2377 }
2378
2379 Out << ":";
2380 }
2381 else {
2382 assert (isa<DefaultStmt>(Label));
2383 Out << "\\ldefault:";
2384 }
2385 }
2386 else
2387 Out << "\\l(implicit) default:";
2388 }
2389 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002390 // FIXME
2391 }
2392 else {
2393 Out << "\\lCondition: ";
2394 if (*E.getSrc()->succ_begin() == E.getDst())
2395 Out << "true";
2396 else
2397 Out << "false";
2398 }
2399
2400 Out << "\\l";
2401 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002402
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002403 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2404 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002405 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002406 }
2407 }
2408
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002409 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002410
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002411 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2412 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002413
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002414 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002415 return Out.str();
2416 }
2417};
2418} // end llvm namespace
2419#endif
2420
Ted Kremenekffe0f432008-03-07 22:58:01 +00002421#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002422
2423template <typename ITERATOR>
2424GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2425
2426template <>
2427GRExprEngine::NodeTy*
2428GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2429 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2430 return I->first;
2431}
2432
Ted Kremenekffe0f432008-03-07 22:58:01 +00002433template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002434static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002435 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002436
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002437 llvm::SmallPtrSet<void*,10> CachedSources;
2438
2439 for ( ; I != E; ++I ) {
2440 GRExprEngine::NodeTy* N = GetGraphNode(I);
2441 void* p = N->getLocation().getRawData();
2442
2443 if (CachedSources.count(p))
2444 continue;
2445
2446 CachedSources.insert(p);
2447
2448 Sources.push_back(N);
2449 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002450}
2451#endif
2452
2453void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002454#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002455 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002456 std::vector<NodeTy*> Src;
2457
2458 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002459 AddSources(Src, null_derefs_begin(), null_derefs_end());
2460 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2461 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2462 AddSources(Src, undef_results_begin(), undef_results_end());
2463 AddSources(Src, bad_calls_begin(), bad_calls_end());
2464 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002465 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002466
Ted Kremenekcb612922008-04-18 19:23:43 +00002467 // The new way.
2468 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2469 (*I)->GetErrorNodes(Src);
2470
2471
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002472 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002473 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002474 else {
2475 GraphPrintCheckerState = this;
2476 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002477
Ted Kremenekffe0f432008-03-07 22:58:01 +00002478 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002479
2480 GraphPrintCheckerState = NULL;
2481 GraphPrintSourceManager = NULL;
2482 }
2483#endif
2484}
2485
2486void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2487#ifndef NDEBUG
2488 GraphPrintCheckerState = this;
2489 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002490
Ted Kremenek493d7a22008-03-11 18:25:33 +00002491 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2492
2493 if (!TrimmedG)
2494 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2495 else {
2496 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2497 delete TrimmedG;
2498 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002499
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002500 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002501 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002502#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002503}