blob: 4db00d2a6b0080edf52d472c81e66c2ca34f3297 [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 Kremenek75732212009-04-01 06:52:48 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek50df4f42008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek0e80dea2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000026
Ted Kremenek9f6b1612008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekd4467432008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000036
Ted Kremenekca5f6202008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000041namespace {
42
43class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
44 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
45 typedef llvm::DenseMap<void*,Checks> MapTy;
46
47 MapTy M;
48 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000049 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000050
51public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000052 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
53 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000054
55 virtual ~MappedBatchAuditor() {
56 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
57
58 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
59 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
60
61 GRSimpleAPICheck* check = *I;
62
63 if (AlreadyVisited.count(check))
64 continue;
65
66 AlreadyVisited.insert(check);
67 delete check;
68 }
69 }
70
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000071 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000072 assert (A && "Check cannot be null.");
73 void* key = reinterpret_cast<void*>((uintptr_t) C);
74 MapTy::iterator I = M.find(key);
75 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
76 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000077
78 void AddCheck(GRSimpleAPICheck *A) {
79 assert (A && "Check cannot be null.");
80 AllStmts = F.Concat(A, AllStmts);
81 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000082
Ted Kremenekabd89ac2008-08-13 04:27:00 +000083 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000084 // First handle the auditors that accept all statements.
85 bool isSink = false;
86 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
87 isSink |= (*I)->Audit(N, VMgr);
88
89 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000090 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
91 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
92 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000093 if (MI != M.end()) {
94 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
95 isSink |= (*I)->Audit(N, VMgr);
96 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000097
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000098 return isSink;
99 }
100};
101
102} // end anonymous namespace
103
104//===----------------------------------------------------------------------===//
105// Engine construction and deletion.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek5f20a632008-05-01 18:33:28 +0000108static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
109 IdentifierInfo* II = &Ctx.Idents.get(name);
110 return Ctx.Selectors.getSelector(0, &II);
111}
112
Ted Kremenekf973eb02008-03-09 18:05:48 +0000113
Ted Kremenek1607f512008-07-02 20:13:38 +0000114GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000115 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000116 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000117 StoreManagerCreator SMC,
118 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000122 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000123 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000124 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000125 ValMgr(StateMgr.getValueManager()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
128 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000129 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000130 BR(BRD, *this),
131 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000132
Ted Kremenek72f52c02008-06-20 21:45:25 +0000133GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000134 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000136}
137
Ted Kremenekca5f6202008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000142
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000143void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000144 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000145 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000146 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000147}
148
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000149void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
150 if (!BatchAuditor)
151 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
152
153 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000154}
155
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000156void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
157 if (!BatchAuditor)
158 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
159
160 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
161}
162
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000163const GRState* GRExprEngine::getInitialState() {
Ted Kremenek04c0add2009-04-10 00:59:50 +0000164 const GRState *state = StateMgr.getInitialState();
165
166 // Precondition: the first argument of 'main' is an integer guaranteed
167 // to be > 0.
168 // FIXME: It would be nice if we had a more general mechanism to add
169 // such preconditions. Some day.
170 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
171 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
172 FD->getNumParams() > 0) {
173 const ParmVarDecl *PD = FD->getParamDecl(0);
174 QualType T = PD->getType();
175 if (T->isIntegerType())
176 if (const MemRegion *R = StateMgr.getRegion(PD)) {
177 SVal V = GetSVal(state, loc::MemRegionVal(R));
178 SVal Constraint = EvalBinOp(BinaryOperator::GT, V,
179 ValMgr.makeZeroVal(T),
180 getContext().IntTy);
181 bool isFeasible = false;
182 const GRState *newState = Assume(state, Constraint, true,
183 isFeasible);
184 if (newState) state = newState;
185 }
186 }
187
188 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000189}
190
Ted Kremenekca5f6202008-04-15 23:06:53 +0000191//===----------------------------------------------------------------------===//
192// Top-level transfer function logic (Dispatcher).
193//===----------------------------------------------------------------------===//
194
195void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
196
Ted Kremenek820c73b2009-03-11 02:41:36 +0000197 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
198 S->getLocStart(),
199 "Error evaluating statement");
200
Ted Kremenekca5f6202008-04-15 23:06:53 +0000201 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000202 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000203
204 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000205 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000206 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000207
208 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000209 if (BatchAuditor)
210 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000211
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000212 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000213 SymbolReaper SymReaper(Liveness, SymMgr);
214 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
215 CurrentStmt, SymReaper)
216 : EntryNode->getState();
217
Ted Kremenek7487f942008-04-24 18:31:42 +0000218 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000219 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000220
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000221 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000222 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000223 else {
224 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000225 SaveOr OldHasGen(Builder->HasGeneratedNode);
226
Ted Kremenekf05eec42008-06-18 05:34:07 +0000227 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
228 Builder->PurgingDeadSymbols = true;
229
Ted Kremenekc7469542008-07-17 23:15:45 +0000230 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000231 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000232
233 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
234 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000235 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000236
237 bool HasAutoGenerated = false;
238
Ted Kremenek7487f942008-04-24 18:31:42 +0000239 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000240
241 NodeSet Dst;
242
Ted Kremenek7487f942008-04-24 18:31:42 +0000243 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000244 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
245
Ted Kremenek7487f942008-04-24 18:31:42 +0000246 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000247 Visit(S, *I, Dst);
248
249 // Do we need to auto-generate a node? We only need to do this to generate
250 // a node with a "cleaned" state; GRCoreEngine will actually handle
251 // auto-transitions for other cases.
252 if (Dst.size() == 1 && *Dst.begin() == EntryNode
253 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
254 HasAutoGenerated = true;
255 builder.generateNode(S, GetState(EntryNode), *I);
256 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000257 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000258
Ted Kremenekca5f6202008-04-15 23:06:53 +0000259 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000260 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000261 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000262
263 // FIXME: Consolidate.
264 StateMgr.CurrentStmt = 0;
265 CurrentStmt = 0;
266
Ted Kremenekfa7be362008-04-24 23:35:58 +0000267 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000268}
269
Ted Kremenek820c73b2009-03-11 02:41:36 +0000270void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
271 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
272 S->getLocStart(),
273 "Error evaluating statement");
274
Ted Kremenekca5f6202008-04-15 23:06:53 +0000275 // FIXME: add metadata to the CFG so that we can disable
276 // this check when we KNOW that there is no block-level subexpression.
277 // The motivation is that this check requires a hashtable lookup.
278
279 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
280 Dst.Add(Pred);
281 return;
282 }
283
284 switch (S->getStmtClass()) {
285
286 default:
287 // Cases we intentionally have "default" handle:
288 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
289
290 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
291 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000292
293 case Stmt::ArraySubscriptExprClass:
294 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
295 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000296
297 case Stmt::AsmStmtClass:
298 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
299 break;
300
301 case Stmt::BinaryOperatorClass: {
302 BinaryOperator* B = cast<BinaryOperator>(S);
303
304 if (B->isLogicalOp()) {
305 VisitLogicalExpr(B, Pred, Dst);
306 break;
307 }
308 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000309 const GRState* state = GetState(Pred);
310 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000311 break;
312 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000313
Ted Kremenek8f520972009-02-25 22:32:02 +0000314 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
315 NodeSet Tmp;
316 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000317 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000318 }
319 else
320 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
321
Ted Kremenekca5f6202008-04-15 23:06:53 +0000322 break;
323 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000324
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000325 case Stmt::CallExprClass:
326 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000327 CallExpr* C = cast<CallExpr>(S);
328 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000329 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000330 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000331
Ted Kremenekca5f6202008-04-15 23:06:53 +0000332 // FIXME: ChooseExpr is really a constant. We need to fix
333 // the CFG do not model them as explicit control-flow.
334
335 case Stmt::ChooseExprClass: { // __builtin_choose_expr
336 ChooseExpr* C = cast<ChooseExpr>(S);
337 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
338 break;
339 }
340
341 case Stmt::CompoundAssignOperatorClass:
342 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
343 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000344
345 case Stmt::CompoundLiteralExprClass:
346 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
347 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000348
349 case Stmt::ConditionalOperatorClass: { // '?' operator
350 ConditionalOperator* C = cast<ConditionalOperator>(S);
351 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
352 break;
353 }
354
355 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000356 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000357 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000358 break;
359
360 case Stmt::DeclStmtClass:
361 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
362 break;
363
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000364 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000365 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000366 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000367 VisitCast(C, C->getSubExpr(), Pred, Dst);
368 break;
369 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000370
371 case Stmt::InitListExprClass:
372 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
373 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000374
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000375 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000376 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
377 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000378
379 case Stmt::ObjCIvarRefExprClass:
380 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
381 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000382
383 case Stmt::ObjCForCollectionStmtClass:
384 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
385 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000386
Ted Kremenekca5f6202008-04-15 23:06:53 +0000387 case Stmt::ObjCMessageExprClass: {
388 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
389 break;
390 }
391
Ted Kremenek3c186252008-12-09 20:18:58 +0000392 case Stmt::ObjCAtThrowStmtClass: {
393 // FIXME: This is not complete. We basically treat @throw as
394 // an abort.
395 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
396 Builder->BuildSinks = true;
397 MakeNode(Dst, S, Pred, GetState(Pred));
398 break;
399 }
400
Ted Kremenekca5f6202008-04-15 23:06:53 +0000401 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000402 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000403 break;
404
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000405 case Stmt::ReturnStmtClass:
406 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
407 break;
408
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000409 case Stmt::SizeOfAlignOfExprClass:
410 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000411 break;
412
413 case Stmt::StmtExprClass: {
414 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000415
416 if (SE->getSubStmt()->body_empty()) {
417 // Empty statement expression.
418 assert(SE->getType() == getContext().VoidTy
419 && "Empty statement expression must have void type.");
420 Dst.Add(Pred);
421 break;
422 }
423
424 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
425 const GRState* state = GetState(Pred);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000426 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000427 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000428 else
429 Dst.Add(Pred);
430
431 break;
432 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000433
434 case Stmt::StringLiteralClass:
435 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
436 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000437
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000438 case Stmt::UnaryOperatorClass: {
439 UnaryOperator *U = cast<UnaryOperator>(S);
440 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
441 NodeSet Tmp;
442 VisitUnaryOperator(U, Pred, Tmp, false);
443 EvalEagerlyAssume(Dst, Tmp, U);
444 }
445 else
446 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000447 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000448 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000449 }
450}
451
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000452void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000453
454 Ex = Ex->IgnoreParens();
455
456 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
457 Dst.Add(Pred);
458 return;
459 }
460
461 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000462
463 case Stmt::ArraySubscriptExprClass:
464 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
465 return;
466
467 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000468 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000469 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
470 return;
471
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000472 case Stmt::ObjCIvarRefExprClass:
473 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
474 return;
475
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000476 case Stmt::UnaryOperatorClass:
477 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
478 return;
479
480 case Stmt::MemberExprClass:
481 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
482 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000483
Ted Kremenekd83daa52008-10-27 21:54:31 +0000484 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000485 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000486 return;
487
Ted Kremenek71c707b2008-10-17 17:24:14 +0000488 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek93cb3d12009-04-21 23:53:32 +0000489 case Stmt::ObjCKVCRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000490 // FIXME: Property assignments are lvalues, but not really "locations".
491 // e.g.: self.x = something;
492 // Here the "self.x" really can translate to a method call (setter) when
493 // the assignment is made. Moreover, the entire assignment expression
494 // evaluate to whatever "something" is, not calling the "getter" for
495 // the property (which would make sense since it can have side effects).
496 // We'll probably treat this as a location, but not one that we can
497 // take the address of. Perhaps we need a new SVal class for cases
498 // like thsis?
499 // Note that we have a similar problem for bitfields, since they don't
500 // have "locations" in the sense that we can take their address.
501 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000502 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000503
504 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000505 const GRState* state = GetState(Pred);
506 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
507 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000508 return;
509 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000510
Ted Kremenek2c829a32008-10-18 04:15:35 +0000511 default:
512 // Arbitrary subexpressions can return aggregate temporaries that
513 // can be used in a lvalue context. We need to enhance our support
514 // of such temporaries in both the environment and the store, so right
515 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000516 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000517 "Other kinds of expressions with non-aggregate/union types do"
518 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000519
Ted Kremenek2c829a32008-10-18 04:15:35 +0000520 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000521 }
522}
523
524//===----------------------------------------------------------------------===//
525// Block entrance. (Update counters).
526//===----------------------------------------------------------------------===//
527
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000528bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000529 GRBlockCounter BC) {
530
531 return BC.getNumVisited(B->getBlockID()) < 3;
532}
533
534//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000535// Generic node creation.
536//===----------------------------------------------------------------------===//
537
538GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
539 NodeTy* Pred,
540 const GRState* St,
541 ProgramPoint::Kind K,
542 const void *tag) {
543
544 assert (Builder && "GRStmtNodeBuilder not present.");
545 SaveAndRestore<const void*> OldTag(Builder->Tag);
546 Builder->Tag = tag;
547 return Builder->MakeNode(Dst, S, Pred, St, K);
548}
549
550//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000551// Branch processing.
552//===----------------------------------------------------------------------===//
553
Ted Kremeneke66ba682009-02-13 01:45:31 +0000554const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000555 Stmt* Terminator,
556 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000557
558 switch (Terminator->getStmtClass()) {
559 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000560 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000561
562 case Stmt::BinaryOperatorClass: { // '&&' and '||'
563
564 BinaryOperator* B = cast<BinaryOperator>(Terminator);
565 BinaryOperator::Opcode Op = B->getOpcode();
566
567 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
568
569 // For &&, if we take the true branch, then the value of the whole
570 // expression is that of the RHS expression.
571 //
572 // For ||, if we take the false branch, then the value of the whole
573 // expression is that of the RHS expression.
574
575 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
576 (Op == BinaryOperator::LOr && !branchTaken)
577 ? B->getRHS() : B->getLHS();
578
Ted Kremeneke66ba682009-02-13 01:45:31 +0000579 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000580 }
581
582 case Stmt::ConditionalOperatorClass: { // ?:
583
584 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
585
586 // For ?, if branchTaken == true then the value is either the LHS or
587 // the condition itself. (GNU extension).
588
589 Expr* Ex;
590
591 if (branchTaken)
592 Ex = C->getLHS() ? C->getLHS() : C->getCond();
593 else
594 Ex = C->getRHS();
595
Ted Kremeneke66ba682009-02-13 01:45:31 +0000596 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000597 }
598
599 case Stmt::ChooseExprClass: { // ?:
600
601 ChooseExpr* C = cast<ChooseExpr>(Terminator);
602
603 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000604 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000605 }
606 }
607}
608
Ted Kremenekc39c2172009-03-13 16:32:54 +0000609/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
610/// to try to recover some path-sensitivity for casts of symbolic
611/// integers that promote their values (which are currently not tracked well).
612/// This function returns the SVal bound to Condition->IgnoreCasts if all the
613// cast(s) did was sign-extend the original value.
614static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
615 Stmt* Condition, ASTContext& Ctx) {
616
617 Expr *Ex = dyn_cast<Expr>(Condition);
618 if (!Ex)
619 return UnknownVal();
620
621 uint64_t bits = 0;
622 bool bitsInit = false;
623
624 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
625 QualType T = CE->getType();
626
627 if (!T->isIntegerType())
628 return UnknownVal();
629
630 uint64_t newBits = Ctx.getTypeSize(T);
631 if (!bitsInit || newBits < bits) {
632 bitsInit = true;
633 bits = newBits;
634 }
635
636 Ex = CE->getSubExpr();
637 }
638
639 // We reached a non-cast. Is it a symbolic value?
640 QualType T = Ex->getType();
641
642 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
643 return UnknownVal();
644
645 return StateMgr.GetSVal(state, Ex);
646}
647
Ted Kremenek13e167f2008-11-12 19:24:17 +0000648void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000649 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000650
Ted Kremenek17c5f112008-02-11 19:21:59 +0000651 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000652 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000653 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000654
Ted Kremenek022b6052008-02-15 22:29:00 +0000655 // Check for NULL conditions; e.g. "for(;;)"
656 if (!Condition) {
657 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000658 return;
659 }
660
Ted Kremeneke43de222009-03-11 03:54:24 +0000661 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
662 Condition->getLocStart(),
663 "Error evaluating branch");
664
Zhongxing Xu097fc982008-10-17 05:57:07 +0000665 SVal V = GetSVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000666
667 switch (V.getBaseKind()) {
668 default:
669 break;
670
Ted Kremenekc39c2172009-03-13 16:32:54 +0000671 case SVal::UnknownKind: {
672 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
673 if (Ex->getType()->isIntegerType()) {
674 // Try to recover some path-sensitivity. Right now casts of symbolic
675 // integers that promote their values are currently not tracked well.
676 // If 'Condition' is such an expression, try and recover the
677 // underlying value and use that instead.
678 SVal recovered = RecoverCastedSymbol(getStateManager(),
679 builder.getState(), Condition,
680 getContext());
681
682 if (!recovered.isUnknown()) {
683 V = recovered;
684 break;
685 }
686 }
687 }
688
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000689 builder.generateNode(MarkBranch(PrevState, Term, true), true);
690 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000691 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000692 }
Ted Kremenek90960972008-01-30 23:03:39 +0000693
Zhongxing Xu097fc982008-10-17 05:57:07 +0000694 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000695 NodeTy* N = builder.generateNode(PrevState, true);
696
697 if (N) {
698 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000699 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000700 }
701
702 builder.markInfeasible(false);
703 return;
704 }
705 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000706
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000707 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000708
Ted Kremenekd4676512008-03-12 21:45:47 +0000709 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000710 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000711
712 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000713 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000714 else
715 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000716
717 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000718
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000719 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000720 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000721
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000722 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000723 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000724 else
725 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000726}
727
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000728/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000729/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000730void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000731
Ted Kremeneke66ba682009-02-13 01:45:31 +0000732 const GRState* state = builder.getState();
733 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000734
735 // Three possibilities:
736 //
737 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000738 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000739 // (3) We have no clue about the label. Dispatch to all targets.
740 //
741
742 typedef IndirectGotoNodeBuilder::iterator iterator;
743
Zhongxing Xu097fc982008-10-17 05:57:07 +0000744 if (isa<loc::GotoLabel>(V)) {
745 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000746
747 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000748 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000749 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000750 return;
751 }
752 }
753
754 assert (false && "No block with label.");
755 return;
756 }
757
Zhongxing Xu097fc982008-10-17 05:57:07 +0000758 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000759 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000760 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000761 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000762 return;
763 }
764
765 // This is really a catch-all. We don't support symbolics yet.
766
Ted Kremenek07baa252008-02-21 18:02:17 +0000767 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000768
769 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000770 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000771}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000772
Ted Kremenekca5f6202008-04-15 23:06:53 +0000773
774void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
775 NodeTy* Pred, NodeSet& Dst) {
776
777 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
778
Ted Kremeneke66ba682009-02-13 01:45:31 +0000779 const GRState* state = GetState(Pred);
780 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000781
782 assert (X.isUndef());
783
784 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
785
786 assert (SE);
787
Ted Kremeneke66ba682009-02-13 01:45:31 +0000788 X = GetBlkExprSVal(state, SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000789
790 // Make sure that we invalidate the previous binding.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000791 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000792}
793
Ted Kremenekaee121c2008-02-13 23:08:21 +0000794/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
795/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000796void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
797 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000798 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000799 Expr* CondE = builder.getCondition();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000800 SVal CondV = GetSVal(state, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000801
Ted Kremenekb31af242008-02-28 09:25:22 +0000802 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000803 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000804 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000805 return;
806 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000807
Ted Kremeneke66ba682009-02-13 01:45:31 +0000808 const GRState* DefaultSt = state;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000809 bool DefaultFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000810
Ted Kremenek07baa252008-02-21 18:02:17 +0000811 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000812 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000813
814 // Evaluate the LHS of the case value.
815 Expr::EvalResult V1;
816 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000817
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000818 // Sanity checks. These go away in Release builds.
819 assert(b && V1.Val.isInt() && !V1.HasSideEffects
820 && "Case condition must evaluate to an integer constant.");
821 b = b; // silence unused variable warning
822 assert(V1.Val.getInt().getBitWidth() ==
823 getContext().getTypeSize(CondE->getType()));
824
Ted Kremenekaee121c2008-02-13 23:08:21 +0000825 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000826 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000827
828 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000829 b = E->Evaluate(V2, getContext());
830 assert(b && V2.Val.isInt() && !V2.HasSideEffects
831 && "Case condition must evaluate to an integer constant.");
832 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000833 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000834 else
835 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000836
837 // FIXME: Eventually we should replace the logic below with a range
838 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000839 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000840
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000841 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000842 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek74556a12009-03-26 03:35:11 +0000843 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
844 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000845
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000846 // Now "assume" that the case matches.
Ted Kremenekd4676512008-03-12 21:45:47 +0000847 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000848 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000849
850 if (isFeasible) {
851 builder.generateCaseStmtNode(I, StNew);
852
853 // If CondV evaluates to a constant, then we know that this
854 // is the *only* case that we can take, so stop evaluating the
855 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000856 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000857 return;
858 }
859
860 // Now "assume" that the case doesn't match. Add this state
861 // to the default state (if it is feasible).
862
Ted Kremenekd4676512008-03-12 21:45:47 +0000863 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000864 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000865
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000866 if (isFeasible) {
867 DefaultFeasible = true;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000868 DefaultSt = StNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000869 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000870
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000871 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000872 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000873 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000874
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000875 ++V1.Val.getInt();
876 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000877
878 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000879 }
880
881 // If we reach here, than we know that the default branch is
882 // possible.
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000883 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000884}
885
Ted Kremenekca5f6202008-04-15 23:06:53 +0000886//===----------------------------------------------------------------------===//
887// Transfer functions: logical operations ('&&', '||').
888//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000889
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000890void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000891 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000892
Ted Kremenek99ecce72008-02-26 19:05:15 +0000893 assert (B->getOpcode() == BinaryOperator::LAnd ||
894 B->getOpcode() == BinaryOperator::LOr);
895
896 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
897
Ted Kremeneke66ba682009-02-13 01:45:31 +0000898 const GRState* state = GetState(Pred);
899 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000900
Ted Kremenekb31af242008-02-28 09:25:22 +0000901 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000902
Ted Kremenekb31af242008-02-28 09:25:22 +0000903 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000904
905 assert (Ex);
906
907 if (Ex == B->getRHS()) {
908
Ted Kremeneke66ba682009-02-13 01:45:31 +0000909 X = GetBlkExprSVal(state, Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000910
Ted Kremenekb31af242008-02-28 09:25:22 +0000911 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000912
Ted Kremenekb31af242008-02-28 09:25:22 +0000913 if (X.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000914 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000915 return;
916 }
917
Ted Kremenek99ecce72008-02-26 19:05:15 +0000918 // We took the RHS. Because the value of the '&&' or '||' expression must
919 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
920 // or 1. Alternatively, we could take a lazy approach, and calculate this
921 // value later when necessary. We don't have the machinery in place for
922 // this right now, and since most logical expressions are used for branches,
923 // the payoff is not likely to be large. Instead, we do eager evaluation.
924
925 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000926 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000927
928 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000929 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000930 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000931
932 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000933 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000934
935 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000936 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000937 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000938 }
939 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000940 // We took the LHS expression. Depending on whether we are '&&' or
941 // '||' we know what the value of the expression is via properties of
942 // the short-circuiting.
943
944 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000945 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000946 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000947}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000948
Ted Kremenekca5f6202008-04-15 23:06:53 +0000949//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000950// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000951//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000952
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000953void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
954 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000955
Ted Kremeneke66ba682009-02-13 01:45:31 +0000956 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000957
Douglas Gregord2baafd2008-10-21 16:13:35 +0000958 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000959
960 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
961
Ted Kremeneke66ba682009-02-13 01:45:31 +0000962 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000963
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000964 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000965 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000966 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000967 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000968 return;
969
970 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
971 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
972
973 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000974 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneke66ba682009-02-13 01:45:31 +0000975 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000976 return;
977
978 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000979 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000980 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000981 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000982 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000983 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000984
985 assert (false &&
986 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000987}
988
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000989/// VisitArraySubscriptExpr - Transfer function for array accesses
990void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000991 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000992
993 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000994 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000995 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000996
997 if (Base->getType()->isVectorType()) {
998 // For vector types get its lvalue.
999 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1000 // In fact, I think this is just a hack. We need to get the right
1001 // semantics.
1002 VisitLValue(Base, Pred, Tmp);
1003 }
1004 else
1005 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001006
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001007 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +00001008 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001009 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +00001010
1011 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001012 const GRState* state = GetState(*I2);
1013 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
1014 GetSVal(state, Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +00001015
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001016 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001017 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenekc4385b42008-04-29 23:24:44 +00001018 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001019 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001020 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001021 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +00001022}
1023
Ted Kremenekd0d86202008-04-21 23:43:38 +00001024/// VisitMemberExpr - Transfer function for member expressions.
1025void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001026 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +00001027
1028 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +00001029 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +00001030
1031 if (M->isArrow())
1032 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1033 else
1034 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1035
Douglas Gregor82d44772008-12-20 23:49:58 +00001036 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1037 if (!Field) // FIXME: skipping member expressions for non-fields
1038 return;
1039
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001040 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001041 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001042 // FIXME: Should we insert some assumption logic in here to determine
1043 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001044 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneke66ba682009-02-13 01:45:31 +00001045 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001046
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001047 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001048 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001049 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001050 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001051 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001052}
1053
Ted Kremeneke66ba682009-02-13 01:45:31 +00001054/// EvalBind - Handle the semantics of binding a value to a specific location.
1055/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1056void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1057 const GRState* state, SVal location, SVal Val) {
1058
Ted Kremeneka42be302009-02-14 01:43:44 +00001059 const GRState* newState = 0;
1060
1061 if (location.isUnknown()) {
1062 // We know that the new state will be the same as the old state since
1063 // the location of the binding is "unknown". Consequently, there
1064 // is no reason to just create a new node.
1065 newState = state;
1066 }
1067 else {
1068 // We are binding to a value other than 'unknown'. Perform the binding
1069 // using the StoreManager.
1070 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1071 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001072
Ted Kremeneka42be302009-02-14 01:43:44 +00001073 // The next thing to do is check if the GRTransferFuncs object wants to
1074 // update the state based on the new binding. If the GRTransferFunc object
1075 // doesn't do anything, just auto-propagate the current state.
1076 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1077 newState != state);
1078
1079 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001080}
1081
1082/// EvalStore - Handle the semantics of a store via an assignment.
1083/// @param Dst The node set to store generated state nodes
1084/// @param Ex The expression representing the location of the store
1085/// @param state The current simulation state
1086/// @param location The location to store the value
1087/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001088void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001089 const GRState* state, SVal location, SVal Val,
1090 const void *tag) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001091
1092 assert (Builder && "GRStmtNodeBuilder must be defined.");
1093
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001094 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001095 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001096
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001097 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001098 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001099
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001100 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001101 state = GetState(Pred);
1102
1103 // Proceed with the store.
1104 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001105 SaveAndRestore<const void*> OldTag(Builder->Tag);
1106 Builder->PointKind = ProgramPoint::PostStoreKind;
1107 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001108 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001109}
1110
1111void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001112 const GRState* state, SVal location,
1113 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001114
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001115 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001116 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001117
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001118 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001119 return;
1120
Ted Kremeneke66ba682009-02-13 01:45:31 +00001121 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001122
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001123 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001124 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001125
1126 // FIXME: Currently symbolic analysis "generates" new symbols
1127 // for the contents of values. We need a better approach.
1128
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001129 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001130 // This is important. We must nuke the old binding.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001131 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001132 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001133 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001134 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001135 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001136 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001137}
1138
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001139void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001140 const GRState* state, SVal location, SVal Val,
1141 const void *tag) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001142
1143 NodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001144 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001145
1146 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001147 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001148}
1149
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001150GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001151 const GRState* state,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001152 SVal location,
1153 const void *tag) {
1154
1155 SaveAndRestore<const void*> OldTag(Builder->Tag);
1156 Builder->Tag = tag;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001157
1158 // Check for loads/stores from/to undefined values.
1159 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001160 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001161 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001162 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001163
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001164 if (N) {
1165 N->markAsSink();
1166 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001167 }
1168
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001169 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001170 }
1171
1172 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1173 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001174 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001175
1176 // During a load, one of two possible situations arise:
1177 // (1) A crash, because the location (pointer) was NULL.
1178 // (2) The location (pointer) is not NULL, and the dereference works.
1179 //
1180 // We add these assumptions.
1181
Zhongxing Xu097fc982008-10-17 05:57:07 +00001182 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001183
1184 // "Assume" that the pointer is not NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001185 bool isFeasibleNotNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001186 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001187
1188 // "Assume" that the pointer is NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001189 bool isFeasibleNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001190 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001191 getStateManager());
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001192
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001193 if (isFeasibleNull) {
1194
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001195 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001196 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001197 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1198
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001199 // We don't use "MakeNode" here because the node will be a sink
1200 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001201 NodeTy* NullNode =
1202 Builder->generateNode(Ex, StNull, Pred,
1203 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001204
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001205 if (NullNode) {
1206
1207 NullNode->markAsSink();
1208
1209 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1210 else ExplicitNullDeref.insert(NullNode);
1211 }
1212 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001213
1214 if (!isFeasibleNotNull)
1215 return 0;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001216
1217 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001218 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001219 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1220 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1221 // Get the index of the accessed element.
1222 SVal Idx = ER->getIndex();
1223 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001224 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1225 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001226
1227 bool isFeasibleInBound = false;
1228 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1229 true, isFeasibleInBound);
1230
1231 bool isFeasibleOutBound = false;
1232 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1233 false, isFeasibleOutBound);
1234
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001235 if (isFeasibleOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001236 // Report warning. Make sink node manually.
1237 NodeTy* OOBNode =
1238 Builder->generateNode(Ex, StOutBound, Pred,
1239 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001240
1241 if (OOBNode) {
1242 OOBNode->markAsSink();
1243
1244 if (isFeasibleInBound)
1245 ImplicitOOBMemAccesses.insert(OOBNode);
1246 else
1247 ExplicitOOBMemAccesses.insert(OOBNode);
1248 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001249 }
1250
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001251 if (!isFeasibleInBound)
1252 return 0;
1253
1254 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001255 }
1256 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001257
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001258 // Generate a new node indicating the checks succeed.
1259 return Builder->generateNode(Ex, StNotNull, Pred,
1260 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001261}
1262
Ted Kremenekca5f6202008-04-15 23:06:53 +00001263//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001264// Transfer function: OSAtomics.
1265//
1266// FIXME: Eventually refactor into a more "plugin" infrastructure.
1267//===----------------------------------------------------------------------===//
1268
1269// Mac OS X:
1270// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1271// atomic.3.html
1272//
1273static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1274 GRExprEngine& Engine,
1275 GRStmtNodeBuilder<GRState>& Builder,
1276 CallExpr* CE, SVal L,
1277 ExplodedNode<GRState>* Pred) {
1278
1279 // Not enough arguments to match OSAtomicCompareAndSwap?
1280 if (CE->getNumArgs() != 3)
1281 return false;
1282
1283 ASTContext &C = Engine.getContext();
1284 Expr *oldValueExpr = CE->getArg(0);
1285 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1286
1287 Expr *newValueExpr = CE->getArg(1);
1288 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1289
1290 // Do the types of 'oldValue' and 'newValue' match?
1291 if (oldValueType != newValueType)
1292 return false;
1293
1294 Expr *theValueExpr = CE->getArg(2);
1295 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1296
1297 // theValueType not a pointer?
1298 if (!theValueType)
1299 return false;
1300
1301 QualType theValueTypePointee =
1302 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1303
1304 // The pointee must match newValueType and oldValueType.
1305 if (theValueTypePointee != newValueType)
1306 return false;
1307
1308 static unsigned magic_load = 0;
1309 static unsigned magic_store = 0;
1310
1311 const void *OSAtomicLoadTag = &magic_load;
1312 const void *OSAtomicStoreTag = &magic_store;
1313
1314 // Load 'theValue'.
1315 GRStateManager &StateMgr = Engine.getStateManager();
1316 const GRState *state = Pred->getState();
1317 ExplodedNodeSet<GRState> Tmp;
1318 SVal location = StateMgr.GetSVal(state, theValueExpr);
1319 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1320
1321 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1322 I != E; ++I) {
1323
1324 ExplodedNode<GRState> *N = *I;
1325 const GRState *stateLoad = N->getState();
1326 SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
1327 SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
1328
1329 // Perform the comparison.
1330 SVal Cmp = Engine.EvalBinOp(BinaryOperator::EQ, theValueVal, oldValueVal,
1331 Engine.getContext().IntTy);
1332 bool isFeasible = false;
1333 const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
1334 isFeasible);
1335
1336 // Were they equal?
1337 if (isFeasible) {
1338 // Perform the store.
1339 ExplodedNodeSet<GRState> TmpStore;
1340 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
1341 StateMgr.GetSVal(stateEqual, newValueExpr),
1342 OSAtomicStoreTag);
1343
1344 // Now bind the result of the comparison.
1345 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1346 E2 = TmpStore.end(); I2 != E2; ++I2) {
1347 ExplodedNode<GRState> *predNew = *I2;
1348 const GRState *stateNew = predNew->getState();
1349 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1350 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1351 }
1352 }
1353
1354 // Were they not equal?
1355 isFeasible = false;
1356 const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
1357 isFeasible);
1358
1359 if (isFeasible) {
1360 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1361 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1362 }
1363 }
1364
1365 return true;
1366}
1367
1368static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1369 GRExprEngine& Engine,
1370 GRStmtNodeBuilder<GRState>& Builder,
1371 CallExpr* CE, SVal L,
1372 ExplodedNode<GRState>* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001373 const FunctionDecl* FD = L.getAsFunctionDecl();
1374 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001375 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001376
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001377 const char *FName = FD->getNameAsCString();
1378
1379 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001380 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1381 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001382 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001383
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001384 // FIXME: Other atomics.
1385 return false;
1386}
1387
1388//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001389// Transfer function: Function calls.
1390//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001391
1392void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1393 assert (Builder && "GRStmtNodeBuilder must be defined.");
1394
1395 // FIXME: Allow us to chain together transfer functions.
1396 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1397 return;
1398
1399 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1400}
1401
Ted Kremenekd9268e32008-02-19 01:44:53 +00001402void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001403 CallExpr::arg_iterator AI,
1404 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001405 NodeSet& Dst)
1406{
1407 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001408 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001409 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1410 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001411 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001412
1413 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1414}
1415
1416void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1417 CallExpr::arg_iterator AI,
1418 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001419 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001420 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001421
Ted Kremenek07baa252008-02-21 18:02:17 +00001422 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001423 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001424 // If the call argument is being bound to a reference parameter,
1425 // visit it as an lvalue, not an rvalue.
1426 bool VisitAsLvalue = false;
1427 if (Proto && ParamIdx < Proto->getNumArgs())
1428 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1429
1430 NodeSet DstTmp;
1431 if (VisitAsLvalue)
1432 VisitLValue(*AI, Pred, DstTmp);
1433 else
1434 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001435 ++AI;
1436
Ted Kremenek769f3482008-03-04 22:01:56 +00001437 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001438 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001439
1440 return;
1441 }
1442
1443 // If we reach here we have processed all of the arguments. Evaluate
1444 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001445
Ted Kremenekc71901d2008-02-25 21:16:03 +00001446 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001447 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001448
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001449 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001450
Ted Kremenekd9268e32008-02-19 01:44:53 +00001451 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001452 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1453
Ted Kremeneke66ba682009-02-13 01:45:31 +00001454 const GRState* state = GetState(*DI);
1455 SVal L = GetSVal(state, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001456
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001457 // FIXME: Add support for symbolic function calls (calls involving
1458 // function pointer values that are symbolic).
1459
1460 // Check for undefined control-flow or calls to NULL.
1461
Zhongxing Xu097fc982008-10-17 05:57:07 +00001462 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001463 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001464
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001465 if (N) {
1466 N->markAsSink();
1467 BadCalls.insert(N);
1468 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001469
Ted Kremenekd9268e32008-02-19 01:44:53 +00001470 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001471 }
1472
1473 // Check for the "noreturn" attribute.
1474
1475 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001476 const FunctionDecl* FD = L.getAsFunctionDecl();
1477 if (FD) {
Ted Kremenek1ce91f22009-04-10 00:01:14 +00001478 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +00001479 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001480 else {
1481 // HACK: Some functions are not marked noreturn, and don't return.
1482 // Here are a few hardwired ones. If this takes too long, we can
1483 // potentially cache these results.
1484 const char* s = FD->getIdentifier()->getName();
1485 unsigned n = strlen(s);
1486
1487 switch (n) {
1488 default:
1489 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001490
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001491 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001492 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1493 break;
1494
1495 case 5:
1496 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001497 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001498 if (CE->getNumArgs() > 0) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001499 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001500 // FIXME: use Assume to inspect the possible symbolic value of
1501 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001502 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001503 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001504 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001505 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001506 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001507 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001508
Ted Kremenek23271be2008-04-22 05:37:33 +00001509 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001510 if (!memcmp(s, "Assert", 6)) {
1511 Builder->BuildSinks = true;
1512 break;
1513 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001514
1515 // FIXME: This is just a wrapper around throwing an exception.
1516 // Eventually inter-procedural analysis should handle this easily.
1517 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1518
Ted Kremenek23271be2008-04-22 05:37:33 +00001519 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001520
1521 case 7:
1522 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1523 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001524
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001525 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001526 if (!memcmp(s ,"db_error", 8) ||
1527 !memcmp(s, "__assert", 8))
1528 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001529 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001530
1531 case 12:
1532 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1533 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001534
Ted Kremenek19903a22008-09-19 02:30:47 +00001535 case 13:
1536 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1537 break;
1538
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001539 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001540 if (!memcmp(s, "dtrace_assfail", 14) ||
1541 !memcmp(s, "yy_fatal_error", 14))
1542 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001543 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001544
1545 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001546 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001547 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1548 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001549 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001550
Ted Kremeneka46fea72008-05-17 00:33:23 +00001551 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001552 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001553
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001554 }
1555 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001556
1557 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001558
Zhongxing Xucac107a2009-04-20 05:24:46 +00001559 if (FD) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001560
Zhongxing Xucac107a2009-04-20 05:24:46 +00001561 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001562 switch (id) {
1563 case Builtin::BI__builtin_expect: {
1564 // For __builtin_expect, just return the value of the subexpression.
1565 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001566 SVal X = GetSVal(state, *(CE->arg_begin()));
1567 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001568 continue;
1569 }
1570
Ted Kremenek19891fa2008-11-02 00:35:01 +00001571 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001572 // FIXME: Refactor into StoreManager itself?
1573 MemRegionManager& RM = getStateManager().getRegionManager();
1574 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001575 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001576
1577 // Set the extent of the region in bytes. This enables us to use the
1578 // SVal of the argument directly. If we save the extent in bits, we
1579 // cannot represent values like symbol*8.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001580 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1581 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001582
Ted Kremeneke66ba682009-02-13 01:45:31 +00001583 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001584 continue;
1585 }
1586
Ted Kremenek21581c62008-03-05 22:59:42 +00001587 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001588 break;
1589 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001590 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001591
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001592 // Check any arguments passed-by-value against being undefined.
1593
1594 bool badArg = false;
1595
1596 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1597 I != E; ++I) {
1598
Zhongxing Xu097fc982008-10-17 05:57:07 +00001599 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001600 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001601
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001602 if (N) {
1603 N->markAsSink();
1604 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001605 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001606
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001607 badArg = true;
1608 break;
1609 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001610 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001611
1612 if (badArg)
1613 continue;
1614
1615 // Dispatch to the plug-in transfer function.
1616
1617 unsigned size = Dst.size();
1618 SaveOr OldHasGen(Builder->HasGeneratedNode);
1619 EvalCall(Dst, CE, L, *DI);
1620
1621 // Handle the case where no nodes where generated. Auto-generate that
1622 // contains the updated state if we aren't generating sinks.
1623
1624 if (!Builder->BuildSinks && Dst.size() == size &&
1625 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001626 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001627 }
1628}
1629
Ted Kremenekca5f6202008-04-15 23:06:53 +00001630//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001631// Transfer function: Objective-C ivar references.
1632//===----------------------------------------------------------------------===//
1633
Ted Kremenek9a48d862009-02-28 20:50:43 +00001634static std::pair<const void*,const void*> EagerlyAssumeTag
1635 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1636
Ted Kremenek34a611b2009-02-25 23:32:10 +00001637void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001638 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1639 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001640
1641 // Test if the previous node was as the same expression. This can happen
1642 // when the expression fails to evaluate to anything meaningful and
1643 // (as an optimization) we don't generate a node.
1644 ProgramPoint P = Pred->getLocation();
1645 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1646 Dst.Add(Pred);
1647 continue;
1648 }
1649
Ted Kremenek8f520972009-02-25 22:32:02 +00001650 const GRState* state = Pred->getState();
Ted Kremenek34a611b2009-02-25 23:32:10 +00001651 SVal V = GetSVal(state, Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001652 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001653 // First assume that the condition is true.
1654 bool isFeasible = false;
1655 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1656 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001657 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1658 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001659 stateTrue, Pred));
1660 }
1661
1662 // Next, assume that the condition is false.
1663 isFeasible = false;
1664 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1665 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001666 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1667 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001668 stateFalse, Pred));
1669 }
1670 }
1671 else
1672 Dst.Add(Pred);
1673 }
1674}
1675
1676//===----------------------------------------------------------------------===//
1677// Transfer function: Objective-C ivar references.
1678//===----------------------------------------------------------------------===//
1679
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001680void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1681 NodeTy* Pred, NodeSet& Dst,
1682 bool asLValue) {
1683
1684 Expr* Base = cast<Expr>(Ex->getBase());
1685 NodeSet Tmp;
1686 Visit(Base, Pred, Tmp);
1687
1688 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001689 const GRState* state = GetState(*I);
1690 SVal BaseVal = GetSVal(state, Base);
1691 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001692
1693 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001694 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001695 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001696 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001697 }
1698}
1699
1700//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001701// Transfer function: Objective-C fast enumeration 'for' statements.
1702//===----------------------------------------------------------------------===//
1703
1704void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1705 NodeTy* Pred, NodeSet& Dst) {
1706
1707 // ObjCForCollectionStmts are processed in two places. This method
1708 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1709 // statements within a basic block. This transfer function does two things:
1710 //
1711 // (1) binds the next container value to 'element'. This creates a new
1712 // node in the ExplodedGraph.
1713 //
1714 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1715 // whether or not the container has any more elements. This value
1716 // will be tested in ProcessBranch. We need to explicitly bind
1717 // this value because a container can contain nil elements.
1718 //
1719 // FIXME: Eventually this logic should actually do dispatches to
1720 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1721 // This will require simulating a temporary NSFastEnumerationState, either
1722 // through an SVal or through the use of MemRegions. This value can
1723 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1724 // terminates we reclaim the temporary (it goes out of scope) and we
1725 // we can test if the SVal is 0 or if the MemRegion is null (depending
1726 // on what approach we take).
1727 //
1728 // For now: simulate (1) by assigning either a symbol or nil if the
1729 // container is empty. Thus this transfer function will by default
1730 // result in state splitting.
1731
Ted Kremenek034a9472008-11-14 19:47:18 +00001732 Stmt* elem = S->getElement();
1733 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001734
1735 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001736 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001737 assert (ElemD->getInit() == 0);
Ted Kremenek034a9472008-11-14 19:47:18 +00001738 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1739 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1740 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001741 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001742
1743 NodeSet Tmp;
1744 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001745
Ted Kremenek034a9472008-11-14 19:47:18 +00001746 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1747 const GRState* state = GetState(*I);
1748 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1749 }
1750}
1751
1752void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1753 NodeTy* Pred, NodeSet& Dst,
1754 SVal ElementV) {
1755
1756
Ted Kremenek13e167f2008-11-12 19:24:17 +00001757
Ted Kremenek034a9472008-11-14 19:47:18 +00001758 // Get the current state. Use 'EvalLocation' to determine if it is a null
1759 // pointer, etc.
1760 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001761
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001762 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1763 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001764 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001765
1766 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek034a9472008-11-14 19:47:18 +00001767
Ted Kremenek13e167f2008-11-12 19:24:17 +00001768 // Handle the case where the container still has elements.
Ted Kremenek034a9472008-11-14 19:47:18 +00001769 QualType IntTy = getContext().IntTy;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001770 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1771 GRStateRef hasElems = state.BindExpr(S, TrueV);
1772
Ted Kremenek13e167f2008-11-12 19:24:17 +00001773 // Handle the case where the container has no elements.
Ted Kremenekd3789d72008-11-12 21:12:46 +00001774 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1775 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001776
1777 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1778 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1779 // FIXME: The proper thing to do is to really iterate over the
1780 // container. We will do this with dispatch logic to the store.
1781 // For now, just 'conjure' up a symbolic value.
Ted Kremenekf5da3252008-12-13 21:49:13 +00001782 QualType T = R->getRValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001783 assert (Loc::IsLocType(T));
1784 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001785 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1786 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1787 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001788
Ted Kremenek034a9472008-11-14 19:47:18 +00001789 // Bind the location to 'nil' on the false branch.
1790 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1791 noElems = noElems.BindLoc(ElementV, nilV);
1792 }
1793
Ted Kremenekd3789d72008-11-12 21:12:46 +00001794 // Create the new nodes.
1795 MakeNode(Dst, S, Pred, hasElems);
1796 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001797}
1798
1799//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001800// Transfer function: Objective-C message expressions.
1801//===----------------------------------------------------------------------===//
1802
1803void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1804 NodeSet& Dst){
1805
1806 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1807 Pred, Dst);
1808}
1809
1810void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001811 ObjCMessageExpr::arg_iterator AI,
1812 ObjCMessageExpr::arg_iterator AE,
1813 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001814 if (AI == AE) {
1815
1816 // Process the receiver.
1817
1818 if (Expr* Receiver = ME->getReceiver()) {
1819 NodeSet Tmp;
1820 Visit(Receiver, Pred, Tmp);
1821
1822 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1823 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1824
1825 return;
1826 }
1827
1828 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1829 return;
1830 }
1831
1832 NodeSet Tmp;
1833 Visit(*AI, Pred, Tmp);
1834
1835 ++AI;
1836
1837 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1838 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1839}
1840
1841void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1842 NodeTy* Pred,
1843 NodeSet& Dst) {
1844
1845 // FIXME: More logic for the processing the method call.
1846
Ted Kremeneke66ba682009-02-13 01:45:31 +00001847 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001848 bool RaisesException = false;
1849
Ted Kremenekca5f6202008-04-15 23:06:53 +00001850
1851 if (Expr* Receiver = ME->getReceiver()) {
1852
Ted Kremeneke66ba682009-02-13 01:45:31 +00001853 SVal L = GetSVal(state, Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001854
Ted Kremenek95a98252009-02-19 04:06:22 +00001855 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001856 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001857 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001858
1859 if (N) {
1860 N->markAsSink();
1861 UndefReceivers.insert(N);
1862 }
1863
1864 return;
1865 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001866
Ted Kremenek95a98252009-02-19 04:06:22 +00001867 // "Assume" that the receiver is not NULL.
1868 bool isFeasibleNotNull = false;
Ted Kremenekf2895872009-04-08 18:51:08 +00001869 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek95a98252009-02-19 04:06:22 +00001870
1871 // "Assume" that the receiver is NULL.
1872 bool isFeasibleNull = false;
1873 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1874
1875 if (isFeasibleNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001876 QualType RetTy = ME->getType();
1877
Ted Kremenek95a98252009-02-19 04:06:22 +00001878 // Check if the receiver was nil and the return value a struct.
Ted Kremenekb3323002009-04-09 05:45:56 +00001879 if(RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001880 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001881 // The [0 ...] expressions will return garbage. Flag either an
1882 // explicit or implicit error. Because of the structure of this
1883 // function we currently do not bifurfacte the state graph at
1884 // this point.
1885 // FIXME: We should bifurcate and fill the returned struct with
1886 // garbage.
1887 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1888 N->markAsSink();
1889 if (isFeasibleNotNull)
1890 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001891 else
Ted Kremenek23712182009-04-09 04:06:51 +00001892 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001893 }
1894 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001895 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001896 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001897 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001898 if (RetTy != Ctx.VoidTy) {
1899 if (BR.getParentMap().isConsumedExpr(ME)) {
1900 // sizeof(void *)
1901 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1902 // sizeof(return type)
1903 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001904
Ted Kremenekb3323002009-04-09 05:45:56 +00001905 if(voidPtrSize < returnTypeSize) {
1906 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1907 N->markAsSink();
1908 if(isFeasibleNotNull)
1909 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001910 else
Ted Kremenekb3323002009-04-09 05:45:56 +00001911 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001912 }
1913 }
1914 else if (!isFeasibleNotNull) {
1915 // Handle the safe cases where the return value is 0 if the
1916 // receiver is nil.
1917 //
1918 // FIXME: For now take the conservative approach that we only
1919 // return null values if we *know* that the receiver is nil.
1920 // This is because we can have surprises like:
1921 //
1922 // ... = [[NSScreens screens] objectAtIndex:0];
1923 //
1924 // What can happen is that [... screens] could return nil, but
1925 // it most likely isn't nil. We should assume the semantics
1926 // of this case unless we have *a lot* more knowledge.
1927 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001928 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekb3323002009-04-09 05:45:56 +00001929 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001930 return;
1931 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001932 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001933 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001934 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001935 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001936 // of this method should assume that the receiver is not nil.
1937 if (!StNotNull)
1938 return;
1939
Ted Kremenekf2895872009-04-08 18:51:08 +00001940 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001941 }
1942
Ted Kremenek5f20a632008-05-01 18:33:28 +00001943 // Check if the "raise" message was sent.
1944 if (ME->getSelector() == RaiseSel)
1945 RaisesException = true;
1946 }
1947 else {
1948
1949 IdentifierInfo* ClsName = ME->getClassName();
1950 Selector S = ME->getSelector();
1951
1952 // Check for special instance methods.
1953
1954 if (!NSExceptionII) {
1955 ASTContext& Ctx = getContext();
1956
1957 NSExceptionII = &Ctx.Idents.get("NSException");
1958 }
1959
1960 if (ClsName == NSExceptionII) {
1961
1962 enum { NUM_RAISE_SELECTORS = 2 };
1963
1964 // Lazily create a cache of the selectors.
1965
1966 if (!NSExceptionInstanceRaiseSelectors) {
1967
1968 ASTContext& Ctx = getContext();
1969
1970 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1971
1972 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1973 unsigned idx = 0;
1974
1975 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001976 II.push_back(&Ctx.Idents.get("raise"));
1977 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001978 NSExceptionInstanceRaiseSelectors[idx++] =
1979 Ctx.Selectors.getSelector(II.size(), &II[0]);
1980
1981 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001982 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001983 NSExceptionInstanceRaiseSelectors[idx++] =
1984 Ctx.Selectors.getSelector(II.size(), &II[0]);
1985 }
1986
1987 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1988 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1989 RaisesException = true; break;
1990 }
1991 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001992 }
1993
1994 // Check for any arguments that are uninitialized/undefined.
1995
1996 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1997 I != E; ++I) {
1998
Ted Kremeneke66ba682009-02-13 01:45:31 +00001999 if (GetSVal(state, *I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00002000
2001 // Generate an error node for passing an uninitialized/undefined value
2002 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002003 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002004
2005 if (N) {
2006 N->markAsSink();
2007 MsgExprUndefArgs[N] = *I;
2008 }
2009
2010 return;
2011 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00002012 }
2013
2014 // Check if we raise an exception. For now treat these as sinks. Eventually
2015 // we will want to handle exceptions properly.
2016
2017 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2018
2019 if (RaisesException)
2020 Builder->BuildSinks = true;
2021
Ted Kremenekca5f6202008-04-15 23:06:53 +00002022 // Dispatch to plug-in transfer function.
2023
2024 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002025 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002026
Ted Kremenekca5f6202008-04-15 23:06:53 +00002027 EvalObjCMessageExpr(Dst, ME, Pred);
2028
2029 // Handle the case where no nodes where generated. Auto-generate that
2030 // contains the updated state if we aren't generating sinks.
2031
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002032 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002033 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002034}
2035
2036//===----------------------------------------------------------------------===//
2037// Transfer functions: Miscellaneous statements.
2038//===----------------------------------------------------------------------===//
2039
Ted Kremenek16354a42009-01-13 01:04:21 +00002040void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2041 QualType PtrTy,
2042 Expr* CastE, NodeTy* Pred,
2043 NodeSet& Dst) {
2044 if (!V.isUnknownOrUndef()) {
2045 // FIXME: Determine if the number of bits of the target type is
2046 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00002047 // If not, flag an error.
Ted Kremenek52978eb2009-03-05 03:44:53 +00002048 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2049 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00002050 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002051 else
2052 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002053}
2054
2055
Ted Kremenek07baa252008-02-21 18:02:17 +00002056void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00002057 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002058 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002059 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002060
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002061 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002062 T = ExCast->getTypeAsWritten();
2063
Zhongxing Xu943909c2008-10-22 08:02:16 +00002064 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002065 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002066 else
2067 Visit(Ex, Pred, S1);
2068
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002069 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00002070 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002071 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002072 Dst.Add(*I1);
2073
Ted Kremenek54eddae2008-01-24 02:02:54 +00002074 return;
2075 }
2076
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002077 // FIXME: The rest of this should probably just go into EvalCall, and
2078 // let the transfer function object be responsible for constructing
2079 // nodes.
2080
Ted Kremenek07baa252008-02-21 18:02:17 +00002081 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00002082 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002083 const GRState* state = GetState(N);
2084 SVal V = GetSVal(state, Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002085 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002086
2087 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002088 if (V.isUnknown()) {
2089 Dst.Add(N);
2090 continue;
2091 }
2092
2093 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002094 if (V.isUndef())
2095 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00002096
2097 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00002098 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002099 C.getCanonicalType(ExTy).getUnqualifiedType())
2100 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002101
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002102 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002103 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002104 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002105 continue;
2106 }
2107
2108 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002109 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002110 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002111 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002112 V = LV->getLoc();
Ted Kremeneke66ba682009-02-13 01:45:31 +00002113 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002114 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002115 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002116
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002117 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002118 }
2119
2120 // Just pass through function and block pointers.
2121 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2122 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002123 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002124 }
2125
Ted Kremenek16354a42009-01-13 01:04:21 +00002126 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002127 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00002128 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00002129 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002130
2131 // Are we casting from an array to a pointer? If so just pass on
2132 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002133 if (T->isPointerType())
2134 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00002135
2136 // Are we casting from an array to an integer? If so, cast the decayed
2137 // pointer value to an integer.
2138 assert(T->isIntegerType());
2139 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2140 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002141 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002142 continue;
2143 }
2144
Ted Kremenekf5da3252008-12-13 21:49:13 +00002145 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00002146 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2147 // FIXME: For TypedViewRegions, we should handle the case where the
2148 // underlying symbolic pointer is a function pointer or
2149 // block pointer.
2150
2151 // FIXME: We should handle the case where we strip off view layers to get
2152 // to a desugared type.
2153
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002154 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00002155 // We get a symbolic function pointer for a dereference of a function
2156 // pointer, but it is of function type. Example:
2157
2158 // struct FPRec {
2159 // void (*my_func)(int * x);
2160 // };
2161 //
2162 // int bar(int x);
2163 //
2164 // int f1_a(struct FPRec* foo) {
2165 // int x;
2166 // (*foo->my_func)(&x);
2167 // return bar(x)+1; // no-warning
2168 // }
2169
2170 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002171
Ted Kremenekf5da3252008-12-13 21:49:13 +00002172 const MemRegion* R = RV->getRegion();
2173 StoreManager& StoreMgr = getStoreManager();
2174
2175 // Delegate to store manager to get the result of casting a region
2176 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002177 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00002178
2179 // Inspect the result. If the MemRegion* returned is NULL, this
2180 // expression evaluates to UnknownVal.
2181 R = Res.getRegion();
2182 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2183
2184 // Generate the new node in the ExplodedGraph.
2185 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00002186 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002187 }
Zhongxing Xu18bcec02009-04-10 06:06:13 +00002188 // All other cases.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002189 DispatchCast: {
2190 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2191 EvalCast(V, CastE->getType())));
2192 continue;
2193 }
2194
2195 PassThrough: {
2196 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2197 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002198 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002199}
2200
Ted Kremenekd83daa52008-10-27 21:54:31 +00002201void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002202 NodeTy* Pred, NodeSet& Dst,
2203 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002204 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2205 NodeSet Tmp;
2206 Visit(ILE, Pred, Tmp);
2207
2208 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002209 const GRState* state = GetState(*I);
2210 SVal ILV = GetSVal(state, ILE);
2211 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002212
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002213 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002214 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002215 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002216 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002217 }
2218}
2219
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002220void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002221
Ted Kremenek811af062008-10-06 18:43:53 +00002222 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002223 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002224
2225 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002226 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002227
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002228 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002229 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002230
2231 // FIXME: static variables may have an initializer, but the second
2232 // time a function is called those values may not be current.
2233 NodeSet Tmp;
2234
Ted Kremenek13e167f2008-11-12 19:24:17 +00002235 if (InitEx)
2236 Visit(InitEx, Pred, Tmp);
Ted Kremenek448ab622008-08-28 18:34:26 +00002237
2238 if (Tmp.empty())
2239 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002240
2241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002242 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002243 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002244
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002245 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2246 QualType T = getContext().getCanonicalType(VD->getType());
2247 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2248 // FIXME: Handle multi-dimensional VLAs.
2249
2250 Expr* SE = VLA->getSizeExpr();
2251 SVal Size = GetSVal(state, SE);
2252
2253 if (Size.isUndef()) {
2254 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2255 N->markAsSink();
2256 ExplicitBadSizedVLA.insert(N);
2257 }
2258 continue;
2259 }
2260
2261 bool isFeasibleZero = false;
2262 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2263
2264 bool isFeasibleNotZero = false;
2265 state = Assume(state, Size, true, isFeasibleNotZero);
2266
2267 if (isFeasibleZero) {
2268 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2269 N->markAsSink();
2270 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2271 else ExplicitBadSizedVLA.insert(N);
2272 }
2273 }
2274
2275 if (!isFeasibleNotZero)
2276 continue;
2277 }
2278
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002279 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002280 if (InitEx) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002281 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002282 QualType T = VD->getType();
2283
2284 // Recover some path-sensitivity if a scalar value evaluated to
2285 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002286 if (InitVal.isUnknown() ||
2287 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002288 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002289 }
2290
Ted Kremeneke66ba682009-02-13 01:45:31 +00002291 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002292
2293 // The next thing to do is check if the GRTransferFuncs object wants to
2294 // update the state based on the new binding. If the GRTransferFunc
2295 // object doesn't do anything, just auto-propagate the current state.
2296 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2297 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2298 InitVal);
2299 }
2300 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002301 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002302 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002303 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002304 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002305}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002306
Ted Kremeneke56ece22008-10-30 17:47:32 +00002307namespace {
2308 // This class is used by VisitInitListExpr as an item in a worklist
2309 // for processing the values contained in an InitListExpr.
2310class VISIBILITY_HIDDEN InitListWLItem {
2311public:
2312 llvm::ImmutableList<SVal> Vals;
2313 GRExprEngine::NodeTy* N;
2314 InitListExpr::reverse_iterator Itr;
2315
2316 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2317 InitListExpr::reverse_iterator itr)
2318 : Vals(vals), N(n), Itr(itr) {}
2319};
2320}
2321
2322
Zhongxing Xuebcad732008-10-30 05:02:23 +00002323void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2324 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002325
Zhongxing Xuebcad732008-10-30 05:02:23 +00002326 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002327 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002328 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002329
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002330 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002331
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002332 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002333
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002334 // Handle base case where the initializer has no elements.
2335 // e.g: static int* myArray[] = {};
2336 if (NumInitElements == 0) {
2337 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2338 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2339 return;
2340 }
2341
2342 // Create a worklist to process the initializers.
2343 llvm::SmallVector<InitListWLItem, 10> WorkList;
2344 WorkList.reserve(NumInitElements);
2345 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002346 InitListExpr::reverse_iterator ItrEnd = E->rend();
2347
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002348 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002349 while (!WorkList.empty()) {
2350 InitListWLItem X = WorkList.back();
2351 WorkList.pop_back();
2352
Zhongxing Xuebcad732008-10-30 05:02:23 +00002353 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002354 Visit(*X.Itr, X.N, Tmp);
2355
2356 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002357
Ted Kremeneke56ece22008-10-30 17:47:32 +00002358 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2359 // Get the last initializer value.
2360 state = GetState(*NI);
2361 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2362
2363 // Construct the new list of values by prepending the new value to
2364 // the already constructed list.
2365 llvm::ImmutableList<SVal> NewVals =
2366 getBasicVals().consVals(InitV, X.Vals);
2367
2368 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002369 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002370 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuebcad732008-10-30 05:02:23 +00002371
Ted Kremeneke56ece22008-10-30 17:47:32 +00002372 // Make final state and node.
Ted Kremenek78c06532008-10-30 18:37:08 +00002373 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002374 }
2375 else {
2376 // Still some initializer values to go. Push them onto the worklist.
2377 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2378 }
2379 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002380 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002381
2382 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002383 }
2384
Ted Kremenek79413a52008-11-13 06:10:40 +00002385 if (T->isUnionType() || T->isVectorType()) {
2386 // FIXME: to be implemented.
2387 // Note: That vectors can return true for T->isIntegerType()
2388 MakeNode(Dst, E, Pred, state);
2389 return;
2390 }
2391
Zhongxing Xuebcad732008-10-30 05:02:23 +00002392 if (Loc::IsLocType(T) || T->isIntegerType()) {
2393 assert (E->getNumInits() == 1);
2394 NodeSet Tmp;
2395 Expr* Init = E->getInit(0);
2396 Visit(Init, Pred, Tmp);
2397 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2398 state = GetState(*I);
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002399 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002400 }
2401 return;
2402 }
2403
Zhongxing Xuebcad732008-10-30 05:02:23 +00002404
2405 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2406 assert(0 && "unprocessed InitListExpr type");
2407}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002408
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002409/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2410void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2411 NodeTy* Pred,
2412 NodeSet& Dst) {
2413 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002414 uint64_t amt;
2415
2416 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002417 if (T == getContext().VoidTy) {
2418 // sizeof(void) == 1 byte.
2419 amt = 1;
2420 }
2421 else if (!T.getTypePtr()->isConstantSizeType()) {
2422 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002423 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002424 }
2425 else if (T->isObjCInterfaceType()) {
2426 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2427 // the compiler has laid out its representation. Just report Unknown
2428 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002429 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002430 }
2431 else {
2432 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002433 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002434 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002435 }
2436 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002437 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002438
Ted Kremenekf10f2882008-03-21 21:30:14 +00002439 MakeNode(Dst, Ex, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002440 BindExpr(GetState(Pred), Ex,
2441 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002442}
2443
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002444
Ted Kremenek07baa252008-02-21 18:02:17 +00002445void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002446 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002447
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002448 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002449
2450 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002451 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002452
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002453 case UnaryOperator::Deref: {
2454
2455 Expr* Ex = U->getSubExpr()->IgnoreParens();
2456 NodeSet Tmp;
2457 Visit(Ex, Pred, Tmp);
2458
2459 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002460
Ted Kremeneke66ba682009-02-13 01:45:31 +00002461 const GRState* state = GetState(*I);
2462 SVal location = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002463
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002464 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002465 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002466 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002467 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002468 }
2469
2470 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002471 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002472
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002473 case UnaryOperator::Real: {
2474
2475 Expr* Ex = U->getSubExpr()->IgnoreParens();
2476 NodeSet Tmp;
2477 Visit(Ex, Pred, Tmp);
2478
2479 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2480
Zhongxing Xu097fc982008-10-17 05:57:07 +00002481 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002482 if (Ex->getType()->isAnyComplexType()) {
2483 // Just report "Unknown."
2484 Dst.Add(*I);
2485 continue;
2486 }
2487
2488 // For all other types, UnaryOperator::Real is an identity operation.
2489 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002490 const GRState* state = GetState(*I);
2491 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002492 }
2493
2494 return;
2495 }
2496
2497 case UnaryOperator::Imag: {
2498
2499 Expr* Ex = U->getSubExpr()->IgnoreParens();
2500 NodeSet Tmp;
2501 Visit(Ex, Pred, Tmp);
2502
2503 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002504 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002505 if (Ex->getType()->isAnyComplexType()) {
2506 // Just report "Unknown."
2507 Dst.Add(*I);
2508 continue;
2509 }
2510
2511 // For all other types, UnaryOperator::Float returns 0.
2512 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002513 const GRState* state = GetState(*I);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002514 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002515 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002516 }
2517
2518 return;
2519 }
2520
2521 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002522 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002523 Dst.Add(Pred);
2524 return;
2525
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002526 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002527 case UnaryOperator::Extension: {
2528
2529 // Unary "+" is a no-op, similar to a parentheses. We still have places
2530 // where it may be a block-level expression, so we need to
2531 // generate an extra node that just propagates the value of the
2532 // subexpression.
2533
2534 Expr* Ex = U->getSubExpr()->IgnoreParens();
2535 NodeSet Tmp;
2536 Visit(Ex, Pred, Tmp);
2537
2538 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002539 const GRState* state = GetState(*I);
2540 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002541 }
2542
2543 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002544 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002545
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002546 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002547
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002548 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002549 Expr* Ex = U->getSubExpr()->IgnoreParens();
2550 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002551 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002552
2553 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002554 const GRState* state = GetState(*I);
2555 SVal V = GetSVal(state, Ex);
2556 state = BindExpr(state, U, V);
2557 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002558 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002559
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002560 return;
2561 }
2562
2563 case UnaryOperator::LNot:
2564 case UnaryOperator::Minus:
2565 case UnaryOperator::Not: {
2566
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002567 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002568 Expr* Ex = U->getSubExpr()->IgnoreParens();
2569 NodeSet Tmp;
2570 Visit(Ex, Pred, Tmp);
2571
2572 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002573 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002574
2575 // Get the value of the subexpression.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002576 SVal V = GetSVal(state, Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002577
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002578 if (V.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002579 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002580 continue;
2581 }
2582
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002583// QualType DstT = getContext().getCanonicalType(U->getType());
2584// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2585//
2586// if (DstT != SrcT) // Perform promotions.
2587// V = EvalCast(V, DstT);
2588//
2589// if (V.isUnknownOrUndef()) {
2590// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2591// continue;
2592// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002593
2594 switch (U->getOpcode()) {
2595 default:
2596 assert(false && "Invalid Opcode.");
2597 break;
2598
2599 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002600 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002601 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002602 break;
2603
2604 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002605 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002606 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002607 break;
2608
2609 case UnaryOperator::LNot:
2610
2611 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2612 //
2613 // Note: technically we do "E == 0", but this is the same in the
2614 // transfer functions as "0 == E".
2615
Zhongxing Xu097fc982008-10-17 05:57:07 +00002616 if (isa<Loc>(V)) {
Ted Kremenekf2895872009-04-08 18:51:08 +00002617 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremenek74556a12009-03-26 03:35:11 +00002618 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2619 U->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002620 state = BindExpr(state, U, Result);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002621 }
2622 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002623 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002624#if 0
Zhongxing Xu097fc982008-10-17 05:57:07 +00002625 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002626 state = SetSVal(state, U, Result);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002627#else
Ted Kremenek74556a12009-03-26 03:35:11 +00002628 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2629 U->getType());
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002630 continue;
2631#endif
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002632 }
2633
2634 break;
2635 }
2636
Ted Kremeneke66ba682009-02-13 01:45:31 +00002637 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002638 }
2639
2640 return;
2641 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002642 }
2643
2644 // Handle ++ and -- (both pre- and post-increment).
2645
2646 assert (U->isIncrementDecrementOp());
2647 NodeSet Tmp;
2648 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002649 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002650
2651 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2652
Ted Kremeneke66ba682009-02-13 01:45:31 +00002653 const GRState* state = GetState(*I);
2654 SVal V1 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002655
2656 // Perform a load.
2657 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002658 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002659
2660 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2661
Ted Kremeneke66ba682009-02-13 01:45:31 +00002662 state = GetState(*I2);
2663 SVal V2 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002664
2665 // Propagate unknown and undefined values.
2666 if (V2.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002667 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002668 continue;
2669 }
2670
Ted Kremeneke43de222009-03-11 03:54:24 +00002671 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002672 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2673 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002674
Ted Kremenek74556a12009-03-26 03:35:11 +00002675 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002676
2677 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002678 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002679 Result = ValMgr.getConjuredSymbolVal(Ex,
2680 Builder->getCurrentBlockCount());
Ted Kremenek65411632009-04-21 22:38:05 +00002681
2682 // If the value is a location, ++/-- should always preserve
2683 // non-nullness. Check if the original value was non-null, and if so propagate
2684 // that constraint.
2685 if (Loc::IsLocType(U->getType())) {
2686 SVal Constraint = EvalBinOp(BinaryOperator::EQ, V2,
2687 ValMgr.makeZeroVal(U->getType()),
2688 getContext().IntTy);
2689
2690 bool isFeasible = false;
2691 Assume(state, Constraint, true, isFeasible);
2692 if (!isFeasible) {
2693 // It isn't feasible for the original value to be null.
2694 // Propagate this constraint.
2695 Constraint = EvalBinOp(BinaryOperator::EQ, Result,
2696 ValMgr.makeZeroVal(U->getType()),
2697 getContext().IntTy);
2698
2699 bool isFeasible = false;
2700 state = Assume(state, Constraint, false, isFeasible);
2701 assert(isFeasible && state);
2702 }
2703 }
2704 }
Ted Kremenek607415e2009-03-20 20:10:45 +00002705
Ted Kremeneke66ba682009-02-13 01:45:31 +00002706 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002707
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002708 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002709 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002710 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002711 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002712}
2713
Ted Kremenek31803c32008-03-17 21:11:24 +00002714void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2715 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2716}
2717
2718void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2719 AsmStmt::outputs_iterator I,
2720 AsmStmt::outputs_iterator E,
2721 NodeTy* Pred, NodeSet& Dst) {
2722 if (I == E) {
2723 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2724 return;
2725 }
2726
2727 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002728 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002729
2730 ++I;
2731
2732 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2733 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2734}
2735
2736void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2737 AsmStmt::inputs_iterator I,
2738 AsmStmt::inputs_iterator E,
2739 NodeTy* Pred, NodeSet& Dst) {
2740 if (I == E) {
2741
2742 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002743 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002744
2745 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2746 // which interprets the inline asm and stores proper results in the
2747 // outputs.
2748
Ted Kremeneke66ba682009-02-13 01:45:31 +00002749 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002750
2751 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2752 OE = A->end_outputs(); OI != OE; ++OI) {
2753
Ted Kremeneke66ba682009-02-13 01:45:31 +00002754 SVal X = GetSVal(state, *OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002755 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002756
Zhongxing Xu097fc982008-10-17 05:57:07 +00002757 if (isa<Loc>(X))
Ted Kremeneke66ba682009-02-13 01:45:31 +00002758 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002759 }
2760
Ted Kremeneke66ba682009-02-13 01:45:31 +00002761 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002762 return;
2763 }
2764
2765 NodeSet Tmp;
2766 Visit(*I, Pred, Tmp);
2767
2768 ++I;
2769
2770 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2771 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2772}
2773
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002774void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2775 assert (Builder && "GRStmtNodeBuilder must be defined.");
2776
2777 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002778
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002779 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2780 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002781
Ted Kremenekc7469542008-07-17 23:15:45 +00002782 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002783
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002784 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002785
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002786 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002787 MakeNode(Dst, S, Pred, GetState(Pred));
2788}
2789
Ted Kremenek108048c2008-03-31 15:02:58 +00002790void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2791
2792 Expr* R = S->getRetValue();
2793
2794 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002795 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002796 return;
2797 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002798
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002799 NodeSet Tmp;
2800 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002801
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002802 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2803 SVal X = GetSVal((*I)->getState(), R);
2804
2805 // Check if we return the address of a stack variable.
2806 if (isa<loc::MemRegionVal>(X)) {
2807 // Determine if the value is on the stack.
2808 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002809
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002810 if (R && getStateManager().hasStackStorage(R)) {
2811 // Create a special node representing the error.
2812 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2813 N->markAsSink();
2814 RetsStackAddr.insert(N);
2815 }
2816 continue;
2817 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002818 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002819 // Check if we return an undefined value.
2820 else if (X.isUndef()) {
2821 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2822 N->markAsSink();
2823 RetsUndef.insert(N);
2824 }
2825 continue;
2826 }
2827
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002828 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002829 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002830}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002831
Ted Kremenekca5f6202008-04-15 23:06:53 +00002832//===----------------------------------------------------------------------===//
2833// Transfer functions: Binary operators.
2834//===----------------------------------------------------------------------===//
2835
Ted Kremeneke66ba682009-02-13 01:45:31 +00002836const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002837 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002838
2839 // Divide by undefined? (potentially zero)
2840
2841 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002842 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002843
2844 if (DivUndef) {
2845 DivUndef->markAsSink();
2846 ExplicitBadDivides.insert(DivUndef);
2847 }
2848
Ted Kremenek6c438f82008-10-20 23:40:25 +00002849 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002850 }
2851
2852 // Check for divide/remainder-by-zero.
2853 // First, "assume" that the denominator is 0 or undefined.
2854
2855 bool isFeasibleZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002856 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002857
2858 // Second, "assume" that the denominator cannot be 0.
2859
2860 bool isFeasibleNotZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002861 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002862
2863 // Create the node for the divide-by-zero (if it occurred).
2864
2865 if (isFeasibleZero)
2866 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2867 DivZeroNode->markAsSink();
2868
2869 if (isFeasibleNotZero)
2870 ImplicitBadDivides.insert(DivZeroNode);
2871 else
2872 ExplicitBadDivides.insert(DivZeroNode);
2873
2874 }
2875
Ted Kremeneke66ba682009-02-13 01:45:31 +00002876 return isFeasibleNotZero ? state : 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002877}
2878
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002879void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002880 GRExprEngine::NodeTy* Pred,
2881 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002882
2883 NodeSet Tmp1;
2884 Expr* LHS = B->getLHS()->IgnoreParens();
2885 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002886
Ted Kremenek52510d82008-12-06 02:39:30 +00002887 // FIXME: Add proper support for ObjCKVCRefExpr.
2888 if (isa<ObjCKVCRefExpr>(LHS)) {
2889 Visit(RHS, Pred, Dst);
2890 return;
2891 }
2892
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002893 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002894 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002895 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002896 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002897
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002898 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002899
Zhongxing Xu097fc982008-10-17 05:57:07 +00002900 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002901
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002902 // Process the RHS.
2903
2904 NodeSet Tmp2;
2905 Visit(RHS, *I1, Tmp2);
2906
2907 // With both the LHS and RHS evaluated, process the operation itself.
2908
2909 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002910
Ted Kremeneke66ba682009-02-13 01:45:31 +00002911 const GRState* state = GetState(*I2);
2912 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002913
Ted Kremeneke66ba682009-02-13 01:45:31 +00002914 SVal RightV = GetSVal(state, RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002915 BinaryOperator::Opcode Op = B->getOpcode();
2916
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002917 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002918
Ted Kremenekf031b872008-01-23 19:59:44 +00002919 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002920
Ted Kremenekd4676512008-03-12 21:45:47 +00002921 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002922 // FIXME: Handle structs.
2923 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002924
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002925 if ((RightV.isUnknown() ||
2926 !getConstraintManager().canReasonAbout(RightV))
2927 && (Loc::IsLocType(T) ||
2928 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002929 unsigned Count = Builder->getCurrentBlockCount();
2930 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002931 }
2932
Ted Kremenekd4676512008-03-12 21:45:47 +00002933 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002934 // to the L-Value represented by the LHS.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002935 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2936 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002937 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002938 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002939
2940 case BinaryOperator::Div:
2941 case BinaryOperator::Rem:
2942
Ted Kremenek6c438f82008-10-20 23:40:25 +00002943 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002944 if (RHS->getType()->isIntegerType() &&
2945 RHS->getType()->isScalarType()) {
2946
Ted Kremeneke66ba682009-02-13 01:45:31 +00002947 state = CheckDivideZero(B, state, *I2, RightV);
2948 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002949 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002950
2951 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002952
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002953 default: {
2954
2955 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002956 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002957
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002958 // Process non-assignements except commas or short-circuited
2959 // logical expressions (LAnd and LOr).
Ted Kremenek07baa252008-02-21 18:02:17 +00002960
Ted Kremenek74556a12009-03-26 03:35:11 +00002961 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002962
2963 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002964 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002965 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002966 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002967 }
2968 else
2969 Dst.Add(*I2);
2970
Ted Kremenekb8782e12008-02-21 19:15:37 +00002971 continue;
2972 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002973
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002974 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002975
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002976 // The operands were *not* undefined, but the result is undefined.
2977 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002978
Ted Kremeneke66ba682009-02-13 01:45:31 +00002979 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002980 UndefNode->markAsSink();
2981 UndefResults.insert(UndefNode);
2982 }
2983
2984 continue;
2985 }
2986
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002987 // Otherwise, create a new node.
2988
Ted Kremeneke66ba682009-02-13 01:45:31 +00002989 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002990 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002991 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002992 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002993
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002994 assert (B->isCompoundAssignmentOp());
2995
Ted Kremenek570882a2009-02-07 00:52:24 +00002996 switch (Op) {
2997 default:
2998 assert(0 && "Invalid opcode for compound assignment.");
2999 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3000 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3001 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3002 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3003 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3004 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3005 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3006 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3007 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3008 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00003009 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003010
3011 // Perform a load (the LHS). This performs the checks for
3012 // null dereferences, and so on.
3013 NodeSet Tmp3;
Ted Kremeneke66ba682009-02-13 01:45:31 +00003014 SVal location = GetSVal(state, LHS);
3015 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003016
3017 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3018
Ted Kremeneke66ba682009-02-13 01:45:31 +00003019 state = GetState(*I3);
3020 SVal V = GetSVal(state, LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003021
Ted Kremenek6c438f82008-10-20 23:40:25 +00003022 // Check for divide-by-zero.
3023 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00003024 && RHS->getType()->isIntegerType()
3025 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00003026
3027 // CheckDivideZero returns a new state where the denominator
3028 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003029 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00003030
Ted Kremeneke66ba682009-02-13 01:45:31 +00003031 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00003032 continue;
3033 }
3034
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003035 // Propagate undefined values (left-side).
3036 if (V.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00003037 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003038 continue;
3039 }
3040
3041 // Propagate unknown values (left and right-side).
3042 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00003043 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3044 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003045 continue;
3046 }
3047
3048 // At this point:
3049 //
3050 // The LHS is not Undef/Unknown.
3051 // The RHS is not Unknown.
3052
3053 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00003054 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003055 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00003056
3057 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3058 CLHSTy = getContext().getCanonicalType(CTy);
3059
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003060 QualType LTy = getContext().getCanonicalType(LHS->getType());
3061 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00003062
3063 // Promote LHS.
3064 V = EvalCast(V, CLHSTy);
3065
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003066 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00003067 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00003068 // Propagate undefined values (right-side).
Ted Kremenek3f755632009-03-05 03:42:31 +00003069 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003070 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003071 continue;
3072 }
3073
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003074 // Compute the result of the operation.
Ted Kremenek74556a12009-03-26 03:35:11 +00003075 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003076
3077 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003078 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003079 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003080 UndefNode->markAsSink();
3081 UndefResults.insert(UndefNode);
3082 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003083 continue;
3084 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003085
3086 // EXPERIMENTAL: "Conjured" symbols.
3087 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003088
3089 SVal LHSVal;
3090
Ted Kremenekd6a5a422009-03-11 02:24:48 +00003091 if ((Result.isUnknown() ||
3092 !getConstraintManager().canReasonAbout(Result))
3093 && (Loc::IsLocType(CTy)
3094 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00003095
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003096 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003097
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003098 // The symbolic value is actually for the type of the left-hand side
3099 // expression, not the computation type, as this is the value the
3100 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00003101 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003102
Zhongxing Xu5c70c772008-11-23 05:52:28 +00003103 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003104 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003105 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003106 else {
3107 // The left-hand side may bind to a different value then the
3108 // computation type.
3109 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3110 }
3111
Ted Kremeneke66ba682009-02-13 01:45:31 +00003112 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3113 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003114 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00003115 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003116 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003117}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003118
3119//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003120// Transfer-function Helpers.
3121//===----------------------------------------------------------------------===//
3122
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003123void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003124 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +00003125 NonLoc L, NonLoc R,
Ted Kremenek74556a12009-03-26 03:35:11 +00003126 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003127
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003128 GRStateSet OStates;
Ted Kremenek74556a12009-03-26 03:35:11 +00003129 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003130
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003131 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003132 MakeNode(Dst, Ex, Pred, *I);
3133}
3134
Ted Kremeneke66ba682009-02-13 01:45:31 +00003135void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003136 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +00003137 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003138
Ted Kremeneke66ba682009-02-13 01:45:31 +00003139 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremenek74556a12009-03-26 03:35:11 +00003140 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003141}
3142
Ted Kremenek74556a12009-03-26 03:35:11 +00003143SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
3144 QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00003145
3146 if (L.isUndef() || R.isUndef())
3147 return UndefinedVal();
3148
3149 if (L.isUnknown() || R.isUnknown())
3150 return UnknownVal();
3151
3152 if (isa<Loc>(L)) {
3153 if (isa<Loc>(R))
3154 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3155 else
3156 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
3157 }
3158
3159 if (isa<Loc>(R)) {
3160 // Support pointer arithmetic where the increment/decrement operand
3161 // is on the left and the pointer on the right.
3162
3163 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3164
3165 // Commute the operands.
3166 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
3167 cast<NonLoc>(L));
3168 }
3169 else
3170 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremenek74556a12009-03-26 03:35:11 +00003171 cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003172}
3173
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003174//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00003175// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003176//===----------------------------------------------------------------------===//
3177
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003178#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003179static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003180static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00003181
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003182namespace llvm {
3183template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003184struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003185 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00003186
Ted Kremeneka853de62008-02-14 22:54:53 +00003187 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3188
3189 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00003190 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00003191 GraphPrintCheckerState->isUndefDeref(N) ||
3192 GraphPrintCheckerState->isUndefStore(N) ||
3193 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00003194 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3195 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00003196 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00003197 GraphPrintCheckerState->isBadCall(N) ||
3198 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00003199 return "color=\"red\",style=\"filled\"";
3200
Ted Kremenekc2d07202008-02-28 20:32:03 +00003201 if (GraphPrintCheckerState->isNoReturnCall(N))
3202 return "color=\"blue\",style=\"filled\"";
3203
Ted Kremeneka853de62008-02-14 22:54:53 +00003204 return "";
3205 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003206
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003207 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003208 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003209
3210 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003211 ProgramPoint Loc = N->getLocation();
3212
3213 switch (Loc.getKind()) {
3214 case ProgramPoint::BlockEntranceKind:
3215 Out << "Block Entrance: B"
3216 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3217 break;
3218
3219 case ProgramPoint::BlockExitKind:
3220 assert (false);
3221 break;
3222
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003223 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003224 if (isa<PostStmt>(Loc)) {
3225 const PostStmt& L = cast<PostStmt>(Loc);
3226 Stmt* S = L.getStmt();
3227 SourceLocation SLoc = S->getLocStart();
3228
3229 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3230 llvm::raw_os_ostream OutS(Out);
3231 S->printPretty(OutS);
3232 OutS.flush();
3233
3234 if (SLoc.isFileID()) {
3235 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003236 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3237 << " col="
3238 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3239 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003240 }
3241
3242 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3243 Out << "\\|Implicit-Null Dereference.\\l";
3244 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3245 Out << "\\|Explicit-Null Dereference.\\l";
3246 else if (GraphPrintCheckerState->isUndefDeref(N))
3247 Out << "\\|Dereference of undefialied value.\\l";
3248 else if (GraphPrintCheckerState->isUndefStore(N))
3249 Out << "\\|Store to Undefined Loc.";
3250 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3251 Out << "\\|Explicit divide-by zero or undefined value.";
3252 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3253 Out << "\\|Implicit divide-by zero or undefined value.";
3254 else if (GraphPrintCheckerState->isUndefResult(N))
3255 Out << "\\|Result of operation is undefined.";
3256 else if (GraphPrintCheckerState->isNoReturnCall(N))
3257 Out << "\\|Call to function marked \"noreturn\".";
3258 else if (GraphPrintCheckerState->isBadCall(N))
3259 Out << "\\|Call to NULL/Undefined.";
3260 else if (GraphPrintCheckerState->isUndefArg(N))
3261 Out << "\\|Argument in call is undefined";
3262
3263 break;
3264 }
3265
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003266 const BlockEdge& E = cast<BlockEdge>(Loc);
3267 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3268 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003269
3270 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003271
3272 SourceLocation SLoc = T->getLocStart();
3273
Ted Kremenek90960972008-01-30 23:03:39 +00003274 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003275
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003276 llvm::raw_os_ostream OutS(Out);
3277 E.getSrc()->printTerminator(OutS);
3278 OutS.flush();
Ted Kremenek90960972008-01-30 23:03:39 +00003279
Ted Kremenekf97c6682008-03-09 03:30:59 +00003280 if (SLoc.isFileID()) {
3281 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003282 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3283 << " col="
3284 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003285 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003286
Ted Kremenekaee121c2008-02-13 23:08:21 +00003287 if (isa<SwitchStmt>(T)) {
3288 Stmt* Label = E.getDst()->getLabel();
3289
3290 if (Label) {
3291 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3292 Out << "\\lcase ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003293 llvm::raw_os_ostream OutS(Out);
3294 C->getLHS()->printPretty(OutS);
3295 OutS.flush();
3296
Ted Kremenekaee121c2008-02-13 23:08:21 +00003297 if (Stmt* RHS = C->getRHS()) {
3298 Out << " .. ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003299 RHS->printPretty(OutS);
3300 OutS.flush();
Ted Kremenekaee121c2008-02-13 23:08:21 +00003301 }
3302
3303 Out << ":";
3304 }
3305 else {
3306 assert (isa<DefaultStmt>(Label));
3307 Out << "\\ldefault:";
3308 }
3309 }
3310 else
3311 Out << "\\l(implicit) default:";
3312 }
3313 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003314 // FIXME
3315 }
3316 else {
3317 Out << "\\lCondition: ";
3318 if (*E.getSrc()->succ_begin() == E.getDst())
3319 Out << "true";
3320 else
3321 Out << "false";
3322 }
3323
3324 Out << "\\l";
3325 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003326
Ted Kremenekb31af242008-02-28 09:25:22 +00003327 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3328 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003329 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003330 }
3331 }
3332
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003333 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003334
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003335 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3336 state.printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003337
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003338 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003339 return Out.str();
3340 }
3341};
3342} // end llvm namespace
3343#endif
3344
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003345#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003346template <typename ITERATOR>
3347GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3348
3349template <>
3350GRExprEngine::NodeTy*
3351GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3352 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3353 return I->first;
3354}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003355#endif
3356
3357void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003358#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003359 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003360 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003361
3362 // Flush any outstanding reports to make sure we cover all the nodes.
3363 // This does not cause them to get displayed.
3364 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3365 const_cast<BugType*>(*I)->FlushReports(BR);
3366
3367 // Iterate through the reports and get their nodes.
3368 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3369 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3370 const BugReportEquivClass& EQ = *I2;
3371 const BugReport &R = **EQ.begin();
3372 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3373 if (N) Src.push_back(N);
3374 }
3375 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003376
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003377 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003378 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003379 else {
3380 GraphPrintCheckerState = this;
3381 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003382
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003383 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003384
3385 GraphPrintCheckerState = NULL;
3386 GraphPrintSourceManager = NULL;
3387 }
3388#endif
3389}
3390
3391void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3392#ifndef NDEBUG
3393 GraphPrintCheckerState = this;
3394 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003395
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003396 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003397
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003398 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003399 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003400 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003401 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003402
Ted Kremenek428d39e2008-01-30 23:24:39 +00003403 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003404 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003405#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003406}