blob: 97e75fa0aca02942df7e762484edea294a7be3fb [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"
Chris Lattner4a9e9272009-04-26 01:32:48 +000018#include "clang/AST/ParentMap.h"
19#include "clang/AST/StmtObjC.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000020#include "clang/Basic/Builtins.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000021#include "clang/Basic/SourceManager.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000023#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000024#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000025#include "llvm/ADT/ImmutableList.h"
26#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000028
Ted Kremenek9f6b1612008-02-27 06:07:00 +000029#ifndef NDEBUG
30#include "llvm/Support/GraphWriter.h"
Ted Kremenek9f6b1612008-02-27 06:07:00 +000031#endif
32
Ted Kremenekd4467432008-02-14 22:16:04 +000033using namespace clang;
34using llvm::dyn_cast;
35using llvm::cast;
36using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000037
Ted Kremenekca5f6202008-04-15 23:06:53 +000038//===----------------------------------------------------------------------===//
39// Engine construction and deletion.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000042namespace {
43
44class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
45 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
46 typedef llvm::DenseMap<void*,Checks> MapTy;
47
48 MapTy M;
49 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000050 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000051
52public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000053 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
54 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000055
56 virtual ~MappedBatchAuditor() {
57 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
58
59 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
60 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
61
62 GRSimpleAPICheck* check = *I;
63
64 if (AlreadyVisited.count(check))
65 continue;
66
67 AlreadyVisited.insert(check);
68 delete check;
69 }
70 }
71
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000072 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000073 assert (A && "Check cannot be null.");
74 void* key = reinterpret_cast<void*>((uintptr_t) C);
75 MapTy::iterator I = M.find(key);
76 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
77 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000078
79 void AddCheck(GRSimpleAPICheck *A) {
80 assert (A && "Check cannot be null.");
81 AllStmts = F.Concat(A, AllStmts);
82 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000083
Ted Kremenekabd89ac2008-08-13 04:27:00 +000084 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000085 // First handle the auditors that accept all statements.
86 bool isSink = false;
87 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
88 isSink |= (*I)->Audit(N, VMgr);
89
90 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000091 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
92 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
93 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000094 if (MI != M.end()) {
95 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
96 isSink |= (*I)->Audit(N, VMgr);
97 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000098
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000099 return isSink;
100 }
101};
102
103} // end anonymous namespace
104
105//===----------------------------------------------------------------------===//
106// Engine construction and deletion.
107//===----------------------------------------------------------------------===//
108
Ted Kremenek5f20a632008-05-01 18:33:28 +0000109static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
110 IdentifierInfo* II = &Ctx.Idents.get(name);
111 return Ctx.Selectors.getSelector(0, &II);
112}
113
Ted Kremenekf973eb02008-03-09 18:05:48 +0000114
Ted Kremenek1607f512008-07-02 20:13:38 +0000115GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000116 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000117 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000118 StoreManagerCreator SMC,
119 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000120 : CoreEngine(cfg, CD, Ctx, *this),
121 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000122 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000123 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000124 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000125 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000126 ValMgr(StateMgr.getValueManager()),
Ted Kremenek40d9c582009-07-16 01:32:00 +0000127 SVator(ValMgr.getSValuator()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000128 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000129 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
130 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000131 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000132 BR(BRD, *this),
133 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000134
Ted Kremenek72f52c02008-06-20 21:45:25 +0000135GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000136 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000137 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000138}
139
Ted Kremenekca5f6202008-04-15 23:06:53 +0000140//===----------------------------------------------------------------------===//
141// Utility methods.
142//===----------------------------------------------------------------------===//
143
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000144
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000145void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000146 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000147 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000148 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000149}
150
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000151void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
152 if (!BatchAuditor)
153 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
154
155 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000156}
157
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000158void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
159 if (!BatchAuditor)
160 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
161
162 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
163}
164
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000165const GRState* GRExprEngine::getInitialState() {
Ted Kremenek04c0add2009-04-10 00:59:50 +0000166 const GRState *state = StateMgr.getInitialState();
167
168 // Precondition: the first argument of 'main' is an integer guaranteed
169 // to be > 0.
170 // FIXME: It would be nice if we had a more general mechanism to add
171 // such preconditions. Some day.
172 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
173 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
174 FD->getNumParams() > 0) {
175 const ParmVarDecl *PD = FD->getParamDecl(0);
176 QualType T = PD->getType();
177 if (T->isIntegerType())
Ted Kremenekf6f0a872009-06-23 21:37:46 +0000178 if (const MemRegion *R = state->getRegion(PD)) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000179 SVal V = state->getSVal(loc::MemRegionVal(R));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000180 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek04c0add2009-04-10 00:59:50 +0000181 ValMgr.makeZeroVal(T),
182 getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +0000183
184 if (const GRState *newState = state->assume(Constraint, true))
185 state = newState;
Ted Kremenek04c0add2009-04-10 00:59:50 +0000186 }
187 }
188
189 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000190}
191
Ted Kremenekca5f6202008-04-15 23:06:53 +0000192//===----------------------------------------------------------------------===//
193// Top-level transfer function logic (Dispatcher).
194//===----------------------------------------------------------------------===//
195
196void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
197
Ted Kremenek820c73b2009-03-11 02:41:36 +0000198 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
199 S->getLocStart(),
200 "Error evaluating statement");
201
Ted Kremenekca5f6202008-04-15 23:06:53 +0000202 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000212
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000214 SymbolReaper SymReaper(Liveness, SymMgr);
215 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
216 CurrentStmt, SymReaper)
217 : EntryNode->getState();
218
Ted Kremenek7487f942008-04-24 18:31:42 +0000219 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000220 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000221
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000222 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000223 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000224 else {
225 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000226 SaveOr OldHasGen(Builder->HasGeneratedNode);
227
Ted Kremenekf05eec42008-06-18 05:34:07 +0000228 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
229 Builder->PurgingDeadSymbols = true;
230
Ted Kremenekc7469542008-07-17 23:15:45 +0000231 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000232 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000233
234 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
235 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000236 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000237
238 bool HasAutoGenerated = false;
239
Ted Kremenek7487f942008-04-24 18:31:42 +0000240 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000241
242 NodeSet Dst;
243
Ted Kremenek7487f942008-04-24 18:31:42 +0000244 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000245 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
246
Ted Kremenek7487f942008-04-24 18:31:42 +0000247 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000248 Visit(S, *I, Dst);
249
250 // Do we need to auto-generate a node? We only need to do this to generate
251 // a node with a "cleaned" state; GRCoreEngine will actually handle
252 // auto-transitions for other cases.
253 if (Dst.size() == 1 && *Dst.begin() == EntryNode
254 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
255 HasAutoGenerated = true;
256 builder.generateNode(S, GetState(EntryNode), *I);
257 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000258 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000259
Ted Kremenekca5f6202008-04-15 23:06:53 +0000260 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000261 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000262 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000263
264 // FIXME: Consolidate.
265 StateMgr.CurrentStmt = 0;
266 CurrentStmt = 0;
267
Ted Kremenekfa7be362008-04-24 23:35:58 +0000268 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000269}
270
Ted Kremenek820c73b2009-03-11 02:41:36 +0000271void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
272 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
273 S->getLocStart(),
274 "Error evaluating statement");
275
Ted Kremenekca5f6202008-04-15 23:06:53 +0000276 // FIXME: add metadata to the CFG so that we can disable
277 // this check when we KNOW that there is no block-level subexpression.
278 // The motivation is that this check requires a hashtable lookup.
279
280 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
281 Dst.Add(Pred);
282 return;
283 }
284
285 switch (S->getStmtClass()) {
286
287 default:
288 // Cases we intentionally have "default" handle:
289 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
290
291 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
292 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000293
294 case Stmt::ArraySubscriptExprClass:
295 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
296 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000297
298 case Stmt::AsmStmtClass:
299 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
300 break;
301
302 case Stmt::BinaryOperatorClass: {
303 BinaryOperator* B = cast<BinaryOperator>(S);
304
305 if (B->isLogicalOp()) {
306 VisitLogicalExpr(B, Pred, Dst);
307 break;
308 }
309 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000310 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000311 MakeNode(Dst, B, Pred, state->bindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000312 break;
313 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000314
Ted Kremenek8f520972009-02-25 22:32:02 +0000315 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
316 NodeSet Tmp;
317 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000318 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000319 }
320 else
321 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
322
Ted Kremenekca5f6202008-04-15 23:06:53 +0000323 break;
324 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000325
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000326 case Stmt::CallExprClass:
327 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000328 CallExpr* C = cast<CallExpr>(S);
329 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000330 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000331 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000332
Ted Kremenekca5f6202008-04-15 23:06:53 +0000333 // FIXME: ChooseExpr is really a constant. We need to fix
334 // the CFG do not model them as explicit control-flow.
335
336 case Stmt::ChooseExprClass: { // __builtin_choose_expr
337 ChooseExpr* C = cast<ChooseExpr>(S);
338 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
339 break;
340 }
341
342 case Stmt::CompoundAssignOperatorClass:
343 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
344 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000345
346 case Stmt::CompoundLiteralExprClass:
347 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
348 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000349
350 case Stmt::ConditionalOperatorClass: { // '?' operator
351 ConditionalOperator* C = cast<ConditionalOperator>(S);
352 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
353 break;
354 }
355
356 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000357 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000358 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000359 break;
360
361 case Stmt::DeclStmtClass:
362 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
363 break;
364
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000365 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000366 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000367 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000368 VisitCast(C, C->getSubExpr(), Pred, Dst);
369 break;
370 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000371
372 case Stmt::InitListExprClass:
373 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
374 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000375
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000376 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000377 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
378 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000379
380 case Stmt::ObjCIvarRefExprClass:
381 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
382 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000383
384 case Stmt::ObjCForCollectionStmtClass:
385 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
386 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000387
Ted Kremenekca5f6202008-04-15 23:06:53 +0000388 case Stmt::ObjCMessageExprClass: {
389 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
390 break;
391 }
392
Ted Kremenek3c186252008-12-09 20:18:58 +0000393 case Stmt::ObjCAtThrowStmtClass: {
394 // FIXME: This is not complete. We basically treat @throw as
395 // an abort.
396 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
397 Builder->BuildSinks = true;
398 MakeNode(Dst, S, Pred, GetState(Pred));
399 break;
400 }
401
Ted Kremenekca5f6202008-04-15 23:06:53 +0000402 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000403 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000404 break;
405
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000406 case Stmt::ReturnStmtClass:
407 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
408 break;
409
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000410 case Stmt::SizeOfAlignOfExprClass:
411 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000412 break;
413
414 case Stmt::StmtExprClass: {
415 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000416
417 if (SE->getSubStmt()->body_empty()) {
418 // Empty statement expression.
419 assert(SE->getType() == getContext().VoidTy
420 && "Empty statement expression must have void type.");
421 Dst.Add(Pred);
422 break;
423 }
424
425 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
426 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000427 MakeNode(Dst, SE, Pred, state->bindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000428 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000429 else
430 Dst.Add(Pred);
431
432 break;
433 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000434
435 case Stmt::StringLiteralClass:
436 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
437 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000438
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000439 case Stmt::UnaryOperatorClass: {
440 UnaryOperator *U = cast<UnaryOperator>(S);
441 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
442 NodeSet Tmp;
443 VisitUnaryOperator(U, Pred, Tmp, false);
444 EvalEagerlyAssume(Dst, Tmp, U);
445 }
446 else
447 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000448 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000449 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000450 }
451}
452
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000453void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000454
455 Ex = Ex->IgnoreParens();
456
457 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
458 Dst.Add(Pred);
459 return;
460 }
461
462 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000463
464 case Stmt::ArraySubscriptExprClass:
465 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
466 return;
467
468 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000469 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000470 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
471 return;
472
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000473 case Stmt::ObjCIvarRefExprClass:
474 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
475 return;
476
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000477 case Stmt::UnaryOperatorClass:
478 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
479 return;
480
481 case Stmt::MemberExprClass:
482 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
483 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000484
Ted Kremenekd83daa52008-10-27 21:54:31 +0000485 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000486 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000487 return;
488
Ted Kremenek71c707b2008-10-17 17:24:14 +0000489 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek93cb3d12009-04-21 23:53:32 +0000490 case Stmt::ObjCKVCRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000491 // FIXME: Property assignments are lvalues, but not really "locations".
492 // e.g.: self.x = something;
493 // Here the "self.x" really can translate to a method call (setter) when
494 // the assignment is made. Moreover, the entire assignment expression
495 // evaluate to whatever "something" is, not calling the "getter" for
496 // the property (which would make sense since it can have side effects).
497 // We'll probably treat this as a location, but not one that we can
498 // take the address of. Perhaps we need a new SVal class for cases
499 // like thsis?
500 // Note that we have a similar problem for bitfields, since they don't
501 // have "locations" in the sense that we can take their address.
502 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000503 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000504
505 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000506 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000507 SVal V = state->getLValue(cast<StringLiteral>(Ex));
508 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000509 return;
510 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000511
Ted Kremenek2c829a32008-10-18 04:15:35 +0000512 default:
513 // Arbitrary subexpressions can return aggregate temporaries that
514 // can be used in a lvalue context. We need to enhance our support
515 // of such temporaries in both the environment and the store, so right
516 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000517 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000518 "Other kinds of expressions with non-aggregate/union types do"
519 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000520
Ted Kremenek2c829a32008-10-18 04:15:35 +0000521 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000522 }
523}
524
525//===----------------------------------------------------------------------===//
526// Block entrance. (Update counters).
527//===----------------------------------------------------------------------===//
528
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000529bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000530 GRBlockCounter BC) {
531
532 return BC.getNumVisited(B->getBlockID()) < 3;
533}
534
535//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000536// Generic node creation.
537//===----------------------------------------------------------------------===//
538
539GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
540 NodeTy* Pred,
541 const GRState* St,
542 ProgramPoint::Kind K,
543 const void *tag) {
544
545 assert (Builder && "GRStmtNodeBuilder not present.");
546 SaveAndRestore<const void*> OldTag(Builder->Tag);
547 Builder->Tag = tag;
548 return Builder->MakeNode(Dst, S, Pred, St, K);
549}
550
551//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000552// Branch processing.
553//===----------------------------------------------------------------------===//
554
Ted Kremeneke66ba682009-02-13 01:45:31 +0000555const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000556 Stmt* Terminator,
557 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000558
559 switch (Terminator->getStmtClass()) {
560 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000561 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000562
563 case Stmt::BinaryOperatorClass: { // '&&' and '||'
564
565 BinaryOperator* B = cast<BinaryOperator>(Terminator);
566 BinaryOperator::Opcode Op = B->getOpcode();
567
568 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
569
570 // For &&, if we take the true branch, then the value of the whole
571 // expression is that of the RHS expression.
572 //
573 // For ||, if we take the false branch, then the value of the whole
574 // expression is that of the RHS expression.
575
576 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
577 (Op == BinaryOperator::LOr && !branchTaken)
578 ? B->getRHS() : B->getLHS();
579
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000580 return state->bindBlkExpr(B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000581 }
582
583 case Stmt::ConditionalOperatorClass: { // ?:
584
585 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
586
587 // For ?, if branchTaken == true then the value is either the LHS or
588 // the condition itself. (GNU extension).
589
590 Expr* Ex;
591
592 if (branchTaken)
593 Ex = C->getLHS() ? C->getLHS() : C->getCond();
594 else
595 Ex = C->getRHS();
596
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000597 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000598 }
599
600 case Stmt::ChooseExprClass: { // ?:
601
602 ChooseExpr* C = cast<ChooseExpr>(Terminator);
603
604 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000605 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000606 }
607 }
608}
609
Ted Kremenekc39c2172009-03-13 16:32:54 +0000610/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
611/// to try to recover some path-sensitivity for casts of symbolic
612/// integers that promote their values (which are currently not tracked well).
613/// This function returns the SVal bound to Condition->IgnoreCasts if all the
614// cast(s) did was sign-extend the original value.
615static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
616 Stmt* Condition, ASTContext& Ctx) {
617
618 Expr *Ex = dyn_cast<Expr>(Condition);
619 if (!Ex)
620 return UnknownVal();
621
622 uint64_t bits = 0;
623 bool bitsInit = false;
624
625 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
626 QualType T = CE->getType();
627
628 if (!T->isIntegerType())
629 return UnknownVal();
630
631 uint64_t newBits = Ctx.getTypeSize(T);
632 if (!bitsInit || newBits < bits) {
633 bitsInit = true;
634 bits = newBits;
635 }
636
637 Ex = CE->getSubExpr();
638 }
639
640 // We reached a non-cast. Is it a symbolic value?
641 QualType T = Ex->getType();
642
643 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
644 return UnknownVal();
645
Ted Kremenekc0cccca2009-06-18 23:58:37 +0000646 return state->getSVal(Ex);
Ted Kremenekc39c2172009-03-13 16:32:54 +0000647}
648
Ted Kremenek13e167f2008-11-12 19:24:17 +0000649void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000650 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000651
Ted Kremenek17c5f112008-02-11 19:21:59 +0000652 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000653 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000654 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000655
Ted Kremenek022b6052008-02-15 22:29:00 +0000656 // Check for NULL conditions; e.g. "for(;;)"
657 if (!Condition) {
658 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000659 return;
660 }
661
Ted Kremeneke43de222009-03-11 03:54:24 +0000662 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
663 Condition->getLocStart(),
664 "Error evaluating branch");
665
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000666 SVal V = PrevState->getSVal(Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000667
668 switch (V.getBaseKind()) {
669 default:
670 break;
671
Ted Kremenekc39c2172009-03-13 16:32:54 +0000672 case SVal::UnknownKind: {
673 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
674 if (Ex->getType()->isIntegerType()) {
675 // Try to recover some path-sensitivity. Right now casts of symbolic
676 // integers that promote their values are currently not tracked well.
677 // If 'Condition' is such an expression, try and recover the
678 // underlying value and use that instead.
679 SVal recovered = RecoverCastedSymbol(getStateManager(),
680 builder.getState(), Condition,
681 getContext());
682
683 if (!recovered.isUnknown()) {
684 V = recovered;
685 break;
686 }
687 }
688 }
689
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000690 builder.generateNode(MarkBranch(PrevState, Term, true), true);
691 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000692 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000693 }
Ted Kremenek90960972008-01-30 23:03:39 +0000694
Zhongxing Xu097fc982008-10-17 05:57:07 +0000695 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000696 NodeTy* N = builder.generateNode(PrevState, true);
697
698 if (N) {
699 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000700 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000701 }
702
703 builder.markInfeasible(false);
704 return;
705 }
706 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000707
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000708 // Process the true branch.
Ted Kremenek5f302192009-07-20 18:44:36 +0000709 if (builder.isFeasible(true)) {
710 if (const GRState *state = PrevState->assume(V, true))
711 builder.generateNode(MarkBranch(state, Term, true), true);
712 else
713 builder.markInfeasible(true);
714 }
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000715
716 // Process the false branch.
Ted Kremenek5f302192009-07-20 18:44:36 +0000717 if (builder.isFeasible(false)) {
718 if (const GRState *state = PrevState->assume(V, false))
719 builder.generateNode(MarkBranch(state, Term, false), false);
720 else
721 builder.markInfeasible(false);
722 }
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000723}
724
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000725/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000726/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000727void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000728
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000729 const GRState *state = builder.getState();
730 SVal V = state->getSVal(builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000731
732 // Three possibilities:
733 //
734 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000735 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000736 // (3) We have no clue about the label. Dispatch to all targets.
737 //
738
739 typedef IndirectGotoNodeBuilder::iterator iterator;
740
Zhongxing Xu097fc982008-10-17 05:57:07 +0000741 if (isa<loc::GotoLabel>(V)) {
742 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000743
744 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000745 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000746 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000747 return;
748 }
749 }
750
751 assert (false && "No block with label.");
752 return;
753 }
754
Zhongxing Xu097fc982008-10-17 05:57:07 +0000755 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000756 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000757 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000758 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000759 return;
760 }
761
762 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekcdc3a3c2009-04-23 17:49:43 +0000763 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000764
765 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000766 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000767}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000768
Ted Kremenekca5f6202008-04-15 23:06:53 +0000769
770void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
771 NodeTy* Pred, NodeSet& Dst) {
772
773 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
774
Ted Kremeneke66ba682009-02-13 01:45:31 +0000775 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000776 SVal X = state->getBlkExprSVal(Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000777
778 assert (X.isUndef());
779
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000780 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
781 assert(SE);
782 X = state->getBlkExprSVal(SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000783
784 // Make sure that we invalidate the previous binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000785 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000786}
787
Ted Kremenekaee121c2008-02-13 23:08:21 +0000788/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
789/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000790void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
791 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000792 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000793 Expr* CondE = builder.getCondition();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000794 SVal CondV = state->getSVal(CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000795
Ted Kremenekb31af242008-02-28 09:25:22 +0000796 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000797 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000798 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000799 return;
800 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000801
Ted Kremeneke66ba682009-02-13 01:45:31 +0000802 const GRState* DefaultSt = state;
Ted Kremenek70970bf2009-06-18 22:57:13 +0000803 bool defaultIsFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000804
Ted Kremenek07baa252008-02-21 18:02:17 +0000805 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000806 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000807
808 // Evaluate the LHS of the case value.
809 Expr::EvalResult V1;
810 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000811
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000812 // Sanity checks. These go away in Release builds.
813 assert(b && V1.Val.isInt() && !V1.HasSideEffects
814 && "Case condition must evaluate to an integer constant.");
815 b = b; // silence unused variable warning
816 assert(V1.Val.getInt().getBitWidth() ==
817 getContext().getTypeSize(CondE->getType()));
818
Ted Kremenekaee121c2008-02-13 23:08:21 +0000819 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000820 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000821
822 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000823 b = E->Evaluate(V2, getContext());
824 assert(b && V2.Val.isInt() && !V2.HasSideEffects
825 && "Case condition must evaluate to an integer constant.");
826 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000827 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000828 else
829 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000830
831 // FIXME: Eventually we should replace the logic below with a range
832 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000833 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000834
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000835 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000836 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000837 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremenek74556a12009-03-26 03:35:11 +0000838 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000839
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000840 // Now "assume" that the case matches.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000841 if (const GRState* stateNew = state->assume(Res, true)) {
842 builder.generateCaseStmtNode(I, stateNew);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000843
844 // If CondV evaluates to a constant, then we know that this
845 // is the *only* case that we can take, so stop evaluating the
846 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000847 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000848 return;
849 }
850
851 // Now "assume" that the case doesn't match. Add this state
852 // to the default state (if it is feasible).
Ted Kremenek70970bf2009-06-18 22:57:13 +0000853 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
854 defaultIsFeasible = true;
855 DefaultSt = stateNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000856 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000857
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000858 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000859 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000860 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000861
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000862 ++V1.Val.getInt();
863 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000864
865 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000866 }
867
868 // If we reach here, than we know that the default branch is
869 // possible.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000870 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000871}
872
Ted Kremenekca5f6202008-04-15 23:06:53 +0000873//===----------------------------------------------------------------------===//
874// Transfer functions: logical operations ('&&', '||').
875//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000876
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000877void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000878 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000879
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000880 assert(B->getOpcode() == BinaryOperator::LAnd ||
881 B->getOpcode() == BinaryOperator::LOr);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000882
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000883 assert(B == CurrentStmt && getCFG().isBlkExpr(B));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000884
Ted Kremeneke66ba682009-02-13 01:45:31 +0000885 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000886 SVal X = state->getBlkExprSVal(B);
887 assert(X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000888
Ted Kremenekb31af242008-02-28 09:25:22 +0000889 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000890
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000891 assert(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000892
893 if (Ex == B->getRHS()) {
894
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000895 X = state->getBlkExprSVal(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000896
Ted Kremenekb31af242008-02-28 09:25:22 +0000897 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000898
Ted Kremenekb31af242008-02-28 09:25:22 +0000899 if (X.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000900 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000901 return;
902 }
903
Ted Kremenek99ecce72008-02-26 19:05:15 +0000904 // We took the RHS. Because the value of the '&&' or '||' expression must
905 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
906 // or 1. Alternatively, we could take a lazy approach, and calculate this
907 // value later when necessary. We don't have the machinery in place for
908 // this right now, and since most logical expressions are used for branches,
Ted Kremenek70970bf2009-06-18 22:57:13 +0000909 // the payoff is not likely to be large. Instead, we do eager evaluation.
910 if (const GRState *newState = state->assume(X, true))
Zhongxing Xue32c7652009-06-23 09:02:15 +0000911 MakeNode(Dst, B, Pred,
912 newState->bindBlkExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000913
Ted Kremenek70970bf2009-06-18 22:57:13 +0000914 if (const GRState *newState = state->assume(X, false))
Zhongxing Xue32c7652009-06-23 09:02:15 +0000915 MakeNode(Dst, B, Pred,
916 newState->bindBlkExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000917 }
918 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000919 // We took the LHS expression. Depending on whether we are '&&' or
920 // '||' we know what the value of the expression is via properties of
921 // the short-circuiting.
Zhongxing Xue32c7652009-06-23 09:02:15 +0000922 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
923 B->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000924 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000925 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000926}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000927
Ted Kremenekca5f6202008-04-15 23:06:53 +0000928//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000929// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000930//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000931
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000932void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
933 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000934
Ted Kremeneke66ba682009-02-13 01:45:31 +0000935 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000936
Douglas Gregord2baafd2008-10-21 16:13:35 +0000937 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000938
939 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
940
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000941 SVal V = state->getLValue(VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000942
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000943 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000944 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000945 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000946 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000947 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000948 return;
949
950 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
951 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
952
Zhongxing Xu3c2828d2009-06-23 06:13:19 +0000953 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000954 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000955 return;
956
957 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000958 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000959 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000960 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000961 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000962 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000963 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000964
965 assert (false &&
966 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000967}
968
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000969/// VisitArraySubscriptExpr - Transfer function for array accesses
970void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000971 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000972
973 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000974 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000975 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000976
977 if (Base->getType()->isVectorType()) {
978 // For vector types get its lvalue.
979 // FIXME: This may not be correct. Is the rvalue of a vector its location?
980 // In fact, I think this is just a hack. We need to get the right
981 // semantics.
982 VisitLValue(Base, Pred, Tmp);
983 }
984 else
985 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000986
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000987 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +0000988 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000989 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +0000990
991 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000992 const GRState* state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000993 SVal V = state->getLValue(A->getType(), state->getSVal(Base),
994 state->getSVal(Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +0000995
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000996 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000997 MakeNode(Dst, A, *I2, state->bindExpr(A, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000998 ProgramPoint::PostLValueKind);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000999 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001000 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +00001001 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001002 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +00001003}
1004
Ted Kremenekd0d86202008-04-21 23:43:38 +00001005/// VisitMemberExpr - Transfer function for member expressions.
1006void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001007 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +00001008
1009 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +00001010 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +00001011
1012 if (M->isArrow())
1013 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1014 else
1015 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1016
Douglas Gregor82d44772008-12-20 23:49:58 +00001017 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1018 if (!Field) // FIXME: skipping member expressions for non-fields
1019 return;
1020
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001021 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001022 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001023 // FIXME: Should we insert some assumption logic in here to determine
1024 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001025 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001026 SVal L = state->getLValue(state->getSVal(Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001027
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001028 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001029 MakeNode(Dst, M, *I, state->bindExpr(M, L),
Ted Kremenek0441f112009-05-07 18:27:16 +00001030 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001031 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001032 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001033 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001034}
1035
Ted Kremeneke66ba682009-02-13 01:45:31 +00001036/// EvalBind - Handle the semantics of binding a value to a specific location.
1037/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1038void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1039 const GRState* state, SVal location, SVal Val) {
1040
Ted Kremeneka42be302009-02-14 01:43:44 +00001041 const GRState* newState = 0;
1042
1043 if (location.isUnknown()) {
1044 // We know that the new state will be the same as the old state since
1045 // the location of the binding is "unknown". Consequently, there
1046 // is no reason to just create a new node.
1047 newState = state;
1048 }
1049 else {
1050 // We are binding to a value other than 'unknown'. Perform the binding
1051 // using the StoreManager.
Ted Kremenek49b6a8e2009-06-23 20:38:51 +00001052 newState = state->bindLoc(cast<Loc>(location), Val);
Ted Kremeneka42be302009-02-14 01:43:44 +00001053 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001054
Ted Kremeneka42be302009-02-14 01:43:44 +00001055 // The next thing to do is check if the GRTransferFuncs object wants to
1056 // update the state based on the new binding. If the GRTransferFunc object
1057 // doesn't do anything, just auto-propagate the current state.
1058 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1059 newState != state);
1060
1061 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001062}
1063
1064/// EvalStore - Handle the semantics of a store via an assignment.
1065/// @param Dst The node set to store generated state nodes
1066/// @param Ex The expression representing the location of the store
1067/// @param state The current simulation state
1068/// @param location The location to store the value
1069/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001070void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001071 const GRState* state, SVal location, SVal Val,
1072 const void *tag) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001073
1074 assert (Builder && "GRStmtNodeBuilder must be defined.");
1075
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001076 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001077 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001078
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001079 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001080 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001081
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001082 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001083 state = GetState(Pred);
1084
1085 // Proceed with the store.
1086 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001087 SaveAndRestore<const void*> OldTag(Builder->Tag);
1088 Builder->PointKind = ProgramPoint::PostStoreKind;
1089 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001090 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001091}
1092
1093void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001094 const GRState* state, SVal location,
1095 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001096
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001097 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001098 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001099
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001100 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001101 return;
1102
Ted Kremeneke66ba682009-02-13 01:45:31 +00001103 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001104
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001105 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001106 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001107
1108 // FIXME: Currently symbolic analysis "generates" new symbols
1109 // for the contents of values. We need a better approach.
1110
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001111 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001112 // This is important. We must nuke the old binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001113 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, UnknownVal()), K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001114 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001115 else {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001116 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
Ted Kremenek1369d902009-07-11 04:38:49 +00001117
1118 // Casts can create weird scenarios where a location must be implicitly
1119 // converted to something else. For example:
1120 //
1121 // void *x;
1122 // int *y = (int*) &x; // void** -> int* cast.
1123 // invalidate(y); // 'x' now binds to a symbolic region
1124 // int z = *y;
1125 //
Zhongxing Xu92be7ce2009-07-14 01:12:46 +00001126 //if (isa<Loc>(V) && !Loc::IsLocType(Ex->getType())) {
1127 // V = EvalCast(V, Ex->getType());
1128 //}
Ted Kremenek1369d902009-07-11 04:38:49 +00001129
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001130 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001131 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001132}
1133
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001134void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001135 const GRState* state, SVal location, SVal Val,
1136 const void *tag) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001137
1138 NodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001139 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001140
1141 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001142 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001143}
1144
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001145GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001146 const GRState* state,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001147 SVal location,
1148 const void *tag) {
1149
1150 SaveAndRestore<const void*> OldTag(Builder->Tag);
1151 Builder->Tag = tag;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001152
1153 // Check for loads/stores from/to undefined values.
1154 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001155 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001156 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001157 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001158
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001159 if (N) {
1160 N->markAsSink();
1161 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001162 }
1163
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001164 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001165 }
1166
1167 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1168 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001169 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001170
1171 // During a load, one of two possible situations arise:
1172 // (1) A crash, because the location (pointer) was NULL.
1173 // (2) The location (pointer) is not NULL, and the dereference works.
1174 //
1175 // We add these assumptions.
1176
Zhongxing Xu097fc982008-10-17 05:57:07 +00001177 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001178
1179 // "Assume" that the pointer is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001180 const GRState *StNotNull = state->assume(LV, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001181
1182 // "Assume" that the pointer is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001183 const GRState *StNull = state->assume(LV, false);
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001184
Ted Kremenek70970bf2009-06-18 22:57:13 +00001185 if (StNull) {
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001186 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001187 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001188 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001189
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001190 // We don't use "MakeNode" here because the node will be a sink
1191 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001192 NodeTy* NullNode =
1193 Builder->generateNode(Ex, StNull, Pred,
1194 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001195
Ted Kremenek70970bf2009-06-18 22:57:13 +00001196 if (NullNode) {
1197 NullNode->markAsSink();
1198 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001199 else ExplicitNullDeref.insert(NullNode);
1200 }
1201 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001202
Ted Kremenek70970bf2009-06-18 22:57:13 +00001203 if (!StNotNull)
1204 return NULL;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001205
1206 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001207 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001208 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1209 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1210 // Get the index of the accessed element.
1211 SVal Idx = ER->getIndex();
1212 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001213 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1214 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001215
Ted Kremenek70970bf2009-06-18 22:57:13 +00001216 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
1217 true);
1218 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
1219 false);
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001220
Ted Kremenek70970bf2009-06-18 22:57:13 +00001221 if (StOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001222 // Report warning. Make sink node manually.
1223 NodeTy* OOBNode =
1224 Builder->generateNode(Ex, StOutBound, Pred,
1225 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001226
1227 if (OOBNode) {
1228 OOBNode->markAsSink();
1229
Ted Kremenek70970bf2009-06-18 22:57:13 +00001230 if (StInBound)
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001231 ImplicitOOBMemAccesses.insert(OOBNode);
1232 else
1233 ExplicitOOBMemAccesses.insert(OOBNode);
1234 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001235 }
1236
Ted Kremenek70970bf2009-06-18 22:57:13 +00001237 if (!StInBound)
1238 return NULL;
1239
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001240 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001241 }
1242 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001243
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001244 // Generate a new node indicating the checks succeed.
1245 return Builder->generateNode(Ex, StNotNull, Pred,
1246 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001247}
1248
Ted Kremenekca5f6202008-04-15 23:06:53 +00001249//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001250// Transfer function: OSAtomics.
1251//
1252// FIXME: Eventually refactor into a more "plugin" infrastructure.
1253//===----------------------------------------------------------------------===//
1254
1255// Mac OS X:
1256// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1257// atomic.3.html
1258//
1259static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1260 GRExprEngine& Engine,
1261 GRStmtNodeBuilder<GRState>& Builder,
1262 CallExpr* CE, SVal L,
1263 ExplodedNode<GRState>* Pred) {
1264
1265 // Not enough arguments to match OSAtomicCompareAndSwap?
1266 if (CE->getNumArgs() != 3)
1267 return false;
1268
1269 ASTContext &C = Engine.getContext();
1270 Expr *oldValueExpr = CE->getArg(0);
1271 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1272
1273 Expr *newValueExpr = CE->getArg(1);
1274 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1275
1276 // Do the types of 'oldValue' and 'newValue' match?
1277 if (oldValueType != newValueType)
1278 return false;
1279
1280 Expr *theValueExpr = CE->getArg(2);
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00001281 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001282
1283 // theValueType not a pointer?
1284 if (!theValueType)
1285 return false;
1286
1287 QualType theValueTypePointee =
1288 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1289
1290 // The pointee must match newValueType and oldValueType.
1291 if (theValueTypePointee != newValueType)
1292 return false;
1293
1294 static unsigned magic_load = 0;
1295 static unsigned magic_store = 0;
1296
1297 const void *OSAtomicLoadTag = &magic_load;
1298 const void *OSAtomicStoreTag = &magic_store;
1299
1300 // Load 'theValue'.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001301 const GRState *state = Pred->getState();
1302 ExplodedNodeSet<GRState> Tmp;
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001303 SVal location = state->getSVal(theValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001304 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1305
1306 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1307 I != E; ++I) {
1308
1309 ExplodedNode<GRState> *N = *I;
1310 const GRState *stateLoad = N->getState();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001311 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1312 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001313
1314 // Perform the comparison.
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00001315 SVal Cmp = Engine.EvalBinOp(stateLoad, BinaryOperator::EQ, theValueVal,
1316 oldValueVal, Engine.getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +00001317
1318 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001319
1320 // Were they equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001321 if (stateEqual) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001322 // Perform the store.
1323 ExplodedNodeSet<GRState> TmpStore;
1324 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001325 stateEqual->getSVal(newValueExpr), OSAtomicStoreTag);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001326
1327 // Now bind the result of the comparison.
1328 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1329 E2 = TmpStore.end(); I2 != E2; ++I2) {
1330 ExplodedNode<GRState> *predNew = *I2;
1331 const GRState *stateNew = predNew->getState();
1332 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001333 Engine.MakeNode(Dst, CE, predNew, stateNew->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001334 }
1335 }
1336
1337 // Were they not equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001338 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001339 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001340 Engine.MakeNode(Dst, CE, N, stateNotEqual->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001341 }
1342 }
1343
1344 return true;
1345}
1346
1347static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1348 GRExprEngine& Engine,
1349 GRStmtNodeBuilder<GRState>& Builder,
1350 CallExpr* CE, SVal L,
1351 ExplodedNode<GRState>* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001352 const FunctionDecl* FD = L.getAsFunctionDecl();
1353 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001354 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001355
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001356 const char *FName = FD->getNameAsCString();
1357
1358 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001359 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1360 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001361 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001362
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001363 // FIXME: Other atomics.
1364 return false;
1365}
1366
1367//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001368// Transfer function: Function calls.
1369//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001370
1371void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1372 assert (Builder && "GRStmtNodeBuilder must be defined.");
1373
1374 // FIXME: Allow us to chain together transfer functions.
1375 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1376 return;
1377
1378 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1379}
1380
Ted Kremenekd9268e32008-02-19 01:44:53 +00001381void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001382 CallExpr::arg_iterator AI,
1383 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001384 NodeSet& Dst)
1385{
1386 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001387 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001388 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00001389 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001390 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001391
1392 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1393}
1394
1395void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1396 CallExpr::arg_iterator AI,
1397 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001398 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001399 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001400
Ted Kremenek07baa252008-02-21 18:02:17 +00001401 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001402 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001403 // If the call argument is being bound to a reference parameter,
1404 // visit it as an lvalue, not an rvalue.
1405 bool VisitAsLvalue = false;
1406 if (Proto && ParamIdx < Proto->getNumArgs())
1407 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1408
1409 NodeSet DstTmp;
1410 if (VisitAsLvalue)
1411 VisitLValue(*AI, Pred, DstTmp);
1412 else
1413 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001414 ++AI;
1415
Ted Kremenek769f3482008-03-04 22:01:56 +00001416 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001417 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001418
1419 return;
1420 }
1421
1422 // If we reach here we have processed all of the arguments. Evaluate
1423 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001424
Ted Kremenekc71901d2008-02-25 21:16:03 +00001425 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001426 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001427
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001428 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001429
Ted Kremenekd9268e32008-02-19 01:44:53 +00001430 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001431 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1432
Ted Kremeneke66ba682009-02-13 01:45:31 +00001433 const GRState* state = GetState(*DI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001434 SVal L = state->getSVal(Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001435
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001436 // FIXME: Add support for symbolic function calls (calls involving
1437 // function pointer values that are symbolic).
1438
1439 // Check for undefined control-flow or calls to NULL.
1440
Zhongxing Xu097fc982008-10-17 05:57:07 +00001441 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001442 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001443
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001444 if (N) {
1445 N->markAsSink();
1446 BadCalls.insert(N);
1447 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001448
Ted Kremenekd9268e32008-02-19 01:44:53 +00001449 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001450 }
1451
1452 // Check for the "noreturn" attribute.
1453
1454 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001455 const FunctionDecl* FD = L.getAsFunctionDecl();
1456 if (FD) {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +00001457 if (FD->getAttr<NoReturnAttr>() ||
1458 FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +00001459 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001460 else {
1461 // HACK: Some functions are not marked noreturn, and don't return.
1462 // Here are a few hardwired ones. If this takes too long, we can
1463 // potentially cache these results.
1464 const char* s = FD->getIdentifier()->getName();
1465 unsigned n = strlen(s);
1466
1467 switch (n) {
1468 default:
1469 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001470
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001471 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001472 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1473 break;
1474
1475 case 5:
1476 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001477 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001478 if (CE->getNumArgs() > 0) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001479 SVal X = state->getSVal(*CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001480 // FIXME: use Assume to inspect the possible symbolic value of
1481 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001482 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001483 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001484 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001485 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001486 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001487 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001488
Ted Kremenek23271be2008-04-22 05:37:33 +00001489 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001490 if (!memcmp(s, "Assert", 6)) {
1491 Builder->BuildSinks = true;
1492 break;
1493 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001494
1495 // FIXME: This is just a wrapper around throwing an exception.
1496 // Eventually inter-procedural analysis should handle this easily.
1497 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1498
Ted Kremenek23271be2008-04-22 05:37:33 +00001499 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001500
1501 case 7:
1502 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1503 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001504
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001505 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001506 if (!memcmp(s ,"db_error", 8) ||
1507 !memcmp(s, "__assert", 8))
1508 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001509 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001510
1511 case 12:
1512 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1513 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001514
Ted Kremenek19903a22008-09-19 02:30:47 +00001515 case 13:
1516 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1517 break;
1518
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001519 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001520 if (!memcmp(s, "dtrace_assfail", 14) ||
1521 !memcmp(s, "yy_fatal_error", 14))
1522 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001523 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001524
1525 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001526 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001527 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1528 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001529 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001530
Ted Kremeneka46fea72008-05-17 00:33:23 +00001531 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001532 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001533
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001534 }
1535 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001536
1537 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001538
Zhongxing Xucac107a2009-04-20 05:24:46 +00001539 if (FD) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001540
Zhongxing Xucac107a2009-04-20 05:24:46 +00001541 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001542 switch (id) {
1543 case Builtin::BI__builtin_expect: {
1544 // For __builtin_expect, just return the value of the subexpression.
1545 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001546 SVal X = state->getSVal(*(CE->arg_begin()));
1547 MakeNode(Dst, CE, *DI, state->bindExpr(CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001548 continue;
1549 }
1550
Ted Kremenek19891fa2008-11-02 00:35:01 +00001551 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001552 // FIXME: Refactor into StoreManager itself?
1553 MemRegionManager& RM = getStateManager().getRegionManager();
1554 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001555 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001556
1557 // Set the extent of the region in bytes. This enables us to use the
1558 // SVal of the argument directly. If we save the extent in bits, we
1559 // cannot represent values like symbol*8.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001560 SVal Extent = state->getSVal(*(CE->arg_begin()));
Ted Kremeneke66ba682009-02-13 01:45:31 +00001561 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001562
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001563 MakeNode(Dst, CE, *DI, state->bindExpr(CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001564 continue;
1565 }
1566
Ted Kremenek21581c62008-03-05 22:59:42 +00001567 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001568 break;
1569 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001570 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001571
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001572 // Check any arguments passed-by-value against being undefined.
1573
1574 bool badArg = false;
1575
1576 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1577 I != E; ++I) {
1578
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001579 if (GetState(*DI)->getSVal(*I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001580 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001581
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001582 if (N) {
1583 N->markAsSink();
1584 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001585 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001586
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001587 badArg = true;
1588 break;
1589 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001590 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001591
1592 if (badArg)
1593 continue;
1594
1595 // Dispatch to the plug-in transfer function.
1596
1597 unsigned size = Dst.size();
1598 SaveOr OldHasGen(Builder->HasGeneratedNode);
1599 EvalCall(Dst, CE, L, *DI);
1600
1601 // Handle the case where no nodes where generated. Auto-generate that
1602 // contains the updated state if we aren't generating sinks.
1603
1604 if (!Builder->BuildSinks && Dst.size() == size &&
1605 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001606 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001607 }
1608}
1609
Ted Kremenekca5f6202008-04-15 23:06:53 +00001610//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001611// Transfer function: Objective-C ivar references.
1612//===----------------------------------------------------------------------===//
1613
Ted Kremenek9a48d862009-02-28 20:50:43 +00001614static std::pair<const void*,const void*> EagerlyAssumeTag
1615 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1616
Ted Kremenek34a611b2009-02-25 23:32:10 +00001617void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001618 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1619 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001620
1621 // Test if the previous node was as the same expression. This can happen
1622 // when the expression fails to evaluate to anything meaningful and
1623 // (as an optimization) we don't generate a node.
1624 ProgramPoint P = Pred->getLocation();
1625 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1626 Dst.Add(Pred);
1627 continue;
1628 }
1629
Ted Kremenek8f520972009-02-25 22:32:02 +00001630 const GRState* state = Pred->getState();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001631 SVal V = state->getSVal(Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001632 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001633 // First assume that the condition is true.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001634 if (const GRState *stateTrue = state->assume(V, true)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00001635 stateTrue = stateTrue->bindExpr(Ex,
1636 ValMgr.makeIntVal(1U, Ex->getType()));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001637 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001638 stateTrue, Pred));
1639 }
1640
1641 // Next, assume that the condition is false.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001642 if (const GRState *stateFalse = state->assume(V, false)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00001643 stateFalse = stateFalse->bindExpr(Ex,
1644 ValMgr.makeIntVal(0U, Ex->getType()));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001645 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001646 stateFalse, Pred));
1647 }
1648 }
1649 else
1650 Dst.Add(Pred);
1651 }
1652}
1653
1654//===----------------------------------------------------------------------===//
1655// Transfer function: Objective-C ivar references.
1656//===----------------------------------------------------------------------===//
1657
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001658void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1659 NodeTy* Pred, NodeSet& Dst,
1660 bool asLValue) {
1661
1662 Expr* Base = cast<Expr>(Ex->getBase());
1663 NodeSet Tmp;
1664 Visit(Base, Pred, Tmp);
1665
1666 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001667 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001668 SVal BaseVal = state->getSVal(Base);
1669 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001670
1671 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001672 MakeNode(Dst, Ex, *I, state->bindExpr(Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001673 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001674 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001675 }
1676}
1677
1678//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001679// Transfer function: Objective-C fast enumeration 'for' statements.
1680//===----------------------------------------------------------------------===//
1681
1682void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1683 NodeTy* Pred, NodeSet& Dst) {
1684
1685 // ObjCForCollectionStmts are processed in two places. This method
1686 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1687 // statements within a basic block. This transfer function does two things:
1688 //
1689 // (1) binds the next container value to 'element'. This creates a new
1690 // node in the ExplodedGraph.
1691 //
1692 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1693 // whether or not the container has any more elements. This value
1694 // will be tested in ProcessBranch. We need to explicitly bind
1695 // this value because a container can contain nil elements.
1696 //
1697 // FIXME: Eventually this logic should actually do dispatches to
1698 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1699 // This will require simulating a temporary NSFastEnumerationState, either
1700 // through an SVal or through the use of MemRegions. This value can
1701 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1702 // terminates we reclaim the temporary (it goes out of scope) and we
1703 // we can test if the SVal is 0 or if the MemRegion is null (depending
1704 // on what approach we take).
1705 //
1706 // For now: simulate (1) by assigning either a symbol or nil if the
1707 // container is empty. Thus this transfer function will by default
1708 // result in state splitting.
1709
Ted Kremenek034a9472008-11-14 19:47:18 +00001710 Stmt* elem = S->getElement();
1711 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001712
1713 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001714 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001715 assert (ElemD->getInit() == 0);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001716 ElementV = GetState(Pred)->getLValue(ElemD);
Ted Kremenek034a9472008-11-14 19:47:18 +00001717 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1718 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001719 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001720
1721 NodeSet Tmp;
1722 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001723
Ted Kremenek034a9472008-11-14 19:47:18 +00001724 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1725 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001726 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek034a9472008-11-14 19:47:18 +00001727 }
1728}
1729
1730void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1731 NodeTy* Pred, NodeSet& Dst,
1732 SVal ElementV) {
1733
1734
Ted Kremenek13e167f2008-11-12 19:24:17 +00001735
Ted Kremenek034a9472008-11-14 19:47:18 +00001736 // Get the current state. Use 'EvalLocation' to determine if it is a null
1737 // pointer, etc.
1738 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001739
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001740 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1741 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001742 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001743
Ted Kremenek18a636d2009-06-18 01:23:53 +00001744 const GRState *state = GetState(Pred);
Ted Kremenek034a9472008-11-14 19:47:18 +00001745
Ted Kremenek13e167f2008-11-12 19:24:17 +00001746 // Handle the case where the container still has elements.
Zhongxing Xue32c7652009-06-23 09:02:15 +00001747 SVal TrueV = ValMgr.makeTruthVal(1);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001748 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001749
Ted Kremenek13e167f2008-11-12 19:24:17 +00001750 // Handle the case where the container has no elements.
Zhongxing Xue32c7652009-06-23 09:02:15 +00001751 SVal FalseV = ValMgr.makeTruthVal(0);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001752 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001753
1754 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1755 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1756 // FIXME: The proper thing to do is to really iterate over the
1757 // container. We will do this with dispatch logic to the store.
1758 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu20362702009-05-09 03:57:34 +00001759 QualType T = R->getValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001760 assert (Loc::IsLocType(T));
1761 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001762 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
Zhongxing Xue32c7652009-06-23 09:02:15 +00001763 SVal V = ValMgr.makeLoc(Sym);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001764 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001765
Ted Kremenek034a9472008-11-14 19:47:18 +00001766 // Bind the location to 'nil' on the false branch.
Zhongxing Xue32c7652009-06-23 09:02:15 +00001767 SVal nilV = ValMgr.makeIntVal(0, T);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001768 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001769 }
1770
Ted Kremenekd3789d72008-11-12 21:12:46 +00001771 // Create the new nodes.
1772 MakeNode(Dst, S, Pred, hasElems);
1773 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001774}
1775
1776//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001777// Transfer function: Objective-C message expressions.
1778//===----------------------------------------------------------------------===//
1779
1780void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1781 NodeSet& Dst){
1782
1783 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1784 Pred, Dst);
1785}
1786
1787void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001788 ObjCMessageExpr::arg_iterator AI,
1789 ObjCMessageExpr::arg_iterator AE,
1790 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001791 if (AI == AE) {
1792
1793 // Process the receiver.
1794
1795 if (Expr* Receiver = ME->getReceiver()) {
1796 NodeSet Tmp;
1797 Visit(Receiver, Pred, Tmp);
1798
1799 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1800 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1801
1802 return;
1803 }
1804
1805 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1806 return;
1807 }
1808
1809 NodeSet Tmp;
1810 Visit(*AI, Pred, Tmp);
1811
1812 ++AI;
1813
1814 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1815 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1816}
1817
1818void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1819 NodeTy* Pred,
1820 NodeSet& Dst) {
1821
1822 // FIXME: More logic for the processing the method call.
1823
Ted Kremeneke66ba682009-02-13 01:45:31 +00001824 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001825 bool RaisesException = false;
1826
Ted Kremenekca5f6202008-04-15 23:06:53 +00001827
1828 if (Expr* Receiver = ME->getReceiver()) {
1829
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001830 SVal L = state->getSVal(Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001831
Ted Kremenek95a98252009-02-19 04:06:22 +00001832 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001833 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001834 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001835
1836 if (N) {
1837 N->markAsSink();
1838 UndefReceivers.insert(N);
1839 }
1840
1841 return;
1842 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001843
Ted Kremenek95a98252009-02-19 04:06:22 +00001844 // "Assume" that the receiver is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001845 const GRState *StNotNull = state->assume(L, true);
Ted Kremenek95a98252009-02-19 04:06:22 +00001846
1847 // "Assume" that the receiver is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001848 const GRState *StNull = state->assume(L, false);
Ted Kremenek95a98252009-02-19 04:06:22 +00001849
Ted Kremenek70970bf2009-06-18 22:57:13 +00001850 if (StNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001851 QualType RetTy = ME->getType();
1852
Ted Kremenek95a98252009-02-19 04:06:22 +00001853 // Check if the receiver was nil and the return value a struct.
Ted Kremenekb3323002009-04-09 05:45:56 +00001854 if(RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001855 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001856 // The [0 ...] expressions will return garbage. Flag either an
1857 // explicit or implicit error. Because of the structure of this
1858 // function we currently do not bifurfacte the state graph at
1859 // this point.
1860 // FIXME: We should bifurcate and fill the returned struct with
1861 // garbage.
1862 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1863 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001864 if (StNotNull)
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001865 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001866 else
Ted Kremenek23712182009-04-09 04:06:51 +00001867 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001868 }
1869 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001870 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001871 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001872 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001873 if (RetTy != Ctx.VoidTy) {
1874 if (BR.getParentMap().isConsumedExpr(ME)) {
1875 // sizeof(void *)
1876 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1877 // sizeof(return type)
1878 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001879
Ted Kremenekb3323002009-04-09 05:45:56 +00001880 if(voidPtrSize < returnTypeSize) {
1881 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1882 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001883 if(StNotNull)
Ted Kremenekb3323002009-04-09 05:45:56 +00001884 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001885 else
Ted Kremenekb3323002009-04-09 05:45:56 +00001886 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001887 }
1888 }
Ted Kremenek70970bf2009-06-18 22:57:13 +00001889 else if (!StNotNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001890 // Handle the safe cases where the return value is 0 if the
1891 // receiver is nil.
1892 //
1893 // FIXME: For now take the conservative approach that we only
1894 // return null values if we *know* that the receiver is nil.
1895 // This is because we can have surprises like:
1896 //
1897 // ... = [[NSScreens screens] objectAtIndex:0];
1898 //
1899 // What can happen is that [... screens] could return nil, but
1900 // it most likely isn't nil. We should assume the semantics
1901 // of this case unless we have *a lot* more knowledge.
1902 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001903 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001904 MakeNode(Dst, ME, Pred, StNull->bindExpr(ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001905 return;
1906 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001907 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001908 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001909 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001910 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001911 // of this method should assume that the receiver is not nil.
1912 if (!StNotNull)
1913 return;
1914
Ted Kremenekf2895872009-04-08 18:51:08 +00001915 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001916 }
1917
Ted Kremenek5f20a632008-05-01 18:33:28 +00001918 // Check if the "raise" message was sent.
1919 if (ME->getSelector() == RaiseSel)
1920 RaisesException = true;
1921 }
1922 else {
1923
1924 IdentifierInfo* ClsName = ME->getClassName();
1925 Selector S = ME->getSelector();
1926
1927 // Check for special instance methods.
1928
1929 if (!NSExceptionII) {
1930 ASTContext& Ctx = getContext();
1931
1932 NSExceptionII = &Ctx.Idents.get("NSException");
1933 }
1934
1935 if (ClsName == NSExceptionII) {
1936
1937 enum { NUM_RAISE_SELECTORS = 2 };
1938
1939 // Lazily create a cache of the selectors.
1940
1941 if (!NSExceptionInstanceRaiseSelectors) {
1942
1943 ASTContext& Ctx = getContext();
1944
1945 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1946
1947 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1948 unsigned idx = 0;
1949
1950 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001951 II.push_back(&Ctx.Idents.get("raise"));
1952 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001953 NSExceptionInstanceRaiseSelectors[idx++] =
1954 Ctx.Selectors.getSelector(II.size(), &II[0]);
1955
1956 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001957 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001958 NSExceptionInstanceRaiseSelectors[idx++] =
1959 Ctx.Selectors.getSelector(II.size(), &II[0]);
1960 }
1961
1962 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1963 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1964 RaisesException = true; break;
1965 }
1966 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001967 }
1968
1969 // Check for any arguments that are uninitialized/undefined.
1970
1971 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1972 I != E; ++I) {
1973
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001974 if (state->getSVal(*I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001975
1976 // Generate an error node for passing an uninitialized/undefined value
1977 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001978 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001979
1980 if (N) {
1981 N->markAsSink();
1982 MsgExprUndefArgs[N] = *I;
1983 }
1984
1985 return;
1986 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001987 }
1988
1989 // Check if we raise an exception. For now treat these as sinks. Eventually
1990 // we will want to handle exceptions properly.
1991
1992 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1993
1994 if (RaisesException)
1995 Builder->BuildSinks = true;
1996
Ted Kremenekca5f6202008-04-15 23:06:53 +00001997 // Dispatch to plug-in transfer function.
1998
1999 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002000 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002001
Ted Kremenekca5f6202008-04-15 23:06:53 +00002002 EvalObjCMessageExpr(Dst, ME, Pred);
2003
2004 // Handle the case where no nodes where generated. Auto-generate that
2005 // contains the updated state if we aren't generating sinks.
2006
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002007 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002008 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00002009}
2010
2011//===----------------------------------------------------------------------===//
2012// Transfer functions: Miscellaneous statements.
2013//===----------------------------------------------------------------------===//
2014
Ted Kremenek16354a42009-01-13 01:04:21 +00002015void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2016 QualType PtrTy,
2017 Expr* CastE, NodeTy* Pred,
2018 NodeSet& Dst) {
2019 if (!V.isUnknownOrUndef()) {
2020 // FIXME: Determine if the number of bits of the target type is
2021 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00002022 // If not, flag an error.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002023 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, EvalCast(cast<Loc>(V),
Ted Kremenek52978eb2009-03-05 03:44:53 +00002024 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00002025 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002026 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002027 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002028}
2029
2030
Ted Kremenek07baa252008-02-21 18:02:17 +00002031void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00002032 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002033 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002034 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002035
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002036 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002037 T = ExCast->getTypeAsWritten();
2038
Zhongxing Xu943909c2008-10-22 08:02:16 +00002039 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002040 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002041 else
2042 Visit(Ex, Pred, S1);
2043
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002044 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00002045 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002046 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002047 Dst.Add(*I1);
2048
Ted Kremenek54eddae2008-01-24 02:02:54 +00002049 return;
2050 }
2051
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002052 // FIXME: The rest of this should probably just go into EvalCall, and
2053 // let the transfer function object be responsible for constructing
2054 // nodes.
2055
Ted Kremenek07baa252008-02-21 18:02:17 +00002056 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00002057 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002058 const GRState* state = GetState(N);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002059 SVal V = state->getSVal(Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002060 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002061
2062 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002063 if (V.isUnknown()) {
2064 Dst.Add(N);
2065 continue;
2066 }
2067
2068 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002069 if (V.isUndef())
2070 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00002071
2072 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00002073 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002074 C.getCanonicalType(ExTy).getUnqualifiedType())
2075 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002076
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002077 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002078 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002079 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002080 continue;
2081 }
2082
2083 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002084 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002085 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002086 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002087 V = LV->getLoc();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002088 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002089 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002090 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002091
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002092 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002093 }
2094
2095 // Just pass through function and block pointers.
2096 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2097 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002098 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002099 }
2100
Ted Kremenek16354a42009-01-13 01:04:21 +00002101 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002102 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00002103 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00002104 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002105
2106 // Are we casting from an array to a pointer? If so just pass on
2107 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002108 if (T->isPointerType())
2109 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00002110
2111 // Are we casting from an array to an integer? If so, cast the decayed
2112 // pointer value to an integer.
2113 assert(T->isIntegerType());
2114 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2115 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002116 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002117 continue;
2118 }
2119
Ted Kremenekf5da3252008-12-13 21:49:13 +00002120 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00002121 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2122 // FIXME: For TypedViewRegions, we should handle the case where the
2123 // underlying symbolic pointer is a function pointer or
2124 // block pointer.
2125
2126 // FIXME: We should handle the case where we strip off view layers to get
2127 // to a desugared type.
2128
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002129 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00002130 // We get a symbolic function pointer for a dereference of a function
2131 // pointer, but it is of function type. Example:
2132
2133 // struct FPRec {
2134 // void (*my_func)(int * x);
2135 // };
2136 //
2137 // int bar(int x);
2138 //
2139 // int f1_a(struct FPRec* foo) {
2140 // int x;
2141 // (*foo->my_func)(&x);
2142 // return bar(x)+1; // no-warning
2143 // }
2144
2145 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002146
Ted Kremenekf5da3252008-12-13 21:49:13 +00002147 const MemRegion* R = RV->getRegion();
2148 StoreManager& StoreMgr = getStoreManager();
2149
2150 // Delegate to store manager to get the result of casting a region
2151 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002152 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00002153
2154 // Inspect the result. If the MemRegion* returned is NULL, this
2155 // expression evaluates to UnknownVal.
2156 R = Res.getRegion();
2157 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2158
2159 // Generate the new node in the ExplodedGraph.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002160 MakeNode(Dst, CastE, N, Res.getState()->bindExpr(CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00002161 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002162 }
Zhongxing Xu18bcec02009-04-10 06:06:13 +00002163 // All other cases.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002164 DispatchCast: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002165 MakeNode(Dst, CastE, N, state->bindExpr(CastE,
2166 EvalCast(V, CastE->getType())));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002167 continue;
2168 }
2169
2170 PassThrough: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002171 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002172 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002173 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002174}
2175
Ted Kremenekd83daa52008-10-27 21:54:31 +00002176void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002177 NodeTy* Pred, NodeSet& Dst,
2178 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002179 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2180 NodeSet Tmp;
2181 Visit(ILE, Pred, Tmp);
2182
2183 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002184 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002185 SVal ILV = state->getSVal(ILE);
2186 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002187
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002188 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002189 MakeNode(Dst, CL, *I, state->bindExpr(CL, state->getLValue(CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002190 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002191 MakeNode(Dst, CL, *I, state->bindExpr(CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002192 }
2193}
2194
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002195void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002196
Ted Kremenek811af062008-10-06 18:43:53 +00002197 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002198 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002199
2200 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002201 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002202
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002203 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002204 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002205
2206 // FIXME: static variables may have an initializer, but the second
2207 // time a function is called those values may not be current.
2208 NodeSet Tmp;
2209
Ted Kremenek13e167f2008-11-12 19:24:17 +00002210 if (InitEx)
2211 Visit(InitEx, Pred, Tmp);
Ted Kremenek9c677732009-07-17 23:48:26 +00002212 else
Ted Kremenek448ab622008-08-28 18:34:26 +00002213 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002214
2215 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002216 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002217 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002218
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002219 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2220 QualType T = getContext().getCanonicalType(VD->getType());
2221 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2222 // FIXME: Handle multi-dimensional VLAs.
2223
2224 Expr* SE = VLA->getSizeExpr();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002225 SVal Size = state->getSVal(SE);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002226
2227 if (Size.isUndef()) {
2228 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2229 N->markAsSink();
2230 ExplicitBadSizedVLA.insert(N);
2231 }
2232 continue;
2233 }
2234
Ted Kremenek70970bf2009-06-18 22:57:13 +00002235 const GRState* zeroState = state->assume(Size, false);
2236 state = state->assume(Size, true);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002237
Ted Kremenek70970bf2009-06-18 22:57:13 +00002238 if (zeroState) {
2239 if (NodeTy* N = Builder->generateNode(DS, zeroState, Pred)) {
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002240 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00002241 if (state)
2242 ImplicitBadSizedVLA.insert(N);
2243 else
2244 ExplicitBadSizedVLA.insert(N);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002245 }
2246 }
2247
Ted Kremenek70970bf2009-06-18 22:57:13 +00002248 if (!state)
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002249 continue;
2250 }
2251
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002252 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002253 if (InitEx) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002254 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002255 QualType T = VD->getType();
2256
2257 // Recover some path-sensitivity if a scalar value evaluated to
2258 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002259 if (InitVal.isUnknown() ||
2260 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002261 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002262 }
2263
Ted Kremenek1a01dca2009-06-23 20:27:53 +00002264 state = state->bindDecl(VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002265
2266 // The next thing to do is check if the GRTransferFuncs object wants to
2267 // update the state based on the new binding. If the GRTransferFunc
2268 // object doesn't do anything, just auto-propagate the current state.
2269 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
Ted Kremenekf6f0a872009-06-23 21:37:46 +00002270 getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD)),
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002271 InitVal);
2272 }
2273 else {
Ted Kremenek1a01dca2009-06-23 20:27:53 +00002274 state = state->bindDeclWithNoInit(VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002275 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002276 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002277 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002278}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002279
Ted Kremeneke56ece22008-10-30 17:47:32 +00002280namespace {
2281 // This class is used by VisitInitListExpr as an item in a worklist
2282 // for processing the values contained in an InitListExpr.
2283class VISIBILITY_HIDDEN InitListWLItem {
2284public:
2285 llvm::ImmutableList<SVal> Vals;
2286 GRExprEngine::NodeTy* N;
2287 InitListExpr::reverse_iterator Itr;
2288
2289 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2290 InitListExpr::reverse_iterator itr)
2291 : Vals(vals), N(n), Itr(itr) {}
2292};
2293}
2294
2295
Zhongxing Xuebcad732008-10-30 05:02:23 +00002296void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2297 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002298
Zhongxing Xuebcad732008-10-30 05:02:23 +00002299 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002300 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002301 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002302
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002303 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002304
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002305 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002306
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002307 // Handle base case where the initializer has no elements.
2308 // e.g: static int* myArray[] = {};
2309 if (NumInitElements == 0) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00002310 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002311 MakeNode(Dst, E, Pred, state->bindExpr(E, V));
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002312 return;
2313 }
2314
2315 // Create a worklist to process the initializers.
2316 llvm::SmallVector<InitListWLItem, 10> WorkList;
2317 WorkList.reserve(NumInitElements);
2318 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002319 InitListExpr::reverse_iterator ItrEnd = E->rend();
2320
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002321 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002322 while (!WorkList.empty()) {
2323 InitListWLItem X = WorkList.back();
2324 WorkList.pop_back();
2325
Zhongxing Xuebcad732008-10-30 05:02:23 +00002326 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002327 Visit(*X.Itr, X.N, Tmp);
2328
2329 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002330
Ted Kremeneke56ece22008-10-30 17:47:32 +00002331 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2332 // Get the last initializer value.
2333 state = GetState(*NI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002334 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002335
2336 // Construct the new list of values by prepending the new value to
2337 // the already constructed list.
2338 llvm::ImmutableList<SVal> NewVals =
2339 getBasicVals().consVals(InitV, X.Vals);
2340
2341 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002342 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xue32c7652009-06-23 09:02:15 +00002343 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xuebcad732008-10-30 05:02:23 +00002344
Ted Kremeneke56ece22008-10-30 17:47:32 +00002345 // Make final state and node.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002346 MakeNode(Dst, E, *NI, state->bindExpr(E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002347 }
2348 else {
2349 // Still some initializer values to go. Push them onto the worklist.
2350 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2351 }
2352 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002353 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002354
2355 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002356 }
2357
Ted Kremenek79413a52008-11-13 06:10:40 +00002358 if (T->isUnionType() || T->isVectorType()) {
2359 // FIXME: to be implemented.
2360 // Note: That vectors can return true for T->isIntegerType()
2361 MakeNode(Dst, E, Pred, state);
2362 return;
2363 }
2364
Zhongxing Xuebcad732008-10-30 05:02:23 +00002365 if (Loc::IsLocType(T) || T->isIntegerType()) {
2366 assert (E->getNumInits() == 1);
2367 NodeSet Tmp;
2368 Expr* Init = E->getInit(0);
2369 Visit(Init, Pred, Tmp);
2370 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2371 state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002372 MakeNode(Dst, E, *I, state->bindExpr(E, state->getSVal(Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002373 }
2374 return;
2375 }
2376
Zhongxing Xuebcad732008-10-30 05:02:23 +00002377
2378 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2379 assert(0 && "unprocessed InitListExpr type");
2380}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002381
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002382/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2383void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2384 NodeTy* Pred,
2385 NodeSet& Dst) {
2386 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002387 uint64_t amt;
2388
2389 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002390 if (T == getContext().VoidTy) {
2391 // sizeof(void) == 1 byte.
2392 amt = 1;
2393 }
2394 else if (!T.getTypePtr()->isConstantSizeType()) {
2395 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002396 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002397 }
2398 else if (T->isObjCInterfaceType()) {
2399 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2400 // the compiler has laid out its representation. Just report Unknown
2401 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002402 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002403 }
2404 else {
2405 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002406 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002407 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002408 }
2409 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002410 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002411
Ted Kremenekf10f2882008-03-21 21:30:14 +00002412 MakeNode(Dst, Ex, Pred,
Zhongxing Xue32c7652009-06-23 09:02:15 +00002413 GetState(Pred)->bindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002414}
2415
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002416
Ted Kremenek07baa252008-02-21 18:02:17 +00002417void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002418 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002419
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002420 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002421
2422 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002423 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002424
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002425 case UnaryOperator::Deref: {
2426
2427 Expr* Ex = U->getSubExpr()->IgnoreParens();
2428 NodeSet Tmp;
2429 Visit(Ex, Pred, Tmp);
2430
2431 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002432
Ted Kremeneke66ba682009-02-13 01:45:31 +00002433 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002434 SVal location = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002435
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002436 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002437 MakeNode(Dst, U, *I, state->bindExpr(U, location),
Ted Kremenek0441f112009-05-07 18:27:16 +00002438 ProgramPoint::PostLValueKind);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002439 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002440 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002441 }
2442
2443 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002444 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002445
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002446 case UnaryOperator::Real: {
2447
2448 Expr* Ex = U->getSubExpr()->IgnoreParens();
2449 NodeSet Tmp;
2450 Visit(Ex, Pred, Tmp);
2451
2452 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2453
Zhongxing Xu097fc982008-10-17 05:57:07 +00002454 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002455 if (Ex->getType()->isAnyComplexType()) {
2456 // Just report "Unknown."
2457 Dst.Add(*I);
2458 continue;
2459 }
2460
2461 // For all other types, UnaryOperator::Real is an identity operation.
2462 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002463 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002464 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002465 }
2466
2467 return;
2468 }
2469
2470 case UnaryOperator::Imag: {
2471
2472 Expr* Ex = U->getSubExpr()->IgnoreParens();
2473 NodeSet Tmp;
2474 Visit(Ex, Pred, Tmp);
2475
2476 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002477 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002478 if (Ex->getType()->isAnyComplexType()) {
2479 // Just report "Unknown."
2480 Dst.Add(*I);
2481 continue;
2482 }
2483
2484 // For all other types, UnaryOperator::Float returns 0.
2485 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002486 const GRState* state = GetState(*I);
Zhongxing Xue32c7652009-06-23 09:02:15 +00002487 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002488 MakeNode(Dst, U, *I, state->bindExpr(U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002489 }
2490
2491 return;
2492 }
2493
2494 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002495 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002496 Dst.Add(Pred);
2497 return;
2498
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002499 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002500 case UnaryOperator::Extension: {
2501
2502 // Unary "+" is a no-op, similar to a parentheses. We still have places
2503 // where it may be a block-level expression, so we need to
2504 // generate an extra node that just propagates the value of the
2505 // subexpression.
2506
2507 Expr* Ex = U->getSubExpr()->IgnoreParens();
2508 NodeSet Tmp;
2509 Visit(Ex, Pred, Tmp);
2510
2511 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002512 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002513 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002514 }
2515
2516 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002517 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002518
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002519 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002520
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002521 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002522 Expr* Ex = U->getSubExpr()->IgnoreParens();
2523 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002524 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002525
2526 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002527 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002528 SVal V = state->getSVal(Ex);
2529 state = state->bindExpr(U, V);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002530 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002531 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002532
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002533 return;
2534 }
2535
2536 case UnaryOperator::LNot:
2537 case UnaryOperator::Minus:
2538 case UnaryOperator::Not: {
2539
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002540 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002541 Expr* Ex = U->getSubExpr()->IgnoreParens();
2542 NodeSet Tmp;
2543 Visit(Ex, Pred, Tmp);
2544
2545 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002546 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002547
2548 // Get the value of the subexpression.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002549 SVal V = state->getSVal(Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002550
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002551 if (V.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002552 MakeNode(Dst, U, *I, state->bindExpr(U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002553 continue;
2554 }
2555
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002556// QualType DstT = getContext().getCanonicalType(U->getType());
2557// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2558//
2559// if (DstT != SrcT) // Perform promotions.
2560// V = EvalCast(V, DstT);
2561//
2562// if (V.isUnknownOrUndef()) {
2563// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2564// continue;
2565// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002566
2567 switch (U->getOpcode()) {
2568 default:
2569 assert(false && "Invalid Opcode.");
2570 break;
2571
2572 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002573 // FIXME: Do we need to handle promotions?
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002574 state = state->bindExpr(U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002575 break;
2576
2577 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002578 // FIXME: Do we need to handle promotions?
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002579 state = state->bindExpr(U, EvalMinus(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002580 break;
2581
2582 case UnaryOperator::LNot:
2583
2584 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2585 //
2586 // Note: technically we do "E == 0", but this is the same in the
2587 // transfer functions as "0 == E".
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002588 SVal Result;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002589
Zhongxing Xu097fc982008-10-17 05:57:07 +00002590 if (isa<Loc>(V)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +00002591 Loc X = ValMgr.makeNull();
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002592 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2593 U->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002594 }
2595 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002596 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002597 Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X,
2598 U->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002599 }
2600
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002601 state = state->bindExpr(U, Result);
2602
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002603 break;
2604 }
2605
Ted Kremeneke66ba682009-02-13 01:45:31 +00002606 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002607 }
2608
2609 return;
2610 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002611 }
2612
2613 // Handle ++ and -- (both pre- and post-increment).
2614
2615 assert (U->isIncrementDecrementOp());
2616 NodeSet Tmp;
2617 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002618 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002619
2620 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2621
Ted Kremeneke66ba682009-02-13 01:45:31 +00002622 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002623 SVal V1 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002624
2625 // Perform a load.
2626 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002627 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002628
2629 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2630
Ted Kremeneke66ba682009-02-13 01:45:31 +00002631 state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002632 SVal V2 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002633
2634 // Propagate unknown and undefined values.
2635 if (V2.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002636 MakeNode(Dst, U, *I2, state->bindExpr(U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002637 continue;
2638 }
2639
Ted Kremeneke43de222009-03-11 03:54:24 +00002640 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002641 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2642 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002643
Zhongxing Xue32c7652009-06-23 09:02:15 +00002644 SVal Result = EvalBinOp(state, Op, V2, ValMgr.makeIntVal(1U,U->getType()),
Zhongxing Xuc890e332009-05-20 09:00:16 +00002645 U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002646
2647 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002648 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002649 Result = ValMgr.getConjuredSymbolVal(Ex,
2650 Builder->getCurrentBlockCount());
Ted Kremenek65411632009-04-21 22:38:05 +00002651
2652 // If the value is a location, ++/-- should always preserve
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002653 // non-nullness. Check if the original value was non-null, and if so
2654 // propagate that constraint.
Ted Kremenek65411632009-04-21 22:38:05 +00002655 if (Loc::IsLocType(U->getType())) {
Zhongxing Xuc890e332009-05-20 09:00:16 +00002656 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenek65411632009-04-21 22:38:05 +00002657 ValMgr.makeZeroVal(U->getType()),
2658 getContext().IntTy);
2659
Ted Kremenek70970bf2009-06-18 22:57:13 +00002660 if (!state->assume(Constraint, true)) {
Ted Kremenek65411632009-04-21 22:38:05 +00002661 // It isn't feasible for the original value to be null.
2662 // Propagate this constraint.
Zhongxing Xuc890e332009-05-20 09:00:16 +00002663 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenek65411632009-04-21 22:38:05 +00002664 ValMgr.makeZeroVal(U->getType()),
2665 getContext().IntTy);
2666
Ted Kremenek70970bf2009-06-18 22:57:13 +00002667 state = state->assume(Constraint, false);
2668 assert(state);
Ted Kremenek65411632009-04-21 22:38:05 +00002669 }
2670 }
2671 }
Ted Kremenek607415e2009-03-20 20:10:45 +00002672
Ted Kremenek70970bf2009-06-18 22:57:13 +00002673 state = state->bindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002674
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002675 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002676 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002677 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002678 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002679}
2680
Ted Kremenek31803c32008-03-17 21:11:24 +00002681void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2682 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2683}
2684
2685void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2686 AsmStmt::outputs_iterator I,
2687 AsmStmt::outputs_iterator E,
2688 NodeTy* Pred, NodeSet& Dst) {
2689 if (I == E) {
2690 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2691 return;
2692 }
2693
2694 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002695 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002696
2697 ++I;
2698
2699 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2700 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2701}
2702
2703void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2704 AsmStmt::inputs_iterator I,
2705 AsmStmt::inputs_iterator E,
2706 NodeTy* Pred, NodeSet& Dst) {
2707 if (I == E) {
2708
2709 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002710 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002711
2712 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2713 // which interprets the inline asm and stores proper results in the
2714 // outputs.
2715
Ted Kremeneke66ba682009-02-13 01:45:31 +00002716 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002717
2718 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2719 OE = A->end_outputs(); OI != OE; ++OI) {
2720
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002721 SVal X = state->getSVal(*OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002722 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002723
Zhongxing Xu097fc982008-10-17 05:57:07 +00002724 if (isa<Loc>(X))
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002725 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002726 }
2727
Ted Kremeneke66ba682009-02-13 01:45:31 +00002728 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002729 return;
2730 }
2731
2732 NodeSet Tmp;
2733 Visit(*I, Pred, Tmp);
2734
2735 ++I;
2736
2737 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2738 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2739}
2740
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002741void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2742 assert (Builder && "GRStmtNodeBuilder must be defined.");
2743
2744 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002745
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002746 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2747 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002748
Ted Kremenekc7469542008-07-17 23:15:45 +00002749 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002750
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002751 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002752
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002753 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002754 MakeNode(Dst, S, Pred, GetState(Pred));
2755}
2756
Ted Kremenek108048c2008-03-31 15:02:58 +00002757void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2758
2759 Expr* R = S->getRetValue();
2760
2761 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002762 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002763 return;
2764 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002765
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002766 NodeSet Tmp;
2767 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002768
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002769 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002770 SVal X = (*I)->getState()->getSVal(R);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002771
2772 // Check if we return the address of a stack variable.
2773 if (isa<loc::MemRegionVal>(X)) {
2774 // Determine if the value is on the stack.
2775 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002776
Ted Kremenekdd2ec492009-06-23 18:05:21 +00002777 if (R && R->hasStackStorage()) {
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002778 // Create a special node representing the error.
2779 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2780 N->markAsSink();
2781 RetsStackAddr.insert(N);
2782 }
2783 continue;
2784 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002785 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002786 // Check if we return an undefined value.
2787 else if (X.isUndef()) {
2788 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2789 N->markAsSink();
2790 RetsUndef.insert(N);
2791 }
2792 continue;
2793 }
2794
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002795 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002796 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002797}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002798
Ted Kremenekca5f6202008-04-15 23:06:53 +00002799//===----------------------------------------------------------------------===//
2800// Transfer functions: Binary operators.
2801//===----------------------------------------------------------------------===//
2802
Ted Kremeneke66ba682009-02-13 01:45:31 +00002803const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002804 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002805
2806 // Divide by undefined? (potentially zero)
2807
2808 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002809 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002810
2811 if (DivUndef) {
2812 DivUndef->markAsSink();
2813 ExplicitBadDivides.insert(DivUndef);
2814 }
2815
Ted Kremenek6c438f82008-10-20 23:40:25 +00002816 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002817 }
2818
2819 // Check for divide/remainder-by-zero.
2820 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002821 const GRState* zeroState = state->assume(Denom, false);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002822
2823 // Second, "assume" that the denominator cannot be 0.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002824 state = state->assume(Denom, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002825
Ted Kremenek70970bf2009-06-18 22:57:13 +00002826 // Create the node for the divide-by-zero (if it occurred).
2827 if (zeroState)
2828 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, zeroState, Pred)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002829 DivZeroNode->markAsSink();
2830
Ted Kremenek70970bf2009-06-18 22:57:13 +00002831 if (state)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002832 ImplicitBadDivides.insert(DivZeroNode);
2833 else
2834 ExplicitBadDivides.insert(DivZeroNode);
2835
2836 }
2837
Ted Kremenek70970bf2009-06-18 22:57:13 +00002838 return state;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002839}
2840
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002841void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002842 GRExprEngine::NodeTy* Pred,
2843 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002844
2845 NodeSet Tmp1;
2846 Expr* LHS = B->getLHS()->IgnoreParens();
2847 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002848
Ted Kremenek52510d82008-12-06 02:39:30 +00002849 // FIXME: Add proper support for ObjCKVCRefExpr.
2850 if (isa<ObjCKVCRefExpr>(LHS)) {
2851 Visit(RHS, Pred, Dst);
2852 return;
2853 }
2854
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002855 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002856 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002857 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002858 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002859
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002860 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002861
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002862 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002863
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002864 // Process the RHS.
2865
2866 NodeSet Tmp2;
2867 Visit(RHS, *I1, Tmp2);
2868
2869 // With both the LHS and RHS evaluated, process the operation itself.
2870
2871 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002872
Ted Kremeneke66ba682009-02-13 01:45:31 +00002873 const GRState* state = GetState(*I2);
2874 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002875
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002876 SVal RightV = state->getSVal(RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002877 BinaryOperator::Opcode Op = B->getOpcode();
2878
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002879 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002880
Ted Kremenekf031b872008-01-23 19:59:44 +00002881 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002882
Ted Kremenekd4676512008-03-12 21:45:47 +00002883 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002884 // FIXME: Handle structs.
2885 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002886
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002887 if ((RightV.isUnknown() ||
2888 !getConstraintManager().canReasonAbout(RightV))
2889 && (Loc::IsLocType(T) ||
2890 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002891 unsigned Count = Builder->getCurrentBlockCount();
2892 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002893 }
2894
Ted Kremenekd4676512008-03-12 21:45:47 +00002895 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002896 // to the L-Value represented by the LHS.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002897 EvalStore(Dst, B, LHS, *I2, state->bindExpr(B, RightV), LeftV,
Ted Kremeneke66ba682009-02-13 01:45:31 +00002898 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002899 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002900 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002901
2902 case BinaryOperator::Div:
2903 case BinaryOperator::Rem:
2904
Ted Kremenek6c438f82008-10-20 23:40:25 +00002905 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002906 if (RHS->getType()->isIntegerType() &&
2907 RHS->getType()->isScalarType()) {
2908
Ted Kremeneke66ba682009-02-13 01:45:31 +00002909 state = CheckDivideZero(B, state, *I2, RightV);
2910 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002911 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002912
2913 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002914
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002915 default: {
2916
2917 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002918 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002919
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00002920 // Process non-assignments except commas or short-circuited
2921 // logical expressions (LAnd and LOr).
Zhongxing Xuc890e332009-05-20 09:00:16 +00002922 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002923
2924 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002925 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002926 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002927 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002928 }
2929 else
2930 Dst.Add(*I2);
2931
Ted Kremenekb8782e12008-02-21 19:15:37 +00002932 continue;
2933 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002934
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002935 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002936
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002937 // The operands were *not* undefined, but the result is undefined.
2938 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002939
Ted Kremeneke66ba682009-02-13 01:45:31 +00002940 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002941 UndefNode->markAsSink();
2942 UndefResults.insert(UndefNode);
2943 }
2944
2945 continue;
2946 }
2947
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002948 // Otherwise, create a new node.
2949
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002950 MakeNode(Dst, B, *I2, state->bindExpr(B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002951 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002952 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002953 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002954
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002955 assert (B->isCompoundAssignmentOp());
2956
Ted Kremenek570882a2009-02-07 00:52:24 +00002957 switch (Op) {
2958 default:
2959 assert(0 && "Invalid opcode for compound assignment.");
2960 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2961 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2962 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2963 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2964 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2965 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2966 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2967 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2968 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2969 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00002970 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002971
2972 // Perform a load (the LHS). This performs the checks for
2973 // null dereferences, and so on.
2974 NodeSet Tmp3;
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002975 SVal location = state->getSVal(LHS);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002976 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002977
2978 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2979
Ted Kremeneke66ba682009-02-13 01:45:31 +00002980 state = GetState(*I3);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002981 SVal V = state->getSVal(LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002982
Ted Kremenek6c438f82008-10-20 23:40:25 +00002983 // Check for divide-by-zero.
2984 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00002985 && RHS->getType()->isIntegerType()
2986 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002987
2988 // CheckDivideZero returns a new state where the denominator
2989 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002990 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002991
Ted Kremeneke66ba682009-02-13 01:45:31 +00002992 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00002993 continue;
2994 }
2995
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002996 // Propagate undefined values (left-side).
2997 if (V.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002998 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002999 continue;
3000 }
3001
3002 // Propagate unknown values (left and right-side).
3003 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003004 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, UnknownVal()),
Ted Kremeneke66ba682009-02-13 01:45:31 +00003005 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003006 continue;
3007 }
3008
3009 // At this point:
3010 //
3011 // The LHS is not Undef/Unknown.
3012 // The RHS is not Unknown.
3013
3014 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00003015 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003016 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00003017
3018 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3019 CLHSTy = getContext().getCanonicalType(CTy);
3020
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003021 QualType LTy = getContext().getCanonicalType(LHS->getType());
3022 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00003023
3024 // Promote LHS.
3025 V = EvalCast(V, CLHSTy);
3026
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003027 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00003028 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00003029 // Propagate undefined values (right-side).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003030 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003031 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003032 continue;
3033 }
3034
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003035 // Compute the result of the operation.
Ted Kremenekd1c53ff2009-06-26 00:05:51 +00003036 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
Zhongxing Xuc890e332009-05-20 09:00:16 +00003037 B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003038
3039 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003040 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003041 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003042 UndefNode->markAsSink();
3043 UndefResults.insert(UndefNode);
3044 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003045 continue;
3046 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003047
3048 // EXPERIMENTAL: "Conjured" symbols.
3049 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003050
3051 SVal LHSVal;
3052
Ted Kremenekd6a5a422009-03-11 02:24:48 +00003053 if ((Result.isUnknown() ||
3054 !getConstraintManager().canReasonAbout(Result))
3055 && (Loc::IsLocType(CTy)
3056 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00003057
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003058 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003059
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003060 // The symbolic value is actually for the type of the left-hand side
3061 // expression, not the computation type, as this is the value the
3062 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00003063 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003064
Zhongxing Xu5c70c772008-11-23 05:52:28 +00003065 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003066 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003067 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003068 else {
3069 // The left-hand side may bind to a different value then the
3070 // computation type.
3071 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3072 }
3073
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003074 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, Result), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003075 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003076 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00003077 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003078 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003079}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003080
3081//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003082// Transfer-function Helpers.
3083//===----------------------------------------------------------------------===//
3084
Zhongxing Xuc890e332009-05-20 09:00:16 +00003085SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3086 SVal L, SVal R, QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00003087
3088 if (L.isUndef() || R.isUndef())
3089 return UndefinedVal();
3090
3091 if (L.isUnknown() || R.isUnknown())
3092 return UnknownVal();
3093
3094 if (isa<Loc>(L)) {
3095 if (isa<Loc>(R))
Ted Kremenek40d9c582009-07-16 01:32:00 +00003096 return SVator.EvalBinOpLL(Op, cast<Loc>(L), cast<Loc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003097 else
Ted Kremenek40d9c582009-07-16 01:32:00 +00003098 return SVator.EvalBinOpLN(state, Op, cast<Loc>(L), cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003099 }
3100
3101 if (isa<Loc>(R)) {
3102 // Support pointer arithmetic where the increment/decrement operand
3103 // is on the left and the pointer on the right.
3104
3105 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3106
3107 // Commute the operands.
Ted Kremenek40d9c582009-07-16 01:32:00 +00003108 return SVator.EvalBinOpLN(state, Op, cast<Loc>(R), cast<NonLoc>(L), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003109 }
3110 else
Ted Kremenek40d9c582009-07-16 01:32:00 +00003111 return SVator.EvalBinOpNN(Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003112}
3113
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003114//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00003115// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003116//===----------------------------------------------------------------------===//
3117
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003118#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003119static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003120static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00003121
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003122namespace llvm {
3123template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003124struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003125 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00003126
Ted Kremeneka853de62008-02-14 22:54:53 +00003127 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3128
3129 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00003130 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00003131 GraphPrintCheckerState->isUndefDeref(N) ||
3132 GraphPrintCheckerState->isUndefStore(N) ||
3133 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00003134 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3135 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00003136 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00003137 GraphPrintCheckerState->isBadCall(N) ||
3138 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00003139 return "color=\"red\",style=\"filled\"";
3140
Ted Kremenekc2d07202008-02-28 20:32:03 +00003141 if (GraphPrintCheckerState->isNoReturnCall(N))
3142 return "color=\"blue\",style=\"filled\"";
3143
Ted Kremeneka853de62008-02-14 22:54:53 +00003144 return "";
3145 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003146
Owen Anderson18ead7b2009-06-24 17:37:55 +00003147 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*,
3148 bool ShortNames) {
Ted Kremenekdd04ed62009-06-24 23:06:47 +00003149
3150 std::string sbuf;
3151 llvm::raw_string_ostream Out(sbuf);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003152
3153 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003154 ProgramPoint Loc = N->getLocation();
3155
3156 switch (Loc.getKind()) {
3157 case ProgramPoint::BlockEntranceKind:
3158 Out << "Block Entrance: B"
3159 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3160 break;
3161
3162 case ProgramPoint::BlockExitKind:
3163 assert (false);
3164 break;
3165
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003166 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003167 if (isa<PostStmt>(Loc)) {
3168 const PostStmt& L = cast<PostStmt>(Loc);
3169 Stmt* S = L.getStmt();
3170 SourceLocation SLoc = S->getLocStart();
3171
3172 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattner7099c782009-06-30 01:26:17 +00003173 LangOptions LO; // FIXME.
3174 S->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003175
3176 if (SLoc.isFileID()) {
3177 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003178 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3179 << " col="
3180 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3181 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003182 }
3183
Ted Kremenek0441f112009-05-07 18:27:16 +00003184 if (isa<PostLoad>(Loc))
3185 Out << "\\lPostLoad\\l;";
3186 else if (isa<PostStore>(Loc))
3187 Out << "\\lPostStore\\l";
3188 else if (isa<PostLValue>(Loc))
3189 Out << "\\lPostLValue\\l";
3190 else if (isa<PostLocationChecksSucceed>(Loc))
3191 Out << "\\lPostLocationChecksSucceed\\l";
3192 else if (isa<PostNullCheckFailed>(Loc))
3193 Out << "\\lPostNullCheckFailed\\l";
3194
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003195 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3196 Out << "\\|Implicit-Null Dereference.\\l";
3197 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3198 Out << "\\|Explicit-Null Dereference.\\l";
3199 else if (GraphPrintCheckerState->isUndefDeref(N))
3200 Out << "\\|Dereference of undefialied value.\\l";
3201 else if (GraphPrintCheckerState->isUndefStore(N))
3202 Out << "\\|Store to Undefined Loc.";
3203 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3204 Out << "\\|Explicit divide-by zero or undefined value.";
3205 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3206 Out << "\\|Implicit divide-by zero or undefined value.";
3207 else if (GraphPrintCheckerState->isUndefResult(N))
3208 Out << "\\|Result of operation is undefined.";
3209 else if (GraphPrintCheckerState->isNoReturnCall(N))
3210 Out << "\\|Call to function marked \"noreturn\".";
3211 else if (GraphPrintCheckerState->isBadCall(N))
3212 Out << "\\|Call to NULL/Undefined.";
3213 else if (GraphPrintCheckerState->isUndefArg(N))
3214 Out << "\\|Argument in call is undefined";
3215
3216 break;
3217 }
3218
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003219 const BlockEdge& E = cast<BlockEdge>(Loc);
3220 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3221 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003222
3223 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003224
3225 SourceLocation SLoc = T->getLocStart();
3226
Ted Kremenek90960972008-01-30 23:03:39 +00003227 Out << "\\|Terminator: ";
Chris Lattner7099c782009-06-30 01:26:17 +00003228 LangOptions LO; // FIXME.
3229 E.getSrc()->printTerminator(Out, LO);
Ted Kremenek90960972008-01-30 23:03:39 +00003230
Ted Kremenekf97c6682008-03-09 03:30:59 +00003231 if (SLoc.isFileID()) {
3232 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003233 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3234 << " col="
3235 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003236 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003237
Ted Kremenekaee121c2008-02-13 23:08:21 +00003238 if (isa<SwitchStmt>(T)) {
3239 Stmt* Label = E.getDst()->getLabel();
3240
3241 if (Label) {
3242 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3243 Out << "\\lcase ";
Chris Lattner7099c782009-06-30 01:26:17 +00003244 LangOptions LO; // FIXME.
3245 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003246
Ted Kremenekaee121c2008-02-13 23:08:21 +00003247 if (Stmt* RHS = C->getRHS()) {
3248 Out << " .. ";
Chris Lattner7099c782009-06-30 01:26:17 +00003249 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenekaee121c2008-02-13 23:08:21 +00003250 }
3251
3252 Out << ":";
3253 }
3254 else {
3255 assert (isa<DefaultStmt>(Label));
3256 Out << "\\ldefault:";
3257 }
3258 }
3259 else
3260 Out << "\\l(implicit) default:";
3261 }
3262 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003263 // FIXME
3264 }
3265 else {
3266 Out << "\\lCondition: ";
3267 if (*E.getSrc()->succ_begin() == E.getDst())
3268 Out << "true";
3269 else
3270 Out << "false";
3271 }
3272
3273 Out << "\\l";
3274 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003275
Ted Kremenekb31af242008-02-28 09:25:22 +00003276 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3277 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003278 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003279 }
3280 }
3281
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003282 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003283
Ted Kremenek18a636d2009-06-18 01:23:53 +00003284 const GRState *state = N->getState();
3285 state->printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003286
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003287 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003288 return Out.str();
3289 }
3290};
3291} // end llvm namespace
3292#endif
3293
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003294#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003295template <typename ITERATOR>
3296GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3297
3298template <>
3299GRExprEngine::NodeTy*
3300GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3301 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3302 return I->first;
3303}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003304#endif
3305
3306void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003307#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003308 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003309 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003310
3311 // Flush any outstanding reports to make sure we cover all the nodes.
3312 // This does not cause them to get displayed.
3313 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3314 const_cast<BugType*>(*I)->FlushReports(BR);
3315
3316 // Iterate through the reports and get their nodes.
3317 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3318 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3319 const BugReportEquivClass& EQ = *I2;
3320 const BugReport &R = **EQ.begin();
3321 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3322 if (N) Src.push_back(N);
3323 }
3324 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003325
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003326 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003327 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003328 else {
3329 GraphPrintCheckerState = this;
3330 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003331
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003332 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003333
3334 GraphPrintCheckerState = NULL;
3335 GraphPrintSourceManager = NULL;
3336 }
3337#endif
3338}
3339
3340void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3341#ifndef NDEBUG
3342 GraphPrintCheckerState = this;
3343 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003344
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003345 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003346
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003347 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003348 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003349 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003350 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003351
Ted Kremenek428d39e2008-01-30 23:24:39 +00003352 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003353 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003354#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003355}