blob: e8c5be51d6ac3e24e78cbf3a5f7512014cd51a3c [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-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 Kremenek50df4f42008-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 Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek0e80dea2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
21#include "clang/Basic/SourceManager.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000023#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000024#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000025#include "llvm/ADT/ImmutableList.h"
26#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000028
Ted Kremenek9f6b1612008-02-27 06:07:00 +000029#ifndef NDEBUG
30#include "llvm/Support/GraphWriter.h"
31#include <sstream>
32#endif
33
Ted Kremenekd4467432008-02-14 22:16:04 +000034using namespace clang;
35using llvm::dyn_cast;
36using llvm::cast;
37using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000038
Ted Kremenekca5f6202008-04-15 23:06:53 +000039//===----------------------------------------------------------------------===//
40// Engine construction and deletion.
41//===----------------------------------------------------------------------===//
42
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000043namespace {
44
45class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
46 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
47 typedef llvm::DenseMap<void*,Checks> MapTy;
48
49 MapTy M;
50 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000051 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000052
53public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000054 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
55 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000056
57 virtual ~MappedBatchAuditor() {
58 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
59
60 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
61 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
62
63 GRSimpleAPICheck* check = *I;
64
65 if (AlreadyVisited.count(check))
66 continue;
67
68 AlreadyVisited.insert(check);
69 delete check;
70 }
71 }
72
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000073 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000074 assert (A && "Check cannot be null.");
75 void* key = reinterpret_cast<void*>((uintptr_t) C);
76 MapTy::iterator I = M.find(key);
77 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
78 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000079
80 void AddCheck(GRSimpleAPICheck *A) {
81 assert (A && "Check cannot be null.");
82 AllStmts = F.Concat(A, AllStmts);
83 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000084
Ted Kremenekabd89ac2008-08-13 04:27:00 +000085 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000086 // First handle the auditors that accept all statements.
87 bool isSink = false;
88 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
89 isSink |= (*I)->Audit(N, VMgr);
90
91 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000092 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
93 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
94 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000095 if (MI != M.end()) {
96 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
97 isSink |= (*I)->Audit(N, VMgr);
98 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000099
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000100 return isSink;
101 }
102};
103
104} // end anonymous namespace
105
106//===----------------------------------------------------------------------===//
107// Engine construction and deletion.
108//===----------------------------------------------------------------------===//
109
Ted Kremenek5f20a632008-05-01 18:33:28 +0000110static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
111 IdentifierInfo* II = &Ctx.Idents.get(name);
112 return Ctx.Selectors.getSelector(0, &II);
113}
114
Ted Kremenekf973eb02008-03-09 18:05:48 +0000115
Ted Kremenek1607f512008-07-02 20:13:38 +0000116GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000117 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000118 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000119 StoreManagerCreator SMC,
120 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000121 : CoreEngine(cfg, CD, Ctx, *this),
122 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000123 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000124 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000125 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000127 ValMgr(StateMgr.getValueManager()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000128 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000129 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
130 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000131 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000132 BR(BRD, *this),
133 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000134
Ted Kremenek72f52c02008-06-20 21:45:25 +0000135GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000136 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000137 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000138}
139
Ted Kremenekca5f6202008-04-15 23:06:53 +0000140//===----------------------------------------------------------------------===//
141// Utility methods.
142//===----------------------------------------------------------------------===//
143
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000144
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000145void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000146 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000147 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000148 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000149}
150
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000151void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
152 if (!BatchAuditor)
153 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
154
155 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000156}
157
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000158void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
159 if (!BatchAuditor)
160 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
161
162 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
163}
164
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000165const GRState* GRExprEngine::getInitialState() {
Ted Kremenek04c0add2009-04-10 00:59:50 +0000166 const GRState *state = StateMgr.getInitialState();
167
168 // Precondition: the first argument of 'main' is an integer guaranteed
169 // to be > 0.
170 // FIXME: It would be nice if we had a more general mechanism to add
171 // such preconditions. Some day.
172 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
173 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
174 FD->getNumParams() > 0) {
175 const ParmVarDecl *PD = FD->getParamDecl(0);
176 QualType T = PD->getType();
177 if (T->isIntegerType())
178 if (const MemRegion *R = StateMgr.getRegion(PD)) {
179 SVal V = GetSVal(state, loc::MemRegionVal(R));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000180 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek04c0add2009-04-10 00:59:50 +0000181 ValMgr.makeZeroVal(T),
182 getContext().IntTy);
183 bool isFeasible = false;
184 const GRState *newState = Assume(state, Constraint, true,
185 isFeasible);
186 if (newState) state = newState;
187 }
188 }
189
190 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000191}
192
Ted Kremenekca5f6202008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek820c73b2009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremenekca5f6202008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000213
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000215 SymbolReaper SymReaper(Liveness, SymMgr);
216 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt, SymReaper)
218 : EntryNode->getState();
219
Ted Kremenek7487f942008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000222
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenekf05eec42008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenekc7469542008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000237 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek7487f942008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek7487f942008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek7487f942008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000249 Visit(S, *I, Dst);
250
251 // Do we need to auto-generate a node? We only need to do this to generate
252 // a node with a "cleaned" state; GRCoreEngine will actually handle
253 // auto-transitions for other cases.
254 if (Dst.size() == 1 && *Dst.begin() == EntryNode
255 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
256 HasAutoGenerated = true;
257 builder.generateNode(S, GetState(EntryNode), *I);
258 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000259 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000260
Ted Kremenekca5f6202008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenekfa7be362008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000270}
271
Ted Kremenek820c73b2009-03-11 02:41:36 +0000272void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
273 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
274 S->getLocStart(),
275 "Error evaluating statement");
276
Ted Kremenekca5f6202008-04-15 23:06:53 +0000277 // FIXME: add metadata to the CFG so that we can disable
278 // this check when we KNOW that there is no block-level subexpression.
279 // The motivation is that this check requires a hashtable lookup.
280
281 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
282 Dst.Add(Pred);
283 return;
284 }
285
286 switch (S->getStmtClass()) {
287
288 default:
289 // Cases we intentionally have "default" handle:
290 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
291
292 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
293 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000298
299 case Stmt::AsmStmtClass:
300 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
301 break;
302
303 case Stmt::BinaryOperatorClass: {
304 BinaryOperator* B = cast<BinaryOperator>(S);
305
306 if (B->isLogicalOp()) {
307 VisitLogicalExpr(B, Pred, Dst);
308 break;
309 }
310 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
312 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000315
Ted Kremenek8f520972009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremenekca5f6202008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000326
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000331 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000332 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000333
Ted Kremenekca5f6202008-04-15 23:06:53 +0000334 // FIXME: ChooseExpr is really a constant. We need to fix
335 // the CFG do not model them as explicit control-flow.
336
337 case Stmt::ChooseExprClass: { // __builtin_choose_expr
338 ChooseExpr* C = cast<ChooseExpr>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::CompoundAssignOperatorClass:
344 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
345 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000350
351 case Stmt::ConditionalOperatorClass: { // '?' operator
352 ConditionalOperator* C = cast<ConditionalOperator>(S);
353 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
354 break;
355 }
356
357 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000376
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000388
Ted Kremenekca5f6202008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenek3c186252008-12-09 20:18:58 +0000394 case Stmt::ObjCAtThrowStmtClass: {
395 // FIXME: This is not complete. We basically treat @throw as
396 // an abort.
397 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
398 Builder->BuildSinks = true;
399 MakeNode(Dst, S, Pred, GetState(Pred));
400 break;
401 }
402
Ted Kremenekca5f6202008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000417
418 if (SE->getSubStmt()->body_empty()) {
419 // Empty statement expression.
420 assert(SE->getType() == getContext().VoidTy
421 && "Empty statement expression must have void type.");
422 Dst.Add(Pred);
423 break;
424 }
425
426 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
427 const GRState* state = GetState(Pred);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000428 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000429 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000439
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000440 case Stmt::UnaryOperatorClass: {
441 UnaryOperator *U = cast<UnaryOperator>(S);
442 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
443 NodeSet Tmp;
444 VisitUnaryOperator(U, Pred, Tmp, false);
445 EvalEagerlyAssume(Dst, Tmp, U);
446 }
447 else
448 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000449 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000450 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000455
456 Ex = Ex->IgnoreParens();
457
458 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
459 Dst.Add(Pred);
460 return;
461 }
462
463 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000464
465 case Stmt::ArraySubscriptExprClass:
466 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000478 case Stmt::UnaryOperatorClass:
479 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
480 return;
481
482 case Stmt::MemberExprClass:
483 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
484 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000485
Ted Kremenekd83daa52008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000488 return;
489
Ted Kremenek71c707b2008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek93cb3d12009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000492 // FIXME: Property assignments are lvalues, but not really "locations".
493 // e.g.: self.x = something;
494 // Here the "self.x" really can translate to a method call (setter) when
495 // the assignment is made. Moreover, the entire assignment expression
496 // evaluate to whatever "something" is, not calling the "getter" for
497 // the property (which would make sense since it can have side effects).
498 // We'll probably treat this as a location, but not one that we can
499 // take the address of. Perhaps we need a new SVal class for cases
500 // like thsis?
501 // Note that we have a similar problem for bitfields, since they don't
502 // have "locations" in the sense that we can take their address.
503 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000504 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
508 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000512
Ted Kremenek2c829a32008-10-18 04:15:35 +0000513 default:
514 // Arbitrary subexpressions can return aggregate temporaries that
515 // can be used in a lvalue context. We need to enhance our support
516 // of such temporaries in both the environment and the store, so right
517 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000521
Ted Kremenek2c829a32008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000537// Generic node creation.
538//===----------------------------------------------------------------------===//
539
540GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
541 NodeTy* Pred,
542 const GRState* St,
543 ProgramPoint::Kind K,
544 const void *tag) {
545
546 assert (Builder && "GRStmtNodeBuilder not present.");
547 SaveAndRestore<const void*> OldTag(Builder->Tag);
548 Builder->Tag = tag;
549 return Builder->MakeNode(Dst, S, Pred, St, K);
550}
551
552//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneke66ba682009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000562 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000563
564 case Stmt::BinaryOperatorClass: { // '&&' and '||'
565
566 BinaryOperator* B = cast<BinaryOperator>(Terminator);
567 BinaryOperator::Opcode Op = B->getOpcode();
568
569 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
570
571 // For &&, if we take the true branch, then the value of the whole
572 // expression is that of the RHS expression.
573 //
574 // For ||, if we take the false branch, then the value of the whole
575 // expression is that of the RHS expression.
576
577 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
578 (Op == BinaryOperator::LOr && !branchTaken)
579 ? B->getRHS() : B->getLHS();
580
Ted Kremeneke66ba682009-02-13 01:45:31 +0000581 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000582 }
583
584 case Stmt::ConditionalOperatorClass: { // ?:
585
586 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
587
588 // For ?, if branchTaken == true then the value is either the LHS or
589 // the condition itself. (GNU extension).
590
591 Expr* Ex;
592
593 if (branchTaken)
594 Ex = C->getLHS() ? C->getLHS() : C->getCond();
595 else
596 Ex = C->getRHS();
597
Ted Kremeneke66ba682009-02-13 01:45:31 +0000598 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000599 }
600
601 case Stmt::ChooseExprClass: { // ?:
602
603 ChooseExpr* C = cast<ChooseExpr>(Terminator);
604
605 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000606 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenekc39c2172009-03-13 16:32:54 +0000611/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
612/// to try to recover some path-sensitivity for casts of symbolic
613/// integers that promote their values (which are currently not tracked well).
614/// This function returns the SVal bound to Condition->IgnoreCasts if all the
615// cast(s) did was sign-extend the original value.
616static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
617 Stmt* Condition, ASTContext& Ctx) {
618
619 Expr *Ex = dyn_cast<Expr>(Condition);
620 if (!Ex)
621 return UnknownVal();
622
623 uint64_t bits = 0;
624 bool bitsInit = false;
625
626 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
627 QualType T = CE->getType();
628
629 if (!T->isIntegerType())
630 return UnknownVal();
631
632 uint64_t newBits = Ctx.getTypeSize(T);
633 if (!bitsInit || newBits < bits) {
634 bitsInit = true;
635 bits = newBits;
636 }
637
638 Ex = CE->getSubExpr();
639 }
640
641 // We reached a non-cast. Is it a symbolic value?
642 QualType T = Ex->getType();
643
644 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
645 return UnknownVal();
646
647 return StateMgr.GetSVal(state, Ex);
648}
649
Ted Kremenek13e167f2008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000652
Ted Kremenek17c5f112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000656
Ted Kremenek022b6052008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremeneke43de222009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Zhongxing Xu097fc982008-10-17 05:57:07 +0000667 SVal V = GetSVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenekc39c2172009-03-13 16:32:54 +0000673 case SVal::UnknownKind: {
674 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
675 if (Ex->getType()->isIntegerType()) {
676 // Try to recover some path-sensitivity. Right now casts of symbolic
677 // integers that promote their values are currently not tracked well.
678 // If 'Condition' is such an expression, try and recover the
679 // underlying value and use that instead.
680 SVal recovered = RecoverCastedSymbol(getStateManager(),
681 builder.getState(), Condition,
682 getContext());
683
684 if (!recovered.isUnknown()) {
685 V = recovered;
686 break;
687 }
688 }
689 }
690
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000693 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000694 }
Ted Kremenek90960972008-01-30 23:03:39 +0000695
Zhongxing Xu097fc982008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000708
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000710
Ted Kremenekd4676512008-03-12 21:45:47 +0000711 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000712 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000713
714 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000715 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000716 else
717 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000718
719 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000720
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000721 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000722 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000723
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000724 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000725 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000726 else
727 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000728}
729
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000730/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000731/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000732void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000733
Ted Kremeneke66ba682009-02-13 01:45:31 +0000734 const GRState* state = builder.getState();
735 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000736
737 // Three possibilities:
738 //
739 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000740 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000741 // (3) We have no clue about the label. Dispatch to all targets.
742 //
743
744 typedef IndirectGotoNodeBuilder::iterator iterator;
745
Zhongxing Xu097fc982008-10-17 05:57:07 +0000746 if (isa<loc::GotoLabel>(V)) {
747 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000748
749 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000750 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000751 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000752 return;
753 }
754 }
755
756 assert (false && "No block with label.");
757 return;
758 }
759
Zhongxing Xu097fc982008-10-17 05:57:07 +0000760 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000761 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000762 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000763 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000764 return;
765 }
766
767 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekcdc3a3c2009-04-23 17:49:43 +0000768 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000769
770 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000771 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000772}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000773
Ted Kremenekca5f6202008-04-15 23:06:53 +0000774
775void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
776 NodeTy* Pred, NodeSet& Dst) {
777
778 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
779
Ted Kremeneke66ba682009-02-13 01:45:31 +0000780 const GRState* state = GetState(Pred);
781 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000782
783 assert (X.isUndef());
784
785 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
786
787 assert (SE);
788
Ted Kremeneke66ba682009-02-13 01:45:31 +0000789 X = GetBlkExprSVal(state, SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000790
791 // Make sure that we invalidate the previous binding.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000792 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000793}
794
Ted Kremenekaee121c2008-02-13 23:08:21 +0000795/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
796/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000797void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
798 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000799 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000800 Expr* CondE = builder.getCondition();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000801 SVal CondV = GetSVal(state, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000802
Ted Kremenekb31af242008-02-28 09:25:22 +0000803 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000804 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000805 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000806 return;
807 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000808
Ted Kremeneke66ba682009-02-13 01:45:31 +0000809 const GRState* DefaultSt = state;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000810 bool DefaultFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000811
Ted Kremenek07baa252008-02-21 18:02:17 +0000812 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000813 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000814
815 // Evaluate the LHS of the case value.
816 Expr::EvalResult V1;
817 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000818
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000819 // Sanity checks. These go away in Release builds.
820 assert(b && V1.Val.isInt() && !V1.HasSideEffects
821 && "Case condition must evaluate to an integer constant.");
822 b = b; // silence unused variable warning
823 assert(V1.Val.getInt().getBitWidth() ==
824 getContext().getTypeSize(CondE->getType()));
825
Ted Kremenekaee121c2008-02-13 23:08:21 +0000826 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000827 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000828
829 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000830 b = E->Evaluate(V2, getContext());
831 assert(b && V2.Val.isInt() && !V2.HasSideEffects
832 && "Case condition must evaluate to an integer constant.");
833 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000834 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000835 else
836 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000837
838 // FIXME: Eventually we should replace the logic below with a range
839 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000840 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000841
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000842 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000843 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000844 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremenek74556a12009-03-26 03:35:11 +0000845 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000846
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000847 // Now "assume" that the case matches.
Ted Kremenekd4676512008-03-12 21:45:47 +0000848 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000849 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000850
851 if (isFeasible) {
852 builder.generateCaseStmtNode(I, StNew);
853
854 // If CondV evaluates to a constant, then we know that this
855 // is the *only* case that we can take, so stop evaluating the
856 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000857 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000858 return;
859 }
860
861 // Now "assume" that the case doesn't match. Add this state
862 // to the default state (if it is feasible).
863
Ted Kremenekd4676512008-03-12 21:45:47 +0000864 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000865 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000866
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000867 if (isFeasible) {
868 DefaultFeasible = true;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000869 DefaultSt = StNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000870 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000871
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000872 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000873 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000874 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000875
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000876 ++V1.Val.getInt();
877 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000878
879 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000880 }
881
882 // If we reach here, than we know that the default branch is
883 // possible.
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000884 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000885}
886
Ted Kremenekca5f6202008-04-15 23:06:53 +0000887//===----------------------------------------------------------------------===//
888// Transfer functions: logical operations ('&&', '||').
889//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000890
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000891void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000892 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000893
Ted Kremenek99ecce72008-02-26 19:05:15 +0000894 assert (B->getOpcode() == BinaryOperator::LAnd ||
895 B->getOpcode() == BinaryOperator::LOr);
896
897 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
898
Ted Kremeneke66ba682009-02-13 01:45:31 +0000899 const GRState* state = GetState(Pred);
900 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000901
Ted Kremenekb31af242008-02-28 09:25:22 +0000902 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000903
Ted Kremenekb31af242008-02-28 09:25:22 +0000904 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000905
906 assert (Ex);
907
908 if (Ex == B->getRHS()) {
909
Ted Kremeneke66ba682009-02-13 01:45:31 +0000910 X = GetBlkExprSVal(state, Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000911
Ted Kremenekb31af242008-02-28 09:25:22 +0000912 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000913
Ted Kremenekb31af242008-02-28 09:25:22 +0000914 if (X.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000915 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000916 return;
917 }
918
Ted Kremenek99ecce72008-02-26 19:05:15 +0000919 // We took the RHS. Because the value of the '&&' or '||' expression must
920 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
921 // or 1. Alternatively, we could take a lazy approach, and calculate this
922 // value later when necessary. We don't have the machinery in place for
923 // this right now, and since most logical expressions are used for branches,
924 // the payoff is not likely to be large. Instead, we do eager evaluation.
925
926 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000927 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000928
929 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000930 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000931 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000932
933 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000934 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000935
936 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000937 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000938 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000939 }
940 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000941 // We took the LHS expression. Depending on whether we are '&&' or
942 // '||' we know what the value of the expression is via properties of
943 // the short-circuiting.
944
945 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000946 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000947 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000948}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000949
Ted Kremenekca5f6202008-04-15 23:06:53 +0000950//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000951// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000952//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000953
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000954void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
955 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000956
Ted Kremeneke66ba682009-02-13 01:45:31 +0000957 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000958
Douglas Gregord2baafd2008-10-21 16:13:35 +0000959 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000960
961 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
962
Ted Kremeneke66ba682009-02-13 01:45:31 +0000963 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000964
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000965 if (asLValue)
Ted Kremenek0441f112009-05-07 18:27:16 +0000966 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
967 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000968 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000969 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000970 return;
971
972 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
973 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
974
975 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000976 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneke66ba682009-02-13 01:45:31 +0000977 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000978 return;
979
980 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000981 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000982 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek0441f112009-05-07 18:27:16 +0000983 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
984 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000985 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000986 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000987
988 assert (false &&
989 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000990}
991
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000992/// VisitArraySubscriptExpr - Transfer function for array accesses
993void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000994 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000995
996 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000997 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000998 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000999
1000 if (Base->getType()->isVectorType()) {
1001 // For vector types get its lvalue.
1002 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1003 // In fact, I think this is just a hack. We need to get the right
1004 // semantics.
1005 VisitLValue(Base, Pred, Tmp);
1006 }
1007 else
1008 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001009
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001010 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +00001011 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001012 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +00001013
1014 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001015 const GRState* state = GetState(*I2);
Ted Kremenekaf81ece2009-05-04 06:18:28 +00001016 SVal V = StateMgr.GetLValue(state, A->getType(),
1017 GetSVal(state, Base),
Ted Kremeneke66ba682009-02-13 01:45:31 +00001018 GetSVal(state, Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +00001019
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001020 if (asLValue)
Ted Kremenek0441f112009-05-07 18:27:16 +00001021 MakeNode(Dst, A, *I2, BindExpr(state, A, V),
1022 ProgramPoint::PostLValueKind);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001023 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001024 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001025 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001026 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +00001027}
1028
Ted Kremenekd0d86202008-04-21 23:43:38 +00001029/// VisitMemberExpr - Transfer function for member expressions.
1030void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001031 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +00001032
1033 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +00001034 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +00001035
1036 if (M->isArrow())
1037 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1038 else
1039 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1040
Douglas Gregor82d44772008-12-20 23:49:58 +00001041 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1042 if (!Field) // FIXME: skipping member expressions for non-fields
1043 return;
1044
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001045 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001046 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001047 // FIXME: Should we insert some assumption logic in here to determine
1048 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001049 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneke66ba682009-02-13 01:45:31 +00001050 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001051
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001052 if (asLValue)
Ted Kremenek0441f112009-05-07 18:27:16 +00001053 MakeNode(Dst, M, *I, BindExpr(state, M, L),
1054 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001055 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001056 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001057 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001058}
1059
Ted Kremeneke66ba682009-02-13 01:45:31 +00001060/// EvalBind - Handle the semantics of binding a value to a specific location.
1061/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1062void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1063 const GRState* state, SVal location, SVal Val) {
1064
Ted Kremeneka42be302009-02-14 01:43:44 +00001065 const GRState* newState = 0;
1066
1067 if (location.isUnknown()) {
1068 // We know that the new state will be the same as the old state since
1069 // the location of the binding is "unknown". Consequently, there
1070 // is no reason to just create a new node.
1071 newState = state;
1072 }
1073 else {
1074 // We are binding to a value other than 'unknown'. Perform the binding
1075 // using the StoreManager.
1076 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1077 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001078
Ted Kremeneka42be302009-02-14 01:43:44 +00001079 // The next thing to do is check if the GRTransferFuncs object wants to
1080 // update the state based on the new binding. If the GRTransferFunc object
1081 // doesn't do anything, just auto-propagate the current state.
1082 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1083 newState != state);
1084
1085 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001086}
1087
1088/// EvalStore - Handle the semantics of a store via an assignment.
1089/// @param Dst The node set to store generated state nodes
1090/// @param Ex The expression representing the location of the store
1091/// @param state The current simulation state
1092/// @param location The location to store the value
1093/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001094void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001095 const GRState* state, SVal location, SVal Val,
1096 const void *tag) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001097
1098 assert (Builder && "GRStmtNodeBuilder must be defined.");
1099
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001100 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001101 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001102
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001103 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001104 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001105
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001106 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001107 state = GetState(Pred);
1108
1109 // Proceed with the store.
1110 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001111 SaveAndRestore<const void*> OldTag(Builder->Tag);
1112 Builder->PointKind = ProgramPoint::PostStoreKind;
1113 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001114 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001115}
1116
1117void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001118 const GRState* state, SVal location,
1119 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001120
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001121 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001122 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001123
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001124 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001125 return;
1126
Ted Kremeneke66ba682009-02-13 01:45:31 +00001127 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001128
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001129 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001130 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001131
1132 // FIXME: Currently symbolic analysis "generates" new symbols
1133 // for the contents of values. We need a better approach.
1134
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001135 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001136 // This is important. We must nuke the old binding.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001137 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001138 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001139 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001140 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001141 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001142 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001143}
1144
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001145void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001146 const GRState* state, SVal location, SVal Val,
1147 const void *tag) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001148
1149 NodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001150 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001151
1152 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001153 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001154}
1155
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001156GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001157 const GRState* state,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001158 SVal location,
1159 const void *tag) {
1160
1161 SaveAndRestore<const void*> OldTag(Builder->Tag);
1162 Builder->Tag = tag;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001163
1164 // Check for loads/stores from/to undefined values.
1165 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001166 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001167 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001168 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001169
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001170 if (N) {
1171 N->markAsSink();
1172 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001173 }
1174
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001175 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001176 }
1177
1178 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1179 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001180 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001181
1182 // During a load, one of two possible situations arise:
1183 // (1) A crash, because the location (pointer) was NULL.
1184 // (2) The location (pointer) is not NULL, and the dereference works.
1185 //
1186 // We add these assumptions.
1187
Zhongxing Xu097fc982008-10-17 05:57:07 +00001188 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001189
1190 // "Assume" that the pointer is not NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001191 bool isFeasibleNotNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001192 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001193
1194 // "Assume" that the pointer is NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001195 bool isFeasibleNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001196 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001197 getStateManager());
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001198
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001199 if (isFeasibleNull) {
1200
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001201 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001202 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001203 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1204
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001205 // We don't use "MakeNode" here because the node will be a sink
1206 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001207 NodeTy* NullNode =
1208 Builder->generateNode(Ex, StNull, Pred,
1209 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001210
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001211 if (NullNode) {
1212
1213 NullNode->markAsSink();
1214
1215 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1216 else ExplicitNullDeref.insert(NullNode);
1217 }
1218 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001219
1220 if (!isFeasibleNotNull)
1221 return 0;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001222
1223 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001224 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001225 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1226 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1227 // Get the index of the accessed element.
1228 SVal Idx = ER->getIndex();
1229 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001230 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1231 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001232
1233 bool isFeasibleInBound = false;
1234 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1235 true, isFeasibleInBound);
1236
1237 bool isFeasibleOutBound = false;
1238 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1239 false, isFeasibleOutBound);
1240
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001241 if (isFeasibleOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001242 // Report warning. Make sink node manually.
1243 NodeTy* OOBNode =
1244 Builder->generateNode(Ex, StOutBound, Pred,
1245 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001246
1247 if (OOBNode) {
1248 OOBNode->markAsSink();
1249
1250 if (isFeasibleInBound)
1251 ImplicitOOBMemAccesses.insert(OOBNode);
1252 else
1253 ExplicitOOBMemAccesses.insert(OOBNode);
1254 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001255 }
1256
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001257 if (!isFeasibleInBound)
1258 return 0;
1259
1260 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001261 }
1262 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001263
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001264 // Generate a new node indicating the checks succeed.
1265 return Builder->generateNode(Ex, StNotNull, Pred,
1266 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001267}
1268
Ted Kremenekca5f6202008-04-15 23:06:53 +00001269//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001270// Transfer function: OSAtomics.
1271//
1272// FIXME: Eventually refactor into a more "plugin" infrastructure.
1273//===----------------------------------------------------------------------===//
1274
1275// Mac OS X:
1276// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1277// atomic.3.html
1278//
1279static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1280 GRExprEngine& Engine,
1281 GRStmtNodeBuilder<GRState>& Builder,
1282 CallExpr* CE, SVal L,
1283 ExplodedNode<GRState>* Pred) {
1284
1285 // Not enough arguments to match OSAtomicCompareAndSwap?
1286 if (CE->getNumArgs() != 3)
1287 return false;
1288
1289 ASTContext &C = Engine.getContext();
1290 Expr *oldValueExpr = CE->getArg(0);
1291 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1292
1293 Expr *newValueExpr = CE->getArg(1);
1294 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1295
1296 // Do the types of 'oldValue' and 'newValue' match?
1297 if (oldValueType != newValueType)
1298 return false;
1299
1300 Expr *theValueExpr = CE->getArg(2);
1301 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1302
1303 // theValueType not a pointer?
1304 if (!theValueType)
1305 return false;
1306
1307 QualType theValueTypePointee =
1308 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1309
1310 // The pointee must match newValueType and oldValueType.
1311 if (theValueTypePointee != newValueType)
1312 return false;
1313
1314 static unsigned magic_load = 0;
1315 static unsigned magic_store = 0;
1316
1317 const void *OSAtomicLoadTag = &magic_load;
1318 const void *OSAtomicStoreTag = &magic_store;
1319
1320 // Load 'theValue'.
1321 GRStateManager &StateMgr = Engine.getStateManager();
1322 const GRState *state = Pred->getState();
1323 ExplodedNodeSet<GRState> Tmp;
1324 SVal location = StateMgr.GetSVal(state, theValueExpr);
1325 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1326
1327 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1328 I != E; ++I) {
1329
1330 ExplodedNode<GRState> *N = *I;
1331 const GRState *stateLoad = N->getState();
1332 SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
1333 SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
1334
1335 // Perform the comparison.
Zhongxing Xuc890e332009-05-20 09:00:16 +00001336 SVal Cmp = Engine.EvalBinOp(stateLoad,
1337 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001338 Engine.getContext().IntTy);
1339 bool isFeasible = false;
1340 const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
1341 isFeasible);
1342
1343 // Were they equal?
1344 if (isFeasible) {
1345 // Perform the store.
1346 ExplodedNodeSet<GRState> TmpStore;
1347 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
1348 StateMgr.GetSVal(stateEqual, newValueExpr),
1349 OSAtomicStoreTag);
1350
1351 // Now bind the result of the comparison.
1352 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1353 E2 = TmpStore.end(); I2 != E2; ++I2) {
1354 ExplodedNode<GRState> *predNew = *I2;
1355 const GRState *stateNew = predNew->getState();
1356 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1357 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1358 }
1359 }
1360
1361 // Were they not equal?
1362 isFeasible = false;
1363 const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
1364 isFeasible);
1365
1366 if (isFeasible) {
1367 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1368 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1369 }
1370 }
1371
1372 return true;
1373}
1374
1375static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1376 GRExprEngine& Engine,
1377 GRStmtNodeBuilder<GRState>& Builder,
1378 CallExpr* CE, SVal L,
1379 ExplodedNode<GRState>* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001380 const FunctionDecl* FD = L.getAsFunctionDecl();
1381 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001382 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001383
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001384 const char *FName = FD->getNameAsCString();
1385
1386 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001387 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1388 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001389 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001390
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001391 // FIXME: Other atomics.
1392 return false;
1393}
1394
1395//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001396// Transfer function: Function calls.
1397//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001398
1399void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1400 assert (Builder && "GRStmtNodeBuilder must be defined.");
1401
1402 // FIXME: Allow us to chain together transfer functions.
1403 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1404 return;
1405
1406 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1407}
1408
Ted Kremenekd9268e32008-02-19 01:44:53 +00001409void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001410 CallExpr::arg_iterator AI,
1411 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001412 NodeSet& Dst)
1413{
1414 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001415 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001416 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1417 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001418 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001419
1420 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1421}
1422
1423void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1424 CallExpr::arg_iterator AI,
1425 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001426 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001427 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001428
Ted Kremenek07baa252008-02-21 18:02:17 +00001429 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001430 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001431 // If the call argument is being bound to a reference parameter,
1432 // visit it as an lvalue, not an rvalue.
1433 bool VisitAsLvalue = false;
1434 if (Proto && ParamIdx < Proto->getNumArgs())
1435 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1436
1437 NodeSet DstTmp;
1438 if (VisitAsLvalue)
1439 VisitLValue(*AI, Pred, DstTmp);
1440 else
1441 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001442 ++AI;
1443
Ted Kremenek769f3482008-03-04 22:01:56 +00001444 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001445 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001446
1447 return;
1448 }
1449
1450 // If we reach here we have processed all of the arguments. Evaluate
1451 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001452
Ted Kremenekc71901d2008-02-25 21:16:03 +00001453 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001454 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001455
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001456 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001457
Ted Kremenekd9268e32008-02-19 01:44:53 +00001458 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001459 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1460
Ted Kremeneke66ba682009-02-13 01:45:31 +00001461 const GRState* state = GetState(*DI);
1462 SVal L = GetSVal(state, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001463
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001464 // FIXME: Add support for symbolic function calls (calls involving
1465 // function pointer values that are symbolic).
1466
1467 // Check for undefined control-flow or calls to NULL.
1468
Zhongxing Xu097fc982008-10-17 05:57:07 +00001469 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001470 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001471
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001472 if (N) {
1473 N->markAsSink();
1474 BadCalls.insert(N);
1475 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001476
Ted Kremenekd9268e32008-02-19 01:44:53 +00001477 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001478 }
1479
1480 // Check for the "noreturn" attribute.
1481
1482 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001483 const FunctionDecl* FD = L.getAsFunctionDecl();
1484 if (FD) {
Ted Kremenek1ce91f22009-04-10 00:01:14 +00001485 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +00001486 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001487 else {
1488 // HACK: Some functions are not marked noreturn, and don't return.
1489 // Here are a few hardwired ones. If this takes too long, we can
1490 // potentially cache these results.
1491 const char* s = FD->getIdentifier()->getName();
1492 unsigned n = strlen(s);
1493
1494 switch (n) {
1495 default:
1496 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001497
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001498 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001499 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1500 break;
1501
1502 case 5:
1503 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001504 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001505 if (CE->getNumArgs() > 0) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001506 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001507 // FIXME: use Assume to inspect the possible symbolic value of
1508 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001509 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001510 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001511 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001512 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001513 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001514 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001515
Ted Kremenek23271be2008-04-22 05:37:33 +00001516 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001517 if (!memcmp(s, "Assert", 6)) {
1518 Builder->BuildSinks = true;
1519 break;
1520 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001521
1522 // FIXME: This is just a wrapper around throwing an exception.
1523 // Eventually inter-procedural analysis should handle this easily.
1524 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1525
Ted Kremenek23271be2008-04-22 05:37:33 +00001526 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001527
1528 case 7:
1529 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1530 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001531
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001532 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001533 if (!memcmp(s ,"db_error", 8) ||
1534 !memcmp(s, "__assert", 8))
1535 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001536 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001537
1538 case 12:
1539 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1540 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001541
Ted Kremenek19903a22008-09-19 02:30:47 +00001542 case 13:
1543 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1544 break;
1545
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001546 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001547 if (!memcmp(s, "dtrace_assfail", 14) ||
1548 !memcmp(s, "yy_fatal_error", 14))
1549 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001550 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001551
1552 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001553 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001554 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1555 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001556 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001557
Ted Kremeneka46fea72008-05-17 00:33:23 +00001558 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001559 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001560
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001561 }
1562 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001563
1564 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001565
Zhongxing Xucac107a2009-04-20 05:24:46 +00001566 if (FD) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001567
Zhongxing Xucac107a2009-04-20 05:24:46 +00001568 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001569 switch (id) {
1570 case Builtin::BI__builtin_expect: {
1571 // For __builtin_expect, just return the value of the subexpression.
1572 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001573 SVal X = GetSVal(state, *(CE->arg_begin()));
1574 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001575 continue;
1576 }
1577
Ted Kremenek19891fa2008-11-02 00:35:01 +00001578 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001579 // FIXME: Refactor into StoreManager itself?
1580 MemRegionManager& RM = getStateManager().getRegionManager();
1581 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001582 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001583
1584 // Set the extent of the region in bytes. This enables us to use the
1585 // SVal of the argument directly. If we save the extent in bits, we
1586 // cannot represent values like symbol*8.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001587 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1588 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001589
Ted Kremeneke66ba682009-02-13 01:45:31 +00001590 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001591 continue;
1592 }
1593
Ted Kremenek21581c62008-03-05 22:59:42 +00001594 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001595 break;
1596 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001597 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001598
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001599 // Check any arguments passed-by-value against being undefined.
1600
1601 bool badArg = false;
1602
1603 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1604 I != E; ++I) {
1605
Zhongxing Xu097fc982008-10-17 05:57:07 +00001606 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001607 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001608
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001609 if (N) {
1610 N->markAsSink();
1611 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001612 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001613
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001614 badArg = true;
1615 break;
1616 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001617 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001618
1619 if (badArg)
1620 continue;
1621
1622 // Dispatch to the plug-in transfer function.
1623
1624 unsigned size = Dst.size();
1625 SaveOr OldHasGen(Builder->HasGeneratedNode);
1626 EvalCall(Dst, CE, L, *DI);
1627
1628 // Handle the case where no nodes where generated. Auto-generate that
1629 // contains the updated state if we aren't generating sinks.
1630
1631 if (!Builder->BuildSinks && Dst.size() == size &&
1632 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001633 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001634 }
1635}
1636
Ted Kremenekca5f6202008-04-15 23:06:53 +00001637//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001638// Transfer function: Objective-C ivar references.
1639//===----------------------------------------------------------------------===//
1640
Ted Kremenek9a48d862009-02-28 20:50:43 +00001641static std::pair<const void*,const void*> EagerlyAssumeTag
1642 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1643
Ted Kremenek34a611b2009-02-25 23:32:10 +00001644void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001645 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1646 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001647
1648 // Test if the previous node was as the same expression. This can happen
1649 // when the expression fails to evaluate to anything meaningful and
1650 // (as an optimization) we don't generate a node.
1651 ProgramPoint P = Pred->getLocation();
1652 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1653 Dst.Add(Pred);
1654 continue;
1655 }
1656
Ted Kremenek8f520972009-02-25 22:32:02 +00001657 const GRState* state = Pred->getState();
Ted Kremenek34a611b2009-02-25 23:32:10 +00001658 SVal V = GetSVal(state, Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001659 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001660 // First assume that the condition is true.
1661 bool isFeasible = false;
1662 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1663 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001664 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1665 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001666 stateTrue, Pred));
1667 }
1668
1669 // Next, assume that the condition is false.
1670 isFeasible = false;
1671 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1672 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001673 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1674 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001675 stateFalse, Pred));
1676 }
1677 }
1678 else
1679 Dst.Add(Pred);
1680 }
1681}
1682
1683//===----------------------------------------------------------------------===//
1684// Transfer function: Objective-C ivar references.
1685//===----------------------------------------------------------------------===//
1686
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001687void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1688 NodeTy* Pred, NodeSet& Dst,
1689 bool asLValue) {
1690
1691 Expr* Base = cast<Expr>(Ex->getBase());
1692 NodeSet Tmp;
1693 Visit(Base, Pred, Tmp);
1694
1695 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001696 const GRState* state = GetState(*I);
1697 SVal BaseVal = GetSVal(state, Base);
1698 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001699
1700 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001701 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001702 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001703 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001704 }
1705}
1706
1707//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001708// Transfer function: Objective-C fast enumeration 'for' statements.
1709//===----------------------------------------------------------------------===//
1710
1711void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1712 NodeTy* Pred, NodeSet& Dst) {
1713
1714 // ObjCForCollectionStmts are processed in two places. This method
1715 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1716 // statements within a basic block. This transfer function does two things:
1717 //
1718 // (1) binds the next container value to 'element'. This creates a new
1719 // node in the ExplodedGraph.
1720 //
1721 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1722 // whether or not the container has any more elements. This value
1723 // will be tested in ProcessBranch. We need to explicitly bind
1724 // this value because a container can contain nil elements.
1725 //
1726 // FIXME: Eventually this logic should actually do dispatches to
1727 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1728 // This will require simulating a temporary NSFastEnumerationState, either
1729 // through an SVal or through the use of MemRegions. This value can
1730 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1731 // terminates we reclaim the temporary (it goes out of scope) and we
1732 // we can test if the SVal is 0 or if the MemRegion is null (depending
1733 // on what approach we take).
1734 //
1735 // For now: simulate (1) by assigning either a symbol or nil if the
1736 // container is empty. Thus this transfer function will by default
1737 // result in state splitting.
1738
Ted Kremenek034a9472008-11-14 19:47:18 +00001739 Stmt* elem = S->getElement();
1740 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001741
1742 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001743 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001744 assert (ElemD->getInit() == 0);
Ted Kremenek034a9472008-11-14 19:47:18 +00001745 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1746 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1747 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001748 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001749
1750 NodeSet Tmp;
1751 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001752
Ted Kremenek034a9472008-11-14 19:47:18 +00001753 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1754 const GRState* state = GetState(*I);
1755 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1756 }
1757}
1758
1759void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1760 NodeTy* Pred, NodeSet& Dst,
1761 SVal ElementV) {
1762
1763
Ted Kremenek13e167f2008-11-12 19:24:17 +00001764
Ted Kremenek034a9472008-11-14 19:47:18 +00001765 // Get the current state. Use 'EvalLocation' to determine if it is a null
1766 // pointer, etc.
1767 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001768
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001769 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1770 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001771 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001772
1773 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek034a9472008-11-14 19:47:18 +00001774
Ted Kremenek13e167f2008-11-12 19:24:17 +00001775 // Handle the case where the container still has elements.
Ted Kremenek034a9472008-11-14 19:47:18 +00001776 QualType IntTy = getContext().IntTy;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001777 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1778 GRStateRef hasElems = state.BindExpr(S, TrueV);
1779
Ted Kremenek13e167f2008-11-12 19:24:17 +00001780 // Handle the case where the container has no elements.
Ted Kremenekd3789d72008-11-12 21:12:46 +00001781 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1782 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001783
1784 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1785 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1786 // FIXME: The proper thing to do is to really iterate over the
1787 // container. We will do this with dispatch logic to the store.
1788 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu20362702009-05-09 03:57:34 +00001789 QualType T = R->getValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001790 assert (Loc::IsLocType(T));
1791 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001792 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1793 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1794 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001795
Ted Kremenek034a9472008-11-14 19:47:18 +00001796 // Bind the location to 'nil' on the false branch.
1797 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1798 noElems = noElems.BindLoc(ElementV, nilV);
1799 }
1800
Ted Kremenekd3789d72008-11-12 21:12:46 +00001801 // Create the new nodes.
1802 MakeNode(Dst, S, Pred, hasElems);
1803 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001804}
1805
1806//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001807// Transfer function: Objective-C message expressions.
1808//===----------------------------------------------------------------------===//
1809
1810void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1811 NodeSet& Dst){
1812
1813 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1814 Pred, Dst);
1815}
1816
1817void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001818 ObjCMessageExpr::arg_iterator AI,
1819 ObjCMessageExpr::arg_iterator AE,
1820 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001821 if (AI == AE) {
1822
1823 // Process the receiver.
1824
1825 if (Expr* Receiver = ME->getReceiver()) {
1826 NodeSet Tmp;
1827 Visit(Receiver, Pred, Tmp);
1828
1829 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1830 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1831
1832 return;
1833 }
1834
1835 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1836 return;
1837 }
1838
1839 NodeSet Tmp;
1840 Visit(*AI, Pred, Tmp);
1841
1842 ++AI;
1843
1844 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1845 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1846}
1847
1848void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1849 NodeTy* Pred,
1850 NodeSet& Dst) {
1851
1852 // FIXME: More logic for the processing the method call.
1853
Ted Kremeneke66ba682009-02-13 01:45:31 +00001854 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001855 bool RaisesException = false;
1856
Ted Kremenekca5f6202008-04-15 23:06:53 +00001857
1858 if (Expr* Receiver = ME->getReceiver()) {
1859
Ted Kremeneke66ba682009-02-13 01:45:31 +00001860 SVal L = GetSVal(state, Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001861
Ted Kremenek95a98252009-02-19 04:06:22 +00001862 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001863 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001864 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001865
1866 if (N) {
1867 N->markAsSink();
1868 UndefReceivers.insert(N);
1869 }
1870
1871 return;
1872 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001873
Ted Kremenek95a98252009-02-19 04:06:22 +00001874 // "Assume" that the receiver is not NULL.
1875 bool isFeasibleNotNull = false;
Ted Kremenekf2895872009-04-08 18:51:08 +00001876 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek95a98252009-02-19 04:06:22 +00001877
1878 // "Assume" that the receiver is NULL.
1879 bool isFeasibleNull = false;
1880 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1881
1882 if (isFeasibleNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001883 QualType RetTy = ME->getType();
1884
Ted Kremenek95a98252009-02-19 04:06:22 +00001885 // Check if the receiver was nil and the return value a struct.
Ted Kremenekb3323002009-04-09 05:45:56 +00001886 if(RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001887 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001888 // The [0 ...] expressions will return garbage. Flag either an
1889 // explicit or implicit error. Because of the structure of this
1890 // function we currently do not bifurfacte the state graph at
1891 // this point.
1892 // FIXME: We should bifurcate and fill the returned struct with
1893 // garbage.
1894 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1895 N->markAsSink();
1896 if (isFeasibleNotNull)
1897 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001898 else
Ted Kremenek23712182009-04-09 04:06:51 +00001899 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001900 }
1901 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001902 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001903 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001904 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001905 if (RetTy != Ctx.VoidTy) {
1906 if (BR.getParentMap().isConsumedExpr(ME)) {
1907 // sizeof(void *)
1908 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1909 // sizeof(return type)
1910 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001911
Ted Kremenekb3323002009-04-09 05:45:56 +00001912 if(voidPtrSize < returnTypeSize) {
1913 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1914 N->markAsSink();
1915 if(isFeasibleNotNull)
1916 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001917 else
Ted Kremenekb3323002009-04-09 05:45:56 +00001918 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001919 }
1920 }
1921 else if (!isFeasibleNotNull) {
1922 // Handle the safe cases where the return value is 0 if the
1923 // receiver is nil.
1924 //
1925 // FIXME: For now take the conservative approach that we only
1926 // return null values if we *know* that the receiver is nil.
1927 // This is because we can have surprises like:
1928 //
1929 // ... = [[NSScreens screens] objectAtIndex:0];
1930 //
1931 // What can happen is that [... screens] could return nil, but
1932 // it most likely isn't nil. We should assume the semantics
1933 // of this case unless we have *a lot* more knowledge.
1934 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001935 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekb3323002009-04-09 05:45:56 +00001936 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001937 return;
1938 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001939 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001940 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001941 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001942 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001943 // of this method should assume that the receiver is not nil.
1944 if (!StNotNull)
1945 return;
1946
Ted Kremenekf2895872009-04-08 18:51:08 +00001947 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001948 }
1949
Ted Kremenek5f20a632008-05-01 18:33:28 +00001950 // Check if the "raise" message was sent.
1951 if (ME->getSelector() == RaiseSel)
1952 RaisesException = true;
1953 }
1954 else {
1955
1956 IdentifierInfo* ClsName = ME->getClassName();
1957 Selector S = ME->getSelector();
1958
1959 // Check for special instance methods.
1960
1961 if (!NSExceptionII) {
1962 ASTContext& Ctx = getContext();
1963
1964 NSExceptionII = &Ctx.Idents.get("NSException");
1965 }
1966
1967 if (ClsName == NSExceptionII) {
1968
1969 enum { NUM_RAISE_SELECTORS = 2 };
1970
1971 // Lazily create a cache of the selectors.
1972
1973 if (!NSExceptionInstanceRaiseSelectors) {
1974
1975 ASTContext& Ctx = getContext();
1976
1977 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1978
1979 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1980 unsigned idx = 0;
1981
1982 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001983 II.push_back(&Ctx.Idents.get("raise"));
1984 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001985 NSExceptionInstanceRaiseSelectors[idx++] =
1986 Ctx.Selectors.getSelector(II.size(), &II[0]);
1987
1988 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001989 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001990 NSExceptionInstanceRaiseSelectors[idx++] =
1991 Ctx.Selectors.getSelector(II.size(), &II[0]);
1992 }
1993
1994 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1995 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1996 RaisesException = true; break;
1997 }
1998 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001999 }
2000
2001 // Check for any arguments that are uninitialized/undefined.
2002
2003 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2004 I != E; ++I) {
2005
Ted Kremeneke66ba682009-02-13 01:45:31 +00002006 if (GetSVal(state, *I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00002007
2008 // Generate an error node for passing an uninitialized/undefined value
2009 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002010 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002011
2012 if (N) {
2013 N->markAsSink();
2014 MsgExprUndefArgs[N] = *I;
2015 }
2016
2017 return;
2018 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00002019 }
2020
2021 // Check if we raise an exception. For now treat these as sinks. Eventually
2022 // we will want to handle exceptions properly.
2023
2024 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2025
2026 if (RaisesException)
2027 Builder->BuildSinks = true;
2028
Ted Kremenekca5f6202008-04-15 23:06:53 +00002029 // Dispatch to plug-in transfer function.
2030
2031 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002032 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002033
Ted Kremenekca5f6202008-04-15 23:06:53 +00002034 EvalObjCMessageExpr(Dst, ME, Pred);
2035
2036 // Handle the case where no nodes where generated. Auto-generate that
2037 // contains the updated state if we aren't generating sinks.
2038
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002039 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002040 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002041}
2042
2043//===----------------------------------------------------------------------===//
2044// Transfer functions: Miscellaneous statements.
2045//===----------------------------------------------------------------------===//
2046
Ted Kremenek16354a42009-01-13 01:04:21 +00002047void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2048 QualType PtrTy,
2049 Expr* CastE, NodeTy* Pred,
2050 NodeSet& Dst) {
2051 if (!V.isUnknownOrUndef()) {
2052 // FIXME: Determine if the number of bits of the target type is
2053 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00002054 // If not, flag an error.
Ted Kremenek52978eb2009-03-05 03:44:53 +00002055 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2056 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00002057 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002058 else
2059 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002060}
2061
2062
Ted Kremenek07baa252008-02-21 18:02:17 +00002063void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00002064 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002065 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002066 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002067
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002068 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002069 T = ExCast->getTypeAsWritten();
2070
Zhongxing Xu943909c2008-10-22 08:02:16 +00002071 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002072 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002073 else
2074 Visit(Ex, Pred, S1);
2075
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002076 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00002077 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002078 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002079 Dst.Add(*I1);
2080
Ted Kremenek54eddae2008-01-24 02:02:54 +00002081 return;
2082 }
2083
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002084 // FIXME: The rest of this should probably just go into EvalCall, and
2085 // let the transfer function object be responsible for constructing
2086 // nodes.
2087
Ted Kremenek07baa252008-02-21 18:02:17 +00002088 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00002089 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002090 const GRState* state = GetState(N);
2091 SVal V = GetSVal(state, Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002092 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002093
2094 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002095 if (V.isUnknown()) {
2096 Dst.Add(N);
2097 continue;
2098 }
2099
2100 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002101 if (V.isUndef())
2102 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00002103
2104 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00002105 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002106 C.getCanonicalType(ExTy).getUnqualifiedType())
2107 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002108
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002109 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002110 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002111 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002112 continue;
2113 }
2114
2115 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002116 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002117 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002118 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002119 V = LV->getLoc();
Ted Kremeneke66ba682009-02-13 01:45:31 +00002120 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002121 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002122 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002123
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002124 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002125 }
2126
2127 // Just pass through function and block pointers.
2128 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2129 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002130 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002131 }
2132
Ted Kremenek16354a42009-01-13 01:04:21 +00002133 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002134 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00002135 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00002136 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002137
2138 // Are we casting from an array to a pointer? If so just pass on
2139 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002140 if (T->isPointerType())
2141 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00002142
2143 // Are we casting from an array to an integer? If so, cast the decayed
2144 // pointer value to an integer.
2145 assert(T->isIntegerType());
2146 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2147 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002148 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002149 continue;
2150 }
2151
Ted Kremenekf5da3252008-12-13 21:49:13 +00002152 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00002153 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2154 // FIXME: For TypedViewRegions, we should handle the case where the
2155 // underlying symbolic pointer is a function pointer or
2156 // block pointer.
2157
2158 // FIXME: We should handle the case where we strip off view layers to get
2159 // to a desugared type.
2160
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002161 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00002162 // We get a symbolic function pointer for a dereference of a function
2163 // pointer, but it is of function type. Example:
2164
2165 // struct FPRec {
2166 // void (*my_func)(int * x);
2167 // };
2168 //
2169 // int bar(int x);
2170 //
2171 // int f1_a(struct FPRec* foo) {
2172 // int x;
2173 // (*foo->my_func)(&x);
2174 // return bar(x)+1; // no-warning
2175 // }
2176
2177 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002178
Ted Kremenekf5da3252008-12-13 21:49:13 +00002179 const MemRegion* R = RV->getRegion();
2180 StoreManager& StoreMgr = getStoreManager();
2181
2182 // Delegate to store manager to get the result of casting a region
2183 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002184 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00002185
2186 // Inspect the result. If the MemRegion* returned is NULL, this
2187 // expression evaluates to UnknownVal.
2188 R = Res.getRegion();
2189 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2190
2191 // Generate the new node in the ExplodedGraph.
2192 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00002193 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002194 }
Zhongxing Xu18bcec02009-04-10 06:06:13 +00002195 // All other cases.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002196 DispatchCast: {
2197 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2198 EvalCast(V, CastE->getType())));
2199 continue;
2200 }
2201
2202 PassThrough: {
2203 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2204 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002205 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002206}
2207
Ted Kremenekd83daa52008-10-27 21:54:31 +00002208void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002209 NodeTy* Pred, NodeSet& Dst,
2210 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002211 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2212 NodeSet Tmp;
2213 Visit(ILE, Pred, Tmp);
2214
2215 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002216 const GRState* state = GetState(*I);
2217 SVal ILV = GetSVal(state, ILE);
2218 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002219
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002220 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002221 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002222 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002223 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002224 }
2225}
2226
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002227void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002228
Ted Kremenek811af062008-10-06 18:43:53 +00002229 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002230 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002231
2232 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002233 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002234
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002235 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002236 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002237
2238 // FIXME: static variables may have an initializer, but the second
2239 // time a function is called those values may not be current.
2240 NodeSet Tmp;
2241
Ted Kremenek13e167f2008-11-12 19:24:17 +00002242 if (InitEx)
2243 Visit(InitEx, Pred, Tmp);
Ted Kremenek448ab622008-08-28 18:34:26 +00002244
2245 if (Tmp.empty())
2246 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002247
2248 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002249 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002250 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002251
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002252 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2253 QualType T = getContext().getCanonicalType(VD->getType());
2254 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2255 // FIXME: Handle multi-dimensional VLAs.
2256
2257 Expr* SE = VLA->getSizeExpr();
2258 SVal Size = GetSVal(state, SE);
2259
2260 if (Size.isUndef()) {
2261 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2262 N->markAsSink();
2263 ExplicitBadSizedVLA.insert(N);
2264 }
2265 continue;
2266 }
2267
2268 bool isFeasibleZero = false;
2269 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2270
2271 bool isFeasibleNotZero = false;
2272 state = Assume(state, Size, true, isFeasibleNotZero);
2273
2274 if (isFeasibleZero) {
2275 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2276 N->markAsSink();
2277 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2278 else ExplicitBadSizedVLA.insert(N);
2279 }
2280 }
2281
2282 if (!isFeasibleNotZero)
2283 continue;
2284 }
2285
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002286 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002287 if (InitEx) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002288 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002289 QualType T = VD->getType();
2290
2291 // Recover some path-sensitivity if a scalar value evaluated to
2292 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002293 if (InitVal.isUnknown() ||
2294 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002295 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002296 }
2297
Ted Kremeneke66ba682009-02-13 01:45:31 +00002298 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002299
2300 // The next thing to do is check if the GRTransferFuncs object wants to
2301 // update the state based on the new binding. If the GRTransferFunc
2302 // object doesn't do anything, just auto-propagate the current state.
2303 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2304 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2305 InitVal);
2306 }
2307 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002308 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002309 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002310 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002311 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002312}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002313
Ted Kremeneke56ece22008-10-30 17:47:32 +00002314namespace {
2315 // This class is used by VisitInitListExpr as an item in a worklist
2316 // for processing the values contained in an InitListExpr.
2317class VISIBILITY_HIDDEN InitListWLItem {
2318public:
2319 llvm::ImmutableList<SVal> Vals;
2320 GRExprEngine::NodeTy* N;
2321 InitListExpr::reverse_iterator Itr;
2322
2323 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2324 InitListExpr::reverse_iterator itr)
2325 : Vals(vals), N(n), Itr(itr) {}
2326};
2327}
2328
2329
Zhongxing Xuebcad732008-10-30 05:02:23 +00002330void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2331 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002332
Zhongxing Xuebcad732008-10-30 05:02:23 +00002333 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002334 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002335 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002336
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002337 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002338
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002339 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002340
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002341 // Handle base case where the initializer has no elements.
2342 // e.g: static int* myArray[] = {};
2343 if (NumInitElements == 0) {
2344 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2345 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2346 return;
2347 }
2348
2349 // Create a worklist to process the initializers.
2350 llvm::SmallVector<InitListWLItem, 10> WorkList;
2351 WorkList.reserve(NumInitElements);
2352 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002353 InitListExpr::reverse_iterator ItrEnd = E->rend();
2354
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002355 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002356 while (!WorkList.empty()) {
2357 InitListWLItem X = WorkList.back();
2358 WorkList.pop_back();
2359
Zhongxing Xuebcad732008-10-30 05:02:23 +00002360 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002361 Visit(*X.Itr, X.N, Tmp);
2362
2363 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002364
Ted Kremeneke56ece22008-10-30 17:47:32 +00002365 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2366 // Get the last initializer value.
2367 state = GetState(*NI);
2368 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2369
2370 // Construct the new list of values by prepending the new value to
2371 // the already constructed list.
2372 llvm::ImmutableList<SVal> NewVals =
2373 getBasicVals().consVals(InitV, X.Vals);
2374
2375 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002376 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002377 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuebcad732008-10-30 05:02:23 +00002378
Ted Kremeneke56ece22008-10-30 17:47:32 +00002379 // Make final state and node.
Ted Kremenek78c06532008-10-30 18:37:08 +00002380 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002381 }
2382 else {
2383 // Still some initializer values to go. Push them onto the worklist.
2384 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2385 }
2386 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002387 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002388
2389 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002390 }
2391
Ted Kremenek79413a52008-11-13 06:10:40 +00002392 if (T->isUnionType() || T->isVectorType()) {
2393 // FIXME: to be implemented.
2394 // Note: That vectors can return true for T->isIntegerType()
2395 MakeNode(Dst, E, Pred, state);
2396 return;
2397 }
2398
Zhongxing Xuebcad732008-10-30 05:02:23 +00002399 if (Loc::IsLocType(T) || T->isIntegerType()) {
2400 assert (E->getNumInits() == 1);
2401 NodeSet Tmp;
2402 Expr* Init = E->getInit(0);
2403 Visit(Init, Pred, Tmp);
2404 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2405 state = GetState(*I);
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002406 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002407 }
2408 return;
2409 }
2410
Zhongxing Xuebcad732008-10-30 05:02:23 +00002411
2412 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2413 assert(0 && "unprocessed InitListExpr type");
2414}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002415
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002416/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2417void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2418 NodeTy* Pred,
2419 NodeSet& Dst) {
2420 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002421 uint64_t amt;
2422
2423 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002424 if (T == getContext().VoidTy) {
2425 // sizeof(void) == 1 byte.
2426 amt = 1;
2427 }
2428 else if (!T.getTypePtr()->isConstantSizeType()) {
2429 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002430 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002431 }
2432 else if (T->isObjCInterfaceType()) {
2433 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2434 // the compiler has laid out its representation. Just report Unknown
2435 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002436 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002437 }
2438 else {
2439 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002440 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002441 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002442 }
2443 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002444 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002445
Ted Kremenekf10f2882008-03-21 21:30:14 +00002446 MakeNode(Dst, Ex, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002447 BindExpr(GetState(Pred), Ex,
2448 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002449}
2450
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002451
Ted Kremenek07baa252008-02-21 18:02:17 +00002452void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002453 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002454
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002455 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002456
2457 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002458 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002459
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002460 case UnaryOperator::Deref: {
2461
2462 Expr* Ex = U->getSubExpr()->IgnoreParens();
2463 NodeSet Tmp;
2464 Visit(Ex, Pred, Tmp);
2465
2466 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002467
Ted Kremeneke66ba682009-02-13 01:45:31 +00002468 const GRState* state = GetState(*I);
2469 SVal location = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002470
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002471 if (asLValue)
Ted Kremenek0441f112009-05-07 18:27:16 +00002472 MakeNode(Dst, U, *I, BindExpr(state, U, location),
2473 ProgramPoint::PostLValueKind);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002474 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002475 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002476 }
2477
2478 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002479 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002480
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002481 case UnaryOperator::Real: {
2482
2483 Expr* Ex = U->getSubExpr()->IgnoreParens();
2484 NodeSet Tmp;
2485 Visit(Ex, Pred, Tmp);
2486
2487 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2488
Zhongxing Xu097fc982008-10-17 05:57:07 +00002489 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002490 if (Ex->getType()->isAnyComplexType()) {
2491 // Just report "Unknown."
2492 Dst.Add(*I);
2493 continue;
2494 }
2495
2496 // For all other types, UnaryOperator::Real is an identity operation.
2497 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002498 const GRState* state = GetState(*I);
2499 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002500 }
2501
2502 return;
2503 }
2504
2505 case UnaryOperator::Imag: {
2506
2507 Expr* Ex = U->getSubExpr()->IgnoreParens();
2508 NodeSet Tmp;
2509 Visit(Ex, Pred, Tmp);
2510
2511 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002512 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002513 if (Ex->getType()->isAnyComplexType()) {
2514 // Just report "Unknown."
2515 Dst.Add(*I);
2516 continue;
2517 }
2518
2519 // For all other types, UnaryOperator::Float returns 0.
2520 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002521 const GRState* state = GetState(*I);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002522 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002523 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002524 }
2525
2526 return;
2527 }
2528
2529 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002530 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002531 Dst.Add(Pred);
2532 return;
2533
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002534 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002535 case UnaryOperator::Extension: {
2536
2537 // Unary "+" is a no-op, similar to a parentheses. We still have places
2538 // where it may be a block-level expression, so we need to
2539 // generate an extra node that just propagates the value of the
2540 // subexpression.
2541
2542 Expr* Ex = U->getSubExpr()->IgnoreParens();
2543 NodeSet Tmp;
2544 Visit(Ex, Pred, Tmp);
2545
2546 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002547 const GRState* state = GetState(*I);
2548 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002549 }
2550
2551 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002552 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002553
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002554 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002555
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002556 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002557 Expr* Ex = U->getSubExpr()->IgnoreParens();
2558 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002559 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002560
2561 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002562 const GRState* state = GetState(*I);
2563 SVal V = GetSVal(state, Ex);
2564 state = BindExpr(state, U, V);
2565 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002566 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002567
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002568 return;
2569 }
2570
2571 case UnaryOperator::LNot:
2572 case UnaryOperator::Minus:
2573 case UnaryOperator::Not: {
2574
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002575 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002576 Expr* Ex = U->getSubExpr()->IgnoreParens();
2577 NodeSet Tmp;
2578 Visit(Ex, Pred, Tmp);
2579
2580 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002581 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002582
2583 // Get the value of the subexpression.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002584 SVal V = GetSVal(state, Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002585
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002586 if (V.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002587 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002588 continue;
2589 }
2590
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002591// QualType DstT = getContext().getCanonicalType(U->getType());
2592// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2593//
2594// if (DstT != SrcT) // Perform promotions.
2595// V = EvalCast(V, DstT);
2596//
2597// if (V.isUnknownOrUndef()) {
2598// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2599// continue;
2600// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002601
2602 switch (U->getOpcode()) {
2603 default:
2604 assert(false && "Invalid Opcode.");
2605 break;
2606
2607 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002608 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002609 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002610 break;
2611
2612 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002613 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002614 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002615 break;
2616
2617 case UnaryOperator::LNot:
2618
2619 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2620 //
2621 // Note: technically we do "E == 0", but this is the same in the
2622 // transfer functions as "0 == E".
2623
Zhongxing Xu097fc982008-10-17 05:57:07 +00002624 if (isa<Loc>(V)) {
Ted Kremenekf2895872009-04-08 18:51:08 +00002625 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xuc890e332009-05-20 09:00:16 +00002626 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremenek74556a12009-03-26 03:35:11 +00002627 U->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002628 state = BindExpr(state, U, Result);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002629 }
2630 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002631 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002632#if 0
Zhongxing Xu097fc982008-10-17 05:57:07 +00002633 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002634 state = SetSVal(state, U, Result);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002635#else
Ted Kremenek74556a12009-03-26 03:35:11 +00002636 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2637 U->getType());
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002638 continue;
2639#endif
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002640 }
2641
2642 break;
2643 }
2644
Ted Kremeneke66ba682009-02-13 01:45:31 +00002645 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002646 }
2647
2648 return;
2649 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002650 }
2651
2652 // Handle ++ and -- (both pre- and post-increment).
2653
2654 assert (U->isIncrementDecrementOp());
2655 NodeSet Tmp;
2656 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002657 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002658
2659 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2660
Ted Kremeneke66ba682009-02-13 01:45:31 +00002661 const GRState* state = GetState(*I);
2662 SVal V1 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002663
2664 // Perform a load.
2665 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002666 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002667
2668 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2669
Ted Kremeneke66ba682009-02-13 01:45:31 +00002670 state = GetState(*I2);
2671 SVal V2 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002672
2673 // Propagate unknown and undefined values.
2674 if (V2.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002675 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002676 continue;
2677 }
2678
Ted Kremeneke43de222009-03-11 03:54:24 +00002679 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002680 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2681 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002682
Zhongxing Xuc890e332009-05-20 09:00:16 +00002683 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2684 U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002685
2686 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002687 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002688 Result = ValMgr.getConjuredSymbolVal(Ex,
2689 Builder->getCurrentBlockCount());
Ted Kremenek65411632009-04-21 22:38:05 +00002690
2691 // If the value is a location, ++/-- should always preserve
2692 // non-nullness. Check if the original value was non-null, and if so propagate
2693 // that constraint.
2694 if (Loc::IsLocType(U->getType())) {
Zhongxing Xuc890e332009-05-20 09:00:16 +00002695 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenek65411632009-04-21 22:38:05 +00002696 ValMgr.makeZeroVal(U->getType()),
2697 getContext().IntTy);
2698
2699 bool isFeasible = false;
2700 Assume(state, Constraint, true, isFeasible);
2701 if (!isFeasible) {
2702 // It isn't feasible for the original value to be null.
2703 // Propagate this constraint.
Zhongxing Xuc890e332009-05-20 09:00:16 +00002704 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenek65411632009-04-21 22:38:05 +00002705 ValMgr.makeZeroVal(U->getType()),
2706 getContext().IntTy);
2707
2708 bool isFeasible = false;
2709 state = Assume(state, Constraint, false, isFeasible);
2710 assert(isFeasible && state);
2711 }
2712 }
2713 }
Ted Kremenek607415e2009-03-20 20:10:45 +00002714
Ted Kremeneke66ba682009-02-13 01:45:31 +00002715 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002716
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002717 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002718 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002719 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002720 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002721}
2722
Ted Kremenek31803c32008-03-17 21:11:24 +00002723void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2724 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2725}
2726
2727void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2728 AsmStmt::outputs_iterator I,
2729 AsmStmt::outputs_iterator E,
2730 NodeTy* Pred, NodeSet& Dst) {
2731 if (I == E) {
2732 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2733 return;
2734 }
2735
2736 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002737 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002738
2739 ++I;
2740
2741 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2742 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2743}
2744
2745void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2746 AsmStmt::inputs_iterator I,
2747 AsmStmt::inputs_iterator E,
2748 NodeTy* Pred, NodeSet& Dst) {
2749 if (I == E) {
2750
2751 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002752 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002753
2754 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2755 // which interprets the inline asm and stores proper results in the
2756 // outputs.
2757
Ted Kremeneke66ba682009-02-13 01:45:31 +00002758 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002759
2760 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2761 OE = A->end_outputs(); OI != OE; ++OI) {
2762
Ted Kremeneke66ba682009-02-13 01:45:31 +00002763 SVal X = GetSVal(state, *OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002764 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002765
Zhongxing Xu097fc982008-10-17 05:57:07 +00002766 if (isa<Loc>(X))
Ted Kremeneke66ba682009-02-13 01:45:31 +00002767 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002768 }
2769
Ted Kremeneke66ba682009-02-13 01:45:31 +00002770 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002771 return;
2772 }
2773
2774 NodeSet Tmp;
2775 Visit(*I, Pred, Tmp);
2776
2777 ++I;
2778
2779 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2780 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2781}
2782
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002783void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2784 assert (Builder && "GRStmtNodeBuilder must be defined.");
2785
2786 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002787
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002788 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2789 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002790
Ted Kremenekc7469542008-07-17 23:15:45 +00002791 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002792
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002793 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002794
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002795 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002796 MakeNode(Dst, S, Pred, GetState(Pred));
2797}
2798
Ted Kremenek108048c2008-03-31 15:02:58 +00002799void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2800
2801 Expr* R = S->getRetValue();
2802
2803 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002804 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002805 return;
2806 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002807
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002808 NodeSet Tmp;
2809 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002810
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002811 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2812 SVal X = GetSVal((*I)->getState(), R);
2813
2814 // Check if we return the address of a stack variable.
2815 if (isa<loc::MemRegionVal>(X)) {
2816 // Determine if the value is on the stack.
2817 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002818
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002819 if (R && getStateManager().hasStackStorage(R)) {
2820 // Create a special node representing the error.
2821 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2822 N->markAsSink();
2823 RetsStackAddr.insert(N);
2824 }
2825 continue;
2826 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002827 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002828 // Check if we return an undefined value.
2829 else if (X.isUndef()) {
2830 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2831 N->markAsSink();
2832 RetsUndef.insert(N);
2833 }
2834 continue;
2835 }
2836
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002837 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002838 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002839}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002840
Ted Kremenekca5f6202008-04-15 23:06:53 +00002841//===----------------------------------------------------------------------===//
2842// Transfer functions: Binary operators.
2843//===----------------------------------------------------------------------===//
2844
Ted Kremeneke66ba682009-02-13 01:45:31 +00002845const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002846 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002847
2848 // Divide by undefined? (potentially zero)
2849
2850 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002851 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002852
2853 if (DivUndef) {
2854 DivUndef->markAsSink();
2855 ExplicitBadDivides.insert(DivUndef);
2856 }
2857
Ted Kremenek6c438f82008-10-20 23:40:25 +00002858 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002859 }
2860
2861 // Check for divide/remainder-by-zero.
2862 // First, "assume" that the denominator is 0 or undefined.
2863
2864 bool isFeasibleZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002865 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002866
2867 // Second, "assume" that the denominator cannot be 0.
2868
2869 bool isFeasibleNotZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002870 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002871
2872 // Create the node for the divide-by-zero (if it occurred).
2873
2874 if (isFeasibleZero)
2875 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2876 DivZeroNode->markAsSink();
2877
2878 if (isFeasibleNotZero)
2879 ImplicitBadDivides.insert(DivZeroNode);
2880 else
2881 ExplicitBadDivides.insert(DivZeroNode);
2882
2883 }
2884
Ted Kremeneke66ba682009-02-13 01:45:31 +00002885 return isFeasibleNotZero ? state : 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002886}
2887
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002888void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002889 GRExprEngine::NodeTy* Pred,
2890 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002891
2892 NodeSet Tmp1;
2893 Expr* LHS = B->getLHS()->IgnoreParens();
2894 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002895
Ted Kremenek52510d82008-12-06 02:39:30 +00002896 // FIXME: Add proper support for ObjCKVCRefExpr.
2897 if (isa<ObjCKVCRefExpr>(LHS)) {
2898 Visit(RHS, Pred, Dst);
2899 return;
2900 }
2901
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002902 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002903 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002904 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002905 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002906
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002907 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002908
Zhongxing Xu097fc982008-10-17 05:57:07 +00002909 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002910
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002911 // Process the RHS.
2912
2913 NodeSet Tmp2;
2914 Visit(RHS, *I1, Tmp2);
2915
2916 // With both the LHS and RHS evaluated, process the operation itself.
2917
2918 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002919
Ted Kremeneke66ba682009-02-13 01:45:31 +00002920 const GRState* state = GetState(*I2);
2921 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002922
Ted Kremeneke66ba682009-02-13 01:45:31 +00002923 SVal RightV = GetSVal(state, RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002924 BinaryOperator::Opcode Op = B->getOpcode();
2925
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002926 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002927
Ted Kremenekf031b872008-01-23 19:59:44 +00002928 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002929
Ted Kremenekd4676512008-03-12 21:45:47 +00002930 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002931 // FIXME: Handle structs.
2932 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002933
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002934 if ((RightV.isUnknown() ||
2935 !getConstraintManager().canReasonAbout(RightV))
2936 && (Loc::IsLocType(T) ||
2937 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002938 unsigned Count = Builder->getCurrentBlockCount();
2939 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002940 }
2941
Ted Kremenekd4676512008-03-12 21:45:47 +00002942 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002943 // to the L-Value represented by the LHS.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002944 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2945 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002946 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002947 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002948
2949 case BinaryOperator::Div:
2950 case BinaryOperator::Rem:
2951
Ted Kremenek6c438f82008-10-20 23:40:25 +00002952 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002953 if (RHS->getType()->isIntegerType() &&
2954 RHS->getType()->isScalarType()) {
2955
Ted Kremeneke66ba682009-02-13 01:45:31 +00002956 state = CheckDivideZero(B, state, *I2, RightV);
2957 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002958 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002959
2960 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002961
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002962 default: {
2963
2964 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002965 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002966
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002967 // Process non-assignements except commas or short-circuited
2968 // logical expressions (LAnd and LOr).
Ted Kremenek07baa252008-02-21 18:02:17 +00002969
Zhongxing Xuc890e332009-05-20 09:00:16 +00002970 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002971
2972 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002973 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002974 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002975 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002976 }
2977 else
2978 Dst.Add(*I2);
2979
Ted Kremenekb8782e12008-02-21 19:15:37 +00002980 continue;
2981 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002982
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002983 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002984
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002985 // The operands were *not* undefined, but the result is undefined.
2986 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002987
Ted Kremeneke66ba682009-02-13 01:45:31 +00002988 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002989 UndefNode->markAsSink();
2990 UndefResults.insert(UndefNode);
2991 }
2992
2993 continue;
2994 }
2995
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002996 // Otherwise, create a new node.
2997
Ted Kremeneke66ba682009-02-13 01:45:31 +00002998 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002999 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00003000 }
Ted Kremenekf031b872008-01-23 19:59:44 +00003001 }
Ted Kremenek07baa252008-02-21 18:02:17 +00003002
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003003 assert (B->isCompoundAssignmentOp());
3004
Ted Kremenek570882a2009-02-07 00:52:24 +00003005 switch (Op) {
3006 default:
3007 assert(0 && "Invalid opcode for compound assignment.");
3008 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3009 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3010 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3011 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3012 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3013 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3014 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3015 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3016 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3017 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00003018 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003019
3020 // Perform a load (the LHS). This performs the checks for
3021 // null dereferences, and so on.
3022 NodeSet Tmp3;
Ted Kremeneke66ba682009-02-13 01:45:31 +00003023 SVal location = GetSVal(state, LHS);
3024 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003025
3026 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3027
Ted Kremeneke66ba682009-02-13 01:45:31 +00003028 state = GetState(*I3);
3029 SVal V = GetSVal(state, LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003030
Ted Kremenek6c438f82008-10-20 23:40:25 +00003031 // Check for divide-by-zero.
3032 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00003033 && RHS->getType()->isIntegerType()
3034 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00003035
3036 // CheckDivideZero returns a new state where the denominator
3037 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003038 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00003039
Ted Kremeneke66ba682009-02-13 01:45:31 +00003040 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00003041 continue;
3042 }
3043
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003044 // Propagate undefined values (left-side).
3045 if (V.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00003046 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003047 continue;
3048 }
3049
3050 // Propagate unknown values (left and right-side).
3051 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00003052 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3053 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003054 continue;
3055 }
3056
3057 // At this point:
3058 //
3059 // The LHS is not Undef/Unknown.
3060 // The RHS is not Unknown.
3061
3062 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00003063 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003064 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00003065
3066 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3067 CLHSTy = getContext().getCanonicalType(CTy);
3068
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003069 QualType LTy = getContext().getCanonicalType(LHS->getType());
3070 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00003071
3072 // Promote LHS.
3073 V = EvalCast(V, CLHSTy);
3074
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003075 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00003076 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00003077 // Propagate undefined values (right-side).
Ted Kremenek3f755632009-03-05 03:42:31 +00003078 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003079 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003080 continue;
3081 }
3082
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003083 // Compute the result of the operation.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003084 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3085 B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003086
3087 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003088 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003089 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003090 UndefNode->markAsSink();
3091 UndefResults.insert(UndefNode);
3092 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003093 continue;
3094 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003095
3096 // EXPERIMENTAL: "Conjured" symbols.
3097 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003098
3099 SVal LHSVal;
3100
Ted Kremenekd6a5a422009-03-11 02:24:48 +00003101 if ((Result.isUnknown() ||
3102 !getConstraintManager().canReasonAbout(Result))
3103 && (Loc::IsLocType(CTy)
3104 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00003105
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003106 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003107
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003108 // The symbolic value is actually for the type of the left-hand side
3109 // expression, not the computation type, as this is the value the
3110 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00003111 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003112
Zhongxing Xu5c70c772008-11-23 05:52:28 +00003113 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003114 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003115 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003116 else {
3117 // The left-hand side may bind to a different value then the
3118 // computation type.
3119 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3120 }
3121
Ted Kremeneke66ba682009-02-13 01:45:31 +00003122 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3123 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003124 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00003125 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003126 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003127}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003128
3129//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003130// Transfer-function Helpers.
3131//===----------------------------------------------------------------------===//
3132
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003133void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003134 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +00003135 NonLoc L, NonLoc R,
Ted Kremenek74556a12009-03-26 03:35:11 +00003136 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003137
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003138 GRStateSet OStates;
Ted Kremenek74556a12009-03-26 03:35:11 +00003139 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003140
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003141 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003142 MakeNode(Dst, Ex, Pred, *I);
3143}
3144
Ted Kremeneke66ba682009-02-13 01:45:31 +00003145void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003146 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +00003147 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003148
Ted Kremeneke66ba682009-02-13 01:45:31 +00003149 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremenek74556a12009-03-26 03:35:11 +00003150 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003151}
3152
Zhongxing Xuc890e332009-05-20 09:00:16 +00003153SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3154 SVal L, SVal R, QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00003155
3156 if (L.isUndef() || R.isUndef())
3157 return UndefinedVal();
3158
3159 if (L.isUnknown() || R.isUnknown())
3160 return UnknownVal();
3161
3162 if (isa<Loc>(L)) {
3163 if (isa<Loc>(R))
3164 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3165 else
Zhongxing Xuc890e332009-05-20 09:00:16 +00003166 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek4281e622009-01-30 19:27:39 +00003167 }
3168
3169 if (isa<Loc>(R)) {
3170 // Support pointer arithmetic where the increment/decrement operand
3171 // is on the left and the pointer on the right.
3172
3173 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3174
3175 // Commute the operands.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003176 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek4281e622009-01-30 19:27:39 +00003177 }
3178 else
3179 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremenek74556a12009-03-26 03:35:11 +00003180 cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003181}
3182
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003183//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00003184// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003185//===----------------------------------------------------------------------===//
3186
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003187#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003188static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003189static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00003190
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003191namespace llvm {
3192template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003193struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003194 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00003195
Ted Kremeneka853de62008-02-14 22:54:53 +00003196 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3197
3198 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00003199 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00003200 GraphPrintCheckerState->isUndefDeref(N) ||
3201 GraphPrintCheckerState->isUndefStore(N) ||
3202 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00003203 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3204 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00003205 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00003206 GraphPrintCheckerState->isBadCall(N) ||
3207 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00003208 return "color=\"red\",style=\"filled\"";
3209
Ted Kremenekc2d07202008-02-28 20:32:03 +00003210 if (GraphPrintCheckerState->isNoReturnCall(N))
3211 return "color=\"blue\",style=\"filled\"";
3212
Ted Kremeneka853de62008-02-14 22:54:53 +00003213 return "";
3214 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003215
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003216 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003217 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003218
3219 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003220 ProgramPoint Loc = N->getLocation();
3221
3222 switch (Loc.getKind()) {
3223 case ProgramPoint::BlockEntranceKind:
3224 Out << "Block Entrance: B"
3225 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3226 break;
3227
3228 case ProgramPoint::BlockExitKind:
3229 assert (false);
3230 break;
3231
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003232 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003233 if (isa<PostStmt>(Loc)) {
3234 const PostStmt& L = cast<PostStmt>(Loc);
3235 Stmt* S = L.getStmt();
3236 SourceLocation SLoc = S->getLocStart();
3237
3238 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3239 llvm::raw_os_ostream OutS(Out);
3240 S->printPretty(OutS);
3241 OutS.flush();
3242
3243 if (SLoc.isFileID()) {
3244 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003245 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3246 << " col="
3247 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3248 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003249 }
3250
Ted Kremenek0441f112009-05-07 18:27:16 +00003251 if (isa<PostLoad>(Loc))
3252 Out << "\\lPostLoad\\l;";
3253 else if (isa<PostStore>(Loc))
3254 Out << "\\lPostStore\\l";
3255 else if (isa<PostLValue>(Loc))
3256 Out << "\\lPostLValue\\l";
3257 else if (isa<PostLocationChecksSucceed>(Loc))
3258 Out << "\\lPostLocationChecksSucceed\\l";
3259 else if (isa<PostNullCheckFailed>(Loc))
3260 Out << "\\lPostNullCheckFailed\\l";
3261
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003262 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3263 Out << "\\|Implicit-Null Dereference.\\l";
3264 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3265 Out << "\\|Explicit-Null Dereference.\\l";
3266 else if (GraphPrintCheckerState->isUndefDeref(N))
3267 Out << "\\|Dereference of undefialied value.\\l";
3268 else if (GraphPrintCheckerState->isUndefStore(N))
3269 Out << "\\|Store to Undefined Loc.";
3270 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3271 Out << "\\|Explicit divide-by zero or undefined value.";
3272 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3273 Out << "\\|Implicit divide-by zero or undefined value.";
3274 else if (GraphPrintCheckerState->isUndefResult(N))
3275 Out << "\\|Result of operation is undefined.";
3276 else if (GraphPrintCheckerState->isNoReturnCall(N))
3277 Out << "\\|Call to function marked \"noreturn\".";
3278 else if (GraphPrintCheckerState->isBadCall(N))
3279 Out << "\\|Call to NULL/Undefined.";
3280 else if (GraphPrintCheckerState->isUndefArg(N))
3281 Out << "\\|Argument in call is undefined";
3282
3283 break;
3284 }
3285
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003286 const BlockEdge& E = cast<BlockEdge>(Loc);
3287 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3288 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003289
3290 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003291
3292 SourceLocation SLoc = T->getLocStart();
3293
Ted Kremenek90960972008-01-30 23:03:39 +00003294 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003295
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003296 llvm::raw_os_ostream OutS(Out);
3297 E.getSrc()->printTerminator(OutS);
3298 OutS.flush();
Ted Kremenek90960972008-01-30 23:03:39 +00003299
Ted Kremenekf97c6682008-03-09 03:30:59 +00003300 if (SLoc.isFileID()) {
3301 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003302 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3303 << " col="
3304 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003305 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003306
Ted Kremenekaee121c2008-02-13 23:08:21 +00003307 if (isa<SwitchStmt>(T)) {
3308 Stmt* Label = E.getDst()->getLabel();
3309
3310 if (Label) {
3311 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3312 Out << "\\lcase ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003313 llvm::raw_os_ostream OutS(Out);
3314 C->getLHS()->printPretty(OutS);
3315 OutS.flush();
3316
Ted Kremenekaee121c2008-02-13 23:08:21 +00003317 if (Stmt* RHS = C->getRHS()) {
3318 Out << " .. ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003319 RHS->printPretty(OutS);
3320 OutS.flush();
Ted Kremenekaee121c2008-02-13 23:08:21 +00003321 }
3322
3323 Out << ":";
3324 }
3325 else {
3326 assert (isa<DefaultStmt>(Label));
3327 Out << "\\ldefault:";
3328 }
3329 }
3330 else
3331 Out << "\\l(implicit) default:";
3332 }
3333 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003334 // FIXME
3335 }
3336 else {
3337 Out << "\\lCondition: ";
3338 if (*E.getSrc()->succ_begin() == E.getDst())
3339 Out << "true";
3340 else
3341 Out << "false";
3342 }
3343
3344 Out << "\\l";
3345 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003346
Ted Kremenekb31af242008-02-28 09:25:22 +00003347 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3348 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003349 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003350 }
3351 }
3352
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003353 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003354
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003355 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3356 state.printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003357
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003358 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003359 return Out.str();
3360 }
3361};
3362} // end llvm namespace
3363#endif
3364
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003365#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003366template <typename ITERATOR>
3367GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3368
3369template <>
3370GRExprEngine::NodeTy*
3371GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3372 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3373 return I->first;
3374}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003375#endif
3376
3377void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003378#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003379 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003380 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003381
3382 // Flush any outstanding reports to make sure we cover all the nodes.
3383 // This does not cause them to get displayed.
3384 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3385 const_cast<BugType*>(*I)->FlushReports(BR);
3386
3387 // Iterate through the reports and get their nodes.
3388 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3389 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3390 const BugReportEquivClass& EQ = *I2;
3391 const BugReport &R = **EQ.begin();
3392 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3393 if (N) Src.push_back(N);
3394 }
3395 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003396
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003397 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003398 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003399 else {
3400 GraphPrintCheckerState = this;
3401 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003402
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003403 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003404
3405 GraphPrintCheckerState = NULL;
3406 GraphPrintSourceManager = NULL;
3407 }
3408#endif
3409}
3410
3411void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3412#ifndef NDEBUG
3413 GraphPrintCheckerState = this;
3414 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003415
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003416 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003417
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003418 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003419 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003420 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003421 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003422
Ted Kremenek428d39e2008-01-30 23:24:39 +00003423 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003424 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003425#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003426}