blob: 87432d4815a847f9f15b9d7b01574b7dc1cba30d [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek50df4f42008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek0e80dea2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000025#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000026#include "llvm/ADT/ImmutableList.h"
27#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000029
Ted Kremenek9f6b1612008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
32#include <sstream>
33#endif
34
Ted Kremenekd4467432008-02-14 22:16:04 +000035using namespace clang;
36using llvm::dyn_cast;
37using llvm::cast;
38using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000039
Ted Kremenekca5f6202008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41// Engine construction and deletion.
42//===----------------------------------------------------------------------===//
43
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000044namespace {
45
46class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
47 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
48 typedef llvm::DenseMap<void*,Checks> MapTy;
49
50 MapTy M;
51 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000052 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000053
54public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000055 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
56 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000057
58 virtual ~MappedBatchAuditor() {
59 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
60
61 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
62 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
63
64 GRSimpleAPICheck* check = *I;
65
66 if (AlreadyVisited.count(check))
67 continue;
68
69 AlreadyVisited.insert(check);
70 delete check;
71 }
72 }
73
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000074 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000075 assert (A && "Check cannot be null.");
76 void* key = reinterpret_cast<void*>((uintptr_t) C);
77 MapTy::iterator I = M.find(key);
78 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
79 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000080
81 void AddCheck(GRSimpleAPICheck *A) {
82 assert (A && "Check cannot be null.");
83 AllStmts = F.Concat(A, AllStmts);
84 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000085
Ted Kremenekabd89ac2008-08-13 04:27:00 +000086 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000087 // First handle the auditors that accept all statements.
88 bool isSink = false;
89 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
90 isSink |= (*I)->Audit(N, VMgr);
91
92 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000093 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
94 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
95 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000096 if (MI != M.end()) {
97 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
98 isSink |= (*I)->Audit(N, VMgr);
99 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000100
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremenek5f20a632008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekf973eb02008-03-09 18:05:48 +0000116
Ted Kremenek1607f512008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000118 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000119 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000120 StoreManagerCreator SMC,
121 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000122 : CoreEngine(cfg, CD, Ctx, *this),
123 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000124 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000125 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000126 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000127 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000128 ValMgr(StateMgr.getValueManager()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000129 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000130 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
131 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000132 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000133 BR(BRD, *this),
134 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000135
Ted Kremenek72f52c02008-06-20 21:45:25 +0000136GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000137 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000138 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000139}
140
Ted Kremenekca5f6202008-04-15 23:06:53 +0000141//===----------------------------------------------------------------------===//
142// Utility methods.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000145
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000146void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000147 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000148 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000149 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000150}
151
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000152void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
153 if (!BatchAuditor)
154 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
155
156 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000157}
158
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000159void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
160 if (!BatchAuditor)
161 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
162
163 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
164}
165
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000166const GRState* GRExprEngine::getInitialState() {
Ted Kremenek04c0add2009-04-10 00:59:50 +0000167 const GRState *state = StateMgr.getInitialState();
168
169 // Precondition: the first argument of 'main' is an integer guaranteed
170 // to be > 0.
171 // FIXME: It would be nice if we had a more general mechanism to add
172 // such preconditions. Some day.
173 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
174 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
175 FD->getNumParams() > 0) {
176 const ParmVarDecl *PD = FD->getParamDecl(0);
177 QualType T = PD->getType();
178 if (T->isIntegerType())
179 if (const MemRegion *R = StateMgr.getRegion(PD)) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000180 SVal V = state->getSVal(loc::MemRegionVal(R));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000181 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek04c0add2009-04-10 00:59:50 +0000182 ValMgr.makeZeroVal(T),
183 getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +0000184
185 if (const GRState *newState = state->assume(Constraint, true))
186 state = newState;
Ted Kremenek04c0add2009-04-10 00:59:50 +0000187 }
188 }
189
190 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000191}
192
Ted Kremenekca5f6202008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek820c73b2009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremenekca5f6202008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000213
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000215 SymbolReaper SymReaper(Liveness, SymMgr);
216 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt, SymReaper)
218 : EntryNode->getState();
219
Ted Kremenek7487f942008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000222
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenekf05eec42008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenekc7469542008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000237 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek7487f942008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek7487f942008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek7487f942008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000249 Visit(S, *I, Dst);
250
251 // Do we need to auto-generate a node? We only need to do this to generate
252 // a node with a "cleaned" state; GRCoreEngine will actually handle
253 // auto-transitions for other cases.
254 if (Dst.size() == 1 && *Dst.begin() == EntryNode
255 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
256 HasAutoGenerated = true;
257 builder.generateNode(S, GetState(EntryNode), *I);
258 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000259 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000260
Ted Kremenekca5f6202008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenekfa7be362008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000270}
271
Ted Kremenek820c73b2009-03-11 02:41:36 +0000272void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
273 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
274 S->getLocStart(),
275 "Error evaluating statement");
276
Ted Kremenekca5f6202008-04-15 23:06:53 +0000277 // FIXME: add metadata to the CFG so that we can disable
278 // this check when we KNOW that there is no block-level subexpression.
279 // The motivation is that this check requires a hashtable lookup.
280
281 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
282 Dst.Add(Pred);
283 return;
284 }
285
286 switch (S->getStmtClass()) {
287
288 default:
289 // Cases we intentionally have "default" handle:
290 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
291
292 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
293 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000298
299 case Stmt::AsmStmtClass:
300 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
301 break;
302
303 case Stmt::BinaryOperatorClass: {
304 BinaryOperator* B = cast<BinaryOperator>(S);
305
306 if (B->isLogicalOp()) {
307 VisitLogicalExpr(B, Pred, Dst);
308 break;
309 }
310 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000312 MakeNode(Dst, B, Pred, state->bindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000315
Ted Kremenek8f520972009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremenekca5f6202008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000326
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000331 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000332 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000333
Ted Kremenekca5f6202008-04-15 23:06:53 +0000334 // FIXME: ChooseExpr is really a constant. We need to fix
335 // the CFG do not model them as explicit control-flow.
336
337 case Stmt::ChooseExprClass: { // __builtin_choose_expr
338 ChooseExpr* C = cast<ChooseExpr>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::CompoundAssignOperatorClass:
344 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
345 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000350
351 case Stmt::ConditionalOperatorClass: { // '?' operator
352 ConditionalOperator* C = cast<ConditionalOperator>(S);
353 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
354 break;
355 }
356
357 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000376
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000388
Ted Kremenekca5f6202008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenek3c186252008-12-09 20:18:58 +0000394 case Stmt::ObjCAtThrowStmtClass: {
395 // FIXME: This is not complete. We basically treat @throw as
396 // an abort.
397 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
398 Builder->BuildSinks = true;
399 MakeNode(Dst, S, Pred, GetState(Pred));
400 break;
401 }
402
Ted Kremenekca5f6202008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000417
418 if (SE->getSubStmt()->body_empty()) {
419 // Empty statement expression.
420 assert(SE->getType() == getContext().VoidTy
421 && "Empty statement expression must have void type.");
422 Dst.Add(Pred);
423 break;
424 }
425
426 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
427 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000428 MakeNode(Dst, SE, Pred, state->bindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000429 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000439
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000440 case Stmt::UnaryOperatorClass: {
441 UnaryOperator *U = cast<UnaryOperator>(S);
442 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
443 NodeSet Tmp;
444 VisitUnaryOperator(U, Pred, Tmp, false);
445 EvalEagerlyAssume(Dst, Tmp, U);
446 }
447 else
448 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000449 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000450 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000455
456 Ex = Ex->IgnoreParens();
457
458 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
459 Dst.Add(Pred);
460 return;
461 }
462
463 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000464
465 case Stmt::ArraySubscriptExprClass:
466 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000478 case Stmt::UnaryOperatorClass:
479 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
480 return;
481
482 case Stmt::MemberExprClass:
483 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
484 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000485
Ted Kremenekd83daa52008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000488 return;
489
Ted Kremenek71c707b2008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek93cb3d12009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000492 // FIXME: Property assignments are lvalues, but not really "locations".
493 // e.g.: self.x = something;
494 // Here the "self.x" really can translate to a method call (setter) when
495 // the assignment is made. Moreover, the entire assignment expression
496 // evaluate to whatever "something" is, not calling the "getter" for
497 // the property (which would make sense since it can have side effects).
498 // We'll probably treat this as a location, but not one that we can
499 // take the address of. Perhaps we need a new SVal class for cases
500 // like thsis?
501 // Note that we have a similar problem for bitfields, since they don't
502 // have "locations" in the sense that we can take their address.
503 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000504 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000508 SVal V = state->getLValue(cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000512
Ted Kremenek2c829a32008-10-18 04:15:35 +0000513 default:
514 // Arbitrary subexpressions can return aggregate temporaries that
515 // can be used in a lvalue context. We need to enhance our support
516 // of such temporaries in both the environment and the store, so right
517 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000521
Ted Kremenek2c829a32008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000537// Generic node creation.
538//===----------------------------------------------------------------------===//
539
540GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
541 NodeTy* Pred,
542 const GRState* St,
543 ProgramPoint::Kind K,
544 const void *tag) {
545
546 assert (Builder && "GRStmtNodeBuilder not present.");
547 SaveAndRestore<const void*> OldTag(Builder->Tag);
548 Builder->Tag = tag;
549 return Builder->MakeNode(Dst, S, Pred, St, K);
550}
551
552//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneke66ba682009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000562 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000563
564 case Stmt::BinaryOperatorClass: { // '&&' and '||'
565
566 BinaryOperator* B = cast<BinaryOperator>(Terminator);
567 BinaryOperator::Opcode Op = B->getOpcode();
568
569 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
570
571 // For &&, if we take the true branch, then the value of the whole
572 // expression is that of the RHS expression.
573 //
574 // For ||, if we take the false branch, then the value of the whole
575 // expression is that of the RHS expression.
576
577 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
578 (Op == BinaryOperator::LOr && !branchTaken)
579 ? B->getRHS() : B->getLHS();
580
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000581 return state->bindBlkExpr(B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000582 }
583
584 case Stmt::ConditionalOperatorClass: { // ?:
585
586 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
587
588 // For ?, if branchTaken == true then the value is either the LHS or
589 // the condition itself. (GNU extension).
590
591 Expr* Ex;
592
593 if (branchTaken)
594 Ex = C->getLHS() ? C->getLHS() : C->getCond();
595 else
596 Ex = C->getRHS();
597
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000598 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000599 }
600
601 case Stmt::ChooseExprClass: { // ?:
602
603 ChooseExpr* C = cast<ChooseExpr>(Terminator);
604
605 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000606 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenekc39c2172009-03-13 16:32:54 +0000611/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
612/// to try to recover some path-sensitivity for casts of symbolic
613/// integers that promote their values (which are currently not tracked well).
614/// This function returns the SVal bound to Condition->IgnoreCasts if all the
615// cast(s) did was sign-extend the original value.
616static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
617 Stmt* Condition, ASTContext& Ctx) {
618
619 Expr *Ex = dyn_cast<Expr>(Condition);
620 if (!Ex)
621 return UnknownVal();
622
623 uint64_t bits = 0;
624 bool bitsInit = false;
625
626 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
627 QualType T = CE->getType();
628
629 if (!T->isIntegerType())
630 return UnknownVal();
631
632 uint64_t newBits = Ctx.getTypeSize(T);
633 if (!bitsInit || newBits < bits) {
634 bitsInit = true;
635 bits = newBits;
636 }
637
638 Ex = CE->getSubExpr();
639 }
640
641 // We reached a non-cast. Is it a symbolic value?
642 QualType T = Ex->getType();
643
644 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
645 return UnknownVal();
646
Ted Kremenekc0cccca2009-06-18 23:58:37 +0000647 return state->getSVal(Ex);
Ted Kremenekc39c2172009-03-13 16:32:54 +0000648}
649
Ted Kremenek13e167f2008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000652
Ted Kremenek17c5f112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000656
Ted Kremenek022b6052008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremeneke43de222009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000667 SVal V = PrevState->getSVal(Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenekc39c2172009-03-13 16:32:54 +0000673 case SVal::UnknownKind: {
674 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
675 if (Ex->getType()->isIntegerType()) {
676 // Try to recover some path-sensitivity. Right now casts of symbolic
677 // integers that promote their values are currently not tracked well.
678 // If 'Condition' is such an expression, try and recover the
679 // underlying value and use that instead.
680 SVal recovered = RecoverCastedSymbol(getStateManager(),
681 builder.getState(), Condition,
682 getContext());
683
684 if (!recovered.isUnknown()) {
685 V = recovered;
686 break;
687 }
688 }
689 }
690
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000693 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000694 }
Ted Kremenek90960972008-01-30 23:03:39 +0000695
Zhongxing Xu097fc982008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000708
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000710 if (const GRState *state = PrevState->assume(V, true))
Ted Kremeneke66ba682009-02-13 01:45:31 +0000711 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000712 else
713 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000714
715 // Process the false branch.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000716 if (const GRState *state = PrevState->assume(V, false))
Ted Kremeneke66ba682009-02-13 01:45:31 +0000717 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000718 else
719 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000720}
721
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000722/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000723/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000724void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000725
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000726 const GRState *state = builder.getState();
727 SVal V = state->getSVal(builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000728
729 // Three possibilities:
730 //
731 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000732 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000733 // (3) We have no clue about the label. Dispatch to all targets.
734 //
735
736 typedef IndirectGotoNodeBuilder::iterator iterator;
737
Zhongxing Xu097fc982008-10-17 05:57:07 +0000738 if (isa<loc::GotoLabel>(V)) {
739 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000740
741 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000742 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000743 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000744 return;
745 }
746 }
747
748 assert (false && "No block with label.");
749 return;
750 }
751
Zhongxing Xu097fc982008-10-17 05:57:07 +0000752 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000753 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000754 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000755 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000756 return;
757 }
758
759 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekcdc3a3c2009-04-23 17:49:43 +0000760 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000761
762 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000763 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000764}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000765
Ted Kremenekca5f6202008-04-15 23:06:53 +0000766
767void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
768 NodeTy* Pred, NodeSet& Dst) {
769
770 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
771
Ted Kremeneke66ba682009-02-13 01:45:31 +0000772 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000773 SVal X = state->getBlkExprSVal(Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000774
775 assert (X.isUndef());
776
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000777 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
778 assert(SE);
779 X = state->getBlkExprSVal(SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000780
781 // Make sure that we invalidate the previous binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000782 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000783}
784
Ted Kremenekaee121c2008-02-13 23:08:21 +0000785/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
786/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000787void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
788 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000789 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000790 Expr* CondE = builder.getCondition();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000791 SVal CondV = state->getSVal(CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000792
Ted Kremenekb31af242008-02-28 09:25:22 +0000793 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000794 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000795 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000796 return;
797 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000798
Ted Kremeneke66ba682009-02-13 01:45:31 +0000799 const GRState* DefaultSt = state;
Ted Kremenek70970bf2009-06-18 22:57:13 +0000800 bool defaultIsFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000801
Ted Kremenek07baa252008-02-21 18:02:17 +0000802 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000803 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000804
805 // Evaluate the LHS of the case value.
806 Expr::EvalResult V1;
807 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000808
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000809 // Sanity checks. These go away in Release builds.
810 assert(b && V1.Val.isInt() && !V1.HasSideEffects
811 && "Case condition must evaluate to an integer constant.");
812 b = b; // silence unused variable warning
813 assert(V1.Val.getInt().getBitWidth() ==
814 getContext().getTypeSize(CondE->getType()));
815
Ted Kremenekaee121c2008-02-13 23:08:21 +0000816 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000817 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000818
819 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000820 b = E->Evaluate(V2, getContext());
821 assert(b && V2.Val.isInt() && !V2.HasSideEffects
822 && "Case condition must evaluate to an integer constant.");
823 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000824 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000825 else
826 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000827
828 // FIXME: Eventually we should replace the logic below with a range
829 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000830 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000831
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000832 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000833 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000834 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremenek74556a12009-03-26 03:35:11 +0000835 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000836
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000837 // Now "assume" that the case matches.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000838 if (const GRState* stateNew = state->assume(Res, true)) {
839 builder.generateCaseStmtNode(I, stateNew);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000840
841 // If CondV evaluates to a constant, then we know that this
842 // is the *only* case that we can take, so stop evaluating the
843 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000844 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000845 return;
846 }
847
848 // Now "assume" that the case doesn't match. Add this state
849 // to the default state (if it is feasible).
Ted Kremenek70970bf2009-06-18 22:57:13 +0000850 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
851 defaultIsFeasible = true;
852 DefaultSt = stateNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000853 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000854
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000855 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000856 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000857 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000858
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000859 ++V1.Val.getInt();
860 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000861
862 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000863 }
864
865 // If we reach here, than we know that the default branch is
866 // possible.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000867 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000868}
869
Ted Kremenekca5f6202008-04-15 23:06:53 +0000870//===----------------------------------------------------------------------===//
871// Transfer functions: logical operations ('&&', '||').
872//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000873
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000874void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000875 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000876
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000877 assert(B->getOpcode() == BinaryOperator::LAnd ||
878 B->getOpcode() == BinaryOperator::LOr);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000879
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000880 assert(B == CurrentStmt && getCFG().isBlkExpr(B));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000881
Ted Kremeneke66ba682009-02-13 01:45:31 +0000882 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000883 SVal X = state->getBlkExprSVal(B);
884 assert(X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000885
Ted Kremenekb31af242008-02-28 09:25:22 +0000886 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000887
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000888 assert(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000889
890 if (Ex == B->getRHS()) {
891
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000892 X = state->getBlkExprSVal(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000893
Ted Kremenekb31af242008-02-28 09:25:22 +0000894 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000895
Ted Kremenekb31af242008-02-28 09:25:22 +0000896 if (X.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000897 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000898 return;
899 }
900
Ted Kremenek99ecce72008-02-26 19:05:15 +0000901 // We took the RHS. Because the value of the '&&' or '||' expression must
902 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
903 // or 1. Alternatively, we could take a lazy approach, and calculate this
904 // value later when necessary. We don't have the machinery in place for
905 // this right now, and since most logical expressions are used for branches,
Ted Kremenek70970bf2009-06-18 22:57:13 +0000906 // the payoff is not likely to be large. Instead, we do eager evaluation.
907 if (const GRState *newState = state->assume(X, true))
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000908 MakeNode(Dst, B, Pred, newState->bindBlkExpr(B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000909
Ted Kremenek70970bf2009-06-18 22:57:13 +0000910 if (const GRState *newState = state->assume(X, false))
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000911 MakeNode(Dst, B, Pred, newState->bindBlkExpr(B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000912 }
913 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000914 // We took the LHS expression. Depending on whether we are '&&' or
915 // '||' we know what the value of the expression is via properties of
916 // the short-circuiting.
Ted Kremenek99ecce72008-02-26 19:05:15 +0000917 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000918 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000919 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000920}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000921
Ted Kremenekca5f6202008-04-15 23:06:53 +0000922//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000923// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000924//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000925
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000926void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
927 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000928
Ted Kremeneke66ba682009-02-13 01:45:31 +0000929 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000930
Douglas Gregord2baafd2008-10-21 16:13:35 +0000931 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000932
933 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
934
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000935 SVal V = state->getLValue(VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000936
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000937 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000938 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000939 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000940 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000941 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000942 return;
943
944 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
945 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
946
947 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000948 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000949 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000950 return;
951
952 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000953 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000954 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000955 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000956 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000957 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000958 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000959
960 assert (false &&
961 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000962}
963
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000964/// VisitArraySubscriptExpr - Transfer function for array accesses
965void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000966 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000967
968 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000969 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000970 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000971
972 if (Base->getType()->isVectorType()) {
973 // For vector types get its lvalue.
974 // FIXME: This may not be correct. Is the rvalue of a vector its location?
975 // In fact, I think this is just a hack. We need to get the right
976 // semantics.
977 VisitLValue(Base, Pred, Tmp);
978 }
979 else
980 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000981
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000982 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +0000983 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000984 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +0000985
986 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000987 const GRState* state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000988 SVal V = state->getLValue(A->getType(), state->getSVal(Base),
989 state->getSVal(Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +0000990
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000991 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000992 MakeNode(Dst, A, *I2, state->bindExpr(A, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000993 ProgramPoint::PostLValueKind);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000994 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000995 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000996 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000997 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000998}
999
Ted Kremenekd0d86202008-04-21 23:43:38 +00001000/// VisitMemberExpr - Transfer function for member expressions.
1001void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001002 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +00001003
1004 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +00001005 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +00001006
1007 if (M->isArrow())
1008 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1009 else
1010 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1011
Douglas Gregor82d44772008-12-20 23:49:58 +00001012 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1013 if (!Field) // FIXME: skipping member expressions for non-fields
1014 return;
1015
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001016 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001017 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001018 // FIXME: Should we insert some assumption logic in here to determine
1019 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001020 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001021 SVal L = state->getLValue(state->getSVal(Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001022
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001023 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001024 MakeNode(Dst, M, *I, state->bindExpr(M, L),
Ted Kremenek0441f112009-05-07 18:27:16 +00001025 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001026 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001027 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001028 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001029}
1030
Ted Kremeneke66ba682009-02-13 01:45:31 +00001031/// EvalBind - Handle the semantics of binding a value to a specific location.
1032/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1033void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1034 const GRState* state, SVal location, SVal Val) {
1035
Ted Kremeneka42be302009-02-14 01:43:44 +00001036 const GRState* newState = 0;
1037
1038 if (location.isUnknown()) {
1039 // We know that the new state will be the same as the old state since
1040 // the location of the binding is "unknown". Consequently, there
1041 // is no reason to just create a new node.
1042 newState = state;
1043 }
1044 else {
1045 // We are binding to a value other than 'unknown'. Perform the binding
1046 // using the StoreManager.
1047 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1048 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001049
Ted Kremeneka42be302009-02-14 01:43:44 +00001050 // The next thing to do is check if the GRTransferFuncs object wants to
1051 // update the state based on the new binding. If the GRTransferFunc object
1052 // doesn't do anything, just auto-propagate the current state.
1053 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1054 newState != state);
1055
1056 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001057}
1058
1059/// EvalStore - Handle the semantics of a store via an assignment.
1060/// @param Dst The node set to store generated state nodes
1061/// @param Ex The expression representing the location of the store
1062/// @param state The current simulation state
1063/// @param location The location to store the value
1064/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001065void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001066 const GRState* state, SVal location, SVal Val,
1067 const void *tag) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001068
1069 assert (Builder && "GRStmtNodeBuilder must be defined.");
1070
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001071 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001072 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001073
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001074 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001075 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001076
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001077 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001078 state = GetState(Pred);
1079
1080 // Proceed with the store.
1081 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001082 SaveAndRestore<const void*> OldTag(Builder->Tag);
1083 Builder->PointKind = ProgramPoint::PostStoreKind;
1084 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001085 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001086}
1087
1088void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001089 const GRState* state, SVal location,
1090 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001091
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001092 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001093 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001094
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001095 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001096 return;
1097
Ted Kremeneke66ba682009-02-13 01:45:31 +00001098 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001099
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001100 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001101 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001102
1103 // FIXME: Currently symbolic analysis "generates" new symbols
1104 // for the contents of values. We need a better approach.
1105
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001106 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001107 // This is important. We must nuke the old binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001108 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, UnknownVal()), K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001109 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001110 else {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001111 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
1112 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001113 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001114}
1115
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001116void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001117 const GRState* state, SVal location, SVal Val,
1118 const void *tag) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001119
1120 NodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001121 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001122
1123 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001124 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001125}
1126
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001127GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001128 const GRState* state,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001129 SVal location,
1130 const void *tag) {
1131
1132 SaveAndRestore<const void*> OldTag(Builder->Tag);
1133 Builder->Tag = tag;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001134
1135 // Check for loads/stores from/to undefined values.
1136 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001137 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001138 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001139 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001140
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001141 if (N) {
1142 N->markAsSink();
1143 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001144 }
1145
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001146 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001147 }
1148
1149 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1150 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001151 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001152
1153 // During a load, one of two possible situations arise:
1154 // (1) A crash, because the location (pointer) was NULL.
1155 // (2) The location (pointer) is not NULL, and the dereference works.
1156 //
1157 // We add these assumptions.
1158
Zhongxing Xu097fc982008-10-17 05:57:07 +00001159 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001160
1161 // "Assume" that the pointer is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001162 const GRState *StNotNull = state->assume(LV, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001163
1164 // "Assume" that the pointer is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001165 const GRState *StNull = state->assume(LV, false);
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001166
Ted Kremenek70970bf2009-06-18 22:57:13 +00001167 if (StNull) {
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001168 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001169 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001170 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001171
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001172 // We don't use "MakeNode" here because the node will be a sink
1173 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001174 NodeTy* NullNode =
1175 Builder->generateNode(Ex, StNull, Pred,
1176 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001177
Ted Kremenek70970bf2009-06-18 22:57:13 +00001178 if (NullNode) {
1179 NullNode->markAsSink();
1180 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001181 else ExplicitNullDeref.insert(NullNode);
1182 }
1183 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001184
Ted Kremenek70970bf2009-06-18 22:57:13 +00001185 if (!StNotNull)
1186 return NULL;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001187
1188 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001189 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001190 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1191 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1192 // Get the index of the accessed element.
1193 SVal Idx = ER->getIndex();
1194 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001195 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1196 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001197
Ted Kremenek70970bf2009-06-18 22:57:13 +00001198 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
1199 true);
1200 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
1201 false);
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001202
Ted Kremenek70970bf2009-06-18 22:57:13 +00001203 if (StOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001204 // Report warning. Make sink node manually.
1205 NodeTy* OOBNode =
1206 Builder->generateNode(Ex, StOutBound, Pred,
1207 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001208
1209 if (OOBNode) {
1210 OOBNode->markAsSink();
1211
Ted Kremenek70970bf2009-06-18 22:57:13 +00001212 if (StInBound)
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001213 ImplicitOOBMemAccesses.insert(OOBNode);
1214 else
1215 ExplicitOOBMemAccesses.insert(OOBNode);
1216 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001217 }
1218
Ted Kremenek70970bf2009-06-18 22:57:13 +00001219 if (!StInBound)
1220 return NULL;
1221
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001222 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001223 }
1224 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001225
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001226 // Generate a new node indicating the checks succeed.
1227 return Builder->generateNode(Ex, StNotNull, Pred,
1228 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001229}
1230
Ted Kremenekca5f6202008-04-15 23:06:53 +00001231//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001232// Transfer function: OSAtomics.
1233//
1234// FIXME: Eventually refactor into a more "plugin" infrastructure.
1235//===----------------------------------------------------------------------===//
1236
1237// Mac OS X:
1238// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1239// atomic.3.html
1240//
1241static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1242 GRExprEngine& Engine,
1243 GRStmtNodeBuilder<GRState>& Builder,
1244 CallExpr* CE, SVal L,
1245 ExplodedNode<GRState>* Pred) {
1246
1247 // Not enough arguments to match OSAtomicCompareAndSwap?
1248 if (CE->getNumArgs() != 3)
1249 return false;
1250
1251 ASTContext &C = Engine.getContext();
1252 Expr *oldValueExpr = CE->getArg(0);
1253 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1254
1255 Expr *newValueExpr = CE->getArg(1);
1256 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1257
1258 // Do the types of 'oldValue' and 'newValue' match?
1259 if (oldValueType != newValueType)
1260 return false;
1261
1262 Expr *theValueExpr = CE->getArg(2);
1263 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1264
1265 // theValueType not a pointer?
1266 if (!theValueType)
1267 return false;
1268
1269 QualType theValueTypePointee =
1270 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1271
1272 // The pointee must match newValueType and oldValueType.
1273 if (theValueTypePointee != newValueType)
1274 return false;
1275
1276 static unsigned magic_load = 0;
1277 static unsigned magic_store = 0;
1278
1279 const void *OSAtomicLoadTag = &magic_load;
1280 const void *OSAtomicStoreTag = &magic_store;
1281
1282 // Load 'theValue'.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001283 const GRState *state = Pred->getState();
1284 ExplodedNodeSet<GRState> Tmp;
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001285 SVal location = state->getSVal(theValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001286 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1287
1288 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1289 I != E; ++I) {
1290
1291 ExplodedNode<GRState> *N = *I;
1292 const GRState *stateLoad = N->getState();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001293 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1294 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001295
1296 // Perform the comparison.
Zhongxing Xuc890e332009-05-20 09:00:16 +00001297 SVal Cmp = Engine.EvalBinOp(stateLoad,
1298 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001299 Engine.getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +00001300
1301 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001302
1303 // Were they equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001304 if (stateEqual) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001305 // Perform the store.
1306 ExplodedNodeSet<GRState> TmpStore;
1307 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001308 stateEqual->getSVal(newValueExpr), OSAtomicStoreTag);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001309
1310 // Now bind the result of the comparison.
1311 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1312 E2 = TmpStore.end(); I2 != E2; ++I2) {
1313 ExplodedNode<GRState> *predNew = *I2;
1314 const GRState *stateNew = predNew->getState();
1315 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001316 Engine.MakeNode(Dst, CE, predNew, stateNew->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001317 }
1318 }
1319
1320 // Were they not equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001321 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001322 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001323 Engine.MakeNode(Dst, CE, N, stateNotEqual->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001324 }
1325 }
1326
1327 return true;
1328}
1329
1330static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1331 GRExprEngine& Engine,
1332 GRStmtNodeBuilder<GRState>& Builder,
1333 CallExpr* CE, SVal L,
1334 ExplodedNode<GRState>* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001335 const FunctionDecl* FD = L.getAsFunctionDecl();
1336 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001337 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001338
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001339 const char *FName = FD->getNameAsCString();
1340
1341 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001342 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1343 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001344 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001345
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001346 // FIXME: Other atomics.
1347 return false;
1348}
1349
1350//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001351// Transfer function: Function calls.
1352//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001353
1354void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1355 assert (Builder && "GRStmtNodeBuilder must be defined.");
1356
1357 // FIXME: Allow us to chain together transfer functions.
1358 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1359 return;
1360
1361 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1362}
1363
Ted Kremenekd9268e32008-02-19 01:44:53 +00001364void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001365 CallExpr::arg_iterator AI,
1366 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001367 NodeSet& Dst)
1368{
1369 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001370 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001371 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1372 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001373 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001374
1375 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1376}
1377
1378void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1379 CallExpr::arg_iterator AI,
1380 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001381 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001382 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001383
Ted Kremenek07baa252008-02-21 18:02:17 +00001384 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001385 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001386 // If the call argument is being bound to a reference parameter,
1387 // visit it as an lvalue, not an rvalue.
1388 bool VisitAsLvalue = false;
1389 if (Proto && ParamIdx < Proto->getNumArgs())
1390 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1391
1392 NodeSet DstTmp;
1393 if (VisitAsLvalue)
1394 VisitLValue(*AI, Pred, DstTmp);
1395 else
1396 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001397 ++AI;
1398
Ted Kremenek769f3482008-03-04 22:01:56 +00001399 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001400 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001401
1402 return;
1403 }
1404
1405 // If we reach here we have processed all of the arguments. Evaluate
1406 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001407
Ted Kremenekc71901d2008-02-25 21:16:03 +00001408 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001409 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001410
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001411 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001412
Ted Kremenekd9268e32008-02-19 01:44:53 +00001413 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001414 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1415
Ted Kremeneke66ba682009-02-13 01:45:31 +00001416 const GRState* state = GetState(*DI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001417 SVal L = state->getSVal(Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001418
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001419 // FIXME: Add support for symbolic function calls (calls involving
1420 // function pointer values that are symbolic).
1421
1422 // Check for undefined control-flow or calls to NULL.
1423
Zhongxing Xu097fc982008-10-17 05:57:07 +00001424 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001425 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001426
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001427 if (N) {
1428 N->markAsSink();
1429 BadCalls.insert(N);
1430 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001431
Ted Kremenekd9268e32008-02-19 01:44:53 +00001432 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001433 }
1434
1435 // Check for the "noreturn" attribute.
1436
1437 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001438 const FunctionDecl* FD = L.getAsFunctionDecl();
1439 if (FD) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +00001440 if (FD->getAttr<NoReturnAttr>(getContext()) ||
1441 FD->getAttr<AnalyzerNoReturnAttr>(getContext()))
Ted Kremenekb451dd32008-03-05 21:15:02 +00001442 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001443 else {
1444 // HACK: Some functions are not marked noreturn, and don't return.
1445 // Here are a few hardwired ones. If this takes too long, we can
1446 // potentially cache these results.
1447 const char* s = FD->getIdentifier()->getName();
1448 unsigned n = strlen(s);
1449
1450 switch (n) {
1451 default:
1452 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001453
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001454 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001455 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1456 break;
1457
1458 case 5:
1459 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001460 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001461 if (CE->getNumArgs() > 0) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001462 SVal X = state->getSVal(*CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001463 // FIXME: use Assume to inspect the possible symbolic value of
1464 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001465 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001466 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001467 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001468 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001469 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001470 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001471
Ted Kremenek23271be2008-04-22 05:37:33 +00001472 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001473 if (!memcmp(s, "Assert", 6)) {
1474 Builder->BuildSinks = true;
1475 break;
1476 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001477
1478 // FIXME: This is just a wrapper around throwing an exception.
1479 // Eventually inter-procedural analysis should handle this easily.
1480 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1481
Ted Kremenek23271be2008-04-22 05:37:33 +00001482 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001483
1484 case 7:
1485 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1486 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001487
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001488 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001489 if (!memcmp(s ,"db_error", 8) ||
1490 !memcmp(s, "__assert", 8))
1491 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001492 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001493
1494 case 12:
1495 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1496 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001497
Ted Kremenek19903a22008-09-19 02:30:47 +00001498 case 13:
1499 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1500 break;
1501
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001502 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001503 if (!memcmp(s, "dtrace_assfail", 14) ||
1504 !memcmp(s, "yy_fatal_error", 14))
1505 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001506 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001507
1508 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001509 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001510 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1511 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001512 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001513
Ted Kremeneka46fea72008-05-17 00:33:23 +00001514 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001515 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001516
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001517 }
1518 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001519
1520 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001521
Zhongxing Xucac107a2009-04-20 05:24:46 +00001522 if (FD) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001523
Zhongxing Xucac107a2009-04-20 05:24:46 +00001524 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001525 switch (id) {
1526 case Builtin::BI__builtin_expect: {
1527 // For __builtin_expect, just return the value of the subexpression.
1528 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001529 SVal X = state->getSVal(*(CE->arg_begin()));
1530 MakeNode(Dst, CE, *DI, state->bindExpr(CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001531 continue;
1532 }
1533
Ted Kremenek19891fa2008-11-02 00:35:01 +00001534 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001535 // FIXME: Refactor into StoreManager itself?
1536 MemRegionManager& RM = getStateManager().getRegionManager();
1537 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001538 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001539
1540 // Set the extent of the region in bytes. This enables us to use the
1541 // SVal of the argument directly. If we save the extent in bits, we
1542 // cannot represent values like symbol*8.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001543 SVal Extent = state->getSVal(*(CE->arg_begin()));
Ted Kremeneke66ba682009-02-13 01:45:31 +00001544 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001545
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001546 MakeNode(Dst, CE, *DI, state->bindExpr(CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001547 continue;
1548 }
1549
Ted Kremenek21581c62008-03-05 22:59:42 +00001550 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001551 break;
1552 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001553 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001554
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001555 // Check any arguments passed-by-value against being undefined.
1556
1557 bool badArg = false;
1558
1559 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1560 I != E; ++I) {
1561
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001562 if (GetState(*DI)->getSVal(*I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001563 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001564
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001565 if (N) {
1566 N->markAsSink();
1567 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001568 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001569
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001570 badArg = true;
1571 break;
1572 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001573 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001574
1575 if (badArg)
1576 continue;
1577
1578 // Dispatch to the plug-in transfer function.
1579
1580 unsigned size = Dst.size();
1581 SaveOr OldHasGen(Builder->HasGeneratedNode);
1582 EvalCall(Dst, CE, L, *DI);
1583
1584 // Handle the case where no nodes where generated. Auto-generate that
1585 // contains the updated state if we aren't generating sinks.
1586
1587 if (!Builder->BuildSinks && Dst.size() == size &&
1588 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001589 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001590 }
1591}
1592
Ted Kremenekca5f6202008-04-15 23:06:53 +00001593//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001594// Transfer function: Objective-C ivar references.
1595//===----------------------------------------------------------------------===//
1596
Ted Kremenek9a48d862009-02-28 20:50:43 +00001597static std::pair<const void*,const void*> EagerlyAssumeTag
1598 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1599
Ted Kremenek34a611b2009-02-25 23:32:10 +00001600void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001601 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1602 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001603
1604 // Test if the previous node was as the same expression. This can happen
1605 // when the expression fails to evaluate to anything meaningful and
1606 // (as an optimization) we don't generate a node.
1607 ProgramPoint P = Pred->getLocation();
1608 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1609 Dst.Add(Pred);
1610 continue;
1611 }
1612
Ted Kremenek8f520972009-02-25 22:32:02 +00001613 const GRState* state = Pred->getState();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001614 SVal V = state->getSVal(Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001615 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001616 // First assume that the condition is true.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001617 if (const GRState *stateTrue = state->assume(V, true)) {
1618 stateTrue = stateTrue->bindExpr(Ex, MakeConstantVal(1U, Ex));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001619 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001620 stateTrue, Pred));
1621 }
1622
1623 // Next, assume that the condition is false.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001624 if (const GRState *stateFalse = state->assume(V, false)) {
1625 stateFalse = stateFalse->bindExpr(Ex, MakeConstantVal(0U, Ex));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001626 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001627 stateFalse, Pred));
1628 }
1629 }
1630 else
1631 Dst.Add(Pred);
1632 }
1633}
1634
1635//===----------------------------------------------------------------------===//
1636// Transfer function: Objective-C ivar references.
1637//===----------------------------------------------------------------------===//
1638
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001639void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1640 NodeTy* Pred, NodeSet& Dst,
1641 bool asLValue) {
1642
1643 Expr* Base = cast<Expr>(Ex->getBase());
1644 NodeSet Tmp;
1645 Visit(Base, Pred, Tmp);
1646
1647 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001648 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001649 SVal BaseVal = state->getSVal(Base);
1650 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001651
1652 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001653 MakeNode(Dst, Ex, *I, state->bindExpr(Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001654 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001655 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001656 }
1657}
1658
1659//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001660// Transfer function: Objective-C fast enumeration 'for' statements.
1661//===----------------------------------------------------------------------===//
1662
1663void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1664 NodeTy* Pred, NodeSet& Dst) {
1665
1666 // ObjCForCollectionStmts are processed in two places. This method
1667 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1668 // statements within a basic block. This transfer function does two things:
1669 //
1670 // (1) binds the next container value to 'element'. This creates a new
1671 // node in the ExplodedGraph.
1672 //
1673 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1674 // whether or not the container has any more elements. This value
1675 // will be tested in ProcessBranch. We need to explicitly bind
1676 // this value because a container can contain nil elements.
1677 //
1678 // FIXME: Eventually this logic should actually do dispatches to
1679 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1680 // This will require simulating a temporary NSFastEnumerationState, either
1681 // through an SVal or through the use of MemRegions. This value can
1682 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1683 // terminates we reclaim the temporary (it goes out of scope) and we
1684 // we can test if the SVal is 0 or if the MemRegion is null (depending
1685 // on what approach we take).
1686 //
1687 // For now: simulate (1) by assigning either a symbol or nil if the
1688 // container is empty. Thus this transfer function will by default
1689 // result in state splitting.
1690
Ted Kremenek034a9472008-11-14 19:47:18 +00001691 Stmt* elem = S->getElement();
1692 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001693
1694 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001695 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001696 assert (ElemD->getInit() == 0);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001697 ElementV = GetState(Pred)->getLValue(ElemD);
Ted Kremenek034a9472008-11-14 19:47:18 +00001698 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1699 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001700 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001701
1702 NodeSet Tmp;
1703 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001704
Ted Kremenek034a9472008-11-14 19:47:18 +00001705 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1706 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001707 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek034a9472008-11-14 19:47:18 +00001708 }
1709}
1710
1711void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1712 NodeTy* Pred, NodeSet& Dst,
1713 SVal ElementV) {
1714
1715
Ted Kremenek13e167f2008-11-12 19:24:17 +00001716
Ted Kremenek034a9472008-11-14 19:47:18 +00001717 // Get the current state. Use 'EvalLocation' to determine if it is a null
1718 // pointer, etc.
1719 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001720
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001721 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1722 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001723 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001724
Ted Kremenek18a636d2009-06-18 01:23:53 +00001725 const GRState *state = GetState(Pred);
Ted Kremenek034a9472008-11-14 19:47:18 +00001726
Ted Kremenek13e167f2008-11-12 19:24:17 +00001727 // Handle the case where the container still has elements.
Ted Kremenek034a9472008-11-14 19:47:18 +00001728 QualType IntTy = getContext().IntTy;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001729 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001730 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001731
Ted Kremenek13e167f2008-11-12 19:24:17 +00001732 // Handle the case where the container has no elements.
Ted Kremenekd3789d72008-11-12 21:12:46 +00001733 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001734 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001735
1736 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1737 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1738 // FIXME: The proper thing to do is to really iterate over the
1739 // container. We will do this with dispatch logic to the store.
1740 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu20362702009-05-09 03:57:34 +00001741 QualType T = R->getValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001742 assert (Loc::IsLocType(T));
1743 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001744 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1745 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
Ted Kremenek18a636d2009-06-18 01:23:53 +00001746 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001747
Ted Kremenek034a9472008-11-14 19:47:18 +00001748 // Bind the location to 'nil' on the false branch.
1749 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
Ted Kremenek18a636d2009-06-18 01:23:53 +00001750 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001751 }
1752
Ted Kremenekd3789d72008-11-12 21:12:46 +00001753 // Create the new nodes.
1754 MakeNode(Dst, S, Pred, hasElems);
1755 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001756}
1757
1758//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001759// Transfer function: Objective-C message expressions.
1760//===----------------------------------------------------------------------===//
1761
1762void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1763 NodeSet& Dst){
1764
1765 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1766 Pred, Dst);
1767}
1768
1769void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001770 ObjCMessageExpr::arg_iterator AI,
1771 ObjCMessageExpr::arg_iterator AE,
1772 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001773 if (AI == AE) {
1774
1775 // Process the receiver.
1776
1777 if (Expr* Receiver = ME->getReceiver()) {
1778 NodeSet Tmp;
1779 Visit(Receiver, Pred, Tmp);
1780
1781 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1782 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1783
1784 return;
1785 }
1786
1787 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1788 return;
1789 }
1790
1791 NodeSet Tmp;
1792 Visit(*AI, Pred, Tmp);
1793
1794 ++AI;
1795
1796 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1797 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1798}
1799
1800void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1801 NodeTy* Pred,
1802 NodeSet& Dst) {
1803
1804 // FIXME: More logic for the processing the method call.
1805
Ted Kremeneke66ba682009-02-13 01:45:31 +00001806 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001807 bool RaisesException = false;
1808
Ted Kremenekca5f6202008-04-15 23:06:53 +00001809
1810 if (Expr* Receiver = ME->getReceiver()) {
1811
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001812 SVal L = state->getSVal(Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001813
Ted Kremenek95a98252009-02-19 04:06:22 +00001814 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001815 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001816 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001817
1818 if (N) {
1819 N->markAsSink();
1820 UndefReceivers.insert(N);
1821 }
1822
1823 return;
1824 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001825
Ted Kremenek95a98252009-02-19 04:06:22 +00001826 // "Assume" that the receiver is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001827 const GRState *StNotNull = state->assume(L, true);
Ted Kremenek95a98252009-02-19 04:06:22 +00001828
1829 // "Assume" that the receiver is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001830 const GRState *StNull = state->assume(L, false);
Ted Kremenek95a98252009-02-19 04:06:22 +00001831
Ted Kremenek70970bf2009-06-18 22:57:13 +00001832 if (StNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001833 QualType RetTy = ME->getType();
1834
Ted Kremenek95a98252009-02-19 04:06:22 +00001835 // Check if the receiver was nil and the return value a struct.
Ted Kremenekb3323002009-04-09 05:45:56 +00001836 if(RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001837 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001838 // The [0 ...] expressions will return garbage. Flag either an
1839 // explicit or implicit error. Because of the structure of this
1840 // function we currently do not bifurfacte the state graph at
1841 // this point.
1842 // FIXME: We should bifurcate and fill the returned struct with
1843 // garbage.
1844 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1845 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001846 if (StNotNull)
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001847 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001848 else
Ted Kremenek23712182009-04-09 04:06:51 +00001849 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001850 }
1851 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001852 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001853 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001854 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001855 if (RetTy != Ctx.VoidTy) {
1856 if (BR.getParentMap().isConsumedExpr(ME)) {
1857 // sizeof(void *)
1858 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1859 // sizeof(return type)
1860 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001861
Ted Kremenekb3323002009-04-09 05:45:56 +00001862 if(voidPtrSize < returnTypeSize) {
1863 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1864 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001865 if(StNotNull)
Ted Kremenekb3323002009-04-09 05:45:56 +00001866 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001867 else
Ted Kremenekb3323002009-04-09 05:45:56 +00001868 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001869 }
1870 }
Ted Kremenek70970bf2009-06-18 22:57:13 +00001871 else if (!StNotNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001872 // Handle the safe cases where the return value is 0 if the
1873 // receiver is nil.
1874 //
1875 // FIXME: For now take the conservative approach that we only
1876 // return null values if we *know* that the receiver is nil.
1877 // This is because we can have surprises like:
1878 //
1879 // ... = [[NSScreens screens] objectAtIndex:0];
1880 //
1881 // What can happen is that [... screens] could return nil, but
1882 // it most likely isn't nil. We should assume the semantics
1883 // of this case unless we have *a lot* more knowledge.
1884 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001885 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001886 MakeNode(Dst, ME, Pred, StNull->bindExpr(ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001887 return;
1888 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001889 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001890 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001891 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001892 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001893 // of this method should assume that the receiver is not nil.
1894 if (!StNotNull)
1895 return;
1896
Ted Kremenekf2895872009-04-08 18:51:08 +00001897 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001898 }
1899
Ted Kremenek5f20a632008-05-01 18:33:28 +00001900 // Check if the "raise" message was sent.
1901 if (ME->getSelector() == RaiseSel)
1902 RaisesException = true;
1903 }
1904 else {
1905
1906 IdentifierInfo* ClsName = ME->getClassName();
1907 Selector S = ME->getSelector();
1908
1909 // Check for special instance methods.
1910
1911 if (!NSExceptionII) {
1912 ASTContext& Ctx = getContext();
1913
1914 NSExceptionII = &Ctx.Idents.get("NSException");
1915 }
1916
1917 if (ClsName == NSExceptionII) {
1918
1919 enum { NUM_RAISE_SELECTORS = 2 };
1920
1921 // Lazily create a cache of the selectors.
1922
1923 if (!NSExceptionInstanceRaiseSelectors) {
1924
1925 ASTContext& Ctx = getContext();
1926
1927 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1928
1929 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1930 unsigned idx = 0;
1931
1932 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001933 II.push_back(&Ctx.Idents.get("raise"));
1934 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001935 NSExceptionInstanceRaiseSelectors[idx++] =
1936 Ctx.Selectors.getSelector(II.size(), &II[0]);
1937
1938 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001939 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001940 NSExceptionInstanceRaiseSelectors[idx++] =
1941 Ctx.Selectors.getSelector(II.size(), &II[0]);
1942 }
1943
1944 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1945 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1946 RaisesException = true; break;
1947 }
1948 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001949 }
1950
1951 // Check for any arguments that are uninitialized/undefined.
1952
1953 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1954 I != E; ++I) {
1955
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001956 if (state->getSVal(*I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001957
1958 // Generate an error node for passing an uninitialized/undefined value
1959 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001960 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001961
1962 if (N) {
1963 N->markAsSink();
1964 MsgExprUndefArgs[N] = *I;
1965 }
1966
1967 return;
1968 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001969 }
1970
1971 // Check if we raise an exception. For now treat these as sinks. Eventually
1972 // we will want to handle exceptions properly.
1973
1974 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1975
1976 if (RaisesException)
1977 Builder->BuildSinks = true;
1978
Ted Kremenekca5f6202008-04-15 23:06:53 +00001979 // Dispatch to plug-in transfer function.
1980
1981 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001982 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001983
Ted Kremenekca5f6202008-04-15 23:06:53 +00001984 EvalObjCMessageExpr(Dst, ME, Pred);
1985
1986 // Handle the case where no nodes where generated. Auto-generate that
1987 // contains the updated state if we aren't generating sinks.
1988
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001989 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001990 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001991}
1992
1993//===----------------------------------------------------------------------===//
1994// Transfer functions: Miscellaneous statements.
1995//===----------------------------------------------------------------------===//
1996
Ted Kremenek16354a42009-01-13 01:04:21 +00001997void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1998 QualType PtrTy,
1999 Expr* CastE, NodeTy* Pred,
2000 NodeSet& Dst) {
2001 if (!V.isUnknownOrUndef()) {
2002 // FIXME: Determine if the number of bits of the target type is
2003 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00002004 // If not, flag an error.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002005 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, EvalCast(cast<Loc>(V),
Ted Kremenek52978eb2009-03-05 03:44:53 +00002006 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00002007 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002008 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002009 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002010}
2011
2012
Ted Kremenek07baa252008-02-21 18:02:17 +00002013void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00002014 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002015 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002016 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002017
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002018 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002019 T = ExCast->getTypeAsWritten();
2020
Zhongxing Xu943909c2008-10-22 08:02:16 +00002021 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002022 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002023 else
2024 Visit(Ex, Pred, S1);
2025
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002026 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00002027 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002028 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002029 Dst.Add(*I1);
2030
Ted Kremenek54eddae2008-01-24 02:02:54 +00002031 return;
2032 }
2033
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002034 // FIXME: The rest of this should probably just go into EvalCall, and
2035 // let the transfer function object be responsible for constructing
2036 // nodes.
2037
Ted Kremenek07baa252008-02-21 18:02:17 +00002038 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00002039 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002040 const GRState* state = GetState(N);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002041 SVal V = state->getSVal(Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002042 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002043
2044 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002045 if (V.isUnknown()) {
2046 Dst.Add(N);
2047 continue;
2048 }
2049
2050 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002051 if (V.isUndef())
2052 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00002053
2054 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00002055 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002056 C.getCanonicalType(ExTy).getUnqualifiedType())
2057 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002058
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002059 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002060 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002061 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002062 continue;
2063 }
2064
2065 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002066 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002067 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002068 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002069 V = LV->getLoc();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002070 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002071 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002072 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002073
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002074 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002075 }
2076
2077 // Just pass through function and block pointers.
2078 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2079 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002080 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002081 }
2082
Ted Kremenek16354a42009-01-13 01:04:21 +00002083 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002084 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00002085 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00002086 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002087
2088 // Are we casting from an array to a pointer? If so just pass on
2089 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002090 if (T->isPointerType())
2091 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00002092
2093 // Are we casting from an array to an integer? If so, cast the decayed
2094 // pointer value to an integer.
2095 assert(T->isIntegerType());
2096 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2097 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002098 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002099 continue;
2100 }
2101
Ted Kremenekf5da3252008-12-13 21:49:13 +00002102 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00002103 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2104 // FIXME: For TypedViewRegions, we should handle the case where the
2105 // underlying symbolic pointer is a function pointer or
2106 // block pointer.
2107
2108 // FIXME: We should handle the case where we strip off view layers to get
2109 // to a desugared type.
2110
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002111 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00002112 // We get a symbolic function pointer for a dereference of a function
2113 // pointer, but it is of function type. Example:
2114
2115 // struct FPRec {
2116 // void (*my_func)(int * x);
2117 // };
2118 //
2119 // int bar(int x);
2120 //
2121 // int f1_a(struct FPRec* foo) {
2122 // int x;
2123 // (*foo->my_func)(&x);
2124 // return bar(x)+1; // no-warning
2125 // }
2126
2127 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002128
Ted Kremenekf5da3252008-12-13 21:49:13 +00002129 const MemRegion* R = RV->getRegion();
2130 StoreManager& StoreMgr = getStoreManager();
2131
2132 // Delegate to store manager to get the result of casting a region
2133 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002134 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00002135
2136 // Inspect the result. If the MemRegion* returned is NULL, this
2137 // expression evaluates to UnknownVal.
2138 R = Res.getRegion();
2139 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2140
2141 // Generate the new node in the ExplodedGraph.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002142 MakeNode(Dst, CastE, N, Res.getState()->bindExpr(CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00002143 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002144 }
Zhongxing Xu18bcec02009-04-10 06:06:13 +00002145 // All other cases.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002146 DispatchCast: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002147 MakeNode(Dst, CastE, N, state->bindExpr(CastE,
2148 EvalCast(V, CastE->getType())));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002149 continue;
2150 }
2151
2152 PassThrough: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002153 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002154 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002155 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002156}
2157
Ted Kremenekd83daa52008-10-27 21:54:31 +00002158void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002159 NodeTy* Pred, NodeSet& Dst,
2160 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002161 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2162 NodeSet Tmp;
2163 Visit(ILE, Pred, Tmp);
2164
2165 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002166 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002167 SVal ILV = state->getSVal(ILE);
2168 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002169
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002170 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002171 MakeNode(Dst, CL, *I, state->bindExpr(CL, state->getLValue(CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002172 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002173 MakeNode(Dst, CL, *I, state->bindExpr(CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002174 }
2175}
2176
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002177void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002178
Ted Kremenek811af062008-10-06 18:43:53 +00002179 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002180 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002181
2182 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002183 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002184
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002185 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002186 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002187
2188 // FIXME: static variables may have an initializer, but the second
2189 // time a function is called those values may not be current.
2190 NodeSet Tmp;
2191
Ted Kremenek13e167f2008-11-12 19:24:17 +00002192 if (InitEx)
2193 Visit(InitEx, Pred, Tmp);
Ted Kremenek448ab622008-08-28 18:34:26 +00002194
2195 if (Tmp.empty())
2196 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002197
2198 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002199 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002200 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002201
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002202 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2203 QualType T = getContext().getCanonicalType(VD->getType());
2204 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2205 // FIXME: Handle multi-dimensional VLAs.
2206
2207 Expr* SE = VLA->getSizeExpr();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002208 SVal Size = state->getSVal(SE);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002209
2210 if (Size.isUndef()) {
2211 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2212 N->markAsSink();
2213 ExplicitBadSizedVLA.insert(N);
2214 }
2215 continue;
2216 }
2217
Ted Kremenek70970bf2009-06-18 22:57:13 +00002218 const GRState* zeroState = state->assume(Size, false);
2219 state = state->assume(Size, true);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002220
Ted Kremenek70970bf2009-06-18 22:57:13 +00002221 if (zeroState) {
2222 if (NodeTy* N = Builder->generateNode(DS, zeroState, Pred)) {
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002223 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00002224 if (state)
2225 ImplicitBadSizedVLA.insert(N);
2226 else
2227 ExplicitBadSizedVLA.insert(N);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002228 }
2229 }
2230
Ted Kremenek70970bf2009-06-18 22:57:13 +00002231 if (!state)
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002232 continue;
2233 }
2234
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002235 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002236 if (InitEx) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002237 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002238 QualType T = VD->getType();
2239
2240 // Recover some path-sensitivity if a scalar value evaluated to
2241 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002242 if (InitVal.isUnknown() ||
2243 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002244 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002245 }
2246
Ted Kremeneke66ba682009-02-13 01:45:31 +00002247 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002248
2249 // The next thing to do is check if the GRTransferFuncs object wants to
2250 // update the state based on the new binding. If the GRTransferFunc
2251 // object doesn't do anything, just auto-propagate the current state.
2252 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2253 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2254 InitVal);
2255 }
2256 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002257 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002258 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002259 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002260 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002261}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002262
Ted Kremeneke56ece22008-10-30 17:47:32 +00002263namespace {
2264 // This class is used by VisitInitListExpr as an item in a worklist
2265 // for processing the values contained in an InitListExpr.
2266class VISIBILITY_HIDDEN InitListWLItem {
2267public:
2268 llvm::ImmutableList<SVal> Vals;
2269 GRExprEngine::NodeTy* N;
2270 InitListExpr::reverse_iterator Itr;
2271
2272 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2273 InitListExpr::reverse_iterator itr)
2274 : Vals(vals), N(n), Itr(itr) {}
2275};
2276}
2277
2278
Zhongxing Xuebcad732008-10-30 05:02:23 +00002279void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2280 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002281
Zhongxing Xuebcad732008-10-30 05:02:23 +00002282 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002283 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002284 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002285
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002286 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002287
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002288 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002289
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002290 // Handle base case where the initializer has no elements.
2291 // e.g: static int* myArray[] = {};
2292 if (NumInitElements == 0) {
2293 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002294 MakeNode(Dst, E, Pred, state->bindExpr(E, V));
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002295 return;
2296 }
2297
2298 // Create a worklist to process the initializers.
2299 llvm::SmallVector<InitListWLItem, 10> WorkList;
2300 WorkList.reserve(NumInitElements);
2301 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002302 InitListExpr::reverse_iterator ItrEnd = E->rend();
2303
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002304 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002305 while (!WorkList.empty()) {
2306 InitListWLItem X = WorkList.back();
2307 WorkList.pop_back();
2308
Zhongxing Xuebcad732008-10-30 05:02:23 +00002309 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002310 Visit(*X.Itr, X.N, Tmp);
2311
2312 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002313
Ted Kremeneke56ece22008-10-30 17:47:32 +00002314 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2315 // Get the last initializer value.
2316 state = GetState(*NI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002317 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002318
2319 // Construct the new list of values by prepending the new value to
2320 // the already constructed list.
2321 llvm::ImmutableList<SVal> NewVals =
2322 getBasicVals().consVals(InitV, X.Vals);
2323
2324 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002325 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002326 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuebcad732008-10-30 05:02:23 +00002327
Ted Kremeneke56ece22008-10-30 17:47:32 +00002328 // Make final state and node.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002329 MakeNode(Dst, E, *NI, state->bindExpr(E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002330 }
2331 else {
2332 // Still some initializer values to go. Push them onto the worklist.
2333 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2334 }
2335 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002336 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002337
2338 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002339 }
2340
Ted Kremenek79413a52008-11-13 06:10:40 +00002341 if (T->isUnionType() || T->isVectorType()) {
2342 // FIXME: to be implemented.
2343 // Note: That vectors can return true for T->isIntegerType()
2344 MakeNode(Dst, E, Pred, state);
2345 return;
2346 }
2347
Zhongxing Xuebcad732008-10-30 05:02:23 +00002348 if (Loc::IsLocType(T) || T->isIntegerType()) {
2349 assert (E->getNumInits() == 1);
2350 NodeSet Tmp;
2351 Expr* Init = E->getInit(0);
2352 Visit(Init, Pred, Tmp);
2353 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2354 state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002355 MakeNode(Dst, E, *I, state->bindExpr(E, state->getSVal(Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002356 }
2357 return;
2358 }
2359
Zhongxing Xuebcad732008-10-30 05:02:23 +00002360
2361 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2362 assert(0 && "unprocessed InitListExpr type");
2363}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002364
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002365/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2366void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2367 NodeTy* Pred,
2368 NodeSet& Dst) {
2369 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002370 uint64_t amt;
2371
2372 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002373 if (T == getContext().VoidTy) {
2374 // sizeof(void) == 1 byte.
2375 amt = 1;
2376 }
2377 else if (!T.getTypePtr()->isConstantSizeType()) {
2378 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002379 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002380 }
2381 else if (T->isObjCInterfaceType()) {
2382 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2383 // the compiler has laid out its representation. Just report Unknown
2384 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002385 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002386 }
2387 else {
2388 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002389 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002390 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002391 }
2392 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002393 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002394
Ted Kremenekf10f2882008-03-21 21:30:14 +00002395 MakeNode(Dst, Ex, Pred,
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002396 GetState(Pred)->bindExpr(Ex, NonLoc::MakeVal(getBasicVals(), amt,
2397 Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002398}
2399
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002400
Ted Kremenek07baa252008-02-21 18:02:17 +00002401void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002402 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002403
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002404 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002405
2406 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002407 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002408
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002409 case UnaryOperator::Deref: {
2410
2411 Expr* Ex = U->getSubExpr()->IgnoreParens();
2412 NodeSet Tmp;
2413 Visit(Ex, Pred, Tmp);
2414
2415 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002416
Ted Kremeneke66ba682009-02-13 01:45:31 +00002417 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002418 SVal location = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002419
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002420 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002421 MakeNode(Dst, U, *I, state->bindExpr(U, location),
Ted Kremenek0441f112009-05-07 18:27:16 +00002422 ProgramPoint::PostLValueKind);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002423 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002424 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002425 }
2426
2427 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002428 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002429
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002430 case UnaryOperator::Real: {
2431
2432 Expr* Ex = U->getSubExpr()->IgnoreParens();
2433 NodeSet Tmp;
2434 Visit(Ex, Pred, Tmp);
2435
2436 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2437
Zhongxing Xu097fc982008-10-17 05:57:07 +00002438 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002439 if (Ex->getType()->isAnyComplexType()) {
2440 // Just report "Unknown."
2441 Dst.Add(*I);
2442 continue;
2443 }
2444
2445 // For all other types, UnaryOperator::Real is an identity operation.
2446 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002447 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002448 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002449 }
2450
2451 return;
2452 }
2453
2454 case UnaryOperator::Imag: {
2455
2456 Expr* Ex = U->getSubExpr()->IgnoreParens();
2457 NodeSet Tmp;
2458 Visit(Ex, Pred, Tmp);
2459
2460 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002461 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002462 if (Ex->getType()->isAnyComplexType()) {
2463 // Just report "Unknown."
2464 Dst.Add(*I);
2465 continue;
2466 }
2467
2468 // For all other types, UnaryOperator::Float returns 0.
2469 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002470 const GRState* state = GetState(*I);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002471 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002472 MakeNode(Dst, U, *I, state->bindExpr(U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002473 }
2474
2475 return;
2476 }
2477
2478 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002479 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002480 Dst.Add(Pred);
2481 return;
2482
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002483 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002484 case UnaryOperator::Extension: {
2485
2486 // Unary "+" is a no-op, similar to a parentheses. We still have places
2487 // where it may be a block-level expression, so we need to
2488 // generate an extra node that just propagates the value of the
2489 // subexpression.
2490
2491 Expr* Ex = U->getSubExpr()->IgnoreParens();
2492 NodeSet Tmp;
2493 Visit(Ex, Pred, Tmp);
2494
2495 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002496 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002497 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002498 }
2499
2500 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002501 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002502
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002503 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002504
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002505 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002506 Expr* Ex = U->getSubExpr()->IgnoreParens();
2507 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002508 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002509
2510 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002511 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002512 SVal V = state->getSVal(Ex);
2513 state = state->bindExpr(U, V);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002514 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002515 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002516
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002517 return;
2518 }
2519
2520 case UnaryOperator::LNot:
2521 case UnaryOperator::Minus:
2522 case UnaryOperator::Not: {
2523
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002524 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002525 Expr* Ex = U->getSubExpr()->IgnoreParens();
2526 NodeSet Tmp;
2527 Visit(Ex, Pred, Tmp);
2528
2529 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002530 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002531
2532 // Get the value of the subexpression.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002533 SVal V = state->getSVal(Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002534
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002535 if (V.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002536 MakeNode(Dst, U, *I, state->bindExpr(U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002537 continue;
2538 }
2539
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002540// QualType DstT = getContext().getCanonicalType(U->getType());
2541// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2542//
2543// if (DstT != SrcT) // Perform promotions.
2544// V = EvalCast(V, DstT);
2545//
2546// if (V.isUnknownOrUndef()) {
2547// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2548// continue;
2549// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002550
2551 switch (U->getOpcode()) {
2552 default:
2553 assert(false && "Invalid Opcode.");
2554 break;
2555
2556 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002557 // FIXME: Do we need to handle promotions?
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002558 state = state->bindExpr(U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002559 break;
2560
2561 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002562 // FIXME: Do we need to handle promotions?
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002563 state = state->bindExpr(U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002564 break;
2565
2566 case UnaryOperator::LNot:
2567
2568 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2569 //
2570 // Note: technically we do "E == 0", but this is the same in the
2571 // transfer functions as "0 == E".
2572
Zhongxing Xu097fc982008-10-17 05:57:07 +00002573 if (isa<Loc>(V)) {
Ted Kremenekf2895872009-04-08 18:51:08 +00002574 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xuc890e332009-05-20 09:00:16 +00002575 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremenek74556a12009-03-26 03:35:11 +00002576 U->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002577 state = state->bindExpr(U, Result);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002578 }
2579 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002580 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002581#if 0
Zhongxing Xu097fc982008-10-17 05:57:07 +00002582 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002583 state = SetSVal(state, U, Result);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002584#else
Ted Kremenek74556a12009-03-26 03:35:11 +00002585 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2586 U->getType());
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002587 continue;
2588#endif
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002589 }
2590
2591 break;
2592 }
2593
Ted Kremeneke66ba682009-02-13 01:45:31 +00002594 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002595 }
2596
2597 return;
2598 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002599 }
2600
2601 // Handle ++ and -- (both pre- and post-increment).
2602
2603 assert (U->isIncrementDecrementOp());
2604 NodeSet Tmp;
2605 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002606 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002607
2608 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2609
Ted Kremeneke66ba682009-02-13 01:45:31 +00002610 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002611 SVal V1 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002612
2613 // Perform a load.
2614 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002615 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002616
2617 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2618
Ted Kremeneke66ba682009-02-13 01:45:31 +00002619 state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002620 SVal V2 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002621
2622 // Propagate unknown and undefined values.
2623 if (V2.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002624 MakeNode(Dst, U, *I2, state->bindExpr(U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002625 continue;
2626 }
2627
Ted Kremeneke43de222009-03-11 03:54:24 +00002628 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002629 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2630 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002631
Zhongxing Xuc890e332009-05-20 09:00:16 +00002632 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2633 U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002634
2635 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002636 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002637 Result = ValMgr.getConjuredSymbolVal(Ex,
2638 Builder->getCurrentBlockCount());
Ted Kremenek65411632009-04-21 22:38:05 +00002639
2640 // If the value is a location, ++/-- should always preserve
2641 // non-nullness. Check if the original value was non-null, and if so propagate
2642 // that constraint.
2643 if (Loc::IsLocType(U->getType())) {
Zhongxing Xuc890e332009-05-20 09:00:16 +00002644 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenek65411632009-04-21 22:38:05 +00002645 ValMgr.makeZeroVal(U->getType()),
2646 getContext().IntTy);
2647
Ted Kremenek70970bf2009-06-18 22:57:13 +00002648 if (!state->assume(Constraint, true)) {
Ted Kremenek65411632009-04-21 22:38:05 +00002649 // It isn't feasible for the original value to be null.
2650 // Propagate this constraint.
Zhongxing Xuc890e332009-05-20 09:00:16 +00002651 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenek65411632009-04-21 22:38:05 +00002652 ValMgr.makeZeroVal(U->getType()),
2653 getContext().IntTy);
2654
Ted Kremenek70970bf2009-06-18 22:57:13 +00002655 state = state->assume(Constraint, false);
2656 assert(state);
Ted Kremenek65411632009-04-21 22:38:05 +00002657 }
2658 }
2659 }
Ted Kremenek607415e2009-03-20 20:10:45 +00002660
Ted Kremenek70970bf2009-06-18 22:57:13 +00002661 state = state->bindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002662
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002663 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002664 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002665 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002666 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002667}
2668
Ted Kremenek31803c32008-03-17 21:11:24 +00002669void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2670 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2671}
2672
2673void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2674 AsmStmt::outputs_iterator I,
2675 AsmStmt::outputs_iterator E,
2676 NodeTy* Pred, NodeSet& Dst) {
2677 if (I == E) {
2678 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2679 return;
2680 }
2681
2682 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002683 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002684
2685 ++I;
2686
2687 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2688 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2689}
2690
2691void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2692 AsmStmt::inputs_iterator I,
2693 AsmStmt::inputs_iterator E,
2694 NodeTy* Pred, NodeSet& Dst) {
2695 if (I == E) {
2696
2697 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002698 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002699
2700 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2701 // which interprets the inline asm and stores proper results in the
2702 // outputs.
2703
Ted Kremeneke66ba682009-02-13 01:45:31 +00002704 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002705
2706 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2707 OE = A->end_outputs(); OI != OE; ++OI) {
2708
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002709 SVal X = state->getSVal(*OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002710 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002711
Zhongxing Xu097fc982008-10-17 05:57:07 +00002712 if (isa<Loc>(X))
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002713 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002714 }
2715
Ted Kremeneke66ba682009-02-13 01:45:31 +00002716 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002717 return;
2718 }
2719
2720 NodeSet Tmp;
2721 Visit(*I, Pred, Tmp);
2722
2723 ++I;
2724
2725 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2726 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2727}
2728
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002729void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2730 assert (Builder && "GRStmtNodeBuilder must be defined.");
2731
2732 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002733
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002734 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2735 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002736
Ted Kremenekc7469542008-07-17 23:15:45 +00002737 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002738
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002739 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002740
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002741 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002742 MakeNode(Dst, S, Pred, GetState(Pred));
2743}
2744
Ted Kremenek108048c2008-03-31 15:02:58 +00002745void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2746
2747 Expr* R = S->getRetValue();
2748
2749 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002750 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002751 return;
2752 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002753
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002754 NodeSet Tmp;
2755 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002756
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002757 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002758 SVal X = (*I)->getState()->getSVal(R);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002759
2760 // Check if we return the address of a stack variable.
2761 if (isa<loc::MemRegionVal>(X)) {
2762 // Determine if the value is on the stack.
2763 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002764
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002765 if (R && getStateManager().hasStackStorage(R)) {
2766 // Create a special node representing the error.
2767 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2768 N->markAsSink();
2769 RetsStackAddr.insert(N);
2770 }
2771 continue;
2772 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002773 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002774 // Check if we return an undefined value.
2775 else if (X.isUndef()) {
2776 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2777 N->markAsSink();
2778 RetsUndef.insert(N);
2779 }
2780 continue;
2781 }
2782
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002783 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002784 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002785}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002786
Ted Kremenekca5f6202008-04-15 23:06:53 +00002787//===----------------------------------------------------------------------===//
2788// Transfer functions: Binary operators.
2789//===----------------------------------------------------------------------===//
2790
Ted Kremeneke66ba682009-02-13 01:45:31 +00002791const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002792 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002793
2794 // Divide by undefined? (potentially zero)
2795
2796 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002797 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002798
2799 if (DivUndef) {
2800 DivUndef->markAsSink();
2801 ExplicitBadDivides.insert(DivUndef);
2802 }
2803
Ted Kremenek6c438f82008-10-20 23:40:25 +00002804 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002805 }
2806
2807 // Check for divide/remainder-by-zero.
2808 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002809 const GRState* zeroState = state->assume(Denom, false);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002810
2811 // Second, "assume" that the denominator cannot be 0.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002812 state = state->assume(Denom, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002813
Ted Kremenek70970bf2009-06-18 22:57:13 +00002814 // Create the node for the divide-by-zero (if it occurred).
2815 if (zeroState)
2816 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, zeroState, Pred)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002817 DivZeroNode->markAsSink();
2818
Ted Kremenek70970bf2009-06-18 22:57:13 +00002819 if (state)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002820 ImplicitBadDivides.insert(DivZeroNode);
2821 else
2822 ExplicitBadDivides.insert(DivZeroNode);
2823
2824 }
2825
Ted Kremenek70970bf2009-06-18 22:57:13 +00002826 return state;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002827}
2828
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002829void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002830 GRExprEngine::NodeTy* Pred,
2831 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002832
2833 NodeSet Tmp1;
2834 Expr* LHS = B->getLHS()->IgnoreParens();
2835 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002836
Ted Kremenek52510d82008-12-06 02:39:30 +00002837 // FIXME: Add proper support for ObjCKVCRefExpr.
2838 if (isa<ObjCKVCRefExpr>(LHS)) {
2839 Visit(RHS, Pred, Dst);
2840 return;
2841 }
2842
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002843 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002844 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002845 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002846 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002847
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002848 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002849
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002850 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002851
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002852 // Process the RHS.
2853
2854 NodeSet Tmp2;
2855 Visit(RHS, *I1, Tmp2);
2856
2857 // With both the LHS and RHS evaluated, process the operation itself.
2858
2859 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002860
Ted Kremeneke66ba682009-02-13 01:45:31 +00002861 const GRState* state = GetState(*I2);
2862 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002863
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002864 SVal RightV = state->getSVal(RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002865 BinaryOperator::Opcode Op = B->getOpcode();
2866
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002867 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002868
Ted Kremenekf031b872008-01-23 19:59:44 +00002869 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002870
Ted Kremenekd4676512008-03-12 21:45:47 +00002871 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002872 // FIXME: Handle structs.
2873 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002874
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002875 if ((RightV.isUnknown() ||
2876 !getConstraintManager().canReasonAbout(RightV))
2877 && (Loc::IsLocType(T) ||
2878 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002879 unsigned Count = Builder->getCurrentBlockCount();
2880 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002881 }
2882
Ted Kremenekd4676512008-03-12 21:45:47 +00002883 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002884 // to the L-Value represented by the LHS.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002885 EvalStore(Dst, B, LHS, *I2, state->bindExpr(B, RightV), LeftV,
Ted Kremeneke66ba682009-02-13 01:45:31 +00002886 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002887 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002888 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002889
2890 case BinaryOperator::Div:
2891 case BinaryOperator::Rem:
2892
Ted Kremenek6c438f82008-10-20 23:40:25 +00002893 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002894 if (RHS->getType()->isIntegerType() &&
2895 RHS->getType()->isScalarType()) {
2896
Ted Kremeneke66ba682009-02-13 01:45:31 +00002897 state = CheckDivideZero(B, state, *I2, RightV);
2898 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002899 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002900
2901 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002902
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002903 default: {
2904
2905 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002906 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002907
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002908 // Process non-assignements except commas or short-circuited
2909 // logical expressions (LAnd and LOr).
Ted Kremenek07baa252008-02-21 18:02:17 +00002910
Zhongxing Xuc890e332009-05-20 09:00:16 +00002911 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002912
2913 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002914 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002915 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002916 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002917 }
2918 else
2919 Dst.Add(*I2);
2920
Ted Kremenekb8782e12008-02-21 19:15:37 +00002921 continue;
2922 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002923
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002924 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002925
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002926 // The operands were *not* undefined, but the result is undefined.
2927 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002928
Ted Kremeneke66ba682009-02-13 01:45:31 +00002929 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002930 UndefNode->markAsSink();
2931 UndefResults.insert(UndefNode);
2932 }
2933
2934 continue;
2935 }
2936
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002937 // Otherwise, create a new node.
2938
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002939 MakeNode(Dst, B, *I2, state->bindExpr(B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002940 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002941 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002942 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002943
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002944 assert (B->isCompoundAssignmentOp());
2945
Ted Kremenek570882a2009-02-07 00:52:24 +00002946 switch (Op) {
2947 default:
2948 assert(0 && "Invalid opcode for compound assignment.");
2949 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2950 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2951 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2952 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2953 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2954 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2955 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2956 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2957 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2958 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00002959 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002960
2961 // Perform a load (the LHS). This performs the checks for
2962 // null dereferences, and so on.
2963 NodeSet Tmp3;
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002964 SVal location = state->getSVal(LHS);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002965 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002966
2967 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2968
Ted Kremeneke66ba682009-02-13 01:45:31 +00002969 state = GetState(*I3);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002970 SVal V = state->getSVal(LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002971
Ted Kremenek6c438f82008-10-20 23:40:25 +00002972 // Check for divide-by-zero.
2973 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00002974 && RHS->getType()->isIntegerType()
2975 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002976
2977 // CheckDivideZero returns a new state where the denominator
2978 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002979 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002980
Ted Kremeneke66ba682009-02-13 01:45:31 +00002981 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00002982 continue;
2983 }
2984
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002985 // Propagate undefined values (left-side).
2986 if (V.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002987 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002988 continue;
2989 }
2990
2991 // Propagate unknown values (left and right-side).
2992 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002993 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, UnknownVal()),
Ted Kremeneke66ba682009-02-13 01:45:31 +00002994 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002995 continue;
2996 }
2997
2998 // At this point:
2999 //
3000 // The LHS is not Undef/Unknown.
3001 // The RHS is not Unknown.
3002
3003 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00003004 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003005 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00003006
3007 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3008 CLHSTy = getContext().getCanonicalType(CTy);
3009
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003010 QualType LTy = getContext().getCanonicalType(LHS->getType());
3011 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00003012
3013 // Promote LHS.
3014 V = EvalCast(V, CLHSTy);
3015
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003016 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00003017 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00003018 // Propagate undefined values (right-side).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003019 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003020 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003021 continue;
3022 }
3023
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003024 // Compute the result of the operation.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003025 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3026 B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003027
3028 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003029 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003030 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003031 UndefNode->markAsSink();
3032 UndefResults.insert(UndefNode);
3033 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003034 continue;
3035 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003036
3037 // EXPERIMENTAL: "Conjured" symbols.
3038 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003039
3040 SVal LHSVal;
3041
Ted Kremenekd6a5a422009-03-11 02:24:48 +00003042 if ((Result.isUnknown() ||
3043 !getConstraintManager().canReasonAbout(Result))
3044 && (Loc::IsLocType(CTy)
3045 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00003046
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003047 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003048
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003049 // The symbolic value is actually for the type of the left-hand side
3050 // expression, not the computation type, as this is the value the
3051 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00003052 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003053
Zhongxing Xu5c70c772008-11-23 05:52:28 +00003054 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003055 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003056 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003057 else {
3058 // The left-hand side may bind to a different value then the
3059 // computation type.
3060 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3061 }
3062
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003063 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, Result), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003064 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003065 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00003066 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003067 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003068}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003069
3070//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003071// Transfer-function Helpers.
3072//===----------------------------------------------------------------------===//
3073
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003074void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003075 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +00003076 NonLoc L, NonLoc R,
Ted Kremenek74556a12009-03-26 03:35:11 +00003077 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003078
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003079 GRStateSet OStates;
Ted Kremenek74556a12009-03-26 03:35:11 +00003080 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003081
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003082 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003083 MakeNode(Dst, Ex, Pred, *I);
3084}
3085
Ted Kremeneke66ba682009-02-13 01:45:31 +00003086void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003087 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +00003088 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003089
Ted Kremeneke66ba682009-02-13 01:45:31 +00003090 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremenek74556a12009-03-26 03:35:11 +00003091 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003092}
3093
Zhongxing Xuc890e332009-05-20 09:00:16 +00003094SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3095 SVal L, SVal R, QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00003096
3097 if (L.isUndef() || R.isUndef())
3098 return UndefinedVal();
3099
3100 if (L.isUnknown() || R.isUnknown())
3101 return UnknownVal();
3102
3103 if (isa<Loc>(L)) {
3104 if (isa<Loc>(R))
3105 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3106 else
Zhongxing Xuc890e332009-05-20 09:00:16 +00003107 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek4281e622009-01-30 19:27:39 +00003108 }
3109
3110 if (isa<Loc>(R)) {
3111 // Support pointer arithmetic where the increment/decrement operand
3112 // is on the left and the pointer on the right.
3113
3114 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3115
3116 // Commute the operands.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003117 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek4281e622009-01-30 19:27:39 +00003118 }
3119 else
3120 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremenek74556a12009-03-26 03:35:11 +00003121 cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003122}
3123
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003124//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00003125// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003126//===----------------------------------------------------------------------===//
3127
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003128#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003129static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003130static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00003131
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003132namespace llvm {
3133template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003134struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003135 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00003136
Ted Kremeneka853de62008-02-14 22:54:53 +00003137 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3138
3139 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00003140 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00003141 GraphPrintCheckerState->isUndefDeref(N) ||
3142 GraphPrintCheckerState->isUndefStore(N) ||
3143 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00003144 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3145 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00003146 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00003147 GraphPrintCheckerState->isBadCall(N) ||
3148 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00003149 return "color=\"red\",style=\"filled\"";
3150
Ted Kremenekc2d07202008-02-28 20:32:03 +00003151 if (GraphPrintCheckerState->isNoReturnCall(N))
3152 return "color=\"blue\",style=\"filled\"";
3153
Ted Kremeneka853de62008-02-14 22:54:53 +00003154 return "";
3155 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003156
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003157 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003158 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003159
3160 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003161 ProgramPoint Loc = N->getLocation();
3162
3163 switch (Loc.getKind()) {
3164 case ProgramPoint::BlockEntranceKind:
3165 Out << "Block Entrance: B"
3166 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3167 break;
3168
3169 case ProgramPoint::BlockExitKind:
3170 assert (false);
3171 break;
3172
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003173 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003174 if (isa<PostStmt>(Loc)) {
3175 const PostStmt& L = cast<PostStmt>(Loc);
3176 Stmt* S = L.getStmt();
3177 SourceLocation SLoc = S->getLocStart();
3178
3179 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3180 llvm::raw_os_ostream OutS(Out);
3181 S->printPretty(OutS);
3182 OutS.flush();
3183
3184 if (SLoc.isFileID()) {
3185 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003186 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3187 << " col="
3188 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3189 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003190 }
3191
Ted Kremenek0441f112009-05-07 18:27:16 +00003192 if (isa<PostLoad>(Loc))
3193 Out << "\\lPostLoad\\l;";
3194 else if (isa<PostStore>(Loc))
3195 Out << "\\lPostStore\\l";
3196 else if (isa<PostLValue>(Loc))
3197 Out << "\\lPostLValue\\l";
3198 else if (isa<PostLocationChecksSucceed>(Loc))
3199 Out << "\\lPostLocationChecksSucceed\\l";
3200 else if (isa<PostNullCheckFailed>(Loc))
3201 Out << "\\lPostNullCheckFailed\\l";
3202
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003203 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3204 Out << "\\|Implicit-Null Dereference.\\l";
3205 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3206 Out << "\\|Explicit-Null Dereference.\\l";
3207 else if (GraphPrintCheckerState->isUndefDeref(N))
3208 Out << "\\|Dereference of undefialied value.\\l";
3209 else if (GraphPrintCheckerState->isUndefStore(N))
3210 Out << "\\|Store to Undefined Loc.";
3211 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3212 Out << "\\|Explicit divide-by zero or undefined value.";
3213 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3214 Out << "\\|Implicit divide-by zero or undefined value.";
3215 else if (GraphPrintCheckerState->isUndefResult(N))
3216 Out << "\\|Result of operation is undefined.";
3217 else if (GraphPrintCheckerState->isNoReturnCall(N))
3218 Out << "\\|Call to function marked \"noreturn\".";
3219 else if (GraphPrintCheckerState->isBadCall(N))
3220 Out << "\\|Call to NULL/Undefined.";
3221 else if (GraphPrintCheckerState->isUndefArg(N))
3222 Out << "\\|Argument in call is undefined";
3223
3224 break;
3225 }
3226
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003227 const BlockEdge& E = cast<BlockEdge>(Loc);
3228 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3229 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003230
3231 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003232
3233 SourceLocation SLoc = T->getLocStart();
3234
Ted Kremenek90960972008-01-30 23:03:39 +00003235 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003236
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003237 llvm::raw_os_ostream OutS(Out);
3238 E.getSrc()->printTerminator(OutS);
3239 OutS.flush();
Ted Kremenek90960972008-01-30 23:03:39 +00003240
Ted Kremenekf97c6682008-03-09 03:30:59 +00003241 if (SLoc.isFileID()) {
3242 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003243 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3244 << " col="
3245 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003246 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003247
Ted Kremenekaee121c2008-02-13 23:08:21 +00003248 if (isa<SwitchStmt>(T)) {
3249 Stmt* Label = E.getDst()->getLabel();
3250
3251 if (Label) {
3252 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3253 Out << "\\lcase ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003254 llvm::raw_os_ostream OutS(Out);
3255 C->getLHS()->printPretty(OutS);
3256 OutS.flush();
3257
Ted Kremenekaee121c2008-02-13 23:08:21 +00003258 if (Stmt* RHS = C->getRHS()) {
3259 Out << " .. ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003260 RHS->printPretty(OutS);
3261 OutS.flush();
Ted Kremenekaee121c2008-02-13 23:08:21 +00003262 }
3263
3264 Out << ":";
3265 }
3266 else {
3267 assert (isa<DefaultStmt>(Label));
3268 Out << "\\ldefault:";
3269 }
3270 }
3271 else
3272 Out << "\\l(implicit) default:";
3273 }
3274 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003275 // FIXME
3276 }
3277 else {
3278 Out << "\\lCondition: ";
3279 if (*E.getSrc()->succ_begin() == E.getDst())
3280 Out << "true";
3281 else
3282 Out << "false";
3283 }
3284
3285 Out << "\\l";
3286 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003287
Ted Kremenekb31af242008-02-28 09:25:22 +00003288 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3289 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003290 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003291 }
3292 }
3293
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003294 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003295
Ted Kremenek18a636d2009-06-18 01:23:53 +00003296 const GRState *state = N->getState();
3297 state->printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003298
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003299 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003300 return Out.str();
3301 }
3302};
3303} // end llvm namespace
3304#endif
3305
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003306#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003307template <typename ITERATOR>
3308GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3309
3310template <>
3311GRExprEngine::NodeTy*
3312GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3313 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3314 return I->first;
3315}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003316#endif
3317
3318void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003319#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003320 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003321 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003322
3323 // Flush any outstanding reports to make sure we cover all the nodes.
3324 // This does not cause them to get displayed.
3325 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3326 const_cast<BugType*>(*I)->FlushReports(BR);
3327
3328 // Iterate through the reports and get their nodes.
3329 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3330 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3331 const BugReportEquivClass& EQ = *I2;
3332 const BugReport &R = **EQ.begin();
3333 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3334 if (N) Src.push_back(N);
3335 }
3336 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003337
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003338 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003339 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003340 else {
3341 GraphPrintCheckerState = this;
3342 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003343
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003344 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003345
3346 GraphPrintCheckerState = NULL;
3347 GraphPrintSourceManager = NULL;
3348 }
3349#endif
3350}
3351
3352void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3353#ifndef NDEBUG
3354 GraphPrintCheckerState = this;
3355 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003356
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003357 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003358
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003359 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003360 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003361 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003362 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003363
Ted Kremenek428d39e2008-01-30 23:24:39 +00003364 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003365 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003366#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003367}