blob: a52437c343ebab2ec557275f662080a6cce3999a [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.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 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000072
Ted Kremenek4adc81e2008-08-13 04:27:00 +000073 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000074 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
75 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
76 MapTy::iterator MI = M.find(key);
77
78 if (MI == M.end())
79 return false;
80
81 bool isSink = false;
82
83 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000084 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +000085
86 return isSink;
87 }
88};
89
90} // end anonymous namespace
91
92//===----------------------------------------------------------------------===//
93// Engine construction and deletion.
94//===----------------------------------------------------------------------===//
95
Ted Kremeneke448ab42008-05-01 18:33:28 +000096static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
97 IdentifierInfo* II = &Ctx.Idents.get(name);
98 return Ctx.Selectors.getSelector(0, &II);
99}
100
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000101
Ted Kremenek8b233612008-07-02 20:13:38 +0000102GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000103 LiveVariables& L, BugReporterData& BRD,
104 bool purgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000105 StoreManagerCreator SMC,
106 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000107 : CoreEngine(cfg, CD, Ctx, *this),
108 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000109 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000110 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000111 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000112 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000113 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000114 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
115 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000116 PurgeDead(purgeDead),
117 BR(BRD, *this) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000118
Ted Kremenek1a654b62008-06-20 21:45:25 +0000119GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000120 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000121 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122}
123
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000124//===----------------------------------------------------------------------===//
125// Utility methods.
126//===----------------------------------------------------------------------===//
127
128// SaveAndRestore - A utility class that uses RIIA to save and restore
129// the value of a variable.
130template<typename T>
131struct VISIBILITY_HIDDEN SaveAndRestore {
132 SaveAndRestore(T& x) : X(x), old_value(x) {}
133 ~SaveAndRestore() { X = old_value; }
134 T get() { return old_value; }
135
136 T& X;
137 T old_value;
138};
139
Ted Kremenek186350f2008-04-23 20:12:28 +0000140// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
141// value of a variable is saved, and during the dstor the old value is
142// or'ed with the new value.
143struct VISIBILITY_HIDDEN SaveOr {
144 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
145 ~SaveOr() { X |= old_value; }
146
147 bool& X;
148 bool old_value;
149};
150
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000151void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000152 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000153 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000154 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000155}
156
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000157void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
158 if (!BatchAuditor)
159 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
160
161 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000162}
163
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000164const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000165 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000166}
167
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000168//===----------------------------------------------------------------------===//
169// Top-level transfer function logic (Dispatcher).
170//===----------------------------------------------------------------------===//
171
172void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
173
174 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000175 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000176
177 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000178 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000179 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000180
181 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000182 if (BatchAuditor)
183 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000184
Ted Kremenek241677a2009-01-21 22:26:05 +0000185
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000186 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000187 SymbolReaper SymReaper(Liveness, SymMgr);
188 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
189 CurrentStmt, SymReaper)
190 : EntryNode->getState();
191
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000192 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000193 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000194
Ted Kremenek241677a2009-01-21 22:26:05 +0000195 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000196 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000197 else {
198 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000199 SaveOr OldHasGen(Builder->HasGeneratedNode);
200
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000201 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
202 Builder->PurgingDeadSymbols = true;
203
Ted Kremenek729a9a22008-07-17 23:15:45 +0000204 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000205 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000206
207 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
208 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000209 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000210
211 bool HasAutoGenerated = false;
212
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000213 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214
215 NodeSet Dst;
216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000218 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Visit(S, *I, Dst);
222
223 // Do we need to auto-generate a node? We only need to do this to generate
224 // a node with a "cleaned" state; GRCoreEngine will actually handle
225 // auto-transitions for other cases.
226 if (Dst.size() == 1 && *Dst.begin() == EntryNode
227 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
228 HasAutoGenerated = true;
229 builder.generateNode(S, GetState(EntryNode), *I);
230 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000231 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000232
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000233 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000234 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000236
237 // FIXME: Consolidate.
238 StateMgr.CurrentStmt = 0;
239 CurrentStmt = 0;
240
Ted Kremenek846d4e92008-04-24 23:35:58 +0000241 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000242}
243
244void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
245
246 // FIXME: add metadata to the CFG so that we can disable
247 // this check when we KNOW that there is no block-level subexpression.
248 // The motivation is that this check requires a hashtable lookup.
249
250 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
251 Dst.Add(Pred);
252 return;
253 }
254
255 switch (S->getStmtClass()) {
256
257 default:
258 // Cases we intentionally have "default" handle:
259 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
260
261 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
262 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000263
264 case Stmt::ArraySubscriptExprClass:
265 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
266 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267
268 case Stmt::AsmStmtClass:
269 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
270 break;
271
272 case Stmt::BinaryOperatorClass: {
273 BinaryOperator* B = cast<BinaryOperator>(S);
274
275 if (B->isLogicalOp()) {
276 VisitLogicalExpr(B, Pred, Dst);
277 break;
278 }
279 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000280 const GRState* state = GetState(Pred);
281 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000282 break;
283 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000284
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000285 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
286 break;
287 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000288
Douglas Gregorb4609802008-11-14 16:09:21 +0000289 case Stmt::CallExprClass:
290 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000291 CallExpr* C = cast<CallExpr>(S);
292 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000293 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000294 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000295
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296 // FIXME: ChooseExpr is really a constant. We need to fix
297 // the CFG do not model them as explicit control-flow.
298
299 case Stmt::ChooseExprClass: { // __builtin_choose_expr
300 ChooseExpr* C = cast<ChooseExpr>(S);
301 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
302 break;
303 }
304
305 case Stmt::CompoundAssignOperatorClass:
306 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
307 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000308
309 case Stmt::CompoundLiteralExprClass:
310 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
311 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000312
313 case Stmt::ConditionalOperatorClass: { // '?' operator
314 ConditionalOperator* C = cast<ConditionalOperator>(S);
315 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
316 break;
317 }
318
319 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000320 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000321 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000322 break;
323
324 case Stmt::DeclStmtClass:
325 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
326 break;
327
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000328 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000329 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000330 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000331 VisitCast(C, C->getSubExpr(), Pred, Dst);
332 break;
333 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000334
335 case Stmt::InitListExprClass:
336 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
337 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000338
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000339 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000340 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
341 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000342
343 case Stmt::ObjCIvarRefExprClass:
344 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
345 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000346
347 case Stmt::ObjCForCollectionStmtClass:
348 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
349 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000350
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000351 case Stmt::ObjCMessageExprClass: {
352 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
353 break;
354 }
355
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000356 case Stmt::ObjCAtThrowStmtClass: {
357 // FIXME: This is not complete. We basically treat @throw as
358 // an abort.
359 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
360 Builder->BuildSinks = true;
361 MakeNode(Dst, S, Pred, GetState(Pred));
362 break;
363 }
364
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000365 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000366 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367 break;
368
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000369 case Stmt::ReturnStmtClass:
370 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
371 break;
372
Sebastian Redl05189992008-11-11 17:56:53 +0000373 case Stmt::SizeOfAlignOfExprClass:
374 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000375 break;
376
377 case Stmt::StmtExprClass: {
378 StmtExpr* SE = cast<StmtExpr>(S);
379
Ted Kremeneka8538d92009-02-13 01:45:31 +0000380 const GRState* state = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000381
382 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
383 // probably just remove these from the CFG.
384 assert (!SE->getSubStmt()->body_empty());
385
386 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000387 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000388 else
389 Dst.Add(Pred);
390
391 break;
392 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000393
394 case Stmt::StringLiteralClass:
395 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
396 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000397
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000398 case Stmt::UnaryOperatorClass:
399 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000400 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000401 }
402}
403
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000404void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000405
406 Ex = Ex->IgnoreParens();
407
408 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
409 Dst.Add(Pred);
410 return;
411 }
412
413 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000414
415 case Stmt::ArraySubscriptExprClass:
416 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
417 return;
418
419 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000420 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000421 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
422 return;
423
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000424 case Stmt::ObjCIvarRefExprClass:
425 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
426 return;
427
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000428 case Stmt::UnaryOperatorClass:
429 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
430 return;
431
432 case Stmt::MemberExprClass:
433 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
434 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000435
Ted Kremenek4f090272008-10-27 21:54:31 +0000436 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000437 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000438 return;
439
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000440 case Stmt::ObjCPropertyRefExprClass:
441 // FIXME: Property assignments are lvalues, but not really "locations".
442 // e.g.: self.x = something;
443 // Here the "self.x" really can translate to a method call (setter) when
444 // the assignment is made. Moreover, the entire assignment expression
445 // evaluate to whatever "something" is, not calling the "getter" for
446 // the property (which would make sense since it can have side effects).
447 // We'll probably treat this as a location, but not one that we can
448 // take the address of. Perhaps we need a new SVal class for cases
449 // like thsis?
450 // Note that we have a similar problem for bitfields, since they don't
451 // have "locations" in the sense that we can take their address.
452 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000453 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000454
455 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000456 const GRState* state = GetState(Pred);
457 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
458 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000459 return;
460 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000461
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000462 default:
463 // Arbitrary subexpressions can return aggregate temporaries that
464 // can be used in a lvalue context. We need to enhance our support
465 // of such temporaries in both the environment and the store, so right
466 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000467 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000468 "Other kinds of expressions with non-aggregate/union types do"
469 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000470
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000471 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000472 }
473}
474
475//===----------------------------------------------------------------------===//
476// Block entrance. (Update counters).
477//===----------------------------------------------------------------------===//
478
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000479bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000480 GRBlockCounter BC) {
481
482 return BC.getNumVisited(B->getBlockID()) < 3;
483}
484
485//===----------------------------------------------------------------------===//
486// Branch processing.
487//===----------------------------------------------------------------------===//
488
Ted Kremeneka8538d92009-02-13 01:45:31 +0000489const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000490 Stmt* Terminator,
491 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000492
493 switch (Terminator->getStmtClass()) {
494 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000495 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000496
497 case Stmt::BinaryOperatorClass: { // '&&' and '||'
498
499 BinaryOperator* B = cast<BinaryOperator>(Terminator);
500 BinaryOperator::Opcode Op = B->getOpcode();
501
502 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
503
504 // For &&, if we take the true branch, then the value of the whole
505 // expression is that of the RHS expression.
506 //
507 // For ||, if we take the false branch, then the value of the whole
508 // expression is that of the RHS expression.
509
510 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
511 (Op == BinaryOperator::LOr && !branchTaken)
512 ? B->getRHS() : B->getLHS();
513
Ted Kremeneka8538d92009-02-13 01:45:31 +0000514 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000515 }
516
517 case Stmt::ConditionalOperatorClass: { // ?:
518
519 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
520
521 // For ?, if branchTaken == true then the value is either the LHS or
522 // the condition itself. (GNU extension).
523
524 Expr* Ex;
525
526 if (branchTaken)
527 Ex = C->getLHS() ? C->getLHS() : C->getCond();
528 else
529 Ex = C->getRHS();
530
Ted Kremeneka8538d92009-02-13 01:45:31 +0000531 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000532 }
533
534 case Stmt::ChooseExprClass: { // ?:
535
536 ChooseExpr* C = cast<ChooseExpr>(Terminator);
537
538 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000539 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000540 }
541 }
542}
543
Ted Kremenekaf337412008-11-12 19:24:17 +0000544void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000545 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000546
Ted Kremeneke7d22112008-02-11 19:21:59 +0000547 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000548 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000549 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000550
Ted Kremenekb2331832008-02-15 22:29:00 +0000551 // Check for NULL conditions; e.g. "for(;;)"
552 if (!Condition) {
553 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000554 return;
555 }
556
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000557 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000558
559 switch (V.getBaseKind()) {
560 default:
561 break;
562
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000563 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000564 builder.generateNode(MarkBranch(PrevState, Term, true), true);
565 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000566 return;
567
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000568 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000569 NodeTy* N = builder.generateNode(PrevState, true);
570
571 if (N) {
572 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000573 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000574 }
575
576 builder.markInfeasible(false);
577 return;
578 }
579 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000580
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000581 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000582
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000583 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000584 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000585
586 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000587 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000588 else
589 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000590
591 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000592
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000593 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000594 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000595
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000597 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000598 else
599 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000600}
601
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000602/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000603/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000604void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000605
Ted Kremeneka8538d92009-02-13 01:45:31 +0000606 const GRState* state = builder.getState();
607 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000608
609 // Three possibilities:
610 //
611 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000612 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000613 // (3) We have no clue about the label. Dispatch to all targets.
614 //
615
616 typedef IndirectGotoNodeBuilder::iterator iterator;
617
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000618 if (isa<loc::GotoLabel>(V)) {
619 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000620
621 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000622 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000623 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000624 return;
625 }
626 }
627
628 assert (false && "No block with label.");
629 return;
630 }
631
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000632 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000633 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000634 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000635 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000636 return;
637 }
638
639 // This is really a catch-all. We don't support symbolics yet.
640
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000641 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000642
643 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000644 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000645}
Ted Kremenekf233d482008-02-05 00:26:40 +0000646
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000647
648void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
649 NodeTy* Pred, NodeSet& Dst) {
650
651 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
652
Ted Kremeneka8538d92009-02-13 01:45:31 +0000653 const GRState* state = GetState(Pred);
654 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000655
656 assert (X.isUndef());
657
658 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
659
660 assert (SE);
661
Ted Kremeneka8538d92009-02-13 01:45:31 +0000662 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000663
664 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000665 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000666}
667
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000668/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
669/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000670void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
671 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000672 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000673 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000674 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000675
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000676 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000677 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000678 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679 return;
680 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000681
Ted Kremeneka8538d92009-02-13 01:45:31 +0000682 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000683 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000684
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000685 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000686 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000687
688 // Evaluate the LHS of the case value.
689 Expr::EvalResult V1;
690 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691
Ted Kremenek72afb372009-01-17 01:54:16 +0000692 // Sanity checks. These go away in Release builds.
693 assert(b && V1.Val.isInt() && !V1.HasSideEffects
694 && "Case condition must evaluate to an integer constant.");
695 b = b; // silence unused variable warning
696 assert(V1.Val.getInt().getBitWidth() ==
697 getContext().getTypeSize(CondE->getType()));
698
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000699 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000700 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000701
702 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000703 b = E->Evaluate(V2, getContext());
704 assert(b && V2.Val.isInt() && !V2.HasSideEffects
705 && "Case condition must evaluate to an integer constant.");
706 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000707 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000708 else
709 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000710
711 // FIXME: Eventually we should replace the logic below with a range
712 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000713 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000714
Ted Kremenek14a11402008-03-17 22:17:56 +0000715 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000716 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000717 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000718
Ted Kremenek72afb372009-01-17 01:54:16 +0000719 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000720 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000721 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722
723 if (isFeasible) {
724 builder.generateCaseStmtNode(I, StNew);
725
726 // If CondV evaluates to a constant, then we know that this
727 // is the *only* case that we can take, so stop evaluating the
728 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000729 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000730 return;
731 }
732
733 // Now "assume" that the case doesn't match. Add this state
734 // to the default state (if it is feasible).
735
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000736 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000737 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738
Ted Kremenek5014ab12008-04-23 05:03:18 +0000739 if (isFeasible) {
740 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000741 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000742 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000743
Ted Kremenek14a11402008-03-17 22:17:56 +0000744 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000745 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000746 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747
Ted Kremenek72afb372009-01-17 01:54:16 +0000748 ++V1.Val.getInt();
749 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000750
751 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752 }
753
754 // If we reach here, than we know that the default branch is
755 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000756 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000757}
758
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000759//===----------------------------------------------------------------------===//
760// Transfer functions: logical operations ('&&', '||').
761//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000762
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000763void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000764 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000765
Ted Kremenek05a23782008-02-26 19:05:15 +0000766 assert (B->getOpcode() == BinaryOperator::LAnd ||
767 B->getOpcode() == BinaryOperator::LOr);
768
769 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
770
Ted Kremeneka8538d92009-02-13 01:45:31 +0000771 const GRState* state = GetState(Pred);
772 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000773
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000774 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000775
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000776 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000777
778 assert (Ex);
779
780 if (Ex == B->getRHS()) {
781
Ted Kremeneka8538d92009-02-13 01:45:31 +0000782 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000783
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000784 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000785
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000786 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000787 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000788 return;
789 }
790
Ted Kremenek05a23782008-02-26 19:05:15 +0000791 // We took the RHS. Because the value of the '&&' or '||' expression must
792 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
793 // or 1. Alternatively, we could take a lazy approach, and calculate this
794 // value later when necessary. We don't have the machinery in place for
795 // this right now, and since most logical expressions are used for branches,
796 // the payoff is not likely to be large. Instead, we do eager evaluation.
797
798 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000799 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000800
801 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000802 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000803 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000804
805 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000806 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000807
808 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000809 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000810 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000811 }
812 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000813 // We took the LHS expression. Depending on whether we are '&&' or
814 // '||' we know what the value of the expression is via properties of
815 // the short-circuiting.
816
817 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000818 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000819 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000820}
Ted Kremenek05a23782008-02-26 19:05:15 +0000821
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000822//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000823// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000824//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000825
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000826void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
827 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000828
Ted Kremeneka8538d92009-02-13 01:45:31 +0000829 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000830
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000831 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000832
833 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
834
Ted Kremeneka8538d92009-02-13 01:45:31 +0000835 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000836
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000837 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000838 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000839 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000840 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000841 return;
842
843 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
844 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
845
846 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000847 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000848 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000849 return;
850
851 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000852 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000853 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000854 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000855 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000856 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000857
858 assert (false &&
859 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000860}
861
Ted Kremenek540cbe22008-04-22 04:56:29 +0000862/// VisitArraySubscriptExpr - Transfer function for array accesses
863void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000864 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000865
866 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000867 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000868 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000869 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000870
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000871 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000872 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000873 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000874
875 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000876 const GRState* state = GetState(*I2);
877 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
878 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000879
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000880 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000881 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000882 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000883 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000884 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000885 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000886}
887
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000888/// VisitMemberExpr - Transfer function for member expressions.
889void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000890 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000891
892 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000893 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000894
895 if (M->isArrow())
896 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
897 else
898 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
899
Douglas Gregor86f19402008-12-20 23:49:58 +0000900 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
901 if (!Field) // FIXME: skipping member expressions for non-fields
902 return;
903
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000904 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000905 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000906 // FIXME: Should we insert some assumption logic in here to determine
907 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000908 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000909 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000910
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000911 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000912 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000913 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000914 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000916}
917
Ted Kremeneka8538d92009-02-13 01:45:31 +0000918/// EvalBind - Handle the semantics of binding a value to a specific location.
919/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
920void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
921 const GRState* state, SVal location, SVal Val) {
922
923 unsigned size = Dst.size();
924 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
925 SaveOr OldHasGen(Builder->HasGeneratedNode);
926
927 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, state, location, Val);
928
929 // Handle the case where no nodes where generated. Auto-generate that
930 // contains the updated state if we aren't generating sinks.
931 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
932 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, state,
933 location, Val);
934}
935
936/// EvalStore - Handle the semantics of a store via an assignment.
937/// @param Dst The node set to store generated state nodes
938/// @param Ex The expression representing the location of the store
939/// @param state The current simulation state
940/// @param location The location to store the value
941/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000942void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000943 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000944
945 assert (Builder && "GRStmtNodeBuilder must be defined.");
946
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000947 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000948 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000949
Ted Kremenek8c354752008-12-16 22:02:27 +0000950 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000951 return;
Ted Kremenekb0533962008-04-18 20:35:30 +0000952
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +0000954 state = GetState(Pred);
955
956 // Proceed with the store.
957 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
958 Builder->PointKind = ProgramPoint::PostStoreKind;
959 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960}
961
962void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000963 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964
Ted Kremenek8c354752008-12-16 22:02:27 +0000965 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000966 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000967
Ted Kremenek8c354752008-12-16 22:02:27 +0000968 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969 return;
970
Ted Kremeneka8538d92009-02-13 01:45:31 +0000971 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +0000972
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000973 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000974 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000975
976 // FIXME: Currently symbolic analysis "generates" new symbols
977 // for the contents of values. We need a better approach.
978
Ted Kremenek8c354752008-12-16 22:02:27 +0000979 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +0000980 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000981 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000982 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000983 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000984 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
985 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000986 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000987}
988
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000989void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000990 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000991
992 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000993 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000994
995 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
996 MakeNode(Dst, Ex, *I, (*I)->getState());
997}
998
Ted Kremenek8c354752008-12-16 22:02:27 +0000999GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001000 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001001 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001002
1003 // Check for loads/stores from/to undefined values.
1004 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001005 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001006 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001007 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001008
Ted Kremenek8c354752008-12-16 22:02:27 +00001009 if (N) {
1010 N->markAsSink();
1011 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001012 }
1013
Ted Kremenek8c354752008-12-16 22:02:27 +00001014 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015 }
1016
1017 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1018 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001019 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001020
1021 // During a load, one of two possible situations arise:
1022 // (1) A crash, because the location (pointer) was NULL.
1023 // (2) The location (pointer) is not NULL, and the dereference works.
1024 //
1025 // We add these assumptions.
1026
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001027 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001028
1029 // "Assume" that the pointer is not NULL.
1030
1031 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001032 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001033
1034 // "Assume" that the pointer is NULL.
1035
1036 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001037 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001038 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001039
1040 if (isFeasibleNull) {
1041
Ted Kremenek7360fda2008-09-18 23:09:54 +00001042 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001043 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001044 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1045
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046 // We don't use "MakeNode" here because the node will be a sink
1047 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001048 NodeTy* NullNode =
1049 Builder->generateNode(Ex, StNull, Pred,
1050 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001051
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052 if (NullNode) {
1053
1054 NullNode->markAsSink();
1055
1056 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1057 else ExplicitNullDeref.insert(NullNode);
1058 }
1059 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001060
1061 if (!isFeasibleNotNull)
1062 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001063
1064 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001065 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001066 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1067 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1068 // Get the index of the accessed element.
1069 SVal Idx = ER->getIndex();
1070 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001071 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1072 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001073
1074 bool isFeasibleInBound = false;
1075 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1076 true, isFeasibleInBound);
1077
1078 bool isFeasibleOutBound = false;
1079 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1080 false, isFeasibleOutBound);
1081
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001082 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001083 // Report warning. Make sink node manually.
1084 NodeTy* OOBNode =
1085 Builder->generateNode(Ex, StOutBound, Pred,
1086 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001087
1088 if (OOBNode) {
1089 OOBNode->markAsSink();
1090
1091 if (isFeasibleInBound)
1092 ImplicitOOBMemAccesses.insert(OOBNode);
1093 else
1094 ExplicitOOBMemAccesses.insert(OOBNode);
1095 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001096 }
1097
Ted Kremenek8c354752008-12-16 22:02:27 +00001098 if (!isFeasibleInBound)
1099 return 0;
1100
1101 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001102 }
1103 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001104
Ted Kremenek8c354752008-12-16 22:02:27 +00001105 // Generate a new node indicating the checks succeed.
1106 return Builder->generateNode(Ex, StNotNull, Pred,
1107 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001108}
1109
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001110//===----------------------------------------------------------------------===//
1111// Transfer function: Function calls.
1112//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001113void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001114 CallExpr::arg_iterator AI,
1115 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001116 NodeSet& Dst)
1117{
1118 // Determine the type of function we're calling (if available).
1119 const FunctionTypeProto *Proto = NULL;
1120 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1121 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1122 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1123
1124 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1125}
1126
1127void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1128 CallExpr::arg_iterator AI,
1129 CallExpr::arg_iterator AE,
1130 NodeSet& Dst, const FunctionTypeProto *Proto,
1131 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001132
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001133 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001134 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001135 // If the call argument is being bound to a reference parameter,
1136 // visit it as an lvalue, not an rvalue.
1137 bool VisitAsLvalue = false;
1138 if (Proto && ParamIdx < Proto->getNumArgs())
1139 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1140
1141 NodeSet DstTmp;
1142 if (VisitAsLvalue)
1143 VisitLValue(*AI, Pred, DstTmp);
1144 else
1145 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001146 ++AI;
1147
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001148 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001149 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001150
1151 return;
1152 }
1153
1154 // If we reach here we have processed all of the arguments. Evaluate
1155 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001156
Ted Kremenek994a09b2008-02-25 21:16:03 +00001157 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001158 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001159
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001160 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001161
Ted Kremenekde434242008-02-19 01:44:53 +00001162 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001163 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1164
Ted Kremeneka8538d92009-02-13 01:45:31 +00001165 const GRState* state = GetState(*DI);
1166 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001167
Ted Kremeneka1354a52008-03-03 16:47:31 +00001168 // FIXME: Add support for symbolic function calls (calls involving
1169 // function pointer values that are symbolic).
1170
1171 // Check for undefined control-flow or calls to NULL.
1172
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001173 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001174 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001175
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001176 if (N) {
1177 N->markAsSink();
1178 BadCalls.insert(N);
1179 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001180
Ted Kremenekde434242008-02-19 01:44:53 +00001181 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001182 }
1183
1184 // Check for the "noreturn" attribute.
1185
1186 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1187
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001188 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001189
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001190 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001191
1192 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001193 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001194 else {
1195 // HACK: Some functions are not marked noreturn, and don't return.
1196 // Here are a few hardwired ones. If this takes too long, we can
1197 // potentially cache these results.
1198 const char* s = FD->getIdentifier()->getName();
1199 unsigned n = strlen(s);
1200
1201 switch (n) {
1202 default:
1203 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001204
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001205 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001206 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1207 break;
1208
1209 case 5:
1210 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001211 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001212 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001213 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001214 // FIXME: use Assume to inspect the possible symbolic value of
1215 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001216 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001217 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001218 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001219 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001220 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001221 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001222
1223 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001224 if (!memcmp(s, "Assert", 6)) {
1225 Builder->BuildSinks = true;
1226 break;
1227 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001228
1229 // FIXME: This is just a wrapper around throwing an exception.
1230 // Eventually inter-procedural analysis should handle this easily.
1231 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1232
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001233 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001234
1235 case 7:
1236 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1237 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001238
Ted Kremenekf47bb782008-04-30 17:54:04 +00001239 case 8:
1240 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1241 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001242
1243 case 12:
1244 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1245 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001246
Ted Kremenekf9683082008-09-19 02:30:47 +00001247 case 13:
1248 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1249 break;
1250
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001251 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001252 if (!memcmp(s, "dtrace_assfail", 14) ||
1253 !memcmp(s, "yy_fatal_error", 14))
1254 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001255 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001256
1257 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001258 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1259 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001260 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001261
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001262 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001263 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001264
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001265 }
1266 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001267
1268 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001269
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001270 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001271
Douglas Gregor3e41d602009-02-13 23:20:09 +00001272 if (unsigned id = cast<loc::FuncVal>(L).getDecl()->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001273 switch (id) {
1274 case Builtin::BI__builtin_expect: {
1275 // For __builtin_expect, just return the value of the subexpression.
1276 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001277 SVal X = GetSVal(state, *(CE->arg_begin()));
1278 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001279 continue;
1280 }
1281
Ted Kremenekb3021332008-11-02 00:35:01 +00001282 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001283 // FIXME: Refactor into StoreManager itself?
1284 MemRegionManager& RM = getStateManager().getRegionManager();
1285 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001286 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001287
1288 // Set the extent of the region in bytes. This enables us to use the
1289 // SVal of the argument directly. If we save the extent in bits, we
1290 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001291 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1292 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001293
Ted Kremeneka8538d92009-02-13 01:45:31 +00001294 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001295 continue;
1296 }
1297
Ted Kremenek55aea312008-03-05 22:59:42 +00001298 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001299 break;
1300 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001301 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001302
Ted Kremenek186350f2008-04-23 20:12:28 +00001303 // Check any arguments passed-by-value against being undefined.
1304
1305 bool badArg = false;
1306
1307 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1308 I != E; ++I) {
1309
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001310 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001311 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001312
Ted Kremenek186350f2008-04-23 20:12:28 +00001313 if (N) {
1314 N->markAsSink();
1315 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001316 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001317
Ted Kremenek186350f2008-04-23 20:12:28 +00001318 badArg = true;
1319 break;
1320 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001321 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001322
1323 if (badArg)
1324 continue;
1325
1326 // Dispatch to the plug-in transfer function.
1327
1328 unsigned size = Dst.size();
1329 SaveOr OldHasGen(Builder->HasGeneratedNode);
1330 EvalCall(Dst, CE, L, *DI);
1331
1332 // Handle the case where no nodes where generated. Auto-generate that
1333 // contains the updated state if we aren't generating sinks.
1334
1335 if (!Builder->BuildSinks && Dst.size() == size &&
1336 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001337 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001338 }
1339}
1340
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001341//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001342// Transfer function: Objective-C ivar references.
1343//===----------------------------------------------------------------------===//
1344
1345void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1346 NodeTy* Pred, NodeSet& Dst,
1347 bool asLValue) {
1348
1349 Expr* Base = cast<Expr>(Ex->getBase());
1350 NodeSet Tmp;
1351 Visit(Base, Pred, Tmp);
1352
1353 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001354 const GRState* state = GetState(*I);
1355 SVal BaseVal = GetSVal(state, Base);
1356 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001357
1358 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001359 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001360 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001361 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001362 }
1363}
1364
1365//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001366// Transfer function: Objective-C fast enumeration 'for' statements.
1367//===----------------------------------------------------------------------===//
1368
1369void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1370 NodeTy* Pred, NodeSet& Dst) {
1371
1372 // ObjCForCollectionStmts are processed in two places. This method
1373 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1374 // statements within a basic block. This transfer function does two things:
1375 //
1376 // (1) binds the next container value to 'element'. This creates a new
1377 // node in the ExplodedGraph.
1378 //
1379 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1380 // whether or not the container has any more elements. This value
1381 // will be tested in ProcessBranch. We need to explicitly bind
1382 // this value because a container can contain nil elements.
1383 //
1384 // FIXME: Eventually this logic should actually do dispatches to
1385 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1386 // This will require simulating a temporary NSFastEnumerationState, either
1387 // through an SVal or through the use of MemRegions. This value can
1388 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1389 // terminates we reclaim the temporary (it goes out of scope) and we
1390 // we can test if the SVal is 0 or if the MemRegion is null (depending
1391 // on what approach we take).
1392 //
1393 // For now: simulate (1) by assigning either a symbol or nil if the
1394 // container is empty. Thus this transfer function will by default
1395 // result in state splitting.
1396
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001397 Stmt* elem = S->getElement();
1398 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001399
1400 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001401 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001402 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001403 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1404 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1405 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001406 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001407
1408 NodeSet Tmp;
1409 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001410
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001411 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1412 const GRState* state = GetState(*I);
1413 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1414 }
1415}
1416
1417void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1418 NodeTy* Pred, NodeSet& Dst,
1419 SVal ElementV) {
1420
1421
Ted Kremenekaf337412008-11-12 19:24:17 +00001422
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001423 // Get the current state. Use 'EvalLocation' to determine if it is a null
1424 // pointer, etc.
1425 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001426
Ted Kremenek8c354752008-12-16 22:02:27 +00001427 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1428 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001429 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001430
1431 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001432
Ted Kremenekaf337412008-11-12 19:24:17 +00001433 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001434 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001435 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1436 GRStateRef hasElems = state.BindExpr(S, TrueV);
1437
Ted Kremenekaf337412008-11-12 19:24:17 +00001438 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001439 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1440 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001441
1442 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1443 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1444 // FIXME: The proper thing to do is to really iterate over the
1445 // container. We will do this with dispatch logic to the store.
1446 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001447 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001448 assert (Loc::IsLocType(T));
1449 unsigned Count = Builder->getCurrentBlockCount();
1450 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1451 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001452
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001453 // Bind the location to 'nil' on the false branch.
1454 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1455 noElems = noElems.BindLoc(ElementV, nilV);
1456 }
1457
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001458 // Create the new nodes.
1459 MakeNode(Dst, S, Pred, hasElems);
1460 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001461}
1462
1463//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001464// Transfer function: Objective-C message expressions.
1465//===----------------------------------------------------------------------===//
1466
1467void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1468 NodeSet& Dst){
1469
1470 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1471 Pred, Dst);
1472}
1473
1474void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001475 ObjCMessageExpr::arg_iterator AI,
1476 ObjCMessageExpr::arg_iterator AE,
1477 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001478 if (AI == AE) {
1479
1480 // Process the receiver.
1481
1482 if (Expr* Receiver = ME->getReceiver()) {
1483 NodeSet Tmp;
1484 Visit(Receiver, Pred, Tmp);
1485
1486 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1487 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1488
1489 return;
1490 }
1491
1492 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1493 return;
1494 }
1495
1496 NodeSet Tmp;
1497 Visit(*AI, Pred, Tmp);
1498
1499 ++AI;
1500
1501 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1502 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1503}
1504
1505void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1506 NodeTy* Pred,
1507 NodeSet& Dst) {
1508
1509 // FIXME: More logic for the processing the method call.
1510
Ted Kremeneka8538d92009-02-13 01:45:31 +00001511 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001512 bool RaisesException = false;
1513
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001514
1515 if (Expr* Receiver = ME->getReceiver()) {
1516
Ted Kremeneka8538d92009-02-13 01:45:31 +00001517 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001518
1519 // Check for undefined control-flow or calls to NULL.
1520
1521 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001522 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001523
1524 if (N) {
1525 N->markAsSink();
1526 UndefReceivers.insert(N);
1527 }
1528
1529 return;
1530 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001531
1532 // Check if the "raise" message was sent.
1533 if (ME->getSelector() == RaiseSel)
1534 RaisesException = true;
1535 }
1536 else {
1537
1538 IdentifierInfo* ClsName = ME->getClassName();
1539 Selector S = ME->getSelector();
1540
1541 // Check for special instance methods.
1542
1543 if (!NSExceptionII) {
1544 ASTContext& Ctx = getContext();
1545
1546 NSExceptionII = &Ctx.Idents.get("NSException");
1547 }
1548
1549 if (ClsName == NSExceptionII) {
1550
1551 enum { NUM_RAISE_SELECTORS = 2 };
1552
1553 // Lazily create a cache of the selectors.
1554
1555 if (!NSExceptionInstanceRaiseSelectors) {
1556
1557 ASTContext& Ctx = getContext();
1558
1559 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1560
1561 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1562 unsigned idx = 0;
1563
1564 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001565 II.push_back(&Ctx.Idents.get("raise"));
1566 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001567 NSExceptionInstanceRaiseSelectors[idx++] =
1568 Ctx.Selectors.getSelector(II.size(), &II[0]);
1569
1570 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001571 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001572 NSExceptionInstanceRaiseSelectors[idx++] =
1573 Ctx.Selectors.getSelector(II.size(), &II[0]);
1574 }
1575
1576 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1577 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1578 RaisesException = true; break;
1579 }
1580 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001581 }
1582
1583 // Check for any arguments that are uninitialized/undefined.
1584
1585 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1586 I != E; ++I) {
1587
Ted Kremeneka8538d92009-02-13 01:45:31 +00001588 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001589
1590 // Generate an error node for passing an uninitialized/undefined value
1591 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001592 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001593
1594 if (N) {
1595 N->markAsSink();
1596 MsgExprUndefArgs[N] = *I;
1597 }
1598
1599 return;
1600 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001601 }
1602
1603 // Check if we raise an exception. For now treat these as sinks. Eventually
1604 // we will want to handle exceptions properly.
1605
1606 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1607
1608 if (RaisesException)
1609 Builder->BuildSinks = true;
1610
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001611 // Dispatch to plug-in transfer function.
1612
1613 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001614 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001615
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001616 EvalObjCMessageExpr(Dst, ME, Pred);
1617
1618 // Handle the case where no nodes where generated. Auto-generate that
1619 // contains the updated state if we aren't generating sinks.
1620
Ted Kremenekb0533962008-04-18 20:35:30 +00001621 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001622 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001623}
1624
1625//===----------------------------------------------------------------------===//
1626// Transfer functions: Miscellaneous statements.
1627//===----------------------------------------------------------------------===//
1628
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001629void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1630 QualType PtrTy,
1631 Expr* CastE, NodeTy* Pred,
1632 NodeSet& Dst) {
1633 if (!V.isUnknownOrUndef()) {
1634 // FIXME: Determine if the number of bits of the target type is
1635 // equal or exceeds the number of bits to store the pointer value.
1636 // If not, flag an error.
1637 unsigned bits = getContext().getTypeSize(PtrTy);
1638 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1639 }
1640
1641 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1642}
1643
1644
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001645void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001646 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001647 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001648 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001649
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001650 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001651 T = ExCast->getTypeAsWritten();
1652
Zhongxing Xued340f72008-10-22 08:02:16 +00001653 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001654 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001655 else
1656 Visit(Ex, Pred, S1);
1657
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001658 // Check for casting to "void".
1659 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001660
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001661 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001662 Dst.Add(*I1);
1663
Ted Kremenek874d63f2008-01-24 02:02:54 +00001664 return;
1665 }
1666
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001667 // FIXME: The rest of this should probably just go into EvalCall, and
1668 // let the transfer function object be responsible for constructing
1669 // nodes.
1670
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001671 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001672 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001673 const GRState* state = GetState(N);
1674 SVal V = GetSVal(state, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001675
1676 // Unknown?
1677
1678 if (V.isUnknown()) {
1679 Dst.Add(N);
1680 continue;
1681 }
1682
1683 // Undefined?
1684
1685 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001686 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001687 continue;
1688 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001689
1690 // For const casts, just propagate the value.
1691 ASTContext& C = getContext();
1692
1693 if (C.getCanonicalType(T).getUnqualifiedType() ==
1694 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001695 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001696 continue;
1697 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001698
1699 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001700 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001701 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001702 continue;
1703 }
1704
1705 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001706 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1707 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001708 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001709 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001710 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001711 continue;
1712 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001713
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001714 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001715 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001716 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001717 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001718
1719 // Are we casting from an array to a pointer? If so just pass on
1720 // the decayed value.
1721 if (T->isPointerType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001722 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001723 continue;
1724 }
1725
1726 // Are we casting from an array to an integer? If so, cast the decayed
1727 // pointer value to an integer.
1728 assert(T->isIntegerType());
1729 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1730 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001731 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001732 continue;
1733 }
1734
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001735 // Check for casts from a region to a specific type.
1736 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001737 assert(Loc::IsLocType(T));
1738 assert(Loc::IsLocType(ExTy));
1739
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001740 const MemRegion* R = RV->getRegion();
1741 StoreManager& StoreMgr = getStoreManager();
1742
1743 // Delegate to store manager to get the result of casting a region
1744 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001745 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001746
1747 // Inspect the result. If the MemRegion* returned is NULL, this
1748 // expression evaluates to UnknownVal.
1749 R = Res.getRegion();
1750 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1751
1752 // Generate the new node in the ExplodedGraph.
1753 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001754 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001755 }
1756
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001757 // All other cases.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001758 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1759 EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001760 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001761}
1762
Ted Kremenek4f090272008-10-27 21:54:31 +00001763void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001764 NodeTy* Pred, NodeSet& Dst,
1765 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001766 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1767 NodeSet Tmp;
1768 Visit(ILE, Pred, Tmp);
1769
1770 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001771 const GRState* state = GetState(*I);
1772 SVal ILV = GetSVal(state, ILE);
1773 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001774
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001775 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001776 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001777 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001778 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001779 }
1780}
1781
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001782void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001783
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001784 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001785 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001786
1787 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001788 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001789
Ted Kremenekefd59942008-12-08 22:47:34 +00001790 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001791 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001792
1793 // FIXME: static variables may have an initializer, but the second
1794 // time a function is called those values may not be current.
1795 NodeSet Tmp;
1796
Ted Kremenekaf337412008-11-12 19:24:17 +00001797 if (InitEx)
1798 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001799
1800 if (Tmp.empty())
1801 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001802
1803 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001804 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001805 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001806
1807 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001808 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001809 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00001810 QualType T = VD->getType();
1811
1812 // Recover some path-sensitivity if a scalar value evaluated to
1813 // UnknownVal.
1814 if (InitVal.isUnknown()) {
1815 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001816 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001817 InitVal = loc::SymbolVal(Sym);
1818 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001819 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001820 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001821 InitVal = nonloc::SymbolVal(Sym);
1822 }
1823 }
1824
Ted Kremeneka8538d92009-02-13 01:45:31 +00001825 state = StateMgr.BindDecl(state, VD, InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001826 } else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001827 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001828
1829 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1830 QualType T = getContext().getCanonicalType(VD->getType());
1831 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1832 // FIXME: Handle multi-dimensional VLAs.
1833
1834 Expr* SE = VLA->getSizeExpr();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001835 SVal Size = GetSVal(state, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001836
1837 if (Size.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001838 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
Ted Kremenek159d2482008-12-09 00:44:16 +00001839 N->markAsSink();
1840 ExplicitBadSizedVLA.insert(N);
1841 }
1842 continue;
1843 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001844
1845 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001846 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
Ted Kremenekefd59942008-12-08 22:47:34 +00001847
1848 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001849 state = Assume(state, Size, true, isFeasibleNotZero);
Ted Kremenekefd59942008-12-08 22:47:34 +00001850
1851 if (isFeasibleZero) {
1852 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1853 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001854 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1855 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001856 }
1857 }
1858
1859 if (!isFeasibleNotZero)
1860 continue;
1861 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001862
Ted Kremeneka8538d92009-02-13 01:45:31 +00001863 MakeNode(Dst, DS, *I, state);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001864 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001865}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001866
Ted Kremenekf75b1862008-10-30 17:47:32 +00001867namespace {
1868 // This class is used by VisitInitListExpr as an item in a worklist
1869 // for processing the values contained in an InitListExpr.
1870class VISIBILITY_HIDDEN InitListWLItem {
1871public:
1872 llvm::ImmutableList<SVal> Vals;
1873 GRExprEngine::NodeTy* N;
1874 InitListExpr::reverse_iterator Itr;
1875
1876 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1877 InitListExpr::reverse_iterator itr)
1878 : Vals(vals), N(n), Itr(itr) {}
1879};
1880}
1881
1882
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001883void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1884 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001885
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001886 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001887 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001888 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001889
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001890 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001891
Ted Kremeneka49e3672008-10-30 23:14:36 +00001892 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001893
Ted Kremeneka49e3672008-10-30 23:14:36 +00001894 // Handle base case where the initializer has no elements.
1895 // e.g: static int* myArray[] = {};
1896 if (NumInitElements == 0) {
1897 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1898 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1899 return;
1900 }
1901
1902 // Create a worklist to process the initializers.
1903 llvm::SmallVector<InitListWLItem, 10> WorkList;
1904 WorkList.reserve(NumInitElements);
1905 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001906 InitListExpr::reverse_iterator ItrEnd = E->rend();
1907
Ted Kremeneka49e3672008-10-30 23:14:36 +00001908 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001909 while (!WorkList.empty()) {
1910 InitListWLItem X = WorkList.back();
1911 WorkList.pop_back();
1912
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001913 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001914 Visit(*X.Itr, X.N, Tmp);
1915
1916 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001917
Ted Kremenekf75b1862008-10-30 17:47:32 +00001918 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1919 // Get the last initializer value.
1920 state = GetState(*NI);
1921 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1922
1923 // Construct the new list of values by prepending the new value to
1924 // the already constructed list.
1925 llvm::ImmutableList<SVal> NewVals =
1926 getBasicVals().consVals(InitV, X.Vals);
1927
1928 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001929 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001930 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001931
Ted Kremenekf75b1862008-10-30 17:47:32 +00001932 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001933 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001934 }
1935 else {
1936 // Still some initializer values to go. Push them onto the worklist.
1937 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1938 }
1939 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001940 }
Ted Kremenek87903072008-10-30 18:34:31 +00001941
1942 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001943 }
1944
Ted Kremenek062e2f92008-11-13 06:10:40 +00001945 if (T->isUnionType() || T->isVectorType()) {
1946 // FIXME: to be implemented.
1947 // Note: That vectors can return true for T->isIntegerType()
1948 MakeNode(Dst, E, Pred, state);
1949 return;
1950 }
1951
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001952 if (Loc::IsLocType(T) || T->isIntegerType()) {
1953 assert (E->getNumInits() == 1);
1954 NodeSet Tmp;
1955 Expr* Init = E->getInit(0);
1956 Visit(Init, Pred, Tmp);
1957 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1958 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001959 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001960 }
1961 return;
1962 }
1963
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001964
1965 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1966 assert(0 && "unprocessed InitListExpr type");
1967}
Ted Kremenekf233d482008-02-05 00:26:40 +00001968
Sebastian Redl05189992008-11-11 17:56:53 +00001969/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1970void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1971 NodeTy* Pred,
1972 NodeSet& Dst) {
1973 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001974 uint64_t amt;
1975
1976 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001977 if (T == getContext().VoidTy) {
1978 // sizeof(void) == 1 byte.
1979 amt = 1;
1980 }
1981 else if (!T.getTypePtr()->isConstantSizeType()) {
1982 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00001983 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001984 }
1985 else if (T->isObjCInterfaceType()) {
1986 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1987 // the compiler has laid out its representation. Just report Unknown
1988 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00001989 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001990 }
1991 else {
1992 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00001993 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001994 }
Ted Kremenek87e80342008-03-15 03:13:20 +00001995 }
1996 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001997 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001998
Ted Kremenek0e561a32008-03-21 21:30:14 +00001999 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002000 BindExpr(GetState(Pred), Ex,
2001 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002002}
2003
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002004
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002005void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002006 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002007
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002008 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002009
2010 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002011 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002012
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002013 case UnaryOperator::Deref: {
2014
2015 Expr* Ex = U->getSubExpr()->IgnoreParens();
2016 NodeSet Tmp;
2017 Visit(Ex, Pred, Tmp);
2018
2019 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002020
Ted Kremeneka8538d92009-02-13 01:45:31 +00002021 const GRState* state = GetState(*I);
2022 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002023
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002024 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002025 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002026 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002027 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002028 }
2029
2030 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002031 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002032
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002033 case UnaryOperator::Real: {
2034
2035 Expr* Ex = U->getSubExpr()->IgnoreParens();
2036 NodeSet Tmp;
2037 Visit(Ex, Pred, Tmp);
2038
2039 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2040
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002041 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002042 if (Ex->getType()->isAnyComplexType()) {
2043 // Just report "Unknown."
2044 Dst.Add(*I);
2045 continue;
2046 }
2047
2048 // For all other types, UnaryOperator::Real is an identity operation.
2049 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002050 const GRState* state = GetState(*I);
2051 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002052 }
2053
2054 return;
2055 }
2056
2057 case UnaryOperator::Imag: {
2058
2059 Expr* Ex = U->getSubExpr()->IgnoreParens();
2060 NodeSet Tmp;
2061 Visit(Ex, Pred, Tmp);
2062
2063 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002064 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002065 if (Ex->getType()->isAnyComplexType()) {
2066 // Just report "Unknown."
2067 Dst.Add(*I);
2068 continue;
2069 }
2070
2071 // For all other types, UnaryOperator::Float returns 0.
2072 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002073 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002074 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002075 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002076 }
2077
2078 return;
2079 }
2080
2081 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002082 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002083 Dst.Add(Pred);
2084 return;
2085
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002086 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002087 case UnaryOperator::Extension: {
2088
2089 // Unary "+" is a no-op, similar to a parentheses. We still have places
2090 // where it may be a block-level expression, so we need to
2091 // generate an extra node that just propagates the value of the
2092 // subexpression.
2093
2094 Expr* Ex = U->getSubExpr()->IgnoreParens();
2095 NodeSet Tmp;
2096 Visit(Ex, Pred, Tmp);
2097
2098 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002099 const GRState* state = GetState(*I);
2100 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002101 }
2102
2103 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002104 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002105
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002106 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002107
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002108 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002109 Expr* Ex = U->getSubExpr()->IgnoreParens();
2110 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002111 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002112
2113 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002114 const GRState* state = GetState(*I);
2115 SVal V = GetSVal(state, Ex);
2116 state = BindExpr(state, U, V);
2117 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002118 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002119
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002120 return;
2121 }
2122
2123 case UnaryOperator::LNot:
2124 case UnaryOperator::Minus:
2125 case UnaryOperator::Not: {
2126
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002127 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002128 Expr* Ex = U->getSubExpr()->IgnoreParens();
2129 NodeSet Tmp;
2130 Visit(Ex, Pred, Tmp);
2131
2132 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002133 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002134
2135 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002136 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002137
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002138 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002139 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002140 continue;
2141 }
2142
Ted Kremenek60595da2008-11-15 04:01:56 +00002143// QualType DstT = getContext().getCanonicalType(U->getType());
2144// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2145//
2146// if (DstT != SrcT) // Perform promotions.
2147// V = EvalCast(V, DstT);
2148//
2149// if (V.isUnknownOrUndef()) {
2150// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2151// continue;
2152// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002153
2154 switch (U->getOpcode()) {
2155 default:
2156 assert(false && "Invalid Opcode.");
2157 break;
2158
2159 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002160 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002161 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002162 break;
2163
2164 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002165 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002166 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002167 break;
2168
2169 case UnaryOperator::LNot:
2170
2171 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2172 //
2173 // Note: technically we do "E == 0", but this is the same in the
2174 // transfer functions as "0 == E".
2175
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002176 if (isa<Loc>(V)) {
2177 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2178 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002179 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002180 }
2181 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002182 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002183#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002184 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002185 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002186#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002187 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002188 continue;
2189#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002190 }
2191
2192 break;
2193 }
2194
Ted Kremeneka8538d92009-02-13 01:45:31 +00002195 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002196 }
2197
2198 return;
2199 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002200 }
2201
2202 // Handle ++ and -- (both pre- and post-increment).
2203
2204 assert (U->isIncrementDecrementOp());
2205 NodeSet Tmp;
2206 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002207 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002208
2209 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2210
Ted Kremeneka8538d92009-02-13 01:45:31 +00002211 const GRState* state = GetState(*I);
2212 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002213
2214 // Perform a load.
2215 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002216 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002217
2218 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2219
Ted Kremeneka8538d92009-02-13 01:45:31 +00002220 state = GetState(*I2);
2221 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002222
2223 // Propagate unknown and undefined values.
2224 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002225 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002226 continue;
2227 }
2228
Ted Kremenek443003b2008-02-21 19:29:23 +00002229 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002230
2231 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2232 : BinaryOperator::Sub;
2233
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002234 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Ted Kremeneka8538d92009-02-13 01:45:31 +00002235 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002236
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002237 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002238 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002239 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002240 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002241}
2242
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002243void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2244 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2245}
2246
2247void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2248 AsmStmt::outputs_iterator I,
2249 AsmStmt::outputs_iterator E,
2250 NodeTy* Pred, NodeSet& Dst) {
2251 if (I == E) {
2252 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2253 return;
2254 }
2255
2256 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002257 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002258
2259 ++I;
2260
2261 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2262 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2263}
2264
2265void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2266 AsmStmt::inputs_iterator I,
2267 AsmStmt::inputs_iterator E,
2268 NodeTy* Pred, NodeSet& Dst) {
2269 if (I == E) {
2270
2271 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002272 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002273
2274 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2275 // which interprets the inline asm and stores proper results in the
2276 // outputs.
2277
Ted Kremeneka8538d92009-02-13 01:45:31 +00002278 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002279
2280 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2281 OE = A->end_outputs(); OI != OE; ++OI) {
2282
Ted Kremeneka8538d92009-02-13 01:45:31 +00002283 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002284 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002285
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002286 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002287 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002288 }
2289
Ted Kremeneka8538d92009-02-13 01:45:31 +00002290 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002291 return;
2292 }
2293
2294 NodeSet Tmp;
2295 Visit(*I, Pred, Tmp);
2296
2297 ++I;
2298
2299 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2300 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2301}
2302
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002303void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2304 assert (Builder && "GRStmtNodeBuilder must be defined.");
2305
2306 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002307
Ted Kremenek186350f2008-04-23 20:12:28 +00002308 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2309 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002310
Ted Kremenek729a9a22008-07-17 23:15:45 +00002311 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002312
Ted Kremenekb0533962008-04-18 20:35:30 +00002313 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002314
Ted Kremenekb0533962008-04-18 20:35:30 +00002315 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002316 MakeNode(Dst, S, Pred, GetState(Pred));
2317}
2318
Ted Kremenek02737ed2008-03-31 15:02:58 +00002319void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2320
2321 Expr* R = S->getRetValue();
2322
2323 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002324 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002325 return;
2326 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002327
Ted Kremenek5917d782008-11-21 00:27:44 +00002328 NodeSet Tmp;
2329 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002330
Ted Kremenek5917d782008-11-21 00:27:44 +00002331 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2332 SVal X = GetSVal((*I)->getState(), R);
2333
2334 // Check if we return the address of a stack variable.
2335 if (isa<loc::MemRegionVal>(X)) {
2336 // Determine if the value is on the stack.
2337 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002338
Ted Kremenek5917d782008-11-21 00:27:44 +00002339 if (R && getStateManager().hasStackStorage(R)) {
2340 // Create a special node representing the error.
2341 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2342 N->markAsSink();
2343 RetsStackAddr.insert(N);
2344 }
2345 continue;
2346 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002347 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002348 // Check if we return an undefined value.
2349 else if (X.isUndef()) {
2350 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2351 N->markAsSink();
2352 RetsUndef.insert(N);
2353 }
2354 continue;
2355 }
2356
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002357 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002358 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002359}
Ted Kremenek55deb972008-03-25 00:34:37 +00002360
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002361//===----------------------------------------------------------------------===//
2362// Transfer functions: Binary operators.
2363//===----------------------------------------------------------------------===//
2364
Ted Kremeneka8538d92009-02-13 01:45:31 +00002365const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002366 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002367
2368 // Divide by undefined? (potentially zero)
2369
2370 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002371 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002372
2373 if (DivUndef) {
2374 DivUndef->markAsSink();
2375 ExplicitBadDivides.insert(DivUndef);
2376 }
2377
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002378 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002379 }
2380
2381 // Check for divide/remainder-by-zero.
2382 // First, "assume" that the denominator is 0 or undefined.
2383
2384 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002385 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002386
2387 // Second, "assume" that the denominator cannot be 0.
2388
2389 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002390 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002391
2392 // Create the node for the divide-by-zero (if it occurred).
2393
2394 if (isFeasibleZero)
2395 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2396 DivZeroNode->markAsSink();
2397
2398 if (isFeasibleNotZero)
2399 ImplicitBadDivides.insert(DivZeroNode);
2400 else
2401 ExplicitBadDivides.insert(DivZeroNode);
2402
2403 }
2404
Ted Kremeneka8538d92009-02-13 01:45:31 +00002405 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002406}
2407
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002408void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002409 GRExprEngine::NodeTy* Pred,
2410 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002411
2412 NodeSet Tmp1;
2413 Expr* LHS = B->getLHS()->IgnoreParens();
2414 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002415
Ted Kremenek759623e2008-12-06 02:39:30 +00002416 // FIXME: Add proper support for ObjCKVCRefExpr.
2417 if (isa<ObjCKVCRefExpr>(LHS)) {
2418 Visit(RHS, Pred, Dst);
2419 return;
2420 }
2421
2422
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002423 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002424 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002425 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002426 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002427
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002428 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002429
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002430 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002431
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002432 // Process the RHS.
2433
2434 NodeSet Tmp2;
2435 Visit(RHS, *I1, Tmp2);
2436
2437 // With both the LHS and RHS evaluated, process the operation itself.
2438
2439 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002440
Ted Kremeneka8538d92009-02-13 01:45:31 +00002441 const GRState* state = GetState(*I2);
2442 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002443
Ted Kremeneka8538d92009-02-13 01:45:31 +00002444 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002445 BinaryOperator::Opcode Op = B->getOpcode();
2446
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002447 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002448
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002449 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002450
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002451 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002452 // FIXME: Handle structs.
2453 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002454
Ted Kremenek062e2f92008-11-13 06:10:40 +00002455 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2456 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002457 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002458 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002459
Ted Kremenek062e2f92008-11-13 06:10:40 +00002460 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002461 ? cast<SVal>(loc::SymbolVal(Sym))
2462 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002463 }
2464
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002465 // Simulate the effects of a "store": bind the value of the RHS
2466 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002467
Ted Kremeneka8538d92009-02-13 01:45:31 +00002468 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2469 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002470 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002471 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002472
2473 case BinaryOperator::Div:
2474 case BinaryOperator::Rem:
2475
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002476 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002477 if (RHS->getType()->isIntegerType() &&
2478 RHS->getType()->isScalarType()) {
2479
Ted Kremeneka8538d92009-02-13 01:45:31 +00002480 state = CheckDivideZero(B, state, *I2, RightV);
2481 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002482 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002483
2484 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002485
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002486 default: {
2487
2488 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002489 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002490
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002491 // Process non-assignements except commas or short-circuited
2492 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002493
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002494 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002495
2496 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002497 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002498 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002499 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002500 }
2501 else
2502 Dst.Add(*I2);
2503
Ted Kremenek89063af2008-02-21 19:15:37 +00002504 continue;
2505 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002506
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002507 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002508
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002509 // The operands were *not* undefined, but the result is undefined.
2510 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002511
Ted Kremeneka8538d92009-02-13 01:45:31 +00002512 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002513 UndefNode->markAsSink();
2514 UndefResults.insert(UndefNode);
2515 }
2516
2517 continue;
2518 }
2519
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002520 // Otherwise, create a new node.
2521
Ted Kremeneka8538d92009-02-13 01:45:31 +00002522 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002523 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002524 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002525 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002526
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002527 assert (B->isCompoundAssignmentOp());
2528
Ted Kremenekcad29962009-02-07 00:52:24 +00002529 switch (Op) {
2530 default:
2531 assert(0 && "Invalid opcode for compound assignment.");
2532 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2533 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2534 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2535 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2536 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2537 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2538 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2539 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2540 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2541 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002542 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002543
2544 // Perform a load (the LHS). This performs the checks for
2545 // null dereferences, and so on.
2546 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002547 SVal location = GetSVal(state, LHS);
2548 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002549
2550 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2551
Ted Kremeneka8538d92009-02-13 01:45:31 +00002552 state = GetState(*I3);
2553 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002555 // Check for divide-by-zero.
2556 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002557 && RHS->getType()->isIntegerType()
2558 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002559
2560 // CheckDivideZero returns a new state where the denominator
2561 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002562 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002563
Ted Kremeneka8538d92009-02-13 01:45:31 +00002564 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002565 continue;
2566 }
2567
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 // Propagate undefined values (left-side).
2569 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002570 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002571 continue;
2572 }
2573
2574 // Propagate unknown values (left and right-side).
2575 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002576 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2577 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002578 continue;
2579 }
2580
2581 // At this point:
2582 //
2583 // The LHS is not Undef/Unknown.
2584 // The RHS is not Unknown.
2585
2586 // Get the computation type.
2587 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002588 CTy = getContext().getCanonicalType(CTy);
2589
2590 QualType LTy = getContext().getCanonicalType(LHS->getType());
2591 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002592
2593 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002594 if (LTy != CTy) V = EvalCast(V, CTy);
2595 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002596
2597 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002598 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002599 // Propagate undefined values (right-side).
Ted Kremeneka8538d92009-02-13 01:45:31 +00002600 EvalStore(Dst,B, LHS, *I3, BindExpr(state, B, RightV), location,
2601 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002602 continue;
2603 }
2604
Ted Kremenek60595da2008-11-15 04:01:56 +00002605 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002606 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002607
2608 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002610 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002611 UndefNode->markAsSink();
2612 UndefResults.insert(UndefNode);
2613 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002614 continue;
2615 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002616
2617 // EXPERIMENTAL: "Conjured" symbols.
2618 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002619
2620 SVal LHSVal;
2621
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002622 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2623 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002624
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002625 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002626
Ted Kremenek60595da2008-11-15 04:01:56 +00002627 // The symbolic value is actually for the type of the left-hand side
2628 // expression, not the computation type, as this is the value the
2629 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002630 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002631 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002632 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002633 : cast<SVal>(nonloc::SymbolVal(Sym));
2634
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002635 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002636 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002637 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002638 else {
2639 // The left-hand side may bind to a different value then the
2640 // computation type.
2641 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2642 }
2643
Ted Kremeneka8538d92009-02-13 01:45:31 +00002644 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2645 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002646 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002647 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002648 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002649}
Ted Kremenekee985462008-01-16 18:18:48 +00002650
2651//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002652// Transfer-function Helpers.
2653//===----------------------------------------------------------------------===//
2654
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002655void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002656 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002657 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002658 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002659
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002660 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002661 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2662
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002663 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002664 MakeNode(Dst, Ex, Pred, *I);
2665}
2666
Ted Kremeneka8538d92009-02-13 01:45:31 +00002667void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002668 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002669 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002670
Ted Kremeneka8538d92009-02-13 01:45:31 +00002671 GRStateSet::AutoPopulate AP(OStates, state);
2672 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002673}
2674
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002675SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R) {
2676
2677 if (L.isUndef() || R.isUndef())
2678 return UndefinedVal();
2679
2680 if (L.isUnknown() || R.isUnknown())
2681 return UnknownVal();
2682
2683 if (isa<Loc>(L)) {
2684 if (isa<Loc>(R))
2685 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2686 else
2687 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2688 }
2689
2690 if (isa<Loc>(R)) {
2691 // Support pointer arithmetic where the increment/decrement operand
2692 // is on the left and the pointer on the right.
2693
2694 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2695
2696 // Commute the operands.
2697 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2698 cast<NonLoc>(L));
2699 }
2700 else
2701 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
2702 cast<NonLoc>(R));
2703}
2704
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002705//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002706// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002707//===----------------------------------------------------------------------===//
2708
Ted Kremenekaa66a322008-01-16 21:46:15 +00002709#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002710static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002711static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002712
Ted Kremenekaa66a322008-01-16 21:46:15 +00002713namespace llvm {
2714template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002715struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002716 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002717
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002718 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2719
2720 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002721 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002722 GraphPrintCheckerState->isUndefDeref(N) ||
2723 GraphPrintCheckerState->isUndefStore(N) ||
2724 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002725 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2726 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002727 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002728 GraphPrintCheckerState->isBadCall(N) ||
2729 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002730 return "color=\"red\",style=\"filled\"";
2731
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002732 if (GraphPrintCheckerState->isNoReturnCall(N))
2733 return "color=\"blue\",style=\"filled\"";
2734
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002735 return "";
2736 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002737
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002738 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002739 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002740
2741 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002742 ProgramPoint Loc = N->getLocation();
2743
2744 switch (Loc.getKind()) {
2745 case ProgramPoint::BlockEntranceKind:
2746 Out << "Block Entrance: B"
2747 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2748 break;
2749
2750 case ProgramPoint::BlockExitKind:
2751 assert (false);
2752 break;
2753
Ted Kremenekaa66a322008-01-16 21:46:15 +00002754 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002755 if (isa<PostStmt>(Loc)) {
2756 const PostStmt& L = cast<PostStmt>(Loc);
2757 Stmt* S = L.getStmt();
2758 SourceLocation SLoc = S->getLocStart();
2759
2760 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2761 llvm::raw_os_ostream OutS(Out);
2762 S->printPretty(OutS);
2763 OutS.flush();
2764
2765 if (SLoc.isFileID()) {
2766 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002767 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2768 << " col="
2769 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2770 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00002771 }
2772
2773 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2774 Out << "\\|Implicit-Null Dereference.\\l";
2775 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2776 Out << "\\|Explicit-Null Dereference.\\l";
2777 else if (GraphPrintCheckerState->isUndefDeref(N))
2778 Out << "\\|Dereference of undefialied value.\\l";
2779 else if (GraphPrintCheckerState->isUndefStore(N))
2780 Out << "\\|Store to Undefined Loc.";
2781 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2782 Out << "\\|Explicit divide-by zero or undefined value.";
2783 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2784 Out << "\\|Implicit divide-by zero or undefined value.";
2785 else if (GraphPrintCheckerState->isUndefResult(N))
2786 Out << "\\|Result of operation is undefined.";
2787 else if (GraphPrintCheckerState->isNoReturnCall(N))
2788 Out << "\\|Call to function marked \"noreturn\".";
2789 else if (GraphPrintCheckerState->isBadCall(N))
2790 Out << "\\|Call to NULL/Undefined.";
2791 else if (GraphPrintCheckerState->isUndefArg(N))
2792 Out << "\\|Argument in call is undefined";
2793
2794 break;
2795 }
2796
Ted Kremenekaa66a322008-01-16 21:46:15 +00002797 const BlockEdge& E = cast<BlockEdge>(Loc);
2798 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2799 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002800
2801 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002802
2803 SourceLocation SLoc = T->getLocStart();
2804
Ted Kremenekb38911f2008-01-30 23:03:39 +00002805 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002806
Ted Kremeneka95d3752008-09-13 05:16:45 +00002807 llvm::raw_os_ostream OutS(Out);
2808 E.getSrc()->printTerminator(OutS);
2809 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002810
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002811 if (SLoc.isFileID()) {
2812 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002813 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2814 << " col="
2815 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002816 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002817
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002818 if (isa<SwitchStmt>(T)) {
2819 Stmt* Label = E.getDst()->getLabel();
2820
2821 if (Label) {
2822 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2823 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002824 llvm::raw_os_ostream OutS(Out);
2825 C->getLHS()->printPretty(OutS);
2826 OutS.flush();
2827
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002828 if (Stmt* RHS = C->getRHS()) {
2829 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002830 RHS->printPretty(OutS);
2831 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002832 }
2833
2834 Out << ":";
2835 }
2836 else {
2837 assert (isa<DefaultStmt>(Label));
2838 Out << "\\ldefault:";
2839 }
2840 }
2841 else
2842 Out << "\\l(implicit) default:";
2843 }
2844 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002845 // FIXME
2846 }
2847 else {
2848 Out << "\\lCondition: ";
2849 if (*E.getSrc()->succ_begin() == E.getDst())
2850 Out << "true";
2851 else
2852 Out << "false";
2853 }
2854
2855 Out << "\\l";
2856 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002857
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002858 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2859 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002860 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002861 }
2862 }
2863
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002864 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002865
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002866 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2867 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002868
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002869 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002870 return Out.str();
2871 }
2872};
2873} // end llvm namespace
2874#endif
2875
Ted Kremenekffe0f432008-03-07 22:58:01 +00002876#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002877
2878template <typename ITERATOR>
2879GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2880
2881template <>
2882GRExprEngine::NodeTy*
2883GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2884 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2885 return I->first;
2886}
2887
Ted Kremenekffe0f432008-03-07 22:58:01 +00002888template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002889static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002890 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002891
Ted Kremenekd4527582008-09-16 18:44:52 +00002892 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002893
2894 for ( ; I != E; ++I ) {
2895 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002896 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002897
Ted Kremenekd4527582008-09-16 18:44:52 +00002898 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002899 continue;
2900
Ted Kremenekd4527582008-09-16 18:44:52 +00002901 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002902 Sources.push_back(N);
2903 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002904}
2905#endif
2906
2907void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002908#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002909 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002910 std::vector<NodeTy*> Src;
2911
Ted Kremenekcf118d42009-02-04 23:49:09 +00002912 // FIXME: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002913 AddSources(Src, null_derefs_begin(), null_derefs_end());
2914 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2915 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2916 AddSources(Src, undef_results_begin(), undef_results_end());
2917 AddSources(Src, bad_calls_begin(), bad_calls_end());
2918 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002919 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002920
Ted Kremenekcf118d42009-02-04 23:49:09 +00002921 // FIXME: Enhance BugReporter to have a clean way to query if a node
2922 // is involved in an error... and what kind.
Ted Kremenekcb612922008-04-18 19:23:43 +00002923
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002924 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002925 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002926 else {
2927 GraphPrintCheckerState = this;
2928 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002929
Ted Kremenekffe0f432008-03-07 22:58:01 +00002930 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002931
2932 GraphPrintCheckerState = NULL;
2933 GraphPrintSourceManager = NULL;
2934 }
2935#endif
2936}
2937
2938void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2939#ifndef NDEBUG
2940 GraphPrintCheckerState = this;
2941 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002942
Ted Kremenekcf118d42009-02-04 23:49:09 +00002943 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00002944
Ted Kremenekcf118d42009-02-04 23:49:09 +00002945 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00002946 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00002947 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00002948 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00002949
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002950 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002951 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002952#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002953}