blob: 52175ee2a001752166825127ab5ca885237a4b5e [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek50df4f42008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek0e80dea2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000025#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000026#include "llvm/ADT/ImmutableList.h"
27#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000029
Ted Kremenek9f6b1612008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
32#include <sstream>
33#endif
34
Ted Kremenekd4467432008-02-14 22:16:04 +000035using namespace clang;
36using llvm::dyn_cast;
37using llvm::cast;
38using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000039
Ted Kremenekca5f6202008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41// Engine construction and deletion.
42//===----------------------------------------------------------------------===//
43
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000044namespace {
45
46class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
47 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
48 typedef llvm::DenseMap<void*,Checks> MapTy;
49
50 MapTy M;
51 Checks::Factory F;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000052 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000053
54public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000055 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
56 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000057
58 virtual ~MappedBatchAuditor() {
59 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
60
61 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
62 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
63
64 GRSimpleAPICheck* check = *I;
65
66 if (AlreadyVisited.count(check))
67 continue;
68
69 AlreadyVisited.insert(check);
70 delete check;
71 }
72 }
73
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000074 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000075 assert (A && "Check cannot be null.");
76 void* key = reinterpret_cast<void*>((uintptr_t) C);
77 MapTy::iterator I = M.find(key);
78 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
79 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000080
81 void AddCheck(GRSimpleAPICheck *A) {
82 assert (A && "Check cannot be null.");
83 AllStmts = F.Concat(A, AllStmts);
84 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000085
Ted Kremenekabd89ac2008-08-13 04:27:00 +000086 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000087 // First handle the auditors that accept all statements.
88 bool isSink = false;
89 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
90 isSink |= (*I)->Audit(N, VMgr);
91
92 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000093 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
94 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
95 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000096 if (MI != M.end()) {
97 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
98 isSink |= (*I)->Audit(N, VMgr);
99 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000100
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremenek5f20a632008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekf973eb02008-03-09 18:05:48 +0000116
Ted Kremenek1607f512008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000118 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000119 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000120 StoreManagerCreator SMC,
121 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000122 : CoreEngine(cfg, CD, Ctx, *this),
123 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000124 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000125 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000126 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000127 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekcda58d22009-04-09 16:46:55 +0000128 ValMgr(StateMgr.getValueManager()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000129 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000130 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
131 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000132 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000133 BR(BRD, *this),
134 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000135
Ted Kremenek72f52c02008-06-20 21:45:25 +0000136GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000137 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000138 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000139}
140
Ted Kremenekca5f6202008-04-15 23:06:53 +0000141//===----------------------------------------------------------------------===//
142// Utility methods.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000145
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000146void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000147 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000148 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000149 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000150}
151
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000152void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
153 if (!BatchAuditor)
154 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
155
156 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000157}
158
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000159void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
160 if (!BatchAuditor)
161 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
162
163 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
164}
165
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000166const GRState* GRExprEngine::getInitialState() {
Ted Kremenek04c0add2009-04-10 00:59:50 +0000167 const GRState *state = StateMgr.getInitialState();
168
169 // Precondition: the first argument of 'main' is an integer guaranteed
170 // to be > 0.
171 // FIXME: It would be nice if we had a more general mechanism to add
172 // such preconditions. Some day.
173 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
174 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
175 FD->getNumParams() > 0) {
176 const ParmVarDecl *PD = FD->getParamDecl(0);
177 QualType T = PD->getType();
178 if (T->isIntegerType())
179 if (const MemRegion *R = StateMgr.getRegion(PD)) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000180 SVal V = state->getSVal(loc::MemRegionVal(R));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000181 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek04c0add2009-04-10 00:59:50 +0000182 ValMgr.makeZeroVal(T),
183 getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +0000184
185 if (const GRState *newState = state->assume(Constraint, true))
186 state = newState;
Ted Kremenek04c0add2009-04-10 00:59:50 +0000187 }
188 }
189
190 return state;
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000191}
192
Ted Kremenekca5f6202008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek820c73b2009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremenekca5f6202008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000213
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000215 SymbolReaper SymReaper(Liveness, SymMgr);
216 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt, SymReaper)
218 : EntryNode->getState();
219
Ted Kremenek7487f942008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000222
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenekf05eec42008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenekc7469542008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000237 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek7487f942008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek7487f942008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek7487f942008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000249 Visit(S, *I, Dst);
250
251 // Do we need to auto-generate a node? We only need to do this to generate
252 // a node with a "cleaned" state; GRCoreEngine will actually handle
253 // auto-transitions for other cases.
254 if (Dst.size() == 1 && *Dst.begin() == EntryNode
255 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
256 HasAutoGenerated = true;
257 builder.generateNode(S, GetState(EntryNode), *I);
258 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000259 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000260
Ted Kremenekca5f6202008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenekfa7be362008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000270}
271
Ted Kremenek820c73b2009-03-11 02:41:36 +0000272void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
273 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
274 S->getLocStart(),
275 "Error evaluating statement");
276
Ted Kremenekca5f6202008-04-15 23:06:53 +0000277 // FIXME: add metadata to the CFG so that we can disable
278 // this check when we KNOW that there is no block-level subexpression.
279 // The motivation is that this check requires a hashtable lookup.
280
281 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
282 Dst.Add(Pred);
283 return;
284 }
285
286 switch (S->getStmtClass()) {
287
288 default:
289 // Cases we intentionally have "default" handle:
290 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
291
292 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
293 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000298
299 case Stmt::AsmStmtClass:
300 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
301 break;
302
303 case Stmt::BinaryOperatorClass: {
304 BinaryOperator* B = cast<BinaryOperator>(S);
305
306 if (B->isLogicalOp()) {
307 VisitLogicalExpr(B, Pred, Dst);
308 break;
309 }
310 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000312 MakeNode(Dst, B, Pred, state->bindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000315
Ted Kremenek8f520972009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremenekca5f6202008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000326
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000331 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000332 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000333
Ted Kremenekca5f6202008-04-15 23:06:53 +0000334 // FIXME: ChooseExpr is really a constant. We need to fix
335 // the CFG do not model them as explicit control-flow.
336
337 case Stmt::ChooseExprClass: { // __builtin_choose_expr
338 ChooseExpr* C = cast<ChooseExpr>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::CompoundAssignOperatorClass:
344 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
345 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000350
351 case Stmt::ConditionalOperatorClass: { // '?' operator
352 ConditionalOperator* C = cast<ConditionalOperator>(S);
353 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
354 break;
355 }
356
357 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000376
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000388
Ted Kremenekca5f6202008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenek3c186252008-12-09 20:18:58 +0000394 case Stmt::ObjCAtThrowStmtClass: {
395 // FIXME: This is not complete. We basically treat @throw as
396 // an abort.
397 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
398 Builder->BuildSinks = true;
399 MakeNode(Dst, S, Pred, GetState(Pred));
400 break;
401 }
402
Ted Kremenekca5f6202008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000417
418 if (SE->getSubStmt()->body_empty()) {
419 // Empty statement expression.
420 assert(SE->getType() == getContext().VoidTy
421 && "Empty statement expression must have void type.");
422 Dst.Add(Pred);
423 break;
424 }
425
426 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
427 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000428 MakeNode(Dst, SE, Pred, state->bindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000429 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000439
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000440 case Stmt::UnaryOperatorClass: {
441 UnaryOperator *U = cast<UnaryOperator>(S);
442 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
443 NodeSet Tmp;
444 VisitUnaryOperator(U, Pred, Tmp, false);
445 EvalEagerlyAssume(Dst, Tmp, U);
446 }
447 else
448 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000449 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000450 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000455
456 Ex = Ex->IgnoreParens();
457
458 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
459 Dst.Add(Pred);
460 return;
461 }
462
463 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000464
465 case Stmt::ArraySubscriptExprClass:
466 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000478 case Stmt::UnaryOperatorClass:
479 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
480 return;
481
482 case Stmt::MemberExprClass:
483 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
484 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000485
Ted Kremenekd83daa52008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000488 return;
489
Ted Kremenek71c707b2008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek93cb3d12009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenek71c707b2008-10-17 17:24:14 +0000492 // FIXME: Property assignments are lvalues, but not really "locations".
493 // e.g.: self.x = something;
494 // Here the "self.x" really can translate to a method call (setter) when
495 // the assignment is made. Moreover, the entire assignment expression
496 // evaluate to whatever "something" is, not calling the "getter" for
497 // the property (which would make sense since it can have side effects).
498 // We'll probably treat this as a location, but not one that we can
499 // take the address of. Perhaps we need a new SVal class for cases
500 // like thsis?
501 // Note that we have a similar problem for bitfields, since they don't
502 // have "locations" in the sense that we can take their address.
503 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000504 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000508 SVal V = state->getLValue(cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000512
Ted Kremenek2c829a32008-10-18 04:15:35 +0000513 default:
514 // Arbitrary subexpressions can return aggregate temporaries that
515 // can be used in a lvalue context. We need to enhance our support
516 // of such temporaries in both the environment and the store, so right
517 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000521
Ted Kremenek2c829a32008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000537// Generic node creation.
538//===----------------------------------------------------------------------===//
539
540GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
541 NodeTy* Pred,
542 const GRState* St,
543 ProgramPoint::Kind K,
544 const void *tag) {
545
546 assert (Builder && "GRStmtNodeBuilder not present.");
547 SaveAndRestore<const void*> OldTag(Builder->Tag);
548 Builder->Tag = tag;
549 return Builder->MakeNode(Dst, S, Pred, St, K);
550}
551
552//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneke66ba682009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000562 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000563
564 case Stmt::BinaryOperatorClass: { // '&&' and '||'
565
566 BinaryOperator* B = cast<BinaryOperator>(Terminator);
567 BinaryOperator::Opcode Op = B->getOpcode();
568
569 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
570
571 // For &&, if we take the true branch, then the value of the whole
572 // expression is that of the RHS expression.
573 //
574 // For ||, if we take the false branch, then the value of the whole
575 // expression is that of the RHS expression.
576
577 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
578 (Op == BinaryOperator::LOr && !branchTaken)
579 ? B->getRHS() : B->getLHS();
580
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000581 return state->bindBlkExpr(B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000582 }
583
584 case Stmt::ConditionalOperatorClass: { // ?:
585
586 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
587
588 // For ?, if branchTaken == true then the value is either the LHS or
589 // the condition itself. (GNU extension).
590
591 Expr* Ex;
592
593 if (branchTaken)
594 Ex = C->getLHS() ? C->getLHS() : C->getCond();
595 else
596 Ex = C->getRHS();
597
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000598 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000599 }
600
601 case Stmt::ChooseExprClass: { // ?:
602
603 ChooseExpr* C = cast<ChooseExpr>(Terminator);
604
605 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000606 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenekc39c2172009-03-13 16:32:54 +0000611/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
612/// to try to recover some path-sensitivity for casts of symbolic
613/// integers that promote their values (which are currently not tracked well).
614/// This function returns the SVal bound to Condition->IgnoreCasts if all the
615// cast(s) did was sign-extend the original value.
616static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
617 Stmt* Condition, ASTContext& Ctx) {
618
619 Expr *Ex = dyn_cast<Expr>(Condition);
620 if (!Ex)
621 return UnknownVal();
622
623 uint64_t bits = 0;
624 bool bitsInit = false;
625
626 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
627 QualType T = CE->getType();
628
629 if (!T->isIntegerType())
630 return UnknownVal();
631
632 uint64_t newBits = Ctx.getTypeSize(T);
633 if (!bitsInit || newBits < bits) {
634 bitsInit = true;
635 bits = newBits;
636 }
637
638 Ex = CE->getSubExpr();
639 }
640
641 // We reached a non-cast. Is it a symbolic value?
642 QualType T = Ex->getType();
643
644 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
645 return UnknownVal();
646
Ted Kremenekc0cccca2009-06-18 23:58:37 +0000647 return state->getSVal(Ex);
Ted Kremenekc39c2172009-03-13 16:32:54 +0000648}
649
Ted Kremenek13e167f2008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000652
Ted Kremenek17c5f112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000656
Ted Kremenek022b6052008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremeneke43de222009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000667 SVal V = PrevState->getSVal(Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenekc39c2172009-03-13 16:32:54 +0000673 case SVal::UnknownKind: {
674 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
675 if (Ex->getType()->isIntegerType()) {
676 // Try to recover some path-sensitivity. Right now casts of symbolic
677 // integers that promote their values are currently not tracked well.
678 // If 'Condition' is such an expression, try and recover the
679 // underlying value and use that instead.
680 SVal recovered = RecoverCastedSymbol(getStateManager(),
681 builder.getState(), Condition,
682 getContext());
683
684 if (!recovered.isUnknown()) {
685 V = recovered;
686 break;
687 }
688 }
689 }
690
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000693 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000694 }
Ted Kremenek90960972008-01-30 23:03:39 +0000695
Zhongxing Xu097fc982008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000708
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000710 if (const GRState *state = PrevState->assume(V, true))
Ted Kremeneke66ba682009-02-13 01:45:31 +0000711 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000712 else
713 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000714
715 // Process the false branch.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000716 if (const GRState *state = PrevState->assume(V, false))
Ted Kremeneke66ba682009-02-13 01:45:31 +0000717 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000718 else
719 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000720}
721
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000722/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000723/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000724void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000725
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000726 const GRState *state = builder.getState();
727 SVal V = state->getSVal(builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000728
729 // Three possibilities:
730 //
731 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000732 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000733 // (3) We have no clue about the label. Dispatch to all targets.
734 //
735
736 typedef IndirectGotoNodeBuilder::iterator iterator;
737
Zhongxing Xu097fc982008-10-17 05:57:07 +0000738 if (isa<loc::GotoLabel>(V)) {
739 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000740
741 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000742 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000743 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000744 return;
745 }
746 }
747
748 assert (false && "No block with label.");
749 return;
750 }
751
Zhongxing Xu097fc982008-10-17 05:57:07 +0000752 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000753 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000754 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000755 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000756 return;
757 }
758
759 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekcdc3a3c2009-04-23 17:49:43 +0000760 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000761
762 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000763 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000764}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000765
Ted Kremenekca5f6202008-04-15 23:06:53 +0000766
767void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
768 NodeTy* Pred, NodeSet& Dst) {
769
770 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
771
Ted Kremeneke66ba682009-02-13 01:45:31 +0000772 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000773 SVal X = state->getBlkExprSVal(Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000774
775 assert (X.isUndef());
776
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000777 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
778 assert(SE);
779 X = state->getBlkExprSVal(SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000780
781 // Make sure that we invalidate the previous binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000782 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000783}
784
Ted Kremenekaee121c2008-02-13 23:08:21 +0000785/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
786/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000787void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
788 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000789 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000790 Expr* CondE = builder.getCondition();
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000791 SVal CondV = state->getSVal(CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000792
Ted Kremenekb31af242008-02-28 09:25:22 +0000793 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000794 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000795 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000796 return;
797 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000798
Ted Kremeneke66ba682009-02-13 01:45:31 +0000799 const GRState* DefaultSt = state;
Ted Kremenek70970bf2009-06-18 22:57:13 +0000800 bool defaultIsFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000801
Ted Kremenek07baa252008-02-21 18:02:17 +0000802 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000803 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000804
805 // Evaluate the LHS of the case value.
806 Expr::EvalResult V1;
807 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000808
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000809 // Sanity checks. These go away in Release builds.
810 assert(b && V1.Val.isInt() && !V1.HasSideEffects
811 && "Case condition must evaluate to an integer constant.");
812 b = b; // silence unused variable warning
813 assert(V1.Val.getInt().getBitWidth() ==
814 getContext().getTypeSize(CondE->getType()));
815
Ted Kremenekaee121c2008-02-13 23:08:21 +0000816 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000817 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000818
819 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000820 b = E->Evaluate(V2, getContext());
821 assert(b && V2.Val.isInt() && !V2.HasSideEffects
822 && "Case condition must evaluate to an integer constant.");
823 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000824 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000825 else
826 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000827
828 // FIXME: Eventually we should replace the logic below with a range
829 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000830 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000831
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000832 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000833 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xuc890e332009-05-20 09:00:16 +0000834 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremenek74556a12009-03-26 03:35:11 +0000835 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000836
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000837 // Now "assume" that the case matches.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000838 if (const GRState* stateNew = state->assume(Res, true)) {
839 builder.generateCaseStmtNode(I, stateNew);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000840
841 // If CondV evaluates to a constant, then we know that this
842 // is the *only* case that we can take, so stop evaluating the
843 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000844 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000845 return;
846 }
847
848 // Now "assume" that the case doesn't match. Add this state
849 // to the default state (if it is feasible).
Ted Kremenek70970bf2009-06-18 22:57:13 +0000850 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
851 defaultIsFeasible = true;
852 DefaultSt = stateNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000853 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000854
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000855 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000856 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000857 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000858
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000859 ++V1.Val.getInt();
860 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000861
862 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000863 }
864
865 // If we reach here, than we know that the default branch is
866 // possible.
Ted Kremenek70970bf2009-06-18 22:57:13 +0000867 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000868}
869
Ted Kremenekca5f6202008-04-15 23:06:53 +0000870//===----------------------------------------------------------------------===//
871// Transfer functions: logical operations ('&&', '||').
872//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000873
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000874void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000875 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000876
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000877 assert(B->getOpcode() == BinaryOperator::LAnd ||
878 B->getOpcode() == BinaryOperator::LOr);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000879
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000880 assert(B == CurrentStmt && getCFG().isBlkExpr(B));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000881
Ted Kremeneke66ba682009-02-13 01:45:31 +0000882 const GRState* state = GetState(Pred);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000883 SVal X = state->getBlkExprSVal(B);
884 assert(X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000885
Ted Kremenekb31af242008-02-28 09:25:22 +0000886 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000887
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000888 assert(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000889
890 if (Ex == B->getRHS()) {
891
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000892 X = state->getBlkExprSVal(Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000893
Ted Kremenekb31af242008-02-28 09:25:22 +0000894 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000895
Ted Kremenekb31af242008-02-28 09:25:22 +0000896 if (X.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000897 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000898 return;
899 }
900
Ted Kremenek99ecce72008-02-26 19:05:15 +0000901 // We took the RHS. Because the value of the '&&' or '||' expression must
902 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
903 // or 1. Alternatively, we could take a lazy approach, and calculate this
904 // value later when necessary. We don't have the machinery in place for
905 // this right now, and since most logical expressions are used for branches,
Ted Kremenek70970bf2009-06-18 22:57:13 +0000906 // the payoff is not likely to be large. Instead, we do eager evaluation.
907 if (const GRState *newState = state->assume(X, true))
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000908 MakeNode(Dst, B, Pred, newState->bindBlkExpr(B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000909
Ted Kremenek70970bf2009-06-18 22:57:13 +0000910 if (const GRState *newState = state->assume(X, false))
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000911 MakeNode(Dst, B, Pred, newState->bindBlkExpr(B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000912 }
913 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000914 // We took the LHS expression. Depending on whether we are '&&' or
915 // '||' we know what the value of the expression is via properties of
916 // the short-circuiting.
Ted Kremenek99ecce72008-02-26 19:05:15 +0000917 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000918 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000919 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000920}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000921
Ted Kremenekca5f6202008-04-15 23:06:53 +0000922//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000923// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000924//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000925
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000926void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
927 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000928
Ted Kremeneke66ba682009-02-13 01:45:31 +0000929 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000930
Douglas Gregord2baafd2008-10-21 16:13:35 +0000931 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000932
933 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
934
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000935 SVal V = state->getLValue(VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000936
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000937 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000938 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000939 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000940 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000941 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000942 return;
943
944 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
945 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
946
Zhongxing Xu3c2828d2009-06-23 06:13:19 +0000947 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000948 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000949 return;
950
951 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000952 assert(asLValue);
Zhongxing Xucac107a2009-04-20 05:24:46 +0000953 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000954 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000955 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000956 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000957 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000958
959 assert (false &&
960 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000961}
962
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000963/// VisitArraySubscriptExpr - Transfer function for array accesses
964void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000965 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000966
967 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000968 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000969 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000970
971 if (Base->getType()->isVectorType()) {
972 // For vector types get its lvalue.
973 // FIXME: This may not be correct. Is the rvalue of a vector its location?
974 // In fact, I think this is just a hack. We need to get the right
975 // semantics.
976 VisitLValue(Base, Pred, Tmp);
977 }
978 else
979 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000980
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000981 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +0000982 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000983 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +0000984
985 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000986 const GRState* state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000987 SVal V = state->getLValue(A->getType(), state->getSVal(Base),
988 state->getSVal(Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +0000989
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000990 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +0000991 MakeNode(Dst, A, *I2, state->bindExpr(A, V),
Ted Kremenek0441f112009-05-07 18:27:16 +0000992 ProgramPoint::PostLValueKind);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000993 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000994 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000995 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000996 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000997}
998
Ted Kremenekd0d86202008-04-21 23:43:38 +0000999/// VisitMemberExpr - Transfer function for member expressions.
1000void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001001 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +00001002
1003 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +00001004 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +00001005
1006 if (M->isArrow())
1007 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1008 else
1009 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1010
Douglas Gregor82d44772008-12-20 23:49:58 +00001011 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1012 if (!Field) // FIXME: skipping member expressions for non-fields
1013 return;
1014
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001015 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001016 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001017 // FIXME: Should we insert some assumption logic in here to determine
1018 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001019 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001020 SVal L = state->getLValue(state->getSVal(Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001021
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001022 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001023 MakeNode(Dst, M, *I, state->bindExpr(M, L),
Ted Kremenek0441f112009-05-07 18:27:16 +00001024 ProgramPoint::PostLValueKind);
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001025 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001026 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001027 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001028}
1029
Ted Kremeneke66ba682009-02-13 01:45:31 +00001030/// EvalBind - Handle the semantics of binding a value to a specific location.
1031/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1032void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1033 const GRState* state, SVal location, SVal Val) {
1034
Ted Kremeneka42be302009-02-14 01:43:44 +00001035 const GRState* newState = 0;
1036
1037 if (location.isUnknown()) {
1038 // We know that the new state will be the same as the old state since
1039 // the location of the binding is "unknown". Consequently, there
1040 // is no reason to just create a new node.
1041 newState = state;
1042 }
1043 else {
1044 // We are binding to a value other than 'unknown'. Perform the binding
1045 // using the StoreManager.
1046 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1047 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001048
Ted Kremeneka42be302009-02-14 01:43:44 +00001049 // The next thing to do is check if the GRTransferFuncs object wants to
1050 // update the state based on the new binding. If the GRTransferFunc object
1051 // doesn't do anything, just auto-propagate the current state.
1052 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1053 newState != state);
1054
1055 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001056}
1057
1058/// EvalStore - Handle the semantics of a store via an assignment.
1059/// @param Dst The node set to store generated state nodes
1060/// @param Ex The expression representing the location of the store
1061/// @param state The current simulation state
1062/// @param location The location to store the value
1063/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001064void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001065 const GRState* state, SVal location, SVal Val,
1066 const void *tag) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001067
1068 assert (Builder && "GRStmtNodeBuilder must be defined.");
1069
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001070 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001071 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001072
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001073 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001074 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001075
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001076 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001077 state = GetState(Pred);
1078
1079 // Proceed with the store.
1080 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001081 SaveAndRestore<const void*> OldTag(Builder->Tag);
1082 Builder->PointKind = ProgramPoint::PostStoreKind;
1083 Builder->Tag = tag;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001084 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001085}
1086
1087void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001088 const GRState* state, SVal location,
1089 const void *tag) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001090
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001091 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001092 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001093
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001094 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001095 return;
1096
Ted Kremeneke66ba682009-02-13 01:45:31 +00001097 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001098
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001099 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001100 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001101
1102 // FIXME: Currently symbolic analysis "generates" new symbols
1103 // for the contents of values. We need a better approach.
1104
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001105 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001106 // This is important. We must nuke the old binding.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001107 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, UnknownVal()), K, tag);
Ted Kremenekbf573852008-04-30 04:23:07 +00001108 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001109 else {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001110 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
1111 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V), K, tag);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001112 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001113}
1114
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001115void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001116 const GRState* state, SVal location, SVal Val,
1117 const void *tag) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001118
1119 NodeSet TmpDst;
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001120 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001121
1122 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001123 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001124}
1125
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001126GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001127 const GRState* state,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001128 SVal location,
1129 const void *tag) {
1130
1131 SaveAndRestore<const void*> OldTag(Builder->Tag);
1132 Builder->Tag = tag;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001133
1134 // Check for loads/stores from/to undefined values.
1135 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001136 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001137 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001138 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001139
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001140 if (N) {
1141 N->markAsSink();
1142 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001143 }
1144
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001145 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001146 }
1147
1148 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1149 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001150 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001151
1152 // During a load, one of two possible situations arise:
1153 // (1) A crash, because the location (pointer) was NULL.
1154 // (2) The location (pointer) is not NULL, and the dereference works.
1155 //
1156 // We add these assumptions.
1157
Zhongxing Xu097fc982008-10-17 05:57:07 +00001158 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001159
1160 // "Assume" that the pointer is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001161 const GRState *StNotNull = state->assume(LV, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001162
1163 // "Assume" that the pointer is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001164 const GRState *StNull = state->assume(LV, false);
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001165
Ted Kremenek70970bf2009-06-18 22:57:13 +00001166 if (StNull) {
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001167 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001168 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001169 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001170
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001171 // We don't use "MakeNode" here because the node will be a sink
1172 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001173 NodeTy* NullNode =
1174 Builder->generateNode(Ex, StNull, Pred,
1175 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001176
Ted Kremenek70970bf2009-06-18 22:57:13 +00001177 if (NullNode) {
1178 NullNode->markAsSink();
1179 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001180 else ExplicitNullDeref.insert(NullNode);
1181 }
1182 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001183
Ted Kremenek70970bf2009-06-18 22:57:13 +00001184 if (!StNotNull)
1185 return NULL;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001186
1187 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001188 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001189 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1190 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1191 // Get the index of the accessed element.
1192 SVal Idx = ER->getIndex();
1193 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001194 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1195 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001196
Ted Kremenek70970bf2009-06-18 22:57:13 +00001197 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
1198 true);
1199 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
1200 false);
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001201
Ted Kremenek70970bf2009-06-18 22:57:13 +00001202 if (StOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001203 // Report warning. Make sink node manually.
1204 NodeTy* OOBNode =
1205 Builder->generateNode(Ex, StOutBound, Pred,
1206 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001207
1208 if (OOBNode) {
1209 OOBNode->markAsSink();
1210
Ted Kremenek70970bf2009-06-18 22:57:13 +00001211 if (StInBound)
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001212 ImplicitOOBMemAccesses.insert(OOBNode);
1213 else
1214 ExplicitOOBMemAccesses.insert(OOBNode);
1215 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001216 }
1217
Ted Kremenek70970bf2009-06-18 22:57:13 +00001218 if (!StInBound)
1219 return NULL;
1220
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001221 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001222 }
1223 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001224
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001225 // Generate a new node indicating the checks succeed.
1226 return Builder->generateNode(Ex, StNotNull, Pred,
1227 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001228}
1229
Ted Kremenekca5f6202008-04-15 23:06:53 +00001230//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001231// Transfer function: OSAtomics.
1232//
1233// FIXME: Eventually refactor into a more "plugin" infrastructure.
1234//===----------------------------------------------------------------------===//
1235
1236// Mac OS X:
1237// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1238// atomic.3.html
1239//
1240static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1241 GRExprEngine& Engine,
1242 GRStmtNodeBuilder<GRState>& Builder,
1243 CallExpr* CE, SVal L,
1244 ExplodedNode<GRState>* Pred) {
1245
1246 // Not enough arguments to match OSAtomicCompareAndSwap?
1247 if (CE->getNumArgs() != 3)
1248 return false;
1249
1250 ASTContext &C = Engine.getContext();
1251 Expr *oldValueExpr = CE->getArg(0);
1252 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1253
1254 Expr *newValueExpr = CE->getArg(1);
1255 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1256
1257 // Do the types of 'oldValue' and 'newValue' match?
1258 if (oldValueType != newValueType)
1259 return false;
1260
1261 Expr *theValueExpr = CE->getArg(2);
1262 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1263
1264 // theValueType not a pointer?
1265 if (!theValueType)
1266 return false;
1267
1268 QualType theValueTypePointee =
1269 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1270
1271 // The pointee must match newValueType and oldValueType.
1272 if (theValueTypePointee != newValueType)
1273 return false;
1274
1275 static unsigned magic_load = 0;
1276 static unsigned magic_store = 0;
1277
1278 const void *OSAtomicLoadTag = &magic_load;
1279 const void *OSAtomicStoreTag = &magic_store;
1280
1281 // Load 'theValue'.
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001282 const GRState *state = Pred->getState();
1283 ExplodedNodeSet<GRState> Tmp;
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001284 SVal location = state->getSVal(theValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001285 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1286
1287 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1288 I != E; ++I) {
1289
1290 ExplodedNode<GRState> *N = *I;
1291 const GRState *stateLoad = N->getState();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001292 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1293 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001294
1295 // Perform the comparison.
Zhongxing Xuc890e332009-05-20 09:00:16 +00001296 SVal Cmp = Engine.EvalBinOp(stateLoad,
1297 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001298 Engine.getContext().IntTy);
Ted Kremenek70970bf2009-06-18 22:57:13 +00001299
1300 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001301
1302 // Were they equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001303 if (stateEqual) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001304 // Perform the store.
1305 ExplodedNodeSet<GRState> TmpStore;
1306 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenekc0cccca2009-06-18 23:58:37 +00001307 stateEqual->getSVal(newValueExpr), OSAtomicStoreTag);
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001308
1309 // Now bind the result of the comparison.
1310 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1311 E2 = TmpStore.end(); I2 != E2; ++I2) {
1312 ExplodedNode<GRState> *predNew = *I2;
1313 const GRState *stateNew = predNew->getState();
1314 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001315 Engine.MakeNode(Dst, CE, predNew, stateNew->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001316 }
1317 }
1318
1319 // Were they not equal?
Ted Kremenek70970bf2009-06-18 22:57:13 +00001320 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001321 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001322 Engine.MakeNode(Dst, CE, N, stateNotEqual->bindExpr(CE, Res));
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001323 }
1324 }
1325
1326 return true;
1327}
1328
1329static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1330 GRExprEngine& Engine,
1331 GRStmtNodeBuilder<GRState>& Builder,
1332 CallExpr* CE, SVal L,
1333 ExplodedNode<GRState>* Pred) {
Zhongxing Xucac107a2009-04-20 05:24:46 +00001334 const FunctionDecl* FD = L.getAsFunctionDecl();
1335 if (!FD)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001336 return false;
Zhongxing Xucac107a2009-04-20 05:24:46 +00001337
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001338 const char *FName = FD->getNameAsCString();
1339
1340 // Check for compare and swap.
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001341 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1342 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001343 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenek9b45aa82009-04-11 00:54:13 +00001344
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001345 // FIXME: Other atomics.
1346 return false;
1347}
1348
1349//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001350// Transfer function: Function calls.
1351//===----------------------------------------------------------------------===//
Ted Kremenek8765ebc2009-04-11 00:11:10 +00001352
1353void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1354 assert (Builder && "GRStmtNodeBuilder must be defined.");
1355
1356 // FIXME: Allow us to chain together transfer functions.
1357 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1358 return;
1359
1360 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1361}
1362
Ted Kremenekd9268e32008-02-19 01:44:53 +00001363void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001364 CallExpr::arg_iterator AI,
1365 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001366 NodeSet& Dst)
1367{
1368 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001369 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001370 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1371 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001372 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001373
1374 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1375}
1376
1377void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1378 CallExpr::arg_iterator AI,
1379 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001380 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001381 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001382
Ted Kremenek07baa252008-02-21 18:02:17 +00001383 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001384 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001385 // If the call argument is being bound to a reference parameter,
1386 // visit it as an lvalue, not an rvalue.
1387 bool VisitAsLvalue = false;
1388 if (Proto && ParamIdx < Proto->getNumArgs())
1389 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1390
1391 NodeSet DstTmp;
1392 if (VisitAsLvalue)
1393 VisitLValue(*AI, Pred, DstTmp);
1394 else
1395 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001396 ++AI;
1397
Ted Kremenek769f3482008-03-04 22:01:56 +00001398 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001399 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001400
1401 return;
1402 }
1403
1404 // If we reach here we have processed all of the arguments. Evaluate
1405 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001406
Ted Kremenekc71901d2008-02-25 21:16:03 +00001407 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001408 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001409
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001410 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001411
Ted Kremenekd9268e32008-02-19 01:44:53 +00001412 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001413 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1414
Ted Kremeneke66ba682009-02-13 01:45:31 +00001415 const GRState* state = GetState(*DI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001416 SVal L = state->getSVal(Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001417
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001418 // FIXME: Add support for symbolic function calls (calls involving
1419 // function pointer values that are symbolic).
1420
1421 // Check for undefined control-flow or calls to NULL.
1422
Zhongxing Xu097fc982008-10-17 05:57:07 +00001423 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001424 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001425
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001426 if (N) {
1427 N->markAsSink();
1428 BadCalls.insert(N);
1429 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001430
Ted Kremenekd9268e32008-02-19 01:44:53 +00001431 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001432 }
1433
1434 // Check for the "noreturn" attribute.
1435
1436 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xucac107a2009-04-20 05:24:46 +00001437 const FunctionDecl* FD = L.getAsFunctionDecl();
1438 if (FD) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +00001439 if (FD->getAttr<NoReturnAttr>(getContext()) ||
1440 FD->getAttr<AnalyzerNoReturnAttr>(getContext()))
Ted Kremenekb451dd32008-03-05 21:15:02 +00001441 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001442 else {
1443 // HACK: Some functions are not marked noreturn, and don't return.
1444 // Here are a few hardwired ones. If this takes too long, we can
1445 // potentially cache these results.
1446 const char* s = FD->getIdentifier()->getName();
1447 unsigned n = strlen(s);
1448
1449 switch (n) {
1450 default:
1451 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001452
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001453 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001454 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1455 break;
1456
1457 case 5:
1458 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001459 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001460 if (CE->getNumArgs() > 0) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001461 SVal X = state->getSVal(*CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001462 // FIXME: use Assume to inspect the possible symbolic value of
1463 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001464 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001465 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001466 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001467 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001468 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001469 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001470
Ted Kremenek23271be2008-04-22 05:37:33 +00001471 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001472 if (!memcmp(s, "Assert", 6)) {
1473 Builder->BuildSinks = true;
1474 break;
1475 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001476
1477 // FIXME: This is just a wrapper around throwing an exception.
1478 // Eventually inter-procedural analysis should handle this easily.
1479 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1480
Ted Kremenek23271be2008-04-22 05:37:33 +00001481 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001482
1483 case 7:
1484 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1485 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001486
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001487 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001488 if (!memcmp(s ,"db_error", 8) ||
1489 !memcmp(s, "__assert", 8))
1490 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001491 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001492
1493 case 12:
1494 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1495 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001496
Ted Kremenek19903a22008-09-19 02:30:47 +00001497 case 13:
1498 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1499 break;
1500
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001501 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001502 if (!memcmp(s, "dtrace_assfail", 14) ||
1503 !memcmp(s, "yy_fatal_error", 14))
1504 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001505 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001506
1507 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001508 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001509 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1510 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001511 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001512
Ted Kremeneka46fea72008-05-17 00:33:23 +00001513 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001514 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001515
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001516 }
1517 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001518
1519 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001520
Zhongxing Xucac107a2009-04-20 05:24:46 +00001521 if (FD) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001522
Zhongxing Xucac107a2009-04-20 05:24:46 +00001523 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001524 switch (id) {
1525 case Builtin::BI__builtin_expect: {
1526 // For __builtin_expect, just return the value of the subexpression.
1527 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001528 SVal X = state->getSVal(*(CE->arg_begin()));
1529 MakeNode(Dst, CE, *DI, state->bindExpr(CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001530 continue;
1531 }
1532
Ted Kremenek19891fa2008-11-02 00:35:01 +00001533 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001534 // FIXME: Refactor into StoreManager itself?
1535 MemRegionManager& RM = getStateManager().getRegionManager();
1536 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001537 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001538
1539 // Set the extent of the region in bytes. This enables us to use the
1540 // SVal of the argument directly. If we save the extent in bits, we
1541 // cannot represent values like symbol*8.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001542 SVal Extent = state->getSVal(*(CE->arg_begin()));
Ted Kremeneke66ba682009-02-13 01:45:31 +00001543 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001544
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001545 MakeNode(Dst, CE, *DI, state->bindExpr(CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001546 continue;
1547 }
1548
Ted Kremenek21581c62008-03-05 22:59:42 +00001549 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001550 break;
1551 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001552 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001553
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001554 // Check any arguments passed-by-value against being undefined.
1555
1556 bool badArg = false;
1557
1558 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1559 I != E; ++I) {
1560
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001561 if (GetState(*DI)->getSVal(*I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001562 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001563
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001564 if (N) {
1565 N->markAsSink();
1566 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001567 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001568
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001569 badArg = true;
1570 break;
1571 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001572 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001573
1574 if (badArg)
1575 continue;
1576
1577 // Dispatch to the plug-in transfer function.
1578
1579 unsigned size = Dst.size();
1580 SaveOr OldHasGen(Builder->HasGeneratedNode);
1581 EvalCall(Dst, CE, L, *DI);
1582
1583 // Handle the case where no nodes where generated. Auto-generate that
1584 // contains the updated state if we aren't generating sinks.
1585
1586 if (!Builder->BuildSinks && Dst.size() == size &&
1587 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001588 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001589 }
1590}
1591
Ted Kremenekca5f6202008-04-15 23:06:53 +00001592//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001593// Transfer function: Objective-C ivar references.
1594//===----------------------------------------------------------------------===//
1595
Ted Kremenek9a48d862009-02-28 20:50:43 +00001596static std::pair<const void*,const void*> EagerlyAssumeTag
1597 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1598
Ted Kremenek34a611b2009-02-25 23:32:10 +00001599void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001600 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1601 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001602
1603 // Test if the previous node was as the same expression. This can happen
1604 // when the expression fails to evaluate to anything meaningful and
1605 // (as an optimization) we don't generate a node.
1606 ProgramPoint P = Pred->getLocation();
1607 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1608 Dst.Add(Pred);
1609 continue;
1610 }
1611
Ted Kremenek8f520972009-02-25 22:32:02 +00001612 const GRState* state = Pred->getState();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001613 SVal V = state->getSVal(Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001614 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001615 // First assume that the condition is true.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001616 if (const GRState *stateTrue = state->assume(V, true)) {
1617 stateTrue = stateTrue->bindExpr(Ex, MakeConstantVal(1U, Ex));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001618 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001619 stateTrue, Pred));
1620 }
1621
1622 // Next, assume that the condition is false.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001623 if (const GRState *stateFalse = state->assume(V, false)) {
1624 stateFalse = stateFalse->bindExpr(Ex, MakeConstantVal(0U, Ex));
Ted Kremenek34a611b2009-02-25 23:32:10 +00001625 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001626 stateFalse, Pred));
1627 }
1628 }
1629 else
1630 Dst.Add(Pred);
1631 }
1632}
1633
1634//===----------------------------------------------------------------------===//
1635// Transfer function: Objective-C ivar references.
1636//===----------------------------------------------------------------------===//
1637
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001638void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1639 NodeTy* Pred, NodeSet& Dst,
1640 bool asLValue) {
1641
1642 Expr* Base = cast<Expr>(Ex->getBase());
1643 NodeSet Tmp;
1644 Visit(Base, Pred, Tmp);
1645
1646 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001647 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001648 SVal BaseVal = state->getSVal(Base);
1649 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001650
1651 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001652 MakeNode(Dst, Ex, *I, state->bindExpr(Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001653 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001654 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001655 }
1656}
1657
1658//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001659// Transfer function: Objective-C fast enumeration 'for' statements.
1660//===----------------------------------------------------------------------===//
1661
1662void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1663 NodeTy* Pred, NodeSet& Dst) {
1664
1665 // ObjCForCollectionStmts are processed in two places. This method
1666 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1667 // statements within a basic block. This transfer function does two things:
1668 //
1669 // (1) binds the next container value to 'element'. This creates a new
1670 // node in the ExplodedGraph.
1671 //
1672 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1673 // whether or not the container has any more elements. This value
1674 // will be tested in ProcessBranch. We need to explicitly bind
1675 // this value because a container can contain nil elements.
1676 //
1677 // FIXME: Eventually this logic should actually do dispatches to
1678 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1679 // This will require simulating a temporary NSFastEnumerationState, either
1680 // through an SVal or through the use of MemRegions. This value can
1681 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1682 // terminates we reclaim the temporary (it goes out of scope) and we
1683 // we can test if the SVal is 0 or if the MemRegion is null (depending
1684 // on what approach we take).
1685 //
1686 // For now: simulate (1) by assigning either a symbol or nil if the
1687 // container is empty. Thus this transfer function will by default
1688 // result in state splitting.
1689
Ted Kremenek034a9472008-11-14 19:47:18 +00001690 Stmt* elem = S->getElement();
1691 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001692
1693 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001694 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001695 assert (ElemD->getInit() == 0);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001696 ElementV = GetState(Pred)->getLValue(ElemD);
Ted Kremenek034a9472008-11-14 19:47:18 +00001697 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1698 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001699 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001700
1701 NodeSet Tmp;
1702 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001703
Ted Kremenek034a9472008-11-14 19:47:18 +00001704 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1705 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001706 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek034a9472008-11-14 19:47:18 +00001707 }
1708}
1709
1710void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1711 NodeTy* Pred, NodeSet& Dst,
1712 SVal ElementV) {
1713
1714
Ted Kremenek13e167f2008-11-12 19:24:17 +00001715
Ted Kremenek034a9472008-11-14 19:47:18 +00001716 // Get the current state. Use 'EvalLocation' to determine if it is a null
1717 // pointer, etc.
1718 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001719
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001720 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1721 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001722 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001723
Ted Kremenek18a636d2009-06-18 01:23:53 +00001724 const GRState *state = GetState(Pred);
Ted Kremenek034a9472008-11-14 19:47:18 +00001725
Ted Kremenek13e167f2008-11-12 19:24:17 +00001726 // Handle the case where the container still has elements.
Ted Kremenek034a9472008-11-14 19:47:18 +00001727 QualType IntTy = getContext().IntTy;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001728 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001729 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001730
Ted Kremenek13e167f2008-11-12 19:24:17 +00001731 // Handle the case where the container has no elements.
Ted Kremenekd3789d72008-11-12 21:12:46 +00001732 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
Ted Kremenek18a636d2009-06-18 01:23:53 +00001733 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001734
1735 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1736 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1737 // FIXME: The proper thing to do is to really iterate over the
1738 // container. We will do this with dispatch logic to the store.
1739 // For now, just 'conjure' up a symbolic value.
Zhongxing Xu20362702009-05-09 03:57:34 +00001740 QualType T = R->getValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001741 assert (Loc::IsLocType(T));
1742 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu0ed9d0c2009-04-09 06:49:52 +00001743 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1744 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
Ted Kremenek18a636d2009-06-18 01:23:53 +00001745 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001746
Ted Kremenek034a9472008-11-14 19:47:18 +00001747 // Bind the location to 'nil' on the false branch.
1748 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
Ted Kremenek18a636d2009-06-18 01:23:53 +00001749 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001750 }
1751
Ted Kremenekd3789d72008-11-12 21:12:46 +00001752 // Create the new nodes.
1753 MakeNode(Dst, S, Pred, hasElems);
1754 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001755}
1756
1757//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001758// Transfer function: Objective-C message expressions.
1759//===----------------------------------------------------------------------===//
1760
1761void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1762 NodeSet& Dst){
1763
1764 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1765 Pred, Dst);
1766}
1767
1768void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001769 ObjCMessageExpr::arg_iterator AI,
1770 ObjCMessageExpr::arg_iterator AE,
1771 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001772 if (AI == AE) {
1773
1774 // Process the receiver.
1775
1776 if (Expr* Receiver = ME->getReceiver()) {
1777 NodeSet Tmp;
1778 Visit(Receiver, Pred, Tmp);
1779
1780 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1781 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1782
1783 return;
1784 }
1785
1786 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1787 return;
1788 }
1789
1790 NodeSet Tmp;
1791 Visit(*AI, Pred, Tmp);
1792
1793 ++AI;
1794
1795 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1796 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1797}
1798
1799void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1800 NodeTy* Pred,
1801 NodeSet& Dst) {
1802
1803 // FIXME: More logic for the processing the method call.
1804
Ted Kremeneke66ba682009-02-13 01:45:31 +00001805 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001806 bool RaisesException = false;
1807
Ted Kremenekca5f6202008-04-15 23:06:53 +00001808
1809 if (Expr* Receiver = ME->getReceiver()) {
1810
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001811 SVal L = state->getSVal(Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001812
Ted Kremenek95a98252009-02-19 04:06:22 +00001813 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001814 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001815 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001816
1817 if (N) {
1818 N->markAsSink();
1819 UndefReceivers.insert(N);
1820 }
1821
1822 return;
1823 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001824
Ted Kremenek95a98252009-02-19 04:06:22 +00001825 // "Assume" that the receiver is not NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001826 const GRState *StNotNull = state->assume(L, true);
Ted Kremenek95a98252009-02-19 04:06:22 +00001827
1828 // "Assume" that the receiver is NULL.
Ted Kremenek70970bf2009-06-18 22:57:13 +00001829 const GRState *StNull = state->assume(L, false);
Ted Kremenek95a98252009-02-19 04:06:22 +00001830
Ted Kremenek70970bf2009-06-18 22:57:13 +00001831 if (StNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001832 QualType RetTy = ME->getType();
1833
Ted Kremenek95a98252009-02-19 04:06:22 +00001834 // Check if the receiver was nil and the return value a struct.
Ted Kremenekb3323002009-04-09 05:45:56 +00001835 if(RetTy->isRecordType()) {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001836 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001837 // The [0 ...] expressions will return garbage. Flag either an
1838 // explicit or implicit error. Because of the structure of this
1839 // function we currently do not bifurfacte the state graph at
1840 // this point.
1841 // FIXME: We should bifurcate and fill the returned struct with
1842 // garbage.
1843 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1844 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001845 if (StNotNull)
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001846 NilReceiverStructRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001847 else
Ted Kremenek23712182009-04-09 04:06:51 +00001848 NilReceiverStructRetExplicit.insert(N);
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001849 }
1850 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001851 }
Ted Kremenekb3323002009-04-09 05:45:56 +00001852 else {
Ted Kremenek5ab77002009-04-09 00:00:02 +00001853 ASTContext& Ctx = getContext();
Ted Kremenekb3323002009-04-09 05:45:56 +00001854 if (RetTy != Ctx.VoidTy) {
1855 if (BR.getParentMap().isConsumedExpr(ME)) {
1856 // sizeof(void *)
1857 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1858 // sizeof(return type)
1859 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremenek5ab77002009-04-09 00:00:02 +00001860
Ted Kremenekb3323002009-04-09 05:45:56 +00001861 if(voidPtrSize < returnTypeSize) {
1862 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1863 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00001864 if(StNotNull)
Ted Kremenekb3323002009-04-09 05:45:56 +00001865 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001866 else
Ted Kremenekb3323002009-04-09 05:45:56 +00001867 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekb3323002009-04-09 05:45:56 +00001868 }
1869 }
Ted Kremenek70970bf2009-06-18 22:57:13 +00001870 else if (!StNotNull) {
Ted Kremenekb3323002009-04-09 05:45:56 +00001871 // Handle the safe cases where the return value is 0 if the
1872 // receiver is nil.
1873 //
1874 // FIXME: For now take the conservative approach that we only
1875 // return null values if we *know* that the receiver is nil.
1876 // This is because we can have surprises like:
1877 //
1878 // ... = [[NSScreens screens] objectAtIndex:0];
1879 //
1880 // What can happen is that [... screens] could return nil, but
1881 // it most likely isn't nil. We should assume the semantics
1882 // of this case unless we have *a lot* more knowledge.
1883 //
Ted Kremenekcda58d22009-04-09 16:46:55 +00001884 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001885 MakeNode(Dst, ME, Pred, StNull->bindExpr(ME, V));
Ted Kremenek23712182009-04-09 04:06:51 +00001886 return;
1887 }
Ted Kremeneke7c6d4f2009-04-08 03:07:17 +00001888 }
Ted Kremenek5ab77002009-04-09 00:00:02 +00001889 }
Ted Kremenek95a98252009-02-19 04:06:22 +00001890 }
Ted Kremenekf2895872009-04-08 18:51:08 +00001891 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenek8993b7d2009-04-09 06:02:06 +00001892 // of this method should assume that the receiver is not nil.
1893 if (!StNotNull)
1894 return;
1895
Ted Kremenekf2895872009-04-08 18:51:08 +00001896 state = StNotNull;
Ted Kremenek95a98252009-02-19 04:06:22 +00001897 }
1898
Ted Kremenek5f20a632008-05-01 18:33:28 +00001899 // Check if the "raise" message was sent.
1900 if (ME->getSelector() == RaiseSel)
1901 RaisesException = true;
1902 }
1903 else {
1904
1905 IdentifierInfo* ClsName = ME->getClassName();
1906 Selector S = ME->getSelector();
1907
1908 // Check for special instance methods.
1909
1910 if (!NSExceptionII) {
1911 ASTContext& Ctx = getContext();
1912
1913 NSExceptionII = &Ctx.Idents.get("NSException");
1914 }
1915
1916 if (ClsName == NSExceptionII) {
1917
1918 enum { NUM_RAISE_SELECTORS = 2 };
1919
1920 // Lazily create a cache of the selectors.
1921
1922 if (!NSExceptionInstanceRaiseSelectors) {
1923
1924 ASTContext& Ctx = getContext();
1925
1926 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1927
1928 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1929 unsigned idx = 0;
1930
1931 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001932 II.push_back(&Ctx.Idents.get("raise"));
1933 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001934 NSExceptionInstanceRaiseSelectors[idx++] =
1935 Ctx.Selectors.getSelector(II.size(), &II[0]);
1936
1937 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001938 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001939 NSExceptionInstanceRaiseSelectors[idx++] =
1940 Ctx.Selectors.getSelector(II.size(), &II[0]);
1941 }
1942
1943 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1944 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1945 RaisesException = true; break;
1946 }
1947 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001948 }
1949
1950 // Check for any arguments that are uninitialized/undefined.
1951
1952 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1953 I != E; ++I) {
1954
Ted Kremenek4ea6a182009-06-19 17:10:32 +00001955 if (state->getSVal(*I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001956
1957 // Generate an error node for passing an uninitialized/undefined value
1958 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001959 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001960
1961 if (N) {
1962 N->markAsSink();
1963 MsgExprUndefArgs[N] = *I;
1964 }
1965
1966 return;
1967 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001968 }
1969
1970 // Check if we raise an exception. For now treat these as sinks. Eventually
1971 // we will want to handle exceptions properly.
1972
1973 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1974
1975 if (RaisesException)
1976 Builder->BuildSinks = true;
1977
Ted Kremenekca5f6202008-04-15 23:06:53 +00001978 // Dispatch to plug-in transfer function.
1979
1980 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001981 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001982
Ted Kremenekca5f6202008-04-15 23:06:53 +00001983 EvalObjCMessageExpr(Dst, ME, Pred);
1984
1985 // Handle the case where no nodes where generated. Auto-generate that
1986 // contains the updated state if we aren't generating sinks.
1987
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001988 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001989 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001990}
1991
1992//===----------------------------------------------------------------------===//
1993// Transfer functions: Miscellaneous statements.
1994//===----------------------------------------------------------------------===//
1995
Ted Kremenek16354a42009-01-13 01:04:21 +00001996void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1997 QualType PtrTy,
1998 Expr* CastE, NodeTy* Pred,
1999 NodeSet& Dst) {
2000 if (!V.isUnknownOrUndef()) {
2001 // FIXME: Determine if the number of bits of the target type is
2002 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00002003 // If not, flag an error.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002004 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, EvalCast(cast<Loc>(V),
Ted Kremenek52978eb2009-03-05 03:44:53 +00002005 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00002006 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002007 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002008 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002009}
2010
2011
Ted Kremenek07baa252008-02-21 18:02:17 +00002012void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00002013 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00002014 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00002015 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00002016
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00002017 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00002018 T = ExCast->getTypeAsWritten();
2019
Zhongxing Xu943909c2008-10-22 08:02:16 +00002020 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002021 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00002022 else
2023 Visit(Ex, Pred, S1);
2024
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002025 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00002026 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002027 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00002028 Dst.Add(*I1);
2029
Ted Kremenek54eddae2008-01-24 02:02:54 +00002030 return;
2031 }
2032
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002033 // FIXME: The rest of this should probably just go into EvalCall, and
2034 // let the transfer function object be responsible for constructing
2035 // nodes.
2036
Ted Kremenek07baa252008-02-21 18:02:17 +00002037 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00002038 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002039 const GRState* state = GetState(N);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002040 SVal V = state->getSVal(Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002041 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002042
2043 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002044 if (V.isUnknown()) {
2045 Dst.Add(N);
2046 continue;
2047 }
2048
2049 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002050 if (V.isUndef())
2051 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00002052
2053 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00002054 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002055 C.getCanonicalType(ExTy).getUnqualifiedType())
2056 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002057
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002058 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002059 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002060 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002061 continue;
2062 }
2063
2064 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002065 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002066 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002067 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00002068 V = LV->getLoc();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002069 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002070 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00002071 }
Ted Kremenek3f755632009-03-05 03:42:31 +00002072
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002073 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002074 }
2075
2076 // Just pass through function and block pointers.
2077 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2078 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002079 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00002080 }
2081
Ted Kremenek16354a42009-01-13 01:04:21 +00002082 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002083 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00002084 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00002085 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00002086
2087 // Are we casting from an array to a pointer? If so just pass on
2088 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002089 if (T->isPointerType())
2090 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00002091
2092 // Are we casting from an array to an integer? If so, cast the decayed
2093 // pointer value to an integer.
2094 assert(T->isIntegerType());
2095 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2096 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002097 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00002098 continue;
2099 }
2100
Ted Kremenekf5da3252008-12-13 21:49:13 +00002101 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00002102 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2103 // FIXME: For TypedViewRegions, we should handle the case where the
2104 // underlying symbolic pointer is a function pointer or
2105 // block pointer.
2106
2107 // FIXME: We should handle the case where we strip off view layers to get
2108 // to a desugared type.
2109
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002110 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00002111 // We get a symbolic function pointer for a dereference of a function
2112 // pointer, but it is of function type. Example:
2113
2114 // struct FPRec {
2115 // void (*my_func)(int * x);
2116 // };
2117 //
2118 // int bar(int x);
2119 //
2120 // int f1_a(struct FPRec* foo) {
2121 // int x;
2122 // (*foo->my_func)(&x);
2123 // return bar(x)+1; // no-warning
2124 // }
2125
2126 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002127
Ted Kremenekf5da3252008-12-13 21:49:13 +00002128 const MemRegion* R = RV->getRegion();
2129 StoreManager& StoreMgr = getStoreManager();
2130
2131 // Delegate to store manager to get the result of casting a region
2132 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002133 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00002134
2135 // Inspect the result. If the MemRegion* returned is NULL, this
2136 // expression evaluates to UnknownVal.
2137 R = Res.getRegion();
2138 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2139
2140 // Generate the new node in the ExplodedGraph.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002141 MakeNode(Dst, CastE, N, Res.getState()->bindExpr(CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00002142 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00002143 }
Zhongxing Xu18bcec02009-04-10 06:06:13 +00002144 // All other cases.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002145 DispatchCast: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002146 MakeNode(Dst, CastE, N, state->bindExpr(CastE,
2147 EvalCast(V, CastE->getType())));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002148 continue;
2149 }
2150
2151 PassThrough: {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002152 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00002153 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002154 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002155}
2156
Ted Kremenekd83daa52008-10-27 21:54:31 +00002157void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002158 NodeTy* Pred, NodeSet& Dst,
2159 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002160 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2161 NodeSet Tmp;
2162 Visit(ILE, Pred, Tmp);
2163
2164 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002165 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002166 SVal ILV = state->getSVal(ILE);
2167 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002168
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002169 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002170 MakeNode(Dst, CL, *I, state->bindExpr(CL, state->getLValue(CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002171 else
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002172 MakeNode(Dst, CL, *I, state->bindExpr(CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002173 }
2174}
2175
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002176void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002177
Ted Kremenek811af062008-10-06 18:43:53 +00002178 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002179 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002180
2181 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002182 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002183
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002184 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002185 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002186
2187 // FIXME: static variables may have an initializer, but the second
2188 // time a function is called those values may not be current.
2189 NodeSet Tmp;
2190
Ted Kremenek13e167f2008-11-12 19:24:17 +00002191 if (InitEx)
2192 Visit(InitEx, Pred, Tmp);
Ted Kremenek448ab622008-08-28 18:34:26 +00002193
2194 if (Tmp.empty())
2195 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002196
2197 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002198 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002199 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002200
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002201 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2202 QualType T = getContext().getCanonicalType(VD->getType());
2203 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2204 // FIXME: Handle multi-dimensional VLAs.
2205
2206 Expr* SE = VLA->getSizeExpr();
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002207 SVal Size = state->getSVal(SE);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002208
2209 if (Size.isUndef()) {
2210 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2211 N->markAsSink();
2212 ExplicitBadSizedVLA.insert(N);
2213 }
2214 continue;
2215 }
2216
Ted Kremenek70970bf2009-06-18 22:57:13 +00002217 const GRState* zeroState = state->assume(Size, false);
2218 state = state->assume(Size, true);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002219
Ted Kremenek70970bf2009-06-18 22:57:13 +00002220 if (zeroState) {
2221 if (NodeTy* N = Builder->generateNode(DS, zeroState, Pred)) {
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002222 N->markAsSink();
Ted Kremenek70970bf2009-06-18 22:57:13 +00002223 if (state)
2224 ImplicitBadSizedVLA.insert(N);
2225 else
2226 ExplicitBadSizedVLA.insert(N);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002227 }
2228 }
2229
Ted Kremenek70970bf2009-06-18 22:57:13 +00002230 if (!state)
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002231 continue;
2232 }
2233
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002234 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002235 if (InitEx) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002236 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002237 QualType T = VD->getType();
2238
2239 // Recover some path-sensitivity if a scalar value evaluated to
2240 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002241 if (InitVal.isUnknown() ||
2242 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002243 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002244 }
2245
Ted Kremeneke66ba682009-02-13 01:45:31 +00002246 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002247
2248 // The next thing to do is check if the GRTransferFuncs object wants to
2249 // update the state based on the new binding. If the GRTransferFunc
2250 // object doesn't do anything, just auto-propagate the current state.
2251 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2252 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2253 InitVal);
2254 }
2255 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002256 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002257 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002258 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002259 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002260}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002261
Ted Kremeneke56ece22008-10-30 17:47:32 +00002262namespace {
2263 // This class is used by VisitInitListExpr as an item in a worklist
2264 // for processing the values contained in an InitListExpr.
2265class VISIBILITY_HIDDEN InitListWLItem {
2266public:
2267 llvm::ImmutableList<SVal> Vals;
2268 GRExprEngine::NodeTy* N;
2269 InitListExpr::reverse_iterator Itr;
2270
2271 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2272 InitListExpr::reverse_iterator itr)
2273 : Vals(vals), N(n), Itr(itr) {}
2274};
2275}
2276
2277
Zhongxing Xuebcad732008-10-30 05:02:23 +00002278void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2279 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002280
Zhongxing Xuebcad732008-10-30 05:02:23 +00002281 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002282 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002283 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002284
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002285 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002286
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002287 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002288
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002289 // Handle base case where the initializer has no elements.
2290 // e.g: static int* myArray[] = {};
2291 if (NumInitElements == 0) {
2292 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002293 MakeNode(Dst, E, Pred, state->bindExpr(E, V));
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002294 return;
2295 }
2296
2297 // Create a worklist to process the initializers.
2298 llvm::SmallVector<InitListWLItem, 10> WorkList;
2299 WorkList.reserve(NumInitElements);
2300 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002301 InitListExpr::reverse_iterator ItrEnd = E->rend();
2302
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002303 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002304 while (!WorkList.empty()) {
2305 InitListWLItem X = WorkList.back();
2306 WorkList.pop_back();
2307
Zhongxing Xuebcad732008-10-30 05:02:23 +00002308 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002309 Visit(*X.Itr, X.N, Tmp);
2310
2311 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002312
Ted Kremeneke56ece22008-10-30 17:47:32 +00002313 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2314 // Get the last initializer value.
2315 state = GetState(*NI);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002316 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002317
2318 // Construct the new list of values by prepending the new value to
2319 // the already constructed list.
2320 llvm::ImmutableList<SVal> NewVals =
2321 getBasicVals().consVals(InitV, X.Vals);
2322
2323 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002324 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002325 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuebcad732008-10-30 05:02:23 +00002326
Ted Kremeneke56ece22008-10-30 17:47:32 +00002327 // Make final state and node.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002328 MakeNode(Dst, E, *NI, state->bindExpr(E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002329 }
2330 else {
2331 // Still some initializer values to go. Push them onto the worklist.
2332 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2333 }
2334 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002335 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002336
2337 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002338 }
2339
Ted Kremenek79413a52008-11-13 06:10:40 +00002340 if (T->isUnionType() || T->isVectorType()) {
2341 // FIXME: to be implemented.
2342 // Note: That vectors can return true for T->isIntegerType()
2343 MakeNode(Dst, E, Pred, state);
2344 return;
2345 }
2346
Zhongxing Xuebcad732008-10-30 05:02:23 +00002347 if (Loc::IsLocType(T) || T->isIntegerType()) {
2348 assert (E->getNumInits() == 1);
2349 NodeSet Tmp;
2350 Expr* Init = E->getInit(0);
2351 Visit(Init, Pred, Tmp);
2352 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2353 state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002354 MakeNode(Dst, E, *I, state->bindExpr(E, state->getSVal(Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002355 }
2356 return;
2357 }
2358
Zhongxing Xuebcad732008-10-30 05:02:23 +00002359
2360 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2361 assert(0 && "unprocessed InitListExpr type");
2362}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002363
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002364/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2365void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2366 NodeTy* Pred,
2367 NodeSet& Dst) {
2368 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002369 uint64_t amt;
2370
2371 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002372 if (T == getContext().VoidTy) {
2373 // sizeof(void) == 1 byte.
2374 amt = 1;
2375 }
2376 else if (!T.getTypePtr()->isConstantSizeType()) {
2377 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002378 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002379 }
2380 else if (T->isObjCInterfaceType()) {
2381 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2382 // the compiler has laid out its representation. Just report Unknown
2383 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002384 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002385 }
2386 else {
2387 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002388 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002389 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002390 }
2391 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002392 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002393
Ted Kremenekf10f2882008-03-21 21:30:14 +00002394 MakeNode(Dst, Ex, Pred,
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002395 GetState(Pred)->bindExpr(Ex, NonLoc::MakeVal(getBasicVals(), amt,
2396 Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002397}
2398
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002399
Ted Kremenek07baa252008-02-21 18:02:17 +00002400void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002401 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002402
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002403 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002404
2405 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002406 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002407
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002408 case UnaryOperator::Deref: {
2409
2410 Expr* Ex = U->getSubExpr()->IgnoreParens();
2411 NodeSet Tmp;
2412 Visit(Ex, Pred, Tmp);
2413
2414 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002415
Ted Kremeneke66ba682009-02-13 01:45:31 +00002416 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002417 SVal location = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002418
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002419 if (asLValue)
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002420 MakeNode(Dst, U, *I, state->bindExpr(U, location),
Ted Kremenek0441f112009-05-07 18:27:16 +00002421 ProgramPoint::PostLValueKind);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002422 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002423 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002424 }
2425
2426 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002427 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002428
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002429 case UnaryOperator::Real: {
2430
2431 Expr* Ex = U->getSubExpr()->IgnoreParens();
2432 NodeSet Tmp;
2433 Visit(Ex, Pred, Tmp);
2434
2435 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2436
Zhongxing Xu097fc982008-10-17 05:57:07 +00002437 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002438 if (Ex->getType()->isAnyComplexType()) {
2439 // Just report "Unknown."
2440 Dst.Add(*I);
2441 continue;
2442 }
2443
2444 // For all other types, UnaryOperator::Real is an identity operation.
2445 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002446 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002447 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002448 }
2449
2450 return;
2451 }
2452
2453 case UnaryOperator::Imag: {
2454
2455 Expr* Ex = U->getSubExpr()->IgnoreParens();
2456 NodeSet Tmp;
2457 Visit(Ex, Pred, Tmp);
2458
2459 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002460 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002461 if (Ex->getType()->isAnyComplexType()) {
2462 // Just report "Unknown."
2463 Dst.Add(*I);
2464 continue;
2465 }
2466
2467 // For all other types, UnaryOperator::Float returns 0.
2468 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002469 const GRState* state = GetState(*I);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002470 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002471 MakeNode(Dst, U, *I, state->bindExpr(U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002472 }
2473
2474 return;
2475 }
2476
2477 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002478 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002479 Dst.Add(Pred);
2480 return;
2481
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002482 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002483 case UnaryOperator::Extension: {
2484
2485 // Unary "+" is a no-op, similar to a parentheses. We still have places
2486 // where it may be a block-level expression, so we need to
2487 // generate an extra node that just propagates the value of the
2488 // subexpression.
2489
2490 Expr* Ex = U->getSubExpr()->IgnoreParens();
2491 NodeSet Tmp;
2492 Visit(Ex, Pred, Tmp);
2493
2494 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002495 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002496 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002497 }
2498
2499 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002500 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002501
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002502 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002503
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002504 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002505 Expr* Ex = U->getSubExpr()->IgnoreParens();
2506 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002507 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002508
2509 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002510 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002511 SVal V = state->getSVal(Ex);
2512 state = state->bindExpr(U, V);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002513 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002514 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002515
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002516 return;
2517 }
2518
2519 case UnaryOperator::LNot:
2520 case UnaryOperator::Minus:
2521 case UnaryOperator::Not: {
2522
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002523 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002524 Expr* Ex = U->getSubExpr()->IgnoreParens();
2525 NodeSet Tmp;
2526 Visit(Ex, Pred, Tmp);
2527
2528 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002529 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002530
2531 // Get the value of the subexpression.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002532 SVal V = state->getSVal(Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002533
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002534 if (V.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002535 MakeNode(Dst, U, *I, state->bindExpr(U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002536 continue;
2537 }
2538
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002539// QualType DstT = getContext().getCanonicalType(U->getType());
2540// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2541//
2542// if (DstT != SrcT) // Perform promotions.
2543// V = EvalCast(V, DstT);
2544//
2545// if (V.isUnknownOrUndef()) {
2546// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2547// continue;
2548// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002549
2550 switch (U->getOpcode()) {
2551 default:
2552 assert(false && "Invalid Opcode.");
2553 break;
2554
2555 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002556 // FIXME: Do we need to handle promotions?
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002557 state = state->bindExpr(U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002558 break;
2559
2560 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002561 // FIXME: Do we need to handle promotions?
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002562 state = state->bindExpr(U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002563 break;
2564
2565 case UnaryOperator::LNot:
2566
2567 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2568 //
2569 // Note: technically we do "E == 0", but this is the same in the
2570 // transfer functions as "0 == E".
2571
Zhongxing Xu097fc982008-10-17 05:57:07 +00002572 if (isa<Loc>(V)) {
Ted Kremenekf2895872009-04-08 18:51:08 +00002573 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xuc890e332009-05-20 09:00:16 +00002574 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremenek74556a12009-03-26 03:35:11 +00002575 U->getType());
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002576 state = state->bindExpr(U, Result);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002577 }
2578 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002579 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002580#if 0
Zhongxing Xu097fc982008-10-17 05:57:07 +00002581 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002582 state = SetSVal(state, U, Result);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002583#else
Ted Kremenek74556a12009-03-26 03:35:11 +00002584 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2585 U->getType());
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002586 continue;
2587#endif
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002588 }
2589
2590 break;
2591 }
2592
Ted Kremeneke66ba682009-02-13 01:45:31 +00002593 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002594 }
2595
2596 return;
2597 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002598 }
2599
2600 // Handle ++ and -- (both pre- and post-increment).
2601
2602 assert (U->isIncrementDecrementOp());
2603 NodeSet Tmp;
2604 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002605 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002606
2607 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2608
Ted Kremeneke66ba682009-02-13 01:45:31 +00002609 const GRState* state = GetState(*I);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002610 SVal V1 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002611
2612 // Perform a load.
2613 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002614 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002615
2616 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2617
Ted Kremeneke66ba682009-02-13 01:45:31 +00002618 state = GetState(*I2);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002619 SVal V2 = state->getSVal(Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002620
2621 // Propagate unknown and undefined values.
2622 if (V2.isUnknownOrUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002623 MakeNode(Dst, U, *I2, state->bindExpr(U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002624 continue;
2625 }
2626
Ted Kremeneke43de222009-03-11 03:54:24 +00002627 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002628 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2629 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002630
Zhongxing Xuc890e332009-05-20 09:00:16 +00002631 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2632 U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002633
2634 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek65411632009-04-21 22:38:05 +00002635 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002636 Result = ValMgr.getConjuredSymbolVal(Ex,
2637 Builder->getCurrentBlockCount());
Ted Kremenek65411632009-04-21 22:38:05 +00002638
2639 // If the value is a location, ++/-- should always preserve
2640 // non-nullness. Check if the original value was non-null, and if so propagate
2641 // that constraint.
2642 if (Loc::IsLocType(U->getType())) {
Zhongxing Xuc890e332009-05-20 09:00:16 +00002643 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenek65411632009-04-21 22:38:05 +00002644 ValMgr.makeZeroVal(U->getType()),
2645 getContext().IntTy);
2646
Ted Kremenek70970bf2009-06-18 22:57:13 +00002647 if (!state->assume(Constraint, true)) {
Ted Kremenek65411632009-04-21 22:38:05 +00002648 // It isn't feasible for the original value to be null.
2649 // Propagate this constraint.
Zhongxing Xuc890e332009-05-20 09:00:16 +00002650 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenek65411632009-04-21 22:38:05 +00002651 ValMgr.makeZeroVal(U->getType()),
2652 getContext().IntTy);
2653
Ted Kremenek70970bf2009-06-18 22:57:13 +00002654 state = state->assume(Constraint, false);
2655 assert(state);
Ted Kremenek65411632009-04-21 22:38:05 +00002656 }
2657 }
2658 }
Ted Kremenek607415e2009-03-20 20:10:45 +00002659
Ted Kremenek70970bf2009-06-18 22:57:13 +00002660 state = state->bindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002661
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002662 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002663 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002664 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002665 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002666}
2667
Ted Kremenek31803c32008-03-17 21:11:24 +00002668void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2669 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2670}
2671
2672void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2673 AsmStmt::outputs_iterator I,
2674 AsmStmt::outputs_iterator E,
2675 NodeTy* Pred, NodeSet& Dst) {
2676 if (I == E) {
2677 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2678 return;
2679 }
2680
2681 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002682 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002683
2684 ++I;
2685
2686 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2687 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2688}
2689
2690void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2691 AsmStmt::inputs_iterator I,
2692 AsmStmt::inputs_iterator E,
2693 NodeTy* Pred, NodeSet& Dst) {
2694 if (I == E) {
2695
2696 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002697 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002698
2699 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2700 // which interprets the inline asm and stores proper results in the
2701 // outputs.
2702
Ted Kremeneke66ba682009-02-13 01:45:31 +00002703 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002704
2705 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2706 OE = A->end_outputs(); OI != OE; ++OI) {
2707
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002708 SVal X = state->getSVal(*OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002709 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002710
Zhongxing Xu097fc982008-10-17 05:57:07 +00002711 if (isa<Loc>(X))
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002712 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002713 }
2714
Ted Kremeneke66ba682009-02-13 01:45:31 +00002715 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002716 return;
2717 }
2718
2719 NodeSet Tmp;
2720 Visit(*I, Pred, Tmp);
2721
2722 ++I;
2723
2724 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2725 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2726}
2727
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002728void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2729 assert (Builder && "GRStmtNodeBuilder must be defined.");
2730
2731 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002732
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002733 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2734 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002735
Ted Kremenekc7469542008-07-17 23:15:45 +00002736 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002737
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002738 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002739
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002740 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002741 MakeNode(Dst, S, Pred, GetState(Pred));
2742}
2743
Ted Kremenek108048c2008-03-31 15:02:58 +00002744void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2745
2746 Expr* R = S->getRetValue();
2747
2748 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002749 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002750 return;
2751 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002752
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002753 NodeSet Tmp;
2754 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002755
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002756 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002757 SVal X = (*I)->getState()->getSVal(R);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002758
2759 // Check if we return the address of a stack variable.
2760 if (isa<loc::MemRegionVal>(X)) {
2761 // Determine if the value is on the stack.
2762 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002763
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002764 if (R && getStateManager().hasStackStorage(R)) {
2765 // Create a special node representing the error.
2766 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2767 N->markAsSink();
2768 RetsStackAddr.insert(N);
2769 }
2770 continue;
2771 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002772 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002773 // Check if we return an undefined value.
2774 else if (X.isUndef()) {
2775 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2776 N->markAsSink();
2777 RetsUndef.insert(N);
2778 }
2779 continue;
2780 }
2781
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002782 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002783 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002784}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002785
Ted Kremenekca5f6202008-04-15 23:06:53 +00002786//===----------------------------------------------------------------------===//
2787// Transfer functions: Binary operators.
2788//===----------------------------------------------------------------------===//
2789
Ted Kremeneke66ba682009-02-13 01:45:31 +00002790const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002791 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002792
2793 // Divide by undefined? (potentially zero)
2794
2795 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002796 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002797
2798 if (DivUndef) {
2799 DivUndef->markAsSink();
2800 ExplicitBadDivides.insert(DivUndef);
2801 }
2802
Ted Kremenek6c438f82008-10-20 23:40:25 +00002803 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002804 }
2805
2806 // Check for divide/remainder-by-zero.
2807 // First, "assume" that the denominator is 0 or undefined.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002808 const GRState* zeroState = state->assume(Denom, false);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002809
2810 // Second, "assume" that the denominator cannot be 0.
Ted Kremenek70970bf2009-06-18 22:57:13 +00002811 state = state->assume(Denom, true);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002812
Ted Kremenek70970bf2009-06-18 22:57:13 +00002813 // Create the node for the divide-by-zero (if it occurred).
2814 if (zeroState)
2815 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, zeroState, Pred)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002816 DivZeroNode->markAsSink();
2817
Ted Kremenek70970bf2009-06-18 22:57:13 +00002818 if (state)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002819 ImplicitBadDivides.insert(DivZeroNode);
2820 else
2821 ExplicitBadDivides.insert(DivZeroNode);
2822
2823 }
2824
Ted Kremenek70970bf2009-06-18 22:57:13 +00002825 return state;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002826}
2827
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002828void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002829 GRExprEngine::NodeTy* Pred,
2830 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002831
2832 NodeSet Tmp1;
2833 Expr* LHS = B->getLHS()->IgnoreParens();
2834 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002835
Ted Kremenek52510d82008-12-06 02:39:30 +00002836 // FIXME: Add proper support for ObjCKVCRefExpr.
2837 if (isa<ObjCKVCRefExpr>(LHS)) {
2838 Visit(RHS, Pred, Dst);
2839 return;
2840 }
2841
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002842 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002843 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002844 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002845 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002846
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002847 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002848
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002849 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002850
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002851 // Process the RHS.
2852
2853 NodeSet Tmp2;
2854 Visit(RHS, *I1, Tmp2);
2855
2856 // With both the LHS and RHS evaluated, process the operation itself.
2857
2858 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002859
Ted Kremeneke66ba682009-02-13 01:45:31 +00002860 const GRState* state = GetState(*I2);
2861 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002862
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002863 SVal RightV = state->getSVal(RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002864 BinaryOperator::Opcode Op = B->getOpcode();
2865
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002866 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002867
Ted Kremenekf031b872008-01-23 19:59:44 +00002868 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002869
Ted Kremenekd4676512008-03-12 21:45:47 +00002870 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002871 // FIXME: Handle structs.
2872 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002873
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002874 if ((RightV.isUnknown() ||
2875 !getConstraintManager().canReasonAbout(RightV))
2876 && (Loc::IsLocType(T) ||
2877 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00002878 unsigned Count = Builder->getCurrentBlockCount();
2879 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002880 }
2881
Ted Kremenekd4676512008-03-12 21:45:47 +00002882 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002883 // to the L-Value represented by the LHS.
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002884 EvalStore(Dst, B, LHS, *I2, state->bindExpr(B, RightV), LeftV,
Ted Kremeneke66ba682009-02-13 01:45:31 +00002885 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002886 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002887 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002888
2889 case BinaryOperator::Div:
2890 case BinaryOperator::Rem:
2891
Ted Kremenek6c438f82008-10-20 23:40:25 +00002892 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002893 if (RHS->getType()->isIntegerType() &&
2894 RHS->getType()->isScalarType()) {
2895
Ted Kremeneke66ba682009-02-13 01:45:31 +00002896 state = CheckDivideZero(B, state, *I2, RightV);
2897 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002898 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002899
2900 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002901
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002902 default: {
2903
2904 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002905 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002906
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002907 // Process non-assignements except commas or short-circuited
2908 // logical expressions (LAnd and LOr).
Ted Kremenek07baa252008-02-21 18:02:17 +00002909
Zhongxing Xuc890e332009-05-20 09:00:16 +00002910 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002911
2912 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002913 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002914 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002915 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002916 }
2917 else
2918 Dst.Add(*I2);
2919
Ted Kremenekb8782e12008-02-21 19:15:37 +00002920 continue;
2921 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002922
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002923 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002924
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002925 // The operands were *not* undefined, but the result is undefined.
2926 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002927
Ted Kremeneke66ba682009-02-13 01:45:31 +00002928 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002929 UndefNode->markAsSink();
2930 UndefResults.insert(UndefNode);
2931 }
2932
2933 continue;
2934 }
2935
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002936 // Otherwise, create a new node.
2937
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002938 MakeNode(Dst, B, *I2, state->bindExpr(B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002939 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002940 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002941 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002942
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002943 assert (B->isCompoundAssignmentOp());
2944
Ted Kremenek570882a2009-02-07 00:52:24 +00002945 switch (Op) {
2946 default:
2947 assert(0 && "Invalid opcode for compound assignment.");
2948 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2949 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2950 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2951 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2952 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2953 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2954 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2955 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2956 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2957 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00002958 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002959
2960 // Perform a load (the LHS). This performs the checks for
2961 // null dereferences, and so on.
2962 NodeSet Tmp3;
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002963 SVal location = state->getSVal(LHS);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002964 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002965
2966 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2967
Ted Kremeneke66ba682009-02-13 01:45:31 +00002968 state = GetState(*I3);
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002969 SVal V = state->getSVal(LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002970
Ted Kremenek6c438f82008-10-20 23:40:25 +00002971 // Check for divide-by-zero.
2972 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00002973 && RHS->getType()->isIntegerType()
2974 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002975
2976 // CheckDivideZero returns a new state where the denominator
2977 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002978 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002979
Ted Kremeneke66ba682009-02-13 01:45:31 +00002980 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00002981 continue;
2982 }
2983
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002984 // Propagate undefined values (left-side).
2985 if (V.isUndef()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002986 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002987 continue;
2988 }
2989
2990 // Propagate unknown values (left and right-side).
2991 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremenek4ea6a182009-06-19 17:10:32 +00002992 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, UnknownVal()),
Ted Kremeneke66ba682009-02-13 01:45:31 +00002993 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002994 continue;
2995 }
2996
2997 // At this point:
2998 //
2999 // The LHS is not Undef/Unknown.
3000 // The RHS is not Unknown.
3001
3002 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00003003 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003004 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00003005
3006 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3007 CLHSTy = getContext().getCanonicalType(CTy);
3008
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003009 QualType LTy = getContext().getCanonicalType(LHS->getType());
3010 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00003011
3012 // Promote LHS.
3013 V = EvalCast(V, CLHSTy);
3014
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003015 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00003016 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00003017 // Propagate undefined values (right-side).
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003018 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003019 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003020 continue;
3021 }
3022
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003023 // Compute the result of the operation.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003024 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3025 B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003026
3027 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003028 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00003029 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003030 UndefNode->markAsSink();
3031 UndefResults.insert(UndefNode);
3032 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003033 continue;
3034 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003035
3036 // EXPERIMENTAL: "Conjured" symbols.
3037 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003038
3039 SVal LHSVal;
3040
Ted Kremenekd6a5a422009-03-11 02:24:48 +00003041 if ((Result.isUnknown() ||
3042 !getConstraintManager().canReasonAbout(Result))
3043 && (Loc::IsLocType(CTy)
3044 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00003045
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003046 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003047
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003048 // The symbolic value is actually for the type of the left-hand side
3049 // expression, not the computation type, as this is the value the
3050 // LValue on the LHS will bind to.
Ted Kremeneke4cb3c82009-04-09 22:22:44 +00003051 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003052
Zhongxing Xu5c70c772008-11-23 05:52:28 +00003053 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003054 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00003055 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00003056 else {
3057 // The left-hand side may bind to a different value then the
3058 // computation type.
3059 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3060 }
3061
Ted Kremenek4ea6a182009-06-19 17:10:32 +00003062 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, Result), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00003063 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00003064 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00003065 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003066 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00003067}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003068
3069//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003070// Transfer-function Helpers.
3071//===----------------------------------------------------------------------===//
3072
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003073void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003074 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +00003075 NonLoc L, NonLoc R,
Ted Kremenek74556a12009-03-26 03:35:11 +00003076 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003077
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003078 GRStateSet OStates;
Ted Kremenek74556a12009-03-26 03:35:11 +00003079 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003080
Ted Kremenekabd89ac2008-08-13 04:27:00 +00003081 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003082 MakeNode(Dst, Ex, Pred, *I);
3083}
3084
Ted Kremeneke66ba682009-02-13 01:45:31 +00003085void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek9c4ce602008-07-18 05:53:58 +00003086 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +00003087 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003088
Ted Kremeneke66ba682009-02-13 01:45:31 +00003089 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremenek74556a12009-03-26 03:35:11 +00003090 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003091}
3092
Zhongxing Xuc890e332009-05-20 09:00:16 +00003093SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3094 SVal L, SVal R, QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00003095
3096 if (L.isUndef() || R.isUndef())
3097 return UndefinedVal();
3098
3099 if (L.isUnknown() || R.isUnknown())
3100 return UnknownVal();
3101
3102 if (isa<Loc>(L)) {
3103 if (isa<Loc>(R))
3104 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3105 else
Zhongxing Xuc890e332009-05-20 09:00:16 +00003106 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek4281e622009-01-30 19:27:39 +00003107 }
3108
3109 if (isa<Loc>(R)) {
3110 // Support pointer arithmetic where the increment/decrement operand
3111 // is on the left and the pointer on the right.
3112
3113 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3114
3115 // Commute the operands.
Zhongxing Xuc890e332009-05-20 09:00:16 +00003116 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek4281e622009-01-30 19:27:39 +00003117 }
3118 else
3119 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremenek74556a12009-03-26 03:35:11 +00003120 cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00003121}
3122
Ted Kremenekfa81dff2008-07-17 21:27:31 +00003123//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00003124// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003125//===----------------------------------------------------------------------===//
3126
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003127#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003128static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003129static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00003130
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003131namespace llvm {
3132template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003133struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003134 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00003135
Ted Kremeneka853de62008-02-14 22:54:53 +00003136 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3137
3138 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00003139 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00003140 GraphPrintCheckerState->isUndefDeref(N) ||
3141 GraphPrintCheckerState->isUndefStore(N) ||
3142 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00003143 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3144 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00003145 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00003146 GraphPrintCheckerState->isBadCall(N) ||
3147 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00003148 return "color=\"red\",style=\"filled\"";
3149
Ted Kremenekc2d07202008-02-28 20:32:03 +00003150 if (GraphPrintCheckerState->isNoReturnCall(N))
3151 return "color=\"blue\",style=\"filled\"";
3152
Ted Kremeneka853de62008-02-14 22:54:53 +00003153 return "";
3154 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003155
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003156 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003157 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003158
3159 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003160 ProgramPoint Loc = N->getLocation();
3161
3162 switch (Loc.getKind()) {
3163 case ProgramPoint::BlockEntranceKind:
3164 Out << "Block Entrance: B"
3165 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3166 break;
3167
3168 case ProgramPoint::BlockExitKind:
3169 assert (false);
3170 break;
3171
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003172 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003173 if (isa<PostStmt>(Loc)) {
3174 const PostStmt& L = cast<PostStmt>(Loc);
3175 Stmt* S = L.getStmt();
3176 SourceLocation SLoc = S->getLocStart();
3177
3178 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3179 llvm::raw_os_ostream OutS(Out);
3180 S->printPretty(OutS);
3181 OutS.flush();
3182
3183 if (SLoc.isFileID()) {
3184 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003185 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3186 << " col="
3187 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3188 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003189 }
3190
Ted Kremenek0441f112009-05-07 18:27:16 +00003191 if (isa<PostLoad>(Loc))
3192 Out << "\\lPostLoad\\l;";
3193 else if (isa<PostStore>(Loc))
3194 Out << "\\lPostStore\\l";
3195 else if (isa<PostLValue>(Loc))
3196 Out << "\\lPostLValue\\l";
3197 else if (isa<PostLocationChecksSucceed>(Loc))
3198 Out << "\\lPostLocationChecksSucceed\\l";
3199 else if (isa<PostNullCheckFailed>(Loc))
3200 Out << "\\lPostNullCheckFailed\\l";
3201
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003202 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3203 Out << "\\|Implicit-Null Dereference.\\l";
3204 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3205 Out << "\\|Explicit-Null Dereference.\\l";
3206 else if (GraphPrintCheckerState->isUndefDeref(N))
3207 Out << "\\|Dereference of undefialied value.\\l";
3208 else if (GraphPrintCheckerState->isUndefStore(N))
3209 Out << "\\|Store to Undefined Loc.";
3210 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3211 Out << "\\|Explicit divide-by zero or undefined value.";
3212 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3213 Out << "\\|Implicit divide-by zero or undefined value.";
3214 else if (GraphPrintCheckerState->isUndefResult(N))
3215 Out << "\\|Result of operation is undefined.";
3216 else if (GraphPrintCheckerState->isNoReturnCall(N))
3217 Out << "\\|Call to function marked \"noreturn\".";
3218 else if (GraphPrintCheckerState->isBadCall(N))
3219 Out << "\\|Call to NULL/Undefined.";
3220 else if (GraphPrintCheckerState->isUndefArg(N))
3221 Out << "\\|Argument in call is undefined";
3222
3223 break;
3224 }
3225
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003226 const BlockEdge& E = cast<BlockEdge>(Loc);
3227 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3228 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003229
3230 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003231
3232 SourceLocation SLoc = T->getLocStart();
3233
Ted Kremenek90960972008-01-30 23:03:39 +00003234 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003235
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003236 llvm::raw_os_ostream OutS(Out);
3237 E.getSrc()->printTerminator(OutS);
3238 OutS.flush();
Ted Kremenek90960972008-01-30 23:03:39 +00003239
Ted Kremenekf97c6682008-03-09 03:30:59 +00003240 if (SLoc.isFileID()) {
3241 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003242 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3243 << " col="
3244 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003245 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003246
Ted Kremenekaee121c2008-02-13 23:08:21 +00003247 if (isa<SwitchStmt>(T)) {
3248 Stmt* Label = E.getDst()->getLabel();
3249
3250 if (Label) {
3251 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3252 Out << "\\lcase ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003253 llvm::raw_os_ostream OutS(Out);
3254 C->getLHS()->printPretty(OutS);
3255 OutS.flush();
3256
Ted Kremenekaee121c2008-02-13 23:08:21 +00003257 if (Stmt* RHS = C->getRHS()) {
3258 Out << " .. ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003259 RHS->printPretty(OutS);
3260 OutS.flush();
Ted Kremenekaee121c2008-02-13 23:08:21 +00003261 }
3262
3263 Out << ":";
3264 }
3265 else {
3266 assert (isa<DefaultStmt>(Label));
3267 Out << "\\ldefault:";
3268 }
3269 }
3270 else
3271 Out << "\\l(implicit) default:";
3272 }
3273 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003274 // FIXME
3275 }
3276 else {
3277 Out << "\\lCondition: ";
3278 if (*E.getSrc()->succ_begin() == E.getDst())
3279 Out << "true";
3280 else
3281 Out << "false";
3282 }
3283
3284 Out << "\\l";
3285 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003286
Ted Kremenekb31af242008-02-28 09:25:22 +00003287 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3288 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003289 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003290 }
3291 }
3292
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003293 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003294
Ted Kremenek18a636d2009-06-18 01:23:53 +00003295 const GRState *state = N->getState();
3296 state->printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003297
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003298 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003299 return Out.str();
3300 }
3301};
3302} // end llvm namespace
3303#endif
3304
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003305#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003306template <typename ITERATOR>
3307GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3308
3309template <>
3310GRExprEngine::NodeTy*
3311GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3312 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3313 return I->first;
3314}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003315#endif
3316
3317void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003318#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003319 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003320 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003321
3322 // Flush any outstanding reports to make sure we cover all the nodes.
3323 // This does not cause them to get displayed.
3324 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3325 const_cast<BugType*>(*I)->FlushReports(BR);
3326
3327 // Iterate through the reports and get their nodes.
3328 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3329 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3330 const BugReportEquivClass& EQ = *I2;
3331 const BugReport &R = **EQ.begin();
3332 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3333 if (N) Src.push_back(N);
3334 }
3335 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003336
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003337 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003338 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003339 else {
3340 GraphPrintCheckerState = this;
3341 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003342
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003343 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003344
3345 GraphPrintCheckerState = NULL;
3346 GraphPrintSourceManager = NULL;
3347 }
3348#endif
3349}
3350
3351void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3352#ifndef NDEBUG
3353 GraphPrintCheckerState = this;
3354 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003355
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003356 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003357
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003358 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003359 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003360 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003361 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003362
Ted Kremenek428d39e2008-01-30 23:24:39 +00003363 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003364 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003365#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003366}